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.

203 lines
5.3 KiB

  1. // Retained.h -- Retained 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_RETAINED_H)
  8. #define SLBCSP_RETAINED_H
  9. #include "RsrcCtrlr.h"
  10. // Using the "resource acquisition is initialization" idiom, the
  11. // Retained template manages retaining a Retainable object (the
  12. // resource). Classes derived from Retainable may need to specialize
  13. // this template's c'tor and d'tor rather than take the default.
  14. template<class T>
  15. class Retained
  16. : public ResourceController<T>
  17. {
  18. public:
  19. // Types
  20. // C'tors/D'tors
  21. explicit
  22. Retained(T const &rResource = T());
  23. Retained(Retained<T> const &rhs);
  24. virtual
  25. ~Retained() throw();
  26. // Operators
  27. Retained<T> &
  28. operator=(T const &rhs);
  29. // Operations
  30. // Access
  31. // Predicates
  32. protected:
  33. // Types
  34. // C'tors/D'tors
  35. // Operators
  36. // Operations
  37. void
  38. Acquire();
  39. virtual void
  40. DoAfterAssignment();
  41. virtual void
  42. DoBeforeAssignment();
  43. void
  44. Release();
  45. // Access
  46. // Predicates
  47. // Variables
  48. private:
  49. // Types
  50. // C'tors/D'tors
  51. // Operators
  52. // Operations
  53. void
  54. DoAcquire();
  55. void
  56. DoRelease();
  57. // Access
  58. // Predicates
  59. // Variables
  60. bool m_fIsRetained;
  61. };
  62. ///////////////////////// TEMPLATE METHODS //////////////////////////////
  63. /////////////////////////// PUBLIC /////////////////////////////////
  64. // Types
  65. // C'tors/D'tors
  66. template<class T>
  67. Retained<T>::Retained(T const &rResource)
  68. : ResourceController<T>(rResource),
  69. m_fIsRetained(false)
  70. {
  71. Acquire();
  72. }
  73. template<class T>
  74. Retained<T>::Retained(Retained<T> const &rhs)
  75. : ResourceController<T>(rhs.m_Resource),
  76. m_fIsRetained(false)
  77. {
  78. Acquire();
  79. }
  80. template<class T>
  81. Retained<T>::~Retained() throw()
  82. {
  83. try
  84. {
  85. Release();
  86. }
  87. catch (...)
  88. {
  89. // don't allow exceptions to propagate out of destructors
  90. }
  91. }
  92. // Operators
  93. template<class T>
  94. Retained<T> &
  95. Retained<T>::operator=(T const &rhs)
  96. {
  97. ResourceController<T>::operator=(rhs);
  98. return *this;
  99. }
  100. // Operations
  101. // Access
  102. // Predicates
  103. // Static Variables
  104. /////////////////////////// PROTECTED /////////////////////////////////
  105. // C'tors/D'tors
  106. // Operators
  107. // Operations
  108. template<class T>
  109. void
  110. Retained<T>::Acquire()
  111. {
  112. if (m_Resource && !m_fIsRetained)
  113. {
  114. DoAcquire();
  115. m_fIsRetained = true;
  116. }
  117. }
  118. template<class T>
  119. void
  120. Retained<T>::DoAfterAssignment()
  121. {
  122. ResourceController<T>::DoAfterAssignment();
  123. Acquire();
  124. }
  125. template<class T>
  126. void
  127. Retained<T>::DoBeforeAssignment()
  128. {
  129. Release();
  130. ResourceController<T>::DoBeforeAssignment();
  131. }
  132. template<class T>
  133. void
  134. Retained<T>::Release()
  135. {
  136. if (m_Resource && m_fIsRetained)
  137. {
  138. DoRelease();
  139. m_fIsRetained = false;
  140. }
  141. }
  142. // Access
  143. // Predicates
  144. // Static Variables
  145. /////////////////////////// PRIVATE /////////////////////////////////
  146. // C'tors/D'tors
  147. // Operators
  148. // Operations
  149. template<class T>
  150. void
  151. Retained<T>::DoAcquire()
  152. {
  153. m_Resource->Retain();
  154. }
  155. template<class T>
  156. void
  157. Retained<T>::DoRelease()
  158. {
  159. m_Resource->Relinquish();
  160. }
  161. // Access
  162. // Predicates
  163. // Static Variables
  164. #endif // SLBCSP_RETAINED_H