From 96e10faa84db652e21eb9bcabef5f388277b4de3 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Fri, 5 Jun 2015 19:17:39 +0200 Subject: [PATCH] WIP: first round of stubbing for diff::Record --- src/lib/diff/record.hpp | 192 +++++++++++++++++- .../generic-record-representation-test.cpp | 77 ++++--- 2 files changed, 243 insertions(+), 26 deletions(-) diff --git a/src/lib/diff/record.hpp b/src/lib/diff/record.hpp index e773c1d2b..ac217c7e3 100644 --- a/src/lib/diff/record.hpp +++ b/src/lib/diff/record.hpp @@ -67,10 +67,16 @@ #include "lib/error.hpp" +#include "lib/iter-adapter.hpp" +#include "lib/iter-adapter-stl.hpp" +#include "lib/itertools.hpp" + //#include "lib/util.hpp" //#include "lib/format-string.hpp" -//#include +#include +#include +#include //#include @@ -80,6 +86,7 @@ namespace diff{ namespace error = lumiera::error; //using util::_Fmt; + using std::string; @@ -87,8 +94,191 @@ namespace diff{ template class Record { + using _Vec = std::vector; + using Attrib = std::pair; + using Attribs = _Vec; + using Children = _Vec; + + string type_; + Attribs attribs_; + Children children_; public: + Record() + : type_("NIL") + { } + + template + Record(Symbol typeID, A&& att, C&& chi) + : type_(typeID) + , attribs_(std::forward (att)) + , children_(std::forward (chi)) + { } + + template + Record(Symbol typeID, std::initializer_list const&& att + , std::initializer_list const&& chi) + : type_(typeID) + , attribs_(att) + , children_(chi) + { } + + template + explicit + Record (SEQ const& con) + : type_("NIL") + { + auto p = std::begin(con); + auto e = std::end(con); + if (p!=e && isTypeID (*p)) + type_ = extractTypeID(*(p++)); + for ( ; p!=e && isAttribute(*p); ++p) + attribs_.push_back (*p); + for ( ; p!=e; ++p) + children_.push_back (*p); + } + + Record (std::initializer_list const&& ili) + : Record(ili) + { } + + // all default copy operations acceptable + + + operator std::string() const + { + return "nebbich"; ////TODO + } + + + bool + empty() const + { + return attribs_.empty() + && children_.empty(); + } + + string + getType() const + { + return type_; + } + + bool + hasAttribute (string key) const + { + return false; ////TODO + } + + bool + contains (VAL const& ref) const + { + return false; ////TODO + } + + VAL const& + get (string key) const + { + return "booo"; ////TODO + } + + + /* ==== Exposing scope and contents for iteration ====== */ + + using iterator = IterAdapter; + using scopeIter = typename iter_stl::_SeqT<_Vec>::Range; + using keyIter = TransformIter; + using valIter = TransformIter; + + + 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_); } + + keyIter keys() const { return transformIterator(attribs(), extractKey); } + valIter vals() const { return transformIterator(attribs(), extractVal); } + + protected: /* ==== API for the IterAdapter ==== */ + + /** Implementation of Iteration-logic: pull next element. */ + template + friend void + iterNext (const Record* src, ITER& pos) + { + ++pos; + checkPoint (src,pos); + } + + /** Implementation of Iteration-logic: detect iteration end. + * @remarks seamless continuation of the iteration when reaching + * the end of the attribute collection. In this implementation, + * we use the default constructed \c ITER() to mark iteration end. + */ + template + friend bool + checkPoint (const Record* src, ITER& pos) + { + REQUIRE (src); + if ((pos != ITER()) && (pos != src->children_.end())) + return true; + else + if (pos != ITER() && (pos == src->attribs_.end()) && !src->children_.empty()) + { + pos = src->children_.begin(); + return true; + } + else + { + pos = ITER(); + return false; + } } + + private: + static bool + isAttribute (VAL const& v) + { + return false; ////TODO + } + + static bool + isTypeID (VAL const& v) + { + return false; ////TODO + } + + static string + extractTypeID (VAL const& v) + { + return "todo"; ////TODO + } + + static string + extractKey (VAL const& v) + { + return "todo"; ////TODO + } + + static VAL + extractVal (VAL const& v) + { + return VAL(); ///TODO + } + + + friend bool + operator== (Record const& r1, Record const& r2) + { + return false; ////TODO + } + + friend bool + operator!= (Record const& r1, Record const& r2) + { + return ! (r1 == r2); + } }; diff --git a/tests/library/diff/generic-record-representation-test.cpp b/tests/library/diff/generic-record-representation-test.cpp index af036cc04..e4ad79e17 100644 --- a/tests/library/diff/generic-record-representation-test.cpp +++ b/tests/library/diff/generic-record-representation-test.cpp @@ -24,15 +24,17 @@ #include "lib/test/run.hpp" #include "lib/test/test-helper.hpp" #include "lib/diff/record.hpp" +#include "lib/itertools.hpp" #include "lib/util.hpp" //////TODO necessary? #include //#include -//#include +#include #include -//using std::string; +using std::string; using util::isSameObject; +using util::isnil; using std::vector; //using std::swap; using std::cout; @@ -44,16 +46,41 @@ namespace diff{ namespace test{ // using lumiera::error::LUMIERA_ERROR_LOGIC; + using lumiera::error::LUMIERA_ERROR_INVALID; namespace {//Test fixture.... + using Seq = vector; + using RecS = Record; + + template + inline Seq + contents (IT const& it) + { + Seq collected; + append_all (it, collected); + return collected; + } + + inline Seq + contents (RecS const& rec_of_strings) + { + return contents (rec_of_strings.begin()); + } + + template + inline Seq + strings (std::initializer_list const& con) + { + Seq collected; + for (auto elm : con) + collected.push_back(elm); + return collected; + } }//(End)Test fixture - using Seq = vector; - using RecS = Record; - @@ -85,13 +112,13 @@ namespace test{ simpleUsage() { RecS enterprise("starship" - , {"Name = USS Enterprise" - ,"Registry = NCC-1701-D" - ,"Class = Galaxy" - ,"Owner = United Federation of Planets" - ,"built=2363" - } - , {"Picard", "Riker", "Data", "Worf", "Troi", "Crusher", "La Forge"} + , strings ({"Name = USS Enterprise" + ,"Registry = NCC-1701-D" + ,"Class = Galaxy" + ,"Owner = United Federation of Planets" + ,"built=2363" + }) + , strings ({"Picard", "Riker", "Data", "Troi", "Worf", "Crusher", "La Forge"}) ); CHECK (enterprise.getType() == "starship"); @@ -102,15 +129,15 @@ namespace test{ CHECK (!enterprise.hasAttribute("Owner ")); CHECK (enterprise.contains("Data")); - CHECK (!contains (enterprise, "Woof")); + CHECK (!enterprise.contains("Woof")); CHECK (util::contains (enterprise, "Worf")); VERIFY_ERROR (INVALID, enterprise.get("warp10")); cout << "enterprise = " << string(enterprise)<