// altTypeId.cpp : Defines the entry point for the console application.
//
#include <iostream>
//////////////////////////////////////////////////////////////////////////
// typedef for unsigned int as altTypeId
typedef unsigned int altTypeId;
//////////////////////////////////////////////////////////////////////////
// Base class for TTypeIDGen.
//
// VERY simple un constructable static class; Basically just a wrapper to a
// static variable contained in a function. Whilst this seems weird, doing it
// this way has adavatages; e.g. prevents you having to do all the nonsense
// with initialising a static member variable in a .cpp file.
class CAltTypeIdGen_Base
{
public:
enum
{
ALTTYPEID_INVALID = 0xFFffFFff // supplied so code can initialise with this like NULL for pointers
};
protected:
// generates a type ID
// you would probably want to put this in a .cpp in case you want to use it in a library
static altTypeId GenerateAltTypeId()
{
static altTypeId s_uNextClassID = 0;
return s_uNextClassID++;
}
// only constructible from derived types
CAltTypeIdGen_Base()
{}
};
//////////////////////////////////////////////////////////////////////////
// type specialised templated TypeID class
//
// Type specific static class specialization of CRTTIBase.
// It's called via the GetTypeID macro (below) and essentially wraps a
// statically allocated instance of TypeID Only one TypeID can exist for
// each type, so it functions exactly like a C++ typeID.
// the only caveat to this is that the actual value of the ID will depend
// on the execution order of templated calls to GetTypeID.
//
template< typename T >
class TAltTypeIdGen : public CAltTypeIdGen_Base
{
public:
// this generates a typeID for each class that instantiates the template
static altTypeId GetAltTypeId()
{
static altTypeId s_uClassId = GenerateAltTypeId();
return s_uClassId;
}
private:
// no instance of this class can be created.
TAltTypeIdGen()
{}
};
// define to make CAltTypeIdGen_Base::ALTTYPEID_INVALID less visually traumatic
#define ALTTYPEID_INVALID CAltTypeIdGen_Base::ALTTYPEID_INVALID
// macro for getting hold of a type's altTypeId
#define GetAltTypeIdOf( TYPENAME ) ( TAltTypeIdGen< TYPENAME >::GetAltTypeId() )
//////////////////////////////////////////////////////////////////////////
// template function that resolves to the correct form of
// CReflectionData< T >::GetTypeID() based on the input type
template< typename T >
altTypeId GetAltTypeIdOfInstance( T instance )
{
return TAltTypeIdGen< T >::GetAltTypeId();
}
//////////////////////////////////////////////////////////////////////////
// macro to remove drugery / make code nicer to look at
#define PRINT_TYPE_TEST( INSTANCE, TYPE ) \
{\
std::cout<<"Is "<<#INSTANCE<<" a "<<#TYPE<<"? "<<((GetAltTypeIdOf(TYPE)==GetAltTypeIdOfInstance(INSTANCE))?"YES\n":"NO\n");\
}
int main(int argc, char* argv[])
{
// test the type id
char chTest = 0;
int iTest = 0;
float fTest = 0.0f;
// use printing to prove the typeid works
PRINT_TYPE_TEST( chTest, char );
PRINT_TYPE_TEST( chTest, int );
PRINT_TYPE_TEST( iTest, int );
PRINT_TYPE_TEST( iTest, float );
PRINT_TYPE_TEST( fTest, float );
PRINT_TYPE_TEST( fTest, int );
return 0;
}