cpp-msgpack-rpc
0.2.0
An RPC library implementing MessagePack RPC.
Toggle main menu visibility
Loading...
Searching...
No Matches
backend_list.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 <string_view>
25
#include <unordered_map>
26
#include <utility>
27
28
#include <fmt/format.h>
29
30
#include "
msgpack_rpc/common/msgpack_rpc_exception.h
"
31
#include "
msgpack_rpc/common/status_code.h
"
32
#include "
msgpack_rpc/transport/i_backend.h
"
33
34
namespace
msgpack_rpc::transport
{
35
39
class
BackendList
{
40
public
:
44
BackendList
() =
default
;
45
51
void
append
(std::shared_ptr<transport::IBackend> backend) {
52
const
auto
scheme = backend->scheme();
53
const
auto
result =
backends_
.try_emplace(scheme, std::move(backend));
54
if
(!result.second) {
55
throw
MsgpackRPCException
(StatusCode::INVALID_ARGUMENT,
56
fmt::format(
"Duplicate scheme : \"{}\"."
, scheme));
57
}
58
}
59
66
[[nodiscard]] std::shared_ptr<transport::IBackend>
find
(
67
std::string_view scheme)
const
{
68
const
auto
backend_iter =
backends_
.find(scheme);
69
if
(backend_iter ==
backends_
.end()) {
70
throw
MsgpackRPCException
(StatusCode::INVALID_ARGUMENT,
71
fmt::format(
"Invalid scheme: {}."
, scheme));
72
}
73
return
backend_iter->second;
74
}
75
76
private
:
78
std::unordered_map<std::string_view, std::shared_ptr<transport::IBackend>>
79
backends_
{};
80
};
81
82
}
// namespace msgpack_rpc::transport
msgpack_rpc::MsgpackRPCException
Class of exceptions in cpp-msgpack-rpc library.
Definition
msgpack_rpc_exception.h:35
msgpack_rpc::transport::BackendList::backends_
std::unordered_map< std::string_view, std::shared_ptr< transport::IBackend > > backends_
Backends.
Definition
backend_list.h:79
msgpack_rpc::transport::BackendList::find
std::shared_ptr< transport::IBackend > find(std::string_view scheme) const
Find a backend. Throw an exception when not found.
Definition
backend_list.h:66
msgpack_rpc::transport::BackendList::append
void append(std::shared_ptr< transport::IBackend > backend)
Add a backend of the protocol.
Definition
backend_list.h:51
msgpack_rpc::transport::BackendList::BackendList
BackendList()=default
Constructor.
i_backend.h
Definition of IBackend class.
msgpack_rpc_exception.h
Definition of MsgpackRPCException class.
msgpack_rpc::transport
Namespace of transport of messages.
Definition
backends.h:31
status_code.h
Definition of StatusCode enumeration.
src
msgpack_rpc
transport
backend_list.h
Generated on
for cpp-msgpack-rpc by
1.18.0