| Kea 3.0.2-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
40 using util::Optional<T>::get;
41
48 Triplet<T>& operator=(T other) {
49 min_ = other;
50 util::Optional<T>::default_ = other;
51 max_ = other;
52 // The value is now specified because we just assigned one.
53 util::Optional<T>::unspecified_ = false;
54 return (*this);
55 }
56
60 Triplet()
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
isc::BadValue
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
Definition exceptions/exceptions.h:132
isc::util::Optional
A template representing an optional value.
Definition optional.h:36
isc::util::Optional::unspecified_
bool unspecified_
Flag which indicates if the value is specified.
Definition optional.h:159
isc::util::Optional::Optional
Optional()
Default constructor.
Definition optional.h:94
isc::util::Optional::default_
T default_
Encapsulated value.
Definition optional.h:158
isc::util::Triplet::Triplet
Triplet()
Constructor without parameters.
Definition triplet.h:60
isc::util::Triplet::Triplet
Triplet(T min, T def, T max)
Sets the default value and thresholds.
Definition triplet.h:77
isc::util::Triplet::get
T get(T hint) const
Returns value with a hint.
Definition triplet.h:99
isc::util::Triplet::operator=
Triplet< T > & operator=(T other)
Base type to Triplet conversion.
Definition triplet.h:48
isc::util::Triplet::getMax
T getMax() const
Returns a maximum allowed value.
Definition triplet.h:112
isc::util::Triplet::getMin
T getMin() const
Returns a minimum allowed value.
Definition triplet.h:85
isc::util::Triplet::Triplet
Triplet(T value)
Sets a fixed value.
Definition triplet.h:70
exceptions.h
isc_throw
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Definition exceptions/exceptions.h:210
isc::util
Definition time_utils.cc:43
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148
optional.h