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