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 #include <string>
0008
0009 namespace tools {
0010
0011 class base_handle {
0012 static const std::string& s_class() {
0013 static const std::string s_v("tools::handle");
0014 return s_v;
0015 }
0016 public:
0017 virtual void* object() const = 0;
0018 virtual base_handle* copy() = 0; //can't be const.
0019 virtual void disown() = 0;
0020 public:
0021 base_handle(){
0022 }
0023 base_handle(const std::string& a_class):m_class(a_class){
0024 }
0025 virtual ~base_handle(){
0026 }
0027 protected:
0028 base_handle(base_handle& a_from):m_class(a_from.m_class){
0029 }
0030 private:
0031 base_handle& operator=(base_handle& a_from){
0032 m_class = a_from.m_class;
0033 return *this;
0034 }
0035 public:
0036 const std::string& object_class() const {return m_class;}
0037 private:
0038 std::string m_class;
0039 };
0040
0041 template <class T>
0042 class handle : public base_handle {
0043 typedef base_handle parent;
0044 public:
0045 virtual void* object() const {return m_obj;}
0046 virtual base_handle* copy() {return new handle<T>(*this);}
0047 virtual void disown() {m_owner = false;}
0048 public:
0049 handle(T* a_obj,bool a_owner = true):parent(),m_obj(a_obj),m_owner(a_owner){}
0050 handle(const std::string& a_class,T* a_obj,bool a_owner = true):parent(a_class),m_obj(a_obj),m_owner(a_owner){}
0051 virtual ~handle(){if(m_owner) delete m_obj;}
0052 private:
0053 handle(handle& a_from):parent(a_from){
0054 m_obj = a_from.m_obj;
0055 if(a_from.m_owner) {
0056 // this take ownership.
0057 m_owner = true;
0058 a_from.m_owner = false;
0059 } else {
0060 m_owner = false;
0061 }
0062 }
0063 private:
0064 // in principle the below are not used.
0065 //handle(const handle& a_from){}
0066 //handle& operator=(const handle&){return *this;}
0067 handle& operator=(handle&){return *this;}
0068 protected:
0069 T* m_obj;
0070 bool m_owner;
0071 };
0072
0073 }
0074
0075 #endif