]> git.sesse.net Git - xml-template/commitdiff
Make the C++0x syntax a lot more concise using initializer lists.
authorsgunderson@bigfoot.com <>
Wed, 21 Sep 2011 21:27:03 +0000 (23:27 +0200)
committersgunderson@bigfoot.com <>
Wed, 21 Sep 2011 21:27:03 +0000 (23:27 +0200)
c++0x/clone.cpp
c++0x/passthru.cpp
c++0x/simple.cpp
c++0x/xml-template.cpp
c++0x/xml-template.h

index 5bdfbc66849771e40f876916a48df7a06d2e4446..81b6d11051790b307827ad10c7a2a56ffab695bc 100644 (file)
@@ -6,33 +6,16 @@ using namespace std;
 
 int main(int argc, char **argv)
 {
-       vector<Directive *> 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<string, Directive *> submap;
-               submap.insert(make_pair("li", new Replace("Raindrops on roses")));
-               things.push_back(new Substitute(submap));
-       }
-       {
-               unordered_map<string, Directive *> submap;
-               submap.insert(make_pair("li", new Replace("Whiskers on kittens")));
-               things.push_back(new Substitute(submap));
-       }
-       {
-               unordered_map<string, Directive *> submap;
-               submap.insert(make_pair("li", new Replace("Bright copper kettles")));
-               things.push_back(new Substitute(submap));
-       }
-       {
-               unordered_map<string, Directive *> submap;
-               submap.insert(make_pair("li", new Replace("Warm, woolen mittens")));
-               things.push_back(new Substitute(submap));
-       }
-
-       unordered_map<string, Directive *> 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);
 }
index afc38ec129bc9d8f6a0b3337706efc404fa8847f..f4fc63692a83a7d873b6552ac0ee390f65cb830b 100644 (file)
@@ -8,7 +8,7 @@ using namespace std;
 
 int main(int argc, char **argv)
 {
-       unordered_map<string, Directive*> 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);
 }
index 36766f80c1ffa4acb01e647a7dd723691e6a0a0f..5ac223d09f72eda03769dd1d1bef78b964cf8de3 100644 (file)
@@ -6,10 +6,11 @@ using namespace std;
 
 int main(int argc, char **argv)
 {
-       unordered_map<string, Directive*> 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);
 }
index 1e727613160acf49fb1ec860b88179b0fd7aecb5..62b5dbaee46599f3a8417b8c5c25f997ee59c1b0 100644 (file)
@@ -33,6 +33,9 @@ void Replace::process(xmlNode *node, bool clean) {
 
 Clone::Clone(const std::vector<Directive *> &subdirectives)
        : subdirectives(subdirectives) {}
+       
+Clone::Clone(std::initializer_list<Directive *> 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<string, Directive*> &substitution_map)
        : substitution_map(substitution_map) {}
+       
+Substitute::Substitute(std::initializer_list<std::pair<const std::string, Directive*>> substitution_map)
+       : substitution_map(substitution_map) {}
 
 void Substitute::process(xmlNode *node, bool clean) {
        for (xmlNode *child = node->children; child != NULL; child = child->next) {
index eb132110e8f80604caea2ee12723bce71be26b08..f0da0b8b0cdd1d007aca0b35f2b33457b2d47c3b 100644 (file)
@@ -25,6 +25,7 @@ class Replace : public Directive {
 class Clone : public Directive {
  public:
        Clone(const std::vector<Directive *> &subdirectives);
+       Clone(std::initializer_list<Directive *> 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<std::string, Directive*> &substitution_map);
+       Substitute(std::initializer_list<std::pair<const std::string, Directive*>> substitution_map);
 
        virtual void process(xmlNode *node, bool clean);