Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Jan Koniarik
emlabcpp
Commits
e3965594
Commit
e3965594
authored
Jan 09, 2022
by
Jan Koniarik
Browse files
improved comments for protocol examples
parent
1759d172
Pipeline
#106158
passed with stage
in 51 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
examples/protocol.cpp
View file @
e3965594
...
@@ -13,12 +13,12 @@ int main( int, char*[] )
...
@@ -13,12 +13,12 @@ int main( int, char*[] )
// ---------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------
// protocol library allows serialization and deserialization of C++ native types into binary
// protocol library allows serialization and deserialization of C++ native types into binary
// messages. The protocol is defined with types from the library and the library provides
// messages. The protocol is defined with types from the library and the library provides
// handler that does the conversion based on th
e
definition. The definition is done using
// handler that does the conversion based on th
at
definition. The definition is done using
// types and templates and is separated from the actuall serialization and deserialization.
// types and templates and is separated from the actuall serialization and deserialization.
//
//
// There are two top level types that shall be used: protocol_tuple and
// There are two top level types that shall be used: protocol_tuple and
// protocol_command_group. The user should use these as top level type and
define the
// protocol_command_group
(variant-like)
. The user should use these as top level type and
// protocol in these. They can be nested.
//
define the
protocol in these. They can be nested.
//
//
// For the examples we define the protocol by definining structures that inherit from the
// For the examples we define the protocol by definining structures that inherit from the
// protocol definition, this is not necessary and simple aliases to types can be used, these
// protocol definition, this is not necessary and simple aliases to types can be used, these
...
@@ -28,9 +28,9 @@ int main( int, char*[] )
...
@@ -28,9 +28,9 @@ int main( int, char*[] )
// 'with_items' that make the definition more readable.
// 'with_items' that make the definition more readable.
// ---------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------
// The protocol tuple is defined by endianess
of subsequent types and with items that are
// The protocol tuple is defined by endianess
and items that are stored in the tuple.
//
stored in the tuple. The
protocol_tuple
works with
std::tuple as the type that actually
// protocol_tuple
uses
std::tuple as the type that actually
holds the value. The example
//
holds the value. The example
definition below defines protocol for converting
// definition below defines protocol for converting
// `std::tuple<uint32_t, in16_t, int16_t>` into binary message `protocol_message<8>` of
// `std::tuple<uint32_t, in16_t, int16_t>` into binary message `protocol_message<8>` of
// size 8.
// size 8.
//
//
...
@@ -41,7 +41,9 @@ int main( int, char*[] )
...
@@ -41,7 +41,9 @@ int main( int, char*[] )
};
};
// Once protocol is defined, it can be used with protocol_handler to do the serialization
// Once protocol is defined, it can be used with protocol_handler to do the serialization
// and deserialization.
// and deserialization. The materialization of code for handling the protocol happens in the
// handler itself. The compile time is costly for the handler, it is preferable to use it
// in standalone compilation unit.
using
example_tuple_handler
=
em
::
protocol_handler
<
example_tuple
>
;
using
example_tuple_handler
=
em
::
protocol_handler
<
example_tuple
>
;
std
::
tuple
<
uint32_t
,
int16_t
,
int16_t
>
tuple_val
=
{
666
,
-
2
,
2
};
std
::
tuple
<
uint32_t
,
int16_t
,
int16_t
>
tuple_val
=
{
666
,
-
2
,
2
};
...
@@ -50,6 +52,7 @@ int main( int, char*[] )
...
@@ -50,6 +52,7 @@ int main( int, char*[] )
// The library has support for streams, these however are stored in separate included file
// The library has support for streams, these however are stored in separate included file
// and has to be enable by defining EMLABCPP_USE_STREAMS
// and has to be enable by defining EMLABCPP_USE_STREAMS
std
::
cout
<<
"Message from example_tuple looks like: "
<<
tuple_msg
<<
"
\n
"
;
std
::
cout
<<
"Message from example_tuple looks like: "
<<
tuple_msg
<<
"
\n
"
;
// The message output is: |00:00:02:9a|ff:fe:00:02
// Deserialization produces either the value on successfull serialization or
// Deserialization produces either the value on successfull serialization or
// protocol_error_record with information about what failed and on which byte. Note: the
// protocol_error_record with information about what failed and on which byte. Note: the
...
@@ -114,6 +117,7 @@ int main( int, char*[] )
...
@@ -114,6 +117,7 @@ int main( int, char*[] )
em
::
protocol_message
<
22
>
group_msg
=
example_group_handler
::
serialize
(
group_val
);
em
::
protocol_message
<
22
>
group_msg
=
example_group_handler
::
serialize
(
group_val
);
std
::
cout
<<
"Message from example group looks like: "
<<
group_msg
<<
"
\n
"
;
std
::
cout
<<
"Message from example group looks like: "
<<
group_msg
<<
"
\n
"
;
// this produces: |00:01:00:00|00:2a
example_group_handler
::
extract
(
group_msg
)
example_group_handler
::
extract
(
group_msg
)
.
match
(
.
match
(
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment