g4tools  5.4.0
bmf
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_bmf
5 #define tools_sg_bmf
6 
7 // mf for multiple field.
8 
9 // bmf is intended to have no implementation of :
10 // virtual bool write(io::iwbuf&)
11 // virtual bool read(io::irbuf&)
12 
13 #include "field"
14 
15 #include "../vdata"
16 
17 namespace tools {
18 namespace sg {
19 
20 template <class T>
21 class bmf : public field {
22  typedef field parent;
23 public:
24  static const std::string& s_class() {
25  //we do not use stype(T()).
26  static const std::string s_v("tools::sg::bmf");
27  return s_v;
28  }
29 //static bool is_a(const std::string& a_class) {return rcmp(a_class,s_class());}
30  virtual void* cast(const std::string& a_class) const {
31  if(void* p = cmp_cast< bmf<T> >(this,a_class)) {return p;}
32  return parent::cast(a_class);
33  }
34  virtual const std::string& s_cls() const {return s_class();}
35 public:
36  bmf(){}
37  bmf(const T& a_value) {m_values.push_back(a_value);}
38  bmf(const std::vector<T>& a_v) {m_values = a_v;}
39  virtual ~bmf(){m_values.clear();}
40 public:
41  bmf(const bmf& a_from):parent(a_from),m_values(a_from.m_values){}
42  bmf& operator=(const bmf& a_from){
43  parent::operator=(a_from);
44  if(a_from.m_values!=m_values) m_touched = true;
45  m_values = a_from.m_values;
46  return *this;
47  }
48 public:
49  bmf& operator=(const std::vector<T>& a_from){
50  if(a_from!=m_values) m_touched = true;
51  m_values = a_from;
52  return *this;
53  }
54  bool operator==(const bmf& a_from) const {
55  return m_values==a_from.m_values;
56  }
57  bool operator!=(const bmf& a_from) const {
58  return !operator==(a_from);
59  }
60  const T& operator[](size_t a_index) const {
61  //WARNING : no check is done on a_index.
62  return m_values[a_index];
63  }
64  T& operator[](size_t a_index) {
65  //WARNING : no check is done on a_index.
66  return m_values[a_index];
67  }
68 public:
69  size_t size() const {return m_values.size();}
70  bool empty() const {return m_values.empty();}
71  const std::vector<T>& values() const {return m_values;}
72  std::vector<T>& values() {return m_values;}
73  void add(const T& a_value) {
74  m_values.push_back(a_value);
75  m_touched = true;
76  }
77  void add(const std::vector<T>& a_vals) {
78  if(a_vals.empty()) return;
79  typedef typename std::vector<T>::const_iterator const_it_t;
80  for(const_it_t it=a_vals.begin();it!=a_vals.end();++it){
81  m_values.push_back(*it);
82  }
83  m_touched = true;
84  }
85  void add_allocated(size_t& a_pos,const T& a_1,const T& a_2,const T& a_3) { //used in sg::plotter.
86  std::vector<T>& v = m_values;
87  v[a_pos] = a_1;a_pos++;
88  v[a_pos] = a_2;a_pos++;
89  v[a_pos] = a_3;a_pos++;
90  m_touched = true;
91  }
92  typedef typename std::vector<T>::iterator it_t;
93  void insert(const it_t& a_it,const T& a_value) {
94  m_values.insert(a_it,a_value);
95  m_touched = true;
96  }
97  bool set_value(size_t a_index,const T& a_value) {
98  if(a_index>=m_values.size()) return false;
99  if(m_values[a_index]!=a_value) m_touched = true;
100  m_values[a_index] = a_value;
101  return true;
102  }
103  bool get_value(size_t a_index,T& a_value) {
104  if(a_index>=m_values.size()) {a_value=T();return false;}
105  a_value = m_values[a_index];
106  return true;
107  }
108  void clear() {
109  if(m_values.size()) m_touched = true;
110  m_values.clear();
111  }
112 
113  void set_values(const std::vector<T>& a_values) {
114  if(a_values!=m_values) m_touched = true;
115  m_values = a_values;
116  }
117  void set_value(const T& a_value) { //used in ArcheryTune.
118  bool to_resize = m_values.size()==1?false:true;
119  bool is_eq = ( (m_values.size()>=1) && (m_values[0]==a_value) ) ? true : false;
120  if(to_resize) m_values.resize(1);
121  if(to_resize || !is_eq) m_touched = true;
122  m_values[0] = a_value;
123  }
124 
125 public: //for iv2sg
126  //bool setValues(size_t a_index,size_t a_num,const T* a_vs) {
127  // for(size_t index=0;index<a_num;index++) {
128  // if(!set1Value(a_index+index,a_vs[index])) return false;
129  // }
130  // return true;
131  //}
132 
133  bool setValues(size_t a_index,size_t a_num,const T* a_vs) {
134  // 012345678
135  // 234
136  if((a_index+a_num)>=m_values.size()) m_values.resize(a_index+a_num);
137  for(size_t index=0;index<a_num;index++) {
138  if(a_vs[index]!=m_values[a_index+index]) m_touched = true;
139  m_values[a_index+index] = a_vs[index];
140  }
141  return true;
142  }
143 
144  bool set1Value(size_t a_index,const T& a_value) {
145  if(a_index>=m_values.size()) m_values.resize(a_index+1);
146  if(m_values[a_index]!=a_value) m_touched = true;
147  m_values[a_index] = a_value;
148  return true;
149  }
150  bool setValue(const T& a_value) {set_value(a_value);return true;}
151 
152  bmf& operator=(const T& a_v){
153  if(!setValue(a_v)) {}
154  return *this;
155  }
156  size_t getNum() const {return m_values.size();}
157  T* getValues(size_t a_start) { //for gopaw.
158  if(a_start>=(m_values.size()+1)) return 0;
159  T* data = vec_data(m_values);
160  return data+a_start;
161  }
162 protected:
163  std::vector<T> m_values;
164 };
165 
166 }}
167 
168 #endif
tools::sg::bmf::values
std::vector< T > & values()
Definition: bmf:72
tools::sg::bmf::bmf
bmf(const bmf &a_from)
Definition: bmf:41
tools::sg::field::m_touched
bool m_touched
Definition: field:63
tools::sg::bmf::operator=
bmf & operator=(const std::vector< T > &a_from)
Definition: bmf:49
tools::sg::bmf::get_value
bool get_value(size_t a_index, T &a_value)
Definition: bmf:103
tools::sg::bmf::empty
bool empty() const
Definition: bmf:70
tools::sg::bmf::add
void add(const std::vector< T > &a_vals)
Definition: bmf:77
field
tools::sg::bmf::~bmf
virtual ~bmf()
Definition: bmf:39
tools::sg::bmf::add
void add(const T &a_value)
Definition: bmf:73
tools::sg::bmf::bmf
bmf(const std::vector< T > &a_v)
Definition: bmf:38
tools::vec_data
const T * vec_data(const std::vector< T > &a_vec)
Definition: vdata:18
tools::sg::field
Definition: field:25
tools::sg::bmf::operator[]
const T & operator[](size_t a_index) const
Definition: bmf:60
tools::sg::bmf::values
const std::vector< T > & values() const
Definition: bmf:71
tools::sg::bmf::getValues
T * getValues(size_t a_start)
Definition: bmf:157
tools::sg::bmf::bmf
bmf()
Definition: bmf:36
tools::sg::field::cast
virtual void * cast(const std::string &a_class) const
Definition: field:28
tools::sg::bmf::operator!=
bool operator!=(const bmf &a_from) const
Definition: bmf:57
tools::sg::field::operator=
field & operator=(const field &)
Definition: field:57
tools::sg::bmf::getNum
size_t getNum() const
Definition: bmf:156
tools::sg::bmf::operator=
bmf & operator=(const bmf &a_from)
Definition: bmf:42
tools::sg::bmf::s_class
static const std::string & s_class()
Definition: bmf:24
tools::sg::bmf::operator==
bool operator==(const bmf &a_from) const
Definition: bmf:54
tools::sg::bmf::set_value
bool set_value(size_t a_index, const T &a_value)
Definition: bmf:97
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::sg::bmf::clear
void clear()
Definition: bmf:108
tools::cmp_cast
void * cmp_cast(const TO *a_this, const std::string &a_class)
Definition: scast:15
tools::sg::bmf::m_values
std::vector< T > m_values
Definition: bmf:163
tools::sg::bmf::set_value
void set_value(const T &a_value)
Definition: bmf:117
tools::sg::bmf::add_allocated
void add_allocated(size_t &a_pos, const T &a_1, const T &a_2, const T &a_3)
Definition: bmf:85
tools::sg::bmf::setValue
bool setValue(const T &a_value)
Definition: bmf:150
tools::sg::bmf::operator=
bmf & operator=(const T &a_v)
Definition: bmf:152
tools::sg::bmf::set1Value
bool set1Value(size_t a_index, const T &a_value)
Definition: bmf:144
tools::sg::bmf::it_t
std::vector< T >::iterator it_t
Definition: bmf:92
tools::sg::bmf
Definition: bmf:21
tools::sg::bmf::size
size_t size() const
Definition: bmf:69
tools::sg::bmf::operator[]
T & operator[](size_t a_index)
Definition: bmf:64
tools::sg::bmf::setValues
bool setValues(size_t a_index, size_t a_num, const T *a_vs)
Definition: bmf:133
tools::sg::bmf::s_cls
virtual const std::string & s_cls() const
Definition: bmf:34
tools::sg::bmf::insert
void insert(const it_t &a_it, const T &a_value)
Definition: bmf:93
tools::sg::bmf::cast
virtual void * cast(const std::string &a_class) const
Definition: bmf:30
tools::sg::bmf::bmf
bmf(const T &a_value)
Definition: bmf:37
tools::sg::bmf::set_values
void set_values(const std::vector< T > &a_values)
Definition: bmf:113