Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/thread/externally_locked_stream.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // (C) Copyright 2012 Vicente J. Botet Escriba
0002 // Distributed under the Boost Software License, Version 1.0. (See
0003 // accompanying file LICENSE_1_0.txt or copy at
0004 // http://www.boost.org/LICENSE_1_0.txt)
0005 
0006 
0007 #ifndef BOOST_THREAD_EXTERNALLY_LOCKED_STREAM_HPP
0008 #define BOOST_THREAD_EXTERNALLY_LOCKED_STREAM_HPP
0009 
0010 #include <boost/thread/detail/config.hpp>
0011 #include <boost/thread/detail/move.hpp>
0012 #include <boost/thread/detail/delete.hpp>
0013 
0014 #include <boost/thread/externally_locked.hpp>
0015 #include <boost/thread/lock_traits.hpp>
0016 #include <boost/thread/recursive_mutex.hpp>
0017 #include <boost/thread/strict_lock.hpp>
0018 
0019 #include <boost/config/abi_prefix.hpp>
0020 
0021 namespace boost
0022 {
0023 
0024   template <typename Stream, typename RecursiveMutex=recursive_mutex>
0025   class externally_locked_stream;
0026 
0027   template <class Stream, typename RecursiveMutex=recursive_mutex>
0028   class stream_guard
0029   {
0030 
0031     friend class externally_locked_stream<Stream, RecursiveMutex> ;
0032   public:
0033     typedef typename externally_locked_stream<Stream, RecursiveMutex>::mutex_type mutex_type;
0034 
0035     BOOST_THREAD_MOVABLE_ONLY( stream_guard)
0036 
0037     stream_guard(externally_locked_stream<Stream, RecursiveMutex>& mtx) :
0038       mtx_(&mtx)
0039     {
0040       mtx.lock();
0041     }
0042 
0043     stream_guard(externally_locked_stream<Stream, RecursiveMutex>& mtx, adopt_lock_t) :
0044       mtx_(&mtx)
0045     {
0046     }
0047 
0048     stream_guard(BOOST_THREAD_RV_REF(stream_guard) rhs) BOOST_NOEXCEPT
0049     : mtx_(rhs.mtx_)
0050     {
0051       rhs.mtx_= 0;
0052     }
0053 
0054     ~stream_guard()
0055     {
0056       if (mtx_ != 0) mtx_->unlock();
0057     }
0058 
0059     bool owns_lock(const mutex_type * l) const BOOST_NOEXCEPT
0060     {
0061       return l == mtx_->mutex();
0062     }
0063 
0064     /**
0065      * @Requires mtx_
0066      */
0067     Stream& get() const
0068     {
0069       BOOST_THREAD_ASSERT_PRECONDITION(  mtx_, lock_error() );
0070       return mtx_->get(*this);
0071     }
0072     Stream& bypass() const
0073     {
0074       return get();
0075     }
0076 
0077 
0078   private:
0079     externally_locked_stream<Stream, RecursiveMutex>* mtx_;
0080   };
0081 
0082   template <typename Stream, typename RecursiveMutex>
0083   struct is_strict_lock_sur_parole<stream_guard<Stream, RecursiveMutex> > : true_type
0084   {
0085   };
0086 
0087   /**
0088    * externally_locked_stream cloaks a reference to an stream of type Stream, and actually
0089    * provides full access to that object through the get and set member functions, provided you
0090    * pass a reference to a strict lock object.
0091    */
0092 
0093   //[externally_locked_stream
0094   template <typename Stream, typename RecursiveMutex>
0095   class externally_locked_stream: public externally_locked<Stream&, RecursiveMutex>
0096   {
0097     typedef externally_locked<Stream&, RecursiveMutex> base_type;
0098   public:
0099     BOOST_THREAD_NO_COPYABLE( externally_locked_stream)
0100 
0101     /**
0102      * Effects: Constructs an externally locked object storing the cloaked reference object.
0103      */
0104     externally_locked_stream(Stream& stream, RecursiveMutex& mtx) BOOST_NOEXCEPT :
0105       base_type(stream, mtx)
0106     {
0107     }
0108 
0109     stream_guard<Stream, RecursiveMutex> hold() BOOST_NOEXCEPT
0110     {
0111       return stream_guard<Stream, RecursiveMutex> (*this);
0112     }
0113     Stream& bypass() const
0114     {
0115       stream_guard<Stream, RecursiveMutex> lk(*this);
0116       return get(lk);
0117     }
0118   };
0119   //]
0120 
0121   template <typename Stream, typename RecursiveMutex, typename T>
0122   inline const stream_guard<Stream, RecursiveMutex>& operator<<(const stream_guard<Stream, RecursiveMutex>& lck, T arg)
0123   {
0124     lck.get() << arg;
0125     return lck;
0126   }
0127 
0128   template <typename Stream, typename RecursiveMutex>
0129   inline const stream_guard<Stream, RecursiveMutex>& operator<<(const stream_guard<Stream, RecursiveMutex>& lck, Stream& (*arg)(Stream&))
0130   {
0131     lck.get() << arg;
0132     return lck;
0133   }
0134 
0135   template <typename Stream, typename RecursiveMutex, typename T>
0136   inline const stream_guard<Stream, RecursiveMutex>& operator>>(const stream_guard<Stream, RecursiveMutex>& lck, T& arg)
0137   {
0138     lck.get() >> arg;
0139     return lck;
0140   }
0141 
0142   template <typename Stream, typename RecursiveMutex, typename T>
0143   inline stream_guard<Stream, RecursiveMutex> operator<<(externally_locked_stream<Stream, RecursiveMutex>& mtx, T arg)
0144   {
0145     stream_guard<Stream, RecursiveMutex> lk(mtx);
0146     mtx.get(lk) << arg;
0147     return boost::move(lk);
0148   }
0149 
0150   template <typename Stream, typename RecursiveMutex>
0151   inline stream_guard<Stream, RecursiveMutex> operator<<(externally_locked_stream<Stream, RecursiveMutex>& mtx, Stream& (*arg)(Stream&))
0152   {
0153     stream_guard<Stream, RecursiveMutex> lk(mtx);
0154     mtx.get(lk) << arg;
0155     return boost::move(lk);
0156   }
0157 
0158   template <typename Stream, typename RecursiveMutex, typename T>
0159   inline stream_guard<Stream, RecursiveMutex> operator>>(externally_locked_stream<Stream, RecursiveMutex>& mtx, T& arg)
0160   {
0161     stream_guard<Stream, RecursiveMutex> lk(mtx);
0162     mtx.get(lk) >> arg;
0163     return boost::move(lk);
0164   }
0165 
0166 }
0167 
0168 #include <boost/config/abi_suffix.hpp>
0169 
0170 #endif // header