Changes between Initial Version and Version 1 of Documentation/Serialization


Ignore:
Timestamp:
Dec 16, 2008, 5:17:38 PM (15 years ago)
Author:
Christoph Mayer
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Documentation/Serialization

    v1 v1  
     1= Serialization Tutorial =
     2
     3== First Steps ==
     4
     5In this section we introduce a simple example to show how the serialization works in practice. Two
     6examples are given: First, we show how the inline serialization for small and bit-sensitive works.
     7Second, we show how serialization works with virtual classes.
     8
     9=== Inline serialization ===
     10
     11Non-virtual inline serialization is reasonable on small or bit-sensitive objects (for example IP-Adresses, Ports etc.).
     12To 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
     17USING_SERIALIZATION; /* use serialization namespaces */
     18
     19class IPv4Address : public Serializeable { SERIALIZEABLE
     20private:
     21    uint8_t a,b,c,d;
     22public:
     23    IPv4Address() {
     24        a=1;b=2;c=3;d=4;
     25    }
     26};
     27}}}
     28In the next step, a serializer can be added to this class:
     29{{{
     30#!cpp
     31SERIALIZER_BEGIN( IPv4Address, DEFAULT_V, X )
     32   X && a && b && c && d;
     33SERIALIZER_END()
     34}}}
     35for convenience or personal taste, the macros can also be replaced with
     36{{{
     37#!cpp
     38sznBeginDefault( IPv4Address, X )
     39   X && a && b && c && d;
     40sznEnd()
     41}}}
     42In 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
     45IPv4Address addr;
     46Data data = data_serialize( addr );
     47cout << data << endl;
     48}}}
     49which outputs
     50{{{
     51Binary=[01020304]
     52}}}
     53As you can see the serialization works quite straits forward. The serialized object can now again deserialized with
     54{{{
     55#!cpp
     56IPv4Address addr2;
     57data_deserialize( addr2, data );
     58}}}
     59Be aware that this kind of serialization results in highly-optimized and inlined code. To generate serializers
     60that 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