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.

276 lines
8.3 KiB

  1. // RsrcCtrlr.h -- ReSouRCe ConTRoLleR template class
  2. // (c) Copyright Schlumberger Technology Corp., unpublished work, created
  3. // 1999. This computer program includes Confidential, Proprietary
  4. // Information and is a Trade Secret of Schlumberger Technology Corp. All
  5. // use, disclosure, and/or reproduction is prohibited unless authorized
  6. // in writing. All Rights Reserved.
  7. #if !defined(SLBCSP_RSRCCTRLR_H)
  8. #define SLBCSP_RSRCCTRLR_H
  9. // ResourceController is an abstract base template class to control
  10. // the acquisition and release of a resource. The controlled resource
  11. // (m_Resource) is a protected member. The class implements a handle
  12. // interface to safely access and manipulate that resource in
  13. // conjunction with the derived class.
  14. //
  15. // Example usages of the ResourceController are implementing the
  16. // "resource acquisition is initialization," locking and unlocking an
  17. // object for thread safety, and counted pointer idioms.
  18. //
  19. //
  20. // DERIVED CLASS RESPONSIBILITY: To complete the implementation, the
  21. // derived class must define the Acquire and Release methods to
  22. // perform the resource acquisition and release operations,
  23. // respectively. The ResourceController does not call Acquire in the
  24. // constructor since that method is defined by the derived class and
  25. // the derived portion of the object is not be considered constructed
  26. // at the time the base class constructor is called. For reasons of
  27. // symetry, ResourceController does not call Release in the
  28. // destructor. Therefore, it is the derived classes responsibility to
  29. // define constructors, a destructor, and call Acquire and Release in
  30. // those methods as appropriate.
  31. //
  32. // As with any derived class implementation, any assignment operators
  33. // defined by the derived class should call the the comparable
  34. // versions in ResourceController.
  35. //
  36. // CONSTRAINTS: T must support a default value (have a default
  37. // constructor).
  38. //
  39. // ResourceController was not designed to be referenced directly, that
  40. // is why all of it's methods (except Acquire and Release) are not
  41. // defined virtual.
  42. //
  43. // NOTE: Comparison helpers are defined at the end.
  44. template<class T>
  45. class ResourceController
  46. {
  47. public:
  48. // Types
  49. // PrivateDummy is a helper class to support validity testing of
  50. // ResourceController. This class together with the conversion
  51. // operator PrivateDummy const *() below allows
  52. // ResourceControllers to be tested for nullness (validity tests).
  53. // The implemenation allows these tests in a syntactically natural
  54. // way without allowing heterogeneous comparisons and that won't
  55. // violate the protections that ResourceController provides. The
  56. // technique is a variation from an article by Don Box in "Com
  57. // Smart Pointers Also Considered Harmful," 1996, C++ Report.
  58. class PrivateDummy
  59. {};
  60. // C'tors/D'tors
  61. virtual
  62. ~ResourceController() throw () = 0;
  63. // Operators
  64. operator T() const;
  65. ResourceController<T> &
  66. operator=(T const &rhs);
  67. ResourceController<T> &
  68. operator=(ResourceController<T> const &rhs);
  69. operator PrivateDummy const *() /* const */;
  70. // Enable validity test of ResourceController. See description of
  71. // PrivateDummy type.
  72. // making this a const routine causes compilation
  73. // problems in VC++ 6.0 for some reason.
  74. // Operations
  75. // Access
  76. T
  77. operator->() const;
  78. T
  79. operator*() const;
  80. // Predicates
  81. protected:
  82. // Types
  83. // C'tors/D'tors
  84. explicit
  85. ResourceController(T const &rResource = T());
  86. ResourceController(ResourceController<T> const &rhs);
  87. // Operators
  88. // Operations
  89. void
  90. DoAcquire();
  91. virtual void
  92. DoAfterAssignment();
  93. virtual void
  94. DoBeforeAssignment();
  95. void
  96. DoRelease();
  97. // Access
  98. // Predicates
  99. // Variables
  100. T m_Resource;
  101. private:
  102. // Types
  103. // C'tors/D'tors
  104. // Operators
  105. // Operations
  106. // Access
  107. // Predicates
  108. // Variables
  109. };
  110. ///////////////////////// TEMPLATE METHODS //////////////////////////////
  111. /////////////////////////// PUBLIC /////////////////////////////////
  112. // Types
  113. // C'tors/D'tors
  114. template<class T>
  115. ResourceController<T>::~ResourceController() throw()
  116. {}
  117. // Operators
  118. template<class T>
  119. ResourceController<T>::operator T() const
  120. {
  121. return m_Resource;
  122. }
  123. template<class T>
  124. T
  125. ResourceController<T>::operator->() const
  126. {
  127. return m_Resource;
  128. }
  129. template<class T>
  130. T
  131. ResourceController<T>::operator*() const
  132. {
  133. return m_Resource;
  134. }
  135. template<class T>
  136. ResourceController<T>::operator ResourceController<T>::PrivateDummy const *() /* const */
  137. {
  138. return m_Resource
  139. ? reinterpret_cast<PrivateDummy *>(this)
  140. : 0;
  141. }
  142. template<class T>
  143. ResourceController<T> &
  144. ResourceController<T>::operator=(T const &rhs)
  145. {
  146. if (m_Resource != rhs)
  147. {
  148. DoBeforeAssignment();
  149. m_Resource = rhs;
  150. DoAfterAssignment();
  151. }
  152. return *this;
  153. }
  154. template<class T>
  155. ResourceController<T> &
  156. ResourceController<T>::operator=(ResourceController<T> const &rhs)
  157. {
  158. if (this != &rhs)
  159. {
  160. DoBeforeAssignment();
  161. m_Resource = rhs.m_Resource;
  162. DoAfterAssignment();
  163. }
  164. return *this;
  165. }
  166. // Operations
  167. // Access
  168. // Predicates
  169. // Static Variables
  170. /////////////////////////// PROTECTED /////////////////////////////////
  171. // C'tors/D'tors
  172. template<class T>
  173. ResourceController<T>::ResourceController(T const &rResource)
  174. : m_Resource(rResource)
  175. {}
  176. template<class T>
  177. ResourceController<T>::ResourceController(ResourceController<T> const &rhs)
  178. : m_Resource(rhs.m_Resource)
  179. {}
  180. // Operators
  181. // Operations
  182. template<class T>
  183. void
  184. ResourceController<T>::DoAcquire()
  185. {}
  186. template<class T>
  187. void
  188. ResourceController<T>::DoAfterAssignment()
  189. {}
  190. template<class T>
  191. void
  192. ResourceController<T>::DoBeforeAssignment()
  193. {}
  194. template<class T>
  195. void
  196. ResourceController<T>::DoRelease()
  197. {}
  198. // Access
  199. // Predicates
  200. // Static Variables
  201. /////////////////////////// PRIVATE /////////////////////////////////
  202. // C'tors/D'tors
  203. // Operators
  204. // Operations
  205. // Access
  206. // Predicates
  207. // Static Variables
  208. /////////////////////////// HELPERS /////////////////////////////////
  209. template<class T>
  210. bool
  211. operator==(ResourceController<T> const &lhs,
  212. ResourceController<T> const &rhs)
  213. {
  214. return lhs.m_Resource == rhs.m_Resource;
  215. }
  216. template<class T>
  217. bool
  218. operator!=(ResourceController<T> const &lhs,
  219. ResourceController<T> const &rhs)
  220. {
  221. return !operator==(lhs, rhs);
  222. }
  223. #endif // SLBCSP_RSRCCTRLR_H