]> git.sesse.net Git - xml-template/commitdiff
Add Alternate support to C++0x version, and corresponding attribute2 test.
authorsgunderson@bigfoot.com <>
Wed, 21 Sep 2011 23:11:22 +0000 (01:11 +0200)
committersgunderson@bigfoot.com <>
Wed, 21 Sep 2011 23:11:22 +0000 (01:11 +0200)
c++0x/Makefile
c++0x/attribute2.cpp [new file with mode: 0644]
c++0x/xml-template.cpp
c++0x/xml-template.h

index b2528dfbb78e37062eebbc4d44df0d3294efdb15..3f24295a1ffdbeb0ccb930029a650203ebb175fb 100644 (file)
@@ -15,6 +15,9 @@ clone: clone.o $(LIBS)
 attribute: attribute.o $(LIBS)
        $(CXX) -o $@ $< $(LIBS) $(LDFLAGS)
 
+attribute2: attribute2.o $(LIBS)
+       $(CXX) -o $@ $< $(LIBS) $(LDFLAGS)
+
 namespace: namespace.o $(LIBS)
        $(CXX) -o $@ $< $(LIBS) $(LDFLAGS)
 
diff --git a/c++0x/attribute2.cpp b/c++0x/attribute2.cpp
new file mode 100644 (file)
index 0000000..94cf17d
--- /dev/null
@@ -0,0 +1,21 @@
+#include <stdio.h>
+
+#include "xml-template.h"
+
+using namespace std;
+
+int main(int argc, char **argv)
+{
+       Substitute master_directive = {
+               make_pair("color", new Replace("blue")),
+               make_pair("#things", new Alternate { "li/class", {
+                       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")), },
+               }, { "odd", "even" } }),
+       };
+
+       process_file("../xml/clone.xml", argv[1], &master_directive);
+       return(0);
+}
index fda97fc7ca97b83e28b8b4ab1f3d35558cd3011f..9e19f0914c03a9a14f7a6d9ad8db6dcf68920441 100644 (file)
@@ -49,7 +49,14 @@ string Replace::get_contents() { return str; }
 
 Clone::Clone(const vector<Directive *> &subdirectives)
        : subdirectives(subdirectives) {}
-       
+
+Clone::Clone(const vector<Substitute *> &subdirectives_subs)
+{
+       for (auto it : subdirectives_subs) {
+               subdirectives.push_back(static_cast<Directive *>(it));
+       }
+}
+
 Clone::Clone(initializer_list<Directive *> subdirectives)
        : subdirectives(subdirectives) {}
 
@@ -89,6 +96,19 @@ void Clone::process(xmlNode *node, bool clean)
                clean_node(node);
        }
 }
+       
+Alternate::Alternate(const string &attribute,
+                    const vector<Substitute *> &subdirectives_subs,
+                    const vector<string> &alternatives)
+    : Clone(subdirectives_subs)
+{
+       for (unsigned ix = 0; ix < subdirectives_subs.size(); ++ix) {
+               string value = alternatives[ix % alternatives.size()];
+               subdirectives_subs[ix]->substitution_map.insert(make_pair(
+                       attribute,
+                       new Replace { value }));
+       } 
+}
 
 Substitute::Substitute(const unordered_map<string, Directive*> &substitution_map)
        : substitution_map(substitution_map) {}
index 4728dbf088379b5edc998285408e28ed07e06aff..d29cbaeeb5b17594a5e5db7fd5ea80fcab4fae60 100644 (file)
@@ -25,15 +25,18 @@ class Replace : public Directive {
        const std::string str;
 };
 
+class Substitute;
+
 class Clone : public Directive {
  public:
        Clone(const std::vector<Directive *> &subdirectives);
+       Clone(const std::vector<Substitute *> &subdirectives);
        Clone(std::initializer_list<Directive *> subdirectives);
        ~Clone();
        virtual void process(xmlNode *node, bool clean);
 
  private:
-       const std::vector<Directive *> subdirectives;
+       std::vector<Directive *> subdirectives;
 };
 
 class Substitute : public Directive {
@@ -45,7 +48,15 @@ class Substitute : public Directive {
        virtual void process(xmlNode *node, bool clean);
 
  private:
-       const std::unordered_map<std::string, Directive*> substitution_map;
+       friend class Alternate;
+       std::unordered_map<std::string, Directive*> substitution_map;
+};
+
+class Alternate : public Clone {
+ public:
+       Alternate(const std::string &attribute,
+                 const std::vector<Substitute *> &subdirectives_subs,
+                 const std::vector<std::string> &alternatives);
 };
 
 void process_file(const std::string &input_filename,