From: Marcin Siodelski Date: Wed, 3 Oct 2018 12:36:52 +0000 (+0200) Subject: [#93,!56] Implemented StampedValue. X-Git-Tag: 5-netconf-extend-syntax_base~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7c1a92b031d0200d0677ebf64ae898c58f64c7dc;p=thirdparty%2Fkea.git [#93,!56] Implemented StampedValue. --- diff --git a/src/lib/cc/Makefile.am b/src/lib/cc/Makefile.am index f191e6c819..be2c979c5d 100644 --- a/src/lib/cc/Makefile.am +++ b/src/lib/cc/Makefile.am @@ -11,6 +11,7 @@ libkea_cc_la_SOURCES += command_interpreter.cc command_interpreter.h libkea_cc_la_SOURCES += json_feed.cc json_feed.h libkea_cc_la_SOURCES += simple_parser.cc simple_parser.h libkea_cc_la_SOURCES += stamped_element.cc stamped_element.h +libkea_cc_la_SOURCES += stamped_value.cc stamped_value.h libkea_cc_la_SOURCES += user_context.cc user_context.h libkea_cc_la_LIBADD = $(top_builddir)/src/lib/util/libkea-util.la diff --git a/src/lib/cc/stamped_value.cc b/src/lib/cc/stamped_value.cc new file mode 100644 index 0000000000..a5204c2f46 --- /dev/null +++ b/src/lib/cc/stamped_value.cc @@ -0,0 +1,43 @@ +// Copyright (C) 2018 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 +#include +#include + +namespace isc { +namespace data { + +StampedValue::StampedValue(const std::string& value) + : StampedElement(), value_(value) { +} + +StampedValue::StampedValue(const int64_t value) + : StampedElement(), value_() { + try { + value_ = boost::lexical_cast(value); + } catch (...) { + isc_throw(BadValue, "unable to cast value " << value + << " to a string"); + } +} + +int64_t +StampedValue::getSignedIntegerValue() const { + if (!value_.empty()) { + try { + return (boost::lexical_cast(value_)); + } catch (...) { + isc_throw(BadValue, "unable to cast value " << value_ + << " to a signed integer"); + } + } + + return (0); +} + +} // end of namespace isc::data +} // end of namespace isc diff --git a/src/lib/cc/stamped_value.h b/src/lib/cc/stamped_value.h new file mode 100644 index 0000000000..c6168a6ed2 --- /dev/null +++ b/src/lib/cc/stamped_value.h @@ -0,0 +1,68 @@ +// Copyright (C) 2018 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/. + +#ifndef STAMPED_VALUE_H +#define STAMPED_VALUE_H + +#include +#include +#include +#include + +namespace isc { +namespace data { + +/// @brief This class represents string or signed integer configuration +/// element associated with the modification timestamp. +/// +/// Global configuration elements having simple types, e.g. DHCP +/// timers, need to be associatied with modification timestamps. +/// This association is made by deriving from @c StampedElement. +/// Values can be both integers and strings. Because strings are +/// more flexible, configuration elements are always held as strings +/// in the configuration backends. This class reflects a single value +/// held in the database. The value can be converted to an integer or +/// can be returned as a string. +class StampedValue : public StampedElement { +public: + + /// @brief Constructor. + /// + /// Creates stamped value from a string. + /// + /// @param value Value to be set. + explicit StampedValue(const std::string& value); + + /// @brief Constructor. + /// + /// Creates stamped value from the signed integer. + /// + /// @param value Value to be set. + explicit StampedValue(const int64_t value); + + /// @brief Returns value as string. + std::string getValue() const { + return (value_); + } + + /// @brief Returns value as signed integer. + /// + /// @throw BadValue if the value can't be converted to an integer. + int64_t getSignedIntegerValue() const; + +private: + + /// @brief Holds value as a string. + std::string value_; +}; + +/// @brief Pointer to the stamped value. +typedef boost::shared_ptr StampedValuePtr; + +} // end of namespace isc::data +} // end of namespace isc + +#endif diff --git a/src/lib/cc/tests/Makefile.am b/src/lib/cc/tests/Makefile.am index e33c049b74..74da3b067a 100644 --- a/src/lib/cc/tests/Makefile.am +++ b/src/lib/cc/tests/Makefile.am @@ -20,6 +20,7 @@ run_unittests_SOURCES += data_file_unittests.cc run_unittests_SOURCES += json_feed_unittests.cc run_unittests_SOURCES += simple_parser_unittest.cc run_unittests_SOURCES += stamped_element_unittest.cc +run_unittests_SOURCES += stamped_value_unittest.cc run_unittests_SOURCES += user_context_unittests.cc run_unittests_SOURCES += run_unittests.cc run_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES) diff --git a/src/lib/cc/tests/stamped_value_unittest.cc b/src/lib/cc/tests/stamped_value_unittest.cc new file mode 100644 index 0000000000..6d5651a994 --- /dev/null +++ b/src/lib/cc/tests/stamped_value_unittest.cc @@ -0,0 +1,37 @@ +// Copyright (C) 2018 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 + +#include +#include +#include +#include + +using namespace isc; +using namespace isc::data; + +namespace { + +// Tests that stamped value from string can be created. +TEST(StampedValueTest, createFromString) { + boost::scoped_ptr value; + ASSERT_NO_THROW(value.reset(new StampedValue("foo"))); + EXPECT_EQ("foo", value->getValue()); + EXPECT_THROW(value->getSignedIntegerValue(), BadValue); +} + +// Tests that stamped value from integer can be created. +TEST(StampedValueTest, createFromInteger) { + boost::scoped_ptr value; + ASSERT_NO_THROW(value.reset(new StampedValue(5))); + EXPECT_EQ("5", value->getValue()); + int64_t signed_integer; + ASSERT_NO_THROW(signed_integer = value->getSignedIntegerValue()); + EXPECT_EQ(5, signed_integer); +} + +}