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