Click here to Skip to main content
15,885,278 members
Articles / Desktop Programming / MFC

Serialize CArray Class/Object in C++

Rate me:
Please Sign up or sign in to vote.
2.71/5 (3 votes)
6 Feb 2007 42.2K   302   13   6
Example of how to serialize CArray Class/Object in C++

Introduction

People frequently ask how to serialize CArray object. There is a simple way to serialize CArray. You will find it very simple by reading an example.

Using the Example

This is the class for data to be serialized. I have used int, CString and char* so you can understand better.

C++
#pragma once
class SaveFile :public CObject
{
    DECLARE_SERIAL(SaveFile)
public:
    SaveFile(void);
    SaveFile(SaveFile &Sv)
    {*this=Sv;}
    ~SaveFile(void);
    CString FName;
    char *FData;
    ULONGLONG MySize;
    virtual void Serialize(CArchive &pArchive);
    SaveFile& operator=(SaveFile & SvFi);
};

This is the CPP file of SaveFile class. Use CArchive to store and load CArray from/to file.

C++
#include "StdAfx.h"
#include "SaveFile.h"

IMPLEMENT_SERIAL(SaveFile,CObject,0)
SaveFile::SaveFile(void)
{}
SaveFile::~SaveFile(void)
{}
void SaveFile::Serialize(CArchive & pArchive)
{
    CObject::Serialize(pArchive);
    if(pArchive.IsStoring())
    {    
        pArchive<<FName<<MySize;
        for(int i=0;i<MySize;i++)
        pArchive<<FData[i];
    }
    else
    {       pArchive>>FName>>MySize;
        FData=new char[MySize];
        for(int i=0;i<MySize;i++)
            pArchive>>FData[i];
    }
}

SaveFile& SaveFile::operator =(SaveFile&SvFo)
{
    FName=SvFo.FName;
    MySize=SvFo.MySize;
    FData=new char[MySize];
    for(int i=0;i<MySize;i++)
        FData[i]=SvFo.FData[i];

    return *this;
}

SaveFileHandler class is for storing/loading object of Savefile class to/from file .

In this CPP of SaveFileHandler SerializeElements<SaveFile>(CArchive& ar, SaveFile* pElements, int nCount) function is important. This is a global function which can be called from any class.

SaveFileHandler.h

C++
#pragma once
#include "SaveFile.h"

class SaveFileHandler
{
public:
    SaveFileHandler(void);
public:
    ~SaveFileHandler(void);
    void AddSomeData();
    void SerializeCArray();
    CArray<SaveFile, SaveFile&> SF;
};

SaveFileHandler.cpp

C++
 #include "StdAfx.h"
#include "SaveFileHandler.h"
#include "SaveFile.h"

SaveFileHandler::SaveFileHandler(void)
{}
SaveFileHandler::~SaveFileHandler(void)
{}
SaveFileHandler::AddSomeData()
{
    SaveFile SvFo ;
    int Length;//assuming some length
    SvFo.FData=new char[(size_t)Length]; 
    SvFo.MySize=Length;
    
    for(int i=0;i<Length;i++)
        SvFo.FData[i]=FData[i]; //assuming FData have some Data in it
    SvFo.FName=MainPath;
    this->SF.Add(SvFo);
}

SaveFileHandler::SerializeCArray()
{
    CFile SvFi("SomeFile.dhari",CFile::modeCreate|CFile::modeWrite);
    CArchive Ar(&SvFi,CArchive::store);
        this->SF.Serialize(Ar);
}

template <> inline void AFXAPI SerializeElements<SaveFile>
			(CArchive& ar, SaveFile* pElements, int nCount)
{
    for (int i=0;i < nCount; i++,pElements++) 
    {
     if(ar.IsStoring())
        {
        pElements->Serialize(ar);
        }
      else
        {
        SaveFile* pSv = new SaveFile();
                pSv->Serialize(ar);
               *pElements = *pSv;
        }
    }
}

Conclusion

Feedback is welcome and appreciated whether it is constructive or not.

History

  • 6th February, 2007: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
Pakistan Pakistan
Ali, Murtaza Tahir

http://murtazadharis.blogspot.com/

Comments and Discussions

 
Questionto add your own data change FData Pin
sunbelt579-Feb-14 13:37
sunbelt579-Feb-14 13:37 
Questionwhat about pointer Pin
ring0_zerg1-Mar-09 0:37
ring0_zerg1-Mar-09 0:37 
GeneralSmall bug - memory leaks Pin
zapspam13-Oct-08 1:01
zapspam13-Oct-08 1:01 
GeneralCompilations errors [modified] Pin
Kikoa19-Nov-07 3:30
Kikoa19-Nov-07 3:30 
GeneralRe: Compilations errors Pin
murtaza dhari19-Nov-07 19:04
murtaza dhari19-Nov-07 19:04 
GeneralAlso see... Pin
Ravi Bhavnani6-Feb-07 17:13
professionalRavi Bhavnani6-Feb-07 17:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.