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_stl_vector 5 #define tools_rroot_stl_vector 6 7 #include "buffer" 8 #include "cids" 9 10 #include "../stype" 11 #include "../scast" 12 #include "../cids" 13 14 namespace tools { 15 namespace rroot { 16 17 template <class T> 18 class stl_vector : public virtual iro, public std::vector<T> { 19 static const std::string& s_store_class() { 20 static const std::string s_v("vector<"+stype(T())+">"); 21 return s_v; 22 } 23 public: 24 static const std::string& s_class() { 25 static const std::string s_v("tools::rroot::stl_vector<"+stype(T())+">"); 26 return s_v; 27 } 28 public: //iro 29 virtual void* cast(const std::string& a_class) const { 30 if(void* p = cmp_cast< stl_vector<T> >(this,a_class)) return p; 31 return 0; 32 } 33 virtual const std::string& s_cls() const {return s_class();} 34 public: 35 static cid id_class() {return stl_vector_cid()+_cid(T());} 36 virtual void* cast(cid a_class) const { 37 if(void* p = cmp_cast< stl_vector<T> >(this,a_class)) {return p;} 38 return 0; 39 } 40 virtual iro* copy() const {return new stl_vector<T>(*this);} 41 virtual bool stream(buffer& a_buffer) { 42 std::vector<T>::clear(); 43 44 short v; 45 unsigned int _s,_c; 46 if(!a_buffer.read_version(v,_s,_c)) return false; 47 48 //::printf("debug : stl_vector::stream<%s> : version %d, byte count %d\n", 49 // stype(T()).c_str(),v,c); 50 51 unsigned int num; 52 if(!a_buffer.read(num)) return false; 53 54 //::printf("debug : stl_vector : %d\n",num); 55 56 if(num) { 57 T* vec = new T[num]; 58 if(!a_buffer.read_fast_array<T>(vec,num)) { 59 delete [] vec; 60 return false; 61 } 62 std::vector<T>::resize(num); 63 T* pos = vec; 64 for(unsigned int index=0;index<num;index++,pos++) { 65 std::vector<T>::operator[](index) = *pos; 66 } 67 delete [] vec; 68 } 69 70 return a_buffer.check_byte_count(_s,_c,s_store_class()); 71 } 72 public: 73 stl_vector(){ 74 #ifdef TOOLS_MEM 75 mem::increment(s_class().c_str()); 76 #endif 77 } 78 virtual ~stl_vector(){ 79 #ifdef TOOLS_MEM 80 mem::decrement(s_class().c_str()); 81 #endif 82 } 83 public: 84 stl_vector(const stl_vector& a_from) 85 :iro(a_from) 86 ,std::vector<T>(a_from) 87 { 88 #ifdef TOOLS_MEM 89 mem::increment(s_class().c_str()); 90 #endif 91 } 92 stl_vector& operator=(const stl_vector& a_from){ 93 std::vector<T>::operator=(a_from); 94 return *this; 95 } 96 }; 97 98 template <class T> 99 class stl_vector_vector 100 :public virtual iro 101 ,public std::vector< std::vector<T> > 102 { 103 static const std::string& s_store_class() { 104 static const std::string s_v("vector<vector<"+stype(T())+"> >"); 105 return s_v; 106 } 107 public: 108 static const std::string& s_class() { 109 static const std::string s_v 110 ("tools::rroot::stl_vector_vector<"+stype(T())+">"); 111 return s_v; 112 } 113 public: //iro 114 virtual void* cast(const std::string& a_class) const { 115 if(void* p=cmp_cast< stl_vector_vector<T> >(this,a_class)) return p; 116 return 0; 117 } 118 virtual const std::string& s_cls() const {return s_class();} 119 public: 120 static cid id_class() {return stl_vector_vector_cid()+_cid(T());} 121 virtual void* cast(cid a_class) const { 122 if(void* p = cmp_cast< stl_vector_vector<T> >(this,a_class)) {return p;} 123 return 0; 124 } 125 virtual iro* copy() const {return new stl_vector_vector<T>(*this);} 126 virtual bool stream(buffer& a_buffer) { 127 typedef typename std::vector<T> vec_t; 128 std::vector<vec_t>::clear(); 129 130 short v; 131 unsigned int _s,_c; 132 if(!a_buffer.read_version(v,_s,_c)) return false; 133 134 //::printf("debug : stl_vector_vector::stream<%s> : version %d, byte count %d\n",stype(T()).c_str(),v,c); 135 136 unsigned int vecn; 137 if(!a_buffer.read(vecn)) return false; 138 139 std::vector<vec_t>::resize(vecn); 140 //::printf("debug : stl_vector_vector : %d\n",vecn); 141 for(unsigned int veci=0;veci<vecn;veci++) { 142 vec_t& elem = std::vector<vec_t>::operator[](veci); 143 144 unsigned int num; 145 if(!a_buffer.read(num)) { 146 std::vector<vec_t>::clear(); 147 return false; 148 } 149 //::printf("debug : stl_vector_vector : index %d num %d\n",veci,num); 150 if(num) { 151 T* vec = new T[num]; 152 if(!a_buffer.read_fast_array<T>(vec,num)) { 153 delete [] vec; 154 std::vector<vec_t>::clear(); 155 return false; 156 } 157 elem.resize(num); 158 T* pos = vec; 159 for(unsigned int index=0;index<num;index++,pos++) elem[index] = *pos; 160 delete [] vec; 161 } 162 } 163 164 return a_buffer.check_byte_count(_s,_c,s_store_class()); 165 } 166 public: 167 stl_vector_vector(){ 168 #ifdef TOOLS_MEM 169 mem::increment(s_class().c_str()); 170 #endif 171 } 172 virtual ~stl_vector_vector(){ 173 #ifdef TOOLS_MEM 174 mem::decrement(s_class().c_str()); 175 #endif 176 } 177 public: 178 stl_vector_vector(const stl_vector_vector& a_from) 179 :iro(a_from) 180 ,std::vector< std::vector<T> >(a_from) 181 { 182 #ifdef TOOLS_MEM 183 mem::increment(s_class().c_str()); 184 #endif 185 } 186 stl_vector_vector& operator=(const stl_vector_vector& a_from){ 187 std::vector< std::vector<T> >::operator=(a_from); 188 return *this; 189 } 190 }; 191 192 class stl_vector_string : public virtual iro, public std::vector<std::string> { 193 static const std::string& s_store_class() { 194 static const std::string s_v("vector<string>"); 195 return s_v; 196 } 197 public: 198 static const std::string& s_class() { 199 static const std::string s_v("tools::rroot::stl_vector_string"); 200 return s_v; 201 } 202 public: //iro 203 virtual void* cast(const std::string& a_class) const { 204 if(void* p = cmp_cast<stl_vector_string>(this,a_class)) return p; 205 return 0; 206 } 207 virtual const std::string& s_cls() const {return s_class();} 208 public: 209 static cid id_class() {return stl_vector_string_cid();} 210 virtual void* cast(cid a_class) const { 211 if(void* p = cmp_cast<stl_vector_string>(this,a_class)) {return p;} 212 return 0; 213 } 214 virtual iro* copy() const {return new stl_vector_string(*this);} 215 virtual bool stream(buffer& a_buffer) { 216 std::vector<std::string>::clear(); 217 218 //uint32 startpos = a_buffer.length(); 219 220 //WARNING : not tested yet. 221 222 short v; 223 unsigned int _s,_c; 224 if(!a_buffer.read_version(v,_s,_c)) return false; 225 226 //::printf("debug : stl_vector_string::stream : version %d, byte count %d\n",v,c); 227 228 unsigned int num; 229 if(!a_buffer.read(num)) return false; 230 231 //::printf("debug : stl_vector_string : %d\n",num); 232 233 std::vector<std::string>::resize(num); 234 for(unsigned int index=0;index<num;index++) { 235 std::string& vs = std::vector<std::string>::operator[](index); 236 if(!a_buffer.read(vs)) { 237 std::vector<std::string>::clear(); 238 return false; 239 } 240 } 241 242 //a_buffer.set_offset(startpos+_c+sizeof(unsigned int)); 243 244 return a_buffer.check_byte_count(_s,_c,s_store_class()); 245 } 246 public: 247 stl_vector_string(){ 248 #ifdef TOOLS_MEM 249 mem::increment(s_class().c_str()); 250 #endif 251 } 252 virtual ~stl_vector_string(){ 253 #ifdef TOOLS_MEM 254 mem::decrement(s_class().c_str()); 255 #endif 256 } 257 public: 258 stl_vector_string(const stl_vector_string& a_from) 259 :iro(a_from) 260 ,std::vector<std::string>(a_from) 261 { 262 #ifdef TOOLS_MEM 263 mem::increment(s_class().c_str()); 264 #endif 265 } 266 stl_vector_string& operator=(const stl_vector_string& a_from){ 267 std::vector<std::string>::operator=(a_from); 268 return *this; 269 } 270 }; 271 272 }} 273 274 #endif