MFC Grid manual

CSerializer Class Reference

CSerializer enables you to save objects in binary form and reconstruct them in memory from a binary archive. More...

#include <Dapfor/Common/Serializer.h>

List of all members.


Public Types

enum  Mode { Store, Restore }
 Working mode. More...

Public Member Functions

 CSerializer (Mode mode)
 Constructor.
 CSerializer (const CSerializer &serializer)
 Copy constructor.
 ~CSerializer ()
 Destructor.
bool Write (void *buf, int len)
 Writes bytes to the serializer.
bool Read (void *buf, int len)
 Reads bytes from the serializer.
int SaveToBuffer (void *buffer) const
 Copies serialized data to a binary buffer.
void LoadFromBuffer (const void *buffer, int size)
 Loads data from a binary buffer.
int GetSavingSize () const
 Gets the size of the buffer needed to copy serialized data.
Mode GetMode () const
 Gets a mode.
void Clear ()
 Clears all serialized data.
CSerializeroperator= (const CSerializer &serializer)
 Copy an operator.
Insertion operators
CSerializeroperator<< (char v)
 Writes char-type value to the serializer.
CSerializeroperator<< (unsigned char v)
 Writes unsigned char-type value to the serializer.
CSerializeroperator<< (short v)
 Writes short-type value to the serializer.
CSerializeroperator<< (unsigned short v)
 Writes unsigned short-type value to the serializer.
CSerializeroperator<< (long v)
 Writes long-type value to the serializer.
CSerializeroperator<< (unsigned long v)
 Writes unsigned long-type value to the serializer.
CSerializeroperator<< (int v)
 Writes int-type value to the serializer.
CSerializeroperator<< (unsigned int v)
 Writes unsigned int-type value to the serializer.
CSerializeroperator<< (int64 v)
 Writes __int64-type value to the serializer.
CSerializeroperator<< (float v)
 Writes float-type value to the serializer.
CSerializeroperator<< (double v)
 Writes double-type value to the serializer.
CSerializeroperator<< (bool v)
 Writes bool-type value to the serializer.
CSerializeroperator<< (const CString &v)
 Writes MFC string - type value to the serializer.
CSerializeroperator<< (const TString &v)
 Writes STL string - type value to the serializer.
CSerializeroperator<< (const CValue &v)
 Writes an object of CValue class to the serializer.
CSerializeroperator<< (const CDataObject &v)
 Writes CDataObject instance to the serializer.
CSerializeroperator<< (const CFormat &v)
 Writes an object of CFormat class to the serializer.
Extraction operators
CSerializeroperator>> (char &v)
 Reads char-type value from the serializer.
CSerializeroperator>> (unsigned char &v)
 Reads unsigned char-type value from the serializer.
CSerializeroperator>> (short &v)
 Reads short-type value from the serializer.
CSerializeroperator>> (unsigned short &v)
 Reads unsigned short-type value from the serializer.
CSerializeroperator>> (long &v)
 Reads long-type value from the serializer.
CSerializeroperator>> (unsigned long &v)
 Reads unsigned long-type value from the serializer.
CSerializeroperator>> (int &v)
 Reads int-type value from the serializer.
CSerializeroperator>> (unsigned int &v)
 Reads unsigned int-type value from the serializer.
CSerializeroperator>> (int64 &v)
 Reads __int64-type value from the serializer.
CSerializeroperator>> (float &v)
 Reads float-type value from the serializer.
CSerializeroperator>> (double &v)
 Reads double-type value from the serializer.
CSerializeroperator>> (bool &v)
 Reads bool-type value from the serializer.
CSerializeroperator>> (CString &v)
 Reads MFC string - type value from the serializer.
CSerializeroperator>> (TString &v)
 Reads STL string - type value from the serializer.
CSerializeroperator>> (CValue &v)
 Reads an object of CValue class from the serializer.
CSerializeroperator>> (CDataObject &v)
 Reads CDataObject instance from the serializer.
CSerializeroperator>> (CFormat &v)
 Reads an object of CFormat class from the serializer.

Classes

struct  BadVersionException
 Thrown when an object tries to restore itself from the archive with a version greater than the maximum supported. More...
