cpp-msgpack-rpc
0.2.0
An RPC library implementing MessagePack RPC.
Toggle main menu visibility
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
26
#include "
msgpack_rpc/clients/impl/i_call_future_impl.h
"
27
#include "
msgpack_rpc/clients/server_exception.h
"
28
#include "
msgpack_rpc/messages/call_result.h
"
29
30
namespace
msgpack_rpc::clients
{
31
39
template
<
typename
Result>
40
class
CallFuture
{
41
public
:
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
85
private
:
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
109
template
<>
110
class
CallFuture
<void> {
111
public
:
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
146
private
:
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
call_result.h
Definition of CallResult class.
msgpack_rpc::clients::CallFuture< void >::get_from_call_result
static void get_from_call_result(const messages::CallResult &call_result)
Get the result from CallResult object.
Definition
call_future.h:152
msgpack_rpc::clients::CallFuture< void >::CallFuture
CallFuture(std::shared_ptr< impl::ICallFutureImpl > impl)
Constructor.
Definition
call_future.h:120
msgpack_rpc::clients::CallFuture< void >::get_result
void get_result()
Get the result of RPC.
Definition
call_future.h:128
msgpack_rpc::clients::CallFuture< void >::get_result_within
void get_result_within(std::chrono::nanoseconds timeout)
Get the result of RPC within a timeout.
Definition
call_future.h:141
msgpack_rpc::clients::CallFuture< void >::impl_
std::shared_ptr< impl::ICallFutureImpl > impl_
Object of the internal implementation.
Definition
call_future.h:160
msgpack_rpc::clients::CallFuture::CallFuture
CallFuture(std::shared_ptr< impl::ICallFutureImpl > impl)
Constructor.
Definition
call_future.h:50
msgpack_rpc::clients::CallFuture::impl_
std::shared_ptr< impl::ICallFutureImpl > impl_
Object of the internal implementation.
Definition
call_future.h:101
msgpack_rpc::clients::CallFuture::get_result
Result get_result()
Get the result of RPC.
Definition
call_future.h:63
msgpack_rpc::clients::CallFuture::get_from_call_result
static Result get_from_call_result(const messages::CallResult &call_result)
Get the result from CallResult object.
Definition
call_future.h:92
msgpack_rpc::clients::CallFuture::get_result_within
Result get_result_within(std::chrono::nanoseconds timeout)
Get the result of RPC within a timeout.
Definition
call_future.h:80
msgpack_rpc::clients::ServerException::ServerException
ServerException(msgpack::object object, std::shared_ptr< msgpack::zone > zone)
Constructor.
Definition
server_exception.cpp:31
msgpack_rpc::messages::CallResult
Class of results of methods.
Definition
call_result.h:35
msgpack_rpc::messages::CallResult::zone
std::shared_ptr< msgpack::zone > zone() const noexcept
Get the internal zone in msgpack library.
Definition
call_result.h:129
msgpack_rpc::messages::CallResult::object
msgpack::object object() const noexcept
Get the internal object in msgpack library.
Definition
call_result.h:122
msgpack_rpc::messages::CallResult::result_as
T result_as() const
Get the result.
Definition
call_result.h:104
msgpack_rpc::messages::CallResult::is_success
bool is_success() const noexcept
Check whether this object has a successful result.
Definition
call_result.h:75
i_call_future_impl.h
Definition of ICallFutureImpl class.
msgpack_rpc::clients::impl
Namespace of internal implementations.
Definition
i_call_future_impl.h:26
msgpack_rpc::clients
Namespace of clients.
Definition
call_future.h:30
std
STL namespace.
server_exception.h
Definition of ServerException class.
include
msgpack_rpc
clients
call_future.h
Generated on
for cpp-msgpack-rpc by
1.18.0