1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (C) 2013-2016 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 LABELED_VALUE_H
#define LABELED_VALUE_H

#include <exceptions/exceptions.h>

#include <boost/shared_ptr.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <ostream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <map><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

/// @file labeled_value.h This file defines classes: LabeledValue and
/// LabeledValueSet.

namespace isc {
namespace util {

/// @brief Thrown if an error is encountered handling a LabeledValue.
class LabeledValueError : public isc::Exception {
public:
    LabeledValueError(const char* file, size_t line, const char* what) :
        isc::Exception(file, line, what) { };
};

/// @brief Implements the concept of a constant value with a text label.
///
/// This class implements an association between a constant integer value
/// and a text label. It provides a single constructor, accessors for both
/// the value and label, and boolean operators which treat the value as
/// the "key" for comparisons.  This allows them to be assembled into
/// dictionaries of unique values.  Note, that the labels are not required to
/// be unique but in practice it makes little sense to for them to be
/// otherwise.
class LabeledValue {
public:

    /// @brief Constructor
    ///
    /// @param value the numeric constant value to be labeled.
    /// @param label the text label to associate to this value.
    ///
    /// @throw LabeledValueError if label is empty.
    LabeledValue(const int value, const std::string& label);

    /// @brief Destructor.
    ///
    /// Destructor is virtual to permit derivations.
    virtual ~LabeledValue();

    /// @brief Gets the integer value of this instance.
    ///
    /// @return integer value of this instance.
    int getValue() const;

    /// @brief Gets the text label of this instance.
    ///
    /// @return The text label as string
    std::string getLabel() const;

    /// @brief  Equality operator
    ///
    /// @return True if a.value_ is equal to b.value_.
    bool operator==(const LabeledValue& other) const;

    /// @brief  Inequality operator
    ///
    /// @return True if a.value_ is not equal to b.value_.
    bool operator!=(const LabeledValue& other) const;

    /// @brief  Less-than operator
    ///
    /// @return True if a.value_ is less than b.value_.
    bool operator<(const LabeledValue& other) const;

private:
    /// @brief The numeric value to label.
    int value_;

    /// @brief The text label for the value.
    std::string label_;
};

/// @brief Dumps the label to ostream.
std::ostream& operator<<(std::ostream& os, const LabeledValue& vlp);

/// @brief Defines a shared pointer to a LabeledValue instance.
typedef boost::shared_ptr<LabeledValue> LabeledValuePtr;

/// @brief Defines a map of pointers to LabeledValues keyed by value.
typedef std::map<unsigned int, LabeledValuePtr> LabeledValueMap;


/// @brief Implements a set of unique LabeledValues.
///
/// This class is intended to function as a dictionary of numeric values
/// and the labels associated with them.  It is essentially a thin wrapper
/// around a std::map of LabeledValues, keyed by their values.  This is handy
/// for defining a set of "valid" constants while conveniently associating a
/// text label with each value.
///
/// This class offers two variants of an add method for adding entries to the
/// set, and accessors for finding an entry or an entry's label by value.
/// Note that the add methods may throw but all accessors are exception safe.
/// It is up to the caller to determine when and if an undefined value is
/// exception-worthy.
///
/// More interestingly, a derivation of this class can be used to "define"
/// valid instances of derivations of LabeledValue.
class LabeledValueSet {
public:
    /// @brief Defines a text label returned by when value is not found.
    static const char* UNDEFINED_LABEL;

    /// @brief Constructor
    ///
    /// Constructs an empty set.
    LabeledValueSet();

    /// @brief Destructor
    ///
    /// Destructor is virtual to permit derivations.
    virtual ~LabeledValueSet();

    /// @brief Adds the given entry to the set
    ///
    /// @param entry is the entry to add.
    ///
    /// @throw LabeledValuePtr if the entry is null or the set already
    /// contains an entry with the same value.
    void add(LabeledValuePtr entry);

    /// @brief Adds an entry to the set for the given value and label
    ///
    /// @param value the numeric constant value to be labeled.
    /// @param label the text label to associate to this value.
    ///
    /// @throw LabeledValuePtr if the label is empty, or if the set
    /// already contains an entry with the same value.
    void add(const int value, const std::string& label);

    /// @brief Fetches a pointer to the entry associated with value
    ///
    /// @param value is the value of the entry desired.
    ///
    /// @return A pointer to the entry if the entry was found otherwise the
    /// pointer is empty.
    const LabeledValuePtr& get(int value);

    /// @brief Tests if the set contains an entry for the given value.
    ///
    /// @param value is the value of the entry to test.
    ///
    /// @return True if an entry for value exists in the set, false if not.
    bool isDefined(const int value) const;

    /// @brief Fetches the label for the given value
    ///
    /// @param value is the value for which the label is desired.
    ///
    /// @return the label of the value if defined, otherwise it returns
    /// UNDEFINED_LABEL.
    std::string getLabel(const int value) const;

private:
    /// @brief The map of labeled values.
    LabeledValueMap map_;
};

} // namespace isc::util
} // namespace isc
#endif