g4tools  5.4.0
sf_vec
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_sf_vec
5 #define tools_sg_sf_vec
6 
7 #include "sf"
8 
9 #include "../words"
10 
11 namespace tools {
12 namespace sg {
13 
14 template <class T,class TT> //exa sf_vec<colorf,float>
15 class sf_vec : public bsf<T> {
16  typedef bsf<T> parent;
17 public:
18  static const std::string& s_class() {
19  static const std::string s_v("tools::sg::sf_vec<"+stype(T())+","+stype(TT())+">");
20  return s_v;
21  }
22  virtual void* cast(const std::string& a_class) const {
23  if(void* p = cmp_cast< sf_vec<T,TT> >(this,a_class)) {return p;}
24  return parent::cast(a_class);
25  }
26  virtual const std::string& s_cls() const {return s_class();}
27 public:
28  virtual bool write(io::iwbuf& a_buffer) {
29  const T& vec = parent::m_value;
30  const TT* d = get_data(vec);
31  return a_buffer.write_vec(vec.size(),d);
32  }
33  virtual bool read(io::irbuf& a_buffer) {
34  T& vec = parent::m_value;
35  uint32 n;
36  TT* v;
37  if(!a_buffer.read_vec(n,v)) {
38  //::printf("debug : sf_vec::read : failed(0).\n");
39  return false;
40  }
41  if(n!=vec.size()) {
42  delete [] v;
43 #ifdef TOOLS_MEM
44  mem::decrement(s_new().c_str());
45 #endif
46  //::printf("debug : sf_vec::read : failed(1) : %d (expected %d).\n",
47  // n,vec.size());
48  return false;
49  }
50  for(uint32 index=0;index<n;index++) vec[index] = v[index];
51  delete [] v;
52 #ifdef TOOLS_MEM
53  mem::decrement(s_new().c_str());
54 #endif
55  return true;
56  }
57  virtual bool dump(std::ostream& a_out) {
58  a_out << parent::m_value << std::endl;
59  return true;
60  }
61  virtual bool s_value(std::string& a_s) const {
62  a_s.clear();
63  const T& vec = parent::m_value;
64  for(size_t index=0;index<vec.size();index++) {
65  if(index) a_s += ' ';
66  std::ostringstream strm;
67  strm << vec[index];
68  a_s += strm.str();
69  }
70  return true;
71  }
72  virtual bool s2value(const std::string& a_s) {
73  std::vector<std::string> ws;
74  words(a_s," ",false,ws);
75  T& vec = parent::m_value;
76  if(ws.size()!=vec.size()) return false;
77  T old_vec = vec;
78  for(size_t index=0;index<vec.size();index++) {
79  std::istringstream strm(ws[index].c_str());
80  TT v;
81  strm >> v;
82  if(strm.fail()) {
83  vec = old_vec;
84  return false;
85  }
86  if(vec[index]!=v) parent::m_touched = true;
87  vec[index] = v;
88  }
89  return true;
90  }
91 public:
92  sf_vec():parent(){}
93  sf_vec(const T& a_value):parent(a_value){}
94  virtual ~sf_vec(){}
95 public:
96  sf_vec(const sf_vec& a_from)
97  :parent(a_from){}
98  sf_vec& operator=(const sf_vec& a_from){
99  parent::operator=(a_from);
100  return *this;
101  }
102 public:
103  sf_vec& operator=(const T& a_value){
104  parent::operator=(a_value);
105  return *this;
106  }
107  sf_vec& operator+=(const T& a_value) {
108  parent::value(parent::value()+a_value);
109  return *this;
110  }
111 };
112 
113 }}
114 
115 #endif
tools::sg::field::m_touched
bool m_touched
Definition: field:63
tools::sg::bsf
Definition: bsf:19
tools::sg::sf_vec::operator=
sf_vec & operator=(const sf_vec &a_from)
Definition: sf_vec:98
tools::sg::sf_vec::operator+=
sf_vec & operator+=(const T &a_value)
Definition: sf_vec:107
tools::sg::bsf::m_value
T m_value
Definition: bsf:120
tools::io::irbuf::read_vec
virtual bool read_vec(uint32 &, uchar *&)=0
tools::sg::sf_vec::sf_vec
sf_vec(const T &a_value)
Definition: sf_vec:93
tools::sg::sf_vec::sf_vec
sf_vec(const sf_vec &a_from)
Definition: sf_vec:96
tools::sg::sf_vec::sf_vec
sf_vec()
Definition: sf_vec:92
tools::sg::sf_vec::write
virtual bool write(io::iwbuf &a_buffer)
Definition: sf_vec:28
tools::sg::field
Definition: field:25
tools::sg::bsf::cast
virtual void * cast(const std::string &a_class) const
Definition: bsf:27
tools::sg::sf_vec::dump
virtual bool dump(std::ostream &a_out)
Definition: sf_vec:57
tools::sg::sf_vec::s2value
virtual bool s2value(const std::string &a_s)
Definition: sf_vec:72
tools::sg::sf_vec
Definition: sf_vec:15
tools::sg::sf_vec::operator=
sf_vec & operator=(const T &a_value)
Definition: sf_vec:103
tools::io::iwbuf::write_vec
virtual bool write_vec(uint32, const uchar *)=0
sf
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::sg::sf_vec::read
virtual bool read(io::irbuf &a_buffer)
Definition: sf_vec:33
tools::words
void words(const std::string &a_string, const std::string &a_sep, bool a_take_empty, std::vector< std::string > &a_words, bool a_clear=true)
Definition: words:12
tools::io::irbuf
Definition: irbuf:19
tools::cmp_cast
void * cmp_cast(const TO *a_this, const std::string &a_class)
Definition: scast:15
tools::sg::sf_vec::s_class
static const std::string & s_class()
Definition: sf_vec:18
tools::sg::get_data
const std::string * get_data(const std::vector< std::string > &a_v)
Definition: mf:191
tools::sg::bsf::operator=
bsf & operator=(const bsf &a_from)
Definition: bsf:49
tools::sg::sf_vec::s_cls
virtual const std::string & s_cls() const
Definition: sf_vec:26
tools::io::iwbuf
Definition: iwbuf:15
tools::sg::bsf::value
T & value()
Definition: bsf:98
tools::sg::sf_vec::s_value
virtual bool s_value(std::string &a_s) const
Definition: sf_vec:61
tools::sg::sf_vec::cast
virtual void * cast(const std::string &a_class) const
Definition: sf_vec:22
tools::stype
const std::string & stype(const mat4f &)
Definition: mat4f:73
tools::sg::sf_vec::~sf_vec
virtual ~sf_vec()
Definition: sf_vec:94
tools::uint32
unsigned int uint32
Definition: typedefs:71