summaryrefslogtreecommitdiffstats
path: root/src/lib/util/optional.h
blob: 16d5dc022adde3dc6c6cdf78d4a3dbaf2db4ba3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (C) 2014-2022 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef OPTIONAL_H
#define OPTIONAL_H

#include <exceptions/exceptions.h>
#include <ostream>
#include <string>

namespace isc {
namespace util {

/// @brief A template representing an optional value.
///
/// This template class encapsulates an optional value. The default implementation
/// encapsulates numeric values, but additional specializations are defined
/// as necessary to support other types od data.
///
/// This class includes a boolean flag which indicates if the encapsulated
/// value is specified or unspecified. For example, a configuration parser
/// for the DHCP server may use this class to represent a value of the
/// configuration parameter which may appear in the configuration file, but
/// is not mandatory. The value of the @c Optional may be initialized to
/// "unspecified" initially. When the configuration parser finds that the
/// particular parameter exists in the configuration file, the default value
/// can be overridden and the value may be marked as "specified". If the
/// parameter is not found, the value remains "unspecified" and the appropriate
/// actions may be taken, e.g. the default value may be used.
///
/// @tparam Type of the encapsulated value.
template<typename T>
class Optional {
public:

    /// @brief Type of the encapsulated value.
    typedef T ValueType;

    /// @brief Assigns a new value value and marks it "specified".
    ///
    /// @tparam A Type of the value to be assigned. Typically this is @c T, but
    /// may also be a type that can be cast to @c T.
    /// @param other new actual value.
    template<typename A>
    Optional<T>& operator=(A other) {
        default_ = other;
        unspecified_ = false;
        return (*this);
    }

    /// @brief Type cast operator.
    ///
    /// This operator converts the optional value to the actual value being
    /// encapsulated.
    ///
    /// @return Encapsulated value.
    operator T() const {
        return (default_);
    }

    /// @brief Equality operator.
    ///
    /// @param other value to be compared.
    bool operator==(const T& other) const {
        return (default_ == other);
    }

    /// @brief Inequality operator.
    ///
    /// @param other value to be compared.
    bool operator!=(const T& other) const {
        return (default_ != other);
    }

    /// @brief Default constructor.
    ///
    /// Sets the encapsulated value to 0 and marks it as "unspecified".
    ///
    /// The caller must ensure that the constructor of the class @c T
    /// creates a valid object when invoked with 0 as an argument.
    /// For example, a @c std::string(0) compiles but will crash at
    /// runtime as 0 is not a valid pointer for the
    /// @c std::string(const char*) constructor. Therefore, the
    /// specialization of the @c Optional template for @c std::string
    /// is provided below. It uses @c std::string default constructor.
    ///
    /// For any other type used with this template which doesn't come
    /// with an appropriate constructor, the caller must create a
    /// template specialization similar to the one provided for
    /// @c std::string below.
    Optional()
        : default_(T(0)), unspecified_(true) {
    }

    /// @brief Constructor
    ///
    /// Sets an explicit value and marks it as "specified".
    ///
    /// @tparam A Type of the value to be assigned. Typically this is @c T, but
    /// may also be a type that can be cast to @c T.
    /// @param value value to be assigned.
    /// @param unspecified initial state. Default is "unspecified".
    template<typename A>
    Optional(A value, const bool unspecified = false)
        : default_(value), unspecified_(unspecified) {
    }

    /// @brief Retrieves the encapsulated value.
    ///
    /// @return the encapsulated value
    T get() const {
        return (default_);
    }

    /// @brief Retrieves the encapsulated value if specified, or the given value
    /// otherwise.
    ///
    /// @param or_value the value it defaults to, if unspecified
    ///
    /// @return the encapsulated value or the default value
    T valueOr(T const& or_value) const {
        if (unspecified_) {
            return or_value;
        }
        return default_;
    }

    /// @brief Modifies the flag that indicates whether the value is specified
    /// or unspecified.
    ///
    /// @param unspecified new value of the flag. If it is @c true, the
    /// value is marked as unspecified, otherwise it is marked as specified.
    void unspecified(bool unspecified) {
        unspecified_ = unspecified;
    }

    /// @brief Checks if the value has been specified or unspecified.
    ///
    /// @return true if the value hasn't been specified, false otherwise.
    bool unspecified() const {
        return (unspecified_);
    }

    /// @brief Checks if the encapsulated value is empty.
    ///
    /// This method can be overloaded in the template specializations that
    /// are dedicated to strings, vectors etc.
    ///
    /// @throw isc::InvalidOperation.
    bool empty() const {
        isc_throw(isc::InvalidOperation, "call to empty() not supported");
    }

protected:
    T default_;         ///< Encapsulated value.
    bool unspecified_;  ///< Flag which indicates if the value is specified.
};

/// @brief Specialization of the default @c Optional constructor for
/// strings.
///
/// It calls default string object constructor.
template<>
inline Optional<std::string>::Optional()
    : default_(), unspecified_(true) {
}

/// @brief Specialization of the @c Optional::empty method for strings.
///
/// @return true if the value is empty, false otherwise.
template<>
inline bool Optional<std::string>::empty() const {
    return (default_.empty());
}

/// @brief Inserts an optional value to a stream.
///
/// This function overloads the global operator<< to behave as in
/// @c ostream::operator<< but applied to @c Optional objects.
///
/// @param os A @c std::ostream object to which the value is inserted.
/// @param optional_value An @c Optional object to be inserted into
/// a stream.
/// @tparam Type of the value encapsulated by the @c Optional object.
///
/// @return A reference to the stream after insertion.
template<typename T>
std::ostream&
operator<<(std::ostream& os, const Optional<T>& optional_value) {
    os << optional_value.get();
    return (os);
}


} // end of namespace isc::util
} // end of namespace isc

#endif // OPTIONAL_VALUE_H