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>m_size) {
0117 delete [] m_value;
0118 m_value = new T[ndata];
0119 }
0120
0121 m_size = ndata;
0122 if(!a_buffer.read_fast_array(m_value,ndata)) {
0123 m_out << "tools::rroot::leaf::read_buffer : \"" << name() << "\" :"
0124 << " read_fast_array failed."
0125 << std::endl;
0126 return false;
0127 }
0128 return true;
0129
0130 } else {
0131 if(m_length) {
0132 if(m_length>m_size) {
0133 delete [] m_value;
0134 m_value = new T[m_length];
0135 }
0136 m_size = m_length;
0137 if(!a_buffer.read_fast_array<T>(m_value,m_length)) {
0138 m_out << "tools::rroot::leaf::read_buffer :"
0139 << " read_fast_array failed. m_length " << m_length
0140 << std::endl;
0141 return false;
0142 }
0143 return true;
0144 } else {
0145 m_out << "tools::rroot::leaf::read_buffer :"
0146 << " read_fast_array failed. m_length is zero."
0147 << std::endl;
0148 return false;
0149 }
0150 }
0151 return true;
0152 }
0153 virtual bool print_value(std::ostream& a_out,uint32 a_index) const {
0154 if(!m_value) return false;
0155 if(a_index>=m_size) return false;
0156 a_out << m_value[a_index];
0157 return true;
0158 }
0159 virtual uint32 num_elem() const {return m_size;}
0160 public:
0161 leaf(std::ostream& a_out,ifac& a_fac)
0162 :base_leaf(a_out,a_fac)
0163 ,m_min(T()),m_max(T())
0164 ,m_value(0),m_size(0)
0165 {}
0166 virtual ~leaf(){
0167 delete [] m_value;
0168 }
0169 protected:
0170 leaf(const leaf& a_from)
0171 :iro(a_from)
0172 ,base_leaf(a_from)
0173 ,m_min(T()),m_max(T())
0174 ,m_value(0),m_size(0)
0175 {}
0176 leaf& operator=(const leaf&){return *this;}
0177 public:
0178 bool value(uint32 a_index,T& a_value) const {
0179 if(!m_value) {a_value = T();return false;}
0180 if(a_index>=m_size) {a_value = T();return false;}
0181 a_value = m_value[a_index];
0182 return true;
0183 }
0184 bool value(std::vector<T>& a_v) const {
0185 if(!m_value) {a_v.clear();return false;}
0186 a_v.resize(m_size);
0187 for(uint32 index=0;index<m_size;index++) a_v[index] = m_value[index];
0188 return true;
0189 }
0190 T get_max() const {return m_max;}
0191 protected:
0192 T m_min; //Minimum value if leaf range is specified
0193 T m_max; //Maximum value if leaf range is specified
0194 T* m_value; //!Pointer to data buffer
0195 uint32 m_size; //size of m_value array.
0196 };
0197
0198 class leaf_string : public base_leaf {
0199 static const std::string& s_store_class() {
0200 static const std::string s_v("TLeafC");
0201 return s_v;
0202 }
0203 public:
0204 static const std::string& s_class() {
0205 static const std::string s_v("tools::rroot::leaf_string");
0206 return s_v;
0207 }
0208 public: //iro
0209 virtual void* cast(const std::string& a_class) const {
0210 if(void* p = cmp_cast<leaf_string>(this,a_class)) {return p;}
0211 return base_leaf::cast(a_class);
0212 }
0213 virtual const std::string& s_cls() const {return s_class();}
0214 public:
0215 static cid id_class() {return leaf_string_cid();}
0216 virtual void* cast(cid a_class) const {
0217 if(void* p = cmp_cast<leaf_string>(this,a_class)) {return p;}
0218 return base_leaf::cast(a_class);
0219 }
0220 public:
0221 virtual iro* copy() const {return new leaf_string(*this);}
0222 virtual bool stream(buffer& a_buffer) {
0223 short v;
0224 unsigned int _s,_c;
0225 if(!a_buffer.read_version(v,_s,_c)) return false;
0226 if(!base_leaf::stream(a_buffer)) return false;
0227 if(!a_buffer.read(m_min)) return false;
0228 if(!a_buffer.read(m_max)) return false;
0229 if(!a_buffer.check_byte_count(_s,_c,s_store_class())) return false;
0230 return true;
0231 }
0232 public: //base_leaf
0233 virtual bool read_buffer(buffer& a_buffer) {
0234 delete [] m_value;
0235 m_value = 0;
0236
0237 unsigned char lenchar;
0238 if(!a_buffer.read(lenchar)) {
0239 m_out << "tools::rroot::leaf_string::read_buffer :"
0240 << " read(uchar) failed."
0241 << std::endl;
0242 return false;
0243 }
0244 uint32 len = 0;
0245 if(lenchar < 255) {
0246 len = lenchar;
0247 } else {
0248 if(!a_buffer.read(len)) {
0249 m_out << "tools::rroot::leaf_string::read_buffer :"
0250 << " read(int) failed."
0251 << std::endl;
0252 return false;
0253 }
0254 }
0255 if(len) {
0256 m_value = new char[len+1];
0257
0258 if(!a_buffer.read_fast_array(m_value,len)) {
0259 m_out << "tools::rroot::leaf_string::read_buffer :"
0260 << " read_fast_array failed."
0261 << std::endl;
0262 delete [] m_value;
0263 m_value = 0;
0264 return false;
0265 }
0266 m_value[len] = 0;
0267 } else {
0268 m_value = new char[1];
0269 m_value[0] = 0;
0270 }
0271
0272 return true;
0273 }
0274 virtual bool print_value(std::ostream& a_out,uint32) const {
0275 if(m_value) a_out << m_value;
0276 return true;
0277 }
0278 virtual uint32 num_elem() const {return 1;}
0279 public:
0280 leaf_string(std::ostream& a_out,ifac& a_fac)
0281 :base_leaf(a_out,a_fac)
0282 ,m_min(0),m_max(0),m_value(0){}
0283 virtual ~leaf_string(){
0284 delete [] m_value;
0285 }
0286 protected:
0287 leaf_string(const leaf_string& a_from)
0288 :iro(a_from),base_leaf(a_from)
0289 ,m_min(0),m_max(0),m_value(0){}
0290 leaf_string& operator=(const leaf_string&){return *this;}
0291 public:
0292 const char* value() const {return m_value;}
0293 protected:
0294 int m_min;
0295 int m_max;
0296 char* m_value;
0297 };
0298
0299 class leaf_element : public base_leaf {
0300 static const std::string& s_store_class() {
0301 static const std::string s_v("TLeafElement");
0302 return s_v;
0303 }
0304 public:
0305 static const std::string& s_class() {
0306 static const std::string s_v("tools::rroot::leaf_element");
0307 return s_v;
0308 }
0309 public: //iro
0310 virtual void* cast(const std::string& a_class) const {
0311 if(void* p = cmp_cast<leaf_element>(this,a_class)) {return p;}
0312 return base_leaf::cast(a_class);
0313 }
0314 virtual const std::string& s_cls() const {return s_class();}
0315 public:
0316 static cid id_class() {return leaf_element_cid();}
0317 virtual void* cast(cid a_class) const {
0318 if(void* p = cmp_cast<leaf_element>(this,a_class)) {return p;}
0319 return base_leaf::cast(a_class);
0320 }
0321 public:
0322 virtual iro* copy() const {return new leaf_element(*this);}
0323 virtual bool stream(buffer& a_buffer) {
0324 short v;
0325 unsigned int _s,_c;
0326 if(!a_buffer.read_version(v,_s,_c)) return false;
0327 if(!base_leaf::stream(a_buffer)) return false;
0328 if(!a_buffer.read(fID)) return false;
0329 if(!a_buffer.read(fType)) return false;
0330 if(!a_buffer.check_byte_count(_s,_c,s_store_class())) return false;
0331 return true;
0332 }
0333 public: //base_leaf
0334 virtual bool read_buffer(buffer&) {
0335 m_out << "tools::rroot::leaf_element::read_buffer : dummy." << std::endl;
0336 return false;
0337 }
0338 virtual bool print_value(std::ostream&,uint32) const {return true;}
0339 virtual uint32 num_elem() const {return 0;}
0340 public:
0341 leaf_element(std::ostream& a_out,ifac& a_fac)
0342 :base_leaf(a_out,a_fac),fID(0),fType(0){}
0343 virtual ~leaf_element(){}
0344 protected:
0345 leaf_element(const leaf_element& a_from)
0346 :iro(a_from),base_leaf(a_from),fID(0),fType(0){}
0347 leaf_element& operator=(const leaf_element&){return *this;}
0348 public:
0349 //int id() const {return fID;}
0350 int leaf_type() const {return fType;}
0351 protected:
0352 int fID; //element serial number in fInfo
0353 int fType; //leaf type
0354 };
0355
0356 }}
0357
0358 #include "iobject"
0359
0360 namespace tools {
0361 namespace rroot {
0362
0363 class leaf_object : public base_leaf {
0364 static const std::string& s_store_class() {
0365 static const std::string s_v("TLeafObject");
0366 return s_v;
0367 }
0368 public:
0369 static const std::string& s_class() {
0370 static const std::string s_v("tools::rroot::leaf_object");
0371 return s_v;
0372 }
0373 public: //iro
0374 virtual void* cast(const std::string& a_class) const {
0375 if(void* p = cmp_cast<leaf_object>(this,a_class)) {return p;}
0376 return base_leaf::cast(a_class);
0377 }
0378 virtual const std::string& s_cls() const {return s_class();}
0379 public:
0380 static cid id_class() {return leaf_object_cid();}
0381 virtual void* cast(cid a_class) const {
0382 if(void* p = cmp_cast<leaf_object>(this,a_class)) {return p;}
0383 return base_leaf::cast(a_class);
0384 }
0385 public:
0386 virtual iro* copy() const {return new leaf_object(*this);}
0387 virtual bool stream(buffer& a_buffer) {
0388 short v;
0389 unsigned int _s,_c;
0390 if(!a_buffer.read_version(v,_s,_c)) return false;
0391 if(!base_leaf::stream(a_buffer)) return false;
0392 if(!a_buffer.read(fVirtual)) return false;
0393 if(!a_buffer.check_byte_count(_s,_c,s_store_class())) return false;
0394 return true;
0395 }
0396 public: //base_leaf
0397 virtual bool read_buffer(buffer& a_buffer) {
0398 if(!m_obj) {
0399 m_out << "tools::rroot::leaf_object::read_buffer : m_obj is null." << std::endl;
0400 return false;
0401 }
0402 std::string fClassName;
0403 if (fVirtual) {
0404 unsigned char n;
0405 if(!a_buffer.read(n)) {
0406 m_out << "tools::rroot::leaf_object::read_buffer :"
0407 << " read(unsigned char) failed."
0408 << std::endl;
0409 return false;
0410 }
0411 char classname[128];
0412 if(!a_buffer.read_fast_array(classname,n+1)) {
0413 m_out << "tools::rroot::leaf_object::read_buffer :"
0414 << " readFastArray failed."
0415 << std::endl;
0416 return false;
0417 }
0418 fClassName = classname;
0419 }
0420 if(m_obj->store_class_name()!=fClassName) {
0421 m_out << "tools::rroot::leaf_object::read_buffer : WARNING : class mismatch :"
0422 << " fClassName " << sout(fClassName)
0423 << ". m_obj.store_class_name() " << sout(m_obj->store_class_name())
0424 << std::endl;
0425 }
0426 if(!m_obj->stream(a_buffer)) {
0427 m_out << "tools::rroot::leaf_object::read_buffer :"
0428 << " object stream failed."
0429 << " Object store class was " << m_obj->store_class_name() << "."
0430 << std::endl;
0431 return false;
0432 }
0433
0434 return true;
0435 }
0436 virtual bool print_value(std::ostream&,uint32) const {
0437 m_out << m_obj << std::endl;
0438 return true;
0439 }
0440 virtual uint32 num_elem() const {return 0;}
0441 public:
0442 leaf_object(std::ostream& a_out,ifac& a_fac)
0443 :base_leaf(a_out,a_fac),m_obj(0),fVirtual(true){}
0444 virtual ~leaf_object(){}
0445 protected:
0446 leaf_object(const leaf_object& a_from)
0447 :iro(a_from),base_leaf(a_from),m_obj(0),fVirtual(true){}
0448 leaf_object& operator=(const leaf_object&){return *this;}
0449 public:
0450 void set_object(iobject* a_obj) {m_obj = a_obj;} //do not get ownership.
0451 protected:
0452 iobject* m_obj;
0453 protected:
0454 bool fVirtual; // Support for Virtuality
0455 };
0456
0457 // for SWIG :
0458 inline leaf<int>* cast_leaf_int(base_leaf& a_leaf) {return safe_cast<base_leaf, leaf<int> >(a_leaf);}
0459 inline leaf<float>* cast_leaf_float(base_leaf& a_leaf) {return safe_cast<base_leaf, leaf<float> >(a_leaf);}
0460 inline leaf<double>* cast_leaf_double(base_leaf& a_leaf) {return safe_cast<base_leaf, leaf<double> >(a_leaf);}
0461
0462 }}
0463
0464 #endif