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