123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- /*!
- @file
- Defines `boost::hana::optional`.
- Copyright Louis Dionne 2013-2022
- 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 <boost/hana/fwd/optional.hpp>
- #include <boost/hana/bool.hpp>
- #include <boost/hana/config.hpp>
- #include <boost/hana/core/tag_of.hpp>
- #include <boost/hana/detail/decay.hpp>
- #include <boost/hana/detail/operators/adl.hpp>
- #include <boost/hana/detail/operators/comparable.hpp>
- #include <boost/hana/detail/operators/monad.hpp>
- #include <boost/hana/detail/operators/orderable.hpp>
- #include <boost/hana/detail/wrong.hpp>
- #include <boost/hana/functional/partial.hpp>
- #include <boost/hana/fwd/any_of.hpp>
- #include <boost/hana/fwd/ap.hpp>
- #include <boost/hana/fwd/concat.hpp>
- #include <boost/hana/fwd/core/make.hpp>
- #include <boost/hana/fwd/empty.hpp>
- #include <boost/hana/fwd/equal.hpp>
- #include <boost/hana/fwd/find_if.hpp>
- #include <boost/hana/fwd/flatten.hpp>
- #include <boost/hana/fwd/less.hpp>
- #include <boost/hana/fwd/lift.hpp>
- #include <boost/hana/fwd/transform.hpp>
- #include <boost/hana/fwd/type.hpp>
- #include <boost/hana/fwd/unpack.hpp>
- #include <cstddef> // std::nullptr_t
- #include <type_traits>
- #include <utility>
- namespace boost { namespace hana {
- //////////////////////////////////////////////////////////////////////////
- // optional<>
- //////////////////////////////////////////////////////////////////////////
- namespace detail {
- template <typename T, typename = typename hana::tag_of<T>::type>
- struct nested_type { };
- template <typename T>
- struct nested_type<T, type_tag> { using type = typename T::type; };
- }
- template <typename T>
- struct optional<T> : detail::operators::adl<>, detail::nested_type<T> {
- // 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&&>(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<T&&>(value_); }
- constexpr T const&& value() const&& { return static_cast<T const&&>(value_); }
- constexpr T& operator*() & { return value_; }
- constexpr T const& operator*() const& { return value_; }
- constexpr T&& operator*() && { return static_cast<T&&>(value_); }
- constexpr T const&& operator*() const&& { return static_cast<T const&&>(value_); }
- template <typename U> constexpr T& value_or(U&&) & { return value_; }
- template <typename U> constexpr T const& value_or(U&&) const& { return value_; }
- template <typename U> constexpr T&& value_or(U&&) && { return static_cast<T&&>(value_); }
- template <typename U> constexpr T const&& value_or(U&&) const&& { return static_cast<T const&&>(value_); }
- // We leave this public because it simplifies the implementation, but
- // this should be considered private by users.
- T value_;
- };
- //! @cond
- template <typename ...dummy>
- constexpr auto optional<>::value() const {
- static_assert(detail::wrong<dummy...>{},
- "hana::optional::value() requires a non-empty optional");
- }
- template <typename ...dummy>
- constexpr auto optional<>::operator*() const {
- static_assert(detail::wrong<dummy...>{},
- "hana::optional::operator* requires a non-empty optional");
- }
- template <typename U>
- constexpr U&& optional<>::value_or(U&& u) const {
- return static_cast<U&&>(u);
- }
- template <typename T>
- constexpr auto make_just_t::operator()(T&& t) const {
- return hana::optional<typename detail::decay<T>::type>(static_cast<T&&>(t));
- }
- //! @endcond
- template <typename ...T>
- struct tag_of<optional<T...>> {
- using type = optional_tag;
- };
- //////////////////////////////////////////////////////////////////////////
- // make<optional_tag>
- //////////////////////////////////////////////////////////////////////////
- template <>
- struct make_impl<optional_tag> {
- template <typename X>
- static constexpr auto apply(X&& x)
- { return hana::just(static_cast<X&&>(x)); }
- static constexpr auto apply()
- { return hana::nothing; }
- };
- //////////////////////////////////////////////////////////////////////////
- // Operators
- //////////////////////////////////////////////////////////////////////////
- namespace detail {
- template <>
- struct comparable_operators<optional_tag> {
- static constexpr bool value = true;
- };
- template <>
- struct orderable_operators<optional_tag> {
- static constexpr bool value = true;
- };
- template <>
- struct monad_operators<optional_tag> {
- static constexpr bool value = true;
- };
- }
- //////////////////////////////////////////////////////////////////////////
- // is_just and is_nothing
- //////////////////////////////////////////////////////////////////////////
- //! @cond
- template <typename ...T>
- constexpr auto is_just_t::operator()(optional<T...> const&) const
- { return hana::bool_c<sizeof...(T) != 0>; }
- template <typename ...T>
- constexpr auto is_nothing_t::operator()(optional<T...> const&) const
- { return hana::bool_c<sizeof...(T) == 0>; }
- //! @endcond
- //////////////////////////////////////////////////////////////////////////
- // sfinae
- //////////////////////////////////////////////////////////////////////////
- namespace detail {
- struct sfinae_impl {
- template <typename F, typename ...X, typename = decltype(
- std::declval<F>()(std::declval<X>()...)
- )>
- constexpr decltype(auto) operator()(int, F&& f, X&& ...x) const {
- using Return = decltype(static_cast<F&&>(f)(static_cast<X&&>(x)...));
- static_assert(!std::is_same<Return, void>::value,
- "hana::sfinae(f)(args...) requires f(args...) to be non-void");
- return hana::just(static_cast<F&&>(f)(static_cast<X&&>(x)...));
- }
- template <typename F, typename ...X>
- constexpr auto operator()(long, F&&, X&& ...) const
- { return hana::nothing; }
- };
- }
- //! @cond
- template <typename F>
- constexpr decltype(auto) sfinae_t::operator()(F&& f) const {
- return hana::partial(detail::sfinae_impl{}, int{},
- static_cast<F&&>(f));
- }
- //! @endcond
- //////////////////////////////////////////////////////////////////////////
- // Comparable
- //////////////////////////////////////////////////////////////////////////
- template <>
- struct equal_impl<optional_tag, optional_tag> {
- template <typename T, typename U>
- static constexpr auto apply(hana::optional<T> const& t, hana::optional<U> const& u)
- { return hana::equal(t.value_, u.value_); }
- static constexpr hana::true_ apply(hana::optional<> const&, hana::optional<> const&)
- { return {}; }
- template <typename T, typename U>
- static constexpr hana::false_ apply(T const&, U const&)
- { return {}; }
- };
- //////////////////////////////////////////////////////////////////////////
- // Orderable
- //////////////////////////////////////////////////////////////////////////
- template <>
- struct less_impl<optional_tag, optional_tag> {
- template <typename T>
- static constexpr hana::true_ apply(hana::optional<> const&, hana::optional<T> const&)
- { return {}; }
- static constexpr hana::false_ apply(hana::optional<> const&, hana::optional<> const&)
- { return {}; }
- template <typename T>
- static constexpr hana::false_ apply(hana::optional<T> const&, hana::optional<> const&)
- { return {}; }
- template <typename T, typename U>
- static constexpr auto apply(hana::optional<T> const& x, hana::optional<U> const& y)
- { return hana::less(x.value_, y.value_); }
- };
- //////////////////////////////////////////////////////////////////////////
- // Functor
- //////////////////////////////////////////////////////////////////////////
- template <>
- struct transform_impl<optional_tag> {
- template <typename F>
- static constexpr auto apply(optional<> const&, F&&)
- { return hana::nothing; }
- template <typename T, typename F>
- static constexpr auto apply(optional<T> const& opt, F&& f)
- { return hana::just(static_cast<F&&>(f)(opt.value_)); }
- template <typename T, typename F>
- static constexpr auto apply(optional<T>& opt, F&& f)
- { return hana::just(static_cast<F&&>(f)(opt.value_)); }
- template <typename T, typename F>
- static constexpr auto apply(optional<T>&& opt, F&& f)
- { return hana::just(static_cast<F&&>(f)(static_cast<T&&>(opt.value_))); }
- };
- //////////////////////////////////////////////////////////////////////////
- // Applicative
- //////////////////////////////////////////////////////////////////////////
- template <>
- struct lift_impl<optional_tag> {
- template <typename X>
- static constexpr auto apply(X&& x)
- { return hana::just(static_cast<X&&>(x)); }
- };
- template <>
- struct ap_impl<optional_tag> {
- template <typename F, typename X>
- static constexpr auto ap_helper(F&&, X&&, ...)
- { return hana::nothing; }
- template <typename F, typename X>
- static constexpr auto ap_helper(F&& f, X&& x, hana::true_, hana::true_)
- { return hana::just(static_cast<F&&>(f).value_(static_cast<X&&>(x).value_)); }
- template <typename F, typename X>
- static constexpr auto apply(F&& f, X&& x) {
- return ap_impl::ap_helper(static_cast<F&&>(f), static_cast<X&&>(x),
- hana::is_just(f), hana::is_just(x));
- }
- };
- //////////////////////////////////////////////////////////////////////////
- // Monad
- //////////////////////////////////////////////////////////////////////////
- template <>
- struct flatten_impl<optional_tag> {
- static constexpr auto apply(optional<> const&)
- { return hana::nothing; }
- static constexpr auto apply(optional<optional<>> const&)
- { return hana::nothing; }
- template <typename T>
- static constexpr auto apply(optional<optional<T>> const& opt)
- { return hana::just(opt.value_.value_); }
- template <typename T>
- static constexpr auto apply(optional<optional<T>>&& opt)
- { return hana::just(static_cast<T&&>(opt.value_.value_)); }
- };
- //////////////////////////////////////////////////////////////////////////
- // MonadPlus
- //////////////////////////////////////////////////////////////////////////
- template <>
- struct concat_impl<optional_tag> {
- template <typename Y>
- static constexpr auto apply(hana::optional<>&, Y&& y)
- { return static_cast<Y&&>(y); }
- template <typename Y>
- static constexpr auto apply(hana::optional<>&&, Y&& y)
- { return static_cast<Y&&>(y); }
- template <typename Y>
- static constexpr auto apply(hana::optional<> const&, Y&& y)
- { return static_cast<Y&&>(y); }
- template <typename X, typename Y>
- static constexpr auto apply(X&& x, Y&&)
- { return static_cast<X&&>(x); }
- };
- template <>
- struct empty_impl<optional_tag> {
- static constexpr auto apply()
- { return hana::nothing; }
- };
- //////////////////////////////////////////////////////////////////////////
- // Foldable
- //////////////////////////////////////////////////////////////////////////
- template <>
- struct unpack_impl<optional_tag> {
- template <typename T, typename F>
- static constexpr decltype(auto) apply(optional<T>&& opt, F&& f)
- { return static_cast<F&&>(f)(static_cast<T&&>(opt.value_)); }
- template <typename T, typename F>
- static constexpr decltype(auto) apply(optional<T> const& opt, F&& f)
- { return static_cast<F&&>(f)(opt.value_); }
- template <typename T, typename F>
- static constexpr decltype(auto) apply(optional<T>& opt, F&& f)
- { return static_cast<F&&>(f)(opt.value_); }
- template <typename F>
- static constexpr decltype(auto) apply(optional<> const&, F&& f)
- { return static_cast<F&&>(f)(); }
- };
- //////////////////////////////////////////////////////////////////////////
- // Searchable
- //////////////////////////////////////////////////////////////////////////
- namespace detail {
- template <bool>
- struct optional_find_if {
- template <typename T>
- static constexpr auto apply(T const&)
- { return hana::nothing; }
- };
- template <>
- struct optional_find_if<true> {
- template <typename T>
- static constexpr auto apply(T&& t)
- { return hana::just(static_cast<T&&>(t)); }
- };
- }
- template <>
- struct find_if_impl<optional_tag> {
- template <typename T, typename Pred>
- static constexpr auto apply(hana::optional<T> const& opt, Pred&& pred) {
- constexpr bool found = decltype(static_cast<Pred&&>(pred)(opt.value_))::value;
- return detail::optional_find_if<found>::apply(opt.value_);
- }
- template <typename T, typename Pred>
- static constexpr auto apply(hana::optional<T>& opt, Pred&& pred) {
- constexpr bool found = decltype(static_cast<Pred&&>(pred)(opt.value_))::value;
- return detail::optional_find_if<found>::apply(opt.value_);
- }
- template <typename T, typename Pred>
- static constexpr auto apply(hana::optional<T>&& opt, Pred&& pred) {
- constexpr bool found = decltype(
- static_cast<Pred&&>(pred)(static_cast<T&&>(opt.value_))
- )::value;
- return detail::optional_find_if<found>::apply(static_cast<T&&>(opt.value_));
- }
- template <typename Pred>
- static constexpr auto apply(hana::optional<> const&, Pred&&)
- { return hana::nothing; }
- };
- template <>
- struct any_of_impl<optional_tag> {
- template <typename T, typename Pred>
- static constexpr auto apply(hana::optional<T> const& opt, Pred&& pred)
- { return static_cast<Pred&&>(pred)(opt.value_); }
- template <typename Pred>
- static constexpr hana::false_ apply(hana::optional<> const&, Pred&&)
- { return {}; }
- };
- }} // end namespace boost::hana
- #endif // !BOOST_HANA_OPTIONAL_HPP
|