00001 #ifndef toast_lockable_hpp_INCLUDED
00002 #define toast_lockable_hpp_INCLUDED
00003
00004 #include <sstream>
00005 #include <boost/shared_ptr.hpp>
00006 #include <boost/thread/recursive_mutex.hpp>
00007 #include <boost/thread/mutex.hpp>
00008 #include <boost/bind.hpp>
00009 #include <toast/contracts.hpp>
00010
00011 namespace toast {
00012 namespace async {
00013
00020 template <typename T>
00021 class locked;
00022
00031 template <typename T>
00032 class lockable
00033 {
00034 boost::mutex mutex_;
00035 T data_;
00036 template <typename Y>
00037 friend class locked;
00038
00039 public:
00040
00042 #ifndef TOAST_LOCKABLE_MAX_CONSTRUCTOR_ARITY
00043 #define TOAST_LOCKABLE_MAX_CONSTRUCTOR_ARITY 10
00044 #endif
00045
00046
00047 #define BOOST_PP_ITERATION_LIMITS (0, TOAST_LOCKABLE_MAX_CONSTRUCTOR_ARITY)
00048 #define BOOST_PP_FILENAME_1 <toast/async/lockable.hpp>
00049 #include BOOST_PP_ITERATE()
00051
00052 };
00053
00060 template <typename T>
00061 class locked
00062 {
00063 T &data_;
00064 boost::mutex::scoped_lock lock_;
00065
00066 public:
00067 template <typename Y>
00068 explicit locked(lockable<Y> &t)
00069 : data_(t.data_), lock_(t.mutex_) {}
00070
00071 T &operator*() const { return data_; }
00072 T *operator->() const { return &data_; }
00073 };
00074
00075 }
00076 }
00077
00080 #elif BOOST_PP_IS_ITERATING
00081
00082 #define N BOOST_PP_ITERATION()
00083
00084 #if N > 0
00085 template <BOOST_PP_ENUM_PARAMS(N, typename A) >
00086 #endif
00087 lockable( BOOST_PP_ENUM_BINARY_PARAMS(N, A, a) )
00088 : data_( BOOST_PP_ENUM_PARAMS(N,a) ) {}
00089
00090 #undef N
00091
00092 #endif