Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 3 4 #ifndef tools_rroot_info 5 #define tools_rroot_info 6 7 //#include "buffer" 8 //#include "element" 9 #include "named" 10 #include "obj_array" 11 #include "../forit" 12 13 namespace tools { 14 namespace rroot { 15 16 class streamer_element : public virtual iro { 17 static const std::string& s_store_class() { 18 static const std::string s_v("TStreamerElement"); 19 return s_v; 20 } 21 public: 22 static const std::string& s_class() { 23 static const std::string s_v("tools::rroot::streamer_element"); 24 return s_v; 25 } 26 static cid id_class() {return streamer_element_cid();} 27 public: //iro 28 virtual void* cast(const std::string& a_class) const { 29 if(void* p = cmp_cast<streamer_element>(this,a_class)) return p; 30 return 0; 31 } 32 virtual void* cast(cid a_class) const { 33 if(void* p = cmp_cast<streamer_element>(this,a_class)) {return p;} 34 else return 0; 35 } 36 virtual const std::string& s_cls() const {return s_class();} 37 virtual iro* copy() const {return new streamer_element(*this);} 38 39 virtual bool stream(buffer& a_buffer) { 40 short v; 41 unsigned int _s,_c; 42 if(!a_buffer.read_version(v,_s,_c)) return false; 43 if(!Named_stream(a_buffer,fName,fTitle)) return false; 44 if(!a_buffer.read(fType)) return false; 45 if(!a_buffer.read(fSize)) return false; 46 if(!a_buffer.read(fArrayLength)) return false; 47 if(!a_buffer.read(fArrayDim)) return false; 48 if(!a_buffer.read_fast_array<int>(fMaxIndex,5)) return false; 49 if(!a_buffer.read(fTypeName)) return false; 50 return a_buffer.check_byte_count(_s,_c,s_store_class()); 51 } 52 public: 53 virtual void out(std::ostream& aOut) const { 54 std::string _fname; 55 fullName(_fname); 56 char _s[128]; 57 snpf(_s,sizeof(_s)," %-14s%-15s offset=%3d type=%2d %-20s", 58 fTypeName.c_str(),_fname.c_str(),fOffset,fType,fTitle.c_str()); 59 aOut << _s << std::endl; 60 } 61 public: 62 streamer_element() 63 :fName(),fTitle(),fType(-1) 64 ,fSize(0),fArrayLength(0),fArrayDim(0),fOffset(0) 65 ,fTypeName(){ 66 #ifdef TOOLS_MEM 67 mem::increment(s_class().c_str()); 68 #endif 69 for(int i=0;i<5;i++) fMaxIndex[i] = 0; 70 } 71 virtual ~streamer_element(){ 72 #ifdef TOOLS_MEM 73 mem::decrement(s_class().c_str()); 74 #endif 75 } 76 protected: 77 streamer_element(const streamer_element& a_from) 78 :iro(a_from) 79 ,fName(a_from.fName),fTitle(a_from.fTitle) 80 ,fType(a_from.fType),fSize(a_from.fSize) 81 ,fArrayLength(a_from.fArrayLength) 82 ,fArrayDim(a_from.fArrayDim),fOffset(a_from.fOffset) 83 ,fTypeName(a_from.fTypeName){ 84 #ifdef TOOLS_MEM 85 mem::increment(s_class().c_str()); 86 #endif 87 for(int i=0;i<5;i++) fMaxIndex[i] = a_from.fMaxIndex[i]; 88 } 89 streamer_element& operator=(const streamer_element& a_from){ 90 fName = a_from.fName; 91 fTitle = a_from.fTitle; 92 fType = a_from.fType; 93 fSize = a_from.fSize; 94 fArrayLength = a_from.fArrayLength; 95 fArrayDim = a_from.fArrayDim; 96 fOffset = a_from.fOffset; 97 fTypeName = a_from.fTypeName; 98 for(int i=0;i<5;i++) fMaxIndex[i] = a_from.fMaxIndex[i]; 99 return *this; 100 } 101 public: 102 virtual void fullName(std::string& a_s) const { 103 a_s = fName; 104 for (int i=0;i<fArrayDim;i++) { 105 char cdim[32]; 106 snpf(cdim,sizeof(cdim),"[%d]",fMaxIndex[i]); 107 a_s += cdim; 108 } 109 } 110 const std::string& type_name() const {return fTypeName;} 111 protected: //Named 112 std::string fName; 113 std::string fTitle; 114 protected: 115 int fType; //element type 116 int fSize; //sizeof element 117 int fArrayLength; //cumulative size of all array dims 118 int fArrayDim; //number of array dimensions 119 int fMaxIndex[5]; //Maximum array index for array dimension "dim" 120 int fOffset; //!element offset in class 121 //FIXME Int_t fNewType; //!new element type when reading 122 std::string fTypeName; //Data type name of data member 123 }; 124 125 class dummy_streamer_element : public streamer_element { 126 typedef streamer_element parent; 127 public: //iro 128 virtual iro* copy() const {return new dummy_streamer_element(*this);} 129 virtual bool stream(buffer& a_buffer) { 130 //the below code skips correctly the data in the file. 131 uint32 startpos = a_buffer.length(); 132 short v; 133 unsigned int _s,_c; 134 if(!a_buffer.read_version(v,_s,_c)) return false; 135 136 if(!parent::stream(a_buffer)) return false; 137 138 a_buffer.set_offset(startpos+_c+sizeof(unsigned int)); 139 if(!a_buffer.check_byte_count(_s,_c,"dummy_streamer_element")) return false; 140 return true; 141 } 142 public: 143 dummy_streamer_element(){} 144 virtual ~dummy_streamer_element(){} 145 protected: 146 dummy_streamer_element(const dummy_streamer_element& a_from):iro(a_from),parent(a_from){} 147 dummy_streamer_element& operator=(const dummy_streamer_element& a_from){ 148 parent::operator=(a_from); 149 return *this; 150 } 151 }; 152 153 class streamer_info : public virtual iro { 154 static const std::string& s_store_class() { 155 static const std::string s_v("TStreamerInfo"); 156 return s_v; 157 } 158 public: 159 static const std::string& s_class() { 160 static const std::string s_v("tools::rroot::streamer_info"); 161 return s_v; 162 } 163 static cid id_class() {return streamer_info_cid();} 164 public: //iro 165 virtual void* cast(const std::string& a_class) const { 166 if(void* p = cmp_cast<streamer_info>(this,a_class)) return p; 167 return 0; 168 } 169 virtual void* cast(cid a_class) const { 170 if(void* p = cmp_cast<streamer_info>(this,a_class)) {return p;} 171 else return 0; 172 } 173 virtual const std::string& s_cls() const {return s_class();} 174 virtual iro* copy() const {return new streamer_info(*this);} 175 176 virtual bool stream(buffer& a_buffer) { 177 short v; 178 unsigned int _s,_c; 179 if(!a_buffer.read_version(v,_s,_c)) return false; 180 181 if(!Named_stream(a_buffer,m_name,m_title)) return false; 182 if(!a_buffer.read(m_check_sum)) return false; 183 if(!a_buffer.read(m_streamed_class_version)) return false; 184 185 //TObjArray *fElements; //Array of TStreamerElements 186 {obj_array<streamer_element>* obj; 187 ifac::args args; 188 args[ifac::arg_class()] = (void*)&(streamer_element::s_class()); 189 bool obj_created; 190 if(!pointer_stream(a_buffer,m_fac,args,obj,obj_created)) { 191 a_buffer.out() << "tools::rroot::streamer_info::stream : " 192 << "can't read fElements." 193 << std::endl; 194 return false; 195 } 196 if(obj) m_elements.operator=(*obj); 197 if(obj_created) delete obj;} 198 199 return a_buffer.check_byte_count(_s,_c,s_store_class()); 200 } 201 public: 202 void out(std::ostream& a_out) const { 203 a_out << "streamer_info for class :" 204 << " " << m_name << ", version=" << m_streamed_class_version 205 << std::endl; 206 tools_vforcit(streamer_element*,m_elements,it) (*it)->out(a_out); 207 } 208 public: 209 streamer_info(ifac& a_fac) 210 :m_fac(a_fac) 211 ,m_name() 212 ,m_title() 213 ,m_check_sum(0) 214 ,m_streamed_class_version(0) 215 ,m_elements(a_fac) 216 { 217 #ifdef TOOLS_MEM 218 mem::increment(s_class().c_str()); 219 #endif 220 } 221 virtual ~streamer_info(){ 222 #ifdef TOOLS_MEM 223 mem::decrement(s_class().c_str()); 224 #endif 225 } 226 protected: 227 streamer_info(const streamer_info& a_from) 228 :iro(a_from) 229 ,m_fac(a_from.m_fac) 230 ,m_name(a_from.m_name) 231 ,m_title(a_from.m_name) 232 ,m_check_sum(a_from.m_check_sum) 233 ,m_streamed_class_version(a_from.m_streamed_class_version) 234 ,m_elements(a_from.m_elements) 235 { 236 #ifdef TOOLS_MEM 237 mem::increment(s_class().c_str()); 238 #endif 239 } 240 streamer_info& operator=(const streamer_info& a_from){ 241 m_name = a_from.m_name; 242 m_title = a_from.m_name; 243 m_check_sum = a_from.m_check_sum; 244 m_streamed_class_version = a_from.m_streamed_class_version; 245 m_elements = a_from.m_elements; 246 return *this; 247 } 248 public: 249 const std::string& name() const {return m_name;} 250 streamer_element* find_streamer_element(size_t a_index) const { 251 if(a_index>=m_elements.size()) return 0; 252 return m_elements[a_index]; 253 } 254 protected: 255 ifac& m_fac; 256 protected: //Named 257 std::string m_name; 258 std::string m_title; 259 protected: 260 unsigned int m_check_sum; //checksum of original class 261 int m_streamed_class_version; //Class version identifier 262 //int fNumber; //!Unique identifier 263 obj_array<streamer_element> m_elements; //Array of TStreamerElements 264 }; 265 266 }} 267 268 #endif