1 #include "daq/slc/base/AbstractDBObject.h"
3 #include <daq/slc/base/StringUtil.h>
10 AbstractDBObject::AbstractDBObject() : m_index(0), m_path(), m_id(0)
16 : m_index(obj.m_index), m_path(obj.m_path),
20 AbstractDBObject::~AbstractDBObject()
25 void AbstractDBObject::reset()
30 m_name_v = DBField::NameList();
31 m_pro_m = DBField::PropertyList();
36 DBField::PropertyList::const_iterator it = m_pro_m.find(name);
37 if (it != m_pro_m.end())
return it->second;
41 bool AbstractDBObject::hasField(
const std::string& name)
const
43 DBField::PropertyList::const_iterator it = m_pro_m.find(name);
44 return (it != m_pro_m.end());
47 bool AbstractDBObject::hasValue(
const std::string& name)
const
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;
55 bool AbstractDBObject::hasText(
const std::string& name)
const
57 DBField::PropertyList::const_iterator it = m_pro_m.find(name);
58 return hasField(name) && it->second.getType() == DBField::TEXT;
61 bool AbstractDBObject::hasObject(
const std::string& name)
const
63 DBField::PropertyList::const_iterator it = m_pro_m.find(name);
64 return hasField(name) &&
65 it->second.getType() == DBField::OBJECT;
70 if (!hasField(name)) {
71 m_name_v.push_back(name);
72 m_pro_m.insert(DBField::PropertyList::value_type(name, pro));
76 const std::string AbstractDBObject::getValueText(
const std::string& name)
79 const static int double_precision = std::numeric_limits<double>::digits10 + 2;
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);
94 throw (std::out_of_range(name +
" not found"));
97 void AbstractDBObject::setValueText(
const std::string& name,
98 const std::string& value)
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));
107 setChar(name, (
char)atoi(value.c_str()));
109 case DBField::SHORT: {
110 if (StringUtil::find(value,
"0x"))
111 setShort(name, (
short)strtol(value.c_str(), NULL, 0));
113 setShort(name, (
short)atoi(value.c_str()));
116 if (StringUtil::find(value,
"0x"))
117 setInt(name, (
int)strtol(value.c_str(), NULL, 0));
119 setInt(name, (
int)atoi(value.c_str()));
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;
129 throw (std::out_of_range(name +
" not found"));