cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
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
33
34namespace msgpack_rpc::transport {
35
40public:
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
76private:
78 std::unordered_map<std::string_view, std::shared_ptr<transport::IBackend>>
80};
81
82} // namespace msgpack_rpc::transport
Class of exceptions in cpp-msgpack-rpc library.
std::unordered_map< std::string_view, std::shared_ptr< transport::IBackend > > backends_
Backends.
std::shared_ptr< transport::IBackend > find(std::string_view scheme) const
Find a backend. Throw an exception when not found.
void append(std::shared_ptr< transport::IBackend > backend)
Add a backend of the protocol.
BackendList()=default
Constructor.
Definition of IBackend class.
Definition of MsgpackRPCException class.
Namespace of transport of messages.
Definition backends.h:31
Definition of StatusCode enumeration.