inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/rroot/leaf
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_rroot_leaf
00005 #define inlib_rroot_leaf
00006 
00007 #include "base_leaf"
00008 #include "../stype"
00009 
00010 namespace inlib {
00011 namespace rroot {
00012 
00013 inline const std::string& leaf_store_class(char) {
00014   static const std::string s_v("TLeafB");
00015   return s_v;
00016 }
00017 inline const std::string& leaf_store_class(short) {
00018   static const std::string s_v("TLeafS");
00019   return s_v;
00020 }
00021 inline const std::string& leaf_store_class(int) {
00022   static const std::string s_v("TLeafI");
00023   return s_v;
00024 }
00025 inline const std::string& leaf_store_class(float) {
00026   static const std::string s_v("TLeafF");
00027   return s_v;
00028 }
00029 inline const std::string& leaf_store_class(double) {
00030   static const std::string s_v("TLeafD");
00031   return s_v;
00032 }
00033 
00034 inline const std::string& leaf_float_cls() {
00035   static const std::string s_v("inlib::rroot::leaf<float>");
00036   return s_v;
00037 }
00038 inline const std::string& leaf_double_cls() {
00039   static const std::string s_v("inlib::rroot::leaf<double>");
00040   return s_v;
00041 }
00042 inline const std::string& leaf_int_cls() {
00043   static const std::string s_v("inlib::rroot::leaf<int>");
00044   return s_v;
00045 }
00046 
00047 template <class T>
00048 class leaf : public base_leaf {
00049 public:
00050   static const std::string& s_class() {
00051     static const std::string s_v("inlib::rroot::leaf<"+stype(T())+">");
00052     return s_v;
00053   }
00054 public: //iro
00055   virtual void* cast(const std::string& a_class) const {
00056     if(void* p = inlib::cmp_cast< leaf<T> >(this,a_class)) {return p;}
00057     else return base_leaf::cast(a_class);
00058   }
00059   virtual bool stream(buffer& a_buffer) {
00060     delete [] m_value;
00061     m_value = 0;
00062 
00063     short v;
00064     unsigned int s,c;
00065     if(!a_buffer.read_version(v,s,c)) return false;
00066     if(!base_leaf::stream(a_buffer)) return false;
00067     if(!a_buffer.read(m_min)) return false;
00068     if(!a_buffer.read(m_max)) return false;
00069     if(!a_buffer.check_byte_count(s,c,leaf_store_class(T()))) return false;
00070 
00071     m_value = new T[m_length];
00072 
00073     return true;
00074   }
00075 public: //base_leaf
00076   virtual const std::string& s_cls() const {return s_class();}
00077   virtual bool read_basket(buffer& a_buffer) {
00078     if(!m_leaf_count && (m_ndata == 1)) {
00079 /*
00080       if(!aBuffer.read(m_value[0])) {
00081         std::ostream& out = this->branch().out();
00082         out << "Rio::Leaf::readBasket : \"" << name() << "\" :"
00083             << " read value failed."
00084             << std::endl;
00085         return false;
00086       }
00087       return true;
00088 */
00089       m_out << "inlib::rroot::leaf::read_basket :"
00090             << " case(1) not yet handled."
00091             << std::endl;
00092       return false;
00093     }else {
00094       if(m_leaf_count) {
00095         m_out << "inlib::rroot::leaf::read_basket :"
00096               << " case(2) not yet handled."
00097               << std::endl;
00098         return false;
00099 /*
00100         int len = m_leaf_count->number();
00101         if (len > m_leaf_count->maximum()) {
00102           std::ostream& out = this->branch().out();
00103           out << "Rio::Leaf::readBasket : \"" 
00104               << name() << "\", len = " << len << " and max = " 
00105               << m_leaf_count->maximum() << std::endl;
00106           len = m_leaf_count->maximum();
00107         }
00108         m_ndata = len * fLength;
00109         if(!aBuffer.readFastArray(m_value,len * fLength)) {
00110           std::ostream& out = this->branch().out();
00111           out << "Rio::Leaf::readBasket : \"" << name() << "\" :"
00112               << " readFastArray failed."
00113               << std::endl;
00114           return false;
00115         }
00116         return true;
00117 */
00118       } else {
00119         if(m_length) {
00120           if(!m_value) {
00121             delete [] m_value;
00122             m_value = new T[m_length]; //we assume m_length is constant.
00123           }
00124           if(!a_buffer.read_fast_array<T>(m_value,m_length)) {
00125             m_out << "inlib::rroot::leaf::read_basket :"
00126                   << " read_fast_array failed. m_length " << m_length
00127                   << std::endl;
00128             return false;
00129           }
00130           return true;
00131         } else {
00132           m_out << "inlib::rroot::leaf::read_basket :"
00133                 << " read_fast_array failed. m_length is zero."
00134                 << std::endl;
00135           return false;
00136         }
00137         //::printf("debug : value : %d %g\n",m_value[0],m_value[0]);
00138       }
00139     }
00140     return true;
00141   }
00142   virtual bool print_value(std::ostream& a_out,uint32 a_index) const {
00143     //if(!m_value) return false;
00144     a_out << m_value[a_index];
00145     return true;
00146   }
00147   virtual uint32 num_elem() const {return m_length;}
00148 public:
00149   leaf(std::ostream& a_out,rroot::branch& a_branch,ifac& a_fac)
00150   : base_leaf(a_out,a_branch,a_fac)
00151   ,m_min(T()),m_max(T())
00152   ,m_value(0)
00153   {}
00154   virtual ~leaf(){
00155     delete [] m_value;
00156   }
00157 protected:
00158   leaf(const leaf& a_from):iro(a_from),base_leaf(a_from){}
00159   leaf& operator=(const leaf&){return *this;}
00160 public:
00161   T value(uint32 a_index = 0) const {
00162     //WARNING : fast getter. No check are done on m_value and a_index.
00163     return m_value[a_index];
00164   }
00165 protected:
00166   T m_min;    //Minimum value if leaf range is specified
00167   T m_max;    //Maximum value if leaf range is specified
00168   T* m_value; 
00169   //T** fPointer;  //!Addresss of pointer to data buffer!
00170 };
00171 
00172 class leaf_string : public base_leaf {
00173 public:
00174   static const std::string& s_class() {
00175     static const std::string s_v("inlib::rroot::leaf_string");
00176     return s_v;
00177   }
00178 public: //iro
00179   virtual void* cast(const std::string& a_class) const {
00180     if(void* p = inlib::cmp_cast<leaf_string>(this,a_class)) {return p;}
00181     else return base_leaf::cast(a_class);
00182   }
00183   virtual bool stream(buffer& a_buffer) {
00184     short v;
00185     unsigned int s,c;
00186     if(!a_buffer.read_version(v,s,c)) return false;
00187     if(!base_leaf::stream(a_buffer)) return false;
00188     if(!a_buffer.read(m_min)) return false;
00189     if(!a_buffer.read(m_max)) return false;
00190     if(!a_buffer.check_byte_count(s,c,"TLeafC")) return false;
00191     return true;
00192   }
00193 public: //base_leaf
00194   virtual const std::string& s_cls() const {return s_class();}
00195   virtual bool read_basket(buffer& a_buffer) {
00196     delete [] m_value;
00197     m_value = 0;
00198 
00199     unsigned char lenchar;
00200     if(!a_buffer.read(lenchar)) {
00201       m_out << "inlib::rroot::leaf_string::read_basket :"
00202             << " read(uchar) failed."
00203             << std::endl;
00204       return false;
00205     }
00206     uint32 len = 0;
00207     if(lenchar < 255) {
00208       len = lenchar;
00209     } else {
00210       if(!a_buffer.read(len)) {
00211         m_out << "inlib::rroot::leaf_string::read_basket :"
00212               << " read(int) failed."
00213               << std::endl;
00214         return false;
00215       }
00216     }
00217     if(len) {
00218 
00219       if(!m_length) {
00220         m_out << "inlib::rroot::leaf_string::read_basket :"
00221               << " m_length is zero."
00222               << std::endl;
00223         return false;
00224       }
00225 
00226       if(len >= m_length) len = m_length-1;
00227   
00228       m_value = new char[len+1];
00229 
00230       if(!a_buffer.read_fast_array(m_value,len)) {
00231         m_out << "inlib::rroot::leaf_string::read_basket :"
00232               << " read_fast_array failed."
00233               << std::endl;
00234         delete [] m_value;
00235         m_value = 0;
00236         return false;
00237       }
00238       m_value[len] = 0;
00239     } else {
00240     }
00241 
00242     return true;
00243   }
00244   virtual bool print_value(std::ostream& a_out,uint32) const {
00245     if(m_value) a_out << m_value;
00246     return true;
00247   }
00248   virtual uint32 num_elem() const {return 1;}
00249 public:
00250   leaf_string(std::ostream& a_out,rroot::branch& a_branch,ifac& a_fac)
00251   : base_leaf(a_out,a_branch,a_fac)
00252   ,m_min(0),m_max(0),m_value(0){}
00253   virtual ~leaf_string(){
00254     delete [] m_value;
00255   }
00256 protected:
00257   leaf_string(const leaf_string& a_from):iro(a_from),base_leaf(a_from){}
00258   leaf_string& operator=(const leaf_string&){return *this;}
00259 public:
00260   const char* value() const {return m_value;}
00261 protected:
00262   int m_min;
00263   int m_max;
00264   char* m_value;
00265 };
00266 
00267 class leaf_element : public base_leaf {
00268 public:
00269   static const std::string& s_class() {
00270     static const std::string s_v("inlib::rroot::leaf_element");
00271     return s_v;
00272   }
00273 public: //iro
00274   virtual void* cast(const std::string& a_class) const {
00275     if(void* p = inlib::cmp_cast<leaf_element>(this,a_class)) {return p;}
00276     else return base_leaf::cast(a_class);
00277   }
00278   virtual bool stream(buffer& a_buffer) {
00279     short v;
00280     unsigned int s,c;
00281     if(!a_buffer.read_version(v,s,c)) return false;
00282     if(!base_leaf::stream(a_buffer)) return false;
00283     if(!a_buffer.read(fID)) return false;
00284     if(!a_buffer.read(fType)) return false;
00285     if(!a_buffer.check_byte_count(s,c,"TLeafElement")) return false;
00286     return true;
00287   }
00288 public: //base_leaf
00289   virtual const std::string& s_cls() const {return s_class();}
00290   virtual bool read_basket(buffer&) {
00291     m_out << "inlib::rroot::leaf_element::read_basket :"
00292           << " dummy."
00293           << std::endl;
00294     return false;
00295   }
00296   virtual bool print_value(std::ostream&,uint32) const {return true;}
00297   virtual uint32 num_elem() const {return 0;}
00298 public:
00299   leaf_element(std::ostream& a_out,rroot::branch& a_branch,ifac& a_fac)
00300   : base_leaf(a_out,a_branch,a_fac),fID(0),fType(0){}
00301   virtual ~leaf_element(){}
00302 protected:
00303   leaf_element(const leaf_element& a_from)
00304   :iro(a_from),base_leaf(a_from){}
00305   leaf_element& operator=(const leaf_element&){return *this;}
00306 protected:
00307   int fID;           //element serial number in fInfo
00308   int fType;         //leaf type
00309 };
00310 
00311 }}
00312 
00313 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines