externally_locked_stream.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // (C) Copyright 2012 Vicente J. Botet Escriba
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_THREAD_EXTERNALLY_LOCKED_STREAM_HPP
  6. #define BOOST_THREAD_EXTERNALLY_LOCKED_STREAM_HPP
  7. #include <boost/thread/detail/config.hpp>
  8. #include <boost/thread/detail/move.hpp>
  9. #include <boost/thread/detail/delete.hpp>
  10. #include <boost/thread/externally_locked.hpp>
  11. #include <boost/thread/lock_traits.hpp>
  12. #include <boost/thread/recursive_mutex.hpp>
  13. #include <boost/thread/strict_lock.hpp>
  14. #include <boost/config/abi_prefix.hpp>
  15. namespace boost
  16. {
  17. template <typename Stream, typename RecursiveMutex=recursive_mutex>
  18. class externally_locked_stream;
  19. template <class Stream, typename RecursiveMutex=recursive_mutex>
  20. class stream_guard
  21. {
  22. friend class externally_locked_stream<Stream, RecursiveMutex> ;
  23. public:
  24. typedef typename externally_locked_stream<Stream, RecursiveMutex>::mutex_type mutex_type;
  25. BOOST_THREAD_MOVABLE_ONLY( stream_guard)
  26. stream_guard(externally_locked_stream<Stream, RecursiveMutex>& mtx) :
  27. mtx_(&mtx)
  28. {
  29. mtx.lock();
  30. }
  31. stream_guard(externally_locked_stream<Stream, RecursiveMutex>& mtx, adopt_lock_t) :
  32. mtx_(&mtx)
  33. {
  34. }
  35. stream_guard(BOOST_THREAD_RV_REF(stream_guard) rhs) BOOST_NOEXCEPT
  36. : mtx_(rhs.mtx_)
  37. {
  38. rhs.mtx_= 0;
  39. }
  40. ~stream_guard()
  41. {
  42. if (mtx_ != 0) mtx_->unlock();
  43. }
  44. bool owns_lock(const mutex_type * l) const BOOST_NOEXCEPT
  45. {
  46. return l == mtx_->mutex();
  47. }
  48. /**
  49. * @Requires mtx_
  50. */
  51. Stream& get() const
  52. {
  53. BOOST_THREAD_ASSERT_PRECONDITION( mtx_, lock_error() );
  54. return mtx_->get(*this);
  55. }
  56. Stream& bypass() const
  57. {
  58. return get();
  59. }
  60. private:
  61. externally_locked_stream<Stream, RecursiveMutex>* mtx_;
  62. };
  63. template <typename Stream, typename RecursiveMutex>
  64. struct is_strict_lock_sur_parole<stream_guard<Stream, RecursiveMutex> > : true_type
  65. {
  66. };
  67. /**
  68. * externally_locked_stream cloaks a reference to an stream of type Stream, and actually
  69. * provides full access to that object through the get and set member functions, provided you
  70. * pass a reference to a strict lock object.
  71. */
  72. //[externally_locked_stream
  73. template <typename Stream, typename RecursiveMutex>
  74. class externally_locked_stream: public externally_locked<Stream&, RecursiveMutex>
  75. {
  76. typedef externally_locked<Stream&, RecursiveMutex> base_type;
  77. public:
  78. BOOST_THREAD_NO_COPYABLE( externally_locked_stream)
  79. /**
  80. * Effects: Constructs an externally locked object storing the cloaked reference object.
  81. */
  82. externally_locked_stream(Stream& stream, RecursiveMutex& mtx) BOOST_NOEXCEPT :
  83. base_type(stream, mtx)
  84. {
  85. }
  86. stream_guard<Stream, RecursiveMutex> hold() BOOST_NOEXCEPT
  87. {
  88. return stream_guard<Stream, RecursiveMutex> (*this);
  89. }
  90. Stream& bypass() const
  91. {
  92. stream_guard<Stream, RecursiveMutex> lk(*this);
  93. return get(lk);
  94. }
  95. };
  96. //]
  97. template <typename Stream, typename RecursiveMutex, typename T>
  98. inline const stream_guard<Stream, RecursiveMutex>& operator<<(const stream_guard<Stream, RecursiveMutex>& lck, T arg)
  99. {
  100. lck.get() << arg;
  101. return lck;
  102. }
  103. template <typename Stream, typename RecursiveMutex>
  104. inline const stream_guard<Stream, RecursiveMutex>& operator<<(const stream_guard<Stream, RecursiveMutex>& lck, Stream& (*arg)(Stream&))
  105. {
  106. lck.get() << arg;
  107. return lck;
  108. }
  109. template <typename Stream, typename RecursiveMutex, typename T>
  110. inline const stream_guard<Stream, RecursiveMutex>& operator>>(const stream_guard<Stream, RecursiveMutex>& lck, T& arg)
  111. {
  112. lck.get() >> arg;
  113. return lck;
  114. }
  115. template <typename Stream, typename RecursiveMutex, typename T>
  116. inline stream_guard<Stream, RecursiveMutex> operator<<(externally_locked_stream<Stream, RecursiveMutex>& mtx, T arg)
  117. {
  118. stream_guard<Stream, RecursiveMutex> lk(mtx);
  119. mtx.get(lk) << arg;
  120. return boost::move(lk);
  121. }
  122. template <typename Stream, typename RecursiveMutex>
  123. inline stream_guard<Stream, RecursiveMutex> operator<<(externally_locked_stream<Stream, RecursiveMutex>& mtx, Stream& (*arg)(Stream&))
  124. {
  125. stream_guard<Stream, RecursiveMutex> lk(mtx);
  126. mtx.get(lk) << arg;
  127. return boost::move(lk);
  128. }
  129. template <typename Stream, typename RecursiveMutex, typename T>
  130. inline stream_guard<Stream, RecursiveMutex> operator>>(externally_locked_stream<Stream, RecursiveMutex>& mtx, T& arg)
  131. {
  132. stream_guard<Stream, RecursiveMutex> lk(mtx);
  133. mtx.get(lk) >> arg;
  134. return boost::move(lk);
  135. }
  136. }
  137. #include <boost/config/abi_suffix.hpp>
  138. #endif // header