Geant4 Cross Reference |
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 28 TOOLS_SCLASS(tools::xml::element) 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 { 77 tools_vforcit(atb,m_atbs,it) { 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 { 88 tools_vforcit(atb,m_atbs,it) { 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