[/ / Copyright (c) 2013 Vicente J. Botet Escriba / / Distributed under the Boost Software License, Version 1.0. (See accompanying / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) /] [section:ext_locked_streams Externally Locked Streams - EXPERIMENTAL] [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 :(] [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.] [section:tutorial Tutorial] [endsect] [/tutorial] [/////////////////////] [section:ref Reference] #include namespace boost { template class externally_locked_stream; template class stream_guard; template struct is_strict_lock_sur_parole > : true_type {}; // Stream-like operators template const stream_guard& operator<<(const stream_guard& lck, T arg); template const stream_guard& operator<<(const stream_guard& lck, Stream& (*arg)(Stream&)); template const stream_guard& operator>>(const stream_guard& lck, T& arg); template stream_guard operator<<(externally_locked_stream& mtx, T arg); template stream_guard operator<<(externally_locked_stream& mtx, Stream& (*arg)(Stream&)); template stream_guard operator>>(externally_locked_stream& mtx, T& arg); } [/////////////////////////////////////////] [section:stream_guard Class `stream_guard`] #include namespace boost { template class stream_guard { public: typedef typename externally_locked_stream::mutex_type mutex_type; // Constructors, Assignment and Destructors stream_guard(stream_guard const&) = delete; stream_guard& operator=(stream_guard const&) = delete; stream_guard(externally_locked_stream& mtx); stream_guard(externally_locked_stream& mtx, adopt_lock_t); stream_guard(stream_guard&& rhs); ~stream_guard(); // Observers bool owns_lock(mutex_type const* l) const BOOST_NOEXCEPT; Stream& get() const; Stream& bypass() const; }; } `stream_guard` is a model of __StrictLock. [//////////////////////////////////////////////////] [section:constructor `stream_guard(mutex_type & m)`] [variablelist [[Effects:] [Stores a reference to `m`. Invokes [lock_ref_link `m.lock()`].]] [[Throws:] [Any exception thrown by the call to [lock_ref_link `m.lock()`].]] ] [endsect] [////////////////////////////////////////////////////////////////////////////] [section:constructor_adopt `stream_guard(mutex_type & m,boost::adopt_lock_t)`] [variablelist [[Precondition:] [The current thread owns a lock on `m` equivalent to one obtained by a call to [lock_ref_link `m.lock()`].]] [[Effects:] [Stores a reference to `m`. Takes ownership of the lock state of `m`.]] [[Throws:] [Nothing.]] ] [endsect] [//////////////////////////////////////////////////////////] [section:move_constructor `stream_guard(stream_guard && m)`] [variablelist [[Effects:] [Stores a reference to `m`. Invokes [lock_ref_link `m.lock()`].]] [[Throws:] [Any exception thrown by the call to [lock_ref_link `m.lock()`].]] ] [endsect] [////////////////////////////////////] [section:destructor `~stream_guard()`] [variablelist [[Effects:] [Invokes [unlock_ref_link `m.unlock()`] on the __lockable_concept_type__ object passed to the constructor.]] [[Throws:] [Nothing.]] ] [endsect] [endsect] [//////////////////////////////////////////////////////////////////] [section:externally_locked_stream Class `externally_locked_stream `] #include namespace boost { template class externally_locked_stream: public externally_locked { public: // Constructors, Assignment and Destructors externally_locked_stream(externally_locked_stream const&) = delete; externally_locked_stream& operator=(externally_locked_stream const&) = delete; externally_locked_stream(Stream& stream, RecursiveMutex& mtx); // Modifiers stream_guard hold(); }; } `externally_locked_stream` cloaks a reference to a stream of type `Stream`, and actually provides full access to that object through the `get` member functions, provided you pass a reference to a strict lock object. [////////////////////////////////////////////////////////////////////////] [section:constructor `externally_locked_stream(Stream&, RecursiveMutex&)`] [variablelist [[Effects:] [Constructs an externally locked object storing the cloaked reference object and its locking mutex.]] ] [endsect] [/////////////////////] [section:hold `hold()`] [variablelist [[Returns:] [A stream_guard which will hold the mutex during it lifetime .]] ] [endsect] [endsect] [endsect] [/ref] [endsect] [/Externally Locked Streams]