/* Storage for a very simple basic_result type (C) 2017-2022 Niall Douglas (6 commits) File Created: Oct 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_BASIC_RESULT_STORAGE_HPP #define BOOST_OUTCOME_BASIC_RESULT_STORAGE_HPP #include "../success_failure.hpp" #include "../trait.hpp" #include "value_storage.hpp" BOOST_OUTCOME_V2_NAMESPACE_EXPORT_BEGIN namespace detail { template class basic_result_storage; } // namespace detail namespace hooks { template constexpr inline uint16_t spare_storage(const detail::basic_result_storage *r) noexcept; template constexpr inline void set_spare_storage(detail::basic_result_storage *r, uint16_t v) noexcept; } // namespace hooks namespace policy { struct base; } // namespace policy namespace detail { template // class basic_result_storage { static_assert(trait::type_can_be_used_in_basic_result, "The type R cannot be used in a basic_result"); static_assert(trait::type_can_be_used_in_basic_result, "The type S cannot be used in a basic_result"); friend struct policy::base; template // friend class basic_result_storage; template friend class basic_result_final; template friend constexpr inline uint16_t hooks::spare_storage(const detail::basic_result_storage *r) noexcept; // NOLINT template friend constexpr inline void hooks::set_spare_storage(detail::basic_result_storage *r, uint16_t v) noexcept; // NOLINT struct disable_in_place_value_type { }; struct disable_in_place_error_type { }; protected: using _value_type = std::conditional_t::value, disable_in_place_value_type, R>; using _error_type = std::conditional_t::value, disable_in_place_error_type, EC>; using _state_type = value_storage_select_impl<_value_type, _error_type>; #ifdef BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE value_storage_trivial<_value_type, _error_type> _state; #else _state_type _state; #endif public: // Used by iostream support to access state _state_type &_iostreams_state() { return _state; } const _state_type &_iostreams_state() const { return _state; } protected: basic_result_storage() = default; basic_result_storage(const basic_result_storage &) = default; // NOLINT basic_result_storage(basic_result_storage &&) = default; // NOLINT basic_result_storage &operator=(const basic_result_storage &) = default; // NOLINT basic_result_storage &operator=(basic_result_storage &&) = default; // NOLINT ~basic_result_storage() = default; template constexpr explicit basic_result_storage(in_place_type_t<_value_type> _, Args &&... args) noexcept(detail::is_nothrow_constructible<_value_type, Args...>) : _state{_, static_cast(args)...} { } template constexpr basic_result_storage(in_place_type_t<_value_type> _, std::initializer_list il, Args &&... args) noexcept(detail::is_nothrow_constructible<_value_type, std::initializer_list, Args...>) : _state{_, il, static_cast(args)...} { } template constexpr explicit basic_result_storage(in_place_type_t<_error_type> _, Args &&... args) noexcept(detail::is_nothrow_constructible<_error_type, Args...>) : _state{_, static_cast(args)...} { } template constexpr basic_result_storage(in_place_type_t<_error_type> _, std::initializer_list il, Args &&... args) noexcept(detail::is_nothrow_constructible<_error_type, std::initializer_list, Args...>) : _state{_, il, static_cast(args)...} { } struct compatible_conversion_tag { }; template constexpr basic_result_storage(compatible_conversion_tag /*unused*/, const basic_result_storage &o) noexcept( detail::is_nothrow_constructible<_value_type, T> &&detail::is_nothrow_constructible<_error_type, U>) : _state(o._state) { } template constexpr basic_result_storage(compatible_conversion_tag /*unused*/, basic_result_storage &&o) noexcept( detail::is_nothrow_constructible<_value_type, T> &&detail::is_nothrow_constructible<_error_type, U>) : _state(static_cast(o._state)) { } struct make_error_code_compatible_conversion_tag { }; template constexpr basic_result_storage(make_error_code_compatible_conversion_tag /*unused*/, const basic_result_storage &o) noexcept( detail::is_nothrow_constructible<_value_type, T> &&noexcept(make_error_code(std::declval()))) : _state(o._state._status.have_value() ? _state_type(in_place_type<_value_type>, o._state._value) : _state_type(in_place_type<_error_type>, make_error_code(o._state._error))) { } template constexpr basic_result_storage(make_error_code_compatible_conversion_tag /*unused*/, basic_result_storage &&o) noexcept( detail::is_nothrow_constructible<_value_type, T> &&noexcept(make_error_code(std::declval()))) : _state(o._state._status.have_value() ? _state_type(in_place_type<_value_type>, static_cast(o._state._value)) : _state_type(in_place_type<_error_type>, make_error_code(static_cast(o._state._error)))) { } struct make_exception_ptr_compatible_conversion_tag { }; template constexpr basic_result_storage(make_exception_ptr_compatible_conversion_tag /*unused*/, const basic_result_storage &o) noexcept( detail::is_nothrow_constructible<_value_type, T> &&noexcept(make_exception_ptr(std::declval()))) : _state(o._state._status.have_value() ? _state_type(in_place_type<_value_type>, o._state._value) : _state_type(in_place_type<_error_type>, make_exception_ptr(o._state._error))) { } template constexpr basic_result_storage(make_exception_ptr_compatible_conversion_tag /*unused*/, basic_result_storage &&o) noexcept( detail::is_nothrow_constructible<_value_type, T> &&noexcept(make_exception_ptr(std::declval()))) : _state(o._state._status.have_value() ? _state_type(in_place_type<_value_type>, static_cast(o._state._value)) : _state_type(in_place_type<_error_type>, make_exception_ptr(static_cast(o._state._error)))) { } }; } // namespace detail BOOST_OUTCOME_V2_NAMESPACE_END #endif