]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#93,!56] Implemented StampedValue.
authorMarcin Siodelski <marcin@isc.org>
Wed, 3 Oct 2018 12:36:52 +0000 (14:36 +0200)
committerMarcin Siodelski <marcin@isc.org>
Mon, 8 Oct 2018 20:01:27 +0000 (22:01 +0200)
src/lib/cc/Makefile.am
src/lib/cc/stamped_value.cc [new file with mode: 0644]
src/lib/cc/stamped_value.h [new file with mode: 0644]
src/lib/cc/tests/Makefile.am
src/lib/cc/tests/stamped_value_unittest.cc [new file with mode: 0644]

index f191e6c819e0a1691e31257c053b5a335a8da94e..be2c979c5dd99d1149907194bf5254e3259fdf67 100644 (file)
@@ -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 (file)
index 0000000..a5204c2
--- /dev/null
@@ -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 <cc/stamped_value.h>
+#include <exceptions/exceptions.h>
+#include <boost/lexical_cast.hpp>
+
+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<std::string>(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<int64_t>(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 (file)
index 0000000..c6168a6
--- /dev/null
@@ -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 <cc/stamped_element.h>
+#include <boost/shared_ptr.hpp>
+#include <cstdint>
+#include <string>
+
+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<StampedValue> StampedValuePtr;
+
+} // end of namespace isc::data
+} // end of namespace isc
+
+#endif
index e33c049b74dd5f9fe23b5ae9b3bc1cca010dfef2..74da3b067a9caff871cd86da8aaaf40c9eab1056 100644 (file)
@@ -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 (file)
index 0000000..6d5651a
--- /dev/null
@@ -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 <config.h>
+
+#include <cc/stamped_value.h>
+#include <exceptions/exceptions.h>
+#include <boost/scoped_ptr.hpp>
+#include <gtest/gtest.h>
+
+using namespace isc;
+using namespace isc::data;
+
+namespace {
+
+// Tests that stamped value from string can be created.
+TEST(StampedValueTest, createFromString) {
+    boost::scoped_ptr<StampedValue> 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<StampedValue> 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);
+}
+
+}