From a87410331a3cf42444d92930a9a796a384278c8d Mon Sep 17 00:00:00 2001 From: "sgunderson@bigfoot.com" <> Date: Thu, 22 Sep 2011 01:11:22 +0200 Subject: [PATCH] Add Alternate support to C++0x version, and corresponding attribute2 test. --- c++0x/Makefile | 3 +++ c++0x/attribute2.cpp | 21 +++++++++++++++++++++ c++0x/xml-template.cpp | 22 +++++++++++++++++++++- c++0x/xml-template.h | 15 +++++++++++++-- 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 c++0x/attribute2.cpp diff --git a/c++0x/Makefile b/c++0x/Makefile index b2528df..3f24295 100644 --- a/c++0x/Makefile +++ b/c++0x/Makefile @@ -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 index 0000000..94cf17d --- /dev/null +++ b/c++0x/attribute2.cpp @@ -0,0 +1,21 @@ +#include + +#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); +} diff --git a/c++0x/xml-template.cpp b/c++0x/xml-template.cpp index fda97fc..9e19f09 100644 --- a/c++0x/xml-template.cpp +++ b/c++0x/xml-template.cpp @@ -49,7 +49,14 @@ string Replace::get_contents() { return str; } Clone::Clone(const vector &subdirectives) : subdirectives(subdirectives) {} - + +Clone::Clone(const vector &subdirectives_subs) +{ + for (auto it : subdirectives_subs) { + subdirectives.push_back(static_cast(it)); + } +} + Clone::Clone(initializer_list subdirectives) : subdirectives(subdirectives) {} @@ -89,6 +96,19 @@ void Clone::process(xmlNode *node, bool clean) clean_node(node); } } + +Alternate::Alternate(const string &attribute, + const vector &subdirectives_subs, + const vector &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 &substitution_map) : substitution_map(substitution_map) {} diff --git a/c++0x/xml-template.h b/c++0x/xml-template.h index 4728dbf..d29cbae 100644 --- a/c++0x/xml-template.h +++ b/c++0x/xml-template.h @@ -25,15 +25,18 @@ class Replace : public Directive { const std::string str; }; +class Substitute; + class Clone : public Directive { public: Clone(const std::vector &subdirectives); + Clone(const std::vector &subdirectives); Clone(std::initializer_list subdirectives); ~Clone(); virtual void process(xmlNode *node, bool clean); private: - const std::vector subdirectives; + std::vector 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 substitution_map; + friend class Alternate; + std::unordered_map substitution_map; +}; + +class Alternate : public Clone { + public: + Alternate(const std::string &attribute, + const std::vector &subdirectives_subs, + const std::vector &alternatives); }; void process_file(const std::string &input_filename, -- 2.39.2