Warning, /include/Geant4/tools/xml/element 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_xml_element
0005 #define tools_xml_element
0006
0007 #include "../srep"
0008 #include "../sto"
0009 #include "../scast"
0010
0011 #ifdef TOOLS_MEM
0012 #include "../mem"
0013 #include "../S_STRING"
0014 #endif
0015
0016 namespace tools {
0017 namespace xml {
0018
0019 class ielem {
0020 public:
0021 virtual ~ielem(){}
0022 public:
0023 virtual void* cast(cid) const = 0;
0024 };
0025
0026 class element : public virtual ielem {
0027 #ifdef TOOLS_MEM
0028 TOOLS_SCLASS(tools::xml::element)
0029 #endif
0030 public:
0031 static cid id_class() {return 0;}
0032 virtual void* cast(cid a_class) const {
0033 if(void* p = cmp_cast<element>(this,a_class)) {return p;}
0034 else return 0;
0035 }
0036 public:
0037 typedef std::pair<std::string,std::string> atb;
0038 public:
0039 element(const std::string& a_name,
0040 const std::vector<atb>& a_atbs,
0041 const std::string& a_value){
0042 #ifdef TOOLS_MEM
0043 mem::increment(s_class().c_str());
0044 #endif
0045 m_name = a_name;
0046 m_atbs = a_atbs;
0047 m_value = a_value;
0048 }
0049 virtual ~element(){
0050 #ifdef TOOLS_MEM
0051 mem::decrement(s_class().c_str());
0052 #endif
0053 }
0054 public:
0055 element(const element& a_from)
0056 :ielem(a_from) {
0057 #ifdef TOOLS_MEM
0058 mem::increment(s_class().c_str());
0059 #endif
0060 m_name = a_from.m_name;
0061 m_atbs = a_from.m_atbs;
0062 m_value = a_from.m_value;
0063 }
0064 element& operator=(const element& a_from) {
0065 m_name = a_from.m_name;
0066 m_atbs = a_from.m_atbs;
0067 m_value = a_from.m_value;
0068 return *this;
0069 }
0070 public:
0071 const std::string& name() const {return m_name;}
0072 const std::vector<atb>& attributes() const {return m_atbs;}
0073 const std::string& value() const {return m_value;}
0074
0075 void set_value(const std::string& a_value) {m_value = a_value;}
0076 bool is_attribute(const std::string& a_name) const {
0077 tools_vforcit(atb,m_atbs,it) {
0078 if((*it).first==a_name) return true;
0079 }
0080 return false;
0081 }
0082 void add_attribute(const std::string& a_name,const std::string& a_value){
0083 // No check is done about an existing a_name.
0084 m_atbs.push_back(atb(a_name,a_value));
0085 }
0086
0087 bool attribute_value(const std::string& a_atb,std::string& a_value) const {
0088 tools_vforcit(atb,m_atbs,it) {
0089 if((*it).first==a_atb) {
0090 a_value = (*it).second;
0091 return true;
0092 }
0093 }
0094 a_value.clear();
0095 return false;
0096 }
0097
0098 template <class T>
0099 bool attribute_value(const std::string& a_atb,T& a_value) const {
0100 std::string sv;
0101 if(!attribute_value(a_atb,sv)) {a_value=T();return false;}
0102 return to<T>(sv,a_value);
0103 }
0104
0105
0106 bool set_attribute_value(const std::string& a_atb,const std::string& a_value) {
0107 tools_vforit(atb,m_atbs,it) {
0108 if((*it).first==a_atb) {
0109 (*it).second = a_value;
0110 return true;
0111 }
0112 }
0113 // Not found, add one :
0114 m_atbs.push_back(atb(a_atb,a_value));
0115 return true;
0116 }
0117
0118 void remove_attributes(const std::string& a_atb) {
0119 std::vector<atb>::iterator it;
0120 for(it=m_atbs.begin();it!=m_atbs.end();) {
0121 if((*it).first==a_atb) {
0122 it = m_atbs.erase(it);
0123 } else {
0124 ++it;
0125 }
0126 }
0127 }
0128
0129 void replace(const std::string& a_old,const std::string& a_new) {
0130 // Used by the obuild template system.
0131 tools_vforit(atb,m_atbs,it) {
0132 replace_((*it).second,a_old,a_new);
0133 }
0134 replace_(m_value,a_old,a_new);
0135 }
0136
0137 protected:
0138 std::string m_name;
0139 std::vector<atb> m_atbs;
0140 std::string m_value;
0141 };
0142
0143 }}
0144
0145 #endif