Kea 2.6.0
labeled_value.cc
Go to the documentation of this file.
1// Copyright (C) 2013-2020 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
8
10
11namespace isc {
12namespace util {
13
14/**************************** LabeledValue ****************************/
15
16LabeledValue::LabeledValue(const int value, const std::string& label)
17 : value_(value), label_(label) {
18 if (label.empty()) {
19 isc_throw(LabeledValueError, "labels cannot be empty");
20 }
21}
22
24}
25
26int
28 return (value_);
29}
30
31std::string
33 return (label_);
34}
35
36bool
38 return (this->value_ == other.value_);
39}
40
41bool
43 return (this->value_ != other.value_);
44}
45
46bool
47LabeledValue::operator<(const LabeledValue& other) const {
48 return (this->value_ < other.value_);
49}
50
51std::ostream& operator<<(std::ostream& os, const LabeledValue& vlp) {
52 os << vlp.getLabel();
53 return (os);
54}
55
56/**************************** LabeledValueSet ****************************/
57
58const char* LabeledValueSet::UNDEFINED_LABEL = "UNDEFINED";
59
61}
62
64}
65
66void
68 if (!entry) {
69 isc_throw(LabeledValueError, "cannot add an null entry to set");
70 }
71
72 const int value = entry->getValue();
73 if (isDefined(value)) {
75 "value: " << value << " is already defined as: "
76 << getLabel(value));
77 }
78
79 map_[value] = entry;
80}
81
82void
83LabeledValueSet::add(const int value, const std::string& label) {
84 add(LabeledValuePtr(new LabeledValue(value,label)));
85}
86
87const LabeledValuePtr&
89 static LabeledValuePtr undefined;
90 LabeledValueMap::iterator it = map_.find(value);
91 if (it != map_.end()) {
92 return ((*it).second);
93 }
94
95 // Return an empty pointer when not found.
96 return (undefined);
97}
98
99bool
100LabeledValueSet::isDefined(const int value) const {
101 LabeledValueMap::const_iterator it = map_.find(value);
102 return (it != map_.end());
103}
104
105std::string
106LabeledValueSet::getLabel(const int value) const {
107 LabeledValueMap::const_iterator it = map_.find(value);
108 if (it != map_.end()) {
109 const LabeledValuePtr& ptr = (*it).second;
110 return (ptr->getLabel());
111 }
112
113 return (std::string(UNDEFINED_LABEL));
114}
115
116} // namespace isc::util
117} // namespace isc
Thrown if an error is encountered handling a LabeledValue.
Definition: labeled_value.h:24
static const char * UNDEFINED_LABEL
Defines a text label returned by when value is not found.
bool isDefined(const int value) const
Tests if the set contains an entry for the given value.
const LabeledValuePtr & get(int value)
Fetches a pointer to the entry associated with value.
virtual ~LabeledValueSet()
Destructor.
std::string getLabel(const int value) const
Fetches the label for the given value.
void add(LabeledValuePtr entry)
Adds the given entry to the set.
Implements the concept of a constant value with a text label.
Definition: labeled_value.h:39
int getValue() const
Gets the integer value of this instance.
bool operator!=(const LabeledValue &other) const
Inequality operator.
LabeledValue(const int value, const std::string &label)
Constructor.
bool operator==(const LabeledValue &other) const
Equality operator.
bool operator<(const LabeledValue &other) const
Less-than operator.
std::string getLabel() const
Gets the text label of this instance.
virtual ~LabeledValue()
Destructor.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
This file defines classes: LabeledValue and LabeledValueSet.
std::ostream & operator<<(std::ostream &os, const CSVRow &row)
Overrides standard output stream operator for CSVRow object.
Definition: csv_file.cc:100
boost::shared_ptr< LabeledValue > LabeledValuePtr
Defines a shared pointer to a LabeledValue instance.
Definition: labeled_value.h:92
Defines the logger used by the top-level component of kea-lfc.