Click here to Skip to main content
Click here to Skip to main content

Serialize CArray Class/Object in C++

By , 6 Feb 2007
Rate this:
Please Sign up or sign in to vote.

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.

#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.

#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

#pragma once
#include "SaveFile.h"

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

SaveFileHandler.cpp

 #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

About the Author

murtaza dhari
Software Developer
Pakistan Pakistan
Ali, Murtaza Tahir
 
http://murtazadharis.blogspot.com/

Comments and Discussions

 
Questionto add your own data change FData Pinmembersunbelt579-Feb-14 13:37 
Questionwhat about pointer Pinmemberring0_zerg1-Mar-09 0:37 
GeneralSmall bug - memory leaks Pinmemberzapspam13-Oct-08 1:01 
GeneralCompilations errors [modified] PinmemberKikoa19-Nov-07 3:30 
GeneralRe: Compilations errors Pinmembermurtaza dhari19-Nov-07 19:04 
GeneralAlso see... PinmemberRavi Bhavnani6-Feb-07 17:13 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 6 Feb 2007
Article Copyright 2007 by murtaza dhari
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid