cpp-msgpack-rpc 0.2.0
An RPC library implementing MessagePack RPC.
Loading...
Searching...
No Matches
parse_toml_client_server.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 <exception>
24#include <string>
25#include <string_view>
26#include <unordered_map>
27#include <utility>
28
29#include <toml++/toml.h>
30
37
38namespace msgpack_rpc::config::toml::impl {
39
46inline void parse_toml(
47 const ::toml::table& table, MessageParserConfig& config) {
48 for (const auto& [key, value] : table) {
49 const auto key_str = key.str();
50 if (key_str == "read_buffer_size") {
52 "read_buffer_size", read_buffer_size, std::size_t);
53 }
54 }
55}
56
63inline void parse_toml(const ::toml::table& table, ExecutorConfig& config) {
64 for (const auto& [key, value] : table) {
65 const auto key_str = key.str();
66 if (key_str == "num_transport_threads") {
68 "num_transport_threads", num_transport_threads, std::size_t);
69 } else if (key_str == "num_callback_threads") {
71 "num_callback_threads", num_callback_threads, std::size_t);
72 }
73 }
74}
75
82inline void parse_toml(const ::toml::table& table, ReconnectionConfig& config) {
83 for (const auto& [key, value] : table) {
84 const auto key_str = key.str();
85 if (key_str == "initial_waiting_time_sec") {
87 "initial_waiting_time_sec", initial_waiting_time);
88 } else if (key_str == "max_waiting_time_sec") {
90 "max_waiting_time_sec", max_waiting_time);
91 } else if (key_str == "max_jitter_waiting_time_sec") {
93 "max_jitter_waiting_time_sec", max_jitter_waiting_time);
94 }
95 }
96}
97
104inline void parse_toml(const ::toml::table& table, ClientConfig& config) {
105 for (const auto& [key, value] : table) {
106 const auto key_str = key.str();
107 if (key_str == "uris") {
108 const auto* uris_node = value.as_array();
109 if (uris_node == nullptr) {
110 throw_error(value.source(), "uris");
111 }
112 for (const auto& elem : *uris_node) {
113 const auto* uri_node = elem.as_string();
114 if (uri_node == nullptr) {
115 throw_error(elem.source(), "uris");
116 }
117 try {
118 config.add_uri(uri_node->get());
119 } catch (const std::exception& e) {
120 throw_error(elem.source(), "uris", e.what());
121 }
122 }
123 } else if (key_str == "call_timeout_sec") {
125 "call_timeout_sec", call_timeout);
126 } else if (key_str == "message_parser") {
127 const auto* child_table = value.as_table();
128 if (child_table == nullptr) {
129 throw_error(value.source(), "message_parser");
130 }
131 parse_toml(*child_table, config.message_parser());
132 } else if (key_str == "executor") {
133 const auto* child_table = value.as_table();
134 if (child_table == nullptr) {
135 throw_error(value.source(), "executor");
136 }
137 parse_toml(*child_table, config.executor());
138 } else if (key_str == "reconnection") {
139 const auto* child_table = value.as_table();
140 if (child_table == nullptr) {
141 throw_error(value.source(), "reconnection");
142 }
143 parse_toml(*child_table, config.reconnection());
144 }
145 }
146}
147
154inline void parse_toml(const ::toml::table& table,
155 std::unordered_map<std::string, ClientConfig>& configs) {
156 for (const auto& [key, value] : table) {
157 const auto* table_ptr = value.as_table();
158 if (table_ptr == nullptr) {
159 throw_error(value.source(), "client",
160 "\"client\" must be a table of tables.");
161 }
163 parse_toml(*table_ptr, config);
164 configs.try_emplace(std::string(key.str()), std::move(config));
165 }
166}
167
174inline void parse_toml(const ::toml::table& table, ServerConfig& config) {
175 for (const auto& [key, value] : table) {
176 const auto key_str = key.str();
177 if (key_str == "uris") {
178 const auto* uris_node = value.as_array();
179 if (uris_node == nullptr) {
180 throw_error(value.source(), "uris");
181 }
182 for (const auto& elem : *uris_node) {
183 const auto* uri_node = elem.as_string();
184 if (uri_node == nullptr) {
185 throw_error(elem.source(), "uris");
186 }
187 try {
188 config.add_uri(uri_node->get());
189 } catch (const std::exception& e) {
190 throw_error(elem.source(), "uris", e.what());
191 }
192 }
193 } else if (key_str == "message_parser") {
194 const auto* child_table = value.as_table();
195 if (child_table == nullptr) {
196 throw_error(value.source(), "message_parser");
197 }
198 parse_toml(*child_table, config.message_parser());
199 } else if (key_str == "executor") {
200 const auto* child_table = value.as_table();
201 if (child_table == nullptr) {
202 throw_error(value.source(), "executor");
203 }
204 parse_toml(*child_table, config.executor());
205 }
206 }
207}
208
215inline void parse_toml(const ::toml::table& table,
216 std::unordered_map<std::string, ServerConfig>& configs) {
217 for (const auto& [key, value] : table) {
218 const auto* table_ptr = value.as_table();
219 if (table_ptr == nullptr) {
220 throw_error(value.source(), "server",
221 "\"server\" must be a table of tables.");
222 }
224 parse_toml(*table_ptr, config);
225 configs.try_emplace(std::string(key.str()), std::move(config));
226 }
227}
228
229} // namespace msgpack_rpc::config::toml::impl
Class of configuration of clients.
Class of configuration of executors.
Class of configuration of parsers of messages.
Class of configurations of reconnection.
Class of configurations of servers.
Definition of ClientConfig class.
Definition of ExecutorConfig class.
Definition of MessageParserConfig class.
Namespace of configurations.
void parse_toml(const ::toml::table &table, MessageParserConfig &config)
Parse a configuration of parsers of messages from TOML.
Definition of common functions and macros to parse TOML files.
#define MSGPACK_RPC_PARSE_TOML_VALUE(KEY_STR, CONFIG_FUNCTION, TYPE)
Internal macro to parse a value from TOML.
void throw_error(const ::toml::source_region &source, std::string_view config_key)
Throw an exception for an error of TOML.
#define MSGPACK_RPC_PARSE_TOML_VALUE_DURATION(KEY_STR, CONFIG_FUNCTION)
Internal macro to parse a value from TOML.
Definition of ReconnectionConfig class.
Definition of ServerConfig class.