| 9 | | === Inline serialization === |
| | 7 | === Basic serialization === |
| | 8 | |
| | 9 | We will introduce Serialization using the !PingPong sample provided by Ariba. You will find the following code in {{{PingPongMessage.h}: |
| | 10 | |
| | 11 | {{{ |
| | 12 | #!cpp |
| | 13 | #ifndef PINGPONGMESSAGES_H_ |
| | 14 | #define PINGPONGMESSAGES_H_ |
| | 15 | |
| | 16 | #include <string> |
| | 17 | #include "ariba/ariba.h" |
| | 18 | |
| | 19 | using namespace ariba; |
| | 20 | using std::string; |
| | 21 | |
| | 22 | namespace ariba { |
| | 23 | namespace application { |
| | 24 | namespace pingpong { |
| | 25 | |
| | 26 | using_serialization; |
| | 27 | |
| | 28 | class PingPongMessage : public Message { |
| | 29 | VSERIALIZEABLE; |
| | 30 | public: |
| | 31 | PingPongMessage(); |
| | 32 | PingPongMessage( uint8_t _id, string name = string("<ping>") ); |
| | 33 | virtual ~PingPongMessage(); |
| | 34 | |
| | 35 | string info(); |
| | 36 | uint8_t getid(); |
| | 37 | |
| | 38 | inline string getName() const { |
| | 39 | return name; |
| | 40 | } |
| | 41 | private: |
| | 42 | uint8_t id; |
| | 43 | string name; |
| | 44 | }; |
| | 45 | |
| | 46 | }}} // namespace ariba, appplication , pingpong |
| | 47 | |
| | 48 | sznBeginDefault( ariba::application::pingpong::PingPongMessage, X ) { |
| | 49 | X && id && T(name); |
| | 50 | } sznEnd(); |
| | 51 | |
| | 52 | #endif /* PINGPONGMESSAGES_H_ */ |
| | 53 | }}} |
| | 54 | |
| | 55 | The message format for communication between Ariba !PingPong instances is defined in the class {{{PingPongMessage}}}. It inherits from the {{{Message}}} class and uses the macro {{{VSERIALIZEABLE;}}} to declare this class as being able to serialize and deserialize itself. Note, that each such class must provide a default constructor that takes no arguments. The !PingPongMessage defines two properties {{{id}}} and {{{name}}} that it wants to communicate to a remote instance. |
| | 56 | |
| | 57 | To define the actual serialization code, use the following code as template: |
| | 58 | |
| | 59 | {{{ |
| | 60 | #!cpp |
| | 61 | sznBeginDefault( CLASSNAME, X ) { |
| | 62 | X && SERIALIZATION-VARIABLE-1 && SERIALIZATION_VARIABLE-2 && ...; |
| | 63 | } sznEnd(); |
| | 64 | }}} |
| | 65 | |
| | 66 | As you can see above, CLASSNAME is in the case of the !PingPong sample the complate namespace with the class name {{{ariba::application::pingpong::PingPongMessage}}}. Furthermore the variables that are meant for serialization are combined to the {{{X}}} using the {{{&&}}} operator. This operator is invoked both when serializing and when deserializing. Therefore, this is the only special code handling required. |
| | 67 | |
| | 68 | In your {*.cpp} class you require a further macro. In case of the !PingPong sample this is simply: |
| | 69 | {{{ |
| | 70 | #!cpp |
| | 71 | vsznDefault(PingPongMessage); |
| | 72 | }}} |
| | 73 | |
| | 74 | |