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: 19 additions & 0 deletions include/toml11/get.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,25 @@ get(const basic_value<TC>& v)
cxx::make_index_sequence<std::tuple_size<T>::value>{});
}

// ============================================================================
// std::unordered_set

template<typename T, typename TC>
cxx::enable_if_t<toml::detail::is_unordered_set<T>::value, T>
get(const basic_value<TC>& v)
{
using value_type = typename T::value_type;
const auto& a = v.as_array();

T container;
for (const auto& elem : a)
{
container.insert(get<value_type>(elem));
}
return container;
}


// ============================================================================
// map-like types; most likely STL map, like std::map or std::unordered_map.

Expand Down
7 changes: 7 additions & 0 deletions include/toml11/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_set>
#include <utility>

#if defined(TOML11_HAS_STRING_VIEW)
Expand Down Expand Up @@ -165,6 +166,12 @@ struct is_std_tuple_impl<std::tuple<Ts...>> : std::true_type{};
template<typename T>
using is_std_tuple = is_std_tuple_impl<cxx::remove_cvref_t<T>>;

template<typename T> struct is_unordered_set_impl : std::false_type {};
template<typename T>
struct is_unordered_set_impl<std::unordered_set<T>> : std::true_type {};
template<typename T>
using is_unordered_set = is_unordered_set_impl<cxx::remove_cvref_t<T>>;

#if defined(TOML11_HAS_OPTIONAL)
template<typename T> struct is_std_optional_impl : std::false_type{};
template<typename T>
Expand Down
38 changes: 38 additions & 0 deletions tests/test_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,44 @@ TEST_CASE("testing toml::get<array-of-arrays>")
}
}

TEST_CASE("testing toml::get<unordered_set>")
{
using value_type = toml::value;

{
const value_type v(toml::array{1, 2, 3, 4});
const std::unordered_set<int> uset = toml::get<std::unordered_set<int>>(v);

CHECK_EQ(uset.size(), 4u);
CHECK(uset.count(1) == 1);
CHECK(uset.count(2) == 1);
CHECK(uset.count(3) == 1);
CHECK(uset.count(4) == 1);
}

{
const value_type v(toml::array{"alpha", "beta", "delta"});
const std::unordered_set<std::string> uset = toml::get<std::unordered_set<std::string>>(v);

CHECK_EQ(uset.size(), 3u);
CHECK(uset.count("alpha") == 1);
CHECK(uset.count("beta") == 1);
CHECK(uset.count("delta") == 1);
}

{
value_type v(toml::array{42, 42, 54, 69});
const std::unordered_set<int> uset = toml::get<std::unordered_set<int>>(std::move(v));

// Set will only have one "42"
CHECK_EQ(uset.size(), 3u);
CHECK(uset.count(42) == 1);
CHECK(uset.count(54) == 1);
CHECK(uset.count(69) == 1);
}
}


TEST_CASE("testing toml::get<table-like>")
{
using value_type = toml::value;
Expand Down
Loading