00001 #ifndef DERIVHDL_TMPL_HEADER
00002 #define DERIVHDL_TMPL_HEADER
00003
00004
00005
00014
00015
00016
00017
00018 #include <stdlib.h>
00019
00020
00021
00022 #include "Rangedimexc.hh"
00023
00024
00025
00026 namespace RazorBack {
00027
00069 template <class BaseT_>
00070 class Derivhdl_
00071 {
00072
00073 private:
00074
00079 struct Refcount_
00080 {
00081
00082 int Refcount;
00083 BaseT_* Ptr;
00084
00090 explicit Refcount_(const BaseT_* Bptr):
00091 Refcount(0), Ptr(Bptr==NULL? NULL: Bptr->virtual_ctor()) {}
00092
00094 Refcount_(const Refcount_& Rc):
00095 Refcount(0), Ptr(Rc.Ptr==NULL? NULL: Rc.Ptr->virtual_ctor()) {}
00096
00098 ~Refcount_() { delete Ptr; }
00099
00100 };
00101
00102 Refcount_ *Dataptr;
00103
00104
00105 public:
00106
00108 Derivhdl_(): Dataptr(NULL) {}
00109
00111 Derivhdl_(const BaseT_& B): Dataptr(new Refcount_(&B))
00112 {
00113 Dataptr->Refcount++;
00114 }
00115
00117 Derivhdl_(const Derivhdl_& D): Dataptr(D.Dataptr)
00118 {
00119 if (Dataptr!=NULL) Dataptr->Refcount++;
00120 }
00121
00123 ~Derivhdl_() { unlink_old(); }
00124
00126 Derivhdl_& operator=(const Derivhdl_& Rhs);
00127
00129 bool empty() const { return (Dataptr==NULL); }
00130
00135 const BaseT_& ref() const;
00136 BaseT_& ref();
00137
00142 const BaseT_* ptr() const;
00143 BaseT_* ptr();
00144
00156 template <class DerivedT_>
00157 const DerivedT_* real_ptr() const
00158 {
00159 return dynamic_cast<const DerivedT_*>(ptr());
00160 }
00161 template <class DerivedT_>
00162 DerivedT_* real_ptr()
00163 {
00164 return dynamic_cast<DerivedT_*>(ptr());
00165 }
00166
00172 bool operator<(const Derivhdl_<BaseT_>& Rhs) const
00173 {
00174 return (empty() || !Rhs.empty() && ref()<Rhs.ref());
00175 }
00176
00177
00178 protected:
00179
00180 void unlink_old();
00181 };
00182
00183
00184 }
00185
00186
00187
00188 #ifdef INCLUDE_TMPL_DEFS
00189 #include "Derivhdl.cc"
00190 #endif
00191
00192 #endif // DERIVHDL_TMPL_HEADER