struct  NotSupportedException
 Thrown when an object doesn't support serialization. More...
struct  SerializationException
 Base class for serialization exceptions. More...

Detailed Description

CSerializer enables you to save objects in binary form and reconstruct them in memory from a binary archive.

Example
//For serialization an object should implement pairs of Get- and Set- methods.


//MyClass.h file

//This is a declaration of class, an instance of which can be serialized and deserialized to/from the binary form.
//The class has pairs of Set- and Get- methods that can be called by their identifiers. Also it has a default constructor.
class CMyClass : public Dapfor::Common::CDataObject
{
public:
    //It is better to use enumerations instead of long numeric values...
    //The grid can use the same identifiers to display values returned by 
    //functions of the class.
    enum
    {
        FidPrice,
        FidQuantity,
        FidTime,
        FidCity,
    };

    //Some cities...
    enum City
    {
        London = 100,
        Paris,
        Totonto,
        NewYork,
    };

public:
    CMyClass();
    ~CMyClass();


    //Get- methods
    double  GetPrice() const;
    __int64 GetQuantity() const;
    long    GetTime() const;
    long    GetCity() const;

    //Set- methods
    void  SetPrice(double newPrice);
    void  SetQuantity(__int64 newQuantity);
    void  SetTime(long newTime);
    void  SetCity(long newCity);


private:
    //Private fields
    double  m_Price;
    __int64 m_Quantity;
    long    m_Time;
    City    m_City;

    //Fieldmap declaration.
    DF_DECLARE_FIELD_MAP();

    //Serializable type declaration.
    DF_DECLARE_SERIALIZABLE_TYPE();
};


//MyClass.cpp file



// Enum format strings
Dapfor::Common::CLongEnumFormat::Item cities[] =
{
    {CMyClass::London,  "London"},
    {CMyClass::Paris,   "Paris"},
    {CMyClass::Totonto, "Totonto"},
    {CMyClass::NewYork, "NewYork"},
};


// Declaration of a field map.
// To put an object of CMyClass class into the grid, it is necessary to declare a table enabling calls of specified functions by identifiers. 
// Each data field should have a pair of Set and Get- functions.

DF_IMPLEMENT_SERIALIZABLE_TYPE(CMyClass)
DF_BEGIN_FIELD_MAP(CMyClass)
    DF_DOUBLE_ID(FidPrice,    "Price",    &CMyClass::GetPrice,    &CMyClass::SetPrice, 0)
    DF_INT64_ID (FidQuantity, "Quantity", &CMyClass::GetQuantity, &CMyClass::SetQuantity, 0)
    DF_LONG_ID  (FidTime,     "Time",     &CMyClass::GetTime,     &CMyClass::SetTime, new CCustomLongFormat())
    DF_LONG_ID  (FidCity,     "City",     &CMyClass::GetCity,     &CMyClass::SetCity, DF_ENUM_FORMAT(cities))
DF_END_FIELD_MAP()


CMyClass::CMyClass()
{
}

//Virtual destructor
CMyClass::~CMyClass()
{
    //Object ends the life cycle.
    NotifyDelete();
}


long CMyClass::GetCity() const
{
    return m_City;
}

double  CMyClass::GetPrice() const
{
    return m_Price;
}

__int64 CMyClass::GetQuantity() const
{
    return m_Quantity;
}

long CMyClass::GetTime() const
{
    return m_Time;
}


void  CMyClass::SetPrice(double newPrice)
{
    m_Price = newPrice;
    NotifyUpdate(FidPrice);
}

void  CMyClass::SetQuantity(__int64 newQuantity)
{
    m_Quantity = newQuantity;
    NotifyUpdate(FidQuantity);
}

void  CMyClass::SetTime(long newTime)
{
    m_Time = newTime;
    NotifyUpdate(FidTime);
}

void CMyClass::SetCity(long newCity)
{
    m_City = (City)newCity;
    NotifyUpdate(FidCity);
}





