Kea 2.7.5
optional.h
Go to the documentation of this file.
1// Copyright (C) 2014-2022 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
14namespace isc {
15namespace util {
16
35template<typename T>
36class Optional {
37public:
38
40 typedef T ValueType;
41
47 template<typename A>
48 Optional<T>& operator=(A other) {
49 default_ = other;
50 unspecified_ = false;
51 return (*this);
52 }
53
60 operator T() const {
61 return (default_);
62 }
63
67 bool operator==(const T& other) const {
68 return (default_ == other);
69 }
70
74 bool operator!=(const T& other) const {
75 return (default_ != other);
76 }
77
95 : default_(T(0)), unspecified_(true) {
96 }
97
106 template<typename A>
107 Optional(A value, const bool unspecified = false)
109 }
110
114 T get() const {
115 return (default_);
116 }
117
124 T valueOr(T const& or_value) const {
125 if (unspecified_) {
126 return or_value;
127 }
128 return default_;
129 }
130
139
143 bool unspecified() const {
144 return (unspecified_);
145 }
146
153 bool empty() const {
154 isc_throw(isc::InvalidOperation, "call to empty() not supported");
155 }
156
157protected:
160};
161
166template<>
168 : default_(), unspecified_(true) {
169}
170
174template<>
175inline bool Optional<std::string>::empty() const {
176 return (default_.empty());
177}
178
190template<typename T>
191std::ostream&
192operator<<(std::ostream& os, const Optional<T>& optional_value) {
193 os << optional_value.get();
194 return (os);
195}
196
197
198} // end of namespace isc::util
199} // end of namespace isc
200
201#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:36
T get() const
Retrieves the encapsulated value.
Definition optional.h:114
T ValueType
Type of the encapsulated value.
Definition optional.h:40
T valueOr(T const &or_value) const
Retrieves the encapsulated value if specified, or the given value otherwise.
Definition optional.h:124
bool operator==(const T &other) const
Equality operator.
Definition optional.h:67
bool unspecified_
Flag which indicates if the value is specified.
Definition optional.h:159
bool empty() const
Checks if the encapsulated value is empty.
Definition optional.h:153
Optional()
Default constructor.
Definition optional.h:94
bool operator!=(const T &other) const
Inequality operator.
Definition optional.h:74
T default_
Encapsulated value.
Definition optional.h:158
Optional< T > & operator=(A other)
Assigns a new value value and marks it "specified".
Definition optional.h:48
bool unspecified() const
Checks if the value has been specified or unspecified.
Definition optional.h:143
Optional(A value, const bool unspecified=false)
Constructor.
Definition optional.h:107
void unspecified(bool unspecified)
Modifies the flag that indicates whether the value is specified or unspecified.
Definition optional.h:136
#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.