home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK10 / MFC / SRC / OBJECT.CP$ / object
Encoding:
Text File  |  1992-02-17  |  4.9 KB  |  214 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library. 
  2. // Copyright (C) 1992 Microsoft Corporation 
  3. // All rights reserved. 
  4. //  
  5. // This source code is only intended as a supplement to the 
  6. // Microsoft Foundation Classes Reference and Microsoft 
  7. // QuickHelp documentation provided with the library. 
  8. // See these sources for detailed information regarding the 
  9. // Microsoft Foundation Classes product. 
  10.  
  11. #ifdef _WINDOWS
  12. #ifndef _WINDLL
  13. #include "afxole.h"
  14. #else
  15. #include "afxwin.h"
  16. #endif //_WINDLL
  17. #else
  18. #include "afx.h"
  19. #endif
  20.  
  21. #include "afxcoll.h"
  22.  
  23. #pragma hdrstop
  24.  
  25. #include <new.h>        // for set_new_handler
  26.  
  27. #ifdef AFX_CORE_SEG
  28. #pragma code_seg(AFX_CORE_SEG)
  29. #endif
  30.  
  31. #ifdef _DEBUG
  32. #undef THIS_FILE
  33. static char BASED_CODE THIS_FILE[] = __FILE__;
  34. #endif
  35.  
  36. /////////////////////////////////////////////////////////////////////////////
  37. // Runtime Typing
  38.  
  39. // special runtime-class structure for CObject (no base class)
  40. struct CRuntimeClass NEAR CObject::classCObject =
  41.     { "CObject", sizeof(CObject), 0xffff, NULL, NULL };
  42. static CClassInit _init_CObject(&CObject::classCObject);
  43.  
  44. CRuntimeClass* CObject::GetRuntimeClass() const
  45. {
  46.     return &CObject::classCObject;
  47. }
  48.  
  49. BOOL CObject::IsKindOf(const CRuntimeClass* pClass) const
  50. {
  51.     ASSERT(this != NULL);
  52.     // it better be in valid memory, at least for CObject size
  53.     ASSERT(AfxIsValidAddress(this, sizeof(CObject)));
  54.  
  55.     // simple SI case
  56.     register CRuntimeClass* pClassThis = GetRuntimeClass();
  57.     ASSERT(pClass != NULL);
  58.     ASSERT(pClassThis != NULL);
  59.     while (pClassThis != NULL)
  60.     {
  61.         if (pClassThis == pClass)
  62.             return TRUE;
  63.         pClassThis = pClassThis->m_pBaseClass;
  64.     }
  65.     return FALSE;       // walked to the top, no match
  66. }
  67.  
  68. /////////////////////////////////////////////////////////////////////////////
  69. // Diagnostic Support
  70.  
  71. #ifdef _DEBUG
  72. extern "C" void PASCAL AfxAssertValidObject(const CObject* pOb)
  73. {
  74.     if (pOb == NULL)
  75.     {
  76.         TRACE("ASSERT_VALID fails with NULL pointer\n");
  77.         ASSERT(FALSE);
  78.         return;     // quick escape
  79.     }
  80.     ASSERT(::AfxIsValidAddress(pOb, sizeof(CObject)));
  81.     pOb->AssertValid();
  82.     ASSERT(::AfxIsValidAddress(pOb, pOb->GetRuntimeClass()->m_nObjectSize));
  83. }
  84. #endif
  85.  
  86.  
  87. void CObject::AssertValid() const
  88. {
  89.     ASSERT(this != NULL);
  90. }
  91.  
  92. void
  93. CObject::Dump(CDumpContext& dc) const
  94. {
  95. #ifdef _DEBUG
  96.     dc << "a " << GetRuntimeClass()->m_pszClassName << " at " 
  97.         << (void*) this << " ";
  98. #else
  99.     dc;
  100. #endif //_DEBUG
  101. }
  102.  
  103. ////////////////////////////////////////////////////////////////////////////
  104. // Allocation/Creation
  105.  
  106. CObject* CRuntimeClass::CreateObject()
  107. {
  108.     void* p = CObject::operator new(m_nObjectSize);
  109.     if (!ConstructObject(p))
  110.     {
  111.         CObject::operator delete(p);
  112.         p = NULL;
  113.     }
  114.     return (CObject*) p;
  115. }
  116.  
  117. BOOL CRuntimeClass::ConstructObject(void* pThis)
  118. /*
  119.   -- dynamically construct an instance of this class in the memory
  120.         pointed to by 'pThis'
  121.   -- return FALSE if can't construct (only possible cause is an abstract class)
  122. */
  123. {
  124.     ASSERT(AfxIsValidAddress(pThis, m_nObjectSize));
  125.  
  126.     if (m_pfnConstruct != NULL)
  127.     {
  128.         (*m_pfnConstruct)(pThis);
  129.         return TRUE;
  130.     }
  131.     else
  132.     {
  133.         TRACE("Error: Trying to construct an instance of an abstract class.\n");
  134.         return FALSE;
  135.     }
  136. }
  137.  
  138.  
  139. ////////////////////////////////////////////////////////////////////////////
  140. // Class loader & class serialization
  141.  
  142. BOOL 
  143. CObject::IsSerializable() const
  144.     return (GetRuntimeClass()->m_wSchema != 0xffff);
  145. }
  146.  
  147. CRuntimeClass* CRuntimeClass::pFirstClass = NULL;
  148.  
  149. CClassInit::CClassInit(register CRuntimeClass* pNewClass)
  150. {
  151.     ASSERT(pNewClass->m_pNextClass == NULL);
  152.     pNewClass->m_pNextClass = CRuntimeClass::pFirstClass;
  153.     CRuntimeClass::pFirstClass = pNewClass;
  154. }
  155.  
  156. #ifdef _DEBUG
  157. void PASCAL 
  158. AfxDoForAllClasses(void (*pfn)(const CRuntimeClass*, void*),
  159.     void* pContext)
  160. {
  161.     CRuntimeClass* pClass;
  162.  
  163.     for (pClass = CRuntimeClass::pFirstClass; pClass != NULL;
  164.         pClass = pClass->m_pNextClass)
  165.     {
  166.         (*pfn)(pClass, pContext);
  167.     }
  168. }
  169. #endif
  170.  
  171. /////////////////////////////////////////////////////////////////////////////
  172. // Non-diagnostic memory routines
  173. //
  174.  
  175. #ifndef _DEBUG
  176.     // Debugging version replaces global ::new so this is not needed
  177.  
  178. int cdecl AfxNewHandler(size_t /* nSize */)
  179. {
  180.     //  AFX memory allocation will never return "NULL" it will always throw
  181.     //      a memory exception instead
  182.     AfxThrowMemoryException();
  183.     return 0;
  184. }
  185. #endif //!_DEBUG
  186.  
  187.  
  188. // hook in our own new_handler
  189. static BOOL AfxInitialize()
  190. {
  191.     (void)_afx_version();
  192. #ifdef _DEBUG
  193.  
  194.     // Force reference of the following symbols for CodeView
  195. #ifdef _WINDOWS
  196.     (void)afxTraceFlags;
  197. #endif
  198.  
  199.     (void)afxTraceEnabled;
  200.     (void)afxMemDF;
  201.  
  202.     return AfxDiagnosticInit();
  203. #else
  204.     _set_new_handler(AfxNewHandler);
  205.     return TRUE;
  206. #endif // _DEBUG
  207. }
  208.  
  209. static BOOL bInitialized = AfxInitialize();
  210.         // a way to force initialization
  211.  
  212. /////////////////////////////////////////////////////////////////////////////
  213.