Kea 3.2.0-git
optional.h
Go to the documentation of this file.
1// Copyright (C) 2014-2026 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 OPTIONAL_H
8#define OPTIONAL_H
9
11#include <ostream>
12#include <string>
13#include <type_traits>
14
15namespace isc {
16namespace util {
17
36template<typename T>
37class Optional {
38public:
39
41 typedef T ValueType;
42
48 template<typename A>
50 default_ = other;
51 unspecified_ = false;
52 return (*this);
53 }
54
61 operator T() const {
62 return (default_);
63 }
64
68 bool operator==(const T& other) const {
69 return (default_ == other);
70 }
71
75 bool operator!=(const T& other) const {
76 return (default_ != other);
77 }
78
99 // cppcheck-suppress nullPointer
101 : default_(T(0)), unspecified_(true) {
102 }
103
112 template<typename A>
113 Optional(A value, const bool unspecified = false)
115 }
116
122 // cppcheck-suppress returnByReference
123 T get() const {
124 return (default_);
125 }
126
133 T valueOr(T const& or_value) const {
134 if (unspecified_) {
135 return or_value;
136 }
137 return default_;
138 }
139
148
152 bool unspecified() const {
153 return (unspecified_);
154 }
155
162 bool empty() const {
163 isc_throw(isc::InvalidOperation, "call to empty() not supported");
164 }
165
166protected:
169
170private:
171 // Refuse instantiation by a reference type.
172 static_assert(!std::is_reference<T>::value, "!std::is_reference<T>::value");
173};
174
179template<>
183
187template<>
188inline bool Optional<std::string>::empty() const {
189 return (default_.empty());
190}
191
203template<typename T>
204std::ostream&
205operator<<(std::ostream& os, const Optional<T>& optional_value) {
206 os << optional_value.get();
207 return (os);
208}
209
210
211} // end of namespace isc::util
212} // end of namespace isc
213
214#endif // OPTIONAL_VALUE_H
A generic exception that is thrown if a function is called in a prohibited way.
A template representing an optional value.
Definition optional.h:37
T get() const
Retrieves the encapsulated value.
Definition optional.h:123
T valueOr(T const &or_value) const
Retrieves the encapsulated value if specified, or the given value otherwise.
Definition optional.h:133
bool operator==(const T &other) const
Equality operator.
Definition optional.h:68
bool empty() const
Checks if the encapsulated value is empty.
Definition optional.h:162
Optional()
Default constructor.
Definition optional.h:100
bool operator!=(const T &other) const
Inequality operator.
Definition optional.h:75
Optional< T > & operator=(A other)
Assigns a new value value and marks it "specified".
Definition optional.h:49
bool unspecified() const
Checks if the value has been specified or unspecified.
Definition optional.h:152
Optional(A value, const bool unspecified=false)
Constructor.
Definition optional.h:113
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::ostream & operator<<(std::ostream &os, const CSVRow &row)
Overrides standard output stream operator for CSVRow object.
Definition csv_file.cc:100
Defines the logger used by the top-level component of kea-lfc.