Warning, /include/Geant4/tools/rroot/stl_vector 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_stl_vector
0005 #define tools_rroot_stl_vector
0006
0007 #include "buffer"
0008 #include "cids"
0009
0010 #include "../stype"
0011 #include "../scast"
0012 #include "../cids"
0013
0014 namespace tools {
0015 namespace rroot {
0016
0017 template <class T>
0018 class stl_vector : public virtual iro, public std::vector<T> {
0019 static const std::string& s_store_class() {
0020 static const std::string s_v("vector<"+stype(T())+">");
0021 return s_v;
0022 }
0023 public:
0024 static const std::string& s_class() {
0025 static const std::string s_v("tools::rroot::stl_vector<"+stype(T())+">");
0026 return s_v;
0027 }
0028 public: //iro
0029 virtual void* cast(const std::string& a_class) const {
0030 if(void* p = cmp_cast< stl_vector<T> >(this,a_class)) return p;
0031 return 0;
0032 }
0033 virtual const std::string& s_cls() const {return s_class();}
0034 public:
0035 static cid id_class() {return stl_vector_cid()+_cid(T());}
0036 virtual void* cast(cid a_class) const {
0037 if(void* p = cmp_cast< stl_vector<T> >(this,a_class)) {return p;}
0038 return 0;
0039 }
0040 virtual iro* copy() const {return new stl_vector<T>(*this);}
0041 virtual bool stream(buffer& a_buffer) {
0042 std::vector<T>::clear();
0043
0044 short v;
0045 unsigned int _s,_c;
0046 if(!a_buffer.read_version(v,_s,_c)) return false;
0047
0048 //::printf("debug : stl_vector::stream<%s> : version %d, byte count %d\n",
0049 // stype(T()).c_str(),v,c);
0050
0051 unsigned int num;
0052 if(!a_buffer.read(num)) return false;
0053
0054 //::printf("debug : stl_vector : %d\n",num);
0055
0056 if(num) {
0057 T* vec = new T[num];
0058 if(!a_buffer.read_fast_array<T>(vec,num)) {
0059 delete [] vec;
0060 return false;
0061 }
0062 std::vector<T>::resize(num);
0063 T* pos = vec;
0064 for(unsigned int index=0;index<num;index++,pos++) {
0065 std::vector<T>::operator[](index) = *pos;
0066 }
0067 delete [] vec;
0068 }
0069
0070 return a_buffer.check_byte_count(_s,_c,s_store_class());
0071 }
0072 public:
0073 stl_vector(){
0074 #ifdef TOOLS_MEM
0075 mem::increment(s_class().c_str());
0076 #endif
0077 }
0078 virtual ~stl_vector(){
0079 #ifdef TOOLS_MEM
0080 mem::decrement(s_class().c_str());
0081 #endif
0082 }
0083 public:
0084 stl_vector(const stl_vector& a_from)
0085 :iro(a_from)
0086 ,std::vector<T>(a_from)
0087 {
0088 #ifdef TOOLS_MEM
0089 mem::increment(s_class().c_str());
0090 #endif
0091 }
0092 stl_vector& operator=(const stl_vector& a_from){
0093 std::vector<T>::operator=(a_from);
0094 return *this;
0095 }
0096 };
0097
0098 template <class T>
0099 class stl_vector_vector
0100 :public virtual iro
0101 ,public std::vector< std::vector<T> >
0102 {
0103 static const std::string& s_store_class() {
0104 static const std::string s_v("vector<vector<"+stype(T())+"> >");
0105 return s_v;
0106 }
0107 public:
0108 static const std::string& s_class() {
0109 static const std::string s_v
0110 ("tools::rroot::stl_vector_vector<"+stype(T())+">");
0111 return s_v;
0112 }
0113 public: //iro
0114 virtual void* cast(const std::string& a_class) const {
0115 if(void* p=cmp_cast< stl_vector_vector<T> >(this,a_class)) return p;
0116 return 0;
0117 }
0118 virtual const std::string& s_cls() const {return s_class();}
0119 public:
0120 static cid id_class() {return stl_vector_vector_cid()+_cid(T());}
0121 virtual void* cast(cid a_class) const {
0122 if(void* p = cmp_cast< stl_vector_vector<T> >(this,a_class)) {return p;}
0123 return 0;
0124 }
0125 virtual iro* copy() const {return new stl_vector_vector<T>(*this);}
0126 virtual bool stream(buffer& a_buffer) {
0127 typedef typename std::vector<T> vec_t;
0128 std::vector<vec_t>::clear();
0129
0130 short v;
0131 unsigned int _s,_c;
0132 if(!a_buffer.read_version(v,_s,_c)) return false;
0133
0134 //::printf("debug : stl_vector_vector::stream<%s> : version %d, byte count %d\n",stype(T()).c_str(),v,c);
0135
0136 unsigned int vecn;
0137 if(!a_buffer.read(vecn)) return false;
0138
0139 std::vector<vec_t>::resize(vecn);
0140 //::printf("debug : stl_vector_vector : %d\n",vecn);
0141 for(unsigned int veci=0;veci<vecn;veci++) {
0142 vec_t& elem = std::vector<vec_t>::operator[](veci);
0143
0144 unsigned int num;
0145 if(!a_buffer.read(num)) {
0146 std::vector<vec_t>::clear();
0147 return false;
0148 }
0149 //::printf("debug : stl_vector_vector : index %d num %d\n",veci,num);
0150 if(num) {
0151 T* vec = new T[num];
0152 if(!a_buffer.read_fast_array<T>(vec,num)) {
0153 delete [] vec;
0154 std::vector<vec_t>::clear();
0155 return false;
0156 }
0157 elem.resize(num);
0158 T* pos = vec;
0159 for(unsigned int index=0;index<num;index++,pos++) elem[index] = *pos;
0160 delete [] vec;
0161 }
0162 }
0163
0164 return a_buffer.check_byte_count(_s,_c,s_store_class());
0165 }
0166 public:
0167 stl_vector_vector(){
0168 #ifdef TOOLS_MEM
0169 mem::increment(s_class().c_str());
0170 #endif
0171 }
0172 virtual ~stl_vector_vector(){
0173 #ifdef TOOLS_MEM
0174 mem::decrement(s_class().c_str());
0175 #endif
0176 }
0177 public:
0178 stl_vector_vector(const stl_vector_vector& a_from)
0179 :iro(a_from)
0180 ,std::vector< std::vector<T> >(a_from)
0181 {
0182 #ifdef TOOLS_MEM
0183 mem::increment(s_class().c_str());
0184 #endif
0185 }
0186 stl_vector_vector& operator=(const stl_vector_vector& a_from){
0187 std::vector< std::vector<T> >::operator=(a_from);
0188 return *this;
0189 }
0190 };
0191
0192 class stl_vector_string : public virtual iro, public std::vector<std::string> {
0193 static const std::string& s_store_class() {
0194 static const std::string s_v("vector<string>");
0195 return s_v;
0196 }
0197 public:
0198 static const std::string& s_class() {
0199 static const std::string s_v("tools::rroot::stl_vector_string");
0200 return s_v;
0201 }
0202 public: //iro
0203 virtual void* cast(const std::string& a_class) const {
0204 if(void* p = cmp_cast<stl_vector_string>(this,a_class)) return p;
0205 return 0;
0206 }
0207 virtual const std::string& s_cls() const {return s_class();}
0208 public:
0209 static cid id_class() {return stl_vector_string_cid();}
0210 virtual void* cast(cid a_class) const {
0211 if(void* p = cmp_cast<stl_vector_string>(this,a_class)) {return p;}
0212 return 0;
0213 }
0214 virtual iro* copy() const {return new stl_vector_string(*this);}
0215 virtual bool stream(buffer& a_buffer) {
0216 std::vector<std::string>::clear();
0217
0218 //uint32 startpos = a_buffer.length();
0219
0220 //WARNING : not tested yet.
0221
0222 short v;
0223 unsigned int _s,_c;
0224 if(!a_buffer.read_version(v,_s,_c)) return false;
0225
0226 //::printf("debug : stl_vector_string::stream : version %d, byte count %d\n",v,c);
0227
0228 unsigned int num;
0229 if(!a_buffer.read(num)) return false;
0230
0231 //::printf("debug : stl_vector_string : %d\n",num);
0232
0233 std::vector<std::string>::resize(num);
0234 for(unsigned int index=0;index<num;index++) {
0235 std::string& vs = std::vector<std::string>::operator[](index);
0236 if(!a_buffer.read(vs)) {
0237 std::vector<std::string>::clear();
0238 return false;
0239 }
0240 }
0241
0242 //a_buffer.set_offset(startpos+_c+sizeof(unsigned int));
0243
0244 return a_buffer.check_byte_count(_s,_c,s_store_class());
0245 }
0246 public:
0247 stl_vector_string(){
0248 #ifdef TOOLS_MEM
0249 mem::increment(s_class().c_str());
0250 #endif
0251 }
0252 virtual ~stl_vector_string(){
0253 #ifdef TOOLS_MEM
0254 mem::decrement(s_class().c_str());
0255 #endif
0256 }
0257 public:
0258 stl_vector_string(const stl_vector_string& a_from)
0259 :iro(a_from)
0260 ,std::vector<std::string>(a_from)
0261 {
0262 #ifdef TOOLS_MEM
0263 mem::increment(s_class().c_str());
0264 #endif
0265 }
0266 stl_vector_string& operator=(const stl_vector_string& a_from){
0267 std::vector<std::string>::operator=(a_from);
0268 return *this;
0269 }
0270 };
0271
0272 }}
0273
0274 #endif