Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/Geant4/tools/handle is written in an unsupported language. File is not indexed.

0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003 
0004 #ifndef tools_handle
0005 #define tools_handle
0006 
0007 #ifdef TOOLS_MEM
0008 #include "mem"
0009 #endif
0010 
0011 #include <string>
0012 
0013 namespace tools {
0014 
0015 class base_handle {
0016   static const std::string& s_class() {
0017     static const std::string s_v("tools::handle");
0018     return s_v;
0019   }
0020 public:
0021   virtual void* object() const = 0;
0022   virtual base_handle* copy() = 0; //can't be const.
0023   virtual void disown() = 0;
0024 public:
0025   base_handle(){
0026 #ifdef TOOLS_MEM
0027     mem::increment(s_class().c_str());
0028 #endif
0029   }
0030   base_handle(const std::string& a_class):m_class(a_class){
0031 #ifdef TOOLS_MEM
0032     mem::increment(s_class().c_str());
0033 #endif
0034   }
0035   virtual ~base_handle(){
0036 #ifdef TOOLS_MEM
0037     mem::decrement(s_class().c_str());
0038 #endif
0039   }
0040 protected:
0041   base_handle(base_handle& a_from):m_class(a_from.m_class){
0042 #ifdef TOOLS_MEM
0043     mem::increment(s_class().c_str());
0044 #endif
0045   }
0046 private:
0047   base_handle& operator=(base_handle& a_from){
0048     m_class = a_from.m_class;
0049     return *this;
0050   }
0051 public:
0052   const std::string& object_class() const {return m_class;}
0053 private:
0054   std::string m_class;
0055 };
0056 
0057 template <class T>
0058 class handle : public base_handle {
0059   typedef base_handle parent;
0060 public:
0061   virtual void* object() const {return m_obj;}
0062   virtual base_handle* copy() {return new handle<T>(*this);}
0063   virtual void disown() {m_owner = false;}
0064 public:
0065   handle(T* a_obj,bool a_owner = true):parent(),m_obj(a_obj),m_owner(a_owner){}
0066   handle(const std::string& a_class,T* a_obj,bool a_owner = true):parent(a_class),m_obj(a_obj),m_owner(a_owner){}
0067   virtual ~handle(){if(m_owner) delete m_obj;}
0068 private:
0069   handle(handle& a_from):parent(a_from){
0070     m_obj = a_from.m_obj;
0071     if(a_from.m_owner) {
0072       // this take ownership.
0073       m_owner = true;
0074       a_from.m_owner = false;
0075     } else {
0076       m_owner = false;
0077     }
0078     //a_from.m_obj = 0; //we do not remove the obj ref in a_from.
0079   }
0080 private:
0081   // in principle the below are not used.
0082   //handle(const handle& a_from){}
0083   //handle& operator=(const handle&){return *this;}
0084   handle& operator=(handle&){return *this;}
0085 protected:
0086   T* m_obj;
0087   bool m_owner;
0088 };
0089 
0090 }
0091 
0092 #endif