home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / com / allinone / collect / colledoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-03  |  7.9 KB  |  397 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. // CMyObject
  26.  
  27. IMPLEMENT_SERIAL(CMyObject, CObject, 0)
  28.  
  29. void CMyObject::Serialize(CArchive& ar)
  30. {
  31.     WORD w;
  32.     if (ar.IsStoring())
  33.     {
  34.         w = (WORD)m_int;
  35.         ar << w;
  36.         ar << m_float;
  37.         ar << (CString) m_str;
  38.     }
  39.     else
  40.     {
  41.         ar >> w;
  42.         m_int = w;
  43.         ar >> m_float;
  44.         CString str;
  45.         ar >> str;
  46.         m_str = str.AllocSysString();
  47.     }
  48. }
  49.  
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CCollectDoc
  52.  
  53. IMPLEMENT_DYNCREATE(CCollectDoc, CDocument)
  54.  
  55. BEGIN_MESSAGE_MAP(CCollectDoc, CDocument)
  56.     //{{AFX_MSG_MAP(CCollectDoc)
  57.     //}}AFX_MSG_MAP
  58. END_MESSAGE_MAP()
  59.  
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CCollectDoc construction/destruction
  62.  
  63. CCollectDoc::CCollectDoc()
  64. {
  65.     try {
  66.     IStlListsPtr m_Lists(__uuidof(CStlLists));
  67.     m_stringList = m_Lists;
  68.     m_mystructList = m_Lists;
  69.     m_intList = m_Lists;
  70.  
  71.     IStlArraysPtr m_Arrays(__uuidof(CStlArrays));
  72.     m_dwArray = m_Arrays;
  73.     m_myobArray = m_Arrays;
  74.     m_ptArray = m_Arrays;
  75.  
  76.     IStlMapsPtr m_Maps(__uuidof(CStlMaps));
  77.     m_mapStringToString = m_Maps;
  78.     m_mapStringToMyObject = m_Maps;
  79.     m_mapDWordToMyStruct = m_Maps;
  80.     } catch(_com_error& e) {
  81.         dump_com_error(e);
  82.     }
  83. }
  84.  
  85. CCollectDoc::~CCollectDoc()
  86. {
  87. }
  88.  
  89. BOOL CCollectDoc::OnNewDocument()
  90. {
  91.     if (!CDocument::OnNewDocument())
  92.         return FALSE;
  93.  
  94.     try {
  95.     CString strFirst;
  96.     strFirst.LoadString(IDS_INITIAL_STRING);
  97.     m_stringList->Add = (LPCTSTR) strFirst;
  98.  
  99.     CMyStruct* pMyStruct = new CMyStruct();
  100.     pMyStruct->m_int = 1234;
  101.     pMyStruct->m_float = 12.34f;
  102.     CString str;
  103.     str.LoadString(IDS_INITIAL_STRING);
  104.     pMyStruct->m_str = str.AllocSysString();
  105.     m_mystructList->Add = pMyStruct;
  106.  
  107.     m_intList->Add = 100;
  108.  
  109.     m_dwArray->Add = 100000;
  110.  
  111.     CMyObject* pMyObject = new CMyObject();
  112.     pMyObject->m_int = 5678;
  113.     pMyObject->m_float = 56.78f;
  114.     pMyObject->m_str = str.AllocSysString();
  115.     m_myobArray->Add = pMyObject;
  116.  
  117.     CPoint pt(10,10);
  118.     m_ptArray->Add = pt;
  119.  
  120.     CString strKey, strValue;
  121.     strKey.LoadString(IDS_INITIAL_KEY);
  122.     strValue.LoadString(IDS_INITIAL_VALUE);
  123.     m_mapStringToString->Map[(LPCTSTR) strKey] = (LPCTSTR) strValue;
  124.  
  125.  
  126.     CMyObject* pMyObject2 = new CMyObject();
  127.     pMyObject2->m_int = 1357;
  128.     pMyObject2->m_float = 13.57f;
  129.     pMyObject2->m_str = str.AllocSysString();
  130.     m_mapStringToMyObject->Map[(LPCTSTR) strKey] = pMyObject2;
  131.  
  132.     CMyStruct* pMyStruct2 = new CMyStruct();
  133.     pMyStruct2->m_int = 2468;
  134.     pMyStruct2->m_float = 24.68f;
  135.     pMyStruct2->m_str = str.AllocSysString();
  136.     m_mapDWordToMyStruct->Map[100] = pMyStruct2;
  137.     } catch(_com_error& e) {
  138.         dump_com_error(e);
  139.     }
  140.  
  141.     return TRUE;
  142. }
  143.  
  144. void CCollectDoc::DeleteContents()
  145. {
  146.     try {
  147.     m_stringList->RemoveAll();
  148.  
  149.     m_mystructList->First();
  150.     IMyStruct* pMyStruct = NULL;
  151.     while (m_mystructList->Next(&pMyStruct))
  152.         delete (CMyStruct*) pMyStruct;
  153.  
  154.     m_mystructList->RemoveAll();
  155.  
  156.     m_intList->RemoveAll();
  157.  
  158.     m_dwArray->RemoveAll();
  159.  
  160.     for (int n = 0; n < (int) m_myobArray->Count; n++)
  161.         delete (CMyObject*) m_myobArray->Array[n];
  162.  
  163.     m_myobArray->RemoveAll();
  164.  
  165.     m_mapStringToString->RemoveAll();
  166.  
  167.     m_ptArray->RemoveAll();
  168.  
  169.     m_mapStringToMyObject->First();
  170.     BSTR str;
  171.     while (m_mapStringToMyObject->Next(&str, &pMyStruct))
  172.         delete (CMyObject*) pMyStruct;
  173.  
  174.     m_mapStringToMyObject->RemoveAll();
  175.  
  176.     m_mapDWordToMyStruct->First();
  177.     DWORD dwKey;
  178.     while (m_mapDWordToMyStruct->Next(&dwKey, &pMyStruct))
  179.         delete (CMyStruct*) pMyStruct;
  180.  
  181.     m_mapDWordToMyStruct->RemoveAll();
  182.     } catch(_com_error& e) {
  183.         dump_com_error(e);
  184.     }
  185. }
  186.  
  187. /////////////////////////////////////////////////////////////////////////////
  188. // CCollectDoc serialization
  189.  
  190.  
  191. CArchive& AFXAPI operator<<(CArchive& ar, BSTR b)
  192. {
  193.     ar << (CString) b;
  194.     return ar;
  195. }
  196.  
  197. CArchive& AFXAPI operator>>(CArchive& ar, BSTR& b)
  198. {
  199.     CString str;
  200.     ar >> str;
  201.     b = str.AllocSysString();
  202.     return ar;
  203. }
  204.  
  205. CArchive& AFXAPI operator<<(CArchive& ar, IMyStruct* p)
  206. {
  207.     ar << p->m_int;
  208.     ar << p->m_float;
  209.     ar << (CString) p->m_str;
  210.  
  211.     return ar;
  212. }
  213.  
  214. CArchive& AFXAPI operator>>(CArchive& ar, IMyStruct*& p)
  215. {
  216.     p = new CMyStruct;
  217.     ar >> p->m_int;
  218.     ar >> p->m_float;
  219.     CString str;
  220.     ar >> str;
  221.     p->m_str = str.AllocSysString();
  222.     return ar;
  223. }
  224.  
  225. template <class T, class C>
  226. void Serialize(T t, C* p, CArchive& ar)
  227. {
  228.     long nCount;
  229.     if (ar.IsStoring())
  230.     {
  231.         nCount = p->Count;
  232.         ar << nCount;
  233.         p->First();
  234.         while (p->Next(&t))
  235.         {
  236.             ar << t;
  237.             nCount--;
  238.         }
  239.         ASSERT(nCount == 0);
  240.     }
  241.     else
  242.     {
  243.         ar >> nCount;
  244.         while (nCount-- > 0)
  245.         {
  246.             ar >> t;
  247.             p->Add = t;
  248.         }
  249.     }
  250. }
  251.  
  252. template <class T1, class T2, class C>
  253. void Serialize(T1 t1, T2 t2, C* p, CArchive& ar)
  254. {
  255.     long nCount;
  256.     if (ar.IsStoring())
  257.     {
  258.         nCount = p->Count;
  259.         ar << nCount;
  260.         p->First();
  261.         while (p->Next(&t1, &t2))
  262.         {
  263.             ar << t1;
  264.             ar << t2;
  265.             nCount--;
  266.         }
  267.         ASSERT(nCount == 0);
  268.     }
  269.     else
  270.     {
  271.         ar >> nCount;
  272.         while (nCount-- > 0)
  273.         {
  274.             ar >> t1;
  275.             ar >> t2;
  276.             p->Map[t1] = t2;
  277.         }
  278.     }
  279. }
  280.  
  281. // NOTE: Partial template specialization is not implemented yet.
  282. template <class C>
  283. void Serialize(C* p, CArchive& ar)
  284. {
  285.     long nCount;
  286.     IMyStruct* t;
  287.     CMyObject* t1;
  288.     if (ar.IsStoring())
  289.     {
  290.         nCount = p->Count;
  291.         ar << nCount;
  292.         p->First();
  293.         while (p->Next(&t))
  294.         {
  295.             t1 = (CMyObject*) t;
  296.             ar << (CObject*) t1;
  297.             nCount--;
  298.         }
  299.         ASSERT(nCount == 0);
  300.     }
  301.     else
  302.     {
  303.         ar >> nCount;
  304.         while (nCount-- > 0)
  305.         {
  306.             ar >> t1;
  307.             t = (IMyStruct*) t1;
  308.             p->Add = t;
  309.         }
  310.     }
  311. }
  312.  
  313. // NOTE: Partial template specialization is not implemented yet.
  314. template <class T1, class C>
  315. void Serialize2(T1 t1, C* p, CArchive& ar)
  316. {
  317.     long nCount;
  318.     IMyStruct* t;
  319.     CMyObject* t2;
  320.     if (ar.IsStoring())
  321.     {
  322.         nCount = p->Count;
  323.         ar << nCount;
  324.         p->First();
  325.         while (p->Next(&t1, &t))
  326.         {
  327.             ar << t1;
  328.             t2 = (CMyObject*) t;
  329.             ar << (CObject*) t2;
  330.             nCount--;
  331.         }
  332.         ASSERT(nCount == 0);
  333.     }
  334.     else
  335.     {
  336.         ar >> nCount;
  337.         while (nCount-- > 0)
  338.         {
  339.             ar >> t1;
  340.             ar >> t2;
  341.             t = (IMyStruct*) t2;
  342.             p->Map[t1] = t2;
  343.         }
  344.     }
  345. }
  346.  
  347. void CCollectDoc::Serialize(CArchive& ar)
  348. {
  349.     try {
  350.     BSTR str = 0;
  351.     ::Serialize(str, (IStlStringList*) m_stringList, ar);
  352.  
  353.     IMyStruct* pStr = NULL;
  354.     ::Serialize(pStr, (IStlMyStructList*) m_mystructList, ar);
  355.  
  356.     long n = 0;
  357.     ::Serialize(n, (IStlintList*) m_intList, ar);
  358.  
  359.     DWORD dw = 0;
  360.     ::Serialize(dw, (IStlDWordArray*) m_dwArray, ar);
  361.  
  362.     // NOTE: Partial template specialization is not implemented yet.
  363.     ::Serialize((IStlMyObjectArray*) m_myobArray, ar);
  364.  
  365.     CPoint pt(0, 0);
  366.     ::Serialize(pt, (IStlPointArray*) m_ptArray, ar);
  367.  
  368.     BSTR str2 = 0;
  369.     ::Serialize(str, str2, (IStlMapStringToString*) m_mapStringToString, ar);
  370.  
  371.     // NOTE: Partial template specialization is not implemented yet.
  372.     ::Serialize2(str, (IStlMapStringToMyObject*) m_mapStringToMyObject, ar);
  373.  
  374.     ::Serialize(dw, pStr, (IStlMapDWordToMyStruct*) m_mapDWordToMyStruct, ar);
  375.     } catch(_com_error& e) {
  376.         dump_com_error(e);
  377.     }
  378. }
  379.  
  380. /////////////////////////////////////////////////////////////////////////////
  381. // CCollectDoc diagnostics
  382.  
  383. #ifdef _DEBUG
  384. void CCollectDoc::AssertValid() const
  385. {
  386.     CDocument::AssertValid();
  387. }
  388.  
  389. void CCollectDoc::Dump(CDumpContext& dc) const
  390. {
  391.     CDocument::Dump(dc);
  392. }
  393. #endif //_DEBUG
  394.  
  395. /////////////////////////////////////////////////////////////////////////////
  396. // CCollectDoc commands
  397.