//How to serialize and deserialize a data object:

    //Create a serializer to store a data object
    CSerializer serializer1(CSerializer::Store);
    
    //Our data object
    CMyClass object1;
    object1.SetCity(CMyClass::London);
    object1.SetPrice(123.12);
    object1.SetQuantity(1234);

    //Serialize the name of the class
    serializer1 << object1.GetSerializationName();

    //Serialize the data object itself
    serializer1 << object1;

    //Copy data to the binary buffer
    long size = serializer1.GetSavingSize();
    unsigned char* buffer = new unsigned char[size];
    serializer1.SaveToBuffer(buffer);

    //Create a new serializer
    CSerializer serializer2(CSerializer::Restore);
    
    //Load data from the buffer
    serializer2.LoadFromBuffer(buffer, size);

    delete[] buffer;
    
    //Restore the class name
    std::string name;
    serializer2 >> name;
    
    //Try to create a new data object
    CDataObject* object2 = CTypeFactory::CreateObject(name);
    
    //Deserialise the object itself
    serializer2 >> *object2;

    //The object is created and deserialized.

    //To access to the data object without casting we can use field identifiers
    ASSERT(object2->GetLong(CMyClass::FidCity) == CMyClass::London);
    ASSERT(object2->GetDouble(CMyClass::FidPrice) == 123.12);
    ASSERT(object2->GetInt64(CMyClass::FidQuantity) == 1234);

    //There is another way to call methods: We can cast the type to the CMyClass. 
    //If RTTI is enabled, we can use dynamic_cast<> operator. 
    CMyClass* object3 = (CMyClass*)object2;
    ASSERT(object3->GetCity() == CMyClass::London);
    ASSERT(object3->GetPrice() == 123.12);
    ASSERT(object3->GetQuantity() == 1234);

Member Enumeration Documentation

enum Mode

Working mode.

Enumerator:
Store  Stores objects into the binary storage.
Restore  Reconstructs objects in the memory from the binary storage.


Constructor & Destructor Documentation

CSerializer ( Mode  mode  ) 

Constructor.

Parameters:
[in] mode Serialization mode

CSerializer ( const CSerializer serializer  ) 

Copy constructor.

Parameters:
[in] serializer Reference to the serializer to be copied.


Member Function Documentation

bool Write ( void *  buf,
int  len 
)

Writes bytes to the serializer.

Parameters:
[in] buf Pointer to the buffer.
[in] len Length of the buffer
Returns:
True if the data have been successfully copied from the buffer to the the serializer. Otherwize false.
Exceptions:
SerializationException 

CSerializer & operator<< ( char  v  ) 

Writes char-type value to the serializer.

Parameters:
[in] v Char type value to be serialized to the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator<< ( unsigned char  v  ) 

Writes unsigned char-type value to the serializer.

Parameters:
[in] v Unsigned char type value to be serialized to the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator<< ( short  v  ) 

Writes short-type value to the serializer.

Parameters:
[in] v Short type value to be serialized to the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator<< ( unsigned short  v  ) 

Writes unsigned short-type value to the serializer.

Parameters:
[in] v Unsigned short type value to be serialized to the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator<< ( long  v  ) 

Writes long-type value to the serializer.

Parameters:
[in] v Long type value to be serialized to the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator<< ( unsigned long  v  ) 

Writes unsigned long-type value to the serializer.

Parameters:
[in] v Unsigned long type value to be serialized to the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator<< ( int  v  ) 

Writes int-type value to the serializer.

Parameters:
[in] v Int type value to be serialized to the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator<< ( unsigned int  v  ) 

Writes unsigned int-type value to the serializer.

Parameters:
[in] v Unsigned int type value to be serialized to the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator<< ( int64  v  ) 

Writes __int64-type value to the serializer.

Parameters:
[in] v __int64 type value to be serialized to the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator<< ( float  v  ) 

Writes float-type value to the serializer.

Parameters:
[in] v Float type value to be serialized to the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator<< ( double  v  ) 

Writes double-type value to the serializer.

Parameters:
[in] v Double type value to be serialized to the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator<< ( bool  v  ) 

Writes bool-type value to the serializer.

Parameters:
[in] v Bool type value to be serialized to the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator<< ( const CString &  v  ) 

Writes MFC string - type value to the serializer.

Parameters:
[in] v MFC string type value to be serialized to the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator<< ( const TString &  v  ) 

Writes STL string - type value to the serializer.

