Skip to content

Commit

Permalink
fix: Disable C++20 coroutine features based on BOOST_ASIO_HAS_CO_AWAIT
Browse files Browse the repository at this point in the history
  • Loading branch information
Tradias committed Sep 9, 2021
1 parent 23abb2a commit 1f655c2
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 18 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ add_subdirectory(src)
if(ASIO_GRPC_BUILD_TESTS OR ASIO_GRPC_BUILD_EXAMPLES OR ASIO_GRPC_BUILD_BENCHMARK)
find_package(Boost REQUIRED COMPONENTS coroutine thread)
include(AsioGrpcCompileOptions)
include(AsioGrpcCheckBoostVersion)
endif()

if(ASIO_GRPC_BUILD_TESTS)
Expand Down
6 changes: 4 additions & 2 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ function(asio_grpc_add_benchmark _asio_grpc_name)
${CMAKE_CURRENT_LIST_DIR})
endfunction()

asio_grpc_add_benchmark(benchmark-cpp20)
target_link_libraries(asio-grpc-benchmark-cpp20 PRIVATE asio-grpc-cpp20-compile-options)
if(ASIO_GRPC_BOOST_ASIO_HAS_CO_AWAIT)
asio_grpc_add_benchmark(benchmark-cpp20)
target_link_libraries(asio-grpc-benchmark-cpp20 PRIVATE asio-grpc-cpp20-compile-options)
endif()
4 changes: 4 additions & 0 deletions cmake/AsioGrpcCheckBoostVersion.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
try_compile(
ASIO_GRPC_BOOST_ASIO_HAS_CO_AWAIT "${CMAKE_CURRENT_BINARY_DIR}"
"${CMAKE_CURRENT_LIST_DIR}/check_boost_asio_has_co_await.cpp"
LINK_LIBRARIES Boost::headers CXX_STANDARD 20 CXX_STANDARD_REQUIRED on)
22 changes: 22 additions & 0 deletions cmake/check_boost_asio_has_co_await.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2021 Dennis Hezel
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <boost/asio/awaitable.hpp>

