Kea  1.9.9-git
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 
10 #include <exceptions/exceptions.h>
11 #include <ostream>
12 #include <string>
13 
14 namespace isc {
15 namespace util {
16 
35 template<typename T>
36 class Optional {
37 public:
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)
108  : default_(value), unspecified_(unspecified) {
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 
142 protected:
145 };
146 
151 template<>
153  : default_(), unspecified_(true) {
154 }
155 
159 template<>
160 inline bool Optional<std::string>::empty() const {
161  return (default_.empty());
162 }
163 
175 template<typename T>
176 std::ostream&
177 operator<<(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
void unspecified(bool unspecified)
Modifies the flag that indicates whether the value is specified or unspecified.
Definition: optional.h:121
T ValueType
Type of the encapsulated value.
Definition: optional.h:40
bool unspecified() const
Checks if the value has been specified or unspecified.
Definition: optional.h:128
T default_
Encapsulated value.
Definition: optional.h:143
bool operator!=(const T &other) const
Inequality operator.
Definition: optional.h:74
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Optional()
Default constructor.
Definition: optional.h:94
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
Defines the logger used by the top-level component of kea-dhcp-ddns.
Optional< T > & operator=(A other)
Assigns a new value value and marks it "specified".
Definition: optional.h:48
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
Optional(A value, const bool unspecified=false)
Constructor.
Definition: optional.h:107