Parameters:
[in] v STL string type value to be serialized to the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator<< ( const CValue v  ) 

Writes an object of CValue class to the serializer.

Parameters:
[in] v Object of CValue type to be serialized to the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator<< ( const CDataObject v  ) 

Writes CDataObject instance to the serializer.

Parameters:
[in] v CDataObject to be serialized to the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator<< ( const CFormat v  ) 

Writes an object of CFormat class to the serializer.

Parameters:
[in] v CFormat to be serialized to the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

bool Read ( void *  buf,
int  len 
)

Reads bytes from the serializer.

Parameters:
[in] buf Pointer to the buffer to which the data is copied.
[in] len Length of the buffer
Returns:
True if the data have been successfully copied from the serializer to the buffer. Otherwize false.
Exceptions:
SerializationException 

CSerializer & operator>> ( char &  v  ) 

Reads char-type value from the serializer.

Parameters:
[in] v Char type value that is read from the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator>> ( unsigned char &  v  ) 

Reads unsigned char-type value from the serializer.

Parameters:
[in] v Unsigned char type value that is read from the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator>> ( short &  v  ) 

Reads short-type value from the serializer.

Parameters:
[in] v Short type value that is read from the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator>> ( unsigned short &  v  ) 

Reads unsigned short-type value from the serializer.

Parameters:
[in] v Unsigned short type value that is read from the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator>> ( long &  v  ) 

Reads long-type value from the serializer.

Parameters:
[in] v Long type value that is read from the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator>> ( unsigned long &  v  ) 

Reads unsigned long-type value from the serializer.

Parameters:
[in] v Unsigned long type value that is read from the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator>> ( int &  v  ) 

Reads int-type value from the serializer.

Parameters:
[in] v Int type value that is read from the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator>> ( unsigned int &  v  ) 

Reads unsigned int-type value from the serializer.

Parameters:
[in] v Unsigned int type value that is read from the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator>> ( int64 &  v  ) 

Reads __int64-type value from the serializer.

Parameters:
[in] v __int64 type value that is read from the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator>> ( float &  v  ) 

Reads float-type value from the serializer.

Parameters:
[in] v Float type value that is read from the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator>> ( double &  v  ) 

Reads double-type value from the serializer.

Parameters:
[in] v Double type value that is read from the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator>> ( bool &  v  ) 

Reads bool-type value from the serializer.

Parameters:
[in] v Bool type value that is read from the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator>> ( CString &  v  ) 

Reads MFC string - type value from the serializer.

Parameters:
[in] v string type value that is read from the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator>> ( TString &  v  ) 

Reads STL string - type value from the serializer.

Parameters:
[in] v string type value that is read from the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator>> ( CValue v  ) 

Reads an object of CValue class from the serializer.

Parameters:
[in] v CValue type object that is read from the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator>> ( CDataObject v  ) 

Reads CDataObject instance from the serializer.

Parameters:
[in] v CDataObject that is read from the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

CSerializer & operator>> ( CFormat v  ) 

Reads an object of CFormat class from the serializer.

Parameters:
[in] v CFormat type object that is read from the serializer.
Returns:
Reference to CSerializer object.
Exceptions:
SerializationException 

int SaveToBuffer ( void *  buffer  )  const

Copies serialized data to a binary buffer.

Parameters:
[in] buffer Pointer to the buffer where serialized data is copied.
Returns:
Size of serialized data in bytes.
Exceptions:
SerializationException 

void LoadFromBuffer ( const void *  buffer,
int  maxSize 
)

Loads data from a binary buffer.

Parameters:
[in] buffer Pointer to the buffer from which serialized data is copied.
[in] maxSize Size of the buffer.
Exceptions:
SerializationException 

int GetSavingSize (  )  const

Gets the size of the buffer needed to copy serialized data.

Returns:
Size of serialized data in bytes.

CSerializer::Mode GetMode (  )  const

Gets a mode.

Returns:
Serialization mode.

CSerializer & operator= ( const CSerializer serializer  ) 

Copy an operator.

Parameters:
[in] serializer Reference to the serializer to be copied.
Returns:
Reference to 'this' CSerializer object.