Changes between Version 1 and Version 2 of Documentation/Serialization


Ignore:
Timestamp:
Feb 4, 2010, 6:21:47 PM (15 years ago)
Author:
Christoph Mayer
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Documentation/Serialization

    v1 v2  
    33== First Steps ==
    44
    5 In this section we introduce a simple example to show how the serialization works in practice. Two
    6 examples are given: First, we show how the inline serialization for small and bit-sensitive works.
    7 Second, we show how serialization works with virtual classes.
     5In this section we introduce a simple example to show how the serialization works in practice.
    86
    9 === Inline serialization ===
     7=== Basic serialization ===
     8
     9We 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
     19using namespace ariba;
     20using std::string;
     21
     22namespace ariba {
     23namespace application {
     24namespace pingpong {
     25
     26using_serialization;
     27
     28class PingPongMessage : public Message {
     29        VSERIALIZEABLE;
     30public:
     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        }
     41private:
     42        uint8_t id;
     43        string name;
     44};
     45
     46}}} // namespace ariba, appplication , pingpong
     47
     48sznBeginDefault( ariba::application::pingpong::PingPongMessage, X ) {
     49        X && id && T(name);
     50} sznEnd();
     51
     52#endif /* PINGPONGMESSAGES_H_ */
     53}}}
     54
     55The 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
     57To define the actual serialization code, use the following code as template:
     58
     59{{{
     60#!cpp
     61sznBeginDefault( CLASSNAME, X ) {
     62        X && SERIALIZATION-VARIABLE-1 && SERIALIZATION_VARIABLE-2 && ...;
     63} sznEnd();
     64}}}
     65
     66As 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
     68In your {*.cpp} class you  require a further macro. In case of the !PingPong sample this is simply:
     69{{{
     70#!cpp
     71vsznDefault(PingPongMessage);
     72}}}
     73
     74
    1075
    1176Non-virtual inline serialization is reasonable on small or bit-sensitive objects (for example IP-Adresses, Ports etc.).