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