home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / advanced / collect / colledoc.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  7KB  |  295 lines

  1. // colledoc.cpp : implementation of the CCollectDoc class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "collect.h"
  15.  
  16. #include "colledoc.h"
  17. #include "resource.h"
  18.  
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char BASED_CODE THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CMyStruct
  26.  
  27. void CMyStruct::FormatMyStruct(CString& str)
  28. {
  29.     str.Format(_T("{%i, %.4f, %s}"), m_int, m_float, (LPCTSTR)m_str);
  30. }
  31.  
  32. #if _MSC_VER > 1020
  33. template <> void AFXAPI SerializeElements<CMyStruct*>(CArchive& ar, CMyStruct** ppElements, int nCount)
  34. #else
  35. void SerializeElements(CArchive& ar, CMyStruct** ppElements, int nCount)
  36. #endif
  37. {
  38.     // Since SerializeElements is always called by the framework with nCount=1
  39.     // for a CMap<>, it is a good idea to implement SerializeElement to handle
  40.     // nCount>1, in case you decide to reuse it for a CArray<> with the same
  41.     // element type.
  42.     if (ar.IsStoring())
  43.     {
  44.         for (int i = 0; i < nCount; i++)
  45.         {
  46.             CMyStruct* pMyStruct = *(ppElements + i);
  47.             WORD w = (WORD)pMyStruct->m_int;
  48.             ar << w;
  49.             ar << pMyStruct->m_float;
  50.             ar << pMyStruct->m_str;
  51.             nCount--;
  52.         }
  53.     }
  54.     else
  55.     {
  56.         for (int i = 0; i < nCount; i++)
  57.         {
  58.             CMyStruct* pMyStruct = new CMyStruct;
  59.             *(ppElements + i) = pMyStruct;
  60.             WORD w;
  61.             ar >> w;
  62.             pMyStruct->m_int = w;
  63.             ar >> pMyStruct->m_float;
  64.             ar >> pMyStruct->m_str;
  65.         }
  66.     }
  67. }
  68.  
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CMyObject
  71.  
  72. IMPLEMENT_SERIAL(CMyObject, CObject, 0)
  73.  
  74. CMyObject::CMyObject()
  75. {
  76. }
  77.  
  78. CMyObject::~CMyObject()
  79. {
  80. }
  81.  
  82. void CMyObject::FormatMyObject(CString& str)
  83. {
  84.     str.Format(_T("{%i, %.4f, %s}"), m_int, m_float, (LPCTSTR)m_str);
  85. }
  86.  
  87. void CMyObject::Serialize(CArchive& ar)
  88. {
  89.     WORD w;
  90.     if (ar.IsStoring())
  91.     {
  92.         w = (WORD)m_int;
  93.         ar << w;
  94.         ar << m_float;
  95.         ar << m_str;
  96.     }
  97.     else
  98.     {
  99.         ar >> w;
  100.         m_int = w;
  101.         ar >> m_float;
  102.         ar >> m_str;
  103.     }
  104. }
  105.  
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CCollectDoc
  108.  
  109. IMPLEMENT_DYNCREATE(CCollectDoc, CDocument)
  110.  
  111. BEGIN_MESSAGE_MAP(CCollectDoc, CDocument)
  112.     //{{AFX_MSG_MAP(CCollectDoc)
  113.     //}}AFX_MSG_MAP
  114. END_MESSAGE_MAP()
  115.  
  116. /////////////////////////////////////////////////////////////////////////////
  117. // CCollectDoc construction/destruction
  118.  
  119. CCollectDoc::CCollectDoc()
  120. {
  121. }
  122.  
  123. CCollectDoc::~CCollectDoc()
  124. {
  125. }
  126.  
  127. BOOL CCollectDoc::OnNewDocument()
  128. {
  129.     if (!CDocument::OnNewDocument())
  130.         return FALSE;
  131.  
  132.     CString strFirst;
  133.     strFirst.LoadString(IDS_INITIAL_STRING);
  134.     m_stringList.AddTail(strFirst);
  135.  
  136.     CMyStruct* pMyStruct = new CMyStruct();
  137.     pMyStruct->m_int = 1234;
  138.     pMyStruct->m_float = 12.34f;
  139.     pMyStruct->m_str.LoadString(IDS_INITIAL_STRING);
  140.     m_mystructList.AddTail(pMyStruct);
  141.  
  142.     m_intList.AddTail(100);
  143.  
  144.     m_dwArray.Add(100000);
  145.  
  146.     CMyObject* pMyObject = new CMyObject();
  147.     pMyObject->m_int = 5678;
  148.     pMyObject->m_float = 56.78f;
  149.     pMyObject->m_str.LoadString(IDS_INITIAL_STRING);
  150.     m_myobArray.Add(pMyObject);
  151.  
  152.     CPoint pt(10,10);
  153.     m_ptArray.Add(pt);
  154.  
  155.     CString strKey, strValue;
  156.     strKey.LoadString(IDS_INITIAL_KEY);
  157.     strValue.LoadString(IDS_INITIAL_VALUE);
  158.     m_mapStringToString[strKey] = strValue;
  159.  
  160.  
  161.     CMyObject* pMyObject2 = new CMyObject();
  162.     pMyObject2->m_int = 1357;
  163.     pMyObject2->m_float = 13.57f;
  164.     pMyObject2->m_str.LoadString(IDS_INITIAL_STRING);
  165.     m_mapStringToMyObject[strKey] = pMyObject2;
  166.  
  167.     CMyStruct* pMyStruct2 = new CMyStruct();
  168.     pMyStruct2->m_int = 2468;
  169.     pMyStruct2->m_float = 24.68f;
  170.     pMyStruct2->m_str.LoadString(IDS_INITIAL_STRING);
  171.     m_mapDWordToMyStruct[100] = pMyStruct2;
  172.  
  173.     return TRUE;
  174. }
  175.  
  176. void CCollectDoc::DeleteContents()
  177. {
  178.     m_stringList.RemoveAll();
  179.  
  180.     POSITION pos = m_mystructList.GetHeadPosition();
  181.     while (pos != NULL)
  182.     {
  183.         delete m_mystructList.GetNext(pos);
  184.     }
  185.     m_mystructList.RemoveAll();
  186.  
  187.     m_intList.RemoveAll();
  188.  
  189.     m_dwArray.RemoveAll();
  190.  
  191.     for (int n = 0; n < m_myobArray.GetSize(); n++)
  192.     {
  193.         delete m_myobArray[n];
  194.     }
  195.     m_myobArray.RemoveAll();
  196.  
  197.     m_mapStringToString.RemoveAll();
  198.  
  199.     m_ptArray.RemoveAll();
  200.  
  201.     pos = m_mapStringToMyObject.GetStartPosition();
  202.     while (pos != NULL)
  203.     {
  204.         CString str;
  205.         CMyObject* pMyObject;
  206.         m_mapStringToMyObject.GetNextAssoc(pos, str, pMyObject);
  207.         delete pMyObject;
  208.     }
  209.     m_mapStringToMyObject.RemoveAll();
  210.  
  211.     pos = m_mapDWordToMyStruct.GetStartPosition();
  212.     while (pos != NULL)
  213.     {
  214.         DWORD dwKey;
  215.         CMyStruct* pMyStruct;
  216.         m_mapDWordToMyStruct.GetNextAssoc(pos, dwKey, pMyStruct);
  217.         delete pMyStruct;
  218.     }
  219.     m_mapDWordToMyStruct.RemoveAll();
  220. }
  221.  
  222. /////////////////////////////////////////////////////////////////////////////
  223. // CCollectDoc serialization
  224.  
  225. void CCollectDoc::Serialize(CArchive& ar)
  226. {
  227.     POSITION pos;
  228.     WORD nCount;
  229.     WORD w;
  230.  
  231.     m_stringList.Serialize(ar);
  232.  
  233.     if (ar.IsStoring())
  234.     {
  235.         nCount = (WORD)m_mystructList.GetCount();
  236.         ar << nCount;
  237.         pos = m_mystructList.GetHeadPosition();
  238.         while (pos != NULL)
  239.         {
  240.             CMyStruct* pMyStruct = m_mystructList.GetNext(pos);
  241.             w = (WORD)pMyStruct->m_int;
  242.             ar << w;
  243.             ar << pMyStruct->m_float;
  244.             ar << pMyStruct->m_str;
  245.             nCount--;
  246.         }
  247.         ASSERT(nCount == 0);
  248.     }
  249.     else
  250.     {
  251.         ar >> nCount;
  252.         while (nCount-- > 0)
  253.         {
  254.             CMyStruct* pMyStruct = new CMyStruct;
  255.             ar >> w;
  256.             pMyStruct->m_int = w;
  257.             ar >> pMyStruct->m_float;
  258.             ar >> pMyStruct->m_str;
  259.             m_mystructList.AddTail(pMyStruct);
  260.         }
  261.     }
  262.  
  263.     m_intList.Serialize(ar);
  264.  
  265.     m_dwArray.Serialize(ar);
  266.  
  267.     m_myobArray.Serialize(ar);
  268.  
  269.     m_ptArray.Serialize(ar);
  270.  
  271.     m_mapStringToString.Serialize(ar);
  272.  
  273.     m_mapStringToMyObject.Serialize(ar);
  274.  
  275.     m_mapDWordToMyStruct.Serialize(ar);
  276. }
  277.  
  278. /////////////////////////////////////////////////////////////////////////////
  279. // CCollectDoc diagnostics
  280.  
  281. #ifdef _DEBUG
  282. void CCollectDoc::AssertValid() const
  283. {
  284.     CDocument::AssertValid();
  285. }
  286.  
  287. void CCollectDoc::Dump(CDumpContext& dc) const
  288. {
  289.     CDocument::Dump(dc);
  290. }
  291. #endif //_DEBUG
  292.  
  293. /////////////////////////////////////////////////////////////////////////////
  294. // CCollectDoc commands
  295.