-
October 26th, 2003, 01:43 PM
#1
Serialize CMap
Trying to save and load a CMap. I thought I could just call the SerializeElements. But everybody seams to overload the SerializeElements why is that necsesary? I have not seen one single example of a call to the function.
SerializeElements takes the size of the CMap as input argument. But how do I know the size of the CMap when I load a CMap from a file?
void CGsdoc_b1Doc::Serialize(CArchive& ar)
{
int size = m_UserMap.GetCount();
if (ar.IsStoring())
{
// TODO: add storing code here
SerializeElements ( ar, &m_UserMap , size);
}
else
{
BR
-
October 28th, 2003, 12:47 PM
#2
-
October 28th, 2003, 12:50 PM
#3
This MSDN article explains it pretty well.
-
October 28th, 2003, 02:28 PM
#4
OK,
Now I have put a override function for SerializeElements in the Document implementation. SerializeElements function is called like above in the Serialize function in the Document. But when I debug the code I never end up in the override function. Have I placed the override function in the wrong file maybee?
template <> void AFXAPI SerializeElements <CUser> ( CArchive& ar, CUser* pNewUser, int nCount )
{
for ( int i = 0; i < nCount; i++, pNewUser++ )
{
// Serialize each CPerson object
pNewUser->Serialize( ar );
}
}
-
October 28th, 2003, 03:08 PM
#5
I think I should derive a class from CMap and implement the override function there. In my project I have four Maps storing different types of objects. So now I have to derive four classes from CMap or?
-
October 28th, 2003, 04:14 PM
#6
Ah.. The "Helper functions". Yet another reason not to use
MFC collection classes.
Yes, you have probably put this function in the wrong place.
PHP Code:
void AFXAPI SerializeElements ( CArchive& ar, CUser* pNewUser, int nCount )
{
for ( int i = 0; i < nCount; i++, pNewUser++ )
{
// Serialize each CPerson object
pNewUser->Serialize( ar );
}
}
Put it in the header file for CUser* right after the definition of
the class. You will probaly need to put the body in the .cpp
class for this function.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
October 31st, 2003, 03:44 AM
#7
Now it almost work when I override the SerializeElements in the Document. I have tried to put it in CUser but I got a lot of linking errors. The problem now is that when a CUser is restored by calling its Serialize() function from the SerializeElements() function I want to a add this restored CUser element to the Map. But the compiler complains that the Map is undefined though it is owned by the Document.
Also I dont understand how the next position in the Map can be reached just by incrementing a CUser pointer.
Here is the code from the Document,
void AFXAPI SerializeElements(CArchive& ar, CUser* pUser,int nCount)
{
for ( int i = 0; i < nCount; i++, pUser++ )
{
pUser->Serialize( ar ); //restore CUser object
m_UserMap[pUser->GetUserName] = *pUser;//Add User to Map
}
}
-
October 31st, 2003, 04:13 AM
#8
1. You wont get the linker errors if you just declare the
helper function in the User.h and put the function body in User.cpp
Put the helper in the CUser files.
2. You do not have to worry about adding members to the map.
if you have
CMap<whatever> m_MyMap;
then just call
m_MyMap.Serialize(). and forget about it. the archive will
reconstruct the map for you.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
November 4th, 2003, 03:57 PM
#9
Now I start to understand how to use the serialize functionallity.
It finally seams to work.
Thanks for all the help guys!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|