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.2 KiB

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