sync_streams.qbk 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. [/
  2. / Copyright (c) 2013 Vicente J. Botet Escriba
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section:ext_locked_streams Externally Locked Streams - EXPERIMENTAL]
  8. [warning These features are experimental and subject to change in future versions. There are not too much tests yet, so it is possible that you can find out some trivial bugs :(]
  9. [note These features are based on the [@http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3535.html [*N3535 - C++ Streams Mutex]] C++1y proposal, even if the library proposes al alternative interface.]
  10. [section:tutorial Tutorial]
  11. [endsect] [/tutorial]
  12. [/////////////////////]
  13. [section:ref Reference]
  14. #include <boost/thread/externally_locked_stream.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. template <typename Stream, typename RecursiveMutex>
  22. struct is_strict_lock_sur_parole<stream_guard<Stream, RecursiveMutex> > : true_type {};
  23. // Stream-like operators
  24. template <typename Stream, typename RecursiveMutex, typename T>
  25. const stream_guard<Stream, RecursiveMutex>& operator<<(const stream_guard<Stream, RecursiveMutex>& lck, T arg);
  26. template <typename Stream, typename RecursiveMutex>
  27. const stream_guard<Stream, RecursiveMutex>&
  28. operator<<(const stream_guard<Stream, RecursiveMutex>& lck, Stream& (*arg)(Stream&));
  29. template <typename Stream, typename RecursiveMutex, typename T>
  30. const stream_guard<Stream, RecursiveMutex>&
  31. operator>>(const stream_guard<Stream, RecursiveMutex>& lck, T& arg);
  32. template <typename Stream, typename RecursiveMutex, typename T>
  33. stream_guard<Stream, RecursiveMutex>
  34. operator<<(externally_locked_stream<Stream, RecursiveMutex>& mtx, T arg);
  35. template <typename Stream, typename RecursiveMutex>
  36. stream_guard<Stream, RecursiveMutex>
  37. operator<<(externally_locked_stream<Stream, RecursiveMutex>& mtx, Stream& (*arg)(Stream&));
  38. template <typename Stream, typename RecursiveMutex, typename T>
  39. stream_guard<Stream, RecursiveMutex>
  40. operator>>(externally_locked_stream<Stream, RecursiveMutex>& mtx, T& arg);
  41. }
  42. [/////////////////////////////////////////]
  43. [section:stream_guard Class `stream_guard`]
  44. #include <boost/thread/externally_locked_stream.hpp>
  45. namespace boost
  46. {
  47. template <class Stream, typename RecursiveMutex=recursive_mutex>
  48. class stream_guard
  49. {
  50. public:
  51. typedef typename externally_locked_stream<Stream, RecursiveMutex>::mutex_type mutex_type;
  52. // Constructors, Assignment and Destructors
  53. stream_guard(stream_guard const&) = delete;
  54. stream_guard& operator=(stream_guard const&) = delete;
  55. stream_guard(externally_locked_stream<Stream, RecursiveMutex>& mtx);
  56. stream_guard(externally_locked_stream<Stream, RecursiveMutex>& mtx, adopt_lock_t);
  57. stream_guard(stream_guard&& rhs);
  58. ~stream_guard();
  59. // Observers
  60. bool owns_lock(mutex_type const* l) const BOOST_NOEXCEPT;
  61. Stream& get() const;
  62. Stream& bypass() const;
  63. };
  64. }
  65. `stream_guard` is a model of __StrictLock.
  66. [//////////////////////////////////////////////////]
  67. [section:constructor `stream_guard(mutex_type & m)`]
  68. [variablelist
  69. [[Effects:] [Stores a reference to `m`. Invokes [lock_ref_link `m.lock()`].]]
  70. [[Throws:] [Any exception thrown by the call to [lock_ref_link `m.lock()`].]]
  71. ]
  72. [endsect]
  73. [////////////////////////////////////////////////////////////////////////////]
  74. [section:constructor_adopt `stream_guard(mutex_type & m,boost::adopt_lock_t)`]
  75. [variablelist
  76. [[Precondition:] [The current thread owns a lock on `m` equivalent to one
  77. obtained by a call to [lock_ref_link `m.lock()`].]]
  78. [[Effects:] [Stores a reference to `m`. Takes ownership of the lock state of
  79. `m`.]]
  80. [[Throws:] [Nothing.]]
  81. ]
  82. [endsect]
  83. [//////////////////////////////////////////////////////////]
  84. [section:move_constructor `stream_guard(stream_guard && m)`]
  85. [variablelist
  86. [[Effects:] [Stores a reference to `m`. Invokes [lock_ref_link `m.lock()`].]]
  87. [[Throws:] [Any exception thrown by the call to [lock_ref_link `m.lock()`].]]
  88. ]
  89. [endsect]
  90. [////////////////////////////////////]
  91. [section:destructor `~stream_guard()`]
  92. [variablelist
  93. [[Effects:] [Invokes [unlock_ref_link `m.unlock()`] on the __lockable_concept_type__
  94. object passed to the constructor.]]
  95. [[Throws:] [Nothing.]]
  96. ]
  97. [endsect]
  98. [endsect]
  99. [//////////////////////////////////////////////////////////////////]
  100. [section:externally_locked_stream Class `externally_locked_stream `]
  101. #include <boost/thread/externally_locked_stream.hpp>
  102. namespace boost
  103. {
  104. template <typename Stream, typename RecursiveMutex>
  105. class externally_locked_stream: public externally_locked<Stream&, RecursiveMutex>
  106. {
  107. public:
  108. // Constructors, Assignment and Destructors
  109. externally_locked_stream(externally_locked_stream const&) = delete;
  110. externally_locked_stream& operator=(externally_locked_stream const&) = delete;
  111. externally_locked_stream(Stream& stream, RecursiveMutex& mtx);
  112. // Modifiers
  113. stream_guard<Stream, RecursiveMutex> hold();
  114. };
  115. }
  116. `externally_locked_stream` cloaks a reference to a stream of type `Stream`, and actually
  117. provides full access to that object through the `get` member functions, provided you
  118. pass a reference to a strict lock object.
  119. [////////////////////////////////////////////////////////////////////////]
  120. [section:constructor `externally_locked_stream(Stream&, RecursiveMutex&)`]
  121. [variablelist
  122. [[Effects:] [Constructs an externally locked object storing the cloaked reference object and its locking mutex.]]
  123. ]
  124. [endsect]
  125. [/////////////////////]
  126. [section:hold `hold()`]
  127. [variablelist
  128. [[Returns:] [A stream_guard which will hold the mutex during it lifetime .]]
  129. ]
  130. [endsect]
  131. [endsect]
  132. [endsect] [/ref]
  133. [endsect] [/Externally Locked Streams]