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)
7 #ifndef BOOST_THREAD_EXTERNALLY_LOCKED_STREAM_HPP
8 #define BOOST_THREAD_EXTERNALLY_LOCKED_STREAM_HPP
10 #include <boost/thread/detail/config.hpp>
11 #include <boost/thread/detail/move.hpp>
12 #include <boost/thread/detail/delete.hpp>
14 #include <boost/thread/externally_locked.hpp>
15 #include <boost/thread/lock_traits.hpp>
16 #include <boost/thread/recursive_mutex.hpp>
17 #include <boost/thread/strict_lock.hpp>
19 #include <boost/config/abi_prefix.hpp>
24 template <typename Stream, typename RecursiveMutex=recursive_mutex>
25 class externally_locked_stream;
27 template <class Stream, typename RecursiveMutex=recursive_mutex>
31 friend class externally_locked_stream<Stream, RecursiveMutex> ;
33 typedef typename externally_locked_stream<Stream, RecursiveMutex>::mutex_type mutex_type;
35 BOOST_THREAD_MOVABLE_ONLY( stream_guard)
37 stream_guard(externally_locked_stream<Stream, RecursiveMutex>& mtx) :
43 stream_guard(externally_locked_stream<Stream, RecursiveMutex>& mtx, adopt_lock_t) :
48 stream_guard(BOOST_THREAD_RV_REF(stream_guard) rhs) BOOST_NOEXCEPT
56 if (mtx_ != 0) mtx_->unlock();
59 bool owns_lock(const mutex_type * l) const BOOST_NOEXCEPT
61 return l == mtx_->mutex();
69 BOOST_THREAD_ASSERT_PRECONDITION( mtx_, lock_error() );
70 return mtx_->get(*this);
72 Stream& bypass() const
79 externally_locked_stream<Stream, RecursiveMutex>* mtx_;
82 template <typename Stream, typename RecursiveMutex>
83 struct is_strict_lock_sur_parole<stream_guard<Stream, RecursiveMutex> > : true_type
88 * externally_locked_stream cloaks a reference to an stream of type Stream, and actually
89 * provides full access to that object through the get and set member functions, provided you
90 * pass a reference to a strict lock object.
93 //[externally_locked_stream
94 template <typename Stream, typename RecursiveMutex>
95 class externally_locked_stream: public externally_locked<Stream&, RecursiveMutex>
97 typedef externally_locked<Stream&, RecursiveMutex> base_type;
99 BOOST_THREAD_NO_COPYABLE( externally_locked_stream)
102 * Effects: Constructs an externally locked object storing the cloaked reference object.
104 externally_locked_stream(Stream& stream, RecursiveMutex& mtx) BOOST_NOEXCEPT :
105 base_type(stream, mtx)
109 stream_guard<Stream, RecursiveMutex> hold() BOOST_NOEXCEPT
111 return stream_guard<Stream, RecursiveMutex> (*this);
113 Stream& bypass() const
115 stream_guard<Stream, RecursiveMutex> lk(*this);
121 template <typename Stream, typename RecursiveMutex, typename T>
122 inline const stream_guard<Stream, RecursiveMutex>& operator<<(const stream_guard<Stream, RecursiveMutex>& lck, T arg)
128 template <typename Stream, typename RecursiveMutex>
129 inline const stream_guard<Stream, RecursiveMutex>& operator<<(const stream_guard<Stream, RecursiveMutex>& lck, Stream& (*arg)(Stream&))
135 template <typename Stream, typename RecursiveMutex, typename T>
136 inline const stream_guard<Stream, RecursiveMutex>& operator>>(const stream_guard<Stream, RecursiveMutex>& lck, T& arg)
142 template <typename Stream, typename RecursiveMutex, typename T>
143 inline stream_guard<Stream, RecursiveMutex> operator<<(externally_locked_stream<Stream, RecursiveMutex>& mtx, T arg)
145 stream_guard<Stream, RecursiveMutex> lk(mtx);
147 return boost::move(lk);
150 template <typename Stream, typename RecursiveMutex>
151 inline stream_guard<Stream, RecursiveMutex> operator<<(externally_locked_stream<Stream, RecursiveMutex>& mtx, Stream& (*arg)(Stream&))
153 stream_guard<Stream, RecursiveMutex> lk(mtx);
155 return boost::move(lk);
158 template <typename Stream, typename RecursiveMutex, typename T>
159 inline stream_guard<Stream, RecursiveMutex> operator>>(externally_locked_stream<Stream, RecursiveMutex>& mtx, T& arg)
161 stream_guard<Stream, RecursiveMutex> lk(mtx);
163 return boost::move(lk);
168 #include <boost/config/abi_suffix.hpp>