g4tools  5.4.0
stl_vector
Go to the documentation of this file.
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:
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>
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:
168 #ifdef TOOLS_MEM
169  mem::increment(s_class().c_str());
170 #endif
171  }
173 #ifdef TOOLS_MEM
174  mem::decrement(s_class().c_str());
175 #endif
176  }
177 public:
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  }
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:
248 #ifdef TOOLS_MEM
249  mem::increment(s_class().c_str());
250 #endif
251  }
253 #ifdef TOOLS_MEM
254  mem::decrement(s_class().c_str());
255 #endif
256  }
257 public:
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  }
267  std::vector<std::string>::operator=(a_from);
268  return *this;
269  }
270 };
271 
272 }}
273 
274 #endif
tools::_cid
cid _cid(byte)
Definition: cids:14
tools::rroot::stl_vector_vector
Definition: stl_vector:102
tools::rroot::stl_vector_vector::stl_vector_vector
stl_vector_vector(const stl_vector_vector &a_from)
Definition: stl_vector:178
tools::rroot::stl_vector_cid
cid stl_vector_cid()
Definition: cids:47
tools::rroot::stl_vector_string::id_class
static cid id_class()
Definition: stl_vector:209
tools::rroot::rbuf::read_fast_array
bool read_fast_array(bool *b, uint32 n)
Definition: rbuf:261
tools::rroot::stl_vector_vector::~stl_vector_vector
virtual ~stl_vector_vector()
Definition: stl_vector:172
tools::rroot::stl_vector_string::s_class
static const std::string & s_class()
Definition: stl_vector:198
tools::rroot::stl_vector::s_class
static const std::string & s_class()
Definition: stl_vector:24
tools::rroot::stl_vector_string::copy
virtual iro * copy() const
Definition: stl_vector:214
tools::rroot::buffer::check_byte_count
bool check_byte_count(uint32 a_start_pos, uint32 a_byte_count, const std::string &a_store_cls)
Definition: buffer:568
buffer
tools::rroot::stl_vector::s_cls
virtual const std::string & s_cls() const
Definition: stl_vector:33
tools::rroot::stl_vector::id_class
static cid id_class()
Definition: stl_vector:35
tools::rroot::stl_vector::copy
virtual iro * copy() const
Definition: stl_vector:40
tools::rroot::stl_vector_string::cast
virtual void * cast(const std::string &a_class) const
Definition: stl_vector:203
tools::rroot::stl_vector_vector::operator=
stl_vector_vector & operator=(const stl_vector_vector &a_from)
Definition: stl_vector:186
tools::rroot::stl_vector_vector::copy
virtual iro * copy() const
Definition: stl_vector:125
tools::rroot::stl_vector_vector::cast
virtual void * cast(const std::string &a_class) const
Definition: stl_vector:114
tools::rroot::stl_vector_vector::s_cls
virtual const std::string & s_cls() const
Definition: stl_vector:118
tools::rroot::buffer::read_version
bool read_version(short &a_version)
Definition: buffer:503
tools::rroot::stl_vector::stl_vector
stl_vector()
Definition: stl_vector:73
tools::rroot::stl_vector::stl_vector
stl_vector(const stl_vector &a_from)
Definition: stl_vector:84
tools::rroot::stl_vector_string::operator=
stl_vector_string & operator=(const stl_vector_string &a_from)
Definition: stl_vector:266
tools::rroot::stl_vector::operator=
stl_vector & operator=(const stl_vector &a_from)
Definition: stl_vector:92
tools::rroot::stl_vector_vector::stream
virtual bool stream(buffer &a_buffer)
Definition: stl_vector:126
tools::rroot::stl_vector_string::stream
virtual bool stream(buffer &a_buffer)
Definition: stl_vector:215
tools::rroot::stl_vector::~stl_vector
virtual ~stl_vector()
Definition: stl_vector:78
tools::rroot::stl_vector_vector::id_class
static cid id_class()
Definition: stl_vector:120
tools::rroot::stl_vector_vector_cid
cid stl_vector_vector_cid()
Definition: cids:48
tools::rroot::stl_vector_string::stl_vector_string
stl_vector_string()
Definition: stl_vector:247
tools::rroot::stl_vector_string_cid
cid stl_vector_string_cid()
Definition: cids:37
tools::rroot::stl_vector_string::s_cls
virtual const std::string & s_cls() const
Definition: stl_vector:207
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::cmp_cast
void * cmp_cast(const TO *a_this, const std::string &a_class)
Definition: scast:15
tools::rroot::stl_vector_string::stl_vector_string
stl_vector_string(const stl_vector_string &a_from)
Definition: stl_vector:258
tools::rroot::buffer
Definition: buffer:43
tools::rroot::stl_vector::stream
virtual bool stream(buffer &a_buffer)
Definition: stl_vector:41
tools::rroot::stl_vector_string::~stl_vector_string
virtual ~stl_vector_string()
Definition: stl_vector:252
tools::rroot::stl_vector_vector::stl_vector_vector
stl_vector_vector()
Definition: stl_vector:167
tools::rroot::stl_vector::cast
virtual void * cast(const std::string &a_class) const
Definition: stl_vector:29
tools::rroot::rbuf::read
bool read(unsigned char &a_x)
Definition: rbuf:146
tools::rroot::stl_vector::cast
virtual void * cast(cid a_class) const
Definition: stl_vector:36
tools::rroot::iro
Definition: iro:19
cids
tools::stype
const std::string & stype(const mat4f &)
Definition: mat4f:73
tools::rroot::stl_vector
Definition: stl_vector:18
tools::rroot::stl_vector_string::cast
virtual void * cast(cid a_class) const
Definition: stl_vector:210
tools::rroot::stl_vector_string
Definition: stl_vector:192
tools::rroot::stl_vector_vector::s_class
static const std::string & s_class()
Definition: stl_vector:108
tools::cid
unsigned short cid
Definition: cid:9
tools::rroot::stl_vector_vector::cast
virtual void * cast(cid a_class) const
Definition: stl_vector:121