stamped_value.h 4.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// 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>
11 12 13 14
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index_container.hpp>
15 16 17 18 19 20 21
#include <boost/shared_ptr.hpp>
#include <cstdint>
#include <string>

namespace isc {
namespace data {

22 23 24 25 26
class StampedValue;

/// @brief Pointer to the stamped value.
typedef boost::shared_ptr<StampedValue> StampedValuePtr;

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/// @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.
    ///
45
    /// @param name Name of the value.
46
    /// @param value Value to be set.
47
    StampedValue(const std::string& name, const std::string& value);
48 49 50 51 52

    /// @brief Constructor.
    ///
    /// Creates stamped value from the signed integer.
    ///
53
    /// @param name Name of the value.
54
    /// @param value Value to be set.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    explicit StampedValue(const std::string& name, const int64_t value);

    /// @brief Convenience function creating shared pointer to the object.
    ///
    /// @param name Name of the value.
    /// @param value String value to be encapsulated by this object.
    static StampedValuePtr create(const std::string& name,
                                  const std::string& value);

    /// @brief Convenience function creating shared Pointer to the object.
    ///
    /// @param name Name of the value.
    /// @param value Integer value to be encapsulated by this object.
    static StampedValuePtr create(const std::string& name,
                                  const int64_t value);
70

71 72 73 74 75
    /// @brief Returns value name.
    std::string getName() const {
        return (name_);
    }

76
    /// @brief Returns value as string.
77 78 79 80 81 82 83 84 85 86 87
    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:

88 89 90
    /// @brief Name of the value.
    std::string name_;

91 92 93 94
    /// @brief Holds value as a string.
    std::string value_;
};

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
/// @name Definition of the multi index container for @c StampedValue.
///
//@{

/// @brief Tag for the index for access by value name.
struct StampedValueNameIndexTag { };

/// @brief Tag for the index for access by modification time.
struct StampedValueModificationTimeIndexTag { };

/// @brief Multi index container for @c StampedValue.
typedef boost::multi_index_container<
    StampedValuePtr,
    boost::multi_index::indexed_by<
        // Index used to access value by name.
        boost::multi_index::hashed_non_unique<
            boost::multi_index::tag<StampedValueNameIndexTag>,
            boost::multi_index::const_mem_fun<
                StampedValue,
                std::string,
                &StampedValue::getName
            >
        >,

        // Index used to access value by modification time.
        boost::multi_index::ordered_non_unique<
            boost::multi_index::tag<StampedValueModificationTimeIndexTag>,
            boost::multi_index::const_mem_fun<
                StampedElement,
                boost::posix_time::ptime,
                &StampedElement::getModificationTime
            >
        >
    >
> StampedValueCollection;

//@}
132 133 134 135 136

} // end of namespace isc::data
} // end of namespace isc

#endif