Commit 7c1a92b0 authored by Marcin Siodelski's avatar Marcin Siodelski

[#93,!56] Implemented StampedValue.

parent 9708ea04
......@@ -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
......
// 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
// 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
......@@ -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)
......
// 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);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment