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.

206 lines
5.3 KiB

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