Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
5.5 KiB

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright(C) 1997-1998 Microsoft Corporation all rights reserved.
  4. //
  5. // Module: sdobasedefs.h
  6. //
  7. // Project: Everest
  8. //
  9. // Description: Common classes and definitions
  10. //
  11. // Log:
  12. //
  13. // When Who What
  14. // ---- --- ----
  15. // 6/08/98 TLP Initial Version
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #ifndef __INC_SDO_BASE_DEFS_H_
  19. #define __INC_SDO_BASE_DEFS_H_
  20. #include <ias.h>
  21. #include <sdoias.h>
  22. #include <comutil.h>
  23. #include <comdef.h>
  24. #include <iascomp.h>
  25. #include <map>
  26. #include <list>
  27. using namespace std;
  28. // Ole DB Driver
  29. //
  30. #define IAS_DICTIONARY_DRIVER L"Microsoft.Jet.OLEDB.4.0"
  31. //////////////////////////////////////////////////////////////
  32. // Debug/Error Trace Macros - Wrap Underlying Trace Facilities
  33. //////////////////////////////////////////////////////////////
  34. ///////////////////////////////////
  35. // Trace Functin Wrappers
  36. #define SDO_ERROR_ID 0x100
  37. #define SDO_DEBUG_ID 0x200
  38. #define TRACE_FUNCTION_WRAPPER(x) \
  39. TRACE_FUNCTION(x);
  40. #define ERROR_TRACE_WRAPPER(dbgmsg) \
  41. ErrorTrace(SDO_ERROR_ID, dbgmsg); \
  42. #define ERROR_TRACE_WRAPPER_1(dbgmsg, param) \
  43. ErrorTrace(SDO_ERROR_ID, dbgmsg, param); \
  44. #define DEBUG_TRACE_WRAPPER(dbgmsg) \
  45. DebugTrace(SDO_DEBUG_ID, dbgmsg);
  46. #define DEBUG_TRACE_WRAPPER_1(dbgmsg, param) \
  47. DebugTrace(SDO_ERROR_ID, dbgmsg, param);
  48. /////////////////////////////////////////////////////////
  49. // Object Management Classes
  50. /////////////////////////////////////////////////////////
  51. /////////////////////////////////////////////////////////
  52. //
  53. // Master Pointer Tasks
  54. //
  55. // 1) Object instance counting
  56. // 2) Object construction and destruction
  57. // 3) Object lifetime control through reference counting
  58. //
  59. ////////////////////////////////////////////////////////
  60. template <class T>
  61. class CSdoMasterPtr
  62. {
  63. public:
  64. // Either master pointer constructor can result in an exception being thrown.
  65. // Creator of the master pointer is responsible for handling exceptions
  66. //
  67. /////////////////////////////////////////
  68. CSdoMasterPtr(LONG PointeeType, LONG PointeeId)
  69. : m_pT(new T(PointeeType, PointeeId)), m_dwRefCount(0)
  70. { m_dwInstances++; }
  71. /////////////////////////////////////////
  72. CSdoMasterPtr()
  73. : m_pT(new T), m_dwRefCount(0)
  74. { m_dwInstances++; }
  75. // T must have a copy constructor or must work with the default C++
  76. // copy constructor
  77. //
  78. /////////////////////////////////////////
  79. //CSdoMasterPtr(const CSdoMasterPtr<T>& mp)
  80. // : m_pT(new T(*(mp.m_pT))), m_dwRefCount(0)
  81. //{ m_dwInstances++; }
  82. /////////////////////////////////////////
  83. ~CSdoMasterPtr()
  84. { _ASSERT( 0 == m_dwRefCount ); delete m_pT; }
  85. /////////////////////////////////////////
  86. CSdoMasterPtr<T>& operator = (const CSdoMasterPtr<T>& mp)
  87. {
  88. // Check for assignment to self
  89. //
  90. if ( this ! &mp )
  91. {
  92. // Delete object pointed at and create a new one
  93. // User of the master pointer is responsible for catching
  94. // any exception thrown as a result of creating a object
  95. //
  96. delete m_pT;
  97. m_dwInstances--;
  98. m_pT = new T(*(mp.m_pT));
  99. }
  100. return *this;
  101. }
  102. /////////////////////////////////////////
  103. T* operator->()
  104. { _ASSERT( NULL != m_pT ); return m_pT; }
  105. /////////////////////////////////////////
  106. void Hold(void)
  107. {
  108. m_dwRefCount++;
  109. }
  110. /////////////////////////////////////////
  111. void Release(void)
  112. {
  113. // Handle case where someone calls Release when ref count is 0.
  114. //
  115. if ( m_dwRefCount > 0 )
  116. m_dwRefCount--;
  117. if ( 0 >= m_dwRefCount )
  118. {
  119. m_dwInstances--;
  120. delete this; // ~CSdoMasterPtr() deletes m_pT
  121. }
  122. }
  123. /////////////////////////////////////////
  124. DWORD GetInstanceCount(void);
  125. private:
  126. // T must have a copy constructor or must work with the default C++
  127. // copy constructor. This is not the case here...
  128. //
  129. /////////////////////////////////////////
  130. CSdoMasterPtr(const CSdoMasterPtr<T>& mp)
  131. : m_pT(new T(*(mp.m_pT))), m_dwRefCount(0)
  132. { m_dwInstances++; }
  133. T* m_pT; // Actual object
  134. DWORD m_dwRefCount; // Ref count
  135. static DWORD m_dwInstances; // Instances
  136. };
  137. /////////////////////////////////////////////////////////
  138. //
  139. // SDO Handle Tasks
  140. //
  141. // 1) Master Pointer Object creation
  142. // 2) Hide use of reference counting from programmer
  143. //
  144. ////////////////////////////////////////////////////////
  145. template <class T>
  146. class CSdoHandle
  147. {
  148. public:
  149. /////////////////////////////////////////
  150. CSdoHandle()
  151. : m_mp(NULL) { }
  152. /////////////////////////////////////////
  153. CSdoHandle(CSdoMasterPtr<T>* mp)
  154. : m_mp(mp)
  155. {
  156. _ASSERT( NULL != m_mp );
  157. m_mp->Hold();
  158. }
  159. /////////////////////////////////////////
  160. CSdoHandle(const CSdoHandle<T>& h)
  161. : m_mp(h.m_mp)
  162. {
  163. if ( NULL != m_mp )
  164. m_mp->Hold();
  165. }
  166. /////////////////////////////////////////
  167. ~CSdoHandle()
  168. {
  169. if ( NULL != m_mp )
  170. m_mp->Release();
  171. }
  172. /////////////////////////////////////////
  173. CSdoHandle<T>& operator = (const CSdoHandle<T>& h)
  174. {
  175. // Check for reference to self and instance where
  176. // h points to the same mp we do.
  177. //
  178. if ( this != &h && m_mp != h.m_mp )
  179. {
  180. if ( NULL != m_mp )
  181. m_mp->Release();
  182. m_mp = h.m_mp;
  183. if ( NULL != m_mp )
  184. m_mp->Hold();
  185. }
  186. return *this;
  187. }
  188. /////////////////////////////////////////
  189. CSdoMasterPtr<T>& operator->()
  190. {
  191. _ASSERT( NULL != m_mp );
  192. return *m_mp;
  193. }
  194. /////////////////////////////////////////
  195. bool IsValid()
  196. {
  197. return (NULL != m_mp ? true : false);
  198. }
  199. private:
  200. CSdoMasterPtr<T>* m_mp;
  201. };
  202. #endif //__INC_SDO_BASE_DEFS_H_