/* Says how to convert value, error and exception types (C) 2017-2019 Niall Douglas (12 commits) File Created: Nov 2017 Boost Software License - Version 1.0 - August 17th, 2003 Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute, and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the Software is furnished to do so, all subject to the following: The copyright notices in the Software and this entire statement, including the above license grant, this restriction and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all derivative works of the Software, unless such copies or derivative works are solely in the form of machine-executable object code generated by a source language processor. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef BOOST_OUTCOME_CONVERT_HPP #define BOOST_OUTCOME_CONVERT_HPP #include "detail/basic_result_storage.hpp" BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN namespace convert { #if defined(__cpp_concepts) #if !defined(_MSC_VER) && !defined(__clang__) && __GNUC__ < 9 #define BOOST_OUTCOME_GCC6_CONCEPT_BOOL bool #else #define BOOST_OUTCOME_GCC6_CONCEPT_BOOL #endif namespace detail { template concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL SameHelper = std::is_same::value; template concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL same_as = detail::SameHelper &&detail::SameHelper; } // namespace detail /* The `ValueOrNone` concept. \requires That `U::value_type` exists and that `std::declval().has_value()` returns a `bool` and `std::declval().value()` exists. */ template concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL ValueOrNone = requires(U a) { { a.has_value() } ->detail::same_as; {a.value()}; }; /* The `ValueOrError` concept. \requires That `U::value_type` and `U::error_type` exist; that `std::declval().has_value()` returns a `bool`, `std::declval().value()` and `std::declval().error()` exists. */ template concept BOOST_OUTCOME_GCC6_CONCEPT_BOOL ValueOrError = requires(U a) { { a.has_value() } ->detail::same_as; {a.value()}; {a.error()}; }; #else namespace detail { struct no_match { }; inline no_match match_value_or_none(...); inline no_match match_value_or_error(...); BOOST_OUTCOME_TEMPLATE(class U) BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval().has_value()), BOOST_OUTCOME_TEXPR(std::declval().value())) inline U match_value_or_none(U &&); BOOST_OUTCOME_TEMPLATE(class U) BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TEXPR(std::declval().has_value()), BOOST_OUTCOME_TEXPR(std::declval().value()), BOOST_OUTCOME_TEXPR(std::declval().error())) inline U match_value_or_error(U &&); template static constexpr bool ValueOrNone = !std::is_same>()))>::value; template static constexpr bool ValueOrError = !std::is_same>()))>::value; } // namespace detail /* The `ValueOrNone` concept. \requires That `U::value_type` exists and that `std::declval().has_value()` returns a `bool` and `std::declval().value()` exists. */ template static constexpr bool ValueOrNone = detail::ValueOrNone; /* The `ValueOrError` concept. \requires That `U::value_type` and `U::error_type` exist; that `std::declval().has_value()` returns a `bool`, `std::declval().value()` and `std::declval().error()` exists. */ template static constexpr bool ValueOrError = detail::ValueOrError; #endif namespace detail { template struct make_type { template static constexpr T value(U &&v) { return T{in_place_type, static_cast(v).value()}; } template static constexpr T error(U &&v) { return T{in_place_type, static_cast(v).error()}; } static constexpr T error() { return T{in_place_type}; } }; template struct make_type { template static constexpr T value(U && /*unused*/) { return T{in_place_type}; } template static constexpr T error(U && /*unused*/) { return T{in_place_type}; } static constexpr T error() { return T{in_place_type}; } }; } // namespace detail /*! AWAITING HUGO JSON CONVERSION TOOL type definition value_or_error. Potential doc page: NOT FOUND */ template struct value_or_error { static constexpr bool enable_result_inputs = false; static constexpr bool enable_outcome_inputs = false; BOOST_OUTCOME_TEMPLATE(class X) BOOST_OUTCOME_TREQUIRES(BOOST_OUTCOME_TPRED(std::is_same>::value // &&ValueOrError // && (std::is_void::value_type>::value || BOOST_OUTCOME_V2_NAMESPACE::detail::is_explicitly_constructible::value_type>) // &&(std::is_void::error_type>::value || BOOST_OUTCOME_V2_NAMESPACE::detail::is_explicitly_constructible::error_type>) )) constexpr T operator()(X &&v) { return v.has_value() ? detail::make_type::value(static_cast(v)) : detail::make_type::error(static_cast(v)); } }; } // namespace convert BOOST_OUTCOME_V2_NAMESPACE_END #endif