1 // Copyright 2010 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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.
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.
29 #if !defined(UTILS_OPTIONAL_IPP)
30 #define UTILS_OPTIONAL_IPP
34 #include "utils/defs.hpp"
35 #include "utils/optional.hpp"
36 #include "utils/sanity.hpp"
39 /// Initializes an optional object to the none value.
41 utils::optional< T >::optional(void) :
47 /// Explicitly initializes an optional object to the none value.
49 utils::optional< T >::optional(utils::detail::none_t /* none */) :
55 /// Initializes an optional object to a non-none value.
57 /// \param data The initial value for the object.
59 utils::optional< T >::optional(const T& data) :
67 /// \param other The optional object to copy from.
69 utils::optional< T >::optional(const optional< T >& other) :
70 _data(other._data == NULL ? NULL : new T(*(other._data)))
77 utils::optional< T >::~optional(void)
81 _data = NULL; // Prevent accidental reuse.
85 /// Explicitly assigns an optional object to the none value.
87 /// \return A reference to this.
90 utils::optional< T >::operator=(utils::detail::none_t /* none */)
99 /// Assigns a new value to the optional object.
101 /// \param data The initial value for the object.
103 /// \return A reference to this.
105 utils::optional< T >&
106 utils::optional< T >::operator=(const T& data)
108 T* new_data = new T(data);
116 /// Copies an optional value.
118 /// \param other The optional object to copy from.
120 /// \return A reference to this.
122 utils::optional< T >&
123 utils::optional< T >::operator=(const optional< T >& other)
125 T* new_data = other._data == NULL ? NULL : new T(*(other._data));
133 /// Equality comparator.
135 /// \param other The other object to compare this one to.
137 /// \return True if this object and other are equal; false otherwise.
140 utils::optional< T >::operator==(const optional< T >& other) const
142 if (_data == NULL && other._data == NULL) {
144 } else if (_data == NULL || other._data == NULL) {
147 INV(_data != NULL && other._data != NULL);
148 return *_data == *other._data;
153 /// Inequality comparator.
155 /// \param other The other object to compare this one to.
157 /// \return True if this object and other are different; false otherwise.
160 utils::optional< T >::operator!=(const optional< T >& other) const
162 return !(*this == other);
166 /// Gets the value hold by the optional object.
168 /// \pre The optional object must not be none.
170 /// \return A reference to the data.
173 utils::optional< T >::get(void) const
180 /// Gets the value of this object with a default fallback.
182 /// \param default_value The value to return if this object holds no value.
184 /// \return A reference to the data in the optional object, or the reference
185 /// passed in as a parameter.
188 utils::optional< T >::get_default(const T& default_value) const
193 return default_value;
197 /// Tests whether the optional object contains data or not.
199 /// \return True if the object is not none; false otherwise.
201 utils::optional< T >::operator bool(void) const
203 return _data != NULL;
207 /// Tests whether the optional object contains data or not.
209 /// \return True if the object is not none; false otherwise.
212 utils::optional< T >::get(void)
219 /// Injects the object into a stream.
221 /// \param output The stream into which to inject the object.
222 /// \param object The object to format.
224 /// \return The output stream.
226 std::ostream& utils::operator<<(std::ostream& output,
227 const optional< T >& object)
232 output << object.get();
238 /// Helper function to instantiate optional objects.
240 /// \param value The value for the optional object. Shouldn't be none, as
241 /// optional objects can be constructed from none right away.
243 /// \return A new optional object.
246 utils::make_optional(const T& value)
248 return optional< T >(value);
252 #endif // !defined(UTILS_OPTIONAL_IPP)