numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
toml_helper.h
Go to the documentation of this file.
1/*
2 * Copyright 2022 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 <optional>
23#include <string>
24#include <string_view>
25
26#include <fmt/format.h>
27#include <toml++/toml.h>
28
30
32
43template <typename T>
44inline auto require_value(const ::toml::table& table, std::string_view path,
45 std::string_view config_name, std::string_view type_name) -> T {
46 const auto child_node = table.at_path(path);
47 if (!child_node) {
48 throw invalid_argument(
49 fmt::format("Configuration {} is required.", config_name));
50 }
51 const std::optional<T> value = child_node.value<T>();
52 if (!value) {
53 throw invalid_argument(fmt::format(
54 "Configuration {} must be {}.", config_name, type_name));
55 }
56 return *value;
57}
58
69template <typename T>
70inline void read_optional_value_to(T& output, const ::toml::table& table,
71 std::string_view path, std::string_view config_name,
72 std::string_view type_name) {
73 const auto child_node = table.at_path(path);
74 if (!child_node) {
75 return;
76 }
77 const std::optional<T> value = child_node.value<T>();
78 if (!value) {
79 throw invalid_argument(fmt::format(
80 "Configuration {} must be {}.", config_name, type_name));
81 }
82 output = *value;
83}
84
85} // namespace num_collect::logging::config::toml
Class of exception on invalid arguments.
Definition exception.h:85
Definition of exceptions.
Namespace of logging configuration in TOML files.
auto require_value(const ::toml::table &table, std::string_view path, std::string_view config_name, std::string_view type_name) -> T
Get a value or throw exceptions.
Definition toml_helper.h:44
void read_optional_value_to(T &output, const ::toml::table &table, std::string_view path, std::string_view config_name, std::string_view type_name)
Read a value to a variable.
Definition toml_helper.h:70