Main Page   Namespace List   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

Derivhdl.hh

Go to the documentation of this file.
00001 #ifndef DERIVHDL_TMPL_HEADER
00002 #define DERIVHDL_TMPL_HEADER
00003 
00004 // ==== TEMPLATE HEADER Derivhdl.hh ====
00005 
00014 // 22-Jan-2002, Andras Aszodi
00015 
00016 // ==== STANDARD HEADERS ====
00017 
00018 #include <stdlib.h>
00019 
00020 // ==== MODULE HEADERS ====
00021 
00022 #include "Rangedimexc.hh"
00023 
00024 // ==== CLASSES ====
00025 
00026 namespace RazorBack {
00027 
00069 template <class BaseT_>
00070 class Derivhdl_
00071 {
00072     // data
00073     private:
00074             
00079     struct Refcount_
00080     {
00081         // data
00082         int Refcount;   //< reference counting
00083         BaseT_*  Ptr;    //< pointer to base type
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;     //< pointer to reference-counted internal object
00103     
00104     // methods
00105     public:
00106             
00108     Derivhdl_(): Dataptr(NULL) {}
00109     
00111     Derivhdl_(const BaseT_& B): Dataptr(new Refcount_(&B))
00112     {
00113         Dataptr->Refcount++;    // sets to 1
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     // hidden methods
00178     protected:
00179     
00180     void unlink_old();
00181 };
00182 // END OF CLASS Derivhdl_
00183 
00184 } // RazorBack
00185 
00186 // ==== END OF TEMPLATE HEADER Derivhdl.hh ====
00187 
00188 #ifdef INCLUDE_TMPL_DEFS
00189 #include "Derivhdl.cc"
00190 #endif
00191 
00192 #endif          // DERIVHDL_TMPL_HEADER

Generated at Wed Aug 21 09:33:40 2002 for The Razorback C++ Library: Miscellaneous by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001