diff --git a/src/lib/ha-id.hpp b/src/lib/ha-id.hpp index 9230c8810..8e56b674d 100644 --- a/src/lib/ha-id.hpp +++ b/src/lib/ha-id.hpp @@ -21,7 +21,27 @@ */ -/** @file ha-id.hpp +/** @file ha-id.hpp + ** A template for generating hash based ID tags carrying compile-time type info. + ** While the actual storage is assumed to be based on a POD, the type info is crucial + ** to circumvent the problems with an "object" base class. Frequently, the need to + ** manage some objects in a central facility drives us to rely on a common base class, + ** irrespective of an actual common denominator in the semantics of the objects to + ** be managed within this collection. Typically this results in this common base class + ** being almost worthless as an API or interface, causing lots of type casts when using + ** such a common object management facility. Passing additional context or API information + ** on a metaprogramming level through the management interface helps avoiding these + ** shortcomings. + ** + ** Here we build an ID facility with the following properties: + ** - based on a configurable storage/implementation of the actual hash or index code. + ** - tied to a specific hierarchy of objects (template parameter "BA") + ** - providing an additional template parameter to pass the desired type info + ** - establishing an type hierarchy relation between ID template instances, such that + ** the IDs typed to the derived/specialised objects can stand-in for the generic + ** ID typed to the base class. + ** - providing a Mixin, which allows any hierarchy to use this facility without + ** much code duplication. ** ** @see HaID_test ** @see Placement usage example @@ -33,32 +53,50 @@ #ifndef LIB_HA_ID_H #define LIB_HA_ID_H -//#include "pre.hpp" -//#include "proc/mobject/session/locatingpin.hpp" -//#include "proc/asset/pipe.hpp" +//#include "lib/util.hpp" //#include +#include namespace lib { // using std::tr1::shared_ptr; + using std::rand; - /** @todo WIP a generic hash-index, maybe also usable for assets */ struct LuidH { long dummy_; + + LuidH() : dummy_(rand()) {} }; + /** + * Generic hash based and hierarchically typed ID + * @todo WIP maybe also usable for assets? + */ template struct HaID; template struct HaIndexed { - LuidH id_; - HaID const& getID() const; + typedef HaID RootID; + RootID id_; + + RootID const& getID() const + { + return id_; + } + void resetID(HaIndexed const& ref) + { + this->id_ = ref.getID(); + } + void resetID(RootID const& ref) ///< @todo this mutator should be removed in the final version to keep the actual hash opaque + { + this->id_.dummy_ = ref.dummy_; + } }; template @@ -75,52 +113,8 @@ namespace lib { HaID (T const& ref) : HaID (ref) {} }; - template - inline HaID const& - HaIndexed::getID() const - { - return *(static_cast*> (&id_)); - } - - - struct Base - { - int ii_; - }; - - struct TestA : Base, HaIndexed - { - }; - struct TestBA : TestA {}; - struct TestBB : TestA {}; - - - - /** - */ - class PlacementRef - { - public: - /** - * - */ - class ID - { - }; - - template - class IDp : public ID - { - }; - - - }; - - - - } // namespace lib #endif diff --git a/tests/40components.tests b/tests/40components.tests index d05bea6f9..bad8b8be3 100644 --- a/tests/40components.tests +++ b/tests/40components.tests @@ -215,6 +215,11 @@ out: dtor ~TargetObj(12) successful END +PLANNED "HaID_test" HaID_test < -//#include +#include +#include -//using boost::format; -//using lumiera::Time; -//using util::contains; -using std::string; -//using std::cout; +using boost::format; +//using std::string; +using std::cout; namespace lib { namespace test{ - - /** @todo WIP a generic hash-index, maybe also usable for assets */ - HaID hahaBB1; - TestBA bab; - HaID hahaBA1 (bab); - ///////////////////////////////TODO (Experimentation) + /* == a hierarchy of test-dummy objects to use the HaID == */ + + struct Base + { + int ii_; + }; + + struct TestA : Base, HaIndexed + { + }; + struct TestBA : TestA {}; + struct TestBB : TestA {}; + + /*************************************************************************** @@ -61,7 +62,21 @@ namespace test{ virtual void run (Arg) { + format fmt ("sizeof( %s ) = %d\n"); + /////////////////////////////////TODO + HaID hahaBB1; + + TestBA bab; + bab.resetID(hahaBB1); + + HaID hahaBA1 (bab); + + cout << fmt % "TestBA" % sizeof(bab); + cout << fmt % "ID" % sizeof(hahaBA1); + cout << fmt % "ID" % sizeof(hahaBB1); + + ASSERT (hahaBA1.dummy_ == hahaBB1.dummy_); } };