00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef PALUDIS_GUARD_PALUDIS_UTIL_ACTIVE_OBJECT_PTR_HH
00021 #define PALUDIS_GUARD_PALUDIS_UTIL_ACTIVE_OBJECT_PTR_HH 1
00022
00023 #include <paludis/util/active_object_ptr-fwd.hh>
00024 #include <paludis/util/mutex.hh>
00025 #include <paludis/util/make_shared_ptr.hh>
00026 #include <tr1/memory>
00027
00028 namespace paludis
00029 {
00030 template <typename T_>
00031 class ActiveObjectPtr
00032 {
00033 private:
00034 T_ _ptr;
00035 std::tr1::shared_ptr<Mutex> _mutex;
00036
00037 class Deref
00038 {
00039 private:
00040 const ActiveObjectPtr * _ptr;
00041 std::tr1::shared_ptr<Lock> _lock;
00042
00043 public:
00044 Deref(const ActiveObjectPtr * p) :
00045 _ptr(p),
00046 _lock(make_shared_ptr(new Lock(*p->_mutex)))
00047 {
00048 }
00049
00050 const T_ & operator-> () const
00051 {
00052 return _ptr->_ptr;
00053 }
00054 };
00055
00056 friend class Deref;
00057
00058 public:
00059 ActiveObjectPtr(const T_ & t) :
00060 _ptr(t),
00061 _mutex(new Mutex)
00062 {
00063 }
00064
00065 ActiveObjectPtr(const ActiveObjectPtr & other) :
00066 _ptr(other._ptr),
00067 _mutex(other._mutex)
00068 {
00069 }
00070
00071 ~ActiveObjectPtr()
00072 {
00073 }
00074
00075 ActiveObjectPtr &
00076 operator= (const ActiveObjectPtr & other)
00077 {
00078 if (this != &other)
00079 {
00080 _ptr = other._ptr;
00081 _mutex = other._mutex;
00082 }
00083 return *this;
00084 }
00085
00086 Deref operator-> () const
00087 {
00088 return Deref(this);
00089 }
00090 };
00091 }
00092
00093 #endif