Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 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 19 static const std::string& s_store_class() { 20 static const std::string s_v("vector<"+sty 21 return s_v; 22 } 23 public: 24 static const std::string& s_class() { 25 static const std::string s_v("tools::rroot 26 return s_v; 27 } 28 public: //iro 29 virtual void* cast(const std::string& a_clas 30 if(void* p = cmp_cast< stl_vector<T> >(thi 31 return 0; 32 } 33 virtual const std::string& s_cls() const {re 34 public: 35 static cid id_class() {return stl_vector_cid 36 virtual void* cast(cid a_class) const { 37 if(void* p = cmp_cast< stl_vector<T> >(thi 38 return 0; 39 } 40 virtual iro* copy() const {return new stl_ve 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 47 48 //::printf("debug : stl_vector::stream<%s> 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 65 std::vector<T>::operator[](index) = *p 66 } 67 delete [] vec; 68 } 69 70 return a_buffer.check_byte_count(_s,_c,s_s 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_fr 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<vecto 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<"+styp 111 return s_v; 112 } 113 public: //iro 114 virtual void* cast(const std::string& a_clas 115 if(void* p=cmp_cast< stl_vector_vector<T> 116 return 0; 117 } 118 virtual const std::string& s_cls() const {re 119 public: 120 static cid id_class() {return stl_vector_vec 121 virtual void* cast(cid a_class) const { 122 if(void* p = cmp_cast< stl_vector_vector<T 123 return 0; 124 } 125 virtual iro* copy() const {return new stl_ve 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 133 134 //::printf("debug : stl_vector_vector::str 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 141 for(unsigned int veci=0;veci<vecn;veci++) 142 vec_t& elem = std::vector<vec_t>::operat 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 : 150 if(num) { 151 T* vec = new T[num]; 152 if(!a_buffer.read_fast_array<T>(vec,nu 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;ind 160 delete [] vec; 161 } 162 } 163 164 return a_buffer.check_byte_count(_s,_c,s_s 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 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_vecto 187 std::vector< std::vector<T> >::operator=(a 188 return *this; 189 } 190 }; 191 192 class stl_vector_string : public virtual iro, 193 static const std::string& s_store_class() { 194 static const std::string s_v("vector<strin 195 return s_v; 196 } 197 public: 198 static const std::string& s_class() { 199 static const std::string s_v("tools::rroot 200 return s_v; 201 } 202 public: //iro 203 virtual void* cast(const std::string& a_clas 204 if(void* p = cmp_cast<stl_vector_string>(t 205 return 0; 206 } 207 virtual const std::string& s_cls() const {re 208 public: 209 static cid id_class() {return stl_vector_str 210 virtual void* cast(cid a_class) const { 211 if(void* p = cmp_cast<stl_vector_string>(t 212 return 0; 213 } 214 virtual iro* copy() const {return new stl_ve 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 225 226 //::printf("debug : stl_vector_string::str 227 228 unsigned int num; 229 if(!a_buffer.read(num)) return false; 230 231 //::printf("debug : stl_vector_string : %d 232 233 std::vector<std::string>::resize(num); 234 for(unsigned int index=0;index<num;index++ 235 std::string& vs = std::vector<std::strin 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(u 243 244 return a_buffer.check_byte_count(_s,_c,s_s 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 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_vecto 267 std::vector<std::string>::operator=(a_from 268 return *this; 269 } 270 }; 271 272 }} 273 274 #endif