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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
// Copyright 2010 Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#if !defined(UTILS_OPTIONAL_IPP)
#define UTILS_OPTIONAL_IPP
#include <cstddef>
#include "utils/defs.hpp"
#include "utils/optional.hpp"
#include "utils/sanity.hpp"
/// Initializes an optional object to the none value.
template< class T >
utils::optional< T >::optional(void) :
_data(NULL)
{
}
/// Explicitly initializes an optional object to the none value.
///
/// \param unused_none A copy of the utils::none instance.
template< class T >
utils::optional< T >::optional(
utils::detail::none_t UTILS_UNUSED_PARAM(none)) :
_data(NULL)
{
}
/// Initializes an optional object to a non-none value.
///
/// \param data The initial value for the object.
template< class T >
utils::optional< T >::optional(const T& data) :
_data(new T(data))
{
}
/// Copy constructor.
///
/// \param other The optional object to copy from.
template< class T >
utils::optional< T >::optional(const optional< T >& other) :
_data(other._data == NULL ? NULL : new T(*(other._data)))
{
}
/// Destructor.
template< class T >
utils::optional< T >::~optional(void)
{
if (_data != NULL)
delete _data;
_data = NULL; // Prevent accidental reuse.
}
/// Explicitly assigns an optional object to the none value.
///
/// \param unused_none A copy of the utils::none instance.
///
/// \return A reference to this.
template< class T >
utils::optional< T >&
utils::optional< T >::operator=(utils::detail::none_t UTILS_UNUSED_PARAM(none))
{
if (_data != NULL)
delete _data;
_data = NULL;
return *this;
}
/// Assigns a new value to the optional object.
///
/// \param data The initial value for the object.
///
/// \return A reference to this.
template< class T >
utils::optional< T >&
utils::optional< T >::operator=(const T& data)
{
T* new_data = new T(data);
if (_data != NULL)
delete _data;
_data = new_data;
return *this;
}
/// Copies an optional value.
///
/// \param other The optional object to copy from.
///
/// \return A reference to this.
template< class T >
utils::optional< T >&
utils::optional< T >::operator=(const optional< T >& other)
{
T* new_data = other._data == NULL ? NULL : new T(*(other._data));
if (_data != NULL)
delete _data;
_data = new_data;
return *this;
}
/// Equality comparator.
///
/// \param other The other object to compare this one to.
///
/// \return True if this object and other are equal; false otherwise.
template< class T >
bool
utils::optional< T >::operator==(const optional< T >& other) const
{
if (_data == NULL && other._data == NULL) {
return true;
} else if (_data == NULL || other._data == NULL) {
return false;
} else {
INV(_data != NULL && other._data != NULL);
return *_data == *other._data;
}
}
/// Inequality comparator.
///
/// \param other The other object to compare this one to.
///
/// \return True if this object and other are different; false otherwise.
template< class T >
bool
utils::optional< T >::operator!=(const optional< T >& other) const
{
return !(*this == other);
}
/// Gets the value hold by the optional object.
///
/// \pre The optional object must not be none.
///
/// \return A reference to the data.
template< class T >
const T&
utils::optional< T >::get(void) const
{
PRE(_data != NULL);
return *_data;
}
/// Gets the value of this object with a default fallback.
///
/// \param default_value The value to return if this object holds no value.
///
/// \return A reference to the data in the optional object, or the reference
/// passed in as a parameter.
template< class T >
const T&
utils::optional< T >::get_default(const T& default_value) const
{
if (_data != NULL)
return *_data;
else
return default_value;
}
/// Tests whether the optional object contains data or not.
///
/// \return True if the object is not none; false otherwise.
template< class T >
utils::optional< T >::operator bool(void) const
{
return _data != NULL;
}
/// Tests whether the optional object contains data or not.
///
/// \return True if the object is not none; false otherwise.
template< class T >
T&
utils::optional< T >::get(void)
{
PRE(_data != NULL);
return *_data;
}
/// Injects the object into a stream.
///
/// \param output The stream into which to inject the object.
/// \param object The object to format.
///
/// \return The output stream.
template< class T >
std::ostream& utils::operator<<(std::ostream& output,
const optional< T >& object)
{
if (!object) {
output << "none";
} else {
output << object.get();
}
return output;
}
/// Helper function to instantiate optional objects.
///
/// \param value The value for the optional object. Shouldn't be none, as
/// optional objects can be constructed from none right away.
///
/// \return A new optional object.
template< class T >
utils::optional< T >
utils::make_optional(const T& value)
{
return optional< T >(value);
}
#endif // !defined(UTILS_OPTIONAL_IPP)
|