Serialize CMap
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Serialize CMap

  1. #1
    Join Date
    Jul 2003
    Posts
    79

    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

  2. #2
    Join Date
    Jul 2003
    Posts
    79
    Is this to obvious?

  3. #3
    Join Date
    Sep 2002
    Location
    14 39'19.65"N / 121 1'44.34"E
    Posts
    9,815
    This MSDN article explains it pretty well.

  4. #4
    Join Date
    Jul 2003
    Posts
    79
    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 );
    }
    }

  5. #5
    Join Date
    Jul 2003
    Posts
    79
    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?

  6. #6
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    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  CArchivearCUserpNewUserint nCount )
    {
           for ( 
    int i 0nCounti++, pNewUser++ )
          {
                
    // Serialize each CPerson object
               
    pNewUser->Serializear );
          }

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

  7. #7
    Join Date
    Jul 2003
    Posts
    79
    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
    }
    }

  8. #8
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    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."

  9. #9
    Join Date
    Jul 2003
    Posts
    79
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center