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