Skip to content

Commit

Permalink
feat: Add operator bool and swap to ServerRPCPtr
Browse files Browse the repository at this point in the history
  • Loading branch information
Tradias committed Apr 2, 2024
1 parent bfc59cd commit 8d27a4f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Using [Boost.Asio](https://www.boost.org/doc/libs/1_84_0/doc/html/boost_asio.htm

```cmake
# Make sure CMAKE_PREFIX_PATH contains /desired/installation/directory
find_package(asio-grpc)
find_package(asio-grpc CONFIG REQUIRED)
find_package(Boost)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc Boost::headers)
```
Expand All @@ -123,7 +123,7 @@ Or using [standalone Asio](https://github.com/chriskohlhoff/asio):

```cmake
# Make sure CMAKE_PREFIX_PATH contains /desired/installation/directory
find_package(asio-grpc)
find_package(asio-grpc CONFIG REQUIRED)
find_package(asio)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-standalone-asio asio::asio)
```
Expand All @@ -132,7 +132,7 @@ Or using [libunifex](https://github.com/facebookexperimental/libunifex):

```cmake
# Make sure CMAKE_PREFIX_PATH contains /desired/installation/directory
find_package(asio-grpc)
find_package(asio-grpc CONFIG REQUIRED)
find_package(unifex)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-unifex unifex::unifex)
```
Expand All @@ -141,7 +141,7 @@ Or using [stdexec](https://github.com/NVIDIA/stdexec):

```cmake
# Make sure CMAKE_PREFIX_PATH contains /desired/installation/directory
find_package(asio-grpc)
find_package(asio-grpc CONFIG REQUIRED)
find_package(stdexec)
target_link_libraries(your_app PUBLIC asio-grpc::asio-grpc-stdexec STDEXEC::stdexec)
```
Expand Down
20 changes: 19 additions & 1 deletion src/agrpc/server_rpc_ptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ServerRPCPtr
/**
* @brief Default constructor
*
* The only valid operations after construction are move-assignment and destruction.
* The only valid operations after construction are move-assignment, operator bool, swap and destruction.
*/
ServerRPCPtr() = default;

Expand Down Expand Up @@ -83,6 +83,13 @@ class ServerRPCPtr
*/
const ServerRPCT* operator->() const noexcept { return &server_rpc_->rpc_; }

/**
* @brief Checks whether this pointer owns a ServerRPC
*
* @since 3.1.0
*/
explicit operator bool() const noexcept { return server_rpc_ != nullptr; }

/**
* @brief Get client's initial request message
*/
Expand All @@ -93,6 +100,17 @@ class ServerRPCPtr
*/
decltype(auto) request() const noexcept { return (server_rpc_->request_); }

/**
* @brief Swap contents of two ServerRPCPtr
*
* @since 3.1.0
*/
friend void swap(ServerRPCPtr& lhs, ServerRPCPtr& rhs) noexcept
{
std::swap(lhs.server_rpc_, rhs.server_rpc_);
std::swap(lhs.deleter_, rhs.deleter_);
}

private:
using Pointer = detail::ServerRPCWithRequest<ServerRPCT>*;
using Deleter = void (*)(Pointer) noexcept;
Expand Down
10 changes: 8 additions & 2 deletions test/src/test_server_rpc_17.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,13 +609,19 @@ TEST_CASE_TEMPLATE("ServerRPCPtr automatic cancellation on destruction", RPC, te
});
}

TEST_CASE_FIXTURE(ServerRPCTest<test::ClientStreamingServerRPC>, "ServerRPCPtr move-assignment")
TEST_CASE_FIXTURE(ServerRPCTest<test::ClientStreamingServerRPC>, "ServerRPCPtr move-assignment/swap")
{
ServerRPC::Ptr ptr;
register_callback_and_perform_requests(
[&](ServerRPC::Ptr pointer)
{
ptr = std::move(pointer);
SUBCASE("move") { ptr = std::move(pointer); }
SUBCASE("swap")
{
using std::swap;
swap(ptr, pointer);
CHECK_FALSE(pointer);
}
auto& rpc = *ptr;
rpc.finish({}, test::create_already_exists_status(),
[ptr = std::move(ptr)](bool ok)
Expand Down

0 comments on commit 8d27a4f

Please sign in to comment.