Object Serialization in NMM

Motama GmbH, Saarbruecken, Germany

http://www.motama.com

April 2007

  Copyright (C) 2007,
  Motama GmbH, Saarbruecken, Germany
  http://www.motama.com
 
  Permission is granted to copy, distribute and/or modify this
  document under the terms of the GNU Free Documentation License,
  Version 1.2 or any later version published by the Free Software
  Foundation; with the Invariant Sections being all sections, no
  Front-Cover Texts, and no Back-Cover Texts. A copy of the license
  can be found in the file COPYING.FDL.

  THE DOCUMENT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
  NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE
  DISTRIBUTING THE DOCUMENT BE LIABLE FOR ANY DAMAGES OR OTHER
  LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
  OF OR IN CONNECTION WITH THE DOCUMENT OR THE USE OR OTHER DEALINGS
  IN THE DOCUMENT.


Table of Contents
1. Introduction
2. ValueStreams
2.1. Extensions of OValueStream
2.2. Extensions of IValueStream
3. Object Serialization with ValueStreams
3.1. Usage of C++ Operators
3.1.1. Operator<<
3.1.2. Operator>>
3.1.3. Inheritance
3.2. Class SerializedValue
3.2.1. Inheritance
4. ValueFactrories and Dynamic Object Allocation
4.1. Serialization of a Pointer
4.2. Deserialization of a Pointer

1. Introduction

Whenever an application needs to save objects in a file or send it over a network it must serialize these objects to a formated byte stream that stores all attributes. If the application reads the byte stream again it creates a corresponding object and sets the parameter or the object itself reads the attributes. If more than one object type is used a unique type-id is need to recreate the correct object from byte stream.

For i/o facilities C++ uses different kind of streams like Io-streams for standard input/output or fstreams for files. A stream offers operators (normally the operator<< to write and operator>> to read from stream) to read/write the generic data-types. These predefined operators can be extended with additional operators for user defined data-types. A disadvantage of standard C++ streams is the lack of type information. Objects written to a stream loose any type information and if an object is recreated the stream can not be asked for type of the actual element. So the application developer must write some kind of 'magic number' to identify elements written to a stream. Therefore NMM uses so called ValueStreams which stores the type information of each element. These type information are used for a runtime type safe object serialization/deserialization.

In this context we use the following class as example to show the use of ValueStreams.

namespace MyNamespace {
  class MyStruct {
  public:
                     MyStruct();
                     MyStruct(const MyStruct&);

        string&      getName()            {return __name;}
  const string&      getName()      const {return __name;}

  const list<float>& getValueList() const {return __value_list;}
        list<float>& getValueList()       {return __value_list;}
        int          getCounter()   const {return __counter;}
        void         setCounter(int i)    {__counter = i;}

  private: //The attributes
    string      __name;
    int         __counter;
    list<float> __value_list;
  };
}
      
Section 2 describes the ValueStreams and their specific extensions. Section 3 shows the use of ValueStreams as well as some NMM specific classes that simplify object serialization.