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