From fb95549ffb1735b9fa22883796d8ea66d6b9a7ea Mon Sep 17 00:00:00 2001 From: qicosmos Date: Tue, 9 May 2023 17:06:34 +0800 Subject: [PATCH] remove xml namespace --- README.md | 4 +- benchmark/xml_benchmark.cpp | 5 +-- example/example.cpp | 6 +-- example/xml_example.cpp | 54 +++++++++++++------------- iguana/type_traits.hpp | 2 - iguana/xml_reader.hpp | 4 +- iguana/xml_writer.hpp | 4 +- test/test_xml.cpp | 76 ++++++++++++++++++------------------- 8 files changed, 75 insertions(+), 80 deletions(-) diff --git a/README.md b/README.md index 3be77ac9..2860bbe0 100644 --- a/README.md +++ b/README.md @@ -79,12 +79,12 @@ Serialization of `xml` is similar to `json`. The first step is also defining met person p = {"admin", 20}; iguana::string_stream ss; - iguana::xml::to_xml(ss, p); + iguana::to_xml(ss, p); std::cout << ss.str() << std::endl; std::string xml = " buke 1"; - iguana::xml::from_xml(p, xml.data(), xml.length()); + iguana::from_xml(p, xml.data(), xml.length()); ### A complicated example *iguana* can deal with objects which contain another objects and containers. Here is the example: diff --git a/benchmark/xml_benchmark.cpp b/benchmark/xml_benchmark.cpp index 620885f3..ed83812d 100644 --- a/benchmark/xml_benchmark.cpp +++ b/benchmark/xml_benchmark.cpp @@ -50,8 +50,7 @@ void test_deserialize() { ScopedTimer timer("test deserialize rpm_filelists.xml"); for (int i = 0; i < iterations; ++i) { filelists_t filelist; - iguana::xml::from_xml(filelist, - xmlfilelist.data()); + iguana::from_xml(filelist, xmlfilelist.data()); } } std::cout << "============ deserialize sample_rss.xml ===============\n"; @@ -62,7 +61,7 @@ void test_deserialize() { ScopedTimer timer("test deserialize sample_rss.xml"); for (int i = 0; i < iterations; ++i) { rss_t rss; - iguana::xml::from_xml(rss, xmlrss.data()); + iguana::from_xml(rss, xmlrss.data()); // assert(rss.channel.item.size() == 99); } } diff --git a/example/example.cpp b/example/example.cpp index 969f4a37..d7316ec4 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -109,17 +109,17 @@ void test_json() { void test_xml() { person p = {"admin", 20}; iguana::string_stream ss; - iguana::xml::to_xml(ss, p); + iguana::to_xml(ss, p); std::cout << ss << std::endl; ss.clear(); two t = {"test", {2}, 4}; - iguana::xml::to_xml(ss, t); + iguana::to_xml(ss, t); std::cout << ss << std::endl; two t1; - iguana::xml::from_xml(t1, ss.data()); + iguana::from_xml(t1, ss.data()); std::cout << t1.age << "\n"; } diff --git a/example/xml_example.cpp b/example/xml_example.cpp index c8e96287..79e3587b 100644 --- a/example/xml_example.cpp +++ b/example/xml_example.cpp @@ -42,12 +42,12 @@ void test_to_xml() { // pretty xml std::string ss; - iguana::xml::to_xml_pretty(ss, contents); + iguana::to_xml_pretty(ss, contents); std::cout << ss << "\n"; // non pretty xml std::string s; - iguana::xml::to_xml(s, contents); + iguana::to_xml(s, contents); std::cout << s << "\n"; } @@ -56,11 +56,11 @@ void test_from_xml() { // pretty xml std::string ss; - iguana::xml::to_xml_pretty(ss, contents); + iguana::to_xml_pretty(ss, contents); std::cout << ss << "\n"; Contents contents2{"test"}; - iguana::xml::from_xml(contents2, ss.data()); + iguana::from_xml(contents2, ss.data()); std::cout << contents2.Size << "\n"; std::cout << contents2.Owner.DisplayName << "\n"; assert(contents == contents2); @@ -107,13 +107,13 @@ void test_parse_status() { )"; status_t t{}; - iguana::xml::from_xml(t, str.data()); + iguana::from_xml(t, str.data()); std::cout << t.owner << "\n"; std::cout << t.mtime << ", " << t.atime << "\n"; std::cout << t.storagePolicy << "\n"; std::string ss; - iguana::xml::to_xml(ss, t); + iguana::to_xml(ss, t); } void test_parse_response() { @@ -137,7 +137,7 @@ void test_parse_response() { )"; response t{}; - iguana::xml::from_xml(t, str.data()); + iguana::from_xml(t, str.data()); std::cout << t.status.owner << "\n"; } @@ -155,11 +155,11 @@ void test_optional() { op.d = true; op.e = 'o'; std::string ss; - iguana::xml::to_xml(ss, op); + iguana::to_xml(ss, op); std::cout << ss << "\n"; optional_t op1; - iguana::xml::from_xml(op1, ss.data()); + iguana::from_xml(op1, ss.data()); if (op1.b) { std::cout << *op1.b << "\n"; } @@ -183,15 +183,15 @@ void test_list() { l.list.push_back(optional_t{5, 6, {}, 0, 'l'}); std::string ss; - iguana::xml::to_xml(ss, l); + iguana::to_xml(ss, l); std::cout << ss << "\n"; list_t l1; - iguana::xml::from_xml(l1, ss.data()); + iguana::from_xml(l1, ss.data()); std::cout << l1.list.size() << "\n"; std::string s; - iguana::xml::to_xml_pretty(s, l); + iguana::to_xml_pretty(s, l); std::cout << s << '\n'; } @@ -223,10 +223,10 @@ void test_attribute() { )"; book_t book{}; - iguana::xml::from_xml(book, str.data()); + iguana::from_xml(book, str.data()); std::cout << book; std::string ss; - iguana::xml::to_xml(ss, book); + iguana::to_xml(ss, book); std::cout << "attr to_xml: " << ss << std::endl; } @@ -247,7 +247,7 @@ void test_nested_attribute() { )"; library_t library; - iguana::xml::from_xml(library, str.data()); + iguana::from_xml(library, str.data()); std::cout << "library attribute" << std::endl; for (auto &[k, v] : library.__attr) { std::cout << "[ " << k << " : " << v << "]" @@ -257,13 +257,13 @@ void test_nested_attribute() { std::cout << "\nbook\n" << library.book; std::string ss; - iguana::xml::to_xml(ss, library); + iguana::to_xml(ss, library); std::cout << "library to_xml: " << ss << std::endl; } struct movie_t { std::string title; std::string director; - std::unordered_map __attr; + std::unordered_map __attr; }; REFLECTION(movie_t, title, director, __attr); void test_any_attribute() { @@ -275,7 +275,7 @@ void test_any_attribute() { )"; movie_t movie; - iguana::xml::from_xml(movie, str.data()); + iguana::from_xml(movie, str.data()); std::cout << "movie attribute :" << std::endl; auto &attr = movie.__attr; { @@ -313,18 +313,18 @@ void test_vector() { p.name.push_back("Bob"); p.name.push_back("bbg"); std::string ss; - iguana::xml::to_xml(ss, p); + iguana::to_xml(ss, p); std::cout << ss << std::endl; } struct item_itunes_t { - iguana::xml::namespace_t itunes_author; - iguana::xml::namespace_t itunes_subtitle; - iguana::xml::namespace_t itunes_user; + iguana::namespace_t itunes_author; + iguana::namespace_t itunes_subtitle; + iguana::namespace_t itunes_user; }; REFLECTION(item_itunes_t, itunes_author, itunes_subtitle, itunes_user); struct item_t { - iguana::xml::namespace_t item_itunes; + iguana::namespace_t item_itunes; }; REFLECTION(item_t, item_itunes); void test_namespace() { @@ -339,13 +339,13 @@ void test_namespace() { )"; item_t it; - iguana::xml::from_xml(it, str.data()); + iguana::from_xml(it, str.data()); auto itunes = it.item_itunes.get(); std::cout << "author : " << itunes.itunes_author.get() << "\n"; std::cout << "subtitle : " << itunes.itunes_subtitle.get() << "\n"; std::cout << "user : " << itunes.itunes_user.get() << "\n"; std::string ss; - iguana::xml::to_xml(ss, it); + iguana::to_xml(ss, it); std::cout << "to_xml" << std::endl << ss << "\n"; } @@ -364,7 +364,7 @@ void test_leafnode_attribute() { )"; package_t package; - iguana::xml::from_xml(package, str.data()); + iguana::from_xml(package, str.data()); std::cout << "package attr : \n"; for (auto &[k, v] : package.__attr) { std::cout << "[ " << k << " : " << v << "] "; @@ -375,7 +375,7 @@ void test_leafnode_attribute() { } std::cout << "\nchangelog value : \n" << package.changelog.first << "\n"; std::string ss; - iguana::xml::to_xml(ss, package); + iguana::to_xml(ss, package); std::cout << "to_xml : \n" << ss << "\n"; } diff --git a/iguana/type_traits.hpp b/iguana/type_traits.hpp index f248942c..4ae22355 100644 --- a/iguana/type_traits.hpp +++ b/iguana/type_traits.hpp @@ -34,12 +34,10 @@ template constexpr inline bool is_std_pair_v = false; template constexpr inline bool is_std_pair_v> = true; -namespace xml { template class namespace_t; template constexpr inline bool is_namespace_v = false; template constexpr inline bool is_namespace_v> = true; -} // namespace xml } // namespace iguana diff --git a/iguana/xml_reader.hpp b/iguana/xml_reader.hpp index 54eee95e..32fcf922 100644 --- a/iguana/xml_reader.hpp +++ b/iguana/xml_reader.hpp @@ -10,7 +10,7 @@ #include #include -namespace iguana::xml { +namespace iguana { template void do_read(rapidxml::xml_node *node, T &&t); constexpr inline size_t find_underline(const char *str) { @@ -227,4 +227,4 @@ inline bool from_xml(T &&t, char *buf) { return false; } -} // namespace iguana::xml +} // namespace iguana diff --git a/iguana/xml_writer.hpp b/iguana/xml_writer.hpp index 6dccc48b..083a69ac 100644 --- a/iguana/xml_writer.hpp +++ b/iguana/xml_writer.hpp @@ -13,7 +13,7 @@ #include #include -namespace iguana::xml { +namespace iguana { // to xml template inline void to_xml_impl(Stream &s, T &&t, std::string_view name = ""); @@ -188,5 +188,5 @@ inline bool to_xml_pretty(Stream &s, T &&t) { return r; } -} // namespace iguana::xml +} // namespace iguana #endif // IGUANA_XML17_HPP \ No newline at end of file diff --git a/test/test_xml.cpp b/test/test_xml.cpp index 588efef9..d8b40af1 100644 --- a/test/test_xml.cpp +++ b/test/test_xml.cpp @@ -31,17 +31,17 @@ REFLECTION(simple_t, a, b, c, d, e); TEST_CASE("test simple xml") { simple_t simple{{1, 2, 3}, '|', 0, 1}; std::string str; - iguana::xml::to_xml_pretty(str, simple); + iguana::to_xml_pretty(str, simple); simple_t sfrom; - iguana::xml::from_xml(sfrom, str.data()); + iguana::from_xml(sfrom, str.data()); CHECK(sfrom == simple); std::string xmlstr = R"( 123|FalseTrueoptional? )"; simple_t sfrom2; - iguana::xml::from_xml(sfrom2, xmlstr.data()); + iguana::from_xml(sfrom2, xmlstr.data()); auto t = sfrom2.a; sort(t.begin(), t.end()); CHECK(t == std::vector{1, 2, 3}); @@ -64,20 +64,20 @@ TEST_CASE("test simple nested") { )"; nested_t nest; - iguana::xml::from_xml(nest, str.data()); + iguana::from_xml(nest, str.data()); simple_t res{{1, 2, 3}, '|', 0, 1}; CHECK(res == nest.simple); CHECK(nest.code == 10086); std::string toxmlstr; - iguana::xml::to_xml_pretty(toxmlstr, nest); + iguana::to_xml_pretty(toxmlstr, nest); nested_t nest2; - iguana::xml::from_xml(nest2, toxmlstr.data()); + iguana::from_xml(nest2, toxmlstr.data()); CHECK(nest2.simple == nest.simple); nest2.simple.a = std::vector(); std::string ss; - iguana::xml::to_xml(ss, nest2); + iguana::to_xml(ss, nest2); } struct book_t { @@ -112,7 +112,7 @@ TEST_CASE("test optinal and vector") { )"; std::string origin_str = str; book_t book; - iguana::xml::from_xml(book, str.data()); + iguana::from_xml(book, str.data()); CHECK(book.title == "C++ templates"); CHECK(book.author.size() == 3); @@ -127,9 +127,9 @@ TEST_CASE("test optinal and vector") { CHECK(*book.description == "talking about how to use template"); std::string xml_str; - iguana::xml::to_xml(xml_str, book); + iguana::to_xml(xml_str, book); book_t newbook; - iguana::xml::from_xml(newbook, xml_str.data()); + iguana::from_xml(newbook, xml_str.data()); CHECK_MESSAGE(newbook == book, "the newer must be same as the older"); } @@ -161,7 +161,7 @@ TEST_CASE("test nested vector") { )"; library_t library; - iguana::xml::from_xml(library, str.data()); + iguana::from_xml(library, str.data()); CHECK(library.sum == 2); CHECK(library.book[0].title == "C++ templates"); CHECK(library.book[1].title == "C++ primer"); @@ -177,9 +177,9 @@ TEST_CASE("test nested vector") { } std::string xml_str; - iguana::xml::to_xml(xml_str, library); + iguana::to_xml(xml_str, library); library_t newlibrary; - iguana::xml::from_xml(newlibrary, xml_str.data()); + iguana::from_xml(newlibrary, xml_str.data()); if (newlibrary.book[0].title == library.book[0].title) { CHECK(newlibrary.book[0] == library.book[0]); CHECK(newlibrary.book[1] == library.book[1]); @@ -201,22 +201,22 @@ TEST_CASE("test attribute with map") { )"; book_attr_t b; - iguana::xml::from_xml(b, str.data()); + iguana::from_xml(b, str.data()); CHECK(b.__attr["id"] == 5); CHECK(b.__attr["pages"] == 392.0f); CHECK(b.__attr["price"] == 79.9f); std::string ss; - iguana::xml::to_xml_pretty(ss, b); + iguana::to_xml_pretty(ss, b); book_attr_t b2; - iguana::xml::from_xml(b2, ss.data()); + iguana::from_xml(b2, ss.data()); CHECK(b2.__attr["id"] == 5); CHECK(b2.__attr["pages"] == 392.0f); CHECK(b2.__attr["price"] == 79.9f); } struct book_attr_any_t { - std::unordered_map __attr; + std::unordered_map __attr; std::string title; }; REFLECTION(book_attr_any_t, __attr, title); @@ -227,7 +227,7 @@ TEST_CASE("test attribute with any") { )"; book_attr_any_t b; - iguana::xml::from_xml(b, str.data()); + iguana::from_xml(b, str.data()); auto &map = b.__attr; CHECK(map["id"].get().first); CHECK(map["id"].get().second == 5); @@ -237,9 +237,9 @@ TEST_CASE("test attribute with any") { CHECK(map["price"].get().second == 79.9f); std::string ss; - iguana::xml::to_xml(ss, b); + iguana::to_xml(ss, b); book_attr_any_t b1; - iguana::xml::from_xml(b1, ss.data()); + iguana::from_xml(b1, ss.data()); map = b1.__attr; CHECK(map["id"].get().first); CHECK(map["id"].get().second == 5); @@ -251,7 +251,7 @@ TEST_CASE("test attribute with any") { struct library_attr_t { book_attr_any_t book; - std::unordered_map __attr; + std::unordered_map __attr; }; REFLECTION(library_attr_t, book, __attr); TEST_CASE("Test nested attribute with any") { @@ -263,7 +263,7 @@ TEST_CASE("Test nested attribute with any") { )"; library_attr_t library; - iguana::xml::from_xml(library, str.data()); + iguana::from_xml(library, str.data()); auto map = library.__attr; CHECK(map["code"].get().first); @@ -282,10 +282,10 @@ TEST_CASE("Test nested attribute with any") { CHECK(map["price"].get().second == 79.9f); std::string ss; - iguana::xml::to_xml_pretty(ss, library); + iguana::to_xml_pretty(ss, library); std::cout << ss << std::endl; library_attr_t library1; - iguana::xml::from_xml(library1, ss.data()); + iguana::from_xml(library1, ss.data()); map = library1.__attr; CHECK(map["code"].get().first); CHECK(map["code"].get().second == 102); @@ -309,26 +309,24 @@ TEST_CASE("test exception") { std::string str = R"( 123|FalseTrue + CHECK_FALSE(iguana::from_xml(simple, str.data())); // expected > std::string str2 = R"( 123|Flasetru )"; - CHECK_NOTHROW( - iguana::xml::from_xml(simple, str2.data())); // Failed to parse bool + CHECK_NOTHROW(iguana::from_xml(simple, str2.data())); // Failed to parse bool simple_t simple2{{1, 2, 3}, '|', 0, 1}; std::string ss = "<
>"; - CHECK_FALSE( - iguana::xml::to_xml_pretty(ss, simple2)); // unexpected end of data + CHECK_FALSE(iguana::to_xml_pretty(ss, simple2)); // unexpected end of data } struct item_itunes_t { - iguana::xml::namespace_t itunes_author; - iguana::xml::namespace_t itunes_subtitle; - iguana::xml::namespace_t itunes_user; + iguana::namespace_t itunes_author; + iguana::namespace_t itunes_subtitle; + iguana::namespace_t itunes_user; }; REFLECTION(item_itunes_t, itunes_author, itunes_subtitle, itunes_user); struct item_t { - iguana::xml::namespace_t item_itunes; + iguana::namespace_t item_itunes; }; REFLECTION(item_t, item_itunes); TEST_CASE("test xml namespace") { @@ -342,15 +340,15 @@ TEST_CASE("test xml namespace") { )"; item_t it; - iguana::xml::from_xml(it, str.data()); + iguana::from_xml(it, str.data()); auto itunes = it.item_itunes.get(); CHECK(itunes.itunes_author.get() == "Jupiter Broadcasting"); CHECK(itunes.itunes_user.get() == 10086); std::string ss; - iguana::xml::to_xml(ss, it); + iguana::to_xml(ss, it); item_t it2; - iguana::xml::from_xml(it2, ss.data()); + iguana::from_xml(it2, ss.data()); auto itunes2 = it2.item_itunes.get(); CHECK(itunes2.itunes_author.get() == "Jupiter Broadcasting"); CHECK(itunes2.itunes_user.get() == 10086); @@ -371,7 +369,7 @@ TEST_CASE("test leafnode attribute") { )"; package_t package; - iguana::xml::from_xml(package, str.data()); + iguana::from_xml(package, str.data()); using mp = std::unordered_map; mp p_attr = {{"name", "apr-util-ldap"}, {"arch", "x86_64"}}; mp v_attr = {{"epoch", "0"}, {"ver", "1.6.1"}, {"rel", "6.el8"}}; @@ -382,9 +380,9 @@ TEST_CASE("test leafnode attribute") { CHECK(package.changelog.first == "new version 1.6.1"); CHECK(package.version.first.empty()); std::string ss; - iguana::xml::to_xml(ss, package); + iguana::to_xml(ss, package); package_t package2; - iguana::xml::from_xml(package2, ss.data()); + iguana::from_xml(package2, ss.data()); CHECK(p_attr == package2.__attr); CHECK(v_attr == package2.version.second); CHECK(c_attr == package2.changelog.second);