cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
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
33
34namespace msgpack_rpc::methods {
35
40public:
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
77private:
79 std::unordered_map<messages::MethodNameView, std::unique_ptr<IMethod>>
81};
82
83} // namespace msgpack_rpc::methods
Class of exceptions in cpp-msgpack-rpc library.
Interface of methods.
Definition i_method.h:32
IMethod * get(messages::MethodNameView name) const
Get the method with a name. Throws an exception when not found.
Definition method_dict.h:68
MethodDict()=default
Constructor.
std::unordered_map< messages::MethodNameView, std::unique_ptr< IMethod > > methods_
Dictionary of methods.
Definition method_dict.h:80
void append(std::unique_ptr< IMethod > method)
Append a method.
Definition method_dict.h:51
Definition of IMethod class.
Definition of MethodNameView class.
Definition of MsgpackRPCException class.
Namespace of methods in MessagePack-RPC.
Definition of StatusCode enumeration.