From c2b0de623f03fee762337321f2488a3948aa2a2c Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Mon, 17 Jun 2019 22:50:14 +0900 Subject: [PATCH] feat: enable to convert map-like to toml::value --- toml/value.hpp | 53 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/toml/value.hpp b/toml/value.hpp index d1962f7..bfa5887 100644 --- a/toml/value.hpp +++ b/toml/value.hpp @@ -796,7 +796,7 @@ class basic_value basic_value& operator=(std::initializer_list list) { this->cleanup(); - this->type_ = value_t::array ; + this->type_ = value_t::array; this->region_info_ = std::make_shared(region_base{}); array_type ary; ary.reserve(list.size()); @@ -805,8 +805,10 @@ class basic_value return *this; } - template::value, - std::nullptr_t>::type = nullptr> + template>, + detail::is_container + >::value, std::nullptr_t>::type = nullptr> basic_value(T&& list) : type_(value_t::array), region_info_(std::make_shared(region_base{})) @@ -815,12 +817,14 @@ class basic_value for(const auto& elem : list) {ary.emplace_back(elem);} assigner(this->array_, std::move(ary)); } - template::value, - std::nullptr_t>::type = nullptr> + template>, + detail::is_container + >::value, std::nullptr_t>::type = nullptr> basic_value& operator=(T&& list) { this->cleanup(); - this->type_ = value_t::array ; + this->type_ = value_t::array; this->region_info_ = std::make_shared(region_base{}); array_type ary; ary.reserve(list.size()); @@ -848,11 +852,14 @@ class basic_value basic_value& operator=(const table_type& tab) { this->cleanup(); - this->type_ = value_t::table ; + this->type_ = value_t::table; this->region_info_ = std::make_shared(region_base{}); assigner(this->table_, tab); return *this; } + + // initializer-list ------------------------------------------------------ + basic_value(std::initializer_list> list) : type_(value_t::table), region_info_(std::make_shared(region_base{})) @@ -864,7 +871,7 @@ class basic_value basic_value& operator=(std::initializer_list> list) { this->cleanup(); - this->type_ = value_t::array ; + this->type_ = value_t::table; this->region_info_ = std::make_shared(region_base{}); table_type tab; @@ -873,6 +880,36 @@ class basic_value return *this; } + // other table-like ----------------------------------------------------- + + template>, + detail::is_map + >::value, std::nullptr_t>::type = nullptr> + basic_value(const Map& mp) + : type_(value_t::table), + region_info_(std::make_shared(region_base{})) + { + table_type tab; + for(const auto& elem : mp) {tab[elem.first] = elem.second;} + assigner(this->table_, std::move(tab)); + } + template>, + detail::is_map + >::value, std::nullptr_t>::type = nullptr> + basic_value& operator=(const Map& mp) + { + this->cleanup(); + this->type_ = value_t::table; + this->region_info_ = std::make_shared(region_base{}); + + table_type tab; + for(const auto& elem : mp) {tab[elem.first] = elem.second;} + assigner(this->table_, std::move(tab)); + return *this; + } + // user-defined ========================================================= // convert using into_toml() method -------------------------------------