TF++, Miscellaneous classes and modules in C++:
handle.h
Go to the documentation of this file.
1 
36 // include guard
37 #ifndef TF_HANDLE_H_VERSION
38 
39 #define TF_HANDLE_H_VERSION \
40  "TF_HANDLE_H V1.0 "
41 
42 namespace tfxx {
43 
49 
81 template<class X>
82 class ConstHandle {
83  public:
84  typedef X Tmutableobject;
85  typedef const X Tobject;
87  typedef Tcontainer Tcoc;
89  typedef const Tobject* Tpointer;
90  typedef const Tobject& Treference;
91 
101  Mrep(new Tmutableobject(p)), Mpcount(new int(1)) { }
102 
114  Mrep(p), Mpcount(new int(1)) { }
115 
126  { (*Mpcount)++; }
127 
131  ~ConstHandle() { if (--(*Mpcount)==0) { delete Mrep; delete Mpcount; } }
132 
137  Tpointer operator->() const { return Mrep; }
138 
143  Treference operator*() const { return *Mrep; }
144 
149  {
150  if (Mrep == h.Mrep) return *this;
151  if (--(*Mpcount) == 0) { delete Mrep; delete Mpcount; }
152  Mrep=h.Mrep;
153  Mpcount=h.Mpcount;
154  (*Mpcount)++;
155  return *this;
156  }
157 
158  protected:
160  Tmutableobject* pointer() const { return Mrep;}
161 
163  Tmutableobject& reference() const { return *Mrep;}
164 
165  private:
169  int* Mpcount;
170 }; // template class ConstHandle
171 
174 template<class X>
175 class Handle: public ConstHandle<X> {
176  public:
177  typedef X Tobject;
180  typedef Tbase Tcoc;
182  typedef Tobject* Tpointer;
183  typedef Tobject& Treference;
184 
194  Handle(const Treference p): Tbase(p) { }
195 
206  Handle(Tpointer p): Tbase(p) { }
207 
217  Handle(const Handle& h): Tbase(h) { }
218 
223  Tpointer operator->() const { return Tbase::pointer(); }
224 
229  Treference operator*() const { return Tbase::reference(); }
230 
235  {
236  Tbase::operator=(h);
237  return *this;
238  }
239 }; // template class Handle
240 
243 } // namespace tfxx
244 
245 #endif // TF_HANDLE_H_VERSION (includeguard)
246 
247 /* ----- END OF handle.h ----- */
Tobject & Treference
Definition: handle.h:183
Tpointer operator->() const
operator #tfxx::THandle<class X>::operator->()#
Definition: handle.h:137
ConstHandle & operator=(const ConstHandle &h)
operator #tfxx::THandle<class X>::operator=()# book-keeping asignment operator
Definition: handle.h:148
Tbase Tcoc
Definition: handle.h:180
Tmutableobject * Mrep
internal pointer to the handled object
Definition: handle.h:167
template #tfxx::THandle<class TObj>#:
Definition: handle.h:82
Treference operator*() const
operator #tfxx::THandle<class X>::operator*()#
Definition: handle.h:229
Handle & operator=(const Handle &h)
operator #tfxx::THandle<class X>::operator=()# book-keeping asignment operator
Definition: handle.h:234
int * Mpcount
usage counter
Definition: handle.h:169
Tmutableobject & reference() const
expose the object to derived classes
Definition: handle.h:163
const Tobject & Treference
Definition: handle.h:90
ConstHandle< Tobject > Tbase
Definition: handle.h:178
ConstHandle(Treference p)
constructor #tfxx::THandle<class X>::THandle#:
Definition: handle.h:100
Handle< Tobject > Tcontainer
Definition: handle.h:179
Tmutableobject * pointer() const
expose the pointer to derived classes
Definition: handle.h:160
const Tobject * Tpointer
Definition: handle.h:89
Tobject * Tpointer
Definition: handle.h:182
ConstHandle(Tmutableobject *p)
Definition: handle.h:113
Tpointer operator->() const
operator #tfxx::THandle<class X>::operator->()#
Definition: handle.h:223
Handle(const Handle &h)
constructor #tfxx::THandle<class X>::THandle#:
Definition: handle.h:217
Tcontainer Tcoc
Definition: handle.h:87
~ConstHandle()
desctructor #tfxx::THandle<class X>::~THandle# book-keeping destructor
Definition: handle.h:131
Tcoc Tcontainer_of_const
Definition: handle.h:181
ConstHandle(const ConstHandle &h)
constructor #tfxx::THandle<class X>::THandle#:
Definition: handle.h:125
Tcoc Tcontainer_of_const
Definition: handle.h:88
ConstHandle< Tobject > Tcontainer
Definition: handle.h:86
const X Tobject
Definition: handle.h:85
Handle(const Treference p)
constructor #tfxx::THandle<class X>::THandle#:
Definition: handle.h:194
Handle(Tpointer p)
Definition: handle.h:206
Treference operator*() const
operator #tfxx::THandle<class X>::operator*()#
Definition: handle.h:143
Namespace containing all code of library libtfxx.