1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215 | // Copyright (C) 2018-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <cc/stamped_value.h>
#include <exceptions/exceptions.h>
#include <boost/lexical_cast.hpp><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
namespace isc {
namespace data {
StampedValue::StampedValue(const std::string& name)
: StampedElement(), name_(name), value_() {
}
StampedValue::StampedValue(const std::string& name, const ElementPtr& value)
: StampedElement(), name_(name), value_(value) {
validateConstruct();
}
StampedValue::StampedValue(const std::string& name, const std::string& value)
: StampedElement(), name_(name), value_(Element::create(value)) {
validateConstruct();
}
StampedValuePtr
StampedValue::create(const std::string& name) {
return (StampedValuePtr(new StampedValue(name)));
}
StampedValuePtr
StampedValue::create(const std::string& name, const ElementPtr& value) {
return (StampedValuePtr(new StampedValue(name, value)));
}
StampedValuePtr
StampedValue::create(const std::string& name, const std::string& value) {
return (StampedValuePtr(new StampedValue(name, value)));
}
StampedValuePtr
StampedValue::create(const std::string& name, const std::string& value,
Element::types parameter_type) {
StampedValuePtr stamped_value;
try {
switch (parameter_type) {
case Element::string:
stamped_value = StampedValue::create(name, value);
break;
case Element::integer:
stamped_value = StampedValue::create(name,
Element::create(boost::lexical_cast<int64_t>(value)));
break;
case Element::boolean:
// We only allow "1" and "0" as input to this function.
if ((value != "0") && (value != "1")) {
isc_throw(BadValue, "StampedValue: invalid value " << value
<< " specified as boolean. Expected \"0\" or \"1\"");
}
stamped_value = StampedValue::create(name,
Element::create((value == "0") ? false : true));
break;
case Element::real:
stamped_value = StampedValue::create(name,
Element::create(boost::lexical_cast<double>(value)));
break;
default:
// Invalid data type provided as argument.
isc_throw(TypeError, "StampedValue: unsupported type '"
<< Element::typeToName(parameter_type)
<< " of the parameter '" << name);
}
} catch (const boost::bad_lexical_cast& ex) {
// Failed to cast the value to a given type.
isc_throw(BadValue, "StampedValue: the value of the parameter '"
<< Element::typeToName(parameter_type)
<< "' can't be converted to "
<< Element::typeToName(parameter_type)
<< " type");
}
return (stamped_value);
}
int
StampedValue::getType() const {
if (!value_) {
isc_throw(InvalidOperation, "StampedValue: attempt to retrieve the "
"type of the null value for the '" << name_
<< "' parameter");
}
return (value_->getType());
}
std::string
StampedValue::getValue() const {
validateAccess(Element::string);
try {
switch (static_cast<Element::types>(value_->getType())) {
case Element::string:
return (value_->stringValue());
case Element::integer:
return (boost::lexical_cast<std::string>(value_->intValue()));
case Element::boolean:
return (value_->boolValue() ? "1" : "0");
case Element::real:
{
std::string repr =
boost::lexical_cast<std::string>(value_->doubleValue());
if (repr.find_first_of('.') == std::string::npos) {
repr += ".0";
}
return (repr);
}
default:
// Impossible condition.
isc_throw(TypeError, "StampedValue: invalid type of the '"
<< name_ << "' parameter");
}
} catch (const boost::bad_lexical_cast& ex) {
isc_throw(BadValue, "StampedValue: unable to convert the value of "
"the parameter '" << name_ << "' to string");
}
// unreachable
return (value_->stringValue());
}
int64_t
StampedValue::getIntegerValue() const {
validateAccess(Element::integer);
return (value_->intValue());
}
bool
StampedValue::getBoolValue() const {
validateAccess(Element::boolean);
return (value_->boolValue());
}
double
StampedValue::getDoubleValue() const {
validateAccess(Element::real);
return (value_->doubleValue());
}
void
StampedValue::validateConstruct() const {
if (!value_) {
isc_throw(BadValue, "StampedValue: provided value of the '"
<< name_ << "' parameter is NULL");
}
auto type = value_->getType();
if ((type != Element::string) &&
(type != Element::integer) &&
(type != Element::boolean) &&
(type != Element::real) &&
(type != Element::map)) {
isc_throw(TypeError, "StampedValue: provided value of the '"
<< name_ << "' parameter has invalid type: "
<< Element::typeToName(type));
}
if (type == Element::map) {
size_t count = value_->mapValue().size();
if (count > 1) {
isc_throw(BadValue, "StampedValue: provided value of the '"
<< name_ << "' parameter has more than one element in the map");
}
if (count == 1) {
type = value_->mapValue().begin()->second->getType();
if ((type != Element::string) &&
(type != Element::integer) &&
(type != Element::boolean) &&
(type != Element::real)) {
isc_throw(BadValue, "StampedValue: provided value of the '"
<< name_ << "." << value_->mapValue().begin()->first
<< "' parameter has invalid type: "
<< Element::typeToName(type));
}
}
}
}
void
StampedValue::validateAccess(Element::types type) const {
if (!value_) {
isc_throw(InvalidOperation, "StampedValue: attempt to get null value "
"of the '" << name_ << "' parameter");
}
if ((type != Element::string) && (type != value_->getType())) {
isc_throw(TypeError, "StampedValue: attempt to access a '"
<< name_ << "' parameter as " << Element::typeToName(type)
<< ", but this parameter has "
<< Element::typeToName(value_->getType())
<< " type");
}
}
} // end of namespace isc::data
} // end of namespace isc
|