cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
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
29
30namespace msgpack_rpc::messages {
31
36public:
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&) {
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_) {
107 StatusCode::PRECONDITION_NOT_MET, "This result is an error.");
108 }
109 try {
110 return object_.as<T>();
111 } catch (const msgpack::type_error&) {
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
162private:
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
176
178 msgpack::object object_;
179
181 std::shared_ptr<msgpack::zone> zone_;
182};
183
184} // namespace msgpack_rpc::messages
Class of exceptions in cpp-msgpack-rpc library.
msgpack::object object_
Object in msgpack library.
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.
static CallResult create_error(msgpack::object object, std::shared_ptr< msgpack::zone > zone)
Create an error result.
Definition call_result.h:57
CallResult(const CallResult &)=default
Copy constructor.
T result_as() const
Get the result.
CallResult(CallResult &&) noexcept=default
Move constructor.
bool is_error() const noexcept
Check whether this object has an error.
Definition call_result.h:68
bool is_error_
Whether this is an error.
T error_as() const
Get the error.
Definition call_result.h:84
std::shared_ptr< msgpack::zone > zone_
Zone in msgpack library.
static CallResult create_result(msgpack::object object, std::shared_ptr< msgpack::zone > zone)
Create an successful result.
Definition call_result.h:44
bool is_success() const noexcept
Check whether this object has a successful result.
Definition call_result.h:75
Definition of MsgpackRPCException class.
Namespace of messages.
Definition buffer_view.h:24
STL namespace.
Definition of StatusCode enumeration.