cpp-msgpack-rpc
0.2.0
An RPC library implementing MessagePack RPC.
Toggle main menu visibility
Loading...
Searching...
No Matches
call_result.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 <memory>
23
#include <utility>
24
25
#include <msgpack.hpp>
26
27
#include "
msgpack_rpc/common/msgpack_rpc_exception.h
"
28
#include "
msgpack_rpc/common/status_code.h
"
29
30
namespace
msgpack_rpc::messages
{
31
35
class
CallResult
{
36
public
:
44
[[nodiscard]]
static
CallResult
create_result
(
45
msgpack::object
object
, std::shared_ptr<msgpack::zone>
zone
) {
46
constexpr
bool
is_error
=
false
;
47
return
CallResult
(
is_error
,
object
, std::move(
zone
));
48
}
49
57
[[nodiscard]]
static
CallResult
create_error
(
58
msgpack::object
object
, std::shared_ptr<msgpack::zone>
zone
) {
59
constexpr
bool
is_error
=
true
;
60
return
CallResult
(
is_error
,
object
, std::move(
zone
));
61
}
62
68
[[nodiscard]]
bool
is_error
() const noexcept {
return
is_error_
; }
69
75
[[nodiscard]]
bool
is_success
() const noexcept {
return
!
is_error_
; }
76
83
template
<
typename
T>
84
[[nodiscard]] T
error_as
()
const
{
85
if
(!
is_error_
) {
86
throw
MsgpackRPCException
(StatusCode::PRECONDITION_NOT_MET,
87
"This result is not an error."
);
88
}
89
try
{
90
return
object_
.as<T>();
91
}
catch
(
const
msgpack::type_error&) {
92
throw
MsgpackRPCException
(
93
StatusCode::TYPE_ERROR,
"Invalid type of the error."
);
94
}
95
}
96
103
template
<
typename
T>
104
[[nodiscard]] T
result_as
()
const
{
105
if
(
is_error_
) {
106
throw
MsgpackRPCException
(
107
StatusCode::PRECONDITION_NOT_MET,
"This result is an error."
);
108
}
109
try
{
110
return
object_
.as<T>();
111
}
catch
(
const
msgpack::type_error&) {
112
throw
MsgpackRPCException
(
113
StatusCode::TYPE_ERROR,
"Invalid type of the result."
);
114
}
115
}
116
122
[[nodiscard]] msgpack::object
object
() const noexcept {
return
object_
; }
123
129
[[nodiscard]] std::shared_ptr<msgpack::zone>
zone
() const noexcept {
130
return
zone_
;
131
}
132
136
CallResult
(
const
CallResult
&) =
default
;
137
141
CallResult
(
CallResult
&&) noexcept = default;
142
148
CallResult
& operator=(const
CallResult
&) = default;
149
155
CallResult
& operator=(
CallResult
&&) noexcept = default;
156
160
~
CallResult
() = default;
161
162
private:
170
CallResult
(
bool
is_error
, msgpack::
object
object
,
171
std
::shared_ptr<msgpack::
zone
>
zone
)
172
:
is_error_
(
is_error
),
object_
(
object
),
zone_
(
std
::move(
zone
)) {}
173
175
bool
is_error_
;
176
178
msgpack::object
object_
;
179
181
std::shared_ptr<msgpack::zone>
zone_
;
182
};
183
184
}
// namespace msgpack_rpc::messages
msgpack_rpc::MsgpackRPCException
Class of exceptions in cpp-msgpack-rpc library.
Definition
msgpack_rpc_exception.h:35
msgpack_rpc::messages::CallResult::object_
msgpack::object object_
Object in msgpack library.
Definition
call_result.h:178
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::create_error
static CallResult create_error(msgpack::object object, std::shared_ptr< msgpack::zone > zone)
Create an error result.
Definition
call_result.h:57
msgpack_rpc::messages::CallResult::CallResult
CallResult(const CallResult &)=default
Copy constructor.
msgpack_rpc::messages::CallResult::result_as
T result_as() const
Get the result.
Definition
call_result.h:104
msgpack_rpc::messages::CallResult::CallResult
CallResult(CallResult &&) noexcept=default
Move constructor.
msgpack_rpc::messages::CallResult::is_error
bool is_error() const noexcept
Check whether this object has an error.
Definition
call_result.h:68
msgpack_rpc::messages::CallResult::is_error_
bool is_error_
Whether this is an error.
Definition
call_result.h:175
msgpack_rpc::messages::CallResult::error_as
T error_as() const
Get the error.
Definition
call_result.h:84
msgpack_rpc::messages::CallResult::zone_
std::shared_ptr< msgpack::zone > zone_
Zone in msgpack library.
Definition
call_result.h:181
msgpack_rpc::messages::CallResult::create_result
static CallResult create_result(msgpack::object object, std::shared_ptr< msgpack::zone > zone)
Create an successful result.
Definition
call_result.h:44
msgpack_rpc::messages::CallResult::is_success
bool is_success() const noexcept
Check whether this object has a successful result.
Definition
call_result.h:75
msgpack_rpc_exception.h
Definition of MsgpackRPCException class.
msgpack_rpc::messages
Namespace of messages.
Definition
buffer_view.h:24
std
STL namespace.
status_code.h
Definition of StatusCode enumeration.
include
msgpack_rpc
messages
call_result.h
Generated on
for cpp-msgpack-rpc by
1.18.0