Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions toolbox/util/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
#include <toolbox/util/Utility.hpp>
#include <toolbox/util/String.hpp>

#include <algorithm>
#include <map>
#include <ranges>
#include <stdexcept>
#include <unordered_map>
#include <map>
#include <utility>

namespace toolbox {
inline namespace util {
Expand Down Expand Up @@ -246,10 +248,23 @@ class TOOLBOX_API MultiConfig {
return section(std::string{name});
}

template <typename F>
void for_each_section(F f) const
noexcept(noexcept(
f(std::declval<const MultiConfig::MapType::key_type&>(),
std::declval<const MultiConfig::MapType::mapped_type&>())
))
{
std::for_each(map_.begin(), map_.end(), [&f](const auto& kv) {
f(kv.first, kv.second);
});
}

private:
Config root_;
// Map of named sections.
std::map<std::string, Config> map_;
using MapType = std::map<std::string, Config>;
MapType map_;
};

} // namespace util
Expand Down
30 changes: 30 additions & 0 deletions toolbox/util/Config.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,34 @@ foo=101
BOOST_CHECK_EQUAL(foo_it == foo_rng.end(), true);
}

BOOST_AUTO_TEST_CASE(MultiConfig_for_each_section)
{
const string text{R"(
[First]
key_abc=value_abc

[Second]
key_xyz=value_xyz
)"};

std::map<std::string,util::Config> sections;

MultiConfig c;
istringstream is{text};
c.read(is);
c.for_each_section([&sections](const std::string& name, const util::Config& config) {
sections.emplace(name, config);
});
auto first{sections.find("First")};
auto second{sections.find("Second")};
if (first == sections.end()) {
BOOST_FAIL("Missing section 'First'");
}
if (second == sections.end()) {
BOOST_FAIL("Missing section 'Second'");
}
BOOST_CHECK_EQUAL(first->second.get<std::string_view>("key_abc"), "value_abc"sv);
BOOST_CHECK_EQUAL(second->second.get<std::string_view>("key_xyz"), "value_xyz"sv);
}

BOOST_AUTO_TEST_SUITE_END()