Skip to content

Commit b7f19c8

Browse files
author
Vladimir Karnushin
committed
Add hello_world example
1 parent 353e7c9 commit b7f19c8

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
add_subdirectory (performance_test)
22
add_subdirectory (message_printer)
3+
add_subdirectory (hello_world)
34
# add_subdirectory (Model)

examples/hello_world/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#########################################################################
2+
# The following lines shows how to write CMakeLists.txt for mFAST applications
3+
# when the applications are not in the mFAST source tree.
4+
##########################################################################
5+
6+
# cmake_minimum_required(VERSION 2.8)
7+
# find_package(mFAST REQUIRED COMPONENTS coder)
8+
#
9+
# include_directories(${MFAST_INCLUDE_DIR})
10+
# link_directories(${MFAST_LIBRARY_DIRS})
11+
12+
add_executable (hello_world hello_world.cpp)
13+
target_link_libraries (hello_world ${MFAST_LIBRARIES})

examples/hello_world/hello_world.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
#include <mfast.h>
5+
#include <mfast/coder/fast_decoder.h>
6+
#include <mfast/json/json.h>
7+
#include <mfast/xml_parser/dynamic_templates_description.h>
8+
9+
using std::string;
10+
using std::ostringstream;
11+
using std::cout;
12+
using std::endl;
13+
14+
using mfast::templates_description;
15+
using mfast::dynamic_templates_description;
16+
using mfast::fast_decoder;
17+
using mfast::message_cref;
18+
using mfast::ascii_string_cref;
19+
using mfast::json::encode;
20+
21+
// example from http://jettekfix.com/node/36
22+
static const string fast_template =
23+
"\
24+
<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\
25+
<templates xmlns=\"http://www.fixprotocol.org/ns/fast/td/1.1\">\
26+
<template dictionary=\"1\" id=\"1\" name=\"HelloWorld\">\
27+
<string id=\"58\" name=\"Text\">\
28+
<default value=\"\"></default>\
29+
</string>\
30+
</template>\
31+
</templates>\
32+
";
33+
34+
// 58=HelloWorld<SOH>
35+
static const string fast_message =
36+
"\xE0\x81\x48\x65\x6C\x6C\x6F\x57\x6F\x72\x6C\xE4";
37+
38+
int main() {
39+
dynamic_templates_description description(fast_template);
40+
41+
const templates_description* descriptions[] = {&description};
42+
43+
fast_decoder decoder;
44+
decoder.include(descriptions);
45+
46+
const char* start = fast_message.c_str();
47+
const char* end = start + fast_message.length();
48+
49+
cout << "Decoding message \"58=HelloWorld<SOH>\":" << endl;
50+
cout << endl;
51+
52+
message_cref msg = decoder.decode(start, end);
53+
54+
cout << "Template id: " << msg.id() << endl;
55+
cout << "Template name: " << msg.name() << endl;
56+
cout << endl;
57+
58+
ascii_string_cref field = static_cast<ascii_string_cref>((msg)[0]);
59+
60+
cout << "Field id: " << field.id() << endl;
61+
cout << "Field name: " << field.name() << endl;
62+
cout << "Field content: " << field.c_str() << endl;
63+
cout << endl;
64+
65+
cout << "Encoding message to JSON:" << endl;
66+
67+
ostringstream json_message;
68+
bool result = encode(json_message, msg, 0);
69+
if (result) cout << "Success: " << json_message.str() << endl;
70+
71+
return 0;
72+
}

0 commit comments

Comments
 (0)