Add support for stable coroutines (/await:strict and -fcoroutines)

* The definition CONTINUABLE_HAS_COROUTINE now indicates support for coroutines,
  while CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE is added if the experimental version is used.
This commit is contained in:
Denis Blank 2022-01-20 07:54:45 +01:00
parent 2fcc2bf281
commit 01f9dbd1f4
6 changed files with 175 additions and 139 deletions

View File

@ -1,4 +1,3 @@
# Copyright(c) 2015 - 2020 Denis Blank <denis.blank at outlook dot com> # Copyright(c) 2015 - 2020 Denis Blank <denis.blank at outlook dot com>
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy # Permission is hereby granted, free of charge, to any person obtaining a copy
@ -8,8 +7,8 @@
# copies of the Software, and to permit persons to whom the Software is # copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions : # furnished to do so, subject to the following conditions :
# #
# The above copyright notice and this permission notice shall be included in # The above copyright notice and this permission notice shall be included in all
# all copies or substantial portions of the Software. # copies or substantial portions of the Software.
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
@ -21,7 +20,10 @@
cmake_minimum_required(VERSION 3.11) cmake_minimum_required(VERSION 3.11)
project(continuable VERSION 4.0.0 LANGUAGES C CXX) project(
continuable
VERSION 4.0.0
LANGUAGES C CXX)
if(CTI_CONTINUABLE_IS_FIND_INCLUDED) if(CTI_CONTINUABLE_IS_FIND_INCLUDED)
set(CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT OFF) set(CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT OFF)
@ -30,47 +32,47 @@ else()
CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT) CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT)
endif() endif()
option(CTI_CONTINUABLE_WITH_INSTALL if(CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT)
"Add the continuable install targets" message(
STATUS
"Building with ${CMAKE_CXX_COMPILER_ID} (${CMAKE_CXX_COMPILER_VERSION})")
endif()
option(CTI_CONTINUABLE_WITH_INSTALL "Add the continuable install targets"
${CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT}) ${CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT})
option(CTI_CONTINUABLE_WITH_TESTS option(CTI_CONTINUABLE_WITH_TESTS "Build the continuable unit tests"
"Build the continuable unit tests"
${CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT}) ${CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT})
option(CTI_CONTINUABLE_WITH_EXAMPLES option(CTI_CONTINUABLE_WITH_EXAMPLES "Build the continuable examples"
"Build the continuable examples"
${CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT}) ${CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT})
option(CTI_CONTINUABLE_WITH_BENCHMARKS option(CTI_CONTINUABLE_WITH_BENCHMARKS "Build the continuable benchmarks" OFF)
"Build the continuable benchmarks"
OFF)
option(CTI_CONTINUABLE_WITH_NO_EXCEPTIONS option(CTI_CONTINUABLE_WITH_NO_EXCEPTIONS "Disable exception support" OFF)
"Disable exception support"
OFF)
option(CTI_CONTINUABLE_WITH_UNHANDLED_EXCEPTIONS option(CTI_CONTINUABLE_WITH_UNHANDLED_EXCEPTIONS
"Enable unhandled asynchronous exceptions" "Enable unhandled asynchronous exceptions" OFF)
OFF)
option(CTI_CONTINUABLE_WITH_COROUTINE "Enable C++20 coroutines" OFF)
option(CTI_CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE option(CTI_CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE
"Enable co_await support" "Enable experimental coroutines" OFF)
OFF)
option(CTI_CONTINUABLE_WITH_CPP_LATEST option(CTI_CONTINUABLE_WITH_CPP_LATEST
"Enable the highest C++ standard available for testing polyfills" "Enable the highest C++ standard available for testing polyfills" OFF)
OFF)
option(CTI_CONTINUABLE_WITH_LIGHT_TESTS option(CTI_CONTINUABLE_WITH_LIGHT_TESTS
"Disable some template heavy unit tests (for CI usage)" "Disable some template heavy unit tests (for CI usage)" OFF)
OFF)
# Top level project settings only # Top level project settings only
if(CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT) if(CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT)
set(CTI_CONTINUABLE_WITH_CONCURRENT_JOBS set(CTI_CONTINUABLE_WITH_CONCURRENT_JOBS
"0" CACHE STRING "0"
"Set the number of concurrent compilation jobs (0 = unlimited, for CI usage)") CACHE
STRING
"Set the number of concurrent compilation jobs (0 = unlimited, for CI usage)"
)
else() else()
set(CTI_CONTINUABLE_WITH_CONCURRENT_JOBS "0") set(CTI_CONTINUABLE_WITH_CONCURRENT_JOBS "0")
endif() endif()
@ -98,18 +100,16 @@ endif()
add_library(continuable::continuable-base ALIAS continuable-base) add_library(continuable::continuable-base ALIAS continuable-base)
target_include_directories(continuable-base target_include_directories(
INTERFACE continuable-base
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include> INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
$<INSTALL_INTERFACE:include>) $<INSTALL_INTERFACE:include>)
target_link_libraries(continuable-base target_link_libraries(continuable-base INTERFACE Threads::Threads)
INTERFACE
Threads::Threads)
target_compile_features(continuable-base target_compile_features(
INTERFACE continuable-base
cxx_alias_templates INTERFACE cxx_alias_templates
cxx_auto_type cxx_auto_type
cxx_constexpr cxx_constexpr
cxx_decltype cxx_decltype
@ -124,26 +124,29 @@ target_compile_features(continuable-base
cxx_return_type_deduction) cxx_return_type_deduction)
if(CTI_CONTINUABLE_WITH_CPP_LATEST) if(CTI_CONTINUABLE_WITH_CPP_LATEST)
target_compile_features(continuable-base target_compile_features(continuable-base INTERFACE cxx_std_20)
INTERFACE
cxx_std_17)
endif() endif()
if (CTI_CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE) if(CTI_CONTINUABLE_WITH_COROUTINE)
target_compile_options(continuable-base if(NOT CTI_CONTINUABLE_WITH_CPP_LATEST)
INTERFACE message(FATAL_ERROR "CTI_CONTINUABLE_WITH_COROUTINE requires "
$<$<CXX_COMPILER_ID:MSVC>:/await> "CTI_CONTINUABLE_WITH_CPP_LATEST!")
$<$<CXX_COMPILER_ID:Clang>:-fcoroutines-ts>) endif()
target_compile_definitions(continuable-base target_compile_options(
INTERFACE continuable-base
CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE) INTERFACE $<$<CXX_COMPILER_ID:MSVC>:/await:strict>
$<$<CXX_COMPILER_ID:Clang>:-fcoroutines-ts>
$<$<CXX_COMPILER_ID:GNU>:-fcoroutines>)
elseif(CTI_CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE)
target_compile_options(
continuable-base INTERFACE $<$<CXX_COMPILER_ID:MSVC>:/await>
$<$<CXX_COMPILER_ID:Clang>:-fcoroutines-ts>)
endif() endif()
if(CTI_CONTINUABLE_WITH_UNHANDLED_EXCEPTIONS) if(CTI_CONTINUABLE_WITH_UNHANDLED_EXCEPTIONS)
target_compile_definitions(continuable-base target_compile_definitions(continuable-base
INTERFACE INTERFACE CONTINUABLE_WITH_UNHANDLED_EXCEPTIONS)
CONTINUABLE_WITH_UNHANDLED_EXCEPTIONS)
endif() endif()
if(CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT) if(CTI_CONTINUABLE_IS_TOP_LEVEL_PROJECT)
@ -154,9 +157,7 @@ endif()
add_library(continuable::continuable ALIAS continuable) add_library(continuable::continuable ALIAS continuable)
target_link_libraries(continuable target_link_libraries(continuable INTERFACE continuable::continuable-base
INTERFACE
continuable::continuable-base
function2::function2) function2::function2)
if(CTI_CONTINUABLE_WITH_INSTALL) if(CTI_CONTINUABLE_WITH_INSTALL)
@ -164,8 +165,7 @@ if (CTI_CONTINUABLE_WITH_INSTALL)
include(GNUInstallDirs) include(GNUInstallDirs)
include(CMakePackageConfigHelpers) include(CMakePackageConfigHelpers)
# Create an install target: # Create an install target: Headers and license files
# Headers and license files
install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/continuable" install(DIRECTORY "${PROJECT_SOURCE_DIR}/include/continuable"
DESTINATION "include") DESTINATION "include")
install(FILES "LICENSE.txt" DESTINATION .) install(FILES "LICENSE.txt" DESTINATION .)
@ -180,7 +180,8 @@ if (CTI_CONTINUABLE_WITH_INSTALL)
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
# ConfigVersion.cmake # ConfigVersion.cmake
configure_package_config_file("cmake/config.cmake.in" configure_package_config_file(
"cmake/config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
# PATH_VARS INCLUDE_INSTALL_DIR SYSCONFIG_INSTALL_DIR # PATH_VARS INCLUDE_INSTALL_DIR SYSCONFIG_INSTALL_DIR
@ -189,13 +190,17 @@ if (CTI_CONTINUABLE_WITH_INSTALL)
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
# Targets.cmake # Targets.cmake
export(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}-base export(
TARGETS ${PROJECT_NAME} ${PROJECT_NAME}-base
NAMESPACE ${PROJECT_NAME}:: NAMESPACE ${PROJECT_NAME}::
FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake") FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake")
install(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}-base install(
TARGETS ${PROJECT_NAME} ${PROJECT_NAME}-base
EXPORT "${PROJECT_NAME}Targets"
INCLUDES
DESTINATION "include")
install(
EXPORT "${PROJECT_NAME}Targets" EXPORT "${PROJECT_NAME}Targets"
INCLUDES DESTINATION "include")
install(EXPORT "${PROJECT_NAME}Targets"
NAMESPACE ${PROJECT_NAME}:: NAMESPACE ${PROJECT_NAME}::
DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")

View File

@ -47,10 +47,9 @@
#include <continuable/detail/utility/traits.hpp> #include <continuable/detail/utility/traits.hpp>
#include <continuable/detail/utility/util.hpp> #include <continuable/detail/utility/util.hpp>
#ifdef CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE #if defined(CONTINUABLE_HAS_COROUTINE)
# include <experimental/coroutine>
# include <continuable/detail/other/coroutines.hpp> # include <continuable/detail/other/coroutines.hpp>
#endif // CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE #endif // defined(CONTINUABLE_HAS_COROUTINE)
namespace cti { namespace cti {
/// \defgroup Base Base /// \defgroup Base Base
@ -715,7 +714,7 @@ public:
} }
/// \cond false /// \cond false
#ifdef CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE #if defined(CONTINUABLE_HAS_COROUTINE)
/// \endcond /// \endcond
/// Implements the operator for awaiting on continuables using `co_await`. /// Implements the operator for awaiting on continuables using `co_await`.
/// ///
@ -781,7 +780,7 @@ public:
return detail::awaiting::create_awaiter(std::move(*this).finish()); return detail::awaiting::create_awaiter(std::move(*this).finish());
} }
/// \cond false /// \cond false
#endif // CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE #endif // defined(CONTINUABLE_HAS_COROUTINE)
/// \endcond /// \endcond
private: private:

View File

@ -39,11 +39,9 @@
# include <exception> # include <exception>
#endif // CONTINUABLE_HAS_EXCEPTIONS #endif // CONTINUABLE_HAS_EXCEPTIONS
#ifdef CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE #if defined(CONTINUABLE_HAS_COROUTINE)
# include <continuable/detail/other/coroutines.hpp> # include <continuable/detail/other/coroutines.hpp>
#endif // CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE
#if defined(CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE)
namespace cti { namespace cti {
# if defined(CONTINUABLE_HAS_EXCEPTIONS) # if defined(CONTINUABLE_HAS_EXCEPTIONS)
/// Is thrown from co_await expressions if the awaited continuable is canceled /// Is thrown from co_await expressions if the awaited continuable is canceled
@ -78,7 +76,9 @@ using await_canceled_exception = detail::awaiting::await_canceled_exception;
// As far as I know there is no other way to implement this specialization... // As far as I know there is no other way to implement this specialization...
// NOLINTNEXTLINE(cert-dcl58-cpp) // NOLINTNEXTLINE(cert-dcl58-cpp)
namespace std { namespace std {
# if defined(CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE)
namespace experimental { namespace experimental {
# endif // defined(CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE)
template <typename Data, typename... Args, typename... FunctionArgs> template <typename Data, typename... Args, typename... FunctionArgs>
struct coroutine_traits< struct coroutine_traits<
cti::continuable_base<Data, cti::detail::identity<Args...>>, cti::continuable_base<Data, cti::detail::identity<Args...>>,
@ -87,9 +87,11 @@ struct coroutine_traits<
using promise_type = cti::detail::awaiting::promise_type< using promise_type = cti::detail::awaiting::promise_type<
cti::continuable<Args...>, cti::promise<Args...>, Args...>; cti::continuable<Args...>, cti::promise<Args...>, Args...>;
}; };
# if defined(CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE)
} // namespace experimental } // namespace experimental
# endif // defined(CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE)
} // namespace std } // namespace std
/// \endcond /// \endcond
#endif // CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE #endif // defined(CONTINUABLE_HAS_COROUTINE)
#endif // CONTINUABLE_COROUTINE_HPP_INCLUDED #endif // CONTINUABLE_COROUTINE_HPP_INCLUDED

View File

@ -86,24 +86,40 @@
#endif #endif
#endif #endif
/// Usually this is enabled by the CMake project // Automatically detects support for coroutines.
#if !defined(CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE) // Parts of this detection mechanism were adapted from boost::asio,
// with support added for experimental coroutines.
#if !defined(CONTINUABLE_HAS_DISABLED_COROUTINE) \
&& !defined(CONTINUABLE_HAS_COROUTINE)
/// Define CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE when /// Define CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE when
/// CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE is defined. /// CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE is defined.
#if defined(CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE) #if defined(CONTINUABLE_WITH_EXPERIMENTAL_COROUTINE)
#define CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE #define CONTINUABLE_HAS_COROUTINE 1
#elif defined(_MSC_VER) #elif defined(CONTINUABLE_WITH_COROUTINE)
#if _MSC_FULL_VER >= 190023506 #define CONTINUABLE_HAS_COROUTINE 1
#define CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE 1
#elif defined(_MSC_VER) // Visual Studio
#if (_MSC_VER >= 1928) && (_MSVC_LANG >= 201705)
#define CONTINUABLE_HAS_COROUTINE 1
#elif _MSC_FULL_VER >= 190023506
#if defined(_RESUMABLE_FUNCTIONS_SUPPORTED) #if defined(_RESUMABLE_FUNCTIONS_SUPPORTED)
#define CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE #define CONTINUABLE_HAS_COROUTINE 1
#define CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE 1
#endif // defined(_RESUMABLE_FUNCTIONS_SUPPORTED) #endif // defined(_RESUMABLE_FUNCTIONS_SUPPORTED)
#endif // _MSC_FULL_VER >= 190023506 #endif // _MSC_FULL_VER >= 190023506
#elif defined(__clang__) #elif defined(__clang__) // Clang
#if defined(__cpp_coroutines) && (__cpp_coroutines >= 201707) #if defined(__cpp_coroutines) && (__cpp_coroutines >= 201707)
#define CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE #define CONTINUABLE_HAS_COROUTINE 1
#define CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE 1
#endif // defined(__cpp_coroutines) && (__cpp_coroutines >= 201707) #endif // defined(__cpp_coroutines) && (__cpp_coroutines >= 201707)
#endif // defined(__clang__) #elif defined(__GNUC__) // GCC
#endif // !defined(CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE) #if (__cplusplus >= 201709) && (__cpp_impl_coroutine >= 201902)
#if __has_include(<coroutine>)
#define CONTINUABLE_HAS_COROUTINE 1
#endif // __has_include(<coroutine>)
#endif // (__cplusplus >= 201709) && (__cpp_impl_coroutine >= 201902)
#endif
#endif
/// Define CONTINUABLE_HAS_EXCEPTIONS when exceptions are used /// Define CONTINUABLE_HAS_EXCEPTIONS when exceptions are used
#if !defined(CONTINUABLE_WITH_CUSTOM_ERROR_TYPE) && \ #if !defined(CONTINUABLE_WITH_CUSTOM_ERROR_TYPE) && \

View File

@ -34,7 +34,6 @@
#include <cassert> #include <cassert>
#include <type_traits> #include <type_traits>
#include <experimental/coroutine>
#include <continuable/continuable-primitives.hpp> #include <continuable/continuable-primitives.hpp>
#include <continuable/continuable-result.hpp> #include <continuable/continuable-result.hpp>
#include <continuable/detail/core/annotation.hpp> #include <continuable/detail/core/annotation.hpp>
@ -48,11 +47,24 @@
# include <exception> # include <exception>
#endif // CONTINUABLE_HAS_EXCEPTIONS #endif // CONTINUABLE_HAS_EXCEPTIONS
#if defined(CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE)
# include <experimental/coroutine>
#elif defined(CONTINUABLE_HAS_COROUTINE)
# include <coroutine>
#endif
#if defined(CONTINUABLE_HAS_COROUTINE)
namespace cti { namespace cti {
namespace detail { namespace detail {
namespace awaiting { namespace awaiting {
/// We import the coroutine handle in our namespace /// We import the coroutine handle in our namespace
# if defined(CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE)
using std::experimental::coroutine_handle; using std::experimental::coroutine_handle;
using std::experimental::suspend_never;
# else
using std::coroutine_handle;
using std::suspend_never;
# endif
# if defined(CONTINUABLE_HAS_EXCEPTIONS) # if defined(CONTINUABLE_HAS_EXCEPTIONS)
class await_canceled_exception : public std::exception { class await_canceled_exception : public std::exception {
@ -213,7 +225,7 @@ struct promise_type
return {handle_}; return {handle_};
} }
std::experimental::suspend_never final_suspend() { suspend_never final_suspend() noexcept {
return {}; return {};
} }
@ -235,5 +247,6 @@ struct promise_type
} // namespace awaiting } // namespace awaiting
} // namespace detail } // namespace detail
} // namespace cti } // namespace cti
#endif // defined(CONTINUABLE_HAS_COROUTINE)
#endif // CONTINUABLE_DETAIL_UTIL_HPP_INCLUDED #endif // CONTINUABLE_DETAIL_UTIL_HPP_INCLUDED

View File

@ -24,7 +24,8 @@
#include <test-continuable.hpp> #include <test-continuable.hpp>
#include <continuable/detail/features.hpp> #include <continuable/detail/features.hpp>
#ifdef CONTINUABLE_HAS_EXPERIMENTAL_COROUTINE
#ifdef CONTINUABLE_HAS_COROUTINE
# ifndef CONTINUABLE_WITH_NO_EXCEPTIONS # ifndef CONTINUABLE_WITH_NO_EXCEPTIONS
# include <exception> # include <exception>