Warning, /include/Geant4/tools/rroot/leaf 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_rroot_leaf
0005 #define tools_rroot_leaf
0006
0007 #include "base_leaf"
0008 #include "../stype"
0009 #include "../cids"
0010
0011 namespace tools {
0012 namespace rroot {
0013
0014 inline const std::string& leaf_store_class(char) {
0015 static const std::string s_v("TLeafB");
0016 return s_v;
0017 }
0018 inline const std::string& leaf_store_class(short) {
0019 static const std::string s_v("TLeafS");
0020 return s_v;
0021 }
0022 inline const std::string& leaf_store_class(int) {
0023 static const std::string s_v("TLeafI");
0024 return s_v;
0025 }
0026 inline const std::string& leaf_store_class(float) {
0027 static const std::string s_v("TLeafF");
0028 return s_v;
0029 }
0030 inline const std::string& leaf_store_class(double) {
0031 static const std::string s_v("TLeafD");
0032 return s_v;
0033 }
0034 inline const std::string& leaf_store_class(bool) {
0035 static const std::string s_v("TLeafO");
0036 return s_v;
0037 }
0038
0039 inline const std::string& leaf_float_cls() {
0040 static const std::string s_v("tools::rroot::leaf<float>");
0041 return s_v;
0042 }
0043 inline const std::string& leaf_double_cls() {
0044 static const std::string s_v("tools::rroot::leaf<double>");
0045 return s_v;
0046 }
0047 inline const std::string& leaf_int_cls() {
0048 static const std::string s_v("tools::rroot::leaf<int>");
0049 return s_v;
0050 }
0051 inline const std::string& leaf_bool_cls() {
0052 static const std::string s_v("tools::rroot::leaf<bool>");
0053 return s_v;
0054 }
0055
0056 template <class T>
0057 class leaf : public base_leaf {
0058 public:
0059 typedef T value_t;
0060 public:
0061 static const std::string& s_class() {
0062 static const std::string s_v("tools::rroot::leaf<"+stype(T())+">");
0063 return s_v;
0064 }
0065 public: //iro
0066 virtual void* cast(const std::string& a_class) const {
0067 if(void* p = cmp_cast< leaf<T> >(this,a_class)) {return p;}
0068 return base_leaf::cast(a_class);
0069 }
0070 virtual const std::string& s_cls() const {return s_class();}
0071 public:
0072 static cid id_class() {return base_leaf_cid()+_cid(T());}
0073 virtual void* cast(cid a_class) const {
0074 if(void* p = cmp_cast<leaf>(this,a_class)) {return p;}
0075 return base_leaf::cast(a_class);
0076 }
0077 public:
0078 virtual iro* copy() const {return new leaf<T>(*this);}
0079 virtual bool stream(buffer& a_buffer) {
0080 short v;
0081 unsigned int _s,_c;
0082 if(!a_buffer.read_version(v,_s,_c)) return false;
0083 if(!base_leaf::stream(a_buffer)) return false;
0084 if(!a_buffer.read(m_min)) return false;
0085 if(!a_buffer.read(m_max)) return false;
0086 if(!a_buffer.check_byte_count(_s,_c,leaf_store_class(T()))) return false;
0087 return true;
0088 }
0089 public: //base_leaf
0090 virtual bool read_buffer(buffer& a_buffer) {
0091 if(m_leaf_count) {
0092 leaf<int>* leaf_i = safe_cast<base_leaf, leaf<int> >(*m_leaf_count);
0093 if(!leaf_i) {
0094 m_out << "tools::rroot::leaf::read_buffer : leaf_count not a leaf<int>." << std::endl;
0095 return false;
0096 }
0097 int len;
0098 if(!leaf_i->value(0,len)) {
0099 m_out << "tools::rroot::leaf::read_buffer : leaf<int>.value() failed."
0100 << " m_leaf_count " << m_leaf_count
0101 << " leaf_i " << leaf_i
0102 << " Name " << sout(leaf_i->name())
0103 << " Size " << leaf_i->num_elem() << std::endl;
0104 return false;
0105 }
0106
0107 if (len > leaf_i->get_max()) { //protection.
0108 m_out << "tools::rroot::leaf::read_buffer : warning : " << sout(name())
0109 << ", len = " << len << " > max = "
0110 << leaf_i->get_max() << std::endl;
0111 len = leaf_i->get_max();
0112 }
0113
0114 uint32 ndata = len * m_length;
0115
0116 //if(!ndata) {
0117 // delete [] m_value;
0118 // m_value = new T[1];
0119 // m_size = 0;
0120 // return true;
0121 //}
0122
0123 if(ndata>m_size) {
0124 delete [] m_value;
0125 m_value = new T[ndata];
0126 }
0127
0128 m_size = ndata;
0129 if(!a_buffer.read_fast_array(m_value,ndata)) {
0130 m_out << "tools::rroot::leaf::read_buffer : \"" << name() << "\" :"
0131 << " read_fast_array failed."
0132 << std::endl;
0133 return false;
0134 }
0135 return true;
0136
0137 } else {
0138 if(m_length) {
0139 if(m_length>m_size) {
0140 delete [] m_value;
0141 m_value = new T[m_length];
0142 }
0143 m_size = m_length;
0144 if(!a_buffer.read_fast_array<T>(m_value,m_length)) {
0145 m_out << "tools::rroot::leaf::read_buffer :"
0146 << " read_fast_array failed. m_length " << m_length
0147 << std::endl;
0148 return false;
0149 }
0150 return true;
0151 } else {
0152 m_out << "tools::rroot::leaf::read_buffer :"
0153 << " read_fast_array failed. m_length is zero."
0154 << std::endl;
0155 return false;
0156 }
0157 }
0158 return true;
0159 }
0160 virtual bool print_value(std::ostream& a_out,uint32 a_index) const {
0161 if(!m_value) return false;
0162 if(a_index>=m_size) return false;
0163 a_out << m_value[a_index];
0164 return true;
0165 }
0166 //virtual uint32 num_elem() const {return m_length;}
0167 virtual uint32 num_elem() const {return m_size;}
0168 public:
0169 leaf(std::ostream& a_out,ifac& a_fac)
0170 :base_leaf(a_out,a_fac)
0171 ,m_min(T()),m_max(T())
0172 ,m_value(0),m_size(0)
0173 {}
0174 virtual ~leaf(){
0175 delete [] m_value;
0176 }
0177 protected:
0178 leaf(const leaf& a_from)
0179 :iro(a_from)
0180 ,base_leaf(a_from)
0181 ,m_min(T()),m_max(T())
0182 ,m_value(0),m_size(0)
0183 {}
0184 leaf& operator=(const leaf&){return *this;}
0185 public:
0186 bool value(uint32 a_index,T& a_value) const {
0187 if(!m_value) {a_value = T();return false;}
0188 if(a_index>=m_size) {a_value = T();return false;}
0189 a_value = m_value[a_index];
0190 return true;
0191 }
0192 bool value(std::vector<T>& a_v) const {
0193 if(!m_value) {a_v.clear();return false;}
0194 a_v.resize(m_size);
0195 for(uint32 index=0;index<m_size;index++) a_v[index] = m_value[index];
0196 return true;
0197 }
0198 T get_max() const {return m_max;}
0199 //uint32 size() const {return m_size;}
0200 protected:
0201 T m_min; //Minimum value if leaf range is specified
0202 T m_max; //Maximum value if leaf range is specified
0203 T* m_value; //!Pointer to data buffer
0204 uint32 m_size; //size of m_value array.
0205 };
0206
0207 class leaf_string : public base_leaf {
0208 static const std::string& s_store_class() {
0209 static const std::string s_v("TLeafC");
0210 return s_v;
0211 }
0212 public:
0213 static const std::string& s_class() {
0214 static const std::string s_v("tools::rroot::leaf_string");
0215 return s_v;
0216 }
0217 public: //iro
0218 virtual void* cast(const std::string& a_class) const {
0219 if(void* p = cmp_cast<leaf_string>(this,a_class)) {return p;}
0220 return base_leaf::cast(a_class);
0221 }
0222 virtual const std::string& s_cls() const {return s_class();}
0223 public:
0224 static cid id_class() {return leaf_string_cid();}
0225 virtual void* cast(cid a_class) const {
0226 if(void* p = cmp_cast<leaf_string>(this,a_class)) {return p;}
0227 return base_leaf::cast(a_class);
0228 }
0229 public:
0230 virtual iro* copy() const {return new leaf_string(*this);}
0231 virtual bool stream(buffer& a_buffer) {
0232 short v;
0233 unsigned int _s,_c;
0234 if(!a_buffer.read_version(v,_s,_c)) return false;
0235 if(!base_leaf::stream(a_buffer)) return false;
0236 if(!a_buffer.read(m_min)) return false;
0237 if(!a_buffer.read(m_max)) return false;
0238 if(!a_buffer.check_byte_count(_s,_c,s_store_class())) return false;
0239 return true;
0240 }
0241 public: //base_leaf
0242 virtual bool read_buffer(buffer& a_buffer) {
0243 delete [] m_value;
0244 m_value = 0;
0245
0246 unsigned char lenchar;
0247 if(!a_buffer.read(lenchar)) {
0248 m_out << "tools::rroot::leaf_string::read_buffer :"
0249 << " read(uchar) failed."
0250 << std::endl;
0251 return false;
0252 }
0253 uint32 len = 0;
0254 if(lenchar < 255) {
0255 len = lenchar;
0256 } else {
0257 if(!a_buffer.read(len)) {
0258 m_out << "tools::rroot::leaf_string::read_buffer :"
0259 << " read(int) failed."
0260 << std::endl;
0261 return false;
0262 }
0263 }
0264 if(len) {
0265 //if(!m_length) {
0266 // m_out << "tools::rroot::leaf_string::read_buffer : m_length is zero." << std::endl;
0267 // return false;
0268 //}
0269 //if(len >= m_length) len = m_length-1;
0270
0271 m_value = new char[len+1];
0272
0273 if(!a_buffer.read_fast_array(m_value,len)) {
0274 m_out << "tools::rroot::leaf_string::read_buffer :"
0275 << " read_fast_array failed."
0276 << std::endl;
0277 delete [] m_value;
0278 m_value = 0;
0279 return false;
0280 }
0281 m_value[len] = 0;
0282 } else {
0283 m_value = new char[1];
0284 m_value[0] = 0;
0285 }
0286
0287 return true;
0288 }
0289 virtual bool print_value(std::ostream& a_out,uint32) const {
0290 if(m_value) a_out << m_value;
0291 return true;
0292 }
0293 virtual uint32 num_elem() const {return 1;}
0294 public:
0295 leaf_string(std::ostream& a_out,ifac& a_fac)
0296 :base_leaf(a_out,a_fac)
0297 ,m_min(0),m_max(0),m_value(0){}
0298 virtual ~leaf_string(){
0299 delete [] m_value;
0300 }
0301 protected:
0302 leaf_string(const leaf_string& a_from)
0303 :iro(a_from),base_leaf(a_from)
0304 ,m_min(0),m_max(0),m_value(0){}
0305 leaf_string& operator=(const leaf_string&){return *this;}
0306 public:
0307 const char* value() const {return m_value;}
0308 protected:
0309 int m_min;
0310 int m_max;
0311 char* m_value;
0312 };
0313
0314 class leaf_element : public base_leaf {
0315 static const std::string& s_store_class() {
0316 static const std::string s_v("TLeafElement");
0317 return s_v;
0318 }
0319 public:
0320 static const std::string& s_class() {
0321 static const std::string s_v("tools::rroot::leaf_element");
0322 return s_v;
0323 }
0324 public: //iro
0325 virtual void* cast(const std::string& a_class) const {
0326 if(void* p = cmp_cast<leaf_element>(this,a_class)) {return p;}
0327 return base_leaf::cast(a_class);
0328 }
0329 virtual const std::string& s_cls() const {return s_class();}
0330 public:
0331 static cid id_class() {return leaf_element_cid();}
0332 virtual void* cast(cid a_class) const {
0333 if(void* p = cmp_cast<leaf_element>(this,a_class)) {return p;}
0334 return base_leaf::cast(a_class);
0335 }
0336 public:
0337 virtual iro* copy() const {return new leaf_element(*this);}
0338 virtual bool stream(buffer& a_buffer) {
0339 short v;
0340 unsigned int _s,_c;
0341 if(!a_buffer.read_version(v,_s,_c)) return false;
0342 if(!base_leaf::stream(a_buffer)) return false;
0343 if(!a_buffer.read(fID)) return false;
0344 if(!a_buffer.read(fType)) return false;
0345 if(!a_buffer.check_byte_count(_s,_c,s_store_class())) return false;
0346 return true;
0347 }
0348 public: //base_leaf
0349 virtual bool read_buffer(buffer&) {
0350 m_out << "tools::rroot::leaf_element::read_buffer : dummy." << std::endl;
0351 return false;
0352 }
0353 virtual bool print_value(std::ostream&,uint32) const {return true;}
0354 virtual uint32 num_elem() const {return 0;}
0355 public:
0356 leaf_element(std::ostream& a_out,ifac& a_fac)
0357 :base_leaf(a_out,a_fac),fID(0),fType(0){}
0358 virtual ~leaf_element(){}
0359 protected:
0360 leaf_element(const leaf_element& a_from)
0361 :iro(a_from),base_leaf(a_from),fID(0),fType(0){}
0362 leaf_element& operator=(const leaf_element&){return *this;}
0363 public:
0364 //int id() const {return fID;}
0365 int leaf_type() const {return fType;}
0366 protected:
0367 int fID; //element serial number in fInfo
0368 int fType; //leaf type
0369 };
0370
0371 }}
0372
0373 #include "iobject"
0374
0375 namespace tools {
0376 namespace rroot {
0377
0378 class leaf_object : public base_leaf {
0379 static const std::string& s_store_class() {
0380 static const std::string s_v("TLeafObject");
0381 return s_v;
0382 }
0383 public:
0384 static const std::string& s_class() {
0385 static const std::string s_v("tools::rroot::leaf_object");
0386 return s_v;
0387 }
0388 public: //iro
0389 virtual void* cast(const std::string& a_class) const {
0390 if(void* p = cmp_cast<leaf_object>(this,a_class)) {return p;}
0391 return base_leaf::cast(a_class);
0392 }
0393 virtual const std::string& s_cls() const {return s_class();}
0394 public:
0395 static cid id_class() {return leaf_object_cid();}
0396 virtual void* cast(cid a_class) const {
0397 if(void* p = cmp_cast<leaf_object>(this,a_class)) {return p;}
0398 return base_leaf::cast(a_class);
0399 }
0400 public:
0401 virtual iro* copy() const {return new leaf_object(*this);}
0402 virtual bool stream(buffer& a_buffer) {
0403 short v;
0404 unsigned int _s,_c;
0405 if(!a_buffer.read_version(v,_s,_c)) return false;
0406 if(!base_leaf::stream(a_buffer)) return false;
0407 if(!a_buffer.read(fVirtual)) return false;
0408 if(!a_buffer.check_byte_count(_s,_c,s_store_class())) return false;
0409 return true;
0410 }
0411 public: //base_leaf
0412 virtual bool read_buffer(buffer& a_buffer) {
0413 if(!m_obj) {
0414 m_out << "tools::rroot::leaf_object::read_buffer : m_obj is null." << std::endl;
0415 return false;
0416 }
0417 std::string fClassName;
0418 if (fVirtual) {
0419 unsigned char n;
0420 if(!a_buffer.read(n)) {
0421 m_out << "tools::rroot::leaf_object::read_buffer :"
0422 << " read(unsigned char) failed."
0423 << std::endl;
0424 return false;
0425 }
0426 char classname[128];
0427 if(!a_buffer.read_fast_array(classname,n+1)) {
0428 m_out << "tools::rroot::leaf_object::read_buffer :"
0429 << " readFastArray failed."
0430 << std::endl;
0431 return false;
0432 }
0433 fClassName = classname;
0434 }
0435 if(m_obj->store_class_name()!=fClassName) {
0436 m_out << "tools::rroot::leaf_object::read_buffer : WARNING : class mismatch :"
0437 << " fClassName " << sout(fClassName)
0438 << ". m_obj.store_class_name() " << sout(m_obj->store_class_name())
0439 << std::endl;
0440 //return false;
0441 }
0442 if(!m_obj->stream(a_buffer)) {
0443 m_out << "tools::rroot::leaf_object::read_buffer :"
0444 << " object stream failed."
0445 << " Object store class was " << m_obj->store_class_name() << "."
0446 << std::endl;
0447 return false;
0448 }
0449 // in case we had written a null pointer a Zombie object was created
0450 // we must delete it
0451 //FIXME
0452 //if (object->TestBit(kInvalidObject)) {
0453 // if (object->GetUniqueID() == 123456789) {
0454 // delete object;
0455 // object = 0;
0456 // }
0457 //}
0458 return true;
0459 }
0460 virtual bool print_value(std::ostream&,uint32) const {
0461 m_out << m_obj << std::endl;
0462 return true;
0463 }
0464 virtual uint32 num_elem() const {return 0;}
0465 public:
0466 leaf_object(std::ostream& a_out,ifac& a_fac)
0467 :base_leaf(a_out,a_fac),m_obj(0),fVirtual(true){}
0468 virtual ~leaf_object(){}
0469 protected:
0470 leaf_object(const leaf_object& a_from)
0471 :iro(a_from),base_leaf(a_from),m_obj(0),fVirtual(true){}
0472 leaf_object& operator=(const leaf_object&){return *this;}
0473 public:
0474 void set_object(iobject* a_obj) {m_obj = a_obj;} //do not get ownership.
0475 protected:
0476 iobject* m_obj;
0477 protected:
0478 bool fVirtual; // Support for Virtuality
0479 };
0480
0481 // for SWIG :
0482 inline leaf<int>* cast_leaf_int(base_leaf& a_leaf) {return safe_cast<base_leaf, leaf<int> >(a_leaf);}
0483 inline leaf<float>* cast_leaf_float(base_leaf& a_leaf) {return safe_cast<base_leaf, leaf<float> >(a_leaf);}
0484 inline leaf<double>* cast_leaf_double(base_leaf& a_leaf) {return safe_cast<base_leaf, leaf<double> >(a_leaf);}
0485
0486 }}
0487
0488 #endif