/builds/MusicScience37Projects/utility-libraries/cpp-msgpack-rpc/include/msgpack_rpc/clients/call_future.h
Line | Count | Source |
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 | | */ |
16 | | /*! |
17 | | * \file |
18 | | * \brief Definition of CallFuture class. |
19 | | */ |
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 | | |
32 | | /*! |
33 | | * \brief Class of future object to wait for asynchronous RPCs. |
34 | | * |
35 | | * \tparam Result Type of the result. |
36 | | * |
37 | | * Objects of this class are created by Client::async_call function. |
38 | | */ |
39 | | template <typename Result> |
40 | | class CallFuture { |
41 | | public: |
42 | | /*! |
43 | | * \brief Constructor. |
44 | | * |
45 | | * \param[in] impl Object of the internal implementation. |
46 | | * |
47 | | * \warning Users should create objects of this class using |
48 | | * Client::async_call function. |
49 | | */ |
50 | | explicit CallFuture(std::shared_ptr<impl::ICallFutureImpl> impl) |
51 | 241 | : impl_(std::move(impl)) {} _ZN11msgpack_rpc7clients10CallFutureINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEEC2ENS2_10shared_ptrINS0_4impl15ICallFutureImplEEE Line | Count | Source | 51 | 21 | : impl_(std::move(impl)) {} |
_ZN11msgpack_rpc7clients10CallFutureIiEC2ENSt3__110shared_ptrINS0_4impl15ICallFutureImplEEE Line | Count | Source | 51 | 20 | : impl_(std::move(impl)) {} |
_ZN11msgpack_rpc7clients10CallFutureImEC2ENSt3__110shared_ptrINS0_4impl15ICallFutureImplEEE Line | Count | Source | 51 | 200 | : impl_(std::move(impl)) {} |
|
52 | | |
53 | | /*! |
54 | | * \brief Get the result of RPC. |
55 | | * |
56 | | * \return Result. |
57 | | * |
58 | | * \throw ServerException Errors in the server. |
59 | | * \throw MsgpackRPCException Other errors. |
60 | | * |
61 | | * \note This function will wait for the result if not received. |
62 | | */ |
63 | 241 | [[nodiscard]] Result get_result() { |
64 | 241 | const auto call_result = impl_->get_result(); |
65 | 241 | return get_from_call_result(call_result); |
66 | 241 | } _ZN11msgpack_rpc7clients10CallFutureINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEE10get_resultEv Line | Count | Source | 63 | 21 | [[nodiscard]] Result get_result() { | 64 | 21 | const auto call_result = impl_->get_result(); | 65 | 21 | return get_from_call_result(call_result); | 66 | 21 | } |
_ZN11msgpack_rpc7clients10CallFutureIiE10get_resultEv Line | Count | Source | 63 | 20 | [[nodiscard]] Result get_result() { | 64 | 20 | const auto call_result = impl_->get_result(); | 65 | 20 | return get_from_call_result(call_result); | 66 | 20 | } |
_ZN11msgpack_rpc7clients10CallFutureImE10get_resultEv Line | Count | Source | 63 | 200 | [[nodiscard]] Result get_result() { | 64 | 200 | const auto call_result = impl_->get_result(); | 65 | 200 | return get_from_call_result(call_result); | 66 | 200 | } |
|
67 | | |
68 | | /*! |
69 | | * \brief Get the result of RPC within a timeout. |
70 | | * |
71 | | * \param[in] timeout Timeout. |
72 | | * \return Result. |
73 | | * |
74 | | * \throw ServerException Errors in the server. |
75 | | * \throw MsgpackRPCException Other errors including timeout. |
76 | | * |
77 | | * \note This function will wait for the result if not received, and throw |
78 | | * an exception when no result can be received within the given timeout. |
79 | | */ |
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: |
86 | | /*! |
87 | | * \brief Get the result from CallResult object. |
88 | | * |
89 | | * \param[in] call_result CallResult object. |
90 | | * \return Result. |
91 | | */ |
92 | | [[nodiscard]] static Result get_from_call_result( |
93 | 239 | const messages::CallResult& call_result) { |
94 | 239 | if (call_result.is_success()) { |
95 | 230 | return call_result.result_as<Result>(); |
96 | 230 | } |
97 | 9 | throw ServerException(call_result.object(), call_result.zone()); |
98 | 239 | } _ZN11msgpack_rpc7clients10CallFutureINSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEE20get_from_call_resultERKNS_8messages10CallResultE Line | Count | Source | 93 | 19 | const messages::CallResult& call_result) { | 94 | 19 | if (call_result.is_success()) { | 95 | 10 | return call_result.result_as<Result>(); | 96 | 10 | } | 97 | 9 | throw ServerException(call_result.object(), call_result.zone()); | 98 | 19 | } |
_ZN11msgpack_rpc7clients10CallFutureIiE20get_from_call_resultERKNS_8messages10CallResultE Line | Count | Source | 93 | 20 | const messages::CallResult& call_result) { | 94 | 20 | if (call_result.is_success()) { | 95 | 20 | return call_result.result_as<Result>(); | 96 | 20 | } | 97 | 0 | throw ServerException(call_result.object(), call_result.zone()); | 98 | 20 | } |
_ZN11msgpack_rpc7clients10CallFutureImE20get_from_call_resultERKNS_8messages10CallResultE Line | Count | Source | 93 | 200 | const messages::CallResult& call_result) { | 94 | 200 | if (call_result.is_success()) { | 95 | 200 | return call_result.result_as<Result>(); | 96 | 200 | } | 97 | 0 | throw ServerException(call_result.object(), call_result.zone()); | 98 | 200 | } |
|
99 | | |
100 | | //! Object of the internal implementation. |
101 | | std::shared_ptr<impl::ICallFutureImpl> impl_; |
102 | | }; |
103 | | |
104 | | /*! |
105 | | * \brief Specialization of msgpack_rpc::clients::CallFuture for void type. |
106 | | * |
107 | | * Objects of this class are created by Client::async_call function. |
108 | | */ |
109 | | template <> |
110 | | class CallFuture<void> { |
111 | | public: |
112 | | /*! |
113 | | * \brief Constructor. |
114 | | * |
115 | | * \param[in] impl Object of the internal implementation. |
116 | | * |
117 | | * \warning Users should create objects of this class using |
118 | | * Client::async_call function. |
119 | | */ |
120 | | explicit CallFuture(std::shared_ptr<impl::ICallFutureImpl> impl) |
121 | 9 | : impl_(std::move(impl)) {} |
122 | | |
123 | | /*! |
124 | | * \brief Get the result of RPC. |
125 | | * |
126 | | * \note This function will wait for the result if not received. |
127 | | */ |
128 | 9 | void get_result() { |
129 | 9 | const auto call_result = impl_->get_result(); |
130 | 9 | get_from_call_result(call_result); |
131 | 9 | } |
132 | | |
133 | | /*! |
134 | | * \brief Get the result of RPC within a timeout. |
135 | | * |
136 | | * \param[in] timeout Timeout. |
137 | | * |
138 | | * \note This function will wait for the result if not received, and throw |
139 | | * an exception when no result can be received within the given timeout. |
140 | | */ |
141 | 0 | void get_result_within(std::chrono::nanoseconds timeout) { |
142 | 0 | const auto call_result = impl_->get_result_within(timeout); |
143 | 0 | get_from_call_result(call_result); |
144 | 0 | } |
145 | | |
146 | | private: |
147 | | /*! |
148 | | * \brief Get the result from CallResult object. |
149 | | * |
150 | | * \param[in] call_result CallResult object. |
151 | | */ |
152 | 9 | static void get_from_call_result(const messages::CallResult& call_result) { |
153 | 9 | if (call_result.is_success()) { |
154 | 6 | return; |
155 | 6 | } |
156 | 3 | throw ServerException(call_result.object(), call_result.zone()); |
157 | 9 | } |
158 | | |
159 | | //! Object of the internal implementation. |
160 | | std::shared_ptr<impl::ICallFutureImpl> impl_; |
161 | | }; |
162 | | |
163 | | } // namespace msgpack_rpc::clients |