Skip to content

Commit

Permalink
Merge pull request #20 from dwd/avoid-temps
Browse files Browse the repository at this point in the history
Avoid temporary when setting value via *_element
  • Loading branch information
dwd authored Jul 18, 2024
2 parents 3c18941 + c016c30 commit d00e499
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
47 changes: 19 additions & 28 deletions rapidxml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ namespace rapidxml
}
optional_ptr<xml_node<Ch>> allocate_element(std::initializer_list<const Ch *> 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:
Expand Down Expand Up @@ -1252,19 +1252,16 @@ namespace rapidxml
auto prepend_node(optional_ptr<xml_node<Ch>> ptr) {
return prepend_node(ptr.get());
}
template<typename... Args>
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<typename... Args>
auto prepend_element(std::tuple<view_type, view_type> const & il, Args... args) {
auto child = allocate_element(il, args...);
auto prepend_element(std::tuple<view_type, view_type> const & il, view_type const & value = {}) {
auto child = allocate_element(il, value);
return prepend_node(child);
}
template<typename... Args>
auto prepend_element(std::initializer_list<const Ch *> const & il, Args... args) {
auto child = allocate_element(il, args...);
auto prepend_element(std::initializer_list<const Ch *> const & il, view_type const & value = {}) {
auto child = allocate_element(il, value);
return prepend_node(child);
}

Expand Down Expand Up @@ -1293,19 +1290,16 @@ namespace rapidxml
optional_ptr<xml_node<Ch>> append_node(optional_ptr<xml_node<Ch>> ptr) {
return append_node(ptr.get());
}
template<typename... Args>
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<typename... Args>
auto append_element(std::tuple<view_type, view_type> const & il, Args... args) {
auto child = allocate_element(il, args...);
auto append_element(std::tuple<view_type, view_type> const & il, view_type const & value = {}) {
auto child = allocate_element(il, value);
return append_node(child);
}
template<typename... Args>
auto append_element(std::initializer_list<const Ch *> const & il, Args... args) {
auto child = allocate_element(il, args...);
auto append_element(std::initializer_list<const Ch *> const & il, view_type const & value = {}) {
auto child = allocate_element(il, value);
return append_node(child);
}

Expand Down Expand Up @@ -1335,19 +1329,16 @@ namespace rapidxml
auto insert_node(optional_ptr<xml_node<Ch>> where, optional_ptr<xml_node<Ch>> ptr) {
return insert_node(where.ptr(), ptr.ptr());
}
template<typename... Args>
auto insert_element(optional_ptr<xml_node<Ch>> where, view_type const & v, Args... args) {
auto child = allocate_element(v, args...);
auto insert_element(optional_ptr<xml_node<Ch>> where, view_type const & v, view_type const & value = {}) {
auto child = allocate_element(v, value);
return insert_node(where, child);
}
template<typename... Args>
auto insert_element(optional_ptr<xml_node<Ch>> where, std::tuple<view_type, view_type> const & il, Args... args) {
auto child = allocate_element(il, args...);
auto insert_element(optional_ptr<xml_node<Ch>> where, std::tuple<view_type, view_type> const & il, view_type const & value = {}) {
auto child = allocate_element(il, value);
return insert_node(where, child);
}
template<typename... Args>
auto insert_element(optional_ptr<xml_node<Ch>> where, std::initializer_list<const Ch *> const & il, Args... args) {
auto child = allocate_element(il, args...);
auto insert_element(optional_ptr<xml_node<Ch>> where, std::initializer_list<const Ch *> const & il, view_type const & value = {}) {
auto child = allocate_element(il, value);
return insert_node(where, child);
}

Expand Down
5 changes: 3 additions & 2 deletions test/manipulations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@ TEST(Create, NodeAttr) {
print(doc),
"<fish>pie</fish>\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),
"<fish>\n\t<shark xmlns=\"urn:xmpp:fish:0\"/>\n</fish>\n"
"<fish>\n\t<shark xmlns=\"urn:xmpp:fish:0\">tuna</shark>\n</fish>\n"
);
child->append_element({"urn:xmpp:fish:0", "species"}, "tiger");
EXPECT_EQ(
Expand Down

0 comments on commit d00e499

Please sign in to comment.