Leaked source code of windows server 2003
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.

217 lines
4.9 KiB

  1. /*
  2. * A U T O P T R . H
  3. *
  4. * Implementation of the Standard Template Library (STL) auto_ptr template.
  5. *
  6. * Copyright 1986-1997 Microsoft Corporation, All Rights Reserved
  7. */
  8. #ifndef _AUTOPTR_H_
  9. #define _AUTOPTR_H_
  10. #include <ex\autoptr.h>
  11. // ========================================================================
  12. //
  13. // TEMPLATE CLASS auto_ptr_obsolete
  14. //
  15. // auto_ptr for objects in _davprs
  16. //
  17. template<class X>
  18. class auto_ptr_obsolete
  19. {
  20. mutable X * owner;
  21. X * px;
  22. public:
  23. explicit auto_ptr_obsolete(X* p=0) : owner(p), px(p) {}
  24. auto_ptr_obsolete(const auto_ptr_obsolete<X>& r)
  25. : owner(r.owner), px(r.relinquish()) {}
  26. auto_ptr_obsolete& operator=(const auto_ptr_obsolete<X>& r)
  27. {
  28. if ((void*)&r != (void*)this)
  29. {
  30. delete owner;
  31. owner = r.owner;
  32. px = r.relinquish();
  33. }
  34. return *this;
  35. }
  36. // NOTE: This equals operator is meant to be used to load a
  37. // new pointer (not yet held in any auto-ptr anywhere) into this object.
  38. auto_ptr_obsolete& operator=(X* p)
  39. {
  40. Assert(!owner); // Scream on overwrite of good data.
  41. owner = p;
  42. px = p;
  43. return *this;
  44. }
  45. ~auto_ptr_obsolete() { delete owner; }
  46. bool operator!()const { return (px == NULL); }
  47. operator X*() const { return px; }
  48. X& operator*() const { return *px; }
  49. X* operator->() const { return px; }
  50. X* get() const { return px; }
  51. X* relinquish() const { owner = 0; return px; }
  52. void clear()
  53. {
  54. if (owner)
  55. delete owner;
  56. owner = 0;
  57. px = 0;
  58. }
  59. };
  60. // ========================================================================
  61. //
  62. // TEMPLATE CLASS auto_com_ptr
  63. //
  64. // auto_ptr for COM (IUnknown-derived) objects.
  65. //
  66. // Yes, this is functionally a subset of CComPtr in the ATL,
  67. // but we don't want to pull in the whole friggin' ATL for one
  68. // measly template.
  69. //
  70. template<class X>
  71. class auto_com_ptr
  72. {
  73. X * px;
  74. // NOT IMPLEMENTED
  75. //
  76. void * operator new(size_t cb);
  77. public:
  78. // CONSTRUCTORS
  79. //
  80. explicit auto_com_ptr(X* p=0) : px(p) {}
  81. // Copy constructor -- provided only for returning objects.
  82. // Should ALWAYS be optimized OUT. Scream if we actually execute this code!
  83. //$REVIEW: Should we really be returning objects like this?
  84. //
  85. auto_com_ptr(const auto_com_ptr<X>& r) : px(r.px)
  86. { TrapSz("Copy ctor for auto_com_ptr incorrectly called!"); }
  87. ~auto_com_ptr()
  88. {
  89. if (px)
  90. {
  91. px->Release();
  92. }
  93. }
  94. // MANIPULATORS
  95. //
  96. auto_com_ptr& operator=(const auto_com_ptr<X>& r)
  97. {
  98. if ((void*)&r != (void*)this)
  99. {
  100. clear(); // Release any object we're holding now
  101. px = r.px; // Grab & hold a ref on the object passed in
  102. if (px)
  103. px->AddRef();
  104. }
  105. return *this;
  106. }
  107. // NOTE: This equals operator is meant to be used to load a
  108. // new pointer (not yet held in any auto-ptr anywhere) into this object.
  109. //$REVIEW: The other options is an "additional" wrapper on the rvalue:
  110. //$REVIEW: current- m_pEventRouter = CreateEventRouter( m_szVRoot );
  111. //$REVIEW: other option- m_pEventRouter = auto_com_ptr<>(CreateEventRouter( m_szVRoot ));
  112. //
  113. auto_com_ptr& operator=(X* p)
  114. {
  115. Assert(!px); // Scream on overwrite of good data.
  116. px = p;
  117. return *this;
  118. }
  119. // ACCESSORS
  120. //
  121. bool operator!()const { return (px == NULL); }
  122. operator X*() const { return px; }
  123. X& operator*() const { return *px; }
  124. X* operator->() const { return px; }
  125. X** operator&() { Assert(NULL==px); return &px; }
  126. X* get() const { return px; }
  127. // MANIPULATORS
  128. //
  129. X* relinquish() { X* p = px; px = 0; return p; }
  130. X** load() { Assert(NULL==px); return &px; }
  131. void clear()
  132. {
  133. if (px) // Release any object we're holding now
  134. {
  135. px->Release();
  136. }
  137. px = NULL;
  138. }
  139. };
  140. // ========================================================================
  141. //
  142. // CLASS CMTRefCounted
  143. // TEMPLATE CLASS auto_ref_ptr
  144. //
  145. class CMTRefCounted
  146. {
  147. // NOT IMPLEMENTED
  148. //
  149. CMTRefCounted(const CMTRefCounted& );
  150. CMTRefCounted& operator=(const CMTRefCounted& );
  151. protected:
  152. LONG m_cRef;
  153. public:
  154. CMTRefCounted() : m_cRef(0) {}
  155. virtual ~CMTRefCounted() {}
  156. void AddRef()
  157. {
  158. InterlockedIncrement(&m_cRef);
  159. }
  160. void Release()
  161. {
  162. if (0 == InterlockedDecrement(&m_cRef))
  163. delete this;
  164. }
  165. };
  166. // ========================================================================
  167. //
  168. // TEMPLATE FUNCTION QI_cast
  169. //
  170. // QI directly into an auto_com_ptr.
  171. //
  172. // Queries for the given IID on the punk provided.
  173. // Returns NULL if failure.
  174. // Usage:
  175. // auto_com_ptr<INew> punkNew;
  176. // punkNew = QI_cast<INew, &IID_INew>( punkOld );
  177. // if (!punkNew)
  178. // { // error handling }
  179. //
  180. //$LATER: Fix this func (and all invocations!) to NOT return an auto_com_ptr!
  181. //
  182. template<class I, const IID * piid>
  183. auto_com_ptr<I>
  184. QI_cast( IUnknown * punk )
  185. {
  186. I * p;
  187. punk->QueryInterface( *piid, (LPVOID *) &p );
  188. return auto_com_ptr<I>( p );
  189. }
  190. #include <safeobj.h>
  191. #endif // _AUTOPTR_H_