cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
parameters_serializer.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 <cstddef>
23#include <tuple>
24#include <utility>
25
30
32
36class IParametersSerializer {
37public:
46 messages::MethodNameView method_name,
47 messages::MessageID request_id) const = 0;
48
55 [[nodiscard]] virtual messages::SerializedMessage
57 messages::MethodNameView method_name) const = 0;
58
59 IParametersSerializer(const IParametersSerializer&) = delete;
60 IParametersSerializer& operator=(const IParametersSerializer&) = delete;
61
65 IParametersSerializer(IParametersSerializer&&) noexcept = default;
66
72 IParametersSerializer& operator=(
73 IParametersSerializer&&) noexcept = default;
74
75protected:
77 IParametersSerializer() noexcept = default;
78
80 ~IParametersSerializer() noexcept = default;
81};
82
88template <typename... Parameters>
89class ParametersSerializer final : public IParametersSerializer {
90public:
96 explicit ParametersSerializer(const Parameters&... parameters)
97 : parameters_(parameters...) {}
98
101 messages::MethodNameView method_name,
102 messages::MessageID request_id) const override {
104 method_name, request_id, std::index_sequence_for<Parameters...>());
105 }
106
109 messages::MethodNameView method_name) const override {
111 method_name, std::index_sequence_for<Parameters...>());
112 }
113
115 ParametersSerializer& operator=(const ParametersSerializer&) = delete;
116
121
127 ParametersSerializer& operator=(ParametersSerializer&&) noexcept = default;
128
131
132private:
141 template <std::size_t... Indices>
142 [[nodiscard]] messages::SerializedMessage create_serialized_request_impl(
143 messages::MethodNameView method_name, messages::MessageID request_id,
144 std::index_sequence<Indices...> /*indices*/) const {
146 method_name, request_id, std::get<Indices>(parameters_)...);
147 }
148
156 template <std::size_t... Indices>
157 [[nodiscard]] messages::SerializedMessage
159 std::index_sequence<Indices...> /*indices*/) const {
161 method_name, std::get<Indices>(parameters_)...);
162 }
163
165 std::tuple<const Parameters&...> parameters_;
166};
167
175template <typename... Parameters>
177 const Parameters&... parameters) {
178 return ParametersSerializer<Parameters...>(parameters...);
179}
180
181} // namespace msgpack_rpc::clients::impl
virtual messages::SerializedMessage create_serialized_notification(messages::MethodNameView method_name) const =0
Create a serialized notification data.
virtual messages::SerializedMessage create_serialized_request(messages::MethodNameView method_name, messages::MessageID request_id) const =0
Create a serialized request data.
IParametersSerializer(IParametersSerializer &&) noexcept=default
Move constructor.
IParametersSerializer() noexcept=default
Constructor.
messages::SerializedMessage create_serialized_request_impl(messages::MethodNameView method_name, messages::MessageID request_id, std::index_sequence< Indices... >) const
Create a serialized request data.
ParametersSerializer(ParametersSerializer &&) noexcept=default
Move constructor.
messages::SerializedMessage create_serialized_notification(messages::MethodNameView method_name) const override
Create a serialized notification data.
std::tuple< const Parameters &... > parameters_
Parameters.
messages::SerializedMessage create_serialized_notification_impl(messages::MethodNameView method_name, std::index_sequence< Indices... >) const
Create a serialized notification data.
ParametersSerializer(const Parameters &... parameters)
Constructor.
messages::SerializedMessage create_serialized_request(messages::MethodNameView method_name, messages::MessageID request_id) const override
Create a serialized request data.
static SerializedMessage serialize_notification(MethodNameView method_name, const Parameters &... parameters)
Serialize a notification.
static SerializedMessage serialize_request(MethodNameView method_name, MessageID message_id, const Parameters &... parameters)
Serialize a request.
Class of serialized message data.
Definition of MessageID type.
Definition of MessageSerializer class.
Definition of MethodNameView class.
Namespace of internal implementations.
ParametersSerializer< Parameters... > make_parameters_serializer(const Parameters &... parameters)
Create ParametersSerializer object.
Namespace of messages.
Definition buffer_view.h:24
std::uint32_t MessageID
Type of message IDs.
Definition message_id.h:27
STL namespace.
Definition of SerializedMessage class.