Kea: stamped_value.cc Source File

| Kea 2.2.0

stamped_value.cc
Go to the documentation of this file.
1// Copyright (C) 2018-2020 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
9#include <cc/stamped_value.h>
10#include <exceptions/exceptions.h>
11#include <boost/lexical_cast.hpp>
12
13namespace isc {
14namespace data {
15
16StampedValue::StampedValue(const std::string& name)
17 : StampedElement(), name_(name), value_() {
18}
19
20 StampedValue::StampedValue(const std::string& name, const ElementPtr& value)
21 : StampedElement(), name_(name), value_(value) {
22 validateConstruct();
23}
24
25 StampedValue::StampedValue(const std::string& name, const std::string& value)
26 : StampedElement(), name_(name), value_(Element::create(value)) {
27 validateConstruct();
28}
29
30 StampedValuePtr
31StampedValue::create(const std::string& name) {
32 return (StampedValuePtr(new StampedValue(name)));
33}
34
35 StampedValuePtr
36 StampedValue::create(const std::string& name, const ElementPtr& value) {
37 return (StampedValuePtr(new StampedValue(name, value)));
38}
39
40 StampedValuePtr
41 StampedValue::create(const std::string& name, const std::string& value) {
42 return (StampedValuePtr(new StampedValue(name, value)));
43}
44
45 StampedValuePtr
46 StampedValue::create(const std::string& name, const std::string& value,
47 Element::types parameter_type) {
48 StampedValuePtr stamped_value;
49
50 try {
51 switch (parameter_type) {
52 case Element::string:
53 stamped_value = StampedValue::create(name, value);
54 break;
55
56 case Element::integer:
57 stamped_value = StampedValue::create(name,
58 Element::create(boost::lexical_cast<int64_t>(value)));
59 break;
60
61 case Element::boolean:
62 // We only allow "1" and "0" as input to this function.
63 if ((value != "0") && (value != "1")) {
64 isc_throw(BadValue, "StampedValue: invalid value " << value
65 << " specified as boolean. Expected \"0\" or \"1\"");
66 }
67 stamped_value = StampedValue::create(name,
68 Element::create((value == "0") ? false : true));
69 break;
70
71 case Element::real:
72 stamped_value = StampedValue::create(name,
73 Element::create(boost::lexical_cast<double>(value)));
74 break;
75
76 default:
77 // Invalid data type provided as argument.
78 isc_throw(TypeError, "StampedValue: unsupported type '"
79 << Element::typeToName(parameter_type)
80 << " of the parameter '" << name);
81 }
82
83 } catch (const boost::bad_lexical_cast& ex) {
84 // Failed to cast the value to a given type.
85 isc_throw(BadValue, "StampedValue: the value of the parameter '"
86 << Element::typeToName(parameter_type)
87 << "' can't be converted to "
88 << Element::typeToName(parameter_type)
89 << " type");
90 }
91
92 return (stamped_value);
93}
94
95int
96StampedValue::getType() const {
97 if (!value_) {
98 isc_throw(InvalidOperation, "StampedValue: attempt to retrieve the "
99 "type of the null value for the '" << name_
100 << "' parameter");
101 }
102
103 return (value_->getType());
104}
105
106std::string
107StampedValue::getValue() const {
108 validateAccess(Element::string);
109
110 try {
111 switch (static_cast<Element::types>(value_->getType())) {
112 case Element::string:
113 return (value_->stringValue());
114 case Element::integer:
115 return (boost::lexical_cast<std::string>(value_->intValue()));
116 case Element::boolean:
117 return (value_->boolValue() ? "1" : "0");
118 case Element::real:
119 {
120 std::string repr =
121 boost::lexical_cast<std::string>(value_->doubleValue());
122 if (repr.find_first_of('.') == std::string::npos) {
123 repr += ".0";
124 }
125 return (repr);
126 }
127 default:
128 // Impossible condition.
129 isc_throw(TypeError, "StampedValue: invalid type of the '"
130 << name_ << "' parameter");
131 }
132
133 } catch (const boost::bad_lexical_cast& ex) {
134 isc_throw(BadValue, "StampedValue: unable to convert the value of "
135 "the parameter '" << name_ << "' to string");
136 }
137 // unreachable
138 return (value_->stringValue());
139}
140
141int64_t
142StampedValue::getIntegerValue() const {
143 validateAccess(Element::integer);
144 return (value_->intValue());
145}
146
147bool
148StampedValue::getBoolValue() const {
149 validateAccess(Element::boolean);
150 return (value_->boolValue());
151}
152
153double
154StampedValue::getDoubleValue() const {
155 validateAccess(Element::real);
156 return (value_->doubleValue());
157}
158
159void
160StampedValue::validateConstruct() const {
161 if (!value_) {
162 isc_throw(BadValue, "StampedValue: provided value of the '"
163 << name_ << "' parameter is NULL");
164 }
165
166 if ((value_->getType() != Element::string) &&
167 (value_->getType() != Element::integer) &&
168 (value_->getType() != Element::boolean) &&
169 (value_->getType() != Element::real)) {
170 isc_throw(TypeError, "StampedValue: provided value of the '"
171 << name_ << "' parameter has invalid type: "
172 << Element::typeToName(static_cast<Element::types>(value_->getType())));
173 }
174}
175
176void
177StampedValue::validateAccess(Element::types type) const {
178 if (!value_) {
179 isc_throw(InvalidOperation, "StampedValue: attempt to get null value "
180 "of the '" << name_ << "' parameter");
181 }
182
183 if ((type != Element::string) && (type != value_->getType())) {
184 isc_throw(TypeError, "StampedValue: attempt to access a '"
185 << name_ << "' parameter as " << Element::typeToName(type)
186 << ", but this parameter has "
187 << Element::typeToName(static_cast<Element::types>(value_->getType()))
188 << " type");
189 }
190}
191
192} // end of namespace isc::data
193} // end of namespace isc
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition: exceptions/exceptions.h:132
isc::InvalidOperation
A generic exception that is thrown if a function is called in a prohibited way.
Definition: exceptions/exceptions.h:143
isc::data::Element
The Element class represents a piece of data, used by the command channel and configuration parts.
Definition: data.h:70
isc::data::Element::create
static ElementPtr create(const Position &pos=ZERO_POSITION())
Definition: data.cc:241
isc::data::Element::types
types
Definition: data.h:155
isc::data::Element::real
@ real
Definition: data.h:155
isc::data::Element::integer
@ integer
Definition: data.h:155
isc::data::Element::boolean
@ boolean
Definition: data.h:155
isc::data::Element::string
@ string
Definition: data.h:155
isc::data::Element::typeToName
static std::string typeToName(Element::types type)
Returns the name of the given type as a string.
Definition: data.cc:621
isc::data::StampedElement
This class represents configuration element which is associated with database identifier,...
Definition: stamped_element.h:34
isc::data::StampedValue::getDoubleValue
double getDoubleValue() const
Returns value as a real number.
Definition: stamped_value.cc:154
isc::data::StampedValue::getBoolValue
bool getBoolValue() const
Returns value as a boolean.
Definition: stamped_value.cc:148
isc::data::StampedValue::StampedValue
StampedValue(const std::string &name)
Constructor creating a null value.
Definition: stamped_value.cc:16
isc::data::StampedValue::getValue
std::string getValue() const
Returns value as string.
Definition: stamped_value.cc:107
isc::data::StampedValue::getType
int getType() const
Returns a type of the value.
Definition: stamped_value.cc:96
isc::data::StampedValue::getIntegerValue
int64_t getIntegerValue() const
Returns value as signed integer.
Definition: stamped_value.cc:142
isc::data::StampedValue::create
static StampedValuePtr create(const std::string &name)
Factory function creating a null value.
Definition: stamped_value.cc:31
isc::data::TypeError
A standard Data module exception that is thrown if a function is called for an Element that has a wro...
Definition: data.h:34
name_
const Name & name_
Definition: dns/message.cc:693
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:210
isc::data::StampedValuePtr
boost::shared_ptr< StampedValue > StampedValuePtr
Pointer to the stamped value.
Definition: stamped_value.h:23
isc::data::ElementPtr
boost::shared_ptr< Element > ElementPtr
Definition: data.h:24
isc
Defines the logger used by the top-level component of kea-lfc.
Definition: agent_parser.cc:143
stamped_value.h