From 4465ec1a182b3e2ebc6a681e2e53a185e774cb0a Mon Sep 17 00:00:00 2001 From: "sgunderson@bigfoot.com" <> Date: Wed, 21 Sep 2011 23:27:03 +0200 Subject: [PATCH] Make the C++0x syntax a lot more concise using initializer lists. --- c++0x/clone.cpp | 37 ++++++++++--------------------------- c++0x/passthru.cpp | 4 ++-- c++0x/simple.cpp | 9 +++++---- c++0x/xml-template.cpp | 6 ++++++ c++0x/xml-template.h | 2 ++ 5 files changed, 25 insertions(+), 33 deletions(-) diff --git a/c++0x/clone.cpp b/c++0x/clone.cpp index 5bdfbc6..81b6d11 100644 --- a/c++0x/clone.cpp +++ b/c++0x/clone.cpp @@ -6,33 +6,16 @@ using namespace std; int main(int argc, char **argv) { - vector things; + Substitute master_directive = { + make_pair("color", new Replace("blue")), + make_pair("#things", new Clone { + new Substitute { make_pair("li", new Replace("Raindrops on roses")) }, + new Substitute { make_pair("li", new Replace("Whiskers on kittens")) }, + new Substitute { make_pair("li", new Replace("Bright copper kettles")) }, + new Substitute { make_pair("li", new Replace("Warm, woolen mittens")) }, + }), + }; - { - unordered_map submap; - submap.insert(make_pair("li", new Replace("Raindrops on roses"))); - things.push_back(new Substitute(submap)); - } - { - unordered_map submap; - submap.insert(make_pair("li", new Replace("Whiskers on kittens"))); - things.push_back(new Substitute(submap)); - } - { - unordered_map submap; - submap.insert(make_pair("li", new Replace("Bright copper kettles"))); - things.push_back(new Substitute(submap)); - } - { - unordered_map submap; - submap.insert(make_pair("li", new Replace("Warm, woolen mittens"))); - things.push_back(new Substitute(submap)); - } - - unordered_map master_map; - master_map.insert(make_pair("color", new Replace("blue"))); - master_map.insert(make_pair("#things", new Clone(things))); - - process_file("../xml/clone.xml", argv[1], new Substitute(master_map)); + process_file("../xml/clone.xml", argv[1], &master_directive); return(0); } diff --git a/c++0x/passthru.cpp b/c++0x/passthru.cpp index afc38ec..f4fc636 100644 --- a/c++0x/passthru.cpp +++ b/c++0x/passthru.cpp @@ -8,7 +8,7 @@ using namespace std; int main(int argc, char **argv) { - unordered_map master_map; - process_file("../xml/passthru.xml", argv[1], new Substitute(master_map)); + Substitute master_directive = {}; + process_file("../xml/passthru.xml", argv[1], &master_directive); return(0); } diff --git a/c++0x/simple.cpp b/c++0x/simple.cpp index 36766f8..5ac223d 100644 --- a/c++0x/simple.cpp +++ b/c++0x/simple.cpp @@ -6,10 +6,11 @@ using namespace std; int main(int argc, char **argv) { - unordered_map master_map; - master_map.insert(make_pair("title", new Replace("A very basic example"))); - master_map.insert(make_pair("#hello", new Replace("Hello world!"))); + Substitute master_directive = { + make_pair("title", new Replace("A very basic example")), + make_pair("#hello", new Replace("Hello world!")), + }; - process_file("../xml/simple.xml", argv[1], new Substitute(master_map)); + process_file("../xml/simple.xml", argv[1], &master_directive); return(0); } diff --git a/c++0x/xml-template.cpp b/c++0x/xml-template.cpp index 1e72761..62b5dba 100644 --- a/c++0x/xml-template.cpp +++ b/c++0x/xml-template.cpp @@ -33,6 +33,9 @@ void Replace::process(xmlNode *node, bool clean) { Clone::Clone(const std::vector &subdirectives) : subdirectives(subdirectives) {} + +Clone::Clone(std::initializer_list subdirectives) + : subdirectives(subdirectives) {} void Clone::process(xmlNode *node, bool clean) { // We can't use xmlNewDocFragment, since xmlDOMWrapCloneNode only knows @@ -59,6 +62,9 @@ void Clone::process(xmlNode *node, bool clean) { Substitute::Substitute(const unordered_map &substitution_map) : substitution_map(substitution_map) {} + +Substitute::Substitute(std::initializer_list> substitution_map) + : substitution_map(substitution_map) {} void Substitute::process(xmlNode *node, bool clean) { for (xmlNode *child = node->children; child != NULL; child = child->next) { diff --git a/c++0x/xml-template.h b/c++0x/xml-template.h index eb13211..f0da0b8 100644 --- a/c++0x/xml-template.h +++ b/c++0x/xml-template.h @@ -25,6 +25,7 @@ class Replace : public Directive { class Clone : public Directive { public: Clone(const std::vector &subdirectives); + Clone(std::initializer_list subdirectives); virtual void process(xmlNode *node, bool clean); private: @@ -34,6 +35,7 @@ class Clone : public Directive { class Substitute : public Directive { public: Substitute(const std::unordered_map &substitution_map); + Substitute(std::initializer_list> substitution_map); virtual void process(xmlNode *node, bool clean); -- 2.39.2