Kea  2.1.7-git
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 
9 #include <util/labeled_value.h>
10 
11 namespace isc {
12 namespace util {
13 
14 /**************************** LabeledValue ****************************/
15 
16 LabeledValue::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 
26 int
28  return (value_);
29 }
30 
31 std::string
33  return (label_);
34 }
35 
36 bool
38  return (this->value_ == other.value_);
39 }
40 
41 bool
43  return (this->value_ != other.value_);
44 }
45 
46 bool
48  return (this->value_ < other.value_);
49 }
50 
51 std::ostream& operator<<(std::ostream& os, const LabeledValue& vlp) {
52  os << vlp.getLabel();
53  return (os);
54 }
55 
56 /**************************** LabeledValueSet ****************************/
57 
58 const char* LabeledValueSet::UNDEFINED_LABEL = "UNDEFINED";
59 
61 }
62 
64 }
65 
66 void
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 
82 void
83 LabeledValueSet::add(const int value, const std::string& label) {
84  add(LabeledValuePtr(new LabeledValue(value,label)));
85 }
86 
87 const 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 
99 bool
100 LabeledValueSet::isDefined(const int value) const {
101  LabeledValueMap::const_iterator it = map_.find(value);
102  return (it != map_.end());
103 }
104 
105 std::string
106 LabeledValueSet::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
bool operator==(const LabeledValue &other) const
Equality operator.
This file defines classes: LabeledValue and LabeledValueSet.
bool operator!=(const LabeledValue &other) const
Inequality operator.
virtual ~LabeledValueSet()
Destructor.
LabeledValueSet()
Constructor.
std::ostream & operator<<(std::ostream &os, const CSVRow &row)
Overrides standard output stream operator for CSVRow object.
Definition: csv_file.cc:100
bool isDefined(const int value) const
Tests if the set contains an entry for the given value.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Thrown if an error is encountered handling a LabeledValue.
Definition: labeled_value.h:24
std::string getLabel(const int value) const
Fetches the label for the given value.
boost::shared_ptr< LabeledValue > LabeledValuePtr
Defines a shared pointer to a LabeledValue instance.
Definition: labeled_value.h:92
Implements the concept of a constant value with a text label.
Definition: labeled_value.h:39
const LabeledValuePtr & get(int value)
Fetches a pointer to the entry associated with value.
virtual ~LabeledValue()
Destructor.
Defines the logger used by the top-level component of kea-lfc.
std::string getLabel() const
Gets the text label of this instance.
bool operator<(const LabeledValue &other) const
Less-than operator.
LabeledValue(const int value, const std::string &label)
Constructor.
int getValue() const
Gets the integer value of this instance.
static const char * UNDEFINED_LABEL
Defines a text label returned by when value is not found.
void add(LabeledValuePtr entry)
Adds the given entry to the set.