2009-05-22 02:40:20 +02:00
|
|
|
/*
|
2009-05-23 05:13:51 +02:00
|
|
|
HASH-INDEXED.hpp - generic hash based and typed ID
|
2009-05-22 02:40:20 +02:00
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2009, Hermann Vosseler <Ichthyostega@web.de>
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2009-05-23 05:13:51 +02:00
|
|
|
/** @file hash-indexed.hpp
|
2009-05-22 03:41:58 +02:00
|
|
|
** 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
|
2009-05-23 05:13:51 +02:00
|
|
|
** - establishing an type hierarchy relation between ID related to the base class
|
|
|
|
|
** and the IDs denoting specific subclasses, such that the latter can stand-in
|
|
|
|
|
** for the generic ID.
|
2009-05-22 03:41:58 +02:00
|
|
|
** - providing a Mixin, which allows any hierarchy to use this facility without
|
|
|
|
|
** much code duplication.
|
2009-05-22 02:40:20 +02:00
|
|
|
**
|
2009-05-23 03:43:39 +02:00
|
|
|
** @see HashIndexed_test
|
2009-05-22 02:40:20 +02:00
|
|
|
** @see Placement usage example
|
|
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-05-23 05:13:51 +02:00
|
|
|
#ifndef LIB_HASH_INDEXED_H
|
|
|
|
|
#define LIB_HASH_INDEXED_H
|
2009-05-22 02:40:20 +02:00
|
|
|
|
2009-05-22 03:41:58 +02:00
|
|
|
//#include "lib/util.hpp"
|
2009-05-22 02:40:20 +02:00
|
|
|
|
|
|
|
|
//#include <tr1/memory>
|
2009-05-22 03:41:58 +02:00
|
|
|
#include <cstdlib>
|
2009-05-22 02:40:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
|
|
|
|
|
// using std::tr1::shared_ptr;
|
2009-05-22 03:41:58 +02:00
|
|
|
using std::rand;
|
2009-05-22 02:40:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
struct LuidH
|
|
|
|
|
{
|
|
|
|
|
long dummy_;
|
2009-05-22 03:41:58 +02:00
|
|
|
|
|
|
|
|
LuidH() : dummy_(rand()) {}
|
2009-05-24 01:46:00 +02:00
|
|
|
|
|
|
|
|
bool operator== (LuidH const& o) const { return dummy_ == o.dummy_; }
|
|
|
|
|
bool operator!= (LuidH const& o) const { return dummy_ != o.dummy_; }
|
2009-05-22 02:40:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2009-05-23 05:13:51 +02:00
|
|
|
/************************************************************
|
|
|
|
|
* 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>
|
2009-05-23 03:43:39 +02:00
|
|
|
struct HashIndexed
|
2009-05-22 02:40:20 +02:00
|
|
|
{
|
2009-05-23 05:13:51 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* generic hash based ID, corresponding to the base class BA
|
2009-05-23 03:43:39 +02:00
|
|
|
*/
|
2009-05-23 05:13:51 +02:00
|
|
|
struct ID : IMP
|
2009-05-23 03:43:39 +02:00
|
|
|
{
|
2009-05-23 05:13:51 +02:00
|
|
|
ID () : IMP () {}
|
|
|
|
|
ID (BA const& ref) : IMP (ref.getID()) {}
|
|
|
|
|
ID (IMP const& ir) : IMP (ir) {}
|
2009-05-23 03:43:39 +02:00
|
|
|
};
|
|
|
|
|
|
2009-05-23 05:13:51 +02:00
|
|
|
/**
|
|
|
|
|
* Hash based ID, typed to a specific subclass of BA
|
|
|
|
|
*/
|
2009-05-23 03:43:39 +02:00
|
|
|
template<typename T>
|
|
|
|
|
struct Id : ID
|
|
|
|
|
{
|
|
|
|
|
Id () : ID () {}
|
|
|
|
|
Id (T const& ref) : ID (ref) {}
|
|
|
|
|
};
|
2009-05-22 03:41:58 +02:00
|
|
|
|
2009-05-23 05:13:51 +02:00
|
|
|
ID const&
|
|
|
|
|
getID () const
|
2009-05-22 03:41:58 +02:00
|
|
|
{
|
|
|
|
|
return id_;
|
|
|
|
|
}
|
2009-05-23 05:13:51 +02:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
assignID (HashIndexed const& ref)
|
2009-05-22 03:41:58 +02:00
|
|
|
{
|
|
|
|
|
this->id_ = ref.getID();
|
|
|
|
|
}
|
2009-05-23 05:13:51 +02:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
HashIndexed () : id_() {}
|
|
|
|
|
HashIndexed (IMP const& iref) : id_(iref) {}
|
2009-05-23 03:43:39 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
ID id_;
|
2009-05-22 02:40:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lib
|
|
|
|
|
#endif
|