Solved

Serialization of CObjects

Posted on 1998-10-17
1
369 Views
Last Modified: 2013-11-20
I have a CObLists of CObjects called Leagues.  Each leage has a CObList of other CObjects called teams.  And each one of those objects has a CObList of CObjects called players.  How can I implement serialization so that the whole tree of cobjects is stored into a file?  Then how do I re-read that data back into memory?  Finally, I know I must used macros in the class declarations to specify that they can be serialized. How is this done?
  Thankyou.

class  Player : public CObject{
//stuff
};
class Team : public CObject{
//stuff
   CObList PLAYERS;
};
class  League : public CObject{
//stuff
   CObList TEAMS;
};
0
Comment
Question by:nexguy
1 Comment
 
LVL 8

Accepted Solution

by:
Answers2000 earned 100 total points
Comment Utility
Firstly (you probably know this bit).  To serialize a CObject you need these steps:

(begin quote)
1. Deriving your class from CObject (or from some class derived from CObject).
2. Overriding the Serialize member function.
3. Using the DECLARE_SERIAL macro in the class declaration.
4. Defining a constructor that takes no arguments.
5. Using the IMPLEMENT_SERIAL macro in the implementation file for your class.
(end quote)

Okay here are the specifics for you:
A. Make Player Serialize its data (you know how to do this right) with the above steps.

B. Make Team Serializable - do all normal steps.  For overloaded Serialize function do this
i. Call base class
ii. Check the archive variable IsLoading() member (to tell if loading or saving)
iii. If saving (flag is false)
- write out number of items:
ar << PLAYERS.GetCount() ;
- then step through the list using GetHeadPosition/GetNext and archive each player using "ar <<"
iv. If loading (flag is true)
- clear the obj list
- get the number of items
int nCount ;
ar >> nCount ;
- then loop nCount times, each time, use ar >> tempPlauyer and add this to the list

C. Make League Serializable - do all normal steps.  For overloaded Serialize function do this
i. Call base class
ii. Check the archive variable IsLoading() member (to tell if loading or saving)
iii. If saving (flag is false)
- write out number of items:
ar << TEAMS.GetCount() ;
- then step through the list using GetHeadPosition/GetNext and archive each team
iv. If loading (flag is true)
- clear the obj list
- get the number of items
int nCount ;
ar >> nCount ;
- then loop nCount times, each time, use ar >> tempTeam and this to the list

D. To serialize the whole thing, use the League Serialize functions.

Each of above steps is separate stage in development so you can compile and test each step individually before moving on, OR if really brave go for the whole thing in one go!

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now