|
|
1.1 root 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: {
145: return (GetRuntimeClass()->m_wSchema != 0xffff);
146: }
147:
148: CRuntimeClass* CRuntimeClass::pFirstClass = NULL;
149:
150: CClassInit::CClassInit(register CRuntimeClass* pNewClass)
151: {
152: ASSERT(pNewClass->m_pNextClass == NULL);
153: pNewClass->m_pNextClass = CRuntimeClass::pFirstClass;
154: CRuntimeClass::pFirstClass = pNewClass;
155: }
156:
157: #ifdef _DEBUG
158: void PASCAL
159: AfxDoForAllClasses(void (*pfn)(const CRuntimeClass*, void*),
160: void* pContext)
161: {
162: CRuntimeClass* pClass;
163:
164: for (pClass = CRuntimeClass::pFirstClass; pClass != NULL;
165: pClass = pClass->m_pNextClass)
166: {
167: (*pfn)(pClass, pContext);
168: }
169: }
170: #endif
171:
172: /////////////////////////////////////////////////////////////////////////////
173: // Non-diagnostic memory routines
174: //
175:
176: #ifndef _DEBUG
177: // Debugging version replaces global ::new so this is not needed
178:
179: int cdecl AfxNewHandler(size_t /* nSize */)
180: {
181: // AFX memory allocation will never return "NULL" it will always throw
182: // a memory exception instead
183: AfxThrowMemoryException();
184: return 0;
185: }
186: #endif //!_DEBUG
187:
188:
189: // hook in our own new_handler
190: static BOOL AfxInitialize()
191: {
192: (void)_afx_version();
193: #ifdef _DEBUG
194:
195: // Force reference of the following symbols for CodeView
196: #ifdef _WINDOWS
197: (void)afxTraceFlags;
198: #endif
199:
200: (void)afxTraceEnabled;
201: (void)afxMemDF;
202:
203: return AfxDiagnosticInit();
204: #else
205: _set_new_handler(AfxNewHandler);
206: return TRUE;
207: #endif // _DEBUG
208: }
209:
210: static BOOL bInitialized = AfxInitialize();
211: // a way to force initialization
212:
213: /////////////////////////////////////////////////////////////////////////////
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.