Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Config.cpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#include "Config.hpp"
18
20
21namespace toolbox {
22inline namespace util {
23using namespace std;
24
25Config::Config() = default;
26Config::~Config() = default;
27
28// Copy.
29Config::Config(const Config&) = default;
30Config& Config::operator=(const Config&) = default;
31
32// Move.
34Config& Config::operator=(Config&&) noexcept = default;
35
36const std::string& Config::get(const std::string& key) const
37{
38 auto it = map_.find(key);
39 if (it != map_.end()) {
40 return it->second;
41 }
42 if (!parent_) {
43 throw runtime_error{"missing config key: "s + key};
44 }
45 return parent_->get(key);
46}
47
48const char* Config::get(const std::string& key, const char* dfl) const noexcept
49{
50 auto it = map_.find(key);
51 if (it != map_.end()) {
52 return it->second.c_str();
53 }
54 return parent_ ? parent_->get(key, dfl) : dfl;
55}
56
57istream& Config::read_section(istream& is, string* next)
58{
60 return parse_section(
61 is,
62 [this, &var_sub](const auto& key, string val) {
63 var_sub(val);
64 map_.emplace(key, std::move(val));
65 },
66 next);
67}
68
69MultiConfig::MultiConfig() = default;
71
73MultiConfig& MultiConfig::operator=(MultiConfig&&) noexcept = default;
74
76{
77 map_.clear();
78 root_.clear();
79}
80
81void MultiConfig::read(istream& is)
82{
83 string next;
84 if (root_.read_section(is, next).eof()) {
85 return;
86 }
87 do {
88 const string name{next};
90 config.set_parent(root_);
91 config.read_section(is, next);
92 map_.emplace(name, std::move(config));
93
94 } while (!is.eof());
95}
96
97} // namespace util
98} // namespace toolbox
Simple config reader with environment variable substitution.
Definition Config.hpp:67
Config & operator=(const Config &)
const std::string & get(const std::string &key) const
Throws std::runtime_error if key does not exist.
Definition Config.cpp:36
std::istream & read_section(std::istream &is)
Definition Config.hpp:127
void set_parent(Config &parent) noexcept
Definition Config.hpp:141
void clear() noexcept
Definition Config.cpp:75
void read(std::istream &is)
Definition Config.cpp:81
Variable substitution.
Definition VarSub.hpp:32
STL namespace.
void clear(timeval &tv) noexcept
Definition Time.hpp:294
std::istream & parse_section(std::istream &is, FnT fn, std::string *name=nullptr)
Definition Config.hpp:33
constexpr const auto & get(const detail::Struct< detail::Member< TagsT, ValuesT >... > &s, TagT tag={})
Definition Struct.hpp:110
constexpr auto bind() noexcept
Definition Slot.hpp:92