augment test, rework access to the hashID, template on hash impl

This commit is contained in:
Fischlurch 2009-05-23 05:13:51 +02:00
parent e5d2042190
commit 2bb64a23fa
4 changed files with 63 additions and 46 deletions

View file

@ -1,5 +1,5 @@
/* /*
HA-ID.hpp - generic hash based and hierarchically typed ID HASH-INDEXED.hpp - generic hash based and typed ID
Copyright (C) Lumiera.org Copyright (C) Lumiera.org
2009, Hermann Vosseler <Ichthyostega@web.de> 2009, Hermann Vosseler <Ichthyostega@web.de>
@ -21,7 +21,7 @@
*/ */
/** @file ha-id.hpp /** @file hash-indexed.hpp
** A template for generating hash based ID tags carrying compile-time type info. ** 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 ** 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 ** to circumvent the problems with an "object" base class. Frequently, the need to
@ -37,9 +37,9 @@
** - based on a configurable storage/implementation of the actual hash or index code. ** - based on a configurable storage/implementation of the actual hash or index code.
** - tied to a specific hierarchy of objects (template parameter "BA") ** - tied to a specific hierarchy of objects (template parameter "BA")
** - providing an additional template parameter to pass the desired type info ** - providing an additional template parameter to pass the desired type info
** - establishing an type hierarchy relation between ID template instances, such that ** - establishing an type hierarchy relation between ID related to the base class
** the IDs typed to the derived/specialised objects can stand-in for the generic ** and the IDs denoting specific subclasses, such that the latter can stand-in
** ID typed to the base class. ** for the generic ID.
** - providing a Mixin, which allows any hierarchy to use this facility without ** - providing a Mixin, which allows any hierarchy to use this facility without
** much code duplication. ** much code duplication.
** **
@ -50,8 +50,8 @@
#ifndef LIB_HA_ID_H #ifndef LIB_HASH_INDEXED_H
#define LIB_HA_ID_H #define LIB_HASH_INDEXED_H
//#include "lib/util.hpp" //#include "lib/util.hpp"
@ -73,22 +73,29 @@ namespace lib {
}; };
template<class BA> /************************************************************
* A Mixin to add a private ID type to the target class,
* together with storage to hold an instance of this ID,
* getter and setter, and a templated version of the ID type
* which can be used to pass on specific subclass type info.
*/
template<class BA, class IMP>
struct HashIndexed struct HashIndexed
{ {
/**
* Generic hash based and hierarchically typed ID
* @todo WIP maybe also usable for assets?
*/
template<typename T>
struct Id;
struct ID : LuidH /**
* generic hash based ID, corresponding to the base class BA
*/
struct ID : IMP
{ {
ID () : LuidH () {} ID () : IMP () {}
ID (BA const& ref) : LuidH (ref.getID()) {} ID (BA const& ref) : IMP (ref.getID()) {}
ID (IMP const& ir) : IMP (ir) {}
}; };
/**
* Hash based ID, typed to a specific subclass of BA
*/
template<typename T> template<typename T>
struct Id : ID struct Id : ID
{ {
@ -96,18 +103,21 @@ namespace lib {
Id (T const& ref) : ID (ref) {} Id (T const& ref) : ID (ref) {}
}; };
ID const& getID() const ID const&
getID () const
{ {
return id_; return id_;
} }
void resetID(HashIndexed const& ref)
void
assignID (HashIndexed const& ref)
{ {
this->id_ = ref.getID(); this->id_ = ref.getID();
} }
void resetID(ID const& ref) ///< @todo this mutator should be removed in the final version to keep the actual hash opaque
{ protected:
this->id_.dummy_ = ref.dummy_; HashIndexed () : id_() {}
} HashIndexed (IMP const& iref) : id_(iref) {}
private: private:
ID id_; ID id_;

View file

@ -215,7 +215,7 @@ out: dtor ~TargetObj(12) successful
END END
PLANNED "HaID_test" HaID_test <<END PLANNED "HashIndexed_test" HashIndexed_test <<END
return: 0 return: 0
END END

View file

@ -42,7 +42,7 @@ test_lib_SOURCES = \
$(testlib_srcdir)/exceptionerrortest.cpp \ $(testlib_srcdir)/exceptionerrortest.cpp \
$(testlib_srcdir)/factoryspecialtest.cpp \ $(testlib_srcdir)/factoryspecialtest.cpp \
$(testlib_srcdir)/factorytest.cpp \ $(testlib_srcdir)/factorytest.cpp \
$(testlib_srcdir)/ha-id-test.cpp \ $(testlib_srcdir)/hash-indexed-test.cpp \
$(testlib_srcdir)/helloworldtest.cpp \ $(testlib_srcdir)/helloworldtest.cpp \
$(testlib_srcdir)/lifecycletest.cpp \ $(testlib_srcdir)/lifecycletest.cpp \
$(testlib_srcdir)/mainsuite.cpp \ $(testlib_srcdir)/mainsuite.cpp \

View file

@ -1,5 +1,5 @@
/* /*
HaID(Test) - proof-of-concept test for a hash based and typed ID HashIndexed(Test) - proof-of-concept test for a hash based and typed ID
Copyright (C) Lumiera.org Copyright (C) Lumiera.org
2009, Hermann Vosseler <Ichthyostega@web.de> 2009, Hermann Vosseler <Ichthyostega@web.de>
@ -23,12 +23,12 @@
#include "lib/test/run.hpp" #include "lib/test/run.hpp"
#include "lib/ha-id.hpp" #include "lib/hash-indexed.hpp"
#include <boost/format.hpp> //#include <boost/format.hpp>
#include <iostream> #include <iostream>
using boost::format; //using boost::format;
//using std::string; //using std::string;
using std::cout; using std::cout;
@ -40,49 +40,56 @@ namespace test{
struct Base struct Base
{ {
int ii_; long ii_;
}; };
struct TestA : Base, HashIndexed<TestA> struct TestB : Base, HashIndexed<TestB,LuidH>
{ {
TestB () {}
TestB (ID const& refID) : HashIndexed<TestB,LuidH>(refID) {}
}; };
struct TestBA : TestA {}; struct TestDA : TestB {};
struct TestBB : TestA {}; struct TestDB : TestB {};
/*************************************************************************** /***************************************************************************
* @test proof-of-concept test for a generic hash based and typed ID struct. * @test proof-of-concept test for a generic hash based and typed ID struct.
* @see lib::HaID * @see lib::HashIndexed::Id
*/ */
class HaID_test : public Test class HashIndexed_test : public Test
{ {
virtual void virtual void
run (Arg) run (Arg)
{ {
format fmt ("sizeof( %s ) = %d\n"); TestB::Id<TestDA> idDA;
/////////////////////////////////TODO TestB bb (idDA);
TestA::Id<TestBB> idBB1;
TestBA bab; TestB::Id<TestDB> idDB1 ;
bab.resetID (idBB1); TestB::Id<TestDB> idDB2 (idDB1);
TestA::Id<TestBA> idBA1 (bab); ASSERT (sizeof (idDB1) == sizeof (idDA) );
ASSERT (sizeof (TestB::ID) == sizeof (LuidH));
ASSERT (sizeof (TestDA) == sizeof (LuidH) + sizeof (Base));
cout << fmt % "TestBA" % sizeof(bab); ASSERT (idDA.dummy_ == bb.getID().dummy_);
cout << fmt % "Id<TestBA>" % sizeof(idBA1); ASSERT (idDB1.dummy_ == idDB2.dummy_);
cout << fmt % "Id<TestBB>" % sizeof(idBB1);
ASSERT (idBA1.dummy_ == idBB1.dummy_); TestDA d1;
TestDA d2;
ASSERT (d1.getID().dummy_ != d2.getID().dummy_);
d2 = d1;
ASSERT (d1.getID().dummy_ == d2.getID().dummy_);
} }
}; };
/** Register this test class... */ /** Register this test class... */
LAUNCHER (HaID_test, "unit common"); LAUNCHER (HashIndexed_test, "unit common");
}} // namespace lib::test }} // namespace lib::test