00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00043 #ifndef LIB_HANDLE_H
00044 #define LIB_HANDLE_H
00045
00046 #include "lib/nobug-init.hpp"
00047 #include "lib/bool-checkable.hpp"
00048
00049 #include <tr1/memory>
00050
00051
00052 namespace lib {
00053
00054 using std::tr1::shared_ptr;
00055 using std::tr1::weak_ptr;
00056
00057
00058
00069 template<class IMP>
00070 class Handle
00071 : public lib::BoolCheckable<Handle<IMP> >
00072 {
00073 protected:
00074 typedef std::tr1::shared_ptr<IMP> SmPtr;
00075
00076 SmPtr smPtr_;
00077
00078 public:
00079
00084 Handle ( )
00085 : smPtr_()
00086 { }
00087
00088 Handle (Handle const& r) : smPtr_(r.smPtr_) { }
00089 template<class Y> explicit Handle (shared_ptr<Y> const& r) : smPtr_(r) { }
00090 template<class Y> explicit Handle (weak_ptr<Y> const& wr) : smPtr_(wr) { }
00091 template<class Y> explicit Handle (std::auto_ptr<Y> & ar) : smPtr_(ar) { }
00092
00093 Handle& operator=(Handle const& r) { smPtr_ = r.smPtr_; return *this; }
00094 template<class Y> Handle& operator=(shared_ptr<Y> const& sr) { smPtr_ = sr; return *this; }
00095 template<class Y> Handle& operator=(std::auto_ptr<Y> & ar) { smPtr_ = ar; return *this; }
00096
00097
00103 template<typename DEL>
00104 Handle&
00105 activate (IMP* impl, DEL whenDead)
00106 {
00107 smPtr_.reset (impl, whenDead);
00108 return *this;
00109 }
00110
00113 Handle&
00114 activate(shared_ptr<IMP> const& impl)
00115 {
00116 smPtr_ = impl;
00117 return *this;
00118 }
00119
00126 void close () { smPtr_.reset(); }
00127
00128
00130 bool isValid() const { return bool(smPtr_);}
00131
00132
00133
00134
00135 protected:
00136 IMP&
00137 impl() const
00138 {
00139 REQUIRE (smPtr_.get(), "Lifecycle-Error");
00140 return *(smPtr_.get());
00141 }
00142 };
00143
00144
00145 }
00146 #endif