xref: /freebsd/contrib/kyua/utils/optional.ipp (revision b0d29bc4)
1// Copyright 2010 The Kyua Authors.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9//   notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright
11//   notice, this list of conditions and the following disclaimer in the
12//   documentation and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors
14//   may be used to endorse or promote products derived from this software
15//   without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#if !defined(UTILS_OPTIONAL_IPP)
30#define UTILS_OPTIONAL_IPP
31
32#include <cstddef>
33
34#include "utils/defs.hpp"
35#include "utils/optional.hpp"
36#include "utils/sanity.hpp"
37
38
39/// Initializes an optional object to the none value.
40template< class T >
41utils::optional< T >::optional(void) :
42    _data(NULL)
43{
44}
45
46
47/// Explicitly initializes an optional object to the none value.
48template< class T >
49utils::optional< T >::optional(utils::detail::none_t /* none */) :
50    _data(NULL)
51{
52}
53
54
55/// Initializes an optional object to a non-none value.
56///
57/// \param data The initial value for the object.
58template< class T >
59utils::optional< T >::optional(const T& data) :
60    _data(new T(data))
61{
62}
63
64
65/// Copy constructor.
66///
67/// \param other The optional object to copy from.
68template< class T >
69utils::optional< T >::optional(const optional< T >& other) :
70    _data(other._data == NULL ? NULL : new T(*(other._data)))
71{
72}
73
74
75/// Destructor.
76template< class T >
77utils::optional< T >::~optional(void)
78{
79    if (_data != NULL)
80        delete _data;
81    _data = NULL;  // Prevent accidental reuse.
82}
83
84
85/// Explicitly assigns an optional object to the none value.
86///
87/// \return A reference to this.
88template< class T >
89utils::optional< T >&
90utils::optional< T >::operator=(utils::detail::none_t /* none */)
91{
92    if (_data != NULL)
93        delete _data;
94    _data = NULL;
95    return *this;
96}
97
98
99/// Assigns a new value to the optional object.
100///
101/// \param data The initial value for the object.
102///
103/// \return A reference to this.
104template< class T >
105utils::optional< T >&
106utils::optional< T >::operator=(const T& data)
107{
108    T* new_data = new T(data);
109    if (_data != NULL)
110        delete _data;
111    _data = new_data;
112    return *this;
113}
114
115
116/// Copies an optional value.
117///
118/// \param other The optional object to copy from.
119///
120/// \return A reference to this.
121template< class T >
122utils::optional< T >&
123utils::optional< T >::operator=(const optional< T >& other)
124{
125    T* new_data = other._data == NULL ? NULL : new T(*(other._data));
126    if (_data != NULL)
127        delete _data;
128    _data = new_data;
129    return *this;
130}
131
132
133/// Equality comparator.
134///
135/// \param other The other object to compare this one to.
136///
137/// \return True if this object and other are equal; false otherwise.
138template< class T >
139bool
140utils::optional< T >::operator==(const optional< T >& other) const
141{
142    if (_data == NULL && other._data == NULL) {
143        return true;
144    } else if (_data == NULL || other._data == NULL) {
145        return false;
146    } else {
147        INV(_data != NULL && other._data != NULL);
148        return *_data == *other._data;
149    }
150}
151
152
153/// Inequality comparator.
154///
155/// \param other The other object to compare this one to.
156///
157/// \return True if this object and other are different; false otherwise.
158template< class T >
159bool
160utils::optional< T >::operator!=(const optional< T >& other) const
161{
162    return !(*this == other);
163}
164
165
166/// Gets the value hold by the optional object.
167///
168/// \pre The optional object must not be none.
169///
170/// \return A reference to the data.
171template< class T >
172const T&
173utils::optional< T >::get(void) const
174{
175    PRE(_data != NULL);
176    return *_data;
177}
178
179
180/// Gets the value of this object with a default fallback.
181///
182/// \param default_value The value to return if this object holds no value.
183///
184/// \return A reference to the data in the optional object, or the reference
185/// passed in as a parameter.
186template< class T >
187const T&
188utils::optional< T >::get_default(const T& default_value) const
189{
190    if (_data != NULL)
191        return *_data;
192    else
193        return default_value;
194}
195
196
197/// Tests whether the optional object contains data or not.
198///
199/// \return True if the object is not none; false otherwise.
200template< class T >
201utils::optional< T >::operator bool(void) const
202{
203    return _data != NULL;
204}
205
206
207/// Tests whether the optional object contains data or not.
208///
209/// \return True if the object is not none; false otherwise.
210template< class T >
211T&
212utils::optional< T >::get(void)
213{
214    PRE(_data != NULL);
215    return *_data;
216}
217
218
219/// Injects the object into a stream.
220///
221/// \param output The stream into which to inject the object.
222/// \param object The object to format.
223///
224/// \return The output stream.
225template< class T >
226std::ostream& utils::operator<<(std::ostream& output,
227                                const optional< T >& object)
228{
229    if (!object) {
230        output << "none";
231    } else {
232        output << object.get();
233    }
234    return output;
235}
236
237
238/// Helper function to instantiate optional objects.
239///
240/// \param value The value for the optional object.  Shouldn't be none, as
241///     optional objects can be constructed from none right away.
242///
243/// \return A new optional object.
244template< class T >
245utils::optional< T >
246utils::make_optional(const T& value)
247{
248    return optional< T >(value);
249}
250
251
252#endif  // !defined(UTILS_OPTIONAL_IPP)
253