Belle II Software development
AbstractDBObject.cc
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#include "daq/slc/base/AbstractDBObject.h"
9
10#include <daq/slc/base/StringUtil.h>
11
12#include <cstdlib>
13#include <limits>
14
15using namespace Belle2;
16
17AbstractDBObject::AbstractDBObject() : m_index(0), m_path(), m_id(0)
18{
19
20}
21
22AbstractDBObject::AbstractDBObject(const AbstractDBObject& obj)
23 : m_index(obj.m_index), m_path(obj.m_path),
24 m_id(obj.m_id),
25 m_name(obj.m_name) {}
26
27AbstractDBObject::~AbstractDBObject()
28{
29 reset();
30}
31
33 const AbstractDBObject& object)
34{
35 Serializable::operator=(object);
36 m_index = object.m_index;
37 m_path = object.m_path;
38 m_id = object.m_id;
39 m_name = object.m_name;
40 m_name_v = object.m_name_v;
41 m_pro_m = object.m_pro_m;
42 return *this;
43}
44
45void AbstractDBObject::reset()
46{
47 m_path = "";
48 m_id = 0;
49 m_index = 0;
50 m_name_v = DBField::NameList();
51 m_pro_m = DBField::PropertyList();
52}
53
54DBField::Property AbstractDBObject::getProperty(const std::string& name) const
55{
56 DBField::PropertyList::const_iterator it = m_pro_m.find(name);
57 if (it != m_pro_m.end()) return it->second;
58 return DBField::Property();
59}
60
61bool AbstractDBObject::hasField(const std::string& name) const
62{
63 DBField::PropertyList::const_iterator it = m_pro_m.find(name);
64 return (it != m_pro_m.end());
65}
66
67bool AbstractDBObject::hasValue(const std::string& name) const
68{
69 DBField::PropertyList::const_iterator it = m_pro_m.find(name);
70 return hasField(name) &&
71 it->second.getType() != DBField::TEXT &&
72 it->second.getType() != DBField::OBJECT;
73}
74
75bool AbstractDBObject::hasText(const std::string& name) const
76{
77 DBField::PropertyList::const_iterator it = m_pro_m.find(name);
78 return hasField(name) && it->second.getType() == DBField::TEXT;
79}
80
81bool AbstractDBObject::hasObject(const std::string& name) const
82{
83 DBField::PropertyList::const_iterator it = m_pro_m.find(name);
84 return hasField(name) &&
85 it->second.getType() == DBField::OBJECT;
86}
87
88void AbstractDBObject::add(const std::string& name, DBField::Property pro)
89{
90 if (!hasField(name)) {
91 m_name_v.push_back(name);
92 m_pro_m.insert(DBField::PropertyList::value_type(name, pro));
93 }
94}
95
96const std::string AbstractDBObject::getValueText(const std::string& name)
97const
98{
99 const static int double_precision = std::numeric_limits<double>::digits10 + 2;
100
101 if (hasField(name)) {
102 switch (getProperty(name).getType()) {
103 case DBField::BOOL: return getBool(name) ? "true" : "false";
104 case DBField::CHAR: return StringUtil::form("%d", (int)getChar(name));
105 case DBField::SHORT: return StringUtil::form("%d", (int)getShort(name));
106 case DBField::INT: return StringUtil::form("%d", (int)getInt(name));
107 case DBField::LONG: return StringUtil::form("%ld", getLong(name));
108 case DBField::FLOAT: return StringUtil::form("%f", getFloat(name));
109 case DBField::DOUBLE: return StringUtil::form("%.*e", double_precision, getDouble(name));
110 case DBField::TEXT: return getText(name);
111 default: break;
112 }
113 }
114 throw (std::out_of_range(name + " not found"));
115}
116
117void AbstractDBObject::setValueText(const std::string& name,
118 const std::string& value)
119{
120 if (hasField(name)) {
121 switch (getProperty(name).getType()) {
122 case DBField::BOOL: setBool(name, value == "true" || value == "t"); break;
123 case DBField::CHAR: {
124 if (StringUtil::find(value, "0x"))
125 setChar(name, (char)strtol(value.c_str(), NULL, 0));
126 else
127 setChar(name, (char)atoi(value.c_str()));
128 } break;
129 case DBField::SHORT: {
130 if (StringUtil::find(value, "0x"))
131 setShort(name, (short)strtol(value.c_str(), NULL, 0));
132 else
133 setShort(name, (short)atoi(value.c_str()));
134 } break;
135 case DBField::INT: {
136 if (StringUtil::find(value, "0x"))
137 setInt(name, (int)strtol(value.c_str(), NULL, 0));
138 else
139 setInt(name, (int)atoi(value.c_str()));
140 } break;
141 case DBField::LONG: setLong(name, (long long)atoi(value.c_str())); break;
142 case DBField::FLOAT: setFloat(name, atof(value.c_str())); break;
143 case DBField::DOUBLE: setDouble(name, atof(value.c_str())); break;
144 case DBField::TEXT: setText(name, value); break;
145 default: break;
146 }
147 return;
148 }
149 throw (std::out_of_range(name + " not found"));
150}
151
const AbstractDBObject & operator=(const AbstractDBObject &object)
Operator =.
Abstract base class for different kinds of events.