Kea 2.0.3
optional.h
Go to the documentation of this file.
1// Copyright (C) 2014-2019,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 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>
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
112 T get() const {
113 return (default_);
114 }
115
123 }
124
128 bool unspecified() const {
129 return (unspecified_);
130 }
131
138 bool empty() const {
139 isc_throw(isc::InvalidOperation, "call to empty() not supported");
140 }
141
142protected:
145};
146
151template<>
153 : default_(), unspecified_(true) {
154}
155
159template<>
160inline bool Optional<std::string>::empty() const {
161 return (default_.empty());
162}
163
175template<typename T>
176std::ostream&
177operator<<(std::ostream& os, const Optional<T>& optional_value) {
178 os << optional_value.get();
179 return (os);
180}
181
182
183} // end of namespace isc::util
184} // end of namespace isc
185
186#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:112
T ValueType
Type of the encapsulated value.
Definition: optional.h:40
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:144
bool empty() const
Checks if the encapsulated value is empty.
Definition: optional.h:138
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:143
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:128
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:121
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::ostream & operator<<(std::ostream &os, const Optional< T > &optional_value)
Inserts an optional value to a stream.
Definition: optional.h:177
Defines the logger used by the top-level component of kea-lfc.