Kea  1.5.0
labeled_value.cc
Go to the documentation of this file.
1 // Copyright (C) 2013-2016 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
47 LabeledValue::operator<(const LabeledValue& other) const {
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_[entry->getValue()]=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
isc::util::LabeledValueSet::~LabeledValueSet
virtual ~LabeledValueSet()
Destructor.
Definition: labeled_value.cc:63
isc::util::LabeledValueSet::UNDEFINED_LABEL
static const char * UNDEFINED_LABEL
Defines a text label returned by when value is not found.
Definition: labeled_value.h:117
isc::util::LabeledValueSet::isDefined
bool isDefined(const int value) const
Tests if the set contains an entry for the given value.
Definition: labeled_value.cc:100
isc::util::LabeledValue::operator!=
bool operator!=(const LabeledValue &other) const
Inequality operator.
Definition: labeled_value.cc:42
isc::util::LabeledValue::operator==
bool operator==(const LabeledValue &other) const
Equality operator.
Definition: labeled_value.cc:37
isc::util::LabeledValueSet::getLabel
std::string getLabel(const int value) const
Fetches the label for the given value.
Definition: labeled_value.cc:106
isc::util::LabeledValueSet::get
const LabeledValuePtr & get(int value)
Fetches a pointer to the entry associated with value.
Definition: labeled_value.cc:88
isc::util::LabeledValueSet::LabeledValueSet
LabeledValueSet()
Constructor.
Definition: labeled_value.cc:60
isc
Defines the logger used by the top-level component of kea-dhcp-ddns.
Definition: agent_parser.cc:144
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition: exceptions/exceptions.h:192
isc::util::LabeledValuePtr
boost::shared_ptr< LabeledValue > LabeledValuePtr
Defines a shared pointer to a LabeledValue instance.
Definition: labeled_value.h:92
isc::util::LabeledValue::getLabel
std::string getLabel() const
Gets the text label of this instance.
Definition: labeled_value.cc:32
isc::util::LabeledValue
Implements the concept of a constant value with a text label.
Definition: labeled_value.h:39
isc::util::LabeledValueError
Thrown if an error is encountered handling a LabeledValue.
Definition: labeled_value.h:24
isc::util::LabeledValue::LabeledValue
LabeledValue(const int value, const std::string &label)
Constructor.
Definition: labeled_value.cc:16
isc::util::operator<<
std::ostream & operator<<(std::ostream &os, const CSVRow &row)
Overrides standard output stream operator for CSVRow object.
Definition: csv_file.cc:69
labeled_value.h
isc::util::LabeledValue::operator<
bool operator<(const LabeledValue &other) const
Less-than operator.
Definition: labeled_value.cc:47
isc::util::LabeledValue::~LabeledValue
virtual ~LabeledValue()
Destructor.
Definition: labeled_value.cc:23
isc::util::LabeledValueSet::add
void add(LabeledValuePtr entry)
Adds the given entry to the set.
Definition: labeled_value.cc:67
isc::util::LabeledValue::getValue
int getValue() const
Gets the integer value of this instance.
Definition: labeled_value.cc:27