g4tools  5.4.0
element
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_xml_element
5 #define tools_xml_element
6 
7 #include "../srep"
8 #include "../sto"
9 #include "../scast"
10 
11 #ifdef TOOLS_MEM
12 #include "../mem"
13 #include "../S_STRING"
14 #endif
15 
16 namespace tools {
17 namespace xml {
18 
19 class ielem {
20 public:
21  virtual ~ielem(){}
22 public:
23  virtual void* cast(cid) const = 0;
24 };
25 
26 class element : public virtual ielem {
27 #ifdef TOOLS_MEM
29 #endif
30 public:
31  static cid id_class() {return 0;}
32  virtual void* cast(cid a_class) const {
33  if(void* p = cmp_cast<element>(this,a_class)) {return p;}
34  else return 0;
35  }
36 public:
37  typedef std::pair<std::string,std::string> atb;
38 public:
39  element(const std::string& a_name,
40  const std::vector<atb>& a_atbs,
41  const std::string& a_value){
42 #ifdef TOOLS_MEM
43  mem::increment(s_class().c_str());
44 #endif
45  m_name = a_name;
46  m_atbs = a_atbs;
47  m_value = a_value;
48  }
49  virtual ~element(){
50 #ifdef TOOLS_MEM
51  mem::decrement(s_class().c_str());
52 #endif
53  }
54 public:
55  element(const element& a_from)
56  :ielem(a_from) {
57 #ifdef TOOLS_MEM
58  mem::increment(s_class().c_str());
59 #endif
60  m_name = a_from.m_name;
61  m_atbs = a_from.m_atbs;
62  m_value = a_from.m_value;
63  }
64  element& operator=(const element& a_from) {
65  m_name = a_from.m_name;
66  m_atbs = a_from.m_atbs;
67  m_value = a_from.m_value;
68  return *this;
69  }
70 public:
71  const std::string& name() const {return m_name;}
72  const std::vector<atb>& attributes() const {return m_atbs;}
73  const std::string& value() const {return m_value;}
74 
75  void set_value(const std::string& a_value) {m_value = a_value;}
76  bool is_attribute(const std::string& a_name) const {
78  if((*it).first==a_name) return true;
79  }
80  return false;
81  }
82  void add_attribute(const std::string& a_name,const std::string& a_value){
83  // No check is done about an existing a_name.
84  m_atbs.push_back(atb(a_name,a_value));
85  }
86 
87  bool attribute_value(const std::string& a_atb,std::string& a_value) const {
89  if((*it).first==a_atb) {
90  a_value = (*it).second;
91  return true;
92  }
93  }
94  a_value.clear();
95  return false;
96  }
97 
98  template <class T>
99  bool attribute_value(const std::string& a_atb,T& a_value) const {
100  std::string sv;
101  if(!attribute_value(a_atb,sv)) {a_value=T();return false;}
102  return to<T>(sv,a_value);
103  }
104 
105 
106  bool set_attribute_value(const std::string& a_atb,const std::string& a_value) {
107  tools_vforit(atb,m_atbs,it) {
108  if((*it).first==a_atb) {
109  (*it).second = a_value;
110  return true;
111  }
112  }
113  // Not found, add one :
114  m_atbs.push_back(atb(a_atb,a_value));
115  return true;
116  }
117 
118  void remove_attributes(const std::string& a_atb) {
119  std::vector<atb>::iterator it;
120  for(it=m_atbs.begin();it!=m_atbs.end();) {
121  if((*it).first==a_atb) {
122  it = m_atbs.erase(it);
123  } else {
124  ++it;
125  }
126  }
127  }
128 
129  void replace(const std::string& a_old,const std::string& a_new) {
130  // Used by the obuild template system.
131  tools_vforit(atb,m_atbs,it) {
132  replace_((*it).second,a_old,a_new);
133  }
134  replace_(m_value,a_old,a_new);
135  }
136 
137 protected:
138  std::string m_name;
139  std::vector<atb> m_atbs;
140  std::string m_value;
141 };
142 
143 }}
144 
145 #endif
tools::xml::element
Definition: element:26
tools::xml::element::remove_attributes
void remove_attributes(const std::string &a_atb)
Definition: element:118
tools::xml::ielem::~ielem
virtual ~ielem()
Definition: element:21
tools::xml::ielem
Definition: element:19
tools::xml::element::m_atbs
std::vector< atb > m_atbs
Definition: element:139
tools::xml::element::attributes
const std::vector< atb > & attributes() const
Definition: element:72
tools::xml::element::add_attribute
void add_attribute(const std::string &a_name, const std::string &a_value)
Definition: element:82
tools::xml::element::element
element(const std::string &a_name, const std::vector< atb > &a_atbs, const std::string &a_value)
Definition: element:39
TOOLS_SCLASS
#define TOOLS_SCLASS(a_name)
Definition: S_STRING:41
tools::xml::element::value
const std::string & value() const
Definition: element:73
tools::xml::element::~element
virtual ~element()
Definition: element:49
tools::xml::element::element
element(const element &a_from)
Definition: element:55
tools::xml::element::replace
void replace(const std::string &a_old, const std::string &a_new)
Definition: element:129
tools::xml::element::name
const std::string & name() const
Definition: element:71
tools_vforit
#define tools_vforit(a__T, a__v, a__it)
Definition: forit:13
tools::xml::element::attribute_value
bool attribute_value(const std::string &a_atb, T &a_value) const
Definition: element:99
tools::xml::element::set_attribute_value
bool set_attribute_value(const std::string &a_atb, const std::string &a_value)
Definition: element:106
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::replace_
bool replace_(std::string &a_string, const std::string &a_old, const std::string &a_new)
Definition: srep:44
tools::xml::element::id_class
static cid id_class()
Definition: element:31
tools::xml::element::atb
std::pair< std::string, std::string > atb
Definition: element:37
tools::xml::ielem::cast
virtual void * cast(cid) const =0
tools_vforcit
#define tools_vforcit(a__T, a__v, a__it)
Definition: forit:7
tools::xml::element::is_attribute
bool is_attribute(const std::string &a_name) const
Definition: element:76
tools::xml::element::operator=
element & operator=(const element &a_from)
Definition: element:64
tools::xml::element::attribute_value
bool attribute_value(const std::string &a_atb, std::string &a_value) const
Definition: element:87
tools::xml::element::m_value
std::string m_value
Definition: element:140
tools::xml::element::m_name
std::string m_name
Definition: element:138
tools::xml::element::cast
virtual void * cast(cid a_class) const
Definition: element:32
tools::cid
unsigned short cid
Definition: cid:9
tools::xml::element::set_value
void set_value(const std::string &a_value)
Definition: element:75