Belle II Software  release-06-01-15
AbstractDBObject.h
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 #ifndef _Belle2_AbstractDBObject_hh
9 #define _Belle2_AbstractDBObject_hh
10 
11 #include <daq/slc/base/FieldProperty.h>
12 
13 #include <daq/slc/base/Serializable.h>
14 
15 #include <string>
16 #include <stdexcept>
17 
18 namespace Belle2 {
25  class AbstractDBObject : public Serializable {
26 
27  public:
30  virtual ~AbstractDBObject();
31 
32  public:
33  const std::string& getPath() const { return m_path; }
34  void setPath(const std::string& path) { m_path = path; }
35  int getId() const { return m_id; }
36  void setId(int id) { m_id = id; }
37  const std::string& getName() const { return m_name; }
38  void setName(const std::string& name) { m_name = name; }
39  int getIndex() const { return m_index; }
40  void setIndex(int index) { m_index = index; }
41  DBField::NameList& getFieldNames() { return m_name_v; }
42  const DBField::NameList& getFieldNames() const { return m_name_v; }
43  DBField::Property getProperty(const std::string& name) const;
44  bool hasField(const std::string& name) const;
45  bool hasValue(const std::string& name) const;
46  bool hasText(const std::string& name) const;
47  bool hasObject(const std::string& name) const;
48  void add(const std::string& name, DBField::Property pro);
49  void setValue(const std::string& name, const std::string& value);
50  void setText(const std::string& name, const std::string& value) { addText(name, value); }
51 
52  public:
53  bool getBool(const std::string& name, int index = 0) const;
54  char getChar(const std::string& name, int index = 0) const;
55  short getShort(const std::string& name, int index = 0) const;
56  int getInt(const std::string& name, int index = 0) const;
57  long long getLong(const std::string& name, int index = 0) const;
58  float getFloat(const std::string& name, int index = 0) const;
59  double getDouble(const std::string& name, int index = 0) const;
60  void addBool(const std::string& name, bool value);
61  void addChar(const std::string& name, char value);
62  void addShort(const std::string& name, short value);
63  void addInt(const std::string& name, int value);
64  void addLong(const std::string& name, long long value);
65  void addFloat(const std::string& name, float value);
66  void addDouble(const std::string& name, double value);
67  void setBool(const std::string& name, bool value, int index = 0);
68  void setChar(const std::string& name, int value, int index = 0);
69  void setShort(const std::string& name, int value, int index = 0);
70  void setInt(const std::string& name, int value, int index = 0);
71  void setLong(const std::string& name, long long value, int index = 0);
72  void setFloat(const std::string& name, float value, int index = 0);
73  void setDouble(const std::string& name, double value, int index = 0);
74  const std::string getValueText(const std::string& name) const;
75 
76  public:
77  virtual const void* getValue(const std::string& name) const = 0;
78  virtual const std::string& getText(const std::string& name) const = 0;
79  virtual void addText(const std::string& name, const std::string& value) = 0;
80  virtual void addValue(const std::string& name, const void* value,
81  DBField::Type type, int length) = 0;
82  virtual void setValue(const std::string& name, const void* value, int index) = 0;
83  virtual void setValueText(const std::string& name, const std::string& value);
84 
85  protected:
86  virtual void reset();
87 
88  private:
89  int m_index;
90  std::string m_path;
91  int m_id;
92  std::string m_name;
93  DBField::NameList m_name_v;
94  DBField::PropertyList m_pro_m;
95 
96  private:
97  template<typename T>
98  T getD(const std::string& name, int index = 0) const
99  {
100  const void* value = getValue(name);
101  if (value == NULL/* || index >= getProperty(name).getLength()*/) {
102  throw (std::out_of_range(name + " not found"));
103  }
104  DBField::Type type(getProperty(name).getType());
105  if (type == DBField::INT) return (T)(((int*)value)[index]);
106  if (type == DBField::FLOAT) return (T)(((float*)value)[index]);
107  if (type == DBField::DOUBLE) return (T)(((double*)value)[index]);
108  if (type == DBField::SHORT) return (T)(((short*)value)[index]);
109  if (type == DBField::CHAR) return (T)(((char*)value)[index]);
110  if (type == DBField::LONG) return (T)(((long*)value)[index]);
111  return ((T*)value)[index];
112  }
113 
114  };
115 
116  inline bool AbstractDBObject::getBool(const std::string& name, int index) const
117  {
118  return getD<bool>(name, index);
119  }
120 
121  inline char AbstractDBObject::getChar(const std::string& name, int index) const
122  {
123  return getD<char>(name, index);
124  }
125 
126  inline short AbstractDBObject::getShort(const std::string& name, int index) const
127  {
128  return getD<short>(name, index);
129  }
130 
131  inline int AbstractDBObject::getInt(const std::string& name, int index) const
132  {
133  return getD<int>(name, index);
134  }
135 
136  inline long long AbstractDBObject::getLong(const std::string& name, int index) const
137  {
138  return getD<long long>(name, index);
139  }
140 
141  inline float AbstractDBObject::getFloat(const std::string& name, int index) const
142  {
143  return getD<float>(name, index);
144  }
145 
146  inline double AbstractDBObject::getDouble(const std::string& name, int index) const
147  {
148  return getD<double>(name, index);
149  }
150 
151  inline void AbstractDBObject::setBool(const std::string& name, bool value, int index)
152  {
153  setValue(name, &value, index);
154  }
155 
156  inline void AbstractDBObject::setChar(const std::string& name, int value, int index)
157  {
158  setValue(name, &value, index);
159  }
160 
161  inline void AbstractDBObject::setShort(const std::string& name, int value, int index)
162  {
163  setValue(name, &value, index);
164  }
165 
166  inline void AbstractDBObject::setInt(const std::string& name, int value, int index)
167  {
168  setValue(name, &value, index);
169  }
170 
171  inline void AbstractDBObject::setLong(const std::string& name, long long value, int index)
172  {
173  setValue(name, &value, index);
174  }
175 
176  inline void AbstractDBObject::setFloat(const std::string& name, float value, int index)
177  {
178  setValue(name, &value, index);
179  }
180 
181  inline void AbstractDBObject::setDouble(const std::string& name, double value, int index)
182  {
183  setValue(name, &value, index);
184  }
185 
186  inline void AbstractDBObject::addBool(const std::string& name, bool value)
187  {
188  addValue(name, &value, DBField::BOOL, 0);
189  }
190 
191  inline void AbstractDBObject::addChar(const std::string& name, char value)
192  {
193  addValue(name, &value, DBField::CHAR, 0);
194  }
195 
196  inline void AbstractDBObject::addShort(const std::string& name, short value)
197  {
198  addValue(name, &value, DBField::SHORT, 0);
199  }
200 
201  inline void AbstractDBObject::addInt(const std::string& name, int value)
202  {
203  addValue(name, &value, DBField::INT, 0);
204  }
205 
206  inline void AbstractDBObject::addLong(const std::string& name, long long value)
207  {
208  addValue(name, &value, DBField::LONG, 0);
209  }
210 
211  inline void AbstractDBObject::addFloat(const std::string& name, float value)
212  {
213  addValue(name, &value, DBField::FLOAT, 0);
214  }
215 
216  inline void AbstractDBObject::addDouble(const std::string& name, double value)
217  {
218  addValue(name, &value, DBField::DOUBLE, 0);
219  }
220 
222 }
223 
224 #endif
Abstract base class for different kinds of events.