diff --git a/include/continuable/detail/support/asio.hpp b/include/continuable/detail/support/asio.hpp new file mode 100644 index 0000000..850ea5f --- /dev/null +++ b/include/continuable/detail/support/asio.hpp @@ -0,0 +1,108 @@ +/* + + /~` _ _ _|_. _ _ |_ | _ + \_,(_)| | | || ||_|(_||_)|(/_ + + https://github.com/Naios/continuable + v4.0.0 + + Copyright(c) 2015 - 2019 Denis Blank + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files(the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and / or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions : + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + 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 AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +**/ + +#ifndef CONTINUABLE_DETAIL_ASIO_HPP_INCLUDED +#define CONTINUABLE_DETAIL_ASIO_HPP_INCLUDED + +#if defined(ASIO_STANDALONE) +#include +#include +#include + +#define CTI_ASIO_NAMESPACE_BEGIN namespace asio { +#define CTI_ASIO_NAMESPACE_END } +#else +#include +#include +#include + +#define CTI_ASIO_NAMESPACE_BEGIN namespace boost { namespace asio { +#define CTI_ASIO_NAMESPACE_END }} +#endif + +#include + +namespace cti { +namespace detail { +namespace asio { + +#if defined(ASIO_STANDALONE) +using error_code_t = ::asio::error_code; +using exception_t = ::asio::system_error; +#else +using error_code_t = ::boost::system::error_code; +using exception_t = ::boost::system::system_error; +#endif + +// Binds `promise` to the first argument of a continuable resolver, giving it +// the signature of an ASIO handler. +template +auto promise_resolver_handler(Promise&& promise) noexcept { + return [promise = std::forward(promise)]( + error_code_t e, auto&&... args) mutable noexcept { + if (e) { + promise.set_exception(std::make_exception_ptr(exception_t(std::move(e)))); + } else { + promise.set_value(std::forward(args)...); + } + }; +} + +// Type deduction helper for `Result` in `cti::make_continuable` +template +struct continuable_result; + +template <> +struct continuable_result { + using type = void; +}; + +template <> +struct continuable_result + : continuable_result {}; + +template +struct continuable_result { + using type = T; +}; + +template +struct continuable_result + : continuable_result {}; + +template +using continuable_result_t = typename continuable_result::type; + + + +} // namespace asio +} // namespace detail +} // namespace cti + +#endif // CONTINUABLE_DETAIL_ASIO_HPP_INCLUDED diff --git a/include/continuable/support/asio.hpp b/include/continuable/support/asio.hpp new file mode 100644 index 0000000..a2a0c83 --- /dev/null +++ b/include/continuable/support/asio.hpp @@ -0,0 +1,70 @@ +/* + + /~` _ _ _|_. _ _ |_ | _ + \_,(_)| | | || ||_|(_||_)|(/_ + + https://github.com/Naios/continuable + v4.0.0 + + Copyright(c) 2015 - 2019 Denis Blank + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files(the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and / or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions : + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + 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 AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +**/ + +#ifndef CONTINUABLE_SUPPORT_ASIO_HPP_INCLUDED +#define CONTINUABLE_SUPPORT_ASIO_HPP_INCLUDED + +#include +#include +#include + +CTI_ASIO_NAMESPACE_BEGIN + +// Class used to specify an asynchronous operation should return a continuable. +struct use_cti_t {}; + +// Special value for instance of `use_cti_t`. +constexpr use_cti_t use_cti{}; + +template +class async_result { +public: + template + static auto initiate(Initiation initiation, use_cti_t, Args... args) { + return cti::make_continuable< + cti::detail::asio::continuable_result_t>( + [initiation = std::move(initiation), + init_args = + std::make_tuple(std::move(args)...)](auto&& promise) mutable { + cti::detail::traits::unpack( + [initiation = std::move(initiation), + handler = cti::detail::asio::promise_resolver_handler( + std::forward(promise))]( + auto&&... args) mutable { + std::move(initiation)(std::move(handler), + std::forward(args)...); + }, + std::move(init_args)); + }); + } +}; + +CTI_ASIO_NAMESPACE_END + +#endif // CONTINUABLE_SUPPORT_ASIO_HPP_INCLUDED