From e664ea552fa90fa70694df5aec88c27b82bfd889 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sat, 6 Jun 2015 01:17:42 +0200 Subject: [PATCH] stub the Record::Mutator implementation passes compiler again --- src/lib/diff/record.hpp | 114 ++++++++++++++++-- .../generic-record-representation-test.cpp | 2 +- 2 files changed, 108 insertions(+), 8 deletions(-) diff --git a/src/lib/diff/record.hpp b/src/lib/diff/record.hpp index 20ff4790a..5f87da6ad 100644 --- a/src/lib/diff/record.hpp +++ b/src/lib/diff/record.hpp @@ -73,6 +73,7 @@ //#include "lib/util.hpp" //#include "lib/format-string.hpp" +#include #include #include @@ -182,6 +183,34 @@ namespace diff{ return "booo"; ////TODO } + /** + * While otherwise immutable, + * a Record object can be remoulded + * with the help of a Mutator object + * @remarks a Mutator basically wraps a \em copy + * of the original object. After performing + * the desired changes, the altered copy can either + * be sliced out (by conversion), or moved overwriting + * an existing other Record instance (implemented as swap) + */ + class Mutator; + + + /** + * copy-initialise (or convert) from the given Mutator instance. + * @remarks need to code this explicitly, otherwise Record's + * build-from sequence templated ctor would kick in. + */ + Record (Mutator const& mut) + : Record((Record const&) mut) + { } + Record (Mutator && mut) + : Record(std::move ((Record) mut)) + { } + + friend class Mutator; + + /* ==== Exposing scope and contents for iteration ====== */ @@ -190,16 +219,15 @@ namespace diff{ using keyIter = TransformIter; using valIter = TransformIter; - - iterator begin () const { return iterator(this, attribs_.begin()); } - iterator end () const { return iterator(); } - + /** default iteration exposes all data within this "object", starting with the attributes */ + iterator begin () const { return iterator(this, attribs_.begin()); } + iterator end () const { return iterator(); } scopeIter attribs() const { return iter_stl::eachElm(attribs_); } - scopeIter scope() const { return iter_stl::eachElm(children_); } + scopeIter scope() const { return iter_stl::eachElm(children_); } - keyIter keys() const { return transformIterator(attribs(), extractKey); } - valIter vals() const { return transformIterator(attribs(), extractVal); } + keyIter keys() const { return transformIterator(attribs(), extractKey); } + valIter vals() const { return transformIterator(attribs(), extractVal); } protected: /* ==== API for the IterAdapter ==== */ @@ -255,6 +283,12 @@ namespace diff{ return "todo"; ////TODO } + static VAL + buildTypeAttribute (string const& typeID) + { + return VAL(); ///TODO + } + static string extractKey (VAL const& v) { @@ -283,6 +317,72 @@ namespace diff{ + template + class Record::Mutator + : boost::noncopyable + { + using Rec = Record; + + Rec record_; + + public: + explicit + Mutator (Rec const& startingPoint) + : record_(startingPoint) + { } + + explicit + Mutator (Rec && startingPoint) + : record_(std::move (startingPoint)) + { } + + operator Rec&() + { + return record_; + } + + void + replace (Rec& existingInstance) noexcept + { + std::swap (existingInstance, record_); + } + + + /* === functions to alter contents === */ + + void + setType (string const& newTypeID) + { + set ("type", Rec::buildTypeAttribute (newTypeID)); + record_.type_ = newTypeID; + } + + void + set (string const& key, VAL const& newValue) + { + ///////////TODO; + } + + void + appendChild (VAL const& newChild) + { + record_.children_.push_back (newChild); + } + + void + prependChild (VAL const& newChild) + { + record_.children_.insert (record_.children_.begin(), newChild); + } + + bool + empty() const + { + return record_.empty(); + } + }; + + }} // namespace lib::diff #endif /*LIB_DIFF_GEN_NODE_H*/ diff --git a/tests/library/diff/generic-record-representation-test.cpp b/tests/library/diff/generic-record-representation-test.cpp index 549bd8ed6..13f0da01f 100644 --- a/tests/library/diff/generic-record-representation-test.cpp +++ b/tests/library/diff/generic-record-representation-test.cpp @@ -273,7 +273,7 @@ namespace test{ mut.appendChild("a"); mut.set("a", "1"); - RecS aa(mut); + RecS aa(std::move(mut)); CHECK (a != aa); CHECK ("u" == aa.getType()); CHECK (Seq({"type=u", "a=1", "a"}) == contents(aa));