g4tools  5.4.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
holder
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_sg_holder
5 #define tools_sg_holder
6 
7 // class to manage an object.
8 // Only one holder owns the object. In case
9 // of holder copy(), the new holder receives a null object.
10 
11 #include "node"
12 
13 #include "sf_string"
14 
15 namespace tools {
16 namespace sg {
17 
18 class base_holder : public node {
20 public:
21  base_holder(const std::string& a_name):parent(),m_name(a_name) {}
22  virtual ~base_holder(){}
23 public:
24  base_holder(const base_holder& a_from):parent(a_from),m_name(a_from.m_name) {}
26  parent::operator=(a_from);
27  m_name = a_from.m_name;
28  return *this;
29  }
30 public:
31  const std::string& name() const {return m_name;}
32 protected:
33  std::string m_name;
34 };
35 
36 template <class T>
37 class holder : public base_holder {
38  typedef base_holder parent;
39 public:
40  //WARNING : we do not put the T class name in the name of this class.
41  // We put it in the class_name field.
42  // We do that because of the bsg file format in order to be able
43  // to read back the object without having the T class around.
45 public:
46  virtual void* cast(const std::string& a_class) const {
47  if(void* p = cmp_cast< holder<T> >(this,a_class)) {return p;}
48  return parent::cast(a_class);
49  }
50  virtual node* copy() const {return new holder(*this);}
51  virtual const std::string& s_cls() const {return s_class();}
52 public:
54 public:
55  virtual const desc_fields& node_desc_fields() const {
57  static const desc_fields s_v(parent::node_desc_fields(),1, //WARNING : take care of count.
59  );
60  return s_v;
61  }
62 private:
63  void add_fields(){
65  }
66 public:
67  holder(T* a_obj = 0,const std::string& a_name = "",bool a_own=true)
68  :parent(a_name)
69  ,class_name(T::s_class())
70  ,m_obj(a_obj) //it takes a_obj ownership.
71  ,m_own(a_own)
72  {
73  add_fields();
74  }
75 
76  virtual ~holder(){if(m_own) delete m_obj;}
77 public:
78  //any copy of a holder does NOT own any object (and we empty the name).
79  holder(const holder& a_from)
80  :parent(a_from)
81  ,class_name(std::string())
82  ,m_obj(0)
83  ,m_own(false)
84  {
85  parent::m_name.clear();
86  add_fields();
87  }
88  holder& operator=(const holder& a_from){
89  parent::operator=(a_from);
90  class_name.value().clear();
91  m_obj = 0;
92  m_own = false;
93  parent::m_name.clear();
94  return *this;
95  }
96 public:
97  const T* object() const {return m_obj;}
98  T* object() {return m_obj;}
99 protected:
100  T* m_obj;
101  bool m_own;
102 };
103 
104 template <class T>
105 inline holder<T>* cast_holder(node& a_node) {
106  typedef holder<T> h_t;
107  h_t* _h = (h_t*)a_node.cast(h_t::s_class());
108  if(!_h) return 0;
109  return (_h->class_name.value()==T::s_class()?_h:0);
110 }
111 
112 template <class T>
113 inline const holder<T>* cast_holder(const node& a_node) {
114  typedef holder<T> h_t;
115  h_t* _h = (h_t*)a_node.cast(h_t::s_class());
116  if(!_h) return 0;
117  return (_h->class_name.value()==T::s_class()?_h:0);
118 }
119 
120 template <class T>
121 inline const T* cast_holder_object(const node& a_node) {
122  typedef holder<T> h_t;
123  h_t* _h = (h_t*)a_node.cast(h_t::s_class());
124  if(!_h) return 0;
125  return (_h->class_name.value()==T::s_class()?_h->object():0);
126 }
127 template <class T>
128 inline T* cast_holder_object(node& a_node) {
129  typedef holder<T> h_t;
130  h_t* _h = (h_t*)a_node.cast(h_t::s_class());
131  if(!_h) return 0;
132  return (_h->class_name.value()==T::s_class()?_h->object():0);
133 }
134 
135 template <class T>
136 inline void remove_holders(std::vector<node*>& a_vec){
137  typedef holder<T> h_t;
138 
139  std::vector<node*>::iterator it;
140  for(it=a_vec.begin();it!=a_vec.end();) {
141  if(h_t* h = cast_holder<T>(*(*it))){
142  it = a_vec.erase(it);
143  delete h;
144  continue;
145  }
146 
147  ++it;
148  }
149 }
150 
151 template <class T>
152 inline void remove_holders(std::vector<node*>& a_vec,const std::string& a_name){
153  typedef holder<T> h_t;
154 
155  std::vector<node*>::iterator it;
156  for(it=a_vec.begin();it!=a_vec.end();) {
157  if(h_t* h = cast_holder<T>(*(*it))){
158  if(h->name()==a_name) {
159  it = a_vec.erase(it);
160  delete h;
161  continue;
162  }
163  }
164 
165  ++it;
166  }
167 }
168 
169 template <class T>
170 inline T* find_holder(std::vector<node*>& a_vec,const std::string& a_name) {
171  //return the first named found.
172 
173  typedef holder<T> h_t;
174  std::vector<node*>::iterator it;
175  for(it=a_vec.begin();it!=a_vec.end();++it) {
176  if(h_t* h = cast_holder<T>(*(*it))){
177  if(h->name()==a_name) return h->object();
178  }
179  }
180  return 0;
181 }
182 
183 template <class T>
184 inline T* find_first_holder(std::vector<node*>& a_vec){
185  //return the first T found.
186 
187  typedef holder<T> h_t;
188  std::vector<node*>::iterator it;
189  for(it=a_vec.begin();it!=a_vec.end();++it) {
190  if(h_t* h = cast_holder<T>(*(*it))) return h->object();
191  }
192 
193 
194  return 0;
195 }
196 
197 }}
198 
199 #endif
tools::sg::remove_holders
void remove_holders(std::vector< node * > &a_vec)
Definition: holder:136
node
tools::sg::holder::holder
holder(const holder &a_from)
Definition: holder:79
tools::sg::base_holder
Definition: holder:18
tools::sg::holder::m_obj
T * m_obj
Definition: holder:100
tools::sg::holder::m_own
bool m_own
Definition: holder:101
tools::sg::holder
Definition: holder:37
tools::sg::holder::node_desc_fields
virtual const desc_fields & node_desc_fields() const
Definition: holder:55
tools::sg::node
Definition: node:28
TOOLS_NODE
#define TOOLS_NODE(a__class, a__sclass, a__parent)
Definition: node:324
tools::sg::base_holder::~base_holder
virtual ~base_holder()
Definition: holder:22
tools::sg::holder::copy
virtual node * copy() const
Definition: holder:50
tools::sg::base_holder::operator=
base_holder & operator=(const base_holder &a_from)
Definition: holder:25
tools::sg::holder::s_cls
virtual const std::string & s_cls() const
Definition: holder:51
sf_string
tools::sg::find_holder
T * find_holder(std::vector< node * > &a_vec, const std::string &a_name)
Definition: holder:170
tools::sg::desc_fields
Definition: field_desc:148
tools::sg::holder::object
T * object()
Definition: holder:98
tools::sg::holder::operator=
holder & operator=(const holder &a_from)
Definition: holder:88
tools::sg::base_holder::name
const std::string & name() const
Definition: holder:31
tools::sg::base_holder::base_holder
base_holder(const base_holder &a_from)
Definition: holder:24
tools::sg::base_holder::m_name
std::string m_name
Definition: holder:33
tools::sg::node::cast
virtual void * cast(const std::string &a_class) const
Definition: node:38
tools::sg::holder::cast
virtual void * cast(const std::string &a_class) const
Definition: holder:46
tools::sg::cast_holder_object
const T * cast_holder_object(const node &a_node)
Definition: holder:121
tools::sg::holder::holder
holder(T *a_obj=0, const std::string &a_name="", bool a_own=true)
Definition: holder:67
tools::sg::find_first_holder
T * find_first_holder(std::vector< node * > &a_vec)
Definition: holder:184
tools::sg::cast_holder
holder< T > * cast_holder(node &a_node)
Definition: holder:105
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::sg::node::node_desc_fields
virtual const desc_fields & node_desc_fields() const
Definition: node:48
tools::sg::node::add_field
void add_field(field *a_field)
Definition: node:128
tools::cmp_cast
void * cmp_cast(const TO *a_this, const std::string &a_class)
Definition: scast:15
TOOLS_FIELD_DESC_NODE_CLASS
#define TOOLS_FIELD_DESC_NODE_CLASS(a__class)
Definition: field:68
tools::sg::sf_string
Definition: sf_string:15
TOOLS_T_SCLASS
#define TOOLS_T_SCLASS(a_T, a_name)
Definition: S_STRING:48
tools::sg::holder::object
const T * object() const
Definition: holder:97
tools::sg::bsf::value
T & value()
Definition: bsf:98
tools::sg::holder::class_name
sf_string class_name
Definition: holder:53
tools::sg::holder::~holder
virtual ~holder()
Definition: holder:76
tools::sg::base_holder::base_holder
base_holder(const std::string &a_name)
Definition: holder:21
TOOLS_ARG_FIELD_DESC
#define TOOLS_ARG_FIELD_DESC(a__field)
Definition: field:71