g4tools  5.4.0
mf
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_sg_mf
5 #define tools_sg_mf
6 
7 // mf for multiple field.
8 
9 #include "bmf"
10 
11 #include "../stype"
12 #include "../cstr"
13 #include "../io/iwbuf"
14 #include "../io/irbuf"
15 #include "../HEADER"
16 
17 namespace tools {
18 namespace sg {
19 
20 template <class T>
21 class mf : public bmf<T> {
22  typedef bmf<T> parent;
23 public:
24  static const std::string& s_class() {
25  static const std::string s_v("tools::sg::mf<"+stype(T())+">");
26  return s_v;
27  }
28  virtual void* cast(const std::string& a_class) const {
29  if(void* p = cmp_cast< mf<T> >(this,a_class)) {return p;}
30  return parent::cast(a_class);
31  }
32  virtual const std::string& s_cls() const {return s_class();}
33 public:
34  virtual bool write(io::iwbuf& a_buffer) {
35  const std::vector<T>& vec = parent::m_values;
36  return a_buffer.write_vec((uint32)vec.size(),vec_data(vec));
37  }
38  virtual bool read(io::irbuf& a_buffer) {
39  std::vector<T>& vec = parent::m_values;
40  return a_buffer.read_std_vec(vec);
41  }
42  virtual bool dump(std::ostream& a_out) {
43  const std::vector<T>& vec = parent::m_values;
44  a_out << "size : " << vec.size() << std::endl;
45  typedef typename std::vector<T>::const_iterator cit_t;
46  for(cit_t it=vec.begin();it!=vec.end();++it) {
47  a_out << " " << (*it) << std::endl;
48  }
49  return true;
50  }
51  virtual bool s_value(std::string& a_s) const {a_s.clear();return false;}
52  virtual bool s2value(const std::string&) {return false;}
53 public:
54  mf(){}
55  mf(const T& a_v):parent(a_v){}
56  mf(const std::vector<T>& a_v):parent(a_v){}
57  virtual ~mf(){}
58 public:
59  mf(const mf& a_from):parent(a_from){}
60  mf& operator=(const mf& a_from){
61  //typedef typename parent::iterator bmf_t;
62  parent::operator=(a_from);
63  return *this;
64  }
65 public:
66  mf& operator=(const std::vector<T>& a_from){
67  parent::operator=(a_from);
68  return *this;
69  }
70  mf& operator=(const T& a_v){
71  parent::operator=(a_v);
72  return *this;
73  }
74 };
75 
76 class mf_string : public bmf<std::string> {
78 public:
79  virtual bool write(io::iwbuf& a_buffer) {
80  return a_buffer.write_vec(m_values);
81  }
82  virtual bool read(io::irbuf& a_buffer) {
83  std::vector<std::string>& vec = parent::m_values;
84  return a_buffer.read_vec(vec);
85  }
86  virtual bool dump(std::ostream& a_out) {
87  const std::vector<std::string>& vec = parent::m_values;
88  a_out << "size : " << vec.size() << std::endl;
89  std::vector<std::string>::const_iterator it;
90  for(it=vec.begin();it!=vec.end();++it) {
91  a_out << " \"" << (*it) << "\"" << std::endl;
92  }
93  return true;
94  }
95  virtual bool s_value(std::string& a_s) const {a_s.clear();return false;}
96  virtual bool s2value(const std::string&) {return false;}
97 public:
99  mf_string(const std::string& a_v):parent(a_v){}
100  mf_string(const std::vector<std::string>& a_v):parent(a_v){}
101  virtual ~mf_string(){}
102 public:
103  mf_string(const mf_string& a_from):parent(a_from){}
104  mf_string& operator=(const mf_string& a_from){
105  parent::operator=(a_from);
106  return *this;
107  }
108 public:
109  mf_string& operator=(const std::vector<std::string>& a_value){
110  parent::operator=(a_value);
111  return *this;
112  }
113  mf_string& operator=(const char* a_cstr){
114  parent::operator=(a_cstr);
115  return *this;
116  }
117 };
118 
119 //exa tools::sg::entries.mf_vec<entry_type>
120 
121 template <class T>
122 class mf_enum : public bmf<T> {
123  typedef bmf<T> parent;
124 public:
125  static const std::string& s_class() {
126  static const std::string s_v("tools::sg::mf_enum");
127  return s_v;
128  }
129  virtual void* cast(const std::string& a_class) const {
130  if(void* p = cmp_cast< mf_enum<T> >(this,a_class)) {return p;}
131  return parent::cast(a_class);
132  }
133  virtual const std::string& s_cls() const {return s_class();}
134 public:
135  virtual bool write(io::iwbuf& a_buffer) {
136  const std::vector<T>& vec = parent::m_values;
137  std::vector<int16> v; //an enum can be negative.
138  typedef typename std::vector<T>::const_iterator cit_t;
139  for(cit_t it=vec.begin();it!=vec.end();++it) v.push_back(*it);
140  return a_buffer.write_vec((uint32)v.size(),vec_data(v));
141  }
142  virtual bool read(io::irbuf& a_buffer) {
143  std::vector<int16> v; //an enum can be negative.
144  if(!a_buffer.read_std_vec(v)) return false;
145  std::vector<T>& vec = parent::m_values;
146  vec.clear();
147  std::vector<int16>::const_iterator it;
148  for(it=v.begin();it!=v.end();++it) vec.push_back((T)(*it));
149  return true;
150  }
151  virtual bool dump(std::ostream& a_out) {
152  const std::vector<T>& vec = parent::m_values;
153  a_out << "size : " << vec.size() << std::endl;
154  typedef typename std::vector<T>::const_iterator cit_t;
155  for(cit_t it=vec.begin();it!=vec.end();++it) {
156  a_out << " " << (*it) << std::endl;
157  }
158  return true;
159  }
160  virtual bool s_value(std::string& a_s) const {a_s.clear();return false;}
161  virtual bool s2value(const std::string&) {return false;}
162 public:
164  mf_enum(const T& a_v):parent(a_v){}
165  mf_enum(const std::vector<T>& a_v):parent(a_v){}
166  virtual ~mf_enum(){}
167 public:
168  mf_enum(const mf_enum& a_from):parent(a_from){}
169  mf_enum& operator=(const mf_enum& a_from){
170  parent::operator=(a_from);
171  return *this;
172  }
173 };
174 
175 //exa mf_vec<colorf,float>
176 
178 //the three below funcs are for :
179 // mf_vec< std::vector<std::string> ,std::string> opts;
181 inline std::ostream& operator<<(std::ostream& a_out,const std::vector<std::string>&) {
182  //for mf_vec::dump.
183  return a_out;
184 }
185 
186 inline bool set_from_vec(std::vector<std::string>&,const std::vector<std::string>&) {
187  //for mf_vec::read(io::irbuf&)
188  return false;
189 }
190 
191 inline const std::string* get_data(const std::vector<std::string>& a_v) {
192  return vec_data(a_v);
193 }
197 
198 
199 template <class T,class TT>
200 class mf_vec : public bmf<T> {
201  typedef bmf<T> parent;
202 public:
203  static const std::string& s_class() {
204  static const std::string s_v("tools::sg::mf_vec<"+stype(T())+","+stype(TT())+">");
205  return s_v;
206  }
207  virtual void* cast(const std::string& a_class) const {
208  if(void* p = cmp_cast< mf_vec<T,TT> >(this,a_class)) {return p;}
209  return parent::cast(a_class);
210  }
211  virtual const std::string& s_cls() const {return s_class();}
212 public:
213  virtual bool write(io::iwbuf& a_buffer) {
214  const std::vector<T>& vec = parent::m_values;
215  typedef typename std::vector<TT> vec_t;
216  std::vector<vec_t> vec_vec;
217  typedef typename std::vector<T>::const_iterator cit_t;
218  for(cit_t it=vec.begin();it!=vec.end();++it) {
219 
220  const T& v = (*it);
221  size_t num = v.size();
222  const TT* d = get_data(v);
223 
224  std::vector<TT> std_vec(num);
225  for(size_t i=0;i<num;i++) std_vec[i] = d[i];
226 
227  vec_vec.push_back(std_vec);
228  }
229  return a_buffer.write_std_vec_vec(vec_vec);
230  }
231  virtual bool read(io::irbuf& a_buffer) {
232  std::vector<T>& vec = parent::m_values;
233  vec.clear();
234  typedef typename std::vector<TT> vec_t;
235  std::vector<vec_t> vec_vec;
236  if(!a_buffer.read_std_vec_vec(vec_vec)) return false;
237  typedef typename std::vector<vec_t>::iterator _it_t;
238  for(_it_t it=vec_vec.begin();it!=vec_vec.end();++it) {
239  T x;
240  // x colorf, *it = std::vector<float>
241  // x vecs, *it = std::vector<std::string>
242  if(!set_from_vec(x,*it)) {vec.clear();return false;}
243  vec.push_back(x);
244  }
245  return true;
246  }
247  virtual bool dump(std::ostream& a_out) {
248  const std::vector<T>& vec = parent::m_values;
249  a_out << "size : " << vec.size() << std::endl;
250  typedef typename std::vector<T>::const_iterator cit_t;
251  for(cit_t it=vec.begin();it!=vec.end();++it) {
252  a_out << " " << (*it) << std::endl;
253  }
254  return true;
255  }
256  virtual bool s_value(std::string& a_s) const {a_s.clear();return false;}
257  virtual bool s2value(const std::string&) {return false;}
258 public:
260  mf_vec(const T& a_v):parent(a_v){}
261  mf_vec(const std::vector<T>& a_v):parent(a_v){}
262  virtual ~mf_vec(){}
263 public:
264  mf_vec(const mf_vec& a_from):parent(a_from){}
265  mf_vec& operator=(const mf_vec& a_from){
266  parent::operator=(a_from);
267  return *this;
268  }
269 };
270 
271 template <class T>
272 class mf_std_vec : public bmf< std::vector<T> > {
273  typedef bmf< std::vector<T> > parent;
274 public:
275  static const std::string& s_class() {
276  static const std::string s_v("tools::sg::mf_std_vec<"+stype(T())+">");
277  return s_v;
278  }
279  virtual void* cast(const std::string& a_class) const {
280  if(void* p = cmp_cast< mf_std_vec<T> >(this,a_class)) {return p;}
281  return parent::cast(a_class);
282  }
283  virtual const std::string& s_cls() const {return s_class();}
284 public:
285  virtual bool write(io::iwbuf& a_buffer) {
286  //used in exlib/sg/text_freetype::unitext
287  const std::vector< std::vector<T> >& vec = parent::m_values;
288  return a_buffer.write_std_vec_vec(vec);
289  }
290  virtual bool read(io::irbuf& a_buffer) {
291  std::vector< std::vector<T> >& vec = parent::m_values;
292  return a_buffer.read_std_vec_vec(vec);
293  }
294  virtual bool dump(std::ostream& a_out) {
295  const std::vector< std::vector<T> >& vec = parent::m_values;
296  a_out << "size : " << vec.size() << std::endl;
297  typedef typename std::vector< std::vector<T> >::const_iterator cit_t;
298  for(cit_t it=vec.begin();it!=vec.end();++it) {
299  //a_out << " " << (*it) << std::endl;
300  }
301  return true;
302  }
303  virtual bool s_value(std::string& a_s) const {a_s.clear();return false;}
304  virtual bool s2value(const std::string&) {return false;}
305 public:
307  mf_std_vec(const T& a_v):parent(a_v){}
308  mf_std_vec(const std::vector<T>& a_v):parent(a_v){}
309  virtual ~mf_std_vec(){}
310 public:
311  mf_std_vec(const mf_std_vec& a_from):parent(a_from){}
312  mf_std_vec& operator=(const mf_std_vec& a_from){
313  parent::operator=(a_from);
314  return *this;
315  }
316 };
317 
318 }}
319 
320 #endif
tools::sg::mf_std_vec::mf_std_vec
mf_std_vec(const std::vector< T > &a_v)
Definition: mf:308
tools::sg::mf_std_vec::s2value
virtual bool s2value(const std::string &)
Definition: mf:304
tools::sg::mf::operator=
mf & operator=(const T &a_v)
Definition: mf:70
tools::sg::mf_std_vec
Definition: mf:272
tools::sg::mf_string::operator=
mf_string & operator=(const char *a_cstr)
Definition: mf:113
tools::sg::mf_std_vec::s_class
static const std::string & s_class()
Definition: mf:275
tools::sg::mf_vec::mf_vec
mf_vec()
Definition: mf:259
tools::sg::mf_enum::mf_enum
mf_enum(const std::vector< T > &a_v)
Definition: mf:165
tools::sg::mf_vec::s_class
static const std::string & s_class()
Definition: mf:203
tools::sg::mf_enum::operator=
mf_enum & operator=(const mf_enum &a_from)
Definition: mf:169
tools::sg::mf_vec::s2value
virtual bool s2value(const std::string &)
Definition: mf:257
tools::sg::mf::write
virtual bool write(io::iwbuf &a_buffer)
Definition: mf:34
tools::sg::mf_vec::read
virtual bool read(io::irbuf &a_buffer)
Definition: mf:231
tools::sg::mf_enum::dump
virtual bool dump(std::ostream &a_out)
Definition: mf:151
tools::io::irbuf::read_vec
virtual bool read_vec(uint32 &, uchar *&)=0
tools::sg::mf_vec
Definition: mf:200
tools::sg::mf_vec::mf_vec
mf_vec(const mf_vec &a_from)
Definition: mf:264
tools::sg::mf_std_vec::write
virtual bool write(io::iwbuf &a_buffer)
Definition: mf:285
tools::vec_data
const T * vec_data(const std::vector< T > &a_vec)
Definition: vdata:18
tools::sg::mf_vec::s_cls
virtual const std::string & s_cls() const
Definition: mf:211
tools::sg::mf_enum::s_class
static const std::string & s_class()
Definition: mf:125
tools::sg::mf_std_vec::mf_std_vec
mf_std_vec()
Definition: mf:306
tools::io::irbuf::read_std_vec
bool read_std_vec(std::vector< T > &a_x)
Definition: irbuf:65
tools::sg::mf_vec::mf_vec
mf_vec(const T &a_v)
Definition: mf:260
tools::sg::mf_std_vec::s_value
virtual bool s_value(std::string &a_s) const
Definition: mf:303
tools::sg::mf::cast
virtual void * cast(const std::string &a_class) const
Definition: mf:28
tools::sg::field
Definition: field:25
tools::sg::mf_vec::dump
virtual bool dump(std::ostream &a_out)
Definition: mf:247
tools::sg::mf_std_vec::~mf_std_vec
virtual ~mf_std_vec()
Definition: mf:309
tools::sg::set_from_vec
bool set_from_vec(std::vector< std::string > &, const std::vector< std::string > &)
Definition: mf:186
tools::sg::mf_vec::operator=
mf_vec & operator=(const mf_vec &a_from)
Definition: mf:265
tools::sg::mf_vec::mf_vec
mf_vec(const std::vector< T > &a_v)
Definition: mf:261
tools::sg::mf_enum::cast
virtual void * cast(const std::string &a_class) const
Definition: mf:129
tools::sg::mf::dump
virtual bool dump(std::ostream &a_out)
Definition: mf:42
tools::sg::mf_vec::write
virtual bool write(io::iwbuf &a_buffer)
Definition: mf:213
tools::sg::mf::mf
mf(const std::vector< T > &a_v)
Definition: mf:56
tools::sg::mf_enum::mf_enum
mf_enum(const mf_enum &a_from)
Definition: mf:168
tools::sg::mf
Definition: mf:21
tools::sg::mf_string::operator=
mf_string & operator=(const mf_string &a_from)
Definition: mf:104
tools::sg::mf::operator=
mf & operator=(const mf &a_from)
Definition: mf:60
tools::sg::mf_std_vec::operator=
mf_std_vec & operator=(const mf_std_vec &a_from)
Definition: mf:312
tools::sg::mf::s_class
static const std::string & s_class()
Definition: mf:24
tools::sg::mf_std_vec::s_cls
virtual const std::string & s_cls() const
Definition: mf:283
tools::sg::mf_vec::s_value
virtual bool s_value(std::string &a_s) const
Definition: mf:256
tools::sg::mf_std_vec::cast
virtual void * cast(const std::string &a_class) const
Definition: mf:279
tools::sg::mf_enum::write
virtual bool write(io::iwbuf &a_buffer)
Definition: mf:135
tools::sg::mf_std_vec::mf_std_vec
mf_std_vec(const mf_std_vec &a_from)
Definition: mf:311
tools::sg::field::operator=
field & operator=(const field &)
Definition: field:57
tools::io::iwbuf::write_vec
virtual bool write_vec(uint32, const uchar *)=0
TOOLS_HEADER
#define TOOLS_HEADER(a__class, a__sclass, a__parent)
Definition: HEADER:10
tools::sg::bmf::operator=
bmf & operator=(const bmf &a_from)
Definition: bmf:42
tools::io::iwbuf::write_std_vec_vec
virtual bool write_std_vec_vec(const std_vec_vec_uint_t &)=0
tools::sg::mf_enum::mf_enum
mf_enum(const T &a_v)
Definition: mf:164
tools::sg::mf::read
virtual bool read(io::irbuf &a_buffer)
Definition: mf:38
tools::sg::mf::~mf
virtual ~mf()
Definition: mf:57
tools::sg::mf::mf
mf()
Definition: mf:54
tools::io::irbuf::read_std_vec_vec
virtual bool read_std_vec_vec(std_vec_vec_uint_t &)=0
tools::sg::mf_std_vec::read
virtual bool read(io::irbuf &a_buffer)
Definition: mf:290
tools::sg::mf_string
Definition: mf:76
tools::sg::mf_string::mf_string
mf_string(const std::string &a_v)
Definition: mf:99
tools::sg::mf_string::~mf_string
virtual ~mf_string()
Definition: mf:101
tools::sg::mf_enum::s_cls
virtual const std::string & s_cls() const
Definition: mf:133
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::sg::mf_enum::read
virtual bool read(io::irbuf &a_buffer)
Definition: mf:142
tools::io::irbuf
Definition: irbuf:19
tools::sg::mf_string::mf_string
mf_string(const std::vector< std::string > &a_v)
Definition: mf:100
tools::sg::mf_string::s2value
virtual bool s2value(const std::string &)
Definition: mf:96
tools::sg::mf::s2value
virtual bool s2value(const std::string &)
Definition: mf:52
tools::cmp_cast
void * cmp_cast(const TO *a_this, const std::string &a_class)
Definition: scast:15
tools::sg::mf_string::operator=
mf_string & operator=(const std::vector< std::string > &a_value)
Definition: mf:109
tools::sg::bmf::m_values
std::vector< T > m_values
Definition: bmf:163
tools::sg::operator<<
std::ostream & operator<<(std::ostream &a_out, const std::vector< std::string > &)
Definition: mf:181
tools::sg::mf::s_value
virtual bool s_value(std::string &a_s) const
Definition: mf:51
tools::sg::mf::operator=
mf & operator=(const std::vector< T > &a_from)
Definition: mf:66
tools::sg::mf_vec::cast
virtual void * cast(const std::string &a_class) const
Definition: mf:207
tools::sg::mf_std_vec::mf_std_vec
mf_std_vec(const T &a_v)
Definition: mf:307
tools::sg::mf_enum::~mf_enum
virtual ~mf_enum()
Definition: mf:166
tools::sg::mf_string::read
virtual bool read(io::irbuf &a_buffer)
Definition: mf:82
tools::sg::mf_enum::s_value
virtual bool s_value(std::string &a_s) const
Definition: mf:160
tools::sg::mf::s_cls
virtual const std::string & s_cls() const
Definition: mf:32
tools::sg::get_data
const std::string * get_data(const std::vector< std::string > &a_v)
Definition: mf:191
tools::io::iwbuf
Definition: iwbuf:15
tools::sg::mf_enum::mf_enum
mf_enum()
Definition: mf:163
tools::sg::mf_string::write
virtual bool write(io::iwbuf &a_buffer)
Definition: mf:79
tools::sg::mf_string::dump
virtual bool dump(std::ostream &a_out)
Definition: mf:86
tools::sg::mf_enum::s2value
virtual bool s2value(const std::string &)
Definition: mf:161
tools::sg::mf::mf
mf(const T &a_v)
Definition: mf:55
tools::sg::bmf
Definition: bmf:21
tools::sg::mf_vec::~mf_vec
virtual ~mf_vec()
Definition: mf:262
tools::sg::mf_string::mf_string
mf_string(const mf_string &a_from)
Definition: mf:103
bmf
tools::stype
const std::string & stype(const mat4f &)
Definition: mat4f:73
tools::sg::bmf::cast
virtual void * cast(const std::string &a_class) const
Definition: bmf:30
tools::sg::mf_std_vec::dump
virtual bool dump(std::ostream &a_out)
Definition: mf:294
tools::sg::mf_string::s_value
virtual bool s_value(std::string &a_s) const
Definition: mf:95
tools::sg::mf_string::mf_string
mf_string()
Definition: mf:98
tools::sg::mf::mf
mf(const mf &a_from)
Definition: mf:59
tools::uint32
unsigned int uint32
Definition: typedefs:71
tools::sg::mf_enum
Definition: mf:122