Kea  2.3.4-git
triplet.h
Go to the documentation of this file.
1 // Copyright (C) 2012-2021 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 #ifndef TRIPLET_H
8 #define TRIPLET_H
9 
10 #include <exceptions/exceptions.h>
11 #include <util/optional.h>
12 
13 namespace isc {
14 namespace util {
15 
36 template <class T>
37 class Triplet : public util::Optional<T> {
38 public:
39 
41 
48  Triplet<T>& operator=(T other) {
49  min_ = other;
51  max_ = other;
52  // The value is now specified because we just assigned one.
54  return (*this);
55  }
56 
61  : util::Optional<T>(), min_(0), max_(0) {
62  }
63 
70  Triplet(T value)
71  : util::Optional<T>(value), min_(value), max_(value) {
72  }
73 
77  Triplet(T min, T def, T max)
78  : util::Optional<T>(def), min_(min), max_(max) {
79  if ( (min_ > def) || (def > max_) ) {
80  isc_throw(BadValue, "Invalid triplet values.");
81  }
82  }
83 
85  T getMin() const { return (min_);}
86 
99  T get(T hint) const {
100  if (hint <= min_) {
101  return (min_);
102  }
103 
104  if (hint >= max_) {
105  return (max_);
106  }
107 
108  return (hint);
109  }
110 
112  T getMax() const { return (max_); }
113 
114 private:
115 
117  T min_;
118 
120  T max_;
121 };
122 
123 
124 } // namespace isc::dhcp
125 } // namespace isc
126 
127 #endif // TRIPLET_H
T getMin() const
Returns a minimum allowed value.
Definition: triplet.h:85
Triplet(T min, T def, T max)
Sets the default value and thresholds.
Definition: triplet.h:77
T getMax() const
Returns a maximum allowed value.
Definition: triplet.h:112
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Triplet(T value)
Sets a fixed value.
Definition: triplet.h:70
Triplet()
Constructor without parameters.
Definition: triplet.h:60
This template specifies a parameter value.
Definition: triplet.h:37
Defines the logger used by the top-level component of kea-lfc.
A template representing an optional value.
Definition: optional.h:36
Triplet< T > & operator=(T other)
Base type to Triplet conversion.
Definition: triplet.h:48