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_sg_mf 4 #ifndef tools_sg_mf 5 #define tools_sg_mf 5 #define tools_sg_mf 6 6 7 // mf for multiple field. 7 // mf for multiple field. 8 8 9 #include "bmf" 9 #include "bmf" 10 10 11 #include "../stype" 11 #include "../stype" 12 #include "../cstr" 12 #include "../cstr" 13 #include "../io/iwbuf" 13 #include "../io/iwbuf" 14 #include "../io/irbuf" 14 #include "../io/irbuf" 15 #include "../HEADER" 15 #include "../HEADER" 16 16 17 namespace tools { 17 namespace tools { 18 namespace sg { 18 namespace sg { 19 19 20 template <class T> 20 template <class T> 21 class mf : public bmf<T> { 21 class mf : public bmf<T> { 22 typedef bmf<T> parent; 22 typedef bmf<T> parent; 23 public: 23 public: 24 static const std::string& s_class() { 24 static const std::string& s_class() { 25 static const std::string s_v("tools::sg::m 25 static const std::string s_v("tools::sg::mf<"+stype(T())+">"); 26 return s_v; 26 return s_v; 27 } 27 } 28 virtual void* cast(const std::string& a_clas 28 virtual void* cast(const std::string& a_class) const { 29 if(void* p = cmp_cast< mf<T> >(this,a_clas 29 if(void* p = cmp_cast< mf<T> >(this,a_class)) {return p;} 30 return parent::cast(a_class); 30 return parent::cast(a_class); 31 } 31 } 32 virtual const std::string& s_cls() const {re 32 virtual const std::string& s_cls() const {return s_class();} 33 public: 33 public: 34 virtual bool write(io::iwbuf& a_buffer) { 34 virtual bool write(io::iwbuf& a_buffer) { 35 const std::vector<T>& vec = parent::m_valu 35 const std::vector<T>& vec = parent::m_values; 36 return a_buffer.write_vec((uint32)vec.size 36 return a_buffer.write_vec((uint32)vec.size(),vec_data(vec)); 37 } 37 } 38 virtual bool read(io::irbuf& a_buffer) { 38 virtual bool read(io::irbuf& a_buffer) { 39 std::vector<T>& vec = parent::m_values; 39 std::vector<T>& vec = parent::m_values; 40 return a_buffer.read_std_vec(vec); 40 return a_buffer.read_std_vec(vec); 41 } 41 } 42 virtual bool dump(std::ostream& a_out) { 42 virtual bool dump(std::ostream& a_out) { 43 const std::vector<T>& vec = parent::m_valu 43 const std::vector<T>& vec = parent::m_values; 44 a_out << "size : " << vec.size() << std::e 44 a_out << "size : " << vec.size() << std::endl; 45 typedef typename std::vector<T>::const_ite 45 typedef typename std::vector<T>::const_iterator cit_t; 46 for(cit_t it=vec.begin();it!=vec.end();++i 46 for(cit_t it=vec.begin();it!=vec.end();++it) { 47 a_out << " " << (*it) << std::endl; 47 a_out << " " << (*it) << std::endl; 48 } 48 } 49 return true; 49 return true; 50 } 50 } 51 virtual bool s_value(std::string& a_s) const 51 virtual bool s_value(std::string& a_s) const {a_s.clear();return false;} 52 virtual bool s2value(const std::string&) {re 52 virtual bool s2value(const std::string&) {return false;} 53 public: 53 public: 54 mf(){} 54 mf(){} 55 mf(const T& a_v):parent(a_v){} 55 mf(const T& a_v):parent(a_v){} 56 mf(const std::vector<T>& a_v):parent(a_v){} 56 mf(const std::vector<T>& a_v):parent(a_v){} 57 virtual ~mf(){} 57 virtual ~mf(){} 58 public: 58 public: 59 mf(const mf& a_from):parent(a_from){} 59 mf(const mf& a_from):parent(a_from){} 60 mf& operator=(const mf& a_from){ 60 mf& operator=(const mf& a_from){ 61 //typedef typename parent::iterator bmf_t; 61 //typedef typename parent::iterator bmf_t; 62 parent::operator=(a_from); 62 parent::operator=(a_from); 63 return *this; 63 return *this; 64 } 64 } 65 public: 65 public: 66 mf& operator=(const std::vector<T>& a_from){ 66 mf& operator=(const std::vector<T>& a_from){ 67 parent::operator=(a_from); 67 parent::operator=(a_from); 68 return *this; 68 return *this; 69 } 69 } 70 mf& operator=(const T& a_v){ 70 mf& operator=(const T& a_v){ 71 parent::operator=(a_v); 71 parent::operator=(a_v); 72 return *this; 72 return *this; 73 } 73 } 74 }; 74 }; 75 75 76 class mf_string : public bmf<std::string> { 76 class mf_string : public bmf<std::string> { 77 TOOLS_HEADER(mf_string,tools::sg::mf_string, 77 TOOLS_HEADER(mf_string,tools::sg::mf_string,bmf<std::string>) 78 public: 78 public: 79 virtual bool write(io::iwbuf& a_buffer) { 79 virtual bool write(io::iwbuf& a_buffer) { 80 return a_buffer.write_vec(m_values); 80 return a_buffer.write_vec(m_values); 81 } 81 } 82 virtual bool read(io::irbuf& a_buffer) { 82 virtual bool read(io::irbuf& a_buffer) { 83 std::vector<std::string>& vec = parent::m_ 83 std::vector<std::string>& vec = parent::m_values; 84 return a_buffer.read_vec(vec); 84 return a_buffer.read_vec(vec); 85 } 85 } 86 virtual bool dump(std::ostream& a_out) { 86 virtual bool dump(std::ostream& a_out) { 87 const std::vector<std::string>& vec = pare 87 const std::vector<std::string>& vec = parent::m_values; 88 a_out << "size : " << vec.size() << std::e 88 a_out << "size : " << vec.size() << std::endl; 89 std::vector<std::string>::const_iterator i 89 std::vector<std::string>::const_iterator it; 90 for(it=vec.begin();it!=vec.end();++it) { 90 for(it=vec.begin();it!=vec.end();++it) { 91 a_out << " \"" << (*it) << "\"" << std: 91 a_out << " \"" << (*it) << "\"" << std::endl; 92 } 92 } 93 return true; 93 return true; 94 } 94 } 95 virtual bool s_value(std::string& a_s) const 95 virtual bool s_value(std::string& a_s) const {a_s.clear();return false;} 96 virtual bool s2value(const std::string&) {re 96 virtual bool s2value(const std::string&) {return false;} 97 public: 97 public: 98 mf_string():parent(){} 98 mf_string():parent(){} 99 mf_string(const std::string& a_v):parent(a_v 99 mf_string(const std::string& a_v):parent(a_v){} 100 mf_string(const std::vector<std::string>& a_ 100 mf_string(const std::vector<std::string>& a_v):parent(a_v){} 101 virtual ~mf_string(){} 101 virtual ~mf_string(){} 102 public: 102 public: 103 mf_string(const mf_string& a_from):parent(a_ 103 mf_string(const mf_string& a_from):parent(a_from){} 104 mf_string& operator=(const mf_string& a_from 104 mf_string& operator=(const mf_string& a_from){ 105 parent::operator=(a_from); 105 parent::operator=(a_from); 106 return *this; 106 return *this; 107 } 107 } 108 public: 108 public: 109 mf_string& operator=(const std::vector<std:: 109 mf_string& operator=(const std::vector<std::string>& a_value){ 110 parent::operator=(a_value); 110 parent::operator=(a_value); 111 return *this; 111 return *this; 112 } 112 } 113 mf_string& operator=(const char* a_cstr){ 113 mf_string& operator=(const char* a_cstr){ 114 parent::operator=(a_cstr); 114 parent::operator=(a_cstr); 115 return *this; 115 return *this; 116 } 116 } 117 }; 117 }; 118 118 119 //exa tools::sg::entries.mf_vec<entry_type> 119 //exa tools::sg::entries.mf_vec<entry_type> 120 120 121 template <class T> 121 template <class T> 122 class mf_enum : public bmf<T> { 122 class mf_enum : public bmf<T> { 123 typedef bmf<T> parent; 123 typedef bmf<T> parent; 124 public: 124 public: 125 static const std::string& s_class() { 125 static const std::string& s_class() { 126 static const std::string s_v("tools::sg::m 126 static const std::string s_v("tools::sg::mf_enum"); 127 return s_v; 127 return s_v; 128 } 128 } 129 virtual void* cast(const std::string& a_clas 129 virtual void* cast(const std::string& a_class) const { 130 if(void* p = cmp_cast< mf_enum<T> >(this,a 130 if(void* p = cmp_cast< mf_enum<T> >(this,a_class)) {return p;} 131 return parent::cast(a_class); 131 return parent::cast(a_class); 132 } 132 } 133 virtual const std::string& s_cls() const {re 133 virtual const std::string& s_cls() const {return s_class();} 134 public: 134 public: 135 virtual bool write(io::iwbuf& a_buffer) { 135 virtual bool write(io::iwbuf& a_buffer) { 136 const std::vector<T>& vec = parent::m_valu 136 const std::vector<T>& vec = parent::m_values; 137 std::vector<int16> v; //an enum can be neg 137 std::vector<int16> v; //an enum can be negative. 138 typedef typename std::vector<T>::const_ite 138 typedef typename std::vector<T>::const_iterator cit_t; 139 for(cit_t it=vec.begin();it!=vec.end();++i 139 for(cit_t it=vec.begin();it!=vec.end();++it) v.push_back(*it); 140 return a_buffer.write_vec((uint32)v.size() 140 return a_buffer.write_vec((uint32)v.size(),vec_data(v)); 141 } 141 } 142 virtual bool read(io::irbuf& a_buffer) { 142 virtual bool read(io::irbuf& a_buffer) { 143 std::vector<int16> v; //an enum can be neg 143 std::vector<int16> v; //an enum can be negative. 144 if(!a_buffer.read_std_vec(v)) return false 144 if(!a_buffer.read_std_vec(v)) return false; 145 std::vector<T>& vec = parent::m_values; 145 std::vector<T>& vec = parent::m_values; 146 vec.clear(); 146 vec.clear(); 147 std::vector<int16>::const_iterator it; 147 std::vector<int16>::const_iterator it; 148 for(it=v.begin();it!=v.end();++it) vec.pus 148 for(it=v.begin();it!=v.end();++it) vec.push_back((T)(*it)); 149 return true; 149 return true; 150 } 150 } 151 virtual bool dump(std::ostream& a_out) { 151 virtual bool dump(std::ostream& a_out) { 152 const std::vector<T>& vec = parent::m_valu 152 const std::vector<T>& vec = parent::m_values; 153 a_out << "size : " << vec.size() << std::e 153 a_out << "size : " << vec.size() << std::endl; 154 typedef typename std::vector<T>::const_ite 154 typedef typename std::vector<T>::const_iterator cit_t; 155 for(cit_t it=vec.begin();it!=vec.end();++i 155 for(cit_t it=vec.begin();it!=vec.end();++it) { 156 a_out << " " << (*it) << std::endl; 156 a_out << " " << (*it) << std::endl; 157 } 157 } 158 return true; 158 return true; 159 } 159 } 160 virtual bool s_value(std::string& a_s) const 160 virtual bool s_value(std::string& a_s) const {a_s.clear();return false;} 161 virtual bool s2value(const std::string&) {re 161 virtual bool s2value(const std::string&) {return false;} 162 public: 162 public: 163 mf_enum():parent(){} 163 mf_enum():parent(){} 164 mf_enum(const T& a_v):parent(a_v){} 164 mf_enum(const T& a_v):parent(a_v){} 165 mf_enum(const std::vector<T>& a_v):parent(a_ 165 mf_enum(const std::vector<T>& a_v):parent(a_v){} 166 virtual ~mf_enum(){} 166 virtual ~mf_enum(){} 167 public: 167 public: 168 mf_enum(const mf_enum& a_from):parent(a_from 168 mf_enum(const mf_enum& a_from):parent(a_from){} 169 mf_enum& operator=(const mf_enum& a_from){ 169 mf_enum& operator=(const mf_enum& a_from){ 170 parent::operator=(a_from); 170 parent::operator=(a_from); 171 return *this; 171 return *this; 172 } 172 } 173 }; 173 }; 174 174 175 //exa mf_vec<colorf,float> 175 //exa mf_vec<colorf,float> 176 176 177 ////////////////////////////////////////////// 177 /////////////////////////////////////////////////////////// 178 //the three below funcs are for : 178 //the three below funcs are for : 179 // mf_vec< std::vector<std::string> ,std::str 179 // mf_vec< std::vector<std::string> ,std::string> opts; 180 ////////////////////////////////////////////// 180 /////////////////////////////////////////////////////////// 181 inline std::ostream& operator<<(std::ostream& 181 inline std::ostream& operator<<(std::ostream& a_out,const std::vector<std::string>&) { 182 //for mf_vec::dump. 182 //for mf_vec::dump. 183 return a_out; 183 return a_out; 184 } 184 } 185 185 186 inline bool set_from_vec(std::vector<std::stri 186 inline bool set_from_vec(std::vector<std::string>&,const std::vector<std::string>&) { 187 //for mf_vec::read(io::irbuf&) 187 //for mf_vec::read(io::irbuf&) 188 return false; 188 return false; 189 } 189 } 190 190 191 inline const std::string* get_data(const std:: 191 inline const std::string* get_data(const std::vector<std::string>& a_v) { 192 return vec_data(a_v); 192 return vec_data(a_v); 193 } 193 } 194 ////////////////////////////////////////////// 194 /////////////////////////////////////////////////////////// 195 ////////////////////////////////////////////// 195 /////////////////////////////////////////////////////////// 196 ////////////////////////////////////////////// 196 /////////////////////////////////////////////////////////// 197 197 198 198 199 template <class T,class TT> 199 template <class T,class TT> 200 class mf_vec : public bmf<T> { 200 class mf_vec : public bmf<T> { 201 typedef bmf<T> parent; 201 typedef bmf<T> parent; 202 public: 202 public: 203 static const std::string& s_class() { 203 static const std::string& s_class() { 204 static const std::string s_v("tools::sg::m 204 static const std::string s_v("tools::sg::mf_vec<"+stype(T())+","+stype(TT())+">"); 205 return s_v; 205 return s_v; 206 } 206 } 207 virtual void* cast(const std::string& a_clas 207 virtual void* cast(const std::string& a_class) const { 208 if(void* p = cmp_cast< mf_vec<T,TT> >(this 208 if(void* p = cmp_cast< mf_vec<T,TT> >(this,a_class)) {return p;} 209 return parent::cast(a_class); 209 return parent::cast(a_class); 210 } 210 } 211 virtual const std::string& s_cls() const {re 211 virtual const std::string& s_cls() const {return s_class();} 212 public: 212 public: 213 virtual bool write(io::iwbuf& a_buffer) { 213 virtual bool write(io::iwbuf& a_buffer) { 214 const std::vector<T>& vec = parent::m_valu 214 const std::vector<T>& vec = parent::m_values; 215 typedef typename std::vector<TT> vec_t; 215 typedef typename std::vector<TT> vec_t; 216 std::vector<vec_t> vec_vec; 216 std::vector<vec_t> vec_vec; 217 typedef typename std::vector<T>::const_ite 217 typedef typename std::vector<T>::const_iterator cit_t; 218 for(cit_t it=vec.begin();it!=vec.end();++i 218 for(cit_t it=vec.begin();it!=vec.end();++it) { 219 219 220 const T& v = (*it); 220 const T& v = (*it); 221 size_t num = v.size(); 221 size_t num = v.size(); 222 const TT* d = get_data(v); 222 const TT* d = get_data(v); 223 223 224 std::vector<TT> std_vec(num); 224 std::vector<TT> std_vec(num); 225 for(size_t i=0;i<num;i++) std_vec[i] = d 225 for(size_t i=0;i<num;i++) std_vec[i] = d[i]; 226 226 227 vec_vec.push_back(std_vec); 227 vec_vec.push_back(std_vec); 228 } 228 } 229 return a_buffer.write_std_vec_vec(vec_vec) 229 return a_buffer.write_std_vec_vec(vec_vec); 230 } 230 } 231 virtual bool read(io::irbuf& a_buffer) { 231 virtual bool read(io::irbuf& a_buffer) { 232 std::vector<T>& vec = parent::m_values; 232 std::vector<T>& vec = parent::m_values; 233 vec.clear(); 233 vec.clear(); 234 typedef typename std::vector<TT> vec_t; 234 typedef typename std::vector<TT> vec_t; 235 std::vector<vec_t> vec_vec; 235 std::vector<vec_t> vec_vec; 236 if(!a_buffer.read_std_vec_vec(vec_vec)) re 236 if(!a_buffer.read_std_vec_vec(vec_vec)) return false; 237 typedef typename std::vector<vec_t>::itera 237 typedef typename std::vector<vec_t>::iterator _it_t; 238 for(_it_t it=vec_vec.begin();it!=vec_vec.e 238 for(_it_t it=vec_vec.begin();it!=vec_vec.end();++it) { 239 T x; 239 T x; 240 // x colorf, *it = std::vector<float> 240 // x colorf, *it = std::vector<float> 241 // x vecs, *it = std::vector<std::string 241 // x vecs, *it = std::vector<std::string> 242 if(!set_from_vec(x,*it)) {vec.clear();re 242 if(!set_from_vec(x,*it)) {vec.clear();return false;} 243 vec.push_back(x); 243 vec.push_back(x); 244 } 244 } 245 return true; 245 return true; 246 } 246 } 247 virtual bool dump(std::ostream& a_out) { 247 virtual bool dump(std::ostream& a_out) { 248 const std::vector<T>& vec = parent::m_valu 248 const std::vector<T>& vec = parent::m_values; 249 a_out << "size : " << vec.size() << std::e 249 a_out << "size : " << vec.size() << std::endl; 250 typedef typename std::vector<T>::const_ite 250 typedef typename std::vector<T>::const_iterator cit_t; 251 for(cit_t it=vec.begin();it!=vec.end();++i 251 for(cit_t it=vec.begin();it!=vec.end();++it) { 252 a_out << " " << (*it) << std::endl; 252 a_out << " " << (*it) << std::endl; 253 } 253 } 254 return true; 254 return true; 255 } 255 } 256 virtual bool s_value(std::string& a_s) const 256 virtual bool s_value(std::string& a_s) const {a_s.clear();return false;} 257 virtual bool s2value(const std::string&) {re 257 virtual bool s2value(const std::string&) {return false;} 258 public: 258 public: 259 mf_vec():parent(){} 259 mf_vec():parent(){} 260 mf_vec(const T& a_v):parent(a_v){} 260 mf_vec(const T& a_v):parent(a_v){} 261 mf_vec(const std::vector<T>& a_v):parent(a_v 261 mf_vec(const std::vector<T>& a_v):parent(a_v){} 262 virtual ~mf_vec(){} 262 virtual ~mf_vec(){} 263 public: 263 public: 264 mf_vec(const mf_vec& a_from):parent(a_from){ 264 mf_vec(const mf_vec& a_from):parent(a_from){} 265 mf_vec& operator=(const mf_vec& a_from){ 265 mf_vec& operator=(const mf_vec& a_from){ 266 parent::operator=(a_from); 266 parent::operator=(a_from); 267 return *this; 267 return *this; 268 } 268 } 269 }; 269 }; 270 270 271 template <class T> 271 template <class T> 272 class mf_std_vec : public bmf< std::vector<T> 272 class mf_std_vec : public bmf< std::vector<T> > { 273 typedef bmf< std::vector<T> > parent; 273 typedef bmf< std::vector<T> > parent; 274 public: 274 public: 275 static const std::string& s_class() { 275 static const std::string& s_class() { 276 static const std::string s_v("tools::sg::m 276 static const std::string s_v("tools::sg::mf_std_vec<"+stype(T())+">"); 277 return s_v; 277 return s_v; 278 } 278 } 279 virtual void* cast(const std::string& a_clas 279 virtual void* cast(const std::string& a_class) const { 280 if(void* p = cmp_cast< mf_std_vec<T> >(thi 280 if(void* p = cmp_cast< mf_std_vec<T> >(this,a_class)) {return p;} 281 return parent::cast(a_class); 281 return parent::cast(a_class); 282 } 282 } 283 virtual const std::string& s_cls() const {re 283 virtual const std::string& s_cls() const {return s_class();} 284 public: 284 public: 285 virtual bool write(io::iwbuf& a_buffer) { 285 virtual bool write(io::iwbuf& a_buffer) { 286 //used in exlib/sg/text_freetype::unitext 286 //used in exlib/sg/text_freetype::unitext 287 const std::vector< std::vector<T> >& vec = 287 const std::vector< std::vector<T> >& vec = parent::m_values; 288 return a_buffer.write_std_vec_vec(vec); 288 return a_buffer.write_std_vec_vec(vec); 289 } 289 } 290 virtual bool read(io::irbuf& a_buffer) { 290 virtual bool read(io::irbuf& a_buffer) { 291 std::vector< std::vector<T> >& vec = paren 291 std::vector< std::vector<T> >& vec = parent::m_values; 292 return a_buffer.read_std_vec_vec(vec); 292 return a_buffer.read_std_vec_vec(vec); 293 } 293 } 294 virtual bool dump(std::ostream& a_out) { 294 virtual bool dump(std::ostream& a_out) { 295 const std::vector< std::vector<T> >& vec = 295 const std::vector< std::vector<T> >& vec = parent::m_values; 296 a_out << "size : " << vec.size() << std::e 296 a_out << "size : " << vec.size() << std::endl; 297 typedef typename std::vector< std::vector< 297 typedef typename std::vector< std::vector<T> >::const_iterator cit_t; 298 for(cit_t it=vec.begin();it!=vec.end();++i 298 for(cit_t it=vec.begin();it!=vec.end();++it) { 299 //a_out << " " << (*it) << std::endl; 299 //a_out << " " << (*it) << std::endl; 300 } 300 } 301 return true; 301 return true; 302 } 302 } 303 virtual bool s_value(std::string& a_s) const 303 virtual bool s_value(std::string& a_s) const {a_s.clear();return false;} 304 virtual bool s2value(const std::string&) {re 304 virtual bool s2value(const std::string&) {return false;} 305 public: 305 public: 306 mf_std_vec():parent(){} 306 mf_std_vec():parent(){} 307 mf_std_vec(const T& a_v):parent(a_v){} 307 mf_std_vec(const T& a_v):parent(a_v){} 308 mf_std_vec(const std::vector<T>& a_v):parent 308 mf_std_vec(const std::vector<T>& a_v):parent(a_v){} 309 virtual ~mf_std_vec(){} 309 virtual ~mf_std_vec(){} 310 public: 310 public: 311 mf_std_vec(const mf_std_vec& a_from):parent( 311 mf_std_vec(const mf_std_vec& a_from):parent(a_from){} 312 mf_std_vec& operator=(const mf_std_vec& a_fr 312 mf_std_vec& operator=(const mf_std_vec& a_from){ 313 parent::operator=(a_from); 313 parent::operator=(a_from); 314 return *this; 314 return *this; 315 } 315 } 316 }; 316 }; 317 317 318 }} 318 }} 319 319 320 #endif 320 #endif