From b7f19c8601d7b24f18a5022ec6ab8cac0e214de7 Mon Sep 17 00:00:00 2001 From: Vladimir Karnushin Date: Mon, 26 Dec 2016 09:33:22 +0300 Subject: [PATCH] Add hello_world example --- examples/CMakeLists.txt | 1 + examples/hello_world/CMakeLists.txt | 13 +++++ examples/hello_world/hello_world.cpp | 72 ++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 examples/hello_world/CMakeLists.txt create mode 100644 examples/hello_world/hello_world.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 50bc28b3..e535df58 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory (performance_test) add_subdirectory (message_printer) +add_subdirectory (hello_world) # add_subdirectory (Model) diff --git a/examples/hello_world/CMakeLists.txt b/examples/hello_world/CMakeLists.txt new file mode 100644 index 00000000..81305362 --- /dev/null +++ b/examples/hello_world/CMakeLists.txt @@ -0,0 +1,13 @@ +######################################################################### +# The following lines shows how to write CMakeLists.txt for mFAST applications +# when the applications are not in the mFAST source tree. +########################################################################## + +# cmake_minimum_required(VERSION 2.8) +# find_package(mFAST REQUIRED COMPONENTS coder) +# +# include_directories(${MFAST_INCLUDE_DIR}) +# link_directories(${MFAST_LIBRARY_DIRS}) + +add_executable (hello_world hello_world.cpp) +target_link_libraries (hello_world ${MFAST_LIBRARIES}) diff --git a/examples/hello_world/hello_world.cpp b/examples/hello_world/hello_world.cpp new file mode 100644 index 00000000..082e2be6 --- /dev/null +++ b/examples/hello_world/hello_world.cpp @@ -0,0 +1,72 @@ +#include +#include + +#include +#include +#include +#include + +using std::string; +using std::ostringstream; +using std::cout; +using std::endl; + +using mfast::templates_description; +using mfast::dynamic_templates_description; +using mfast::fast_decoder; +using mfast::message_cref; +using mfast::ascii_string_cref; +using mfast::json::encode; + +// example from http://jettekfix.com/node/36 +static const string fast_template = + "\ +\ +\ + \ +\ +"; + +// 58=HelloWorld +static const string fast_message = + "\xE0\x81\x48\x65\x6C\x6C\x6F\x57\x6F\x72\x6C\xE4"; + +int main() { + dynamic_templates_description description(fast_template); + + const templates_description* descriptions[] = {&description}; + + fast_decoder decoder; + decoder.include(descriptions); + + const char* start = fast_message.c_str(); + const char* end = start + fast_message.length(); + + cout << "Decoding message \"58=HelloWorld\":" << endl; + cout << endl; + + message_cref msg = decoder.decode(start, end); + + cout << "Template id: " << msg.id() << endl; + cout << "Template name: " << msg.name() << endl; + cout << endl; + + ascii_string_cref field = static_cast((msg)[0]); + + cout << "Field id: " << field.id() << endl; + cout << "Field name: " << field.name() << endl; + cout << "Field content: " << field.c_str() << endl; + cout << endl; + + cout << "Encoding message to JSON:" << endl; + + ostringstream json_message; + bool result = encode(json_message, msg, 0); + if (result) cout << "Success: " << json_message.str() << endl; + + return 0; +}