| 1 | = Serialization Tutorial = |
| 2 | |
| 3 | == First Steps == |
| 4 | |
| 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. |
| 8 | |
| 9 | === Inline serialization === |
| 10 | |
| 11 | Non-virtual inline serialization is reasonable on small or bit-sensitive objects (for example IP-Adresses, Ports etc.). |
| 12 | To add serialization to an object it must be inherited from the Serializable class and a specification of the SERIALIZEABLE macro: |
| 13 | {{{ |
| 14 | #!cpp |
| 15 | #include<serialization.h> |
| 16 | |
| 17 | USING_SERIALIZATION; /* use serialization namespaces */ |
| 18 | |
| 19 | class IPv4Address : public Serializeable { SERIALIZEABLE |
| 20 | private: |
| 21 | uint8_t a,b,c,d; |
| 22 | public: |
| 23 | IPv4Address() { |
| 24 | a=1;b=2;c=3;d=4; |
| 25 | } |
| 26 | }; |
| 27 | }}} |
| 28 | In the next step, a serializer can be added to this class: |
| 29 | {{{ |
| 30 | #!cpp |
| 31 | SERIALIZER_BEGIN( IPv4Address, DEFAULT_V, X ) |
| 32 | X && a && b && c && d; |
| 33 | SERIALIZER_END() |
| 34 | }}} |
| 35 | for convenience or personal taste, the macros can also be replaced with |
| 36 | {{{ |
| 37 | #!cpp |
| 38 | sznBeginDefault( IPv4Address, X ) |
| 39 | X && a && b && c && d; |
| 40 | sznEnd() |
| 41 | }}} |
| 42 | In this case we assume that an IP-Address can be mapped bijectively -- therefore no special treatment of serialization and deserialization is needed. So, what happens here is, that a inline serializer is created for class IPv4Address. DEFAULT_V specifies that this serializer is used as default variant and X specifies the variable of the stream that is used to serialize the object. To serialize this object one can use the data_serialize methods: |
| 43 | {{{ |
| 44 | #!cpp |
| 45 | IPv4Address addr; |
| 46 | Data data = data_serialize( addr ); |
| 47 | cout << data << endl; |
| 48 | }}} |
| 49 | which outputs |
| 50 | {{{ |
| 51 | Binary=[01020304] |
| 52 | }}} |
| 53 | As you can see the serialization works quite straits forward. The serialized object can now again deserialized with |
| 54 | {{{ |
| 55 | #!cpp |
| 56 | IPv4Address addr2; |
| 57 | data_deserialize( addr2, data ); |
| 58 | }}} |
| 59 | Be aware that this kind of serialization results in highly-optimized and inlined code. To generate serializers |
| 60 | that are not-inlined and generated once per class, in the next section we discuss virtual serialization. |
| 61 | |
| 62 | === Virtual serialization === |
| 63 | |
| 64 | == Differentiation between serialization and deserialization == |
| 65 | |
| 66 | == Serialization of bits and special types == |
| 67 | |
| 68 | == Message specification, serialization and deserialization == |
| 69 | |
| 70 | |