From c016c30f0f2604e78140e358ba1b8c55a8be5a7f Mon Sep 17 00:00:00 2001 From: Dave Cridland Date: Thu, 18 Jul 2024 14:07:11 +0100 Subject: [PATCH] Avoid temporary when setting value via *_element --- rapidxml.hpp | 47 +++++++++++++++++------------------------- test/manipulations.cpp | 5 +++-- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/rapidxml.hpp b/rapidxml.hpp index eff79a8..ad91a47 100644 --- a/rapidxml.hpp +++ b/rapidxml.hpp @@ -1222,7 +1222,7 @@ namespace rapidxml } optional_ptr> allocate_element(std::initializer_list const & clark_name, view_type const & value) { auto child = allocate_element(clark_name); - child->value(value); + if (!value.empty()) child->value(value); return child; } public: @@ -1252,19 +1252,16 @@ namespace rapidxml auto prepend_node(optional_ptr> ptr) { return prepend_node(ptr.get()); } - template - auto prepend_element(view_type const & v, Args... args) { - auto child = allocate_element(v, args...); + auto prepend_element(view_type const & v, view_type const & value = {}) { + auto child = allocate_element(v, value); return prepend_node(child); } - template - auto prepend_element(std::tuple const & il, Args... args) { - auto child = allocate_element(il, args...); + auto prepend_element(std::tuple const & il, view_type const & value = {}) { + auto child = allocate_element(il, value); return prepend_node(child); } - template - auto prepend_element(std::initializer_list const & il, Args... args) { - auto child = allocate_element(il, args...); + auto prepend_element(std::initializer_list const & il, view_type const & value = {}) { + auto child = allocate_element(il, value); return prepend_node(child); } @@ -1293,19 +1290,16 @@ namespace rapidxml optional_ptr> append_node(optional_ptr> ptr) { return append_node(ptr.get()); } - template - auto append_element(view_type const & v, Args... args) { - auto child = allocate_element(v, args...); + auto append_element(view_type const & v, view_type const & value = {}) { + auto child = allocate_element(v, value); return append_node(child); } - template - auto append_element(std::tuple const & il, Args... args) { - auto child = allocate_element(il, args...); + auto append_element(std::tuple const & il, view_type const & value = {}) { + auto child = allocate_element(il, value); return append_node(child); } - template - auto append_element(std::initializer_list const & il, Args... args) { - auto child = allocate_element(il, args...); + auto append_element(std::initializer_list const & il, view_type const & value = {}) { + auto child = allocate_element(il, value); return append_node(child); } @@ -1335,19 +1329,16 @@ namespace rapidxml auto insert_node(optional_ptr> where, optional_ptr> ptr) { return insert_node(where.ptr(), ptr.ptr()); } - template - auto insert_element(optional_ptr> where, view_type const & v, Args... args) { - auto child = allocate_element(v, args...); + auto insert_element(optional_ptr> where, view_type const & v, view_type const & value = {}) { + auto child = allocate_element(v, value); return insert_node(where, child); } - template - auto insert_element(optional_ptr> where, std::tuple const & il, Args... args) { - auto child = allocate_element(il, args...); + auto insert_element(optional_ptr> where, std::tuple const & il, view_type const & value = {}) { + auto child = allocate_element(il, value); return insert_node(where, child); } - template - auto insert_element(optional_ptr> where, std::initializer_list const & il, Args... args) { - auto child = allocate_element(il, args...); + auto insert_element(optional_ptr> where, std::initializer_list const & il, view_type const & value = {}) { + auto child = allocate_element(il, value); return insert_node(where, child); } diff --git a/test/manipulations.cpp b/test/manipulations.cpp index ddbe00e..d1b4c4b 100644 --- a/test/manipulations.cpp +++ b/test/manipulations.cpp @@ -103,10 +103,11 @@ TEST(Create, NodeAttr) { print(doc), "pie\n" ); - auto child = node->append_element({"urn:xmpp:fish:0", "shark"}); + auto child = node->append_element({"urn:xmpp:fish:0", "shark"}, fn()); + EXPECT_EQ(s.data(), child->value().data()); EXPECT_EQ( print(doc), - "\n\t\n\n" + "\n\ttuna\n\n" ); child->append_element({"urn:xmpp:fish:0", "species"}, "tiger"); EXPECT_EQ(