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.

193 lines
5.6 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/synch/checkout_token.hpp
  3. *
  4. * Purpose: Scoped thread-safe access locking class.
  5. *
  6. * Created: 7th November 2004
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2004-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/checkout_token.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::checkout_token class
  42. * template
  43. * (\ref group__library__synch "Synchronisation" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_SYNCH_HPP_CHECKOUT_TOKEN
  46. #define STLSOFT_INCL_STLSOFT_SYNCH_HPP_CHECKOUT_TOKEN
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_SYNCH_HPP_CHECKOUT_TOKEN_MAJOR 2
  49. # define STLSOFT_VER_STLSOFT_SYNCH_HPP_CHECKOUT_TOKEN_MINOR 0
  50. # define STLSOFT_VER_STLSOFT_SYNCH_HPP_CHECKOUT_TOKEN_REVISION 2
  51. # define STLSOFT_VER_STLSOFT_SYNCH_HPP_CHECKOUT_TOKEN_EDIT 21
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Includes
  55. */
  56. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  57. # include <stlsoft/stlsoft.h>
  58. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  59. #ifndef STLSOFT_INCL_STLSOFT_SYNCH_HPP_LOCK_SCOPE
  60. # include <stlsoft/synch/lock_scope.hpp>
  61. #endif /* !STLSOFT_INCL_STLSOFT_SYNCH_HPP_LOCK_SCOPE */
  62. /* /////////////////////////////////////////////////////////////////////////
  63. * Namespace
  64. */
  65. #ifndef _STLSOFT_NO_NAMESPACE
  66. namespace stlsoft
  67. {
  68. #endif /* _STLSOFT_NO_NAMESPACE */
  69. /* /////////////////////////////////////////////////////////////////////////
  70. * Classes
  71. */
  72. /** \brief This class acts as a
  73. *
  74. * \ingroup group__library__synch
  75. *
  76. * For example, consider that you have a class X whose resources are to be
  77. * accessible to multiple threads, defined as follows:
  78. \code
  79. class X
  80. {
  81. public:
  82. typedef std::queue<int> queue_type;
  83. public:
  84. void lock();
  85. void unlock();
  86. public:
  87. que
  88. };
  89. \endcode
  90. *
  91. * Rather than relying on the good actions of the user, we can instead bind
  92. * the locking of X's resources with access to its queue, using
  93. * \link stlsoft::checkout_token checkout_token\endlink, as follows:
  94. *
  95. \code
  96. \endcode
  97. *
  98. */
  99. template< ss_typename_param_k CT //!< Accessed class type
  100. , ss_typename_param_k MT //!< Accessed member type
  101. >
  102. class checkout_token
  103. {
  104. /// \name Member Types
  105. /// @{
  106. public:
  107. /// \brief The type whose resources are to be protected
  108. typedef CT accessed_class_type;
  109. /// \brief
  110. typedef MT accessed_member_type;
  111. /// \brief The current instantation of the type
  112. typedef checkout_token<CT, MT> class_type;
  113. /// @}
  114. /// \name Construction
  115. /// @{
  116. public:
  117. /// \brief Locks
  118. checkout_token(accessed_class_type& ac, accessed_member_type am)
  119. : m_ac(ac)
  120. , m_am(am)
  121. , m_lock(ac)
  122. {}
  123. /// \brief Cop
  124. checkout_token(class_type const& rhs)
  125. : m_ac(rhs.m_ac)
  126. , m_am(rhs.m_am)
  127. , m_lock(rhs.m_ac)
  128. {}
  129. /// @}
  130. /// \name Accessors
  131. /// @{
  132. public:
  133. accessed_member_type member()
  134. {
  135. return m_am;
  136. }
  137. #if 0
  138. accessed_member_type *operator ->()
  139. {
  140. return &m_am;
  141. }
  142. accessed_member_type const* operator ->() const
  143. {
  144. return &m_am;
  145. }
  146. #endif /* 0 */
  147. /// @}
  148. /// \name Members
  149. /// @{
  150. private:
  151. typedef lock_scope<accessed_class_type> lock_type;
  152. accessed_class_type &m_ac;
  153. accessed_member_type m_am;
  154. lock_type m_lock;
  155. /// @}
  156. /// \name Not to be implemented
  157. /// @{
  158. private:
  159. class_type& operator =(class_type const&);
  160. /// @}
  161. };
  162. /* ////////////////////////////////////////////////////////////////////// */
  163. #ifndef _STLSOFT_NO_NAMESPACE
  164. } // namespace stlsoft
  165. #endif /* _STLSOFT_NO_NAMESPACE */
  166. /* ////////////////////////////////////////////////////////////////////// */
  167. #endif /* !STLSOFT_INCL_STLSOFT_SYNCH_HPP_CHECKOUT_TOKEN */
  168. /* ///////////////////////////// end of file //////////////////////////// */