|
Packit |
209faa |
// Copyright 2010 The Kyua Authors.
|
|
Packit |
209faa |
// All rights reserved.
|
|
Packit |
209faa |
//
|
|
Packit |
209faa |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
209faa |
// modification, are permitted provided that the following conditions are
|
|
Packit |
209faa |
// met:
|
|
Packit |
209faa |
//
|
|
Packit |
209faa |
// * Redistributions of source code must retain the above copyright
|
|
Packit |
209faa |
// notice, this list of conditions and the following disclaimer.
|
|
Packit |
209faa |
// * Redistributions in binary form must reproduce the above copyright
|
|
Packit |
209faa |
// notice, this list of conditions and the following disclaimer in the
|
|
Packit |
209faa |
// documentation and/or other materials provided with the distribution.
|
|
Packit |
209faa |
// * Neither the name of Google Inc. nor the names of its contributors
|
|
Packit |
209faa |
// may be used to endorse or promote products derived from this software
|
|
Packit |
209faa |
// without specific prior written permission.
|
|
Packit |
209faa |
//
|
|
Packit |
209faa |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit |
209faa |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit |
209faa |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
Packit |
209faa |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
Packit |
209faa |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
Packit |
209faa |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
Packit |
209faa |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
Packit |
209faa |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
Packit |
209faa |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
Packit |
209faa |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
Packit |
209faa |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
209faa |
|
|
Packit |
209faa |
/// \file utils/optional.hpp
|
|
Packit |
209faa |
/// Provides the utils::optional class.
|
|
Packit |
209faa |
///
|
|
Packit |
209faa |
/// The class is provided as a separate module on its own to minimize
|
|
Packit |
209faa |
/// header-inclusion side-effects.
|
|
Packit |
209faa |
|
|
Packit |
209faa |
#if !defined(UTILS_OPTIONAL_HPP)
|
|
Packit |
209faa |
#define UTILS_OPTIONAL_HPP
|
|
Packit |
209faa |
|
|
Packit |
209faa |
#include "utils/optional_fwd.hpp"
|
|
Packit |
209faa |
|
|
Packit |
209faa |
#include <ostream>
|
|
Packit |
209faa |
|
|
Packit |
209faa |
namespace utils {
|
|
Packit |
209faa |
|
|
Packit |
209faa |
|
|
Packit |
209faa |
/// Holds a data value or none.
|
|
Packit |
209faa |
///
|
|
Packit |
209faa |
/// This class allows users to represent values that may be uninitialized.
|
|
Packit |
209faa |
/// Instead of having to keep separate variables to track whether a variable is
|
|
Packit |
209faa |
/// supposed to have a value or not, this class allows multiplexing the
|
|
Packit |
209faa |
/// behaviors.
|
|
Packit |
209faa |
///
|
|
Packit |
209faa |
/// This class is a simplified version of Boost.Optional.
|
|
Packit |
209faa |
template< class T >
|
|
Packit |
209faa |
class optional {
|
|
Packit |
209faa |
/// Internal representation of the optional data value.
|
|
Packit |
209faa |
T* _data;
|
|
Packit |
209faa |
|
|
Packit |
209faa |
public:
|
|
Packit |
209faa |
optional(void);
|
|
Packit |
209faa |
optional(utils::detail::none_t);
|
|
Packit |
209faa |
optional(const optional< T >&);
|
|
Packit |
209faa |
explicit optional(const T&);
|
|
Packit |
209faa |
~optional(void);
|
|
Packit |
209faa |
|
|
Packit |
209faa |
optional& operator=(utils::detail::none_t);
|
|
Packit |
209faa |
optional& operator=(const T&);
|
|
Packit |
209faa |
optional& operator=(const optional< T >&);
|
|
Packit |
209faa |
|
|
Packit |
209faa |
bool operator==(const optional< T >&) const;
|
|
Packit |
209faa |
bool operator!=(const optional< T >&) const;
|
|
Packit |
209faa |
|
|
Packit |
209faa |
operator bool(void) const;
|
|
Packit |
209faa |
|
|
Packit |
209faa |
const T& get(void) const;
|
|
Packit |
209faa |
const T& get_default(const T&) const;
|
|
Packit |
209faa |
T& get(void);
|
|
Packit |
209faa |
};
|
|
Packit |
209faa |
|
|
Packit |
209faa |
|
|
Packit |
209faa |
template< class T >
|
|
Packit |
209faa |
std::ostream& operator<<(std::ostream&, const optional< T >&);
|
|
Packit |
209faa |
|
|
Packit |
209faa |
|
|
Packit |
209faa |
template< class T >
|
|
Packit |
209faa |
optional< T > make_optional(const T&);
|
|
Packit |
209faa |
|
|
Packit |
209faa |
|
|
Packit |
209faa |
} // namespace utils
|
|
Packit |
209faa |
|
|
Packit |
209faa |
#endif // !defined(UTILS_OPTIONAL_HPP)
|