Edit: /usr/include/boost/hana/optional.hpp (15422B)
/*!
@file
Defines `boost::hana::optional`.
@copyright Louis Dionne 2013-2017
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/
#ifndef BOOST_HANA_OPTIONAL_HPP
#define BOOST_HANA_OPTIONAL_HPP
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include // std::nullptr_t
#include
#include
BOOST_HANA_NAMESPACE_BEGIN
//////////////////////////////////////////////////////////////////////////
// optional<>
//////////////////////////////////////////////////////////////////////////
namespace detail {
template ::type>
struct nested_type { };
template
struct nested_type { using type = typename T::type; };
}
template
struct optional : detail::operators::adl<>, detail::nested_type {
// 5.3.1, Constructors
constexpr optional() = default;
constexpr optional(optional const&) = default;
constexpr optional(optional&&) = default;
constexpr optional(T const& t)
: value_(t)
{ }
constexpr optional(T&& t)
: value_(static_cast(t))
{ }
// 5.3.3, Assignment
constexpr optional& operator=(optional const&) = default;
constexpr optional& operator=(optional&&) = default;
// 5.3.5, Observers
constexpr T const* operator->() const { return &value_; }
constexpr T* operator->() { return &value_; }
constexpr T& value() & { return value_; }
constexpr T const& value() const& { return value_; }
constexpr T&& value() && { return static_cast(value_); }
constexpr T const&& value() const&& { return static_cast(value_); }
constexpr T& operator*() & { return value_; }
constexpr T const& operator*() const& { return value_; }
constexpr T&& operator*() && { return static_cast(value_); }
constexpr T const&& operator*() const&& { return static_cast(value_); }
template constexpr T& value_or(U&&) & { return value_; }
template constexpr T const& value_or(U&&) const& { return value_; }
template constexpr T&& value_or(U&&) && { return static_cast(value_); }
template constexpr T const&& value_or(U&&) const&& { return static_cast(value_); }
// We leave this public because it simplifies the implementation, but
// this should be considered private by users.
T value_;
};
//! @cond
template
constexpr auto optional<>::value() const {
static_assert(detail::wrong{},
"hana::optional::value() requires a non-empty optional");
}
template
constexpr auto optional<>::operator*() const {
static_assert(detail::wrong{},
"hana::optional::operator* requires a non-empty optional");
}
template
constexpr U&& optional<>::value_or(U&& u) const {
return static_cast(u);
}
template
constexpr auto make_just_t::operator()(T&& t) const {
return hana::optional::type>(static_cast(t));
}
//! @endcond
template
struct tag_of> {
using type = optional_tag;
};
//////////////////////////////////////////////////////////////////////////
// make
//////////////////////////////////////////////////////////////////////////
template <>
struct make_impl {
template
static constexpr auto apply(X&& x)
{ return hana::just(static_cast(x)); }
static constexpr auto apply()
{ return hana::nothing; }
};
//////////////////////////////////////////////////////////////////////////
// Operators
//////////////////////////////////////////////////////////////////////////
namespace detail {
template <>
struct comparable_operators {
static constexpr bool value = true;
};
template <>
struct orderable_operators {
static constexpr bool value = true;
};
template <>
struct monad_operators {
static constexpr bool value = true;
};
}
//////////////////////////////////////////////////////////////////////////
// is_just and is_nothing
//////////////////////////////////////////////////////////////////////////
//! @cond
template
constexpr auto is_just_t::operator()(optional const&) const
{ return hana::bool_c; }
template
constexpr auto is_nothing_t::operator()(optional const&) const
{ return hana::bool_c; }
//! @endcond
//////////////////////////////////////////////////////////////////////////
// sfinae
//////////////////////////////////////////////////////////////////////////
namespace detail {
struct sfinae_impl {
template ()(std::declval()...)
)>
constexpr decltype(auto) operator()(int, F&& f, X&& ...x) const {
using Return = decltype(static_cast(f)(static_cast(x)...));
static_assert(!std::is_same::value,
"hana::sfinae(f)(args...) requires f(args...) to be non-void");
return hana::just(static_cast(f)(static_cast(x)...));
}
template
constexpr auto operator()(long, F&&, X&& ...) const
{ return hana::nothing; }
};
}
//! @cond
template
constexpr decltype(auto) sfinae_t::operator()(F&& f) const {
return hana::partial(detail::sfinae_impl{}, int{},
static_cast(f));
}
//! @endcond
//////////////////////////////////////////////////////////////////////////
// Comparable
//////////////////////////////////////////////////////////////////////////
template <>
struct equal_impl {
template
static constexpr auto apply(hana::optional const& t, hana::optional const& u)
{ return hana::equal(t.value_, u.value_); }
static constexpr hana::true_ apply(hana::optional<> const&, hana::optional<> const&)
{ return {}; }
template
static constexpr hana::false_ apply(T const&, U const&)
{ return {}; }
};
//////////////////////////////////////////////////////////////////////////
// Orderable
//////////////////////////////////////////////////////////////////////////
template <>
struct less_impl {
template
static constexpr hana::true_ apply(hana::optional<> const&, hana::optional const&)
{ return {}; }
static constexpr hana::false_ apply(hana::optional<> const&, hana::optional<> const&)
{ return {}; }
template
static constexpr hana::false_ apply(hana::optional const&, hana::optional<> const&)
{ return {}; }
template
static constexpr auto apply(hana::optional const& x, hana::optional const& y)
{ return hana::less(x.value_, y.value_); }
};
//////////////////////////////////////////////////////////////////////////
// Functor
//////////////////////////////////////////////////////////////////////////
template <>
struct transform_impl {
template
static constexpr auto apply(optional<> const&, F&&)
{ return hana::nothing; }
template
static constexpr auto apply(optional const& opt, F&& f)
{ return hana::just(static_cast(f)(opt.value_)); }
template
static constexpr auto apply(optional& opt, F&& f)
{ return hana::just(static_cast(f)(opt.value_)); }
template
static constexpr auto apply(optional&& opt, F&& f)
{ return hana::just(static_cast(f)(static_cast(opt.value_))); }
};
//////////////////////////////////////////////////////////////////////////
// Applicative
//////////////////////////////////////////////////////////////////////////
template <>
struct lift_impl {
template
static constexpr auto apply(X&& x)
{ return hana::just(static_cast(x)); }
};
template <>
struct ap_impl {
template
static constexpr auto ap_helper(F&&, X&&, ...)
{ return hana::nothing; }
template
static constexpr auto ap_helper(F&& f, X&& x, hana::true_, hana::true_)
{ return hana::just(static_cast(f).value_(static_cast(x).value_)); }
template
static constexpr auto apply(F&& f, X&& x) {
return ap_impl::ap_helper(static_cast(f), static_cast(x),
hana::is_just(f), hana::is_just(x));
}
};
//////////////////////////////////////////////////////////////////////////
// Monad
//////////////////////////////////////////////////////////////////////////
template <>
struct flatten_impl {
static constexpr auto apply(optional<> const&)
{ return hana::nothing; }
static constexpr auto apply(optional> const&)
{ return hana::nothing; }
template
static constexpr auto apply(optional> const& opt)
{ return hana::just(opt.value_.value_); }
template
static constexpr auto apply(optional>&& opt)
{ return hana::just(static_cast(opt.value_.value_)); }
};
//////////////////////////////////////////////////////////////////////////
// MonadPlus
//////////////////////////////////////////////////////////////////////////
template <>
struct concat_impl {
template
static constexpr auto apply(hana::optional<>&, Y&& y)
{ return static_cast(y); }
template
static constexpr auto apply(hana::optional<>&&, Y&& y)
{ return static_cast(y); }
template
static constexpr auto apply(hana::optional<> const&, Y&& y)
{ return static_cast(y); }
template
static constexpr auto apply(X&& x, Y&&)
{ return static_cast(x); }
};
template <>
struct empty_impl {
static constexpr auto apply()
{ return hana::nothing; }
};
//////////////////////////////////////////////////////////////////////////
// Foldable
//////////////////////////////////////////////////////////////////////////
template <>
struct unpack_impl {
template
static constexpr decltype(auto) apply(optional&& opt, F&& f)
{ return static_cast(f)(static_cast(opt.value_)); }
template
static constexpr decltype(auto) apply(optional const& opt, F&& f)
{ return static_cast(f)(opt.value_); }
template
static constexpr decltype(auto) apply(optional& opt, F&& f)
{ return static_cast(f)(opt.value_); }
template
static constexpr decltype(auto) apply(optional<> const&, F&& f)
{ return static_cast(f)(); }
};
//////////////////////////////////////////////////////////////////////////
// Searchable
//////////////////////////////////////////////////////////////////////////
namespace detail {
template
struct optional_find_if {
template
static constexpr auto apply(T const&)
{ return hana::nothing; }
};
template <>
struct optional_find_if {
template
static constexpr auto apply(T&& t)
{ return hana::just(static_cast(t)); }
};
}
template <>
struct find_if_impl {
template
static constexpr auto apply(hana::optional const& opt, Pred&& pred) {
constexpr bool found = decltype(static_cast(pred)(opt.value_))::value;
return detail::optional_find_if::apply(opt.value_);
}
template
static constexpr auto apply(hana::optional& opt, Pred&& pred) {
constexpr bool found = decltype(static_cast(pred)(opt.value_))::value;
return detail::optional_find_if::apply(opt.value_);
}
template
static constexpr auto apply(hana::optional&& opt, Pred&& pred) {
constexpr bool found = decltype(
static_cast(pred)(static_cast(opt.value_))
)::value;
return detail::optional_find_if::apply(static_cast(opt.value_));
}
template
static constexpr auto apply(hana::optional<> const&, Pred&&)
{ return hana::nothing; }
};
template <>
struct any_of_impl {
template
static constexpr auto apply(hana::optional const& opt, Pred&& pred)
{ return static_cast(pred)(opt.value_); }
template
static constexpr hana::false_ apply(hana::optional<> const&, Pred&&)
{ return {}; }
};
BOOST_HANA_NAMESPACE_END
#endif // !BOOST_HANA_OPTIONAL_HPP