Warning, /include/Geant4/tools/sg/holder 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_sg_holder
0005 #define tools_sg_holder
0006
0007 // class to manage an object.
0008 // Only one holder owns the object. In case
0009 // of holder copy(), the new holder receives a null object.
0010
0011 #include "node"
0012
0013 #include "sf_string"
0014
0015 namespace tools {
0016 namespace sg {
0017
0018 class base_holder : public node {
0019 TOOLS_NODE(base_holder,tools::sg::base_holder,node)
0020 public:
0021 base_holder(const std::string& a_name):parent(),m_name(a_name) {}
0022 virtual ~base_holder(){}
0023 public:
0024 base_holder(const base_holder& a_from):parent(a_from),m_name(a_from.m_name) {}
0025 base_holder& operator=(const base_holder& a_from){
0026 parent::operator=(a_from);
0027 m_name = a_from.m_name;
0028 return *this;
0029 }
0030 public:
0031 const std::string& name() const {return m_name;}
0032 protected:
0033 std::string m_name;
0034 };
0035
0036 template <class T>
0037 class holder : public base_holder {
0038 typedef base_holder parent;
0039 public:
0040 //WARNING : we do not put the T class name in the name of this class.
0041 // We put it in the class_name field.
0042 // We do that because of the bsg file format in order to be able
0043 // to read back the object without having the T class around.
0044 TOOLS_T_SCLASS(T,tools::sg::holder)
0045 public:
0046 virtual void* cast(const std::string& a_class) const {
0047 if(void* p = cmp_cast< holder<T> >(this,a_class)) {return p;}
0048 return parent::cast(a_class);
0049 }
0050 virtual node* copy() const {return new holder(*this);}
0051 virtual const std::string& s_cls() const {return s_class();}
0052 public:
0053 sf_string class_name;
0054 public:
0055 virtual const desc_fields& node_desc_fields() const {
0056 TOOLS_FIELD_DESC_NODE_CLASS(tools::sg::holder)
0057 static const desc_fields s_v(parent::node_desc_fields(),1, //WARNING : take care of count.
0058 TOOLS_ARG_FIELD_DESC(class_name)
0059 );
0060 return s_v;
0061 }
0062 private:
0063 void add_fields(){
0064 add_field(&class_name);
0065 }
0066 public:
0067 holder(T* a_obj = 0,const std::string& a_name = "",bool a_own=true)
0068 :parent(a_name)
0069 ,class_name(T::s_class())
0070 ,m_obj(a_obj) //it takes a_obj ownership.
0071 ,m_own(a_own)
0072 {
0073 add_fields();
0074 }
0075
0076 virtual ~holder(){if(m_own) delete m_obj;}
0077 public:
0078 //any copy of a holder does NOT own any object (and we empty the name).
0079 holder(const holder& a_from)
0080 :parent(a_from)
0081 ,class_name(std::string())
0082 ,m_obj(0)
0083 ,m_own(false)
0084 {
0085 parent::m_name.clear();
0086 add_fields();
0087 }
0088 holder& operator=(const holder& a_from){
0089 parent::operator=(a_from);
0090 class_name.value().clear();
0091 m_obj = 0;
0092 m_own = false;
0093 parent::m_name.clear();
0094 return *this;
0095 }
0096 public:
0097 const T* object() const {return m_obj;}
0098 T* object() {return m_obj;}
0099 protected:
0100 T* m_obj;
0101 bool m_own;
0102 };
0103
0104 template <class T>
0105 inline holder<T>* cast_holder(node& a_node) {
0106 typedef holder<T> h_t;
0107 h_t* _h = (h_t*)a_node.cast(h_t::s_class());
0108 if(!_h) return 0;
0109 return (_h->class_name.value()==T::s_class()?_h:0);
0110 }
0111
0112 template <class T>
0113 inline const holder<T>* cast_holder(const node& a_node) {
0114 typedef holder<T> h_t;
0115 h_t* _h = (h_t*)a_node.cast(h_t::s_class());
0116 if(!_h) return 0;
0117 return (_h->class_name.value()==T::s_class()?_h:0);
0118 }
0119
0120 template <class T>
0121 inline const T* cast_holder_object(const node& a_node) {
0122 typedef holder<T> h_t;
0123 h_t* _h = (h_t*)a_node.cast(h_t::s_class());
0124 if(!_h) return 0;
0125 return (_h->class_name.value()==T::s_class()?_h->object():0);
0126 }
0127 template <class T>
0128 inline T* cast_holder_object(node& a_node) {
0129 typedef holder<T> h_t;
0130 h_t* _h = (h_t*)a_node.cast(h_t::s_class());
0131 if(!_h) return 0;
0132 return (_h->class_name.value()==T::s_class()?_h->object():0);
0133 }
0134
0135 template <class T>
0136 inline void remove_holders(std::vector<node*>& a_vec){
0137 typedef holder<T> h_t;
0138
0139 std::vector<node*>::iterator it;
0140 for(it=a_vec.begin();it!=a_vec.end();) {
0141 if(h_t* h = cast_holder<T>(*(*it))){
0142 it = a_vec.erase(it);
0143 delete h;
0144 continue;
0145 }
0146
0147 ++it;
0148 }
0149 }
0150
0151 template <class T>
0152 inline void remove_holders(std::vector<node*>& a_vec,const std::string& a_name){
0153 typedef holder<T> h_t;
0154
0155 std::vector<node*>::iterator it;
0156 for(it=a_vec.begin();it!=a_vec.end();) {
0157 if(h_t* h = cast_holder<T>(*(*it))){
0158 if(h->name()==a_name) {
0159 it = a_vec.erase(it);
0160 delete h;
0161 continue;
0162 }
0163 }
0164
0165 ++it;
0166 }
0167 }
0168
0169 template <class T>
0170 inline T* find_holder(std::vector<node*>& a_vec,const std::string& a_name) {
0171 //return the first named found.
0172
0173 typedef holder<T> h_t;
0174 std::vector<node*>::iterator it;
0175 for(it=a_vec.begin();it!=a_vec.end();++it) {
0176 if(h_t* h = cast_holder<T>(*(*it))){
0177 if(h->name()==a_name) return h->object();
0178 }
0179 }
0180 return 0;
0181 }
0182
0183 template <class T>
0184 inline T* find_first_holder(std::vector<node*>& a_vec){
0185 //return the first T found.
0186
0187 typedef holder<T> h_t;
0188 std::vector<node*>::iterator it;
0189 for(it=a_vec.begin();it!=a_vec.end();++it) {
0190 if(h_t* h = cast_holder<T>(*(*it))) return h->object();
0191 }
0192
0193
0194 return 0;
0195 }
0196
0197 }}
0198
0199 #endif