cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
parsed_parameters.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 <tuple>
24#include <utility>
25
26#include <msgpack.hpp>
27
30
31namespace msgpack_rpc::messages {
32
37public:
45 msgpack::object object, std::shared_ptr<msgpack::zone> zone)
46 : object_(object), zone_(std::move(zone)) {
47 if (object.type != msgpack::type::ARRAY) {
49 StatusCode::INVALID_MESSAGE, "Invalid type of parameters.");
50 }
51 }
52
59 template <typename... Parameters>
60 [[nodiscard]] std::tuple<Parameters...> as() const {
61 try {
62 // msgpack library doesn't check the number of elements in tuples.
63 if (object_.via.array.size != sizeof...(Parameters)) {
64 throw MsgpackRPCException(StatusCode::INVALID_MESSAGE,
65 "Invalid number of parameters.");
66 }
67 return object_.as<std::tuple<Parameters...>>();
68 } catch (const msgpack::type_error&) {
70 StatusCode::TYPE_ERROR, "Invalid types of parameters.");
71 }
72 }
73
74private:
76 msgpack::object object_;
77
79 std::shared_ptr<msgpack::zone> zone_;
80};
81
82} // namespace msgpack_rpc::messages
Class of exceptions in cpp-msgpack-rpc library.
msgpack::object object_
Object of parameters in msgpack library.
std::shared_ptr< msgpack::zone > zone_
Zone in msgpack library.
ParsedParameters(msgpack::object object, std::shared_ptr< msgpack::zone > zone)
Constructor.
std::tuple< Parameters... > as() const
Get parameters as given types.
Definition of MsgpackRPCException class.
Namespace of messages.
Definition buffer_view.h:24
STL namespace.
Definition of StatusCode enumeration.