Merge branch 'master' into find-idx

This commit is contained in:
ToruNiina 2019-10-07 11:11:08 +09:00
commit 1148d01c70
3 changed files with 102 additions and 4 deletions

View File

@ -1091,7 +1091,7 @@ namespace toml
template<>
struct from<ext::foo>
{
ext::foo from_toml(const value& v)
static ext::foo from_toml(const value& v)
{
ext::foo f;
f.a = find<int >(v, "a");
@ -1131,7 +1131,7 @@ template<>
struct from<ext::foo>
{
template<typename C, template<typename ...> class M, template<typename ...> class A>
ext::foo from_toml(const basic_value<C, M, A>& v)
static ext::foo from_toml(const basic_value<C, M, A>& v)
{
ext::foo f;
f.a = find<int >(v, "a");
@ -1187,7 +1187,7 @@ namespace toml
template<>
struct into<ext::foo>
{
toml::table into_toml(const ext::foo& f)
static toml::table into_toml(const ext::foo& f)
{
return toml::table{{"a", f.a}, {"b", f.b}, {"c", f.c}};
}
@ -1492,6 +1492,8 @@ I appreciate the help of the contributors who introduced the great feature to th
- Fixed warnings while type conversion
- @KerstinKeller
- Added installation script to CMake
- J.C. Moyer (@jcmoyer)
- Fixed an example code in the documentation
## Licensing terms

View File

@ -33,6 +33,17 @@ struct bar
return toml::table{{"a", this->a}, {"b", this->b}};
}
};
struct baz
{
int a;
std::string b;
};
struct qux
{
int a;
std::string b;
};
} // extlib
namespace toml
@ -54,6 +65,24 @@ struct into<extlib::foo>
return toml::table{{"a", f.a}, {"b", f.b}};
}
};
template<>
struct from<extlib::baz>
{
static extlib::baz from_toml(const toml::value& v)
{
return extlib::baz{toml::find<int>(v, "a"), toml::find<std::string>(v, "b")};
}
};
template<>
struct into<extlib::qux>
{
static toml::table into_toml(const extlib::qux& f)
{
return toml::table{{"a", f.a}, {"b", f.b}};
}
};
} // toml
// ---------------------------------------------------------------------------
@ -83,6 +112,16 @@ struct bar
return toml::table{{"a", this->a}, {"b", this->b}};
}
};
struct baz
{
int a;
std::string b;
};
struct qux
{
int a;
std::string b;
};
} // extlib2
namespace toml
@ -105,6 +144,28 @@ struct into<extlib2::foo>
return toml::table{{"a", f.a}, {"b", f.b}};
}
};
template<>
struct from<extlib2::baz>
{
template<typename C, template<typename ...> class M, template<typename ...> class A>
static extlib2::baz from_toml(const toml::basic_value<C, M, A>& v)
{
return extlib2::baz{toml::find<int>(v, "a"), toml::find<std::string>(v, "b")};
}
};
template<>
struct into<extlib2::qux>
{
static toml::basic_value<toml::preserve_comments, std::map>
into_toml(const extlib2::qux& f)
{
return toml::basic_value<toml::preserve_comments, std::map>{
{"a", f.a}, {"b", f.b}
};
}
};
} // toml
// ---------------------------------------------------------------------------
@ -188,6 +249,41 @@ BOOST_AUTO_TEST_CASE(test_conversion_by_specialization)
}
}
BOOST_AUTO_TEST_CASE(test_conversion_one_way)
{
{
const toml::value v{{"a", 42}, {"b", "baz"}};
const auto baz = toml::get<extlib::baz>(v);
BOOST_TEST(baz.a == 42);
BOOST_TEST(baz.b == "baz");
}
{
const extlib::qux q{42, "qux"};
const toml::value v(q);
BOOST_TEST(toml::find<int>(v, "a") == 42);
BOOST_TEST(toml::find<std::string>(v, "b") == "qux");
}
{
const toml::basic_value<toml::discard_comments, std::map> v{
{"a", 42}, {"b", "baz"}
};
const auto baz = toml::get<extlib2::baz>(v);
BOOST_TEST(baz.a == 42);
BOOST_TEST(baz.b == "baz");
}
{
const extlib::qux q{42, "qux"};
const toml::basic_value<toml::preserve_comments, std::map> v(q);
BOOST_TEST(toml::find<int>(v, "a") == 42);
BOOST_TEST(toml::find<std::string>(v, "b") == "qux");
}
}
BOOST_AUTO_TEST_CASE(test_recursive_conversion)
{
{

View File

@ -256,7 +256,7 @@ get(const basic_value<C, M, V>&);
// toml::from<T>::from_toml(v)
template<typename T, typename C,
template<typename ...> class M, template<typename ...> class V,
std::size_t S = sizeof(::toml::into<T>)>
std::size_t S = sizeof(::toml::from<T>)>
T get(const basic_value<C, M, V>&);
// ============================================================================