|
SObjectizer
5.5
|
Every message type in SObjectizer must be represented by a separate C++ structure or class. It meant that there should be a dedicated C++ struct or class for a message type. Something like:
This approach has significant advantages for big and long-term projects. Just because every message can be documented. It's easy to find every place where a message is used. Message's members are easily distinguishable:
And because message's members have unique names it is easy to refactor message or message processing code in the future.
Because of that the definition of message types as dedicated C++ structs/classes is the recommended way for programming with SObjectizer.
But there are situations where writing a lot of code with message definitions is just an overkill. For example in small test, samples or quick-and-dirty proof-of-concept applications. Sometimes it is hard to explain why a user should write 20 lines of messages definitions for write-and-forget test program with 10 lines of business logic inside.
To simplify writing of very small programs with SObjectizer a new template class so_5::rt::tuple_as_message_t was introduced in v.5.5.5. It allows to declare std::tuple as message. It fact, the tuple_as_message_t inherits from std::tuple. So an instance of tuple_as_message_t is just an instance of std::tuple.
With tuple_as_message_t it is possible to write like this:
The tuple_as_message_t receives at least one template parameter: a unique type tag. This tag is necessary to distinguish different messages with the same fields list. For example in that case:
To distinguish between evt_line_loaded and evt_result_found, it is necessary to specify different tag1 and tag2 types.
There are several ways to do that:
A user can define its own type tags, like:
But this way can be boring. In the most cases more easiest way is to use so_5::rt::mtag template:
There could be situations where messages must be defined in different modules. It is dangerous to use mtag because there could be overlaps in numbers. Let's see:
If processor::msg_value_found and loader::msg_line_loaded have the same parameter list then SObjectizer will see them as one type and this could be a problem. Type so_5::rt::typed_mtag could be used to solve it:
Usage of tuple_as_message_t allows to rewrite messages from the very first example like this:
Please note that this way of defining messages could be very dangerous and error prone if used in a projects with large code base.**
1.8.14