cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
call_future.h
Go to the documentation of this file.
1/*
2 * Copyright 2023 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <chrono>
23#include <memory>
24#include <utility>
25
29
31
39template <typename Result>
41public:
50 explicit CallFuture(std::shared_ptr<impl::ICallFutureImpl> impl)
51 : impl_(std::move(impl)) {}
52
63 [[nodiscard]] Result get_result() {
64 const auto call_result = impl_->get_result();
65 return get_from_call_result(call_result);
66 }
67
80 [[nodiscard]] Result get_result_within(std::chrono::nanoseconds timeout) {
81 const auto call_result = impl_->get_result_within(timeout);
82 return get_from_call_result(call_result);
83 }
84
85private:
92 [[nodiscard]] static Result get_from_call_result(
93 const messages::CallResult& call_result) {
94 if (call_result.is_success()) {
95 return call_result.result_as<Result>();
96 }
97 throw ServerException(call_result.object(), call_result.zone());
98 }
99
101 std::shared_ptr<impl::ICallFutureImpl> impl_;
102};
103
109template <>
110class CallFuture<void> {
111public:
120 explicit CallFuture(std::shared_ptr<impl::ICallFutureImpl> impl)
121 : impl_(std::move(impl)) {}
122
128 void get_result() {
129 const auto call_result = impl_->get_result();
130 get_from_call_result(call_result);
131 }
132
141 void get_result_within(std::chrono::nanoseconds timeout) {
142 const auto call_result = impl_->get_result_within(timeout);
143 get_from_call_result(call_result);
144 }
145
146private:
152 static void get_from_call_result(const messages::CallResult& call_result) {
153 if (call_result.is_success()) {
154 return;
155 }
156 throw ServerException(call_result.object(), call_result.zone());
157 }
158
160 std::shared_ptr<impl::ICallFutureImpl> impl_;
161};
162
163} // namespace msgpack_rpc::clients
Definition of CallResult class.
static void get_from_call_result(const messages::CallResult &call_result)
Get the result from CallResult object.
CallFuture(std::shared_ptr< impl::ICallFutureImpl > impl)
Constructor.
void get_result()
Get the result of RPC.
void get_result_within(std::chrono::nanoseconds timeout)
Get the result of RPC within a timeout.
std::shared_ptr< impl::ICallFutureImpl > impl_
Object of the internal implementation.
CallFuture(std::shared_ptr< impl::ICallFutureImpl > impl)
Constructor.
Definition call_future.h:50
std::shared_ptr< impl::ICallFutureImpl > impl_
Object of the internal implementation.
Result get_result()
Get the result of RPC.
Definition call_future.h:63
static Result get_from_call_result(const messages::CallResult &call_result)
Get the result from CallResult object.
Definition call_future.h:92
Result get_result_within(std::chrono::nanoseconds timeout)
Get the result of RPC within a timeout.
Definition call_future.h:80
Class of exceptions specifying errors in servers.
Class of results of methods.
Definition call_result.h:35
std::shared_ptr< msgpack::zone > zone() const noexcept
Get the internal zone in msgpack library.
msgpack::object object() const noexcept
Get the internal object in msgpack library.
T result_as() const
Get the result.
bool is_success() const noexcept
Check whether this object has a successful result.
Definition call_result.h:75
Definition of ICallFutureImpl class.
Namespace of internal implementations.
Namespace of clients.
Definition call_future.h:30
STL namespace.
Definition of ServerException class.