int main()
{
#ifndef BOOST_ASIO_HAS_CO_AWAIT
static_assert(false, "");
#endif
}
10 changes: 6 additions & 4 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ target_link_libraries(asio-grpc-example-server PRIVATE Boost::coroutine Boost::t
asio_grpc_add_example(example-client)
target_link_libraries(asio-grpc-example-client PRIVATE Boost::coroutine Boost::thread)

asio_grpc_add_example(hello-world-server-cpp20)
target_link_libraries(asio-grpc-hello-world-server-cpp20 PRIVATE asio-grpc-cpp20-compile-options)
if(ASIO_GRPC_BOOST_ASIO_HAS_CO_AWAIT)
asio_grpc_add_example(hello-world-server-cpp20)
target_link_libraries(asio-grpc-hello-world-server-cpp20 PRIVATE asio-grpc-cpp20-compile-options)

asio_grpc_add_example(hello-world-client-cpp20)
target_link_libraries(asio-grpc-hello-world-client-cpp20 PRIVATE asio-grpc-cpp20-compile-options)
asio_grpc_add_example(hello-world-client-cpp20)
target_link_libraries(asio-grpc-hello-world-client-cpp20 PRIVATE asio-grpc-cpp20-compile-options)
endif()
2 changes: 2 additions & 0 deletions src/agrpc/detail/asioForward.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef AGRPC_DETAIL_ASIOFORWARD_HPP
#define AGRPC_DETAIL_ASIOFORWARD_HPP

#include <version>
//
#include <boost/asio/any_io_executor.hpp>
#include <boost/asio/associated_allocator.hpp>
#include <boost/asio/associated_executor.hpp>
Expand Down
8 changes: 3 additions & 5 deletions src/agrpc/initiate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@
#include "agrpc/detail/initiate.hpp"
#include "agrpc/grpcExecutor.hpp"

#include <version>

#ifdef __cpp_lib_coroutine
#ifdef BOOST_ASIO_HAS_CO_AWAIT
#include <boost/asio/use_awaitable.hpp>
#endif

namespace agrpc
{
#ifdef __cpp_lib_coroutine
#ifdef BOOST_ASIO_HAS_CO_AWAIT
template <class T>
using GrpcAwaitable = asio::awaitable<T, agrpc::GrpcExecutor>;

Expand Down Expand Up @@ -77,7 +75,7 @@ template <class CompletionToken>
return agrpc::get_completion_queue(executor);
}

#ifdef __cpp_impl_coroutine
#ifdef BOOST_ASIO_HAS_CO_AWAIT
template <class Executor = asio::any_io_executor>
[[nodiscard]] auto get_completion_queue(asio::use_awaitable_t<Executor> = {})
-> asio::async_result<asio::use_awaitable_t<Executor>, void(grpc::CompletionQueue*)>::return_type
Expand Down
4 changes: 1 addition & 3 deletions src/agrpc/rpcs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

#include <grpcpp/alarm.h>

#include <version>

namespace agrpc
{
template <class Deadline, class CompletionToken = agrpc::DefaultCompletionToken>
Expand Down Expand Up @@ -222,7 +220,7 @@ auto send_initial_metadata(Responder& responder, CompletionToken token = {})
/*
Client
*/
#ifdef __cpp_impl_coroutine
#ifdef BOOST_ASIO_HAS_CO_AWAIT
template <class RPC, class Stub, class Request, class Reader, class Executor = asio::any_io_executor>
[[nodiscard]] auto request(detail::ClientUnaryRequest<RPC, Request, Reader> rpc, Stub& stub,
grpc::ClientContext& client_context, const Request& request,
Expand Down
4 changes: 4 additions & 0 deletions test/test-asio-grpc-20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ using namespace agrpc;

TEST_SUITE_BEGIN(ASIO_GRPC_TEST_CPP_VERSION* doctest::timeout(180.0));

#ifdef BOOST_ASIO_HAS_CONCEPTS
TEST_CASE("GrpcExecutor fulfills Executor TS concepts")
{
CHECK(asio::execution::executor<agrpc::GrpcExecutor>);
CHECK(asio::execution::executor_of<agrpc::GrpcExecutor, asio::execution::invocable_archetype>);
}
#endif

#ifdef BOOST_ASIO_HAS_CO_AWAIT
TEST_CASE_FIXTURE(test::GrpcContextTest, "co_spawn two Alarms and await their ok")
{
bool ok1 = false;
Expand Down Expand Up @@ -244,6 +247,7 @@ TEST_CASE_FIXTURE(test::GrpcClientServerTest, "awaitable bidirectional streaming
});
grpc_context.run();
}
#endif

TEST_SUITE_END();
} // namespace test_asio_grpc_cpp20
7 changes: 3 additions & 4 deletions test/utils/asioUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@

#include <boost/type_traits/remove_cv_ref.hpp>

#include <type_traits>
#include <version>

#ifdef __cpp_lib_coroutine
#ifdef BOOST_ASIO_HAS_CO_AWAIT
#include <boost/asio/co_spawn.hpp>
#endif

#include <type_traits>

namespace agrpc::test
{
template <class Function, class Allocator>
Expand All @@ -30,7 +29,7 @@ template <class Function, class Allocator>
HandlerWithAssociatedAllocator(Function&&, Allocator&&)
-> HandlerWithAssociatedAllocator<boost::remove_cv_ref_t<Function>, boost::remove_cv_ref_t<Allocator>>;

#ifdef __cpp_lib_coroutine
#ifdef BOOST_ASIO_HAS_CO_AWAIT
template <class Executor, class Function>
auto co_spawn(Executor&& executor, Function function)
{
Expand Down

0 comments on commit 1f655c2

Please sign in to comment.