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.

260 lines
7.2 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/synch/lock_scope.hpp (originally MLLock.h, ::SynesisStd)
  3. *
  4. * Purpose: Synchronisation object lock scoping class.
  5. *
  6. * Created: 1st October 1994
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 1994-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file stlsoft/synch/lock_scope.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::lock_scope class template
  42. * (\ref group__library__synch "Synchronisation" Library).
  43. */
  44. #ifndef STLSOFT_INCL_STLSOFT_SYNCH_HPP_LOCK_SCOPE
  45. #define STLSOFT_INCL_STLSOFT_SYNCH_HPP_LOCK_SCOPE
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define STLSOFT_VER_STLSOFT_SYNCH_HPP_LOCK_SCOPE_MAJOR 6
  48. # define STLSOFT_VER_STLSOFT_SYNCH_HPP_LOCK_SCOPE_MINOR 0
  49. # define STLSOFT_VER_STLSOFT_SYNCH_HPP_LOCK_SCOPE_REVISION 1
  50. # define STLSOFT_VER_STLSOFT_SYNCH_HPP_LOCK_SCOPE_EDIT 112
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  56. # include <stlsoft/stlsoft.h>
  57. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  58. /* /////////////////////////////////////////////////////////////////////////
  59. * Namespace
  60. */
  61. #ifndef _STLSOFT_NO_NAMESPACE
  62. namespace stlsoft
  63. {
  64. #endif /* _STLSOFT_NO_NAMESPACE */
  65. /* /////////////////////////////////////////////////////////////////////////
  66. * Classes
  67. */
  68. // class lock_traits
  69. /** \brief Traits class for lockable objects
  70. *
  71. * \ingroup group__library__synch
  72. *
  73. * \param L The lockable class
  74. */
  75. template<ss_typename_param_k L>
  76. struct lock_traits
  77. {
  78. /// \name Member Types
  79. /// @{
  80. public:
  81. /// \brief The lockable type
  82. typedef L lock_type;
  83. /// \brief The current parameterisation of this type
  84. typedef lock_traits<L> class_type;
  85. /// @}
  86. /// \name Operations
  87. /// @{
  88. public:
  89. /// \brief Locks the given lockable instance
  90. static void lock(lock_type &c)
  91. {
  92. lock_instance(c);
  93. }
  94. /// \brief Unlocks the given lockable instance
  95. static void unlock(lock_type &c)
  96. {
  97. unlock_instance(c);
  98. }
  99. /// @}
  100. };
  101. // class lock_invert_traits
  102. /** \brief Traits class for inverting the lock status of lockable objects
  103. *
  104. * \ingroup group__library__synch
  105. *
  106. * \param L The lockable class
  107. */
  108. template<ss_typename_param_k L>
  109. struct lock_invert_traits
  110. {
  111. /// \name Member Types
  112. /// @{
  113. public:
  114. /// \brief The lockable type
  115. typedef L lock_type;
  116. /// \brief The current parameterisation of this type
  117. typedef lock_invert_traits<L> class_type;
  118. /// @}
  119. /// \name Operations
  120. /// @{
  121. public:
  122. /// \brief Unlocks the given lockable instance
  123. static void lock(lock_type &c)
  124. {
  125. unlock_instance(c);
  126. }
  127. /// \brief Locks the given lockable instance
  128. static void unlock(lock_type &c)
  129. {
  130. lock_instance(c);
  131. }
  132. /// @}
  133. };
  134. // class lock_traits_inverter
  135. /** \brief Traits inverter class for inverting the lock behaviour of lockable traits types
  136. *
  137. * \ingroup group__library__synch
  138. *
  139. * \param L The traits class
  140. */
  141. template<ss_typename_param_k T>
  142. struct lock_traits_inverter
  143. {
  144. /// \name Member Types
  145. /// @{
  146. public:
  147. /// \brief The traits type
  148. typedef T traits_type;
  149. /// \brief The lockable type
  150. typedef ss_typename_type_k traits_type::lock_type lock_type;
  151. /// \brief The current parameterisation of this type
  152. typedef lock_traits_inverter<T> class_type;
  153. /// @}
  154. /// \name Operations
  155. /// @{
  156. public:
  157. /// \brief Unlocks the given lockable instance
  158. static void lock(lock_type &c)
  159. {
  160. traits_type::unlock(c);
  161. }
  162. /// \brief Locks the given lockable instance
  163. static void unlock(lock_type &c)
  164. {
  165. traits_type::lock(c);
  166. }
  167. /// @}
  168. };
  169. // class lock_scope
  170. /**\brief This class scopes the lock status of a lockable type
  171. *
  172. * \ingroup group__library__synch
  173. *
  174. * \param L The lockable type, e.g. stlsoft::null_mutex
  175. * \param T The lock traits. On translators that support default template arguments this defaults to lock_traits<L>
  176. */
  177. template< ss_typename_param_k L
  178. #ifdef STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
  179. , ss_typename_param_k T = lock_traits<L>
  180. #else /* ? STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
  181. , ss_typename_param_k T
  182. #endif /* STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
  183. >
  184. class lock_scope
  185. {
  186. /// \name Member Types
  187. /// @{
  188. public:
  189. /// \brief The lockable type
  190. typedef L lock_type;
  191. /// \brief The traits type
  192. typedef T traits_type;
  193. /// \brief The current parameterisation of this type
  194. typedef lock_scope<L, T> class_type;
  195. /// @}
  196. /// \name Construction
  197. /// @{
  198. public:
  199. /// \brief Locks the lockable instance
  200. lock_scope(lock_type &l)
  201. : m_l(l)
  202. {
  203. traits_type::lock(m_l);
  204. }
  205. /// \brief Unlocks the lockable instance
  206. ~lock_scope() stlsoft_throw_0()
  207. {
  208. traits_type::unlock(m_l);
  209. }
  210. /// @}
  211. /// \name Members
  212. /// @{
  213. private:
  214. lock_type &m_l;
  215. /// @}
  216. /// \name Not to be implemented
  217. /// @{
  218. private:
  219. lock_scope(class_type const& rhs);
  220. lock_scope& operator =(class_type const& rhs);
  221. /// @}
  222. };
  223. /* ////////////////////////////////////////////////////////////////////// */
  224. #ifndef _STLSOFT_NO_NAMESPACE
  225. } // namespace stlsoft
  226. #endif /* _STLSOFT_NO_NAMESPACE */
  227. /* ////////////////////////////////////////////////////////////////////// */
  228. #endif /* !STLSOFT_INCL_STLSOFT_SYNCH_HPP_LOCK_SCOPE */
  229. /* ///////////////////////////// end of file //////////////////////////// */