From c3d767b4448df93c19dc8d7853cb23178ddb1d37 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Fri, 2 Oct 2009 03:35:48 +0200 Subject: [PATCH] try out a recursive approach for the extensible ID more of a general feasability study... to be continued later --- src/lib/sub-id.hpp | 67 +++++++++++++++++++++++++++++++++++++-- tests/lib/sub-id-test.cpp | 25 +++++++++++++-- 2 files changed, 88 insertions(+), 4 deletions(-) diff --git a/src/lib/sub-id.hpp b/src/lib/sub-id.hpp index 8706a03aa..257dc2638 100644 --- a/src/lib/sub-id.hpp +++ b/src/lib/sub-id.hpp @@ -33,6 +33,9 @@ ** - possibility of structured adornments and variations ** - optionally concealing these extensions from the interface level ** + ** The first attempt to build such an entity is based on standard techniques, + ** disregarding performance and memory footprint considerations. + ** ** @see SubID_test ** @see MultiFact ** @@ -43,11 +46,17 @@ #ifndef LIB_SUB_ID_H #define LIB_SUB_ID_H +#include "lib/format.hpp" + //#include +#include +#include namespace lib { - + + using std::string; + using std::ostream; @@ -55,11 +64,65 @@ namespace lib { * */ template - struct SubID + class SubId; + + class SubID { + public: + virtual ~SubID() { } + + virtual operator string() const =0; + }; + + + ostream& + operator<< (ostream& os, SubID const& sID) + { + return os << string(sID); + } + + + + template + class SubId + : public SubID + { + I baseID_; + + public: + SubId (I id) + : baseID_(id) + { } + + operator string() const + { + return util::str (baseID_); + } }; + template + class ExtendedSubId + : public SubId + { + typedef SubId _baID; + + SUZ extID_; + + public: + ExtendedSubId (I i, SUZ const& chain) + : _baID(i) + , extID_(chain) + { } + + operator string() const + { + return _baID::operator string() + + '.' + + string (extID_); + } + }; + diff --git a/tests/lib/sub-id-test.cpp b/tests/lib/sub-id-test.cpp index dfa99046c..e51635b20 100644 --- a/tests/lib/sub-id-test.cpp +++ b/tests/lib/sub-id-test.cpp @@ -32,6 +32,7 @@ #include + namespace lib { namespace test{ @@ -41,10 +42,14 @@ namespace test{ using boost::hash; using std::vector; using std::string; - + using std::cout; + using std::endl; + namespace { // test data + enum Colour { R,G,B }; + } @@ -64,11 +69,11 @@ namespace test{ virtual void run (Arg) { - UNIMPLEMENTED ("SubID brainstorming"); checkBaseType(); checkExtension(); + TODO ("Hash functions, better implementation"); // buildHashtable (buildIDs() ); // buildHashtable (buildIDs() ); } @@ -77,12 +82,28 @@ namespace test{ void checkBaseType () { + typedef SubId CID; + CID c1 (R); + CID c2 (G); + CID c3 (B); + + cout << "..." << c1 << c2 << c3 << endl; } void checkExtension () { + typedef SubId CID; + typedef SubId UID; + + typedef ExtendedSubId CUID; + + SubID const& id1 = CUID(R, 12); + SubID const& id2 = CUID(G, 13); + + cout << "id1=" << id1 << endl; + cout << "id2=" << id2 << endl; }