Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 2 // See the file tools.license for terms. 3 4 #ifndef tools_rroot_leaf 5 #define tools_rroot_leaf 6 7 #include "base_leaf" 8 #include "../stype" 9 #include "../cids" 10 11 namespace tools { 12 namespace rroot { 13 14 inline const std::string& leaf_store_class(cha 15 static const std::string s_v("TLeafB"); 16 return s_v; 17 } 18 inline const std::string& leaf_store_class(sho 19 static const std::string s_v("TLeafS"); 20 return s_v; 21 } 22 inline const std::string& leaf_store_class(int 23 static const std::string s_v("TLeafI"); 24 return s_v; 25 } 26 inline const std::string& leaf_store_class(flo 27 static const std::string s_v("TLeafF"); 28 return s_v; 29 } 30 inline const std::string& leaf_store_class(dou 31 static const std::string s_v("TLeafD"); 32 return s_v; 33 } 34 inline const std::string& leaf_store_class(boo 35 static const std::string s_v("TLeafO"); 36 return s_v; 37 } 38 39 inline const std::string& leaf_float_cls() { 40 static const std::string s_v("tools::rroot:: 41 return s_v; 42 } 43 inline const std::string& leaf_double_cls() { 44 static const std::string s_v("tools::rroot:: 45 return s_v; 46 } 47 inline const std::string& leaf_int_cls() { 48 static const std::string s_v("tools::rroot:: 49 return s_v; 50 } 51 inline const std::string& leaf_bool_cls() { 52 static const std::string s_v("tools::rroot:: 53 return s_v; 54 } 55 56 template <class T> 57 class leaf : public base_leaf { 58 public: 59 typedef T value_t; 60 public: 61 static const std::string& s_class() { 62 static const std::string s_v("tools::rroot 63 return s_v; 64 } 65 public: //iro 66 virtual void* cast(const std::string& a_clas 67 if(void* p = cmp_cast< leaf<T> >(this,a_cl 68 return base_leaf::cast(a_class); 69 } 70 virtual const std::string& s_cls() const {re 71 public: 72 static cid id_class() {return base_leaf_cid( 73 virtual void* cast(cid a_class) const { 74 if(void* p = cmp_cast<leaf>(this,a_class)) 75 return base_leaf::cast(a_class); 76 } 77 public: 78 virtual iro* copy() const {return new leaf<T 79 virtual bool stream(buffer& a_buffer) { 80 short v; 81 unsigned int _s,_c; 82 if(!a_buffer.read_version(v,_s,_c)) return 83 if(!base_leaf::stream(a_buffer)) return fa 84 if(!a_buffer.read(m_min)) return false; 85 if(!a_buffer.read(m_max)) return false; 86 if(!a_buffer.check_byte_count(_s,_c,leaf_s 87 return true; 88 } 89 public: //base_leaf 90 virtual bool read_buffer(buffer& a_buffer) { 91 if(m_leaf_count) { 92 leaf<int>* leaf_i = safe_cast<base_leaf, 93 if(!leaf_i) { 94 m_out << "tools::rroot::leaf::read_buf 95 return false; 96 } 97 int len; 98 if(!leaf_i->value(0,len)) { 99 m_out << "tools::rroot::leaf::read_buf 100 << " m_leaf_count " << m_leaf_count 101 << " leaf_i " << leaf_i 102 << " Name " << sout(leaf_i->name()) 103 << " Size " << leaf_i->num_elem() << s 104 return false; 105 } 106 107 if (len > leaf_i->get_max()) { //protect 108 m_out << "tools::rroot::leaf::read_buf 109 << ", len = " << len << " > max 110 << leaf_i->get_max() << std::end 111 len = leaf_i->get_max(); 112 } 113 114 uint32 ndata = len * m_length; 115 116 //if(!ndata) { 117 // delete [] m_value; 118 // m_value = new T[1]; 119 // m_size = 0; 120 // return true; 121 //} 122 123 if(ndata>m_size) { 124 delete [] m_value; 125 m_value = new T[ndata]; 126 } 127 128 m_size = ndata; 129 if(!a_buffer.read_fast_array(m_value,nda 130 m_out << "tools::rroot::leaf::read_buf 131 << " read_fast_array failed." 132 << std::endl; 133 return false; 134 } 135 return true; 136 137 } else { 138 if(m_length) { 139 if(m_length>m_size) { 140 delete [] m_value; 141 m_value = new T[m_length]; 142 } 143 m_size = m_length; 144 if(!a_buffer.read_fast_array<T>(m_valu 145 m_out << "tools::rroot::leaf::read_b 146 << " read_fast_array failed. m 147 << std::endl; 148 return false; 149 } 150 return true; 151 } else { 152 m_out << "tools::rroot::leaf::read_buf 153 << " read_fast_array failed. m_l 154 << std::endl; 155 return false; 156 } 157 } 158 return true; 159 } 160 virtual bool print_value(std::ostream& a_out 161 if(!m_value) return false; 162 if(a_index>=m_size) return false; 163 a_out << m_value[a_index]; 164 return true; 165 } 166 //virtual uint32 num_elem() const {return m_le 167 virtual uint32 num_elem() const {return m_si 168 public: 169 leaf(std::ostream& a_out,ifac& a_fac) 170 :base_leaf(a_out,a_fac) 171 ,m_min(T()),m_max(T()) 172 ,m_value(0),m_size(0) 173 {} 174 virtual ~leaf(){ 175 delete [] m_value; 176 } 177 protected: 178 leaf(const leaf& a_from) 179 :iro(a_from) 180 ,base_leaf(a_from) 181 ,m_min(T()),m_max(T()) 182 ,m_value(0),m_size(0) 183 {} 184 leaf& operator=(const leaf&){return *this;} 185 public: 186 bool value(uint32 a_index,T& a_value) const 187 if(!m_value) {a_value = T();return false;} 188 if(a_index>=m_size) {a_value = T();return 189 a_value = m_value[a_index]; 190 return true; 191 } 192 bool value(std::vector<T>& a_v) const { 193 if(!m_value) {a_v.clear();return false;} 194 a_v.resize(m_size); 195 for(uint32 index=0;index<m_size;index++) a 196 return true; 197 } 198 T get_max() const {return m_max;} 199 //uint32 size() const {return m_size;} 200 protected: 201 T m_min; //Minimum value if leaf range is 202 T m_max; //Maximum value if leaf range is 203 T* m_value; //!Pointer to data buffer 204 uint32 m_size; //size of m_value array. 205 }; 206 207 class leaf_string : public base_leaf { 208 static const std::string& s_store_class() { 209 static const std::string s_v("TLeafC"); 210 return s_v; 211 } 212 public: 213 static const std::string& s_class() { 214 static const std::string s_v("tools::rroot 215 return s_v; 216 } 217 public: //iro 218 virtual void* cast(const std::string& a_clas 219 if(void* p = cmp_cast<leaf_string>(this,a_ 220 return base_leaf::cast(a_class); 221 } 222 virtual const std::string& s_cls() const {re 223 public: 224 static cid id_class() {return leaf_string_ci 225 virtual void* cast(cid a_class) const { 226 if(void* p = cmp_cast<leaf_string>(this,a_ 227 return base_leaf::cast(a_class); 228 } 229 public: 230 virtual iro* copy() const {return new leaf_s 231 virtual bool stream(buffer& a_buffer) { 232 short v; 233 unsigned int _s,_c; 234 if(!a_buffer.read_version(v,_s,_c)) return 235 if(!base_leaf::stream(a_buffer)) return fa 236 if(!a_buffer.read(m_min)) return false; 237 if(!a_buffer.read(m_max)) return false; 238 if(!a_buffer.check_byte_count(_s,_c,s_stor 239 return true; 240 } 241 public: //base_leaf 242 virtual bool read_buffer(buffer& a_buffer) { 243 delete [] m_value; 244 m_value = 0; 245 246 unsigned char lenchar; 247 if(!a_buffer.read(lenchar)) { 248 m_out << "tools::rroot::leaf_string::rea 249 << " read(uchar) failed." 250 << std::endl; 251 return false; 252 } 253 uint32 len = 0; 254 if(lenchar < 255) { 255 len = lenchar; 256 } else { 257 if(!a_buffer.read(len)) { 258 m_out << "tools::rroot::leaf_string::r 259 << " read(int) failed." 260 << std::endl; 261 return false; 262 } 263 } 264 if(len) { 265 //if(!m_length) { 266 // m_out << "tools::rroot::leaf_string: 267 // return false; 268 //} 269 //if(len >= m_length) len = m_length-1; 270 271 m_value = new char[len+1]; 272 273 if(!a_buffer.read_fast_array(m_value,len 274 m_out << "tools::rroot::leaf_string::r 275 << " read_fast_array failed." 276 << std::endl; 277 delete [] m_value; 278 m_value = 0; 279 return false; 280 } 281 m_value[len] = 0; 282 } else { 283 m_value = new char[1]; 284 m_value[0] = 0; 285 } 286 287 return true; 288 } 289 virtual bool print_value(std::ostream& a_out 290 if(m_value) a_out << m_value; 291 return true; 292 } 293 virtual uint32 num_elem() const {return 1;} 294 public: 295 leaf_string(std::ostream& a_out,ifac& a_fac) 296 :base_leaf(a_out,a_fac) 297 ,m_min(0),m_max(0),m_value(0){} 298 virtual ~leaf_string(){ 299 delete [] m_value; 300 } 301 protected: 302 leaf_string(const leaf_string& a_from) 303 :iro(a_from),base_leaf(a_from) 304 ,m_min(0),m_max(0),m_value(0){} 305 leaf_string& operator=(const leaf_string&){r 306 public: 307 const char* value() const {return m_value;} 308 protected: 309 int m_min; 310 int m_max; 311 char* m_value; 312 }; 313 314 class leaf_element : public base_leaf { 315 static const std::string& s_store_class() { 316 static const std::string s_v("TLeafElement 317 return s_v; 318 } 319 public: 320 static const std::string& s_class() { 321 static const std::string s_v("tools::rroot 322 return s_v; 323 } 324 public: //iro 325 virtual void* cast(const std::string& a_clas 326 if(void* p = cmp_cast<leaf_element>(this,a 327 return base_leaf::cast(a_class); 328 } 329 virtual const std::string& s_cls() const {re 330 public: 331 static cid id_class() {return leaf_element_c 332 virtual void* cast(cid a_class) const { 333 if(void* p = cmp_cast<leaf_element>(this,a 334 return base_leaf::cast(a_class); 335 } 336 public: 337 virtual iro* copy() const {return new leaf_e 338 virtual bool stream(buffer& a_buffer) { 339 short v; 340 unsigned int _s,_c; 341 if(!a_buffer.read_version(v,_s,_c)) return 342 if(!base_leaf::stream(a_buffer)) return fa 343 if(!a_buffer.read(fID)) return false; 344 if(!a_buffer.read(fType)) return false; 345 if(!a_buffer.check_byte_count(_s,_c,s_stor 346 return true; 347 } 348 public: //base_leaf 349 virtual bool read_buffer(buffer&) { 350 m_out << "tools::rroot::leaf_element::read 351 return false; 352 } 353 virtual bool print_value(std::ostream&,uint3 354 virtual uint32 num_elem() const {return 0;} 355 public: 356 leaf_element(std::ostream& a_out,ifac& a_fac 357 :base_leaf(a_out,a_fac),fID(0),fType(0){} 358 virtual ~leaf_element(){} 359 protected: 360 leaf_element(const leaf_element& a_from) 361 :iro(a_from),base_leaf(a_from),fID(0),fType( 362 leaf_element& operator=(const leaf_element&) 363 public: 364 //int id() const {return fID;} 365 int leaf_type() const {return fType;} 366 protected: 367 int fID; //element serial number i 368 int fType; //leaf type 369 }; 370 371 }} 372 373 #include "iobject" 374 375 namespace tools { 376 namespace rroot { 377 378 class leaf_object : public base_leaf { 379 static const std::string& s_store_class() { 380 static const std::string s_v("TLeafObject" 381 return s_v; 382 } 383 public: 384 static const std::string& s_class() { 385 static const std::string s_v("tools::rroot 386 return s_v; 387 } 388 public: //iro 389 virtual void* cast(const std::string& a_clas 390 if(void* p = cmp_cast<leaf_object>(this,a_ 391 return base_leaf::cast(a_class); 392 } 393 virtual const std::string& s_cls() const {re 394 public: 395 static cid id_class() {return leaf_object_ci 396 virtual void* cast(cid a_class) const { 397 if(void* p = cmp_cast<leaf_object>(this,a_ 398 return base_leaf::cast(a_class); 399 } 400 public: 401 virtual iro* copy() const {return new leaf_o 402 virtual bool stream(buffer& a_buffer) { 403 short v; 404 unsigned int _s,_c; 405 if(!a_buffer.read_version(v,_s,_c)) return 406 if(!base_leaf::stream(a_buffer)) return fa 407 if(!a_buffer.read(fVirtual)) return false; 408 if(!a_buffer.check_byte_count(_s,_c,s_stor 409 return true; 410 } 411 public: //base_leaf 412 virtual bool read_buffer(buffer& a_buffer) { 413 if(!m_obj) { 414 m_out << "tools::rroot::leaf_object::rea 415 return false; 416 } 417 std::string fClassName; 418 if (fVirtual) { 419 unsigned char n; 420 if(!a_buffer.read(n)) { 421 m_out << "tools::rroot::leaf_object::r 422 << " read(unsigned char 423 << std::endl; 424 return false; 425 } 426 char classname[128]; 427 if(!a_buffer.read_fast_array(classname,n 428 m_out << "tools::rroot::leaf_object::r 429 << " readFastArray fail 430 << std::endl; 431 return false; 432 } 433 fClassName = classname; 434 } 435 if(m_obj->store_class_name()!=fClassName) 436 m_out << "tools::rroot::leaf_object::rea 437 << " fClassName " << sout(fClassNa 438 << ". m_obj.store_class_name() " << sout 439 << std::endl; 440 //return false; 441 } 442 if(!m_obj->stream(a_buffer)) { 443 m_out << "tools::rroot::leaf_object::rea 444 << " object stream failed." 445 << " Object store class was " << m 446 << std::endl; 447 return false; 448 } 449 // in case we had written a null pointer a 450 // we must delete it 451 //FIXME 452 //if (object->TestBit(kInvalidObject)) { 453 // if (object->GetUniqueID() == 123456789 454 // delete object; 455 // object = 0; 456 // } 457 //} 458 return true; 459 } 460 virtual bool print_value(std::ostream&,uint3 461 m_out << m_obj << std::endl; 462 return true; 463 } 464 virtual uint32 num_elem() const {return 0;} 465 public: 466 leaf_object(std::ostream& a_out,ifac& a_fac) 467 :base_leaf(a_out,a_fac),m_obj(0),fVirtual(tr 468 virtual ~leaf_object(){} 469 protected: 470 leaf_object(const leaf_object& a_from) 471 :iro(a_from),base_leaf(a_from),m_obj(0),fVir 472 leaf_object& operator=(const leaf_object&){r 473 public: 474 void set_object(iobject* a_obj) {m_obj = a_o 475 protected: 476 iobject* m_obj; 477 protected: 478 bool fVirtual; // Support for Virtuality 479 }; 480 481 // for SWIG : 482 inline leaf<int>* cast_leaf_int(base_leaf& 483 inline leaf<float>* cast_leaf_float(base_leaf 484 inline leaf<double>* cast_leaf_double(base_lea 485 486 }} 487 488 #endif