Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef tools_sg_field_desc 5 #define tools_sg_field_desc 6 7 #include <string> 8 #include <cstddef> //ptrdiff_t 9 10 #include <vector> 11 12 // fied_desc are used in static and then we do not master their destruction. 13 //#ifdef TOOLS_MEM 14 //#include "../mem" 15 //#include "../S_STRING" 16 //#endif 17 18 namespace tools { 19 namespace sg { 20 21 class field_desc { 22 //typedef int offset_t; //could be <0 ? 23 //#ifdef TOOLS_MEM 24 // TOOLS_SCLASS(tools::sg::field_desc) 25 //#endif 26 public: 27 typedef ptrdiff_t offset_t; 28 public: 29 field_desc():m_offset(0){ //touchy 30 //#ifdef TOOLS_MEM 31 // mem::increment(s_class().c_str()); 32 //#endif 33 } 34 field_desc(const std::string& a_name, 35 const std::string& a_class, 36 offset_t a_offset, 37 bool a_editable) 38 :m_name(a_name) 39 ,m_class(a_class) 40 ,m_offset(a_offset) 41 ,m_editable(a_editable) 42 { 43 //#ifdef TOOLS_MEM 44 // mem::increment(s_class().c_str()); 45 //#endif 46 } 47 virtual ~field_desc(){ 48 //#ifdef TOOLS_MEM 49 // mem::decrement(s_class().c_str()); 50 //#endif 51 } 52 public: 53 field_desc(const field_desc& a_from) 54 :m_name(a_from.m_name) 55 ,m_class(a_from.m_class) 56 ,m_offset(a_from.m_offset) 57 ,m_editable(a_from.m_editable) 58 ,m_enums(a_from.m_enums) 59 ,m_opts(a_from.m_opts) 60 {} 61 field_desc& operator=(const field_desc& a_from){ 62 m_name = a_from.m_name; 63 m_class = a_from.m_class; 64 m_offset = a_from.m_offset; 65 m_editable = a_from.m_editable; 66 m_enums = a_from.m_enums; 67 m_opts = a_from.m_opts; 68 return *this; 69 } 70 public: 71 const std::string& name() const {return m_name;} 72 const std::string& cls() const {return m_class;} 73 offset_t offset() const {return m_offset;} 74 75 void add_enum(const std::string& a_key,int a_value) {m_enums.push_back(enum_t(a_key,a_value));} 76 typedef std::pair<std::string,int> enum_t; 77 const std::vector<enum_t>& enums() const {return m_enums;} 78 79 void add_opt(const std::string& a_value) {m_opts.push_back(a_value);} 80 const std::vector<std::string>& opts() const {return m_opts;} 81 82 bool editable() const {return m_editable;} 83 protected: 84 std::string m_name; 85 std::string m_class; 86 offset_t m_offset; 87 bool m_editable; 88 std::vector<enum_t> m_enums; 89 std::vector<std::string> m_opts; 90 }; 91 92 }} 93 94 #include <cstdarg> 95 96 namespace tools { 97 namespace sg { 98 99 class field_desc_enums : public field_desc { 100 typedef field_desc parent; 101 public: 102 field_desc_enums(const std::string& a_name,const std::string& a_class,offset_t a_offset,bool a_editable,size_t a_num,...) 103 :parent(a_name,a_class,a_offset,a_editable) 104 { 105 va_list args; 106 va_start(args,a_num); 107 for(size_t index=0;index<a_num;index++) { 108 char* _key = va_arg(args,char*); 109 int _value = va_arg(args,int); 110 m_enums.push_back(enum_t(_key,_value)); 111 } 112 va_end(args); 113 } 114 virtual ~field_desc_enums() {} 115 public: 116 field_desc_enums(const field_desc_enums& a_from):parent(a_from) {} 117 field_desc_enums& operator=(const field_desc_enums& a_from){parent::operator=(a_from);return *this;} 118 }; 119 120 class field_desc_opts : public field_desc { 121 typedef field_desc parent; 122 public: 123 field_desc_opts(const std::string& a_name,const std::string& a_class,offset_t a_offset,bool a_editable,size_t a_num,...) 124 :parent(a_name,a_class,a_offset,a_editable) 125 { 126 va_list args; 127 va_start(args,a_num); 128 for(size_t index=0;index<a_num;index++) { 129 char* _value = va_arg(args,char*); 130 m_opts.push_back(_value); 131 } 132 va_end(args); 133 } 134 virtual ~field_desc_opts() {} 135 public: 136 field_desc_opts(const field_desc_opts& a_from):parent(a_from) {} 137 field_desc_opts& operator=(const field_desc_opts& a_from){parent::operator=(a_from);return *this;} 138 }; 139 140 }} 141 142 #include <ostream> 143 #include "../forit" 144 145 namespace tools { 146 namespace sg { 147 148 class desc_fields : public std::vector<field_desc> { 149 typedef std::vector<field_desc> parent; 150 public: 151 desc_fields(){} 152 desc_fields(const desc_fields& a_parent,size_t a_num,...){ 153 parent::operator=(a_parent); 154 va_list args; 155 va_start(args,a_num); 156 for(size_t index=0;index<a_num;index++) { 157 field_desc* _fd = va_arg(args,field_desc*); //we get ownership. 158 parent::push_back(*_fd); 159 delete _fd; 160 } 161 va_end(args); 162 } 163 virtual ~desc_fields() {} 164 public: 165 desc_fields(const desc_fields& a_from):parent(a_from) {} 166 desc_fields& operator=(const desc_fields& a_from){parent::operator=(a_from);return *this;} 167 public: 168 void dump(std::ostream& a_out) const { 169 a_out << "num fields " << parent::size() << " :" << std::endl; 170 tools_vforcit(field_desc,*this,it) { 171 const field_desc& _fd = *it; 172 a_out << "name " << _fd.name() << std::endl; 173 a_out << "class " << _fd.cls() << std::endl; 174 a_out << "offset " << _fd.offset() << std::endl; 175 a_out << "editable " << (_fd.editable()?"yes":"no") << std::endl; 176 {const std::vector<field_desc::enum_t>& _enums = _fd.enums(); 177 if(_enums.size()) { 178 a_out << "num enums " << _enums.size() << " :" << std::endl; 179 tools_vforcit(field_desc::enum_t,_enums,eit) { 180 a_out << "key " << (*eit).first << ", value " << (*eit).second << std::endl; 181 } 182 }} 183 {const std::vector<std::string>& _opts = _fd.opts(); 184 if(_opts.size()) { 185 a_out << "num options " << _opts.size() << " :" << std::endl; 186 tools_vforcit(std::string,_opts,oit) { 187 a_out << " " << (*oit) << std::endl; 188 } 189 }} 190 } 191 } 192 }; 193 194 }} 195 196 #endif