1 // Copyright 2010 Google Inc.
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 /// \param unused_none A copy of the utils::none instance.
51 utils::optional< T >::optional(
52 utils::detail::none_t UTILS_UNUSED_PARAM(none)) :
58 /// Initializes an optional object to a non-none value.
60 /// \param data The initial value for the object.
62 utils::optional< T >::optional(const T& data) :
70 /// \param other The optional object to copy from.
72 utils::optional< T >::optional(const optional< T >& other) :
73 _data(other._data == NULL ? NULL : new T(*(other._data)))
80 utils::optional< T >::~optional(void)
84 _data = NULL; // Prevent accidental reuse.
88 /// Explicitly assigns an optional object to the none value.
90 /// \param unused_none A copy of the utils::none instance.
92 /// \return A reference to this.
95 utils::optional< T >::operator=(utils::detail::none_t UTILS_UNUSED_PARAM(none))
104 /// Assigns a new value to the optional object.
106 /// \param data The initial value for the object.
108 /// \return A reference to this.
110 utils::optional< T >&
111 utils::optional< T >::operator=(const T& data)
113 T* new_data = new T(data);
121 /// Copies an optional value.
123 /// \param other The optional object to copy from.
125 /// \return A reference to this.
127 utils::optional< T >&
128 utils::optional< T >::operator=(const optional< T >& other)
130 T* new_data = other._data == NULL ? NULL : new T(*(other._data));
138 /// Equality comparator.
140 /// \param other The other object to compare this one to.
142 /// \return True if this object and other are equal; false otherwise.
145 utils::optional< T >::operator==(const optional< T >& other) const
147 if (_data == NULL && other._data == NULL) {
149 } else if (_data == NULL || other._data == NULL) {
152 INV(_data != NULL && other._data != NULL);
153 return *_data == *other._data;
158 /// Inequality comparator.
160 /// \param other The other object to compare this one to.
162 /// \return True if this object and other are different; false otherwise.
165 utils::optional< T >::operator!=(const optional< T >& other) const
167 return !(*this == other);
171 /// Gets the value hold by the optional object.
173 /// \pre The optional object must not be none.
175 /// \return A reference to the data.
178 utils::optional< T >::get(void) const
185 /// Gets the value of this object with a default fallback.
187 /// \param default_value The value to return if this object holds no value.
189 /// \return A reference to the data in the optional object, or the reference
190 /// passed in as a parameter.
193 utils::optional< T >::get_default(const T& default_value) const
198 return default_value;
202 /// Tests whether the optional object contains data or not.
204 /// \return True if the object is not none; false otherwise.
206 utils::optional< T >::operator bool(void) const
208 return _data != NULL;
212 /// Tests whether the optional object contains data or not.
214 /// \return True if the object is not none; false otherwise.
217 utils::optional< T >::get(void)
224 /// Injects the object into a stream.
226 /// \param output The stream into which to inject the object.
227 /// \param object The object to format.
229 /// \return The output stream.
231 std::ostream& utils::operator<<(std::ostream& output,
232 const optional< T >& object)
237 output << object.get();
243 /// Helper function to instantiate optional objects.
245 /// \param value The value for the optional object. Shouldn't be none, as
246 /// optional objects can be constructed from none right away.
248 /// \return A new optional object.
251 utils::make_optional(const T& value)
253 return optional< T >(value);
257 #endif // !defined(UTILS_OPTIONAL_IPP)