cpp-msgpack-rpc
0.2.0
An RPC library implementing MessagePack RPC.
Toggle main menu visibility
Loading...
Searching...
No Matches
method_dict.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 <string>
24
#include <unordered_map>
25
#include <utility>
26
27
#include <fmt/format.h>
28
29
#include "
msgpack_rpc/common/msgpack_rpc_exception.h
"
30
#include "
msgpack_rpc/common/status_code.h
"
31
#include "
msgpack_rpc/messages/method_name_view.h
"
32
#include "
msgpack_rpc/methods/i_method.h
"
33
34
namespace
msgpack_rpc::methods
{
35
39
class
MethodDict
{
40
public
:
44
explicit
MethodDict
() =
default
;
45
51
void
append
(std::unique_ptr<IMethod> method) {
52
const
messages::MethodNameView
method_name = method->name();
53
const
auto
result =
54
methods_
.try_emplace(method_name, std::move(method));
55
if
(!result.second) {
56
const
auto
message =
57
fmt::format(
"Duplicate method name {}."
, method_name);
58
throw
MsgpackRPCException
(StatusCode::INVALID_ARGUMENT, message);
59
}
60
}
61
68
[[nodiscard]]
IMethod
*
get
(
messages::MethodNameView
name)
const
{
69
const
auto
iter =
methods_
.find(name);
70
if
(iter ==
methods_
.end()) {
71
const
auto
message = fmt::format(
"Method {} not found."
, name);
72
throw
MsgpackRPCException
(StatusCode::INVALID_MESSAGE, message);
73
}
74
return
iter->second.get();
75
}
76
77
private
:
79
std::unordered_map<messages::MethodNameView, std::unique_ptr<IMethod>>
80
methods_
{};
81
};
82
83
}
// namespace msgpack_rpc::methods
msgpack_rpc::MsgpackRPCException
Class of exceptions in cpp-msgpack-rpc library.
Definition
msgpack_rpc_exception.h:35
msgpack_rpc::messages::MethodNameView
Class of method names.
Definition
method_name_view.h:39
msgpack_rpc::methods::IMethod
Interface of methods.
Definition
i_method.h:32
msgpack_rpc::methods::MethodDict::get
IMethod * get(messages::MethodNameView name) const
Get the method with a name. Throws an exception when not found.
Definition
method_dict.h:68
msgpack_rpc::methods::MethodDict::MethodDict
MethodDict()=default
Constructor.
msgpack_rpc::methods::MethodDict::methods_
std::unordered_map< messages::MethodNameView, std::unique_ptr< IMethod > > methods_
Dictionary of methods.
Definition
method_dict.h:80
msgpack_rpc::methods::MethodDict::append
void append(std::unique_ptr< IMethod > method)
Append a method.
Definition
method_dict.h:51
i_method.h
Definition of IMethod class.
method_name_view.h
Definition of MethodNameView class.
msgpack_rpc_exception.h
Definition of MsgpackRPCException class.
msgpack_rpc::methods
Namespace of methods in MessagePack-RPC.
Definition
functional_method.h:41
status_code.h
Definition of StatusCode enumeration.
src
msgpack_rpc
methods
method_dict.h
Generated on
for cpp-msgpack-rpc by
1.18.0