| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#if !defined( INCLUDED_TYPESYSTEM_H ) |
| 23 |
#define INCLUDED_TYPESYSTEM_H |
| 24 |
|
| 25 |
|
| 26 |
#include <list> |
| 27 |
#include <vector> |
| 28 |
#include "generic/callback.h" |
| 29 |
#include "generic/static.h" |
| 30 |
|
| 31 |
class InitialiserList |
| 32 |
{ |
| 33 |
typedef std::list<Callback<void()>> Initialisers; |
| 34 |
Initialisers m_initialisers; |
| 35 |
mutable bool m_initialised; |
| 36 |
public: |
| 37 |
InitialiserList() : m_initialised( false ){ |
| 38 |
} |
| 39 |
void addInitialiser( const Callback<void()>& callback ){ |
| 40 |
m_initialisers.push_back( callback ); |
| 41 |
} |
| 42 |
void initialise() const { |
| 43 |
if ( !m_initialised ) { |
| 44 |
m_initialised = true; |
| 45 |
|
| 46 |
for ( Initialisers::const_iterator i = m_initialisers.begin(); i != m_initialisers.end(); ++i ) |
| 47 |
{ |
| 48 |
( *i )( ); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
}; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
class TypeSystemInitialiser : public InitialiserList |
| 57 |
{ |
| 58 |
}; |
| 59 |
|
| 60 |
typedef SmartStatic<TypeSystemInitialiser> StaticTypeSystemInitialiser; |
| 61 |
|
| 62 |
class TypeSystemRef : public StaticTypeSystemInitialiser |
| 63 |
{ |
| 64 |
public: |
| 65 |
TypeSystemRef(){ |
| 66 |
StaticTypeSystemInitialiser::instance().initialise(); |
| 67 |
} |
| 68 |
}; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
typedef std::size_t TypeId; |
| 73 |
typedef void*( *TypeCast )( void* ); |
| 74 |
|
| 75 |
template<std::size_t SIZE> |
| 76 |
class TypeCastTable |
| 77 |
{ |
| 78 |
TypeCast m_casts[SIZE]; |
| 79 |
public: |
| 80 |
TypeCastTable(){ |
| 81 |
std::uninitialized_fill( m_casts, m_casts + SIZE, TypeCast( 0 ) ); |
| 82 |
} |
| 83 |
void install( TypeId typeId, TypeCast typeCast ){ |
| 84 |
m_casts[typeId] = typeCast; |
| 85 |
} |
| 86 |
void* cast( TypeId typeId, void* p ){ |
| 87 |
TypeCast typeCast = m_casts[typeId]; |
| 88 |
if ( typeCast != 0 ) { |
| 89 |
return typeCast( p ); |
| 90 |
} |
| 91 |
return 0; |
| 92 |
} |
| 93 |
}; |
| 94 |
|
| 95 |
template<typename Type, typename Cast> |
| 96 |
class CastInstaller |
| 97 |
{ |
| 98 |
public: |
| 99 |
static void install( TypeCastTable<Type::SIZE>& table ){ |
| 100 |
table.install( Type::getTypeId(), Cast::cast ); |
| 101 |
} |
| 102 |
}; |
| 103 |
|
| 104 |
template<typename Type> |
| 105 |
class IdentityCast |
| 106 |
{ |
| 107 |
public: |
| 108 |
static void* cast( void* p ){ |
| 109 |
return p; |
| 110 |
} |
| 111 |
}; |
| 112 |
|
| 113 |
template<typename Type, typename Base> |
| 114 |
class StaticCast |
| 115 |
{ |
| 116 |
public: |
| 117 |
static void* cast( void* p ){ |
| 118 |
return static_cast<Base*>( reinterpret_cast<Type*>( p ) ); |
| 119 |
} |
| 120 |
}; |
| 121 |
|
| 122 |
template<typename Type> |
| 123 |
class NullType |
| 124 |
{ |
| 125 |
}; |
| 126 |
|
| 127 |
template<typename Type, typename Contained> |
| 128 |
class ContainedCast |
| 129 |
{ |
| 130 |
public: |
| 131 |
static void* cast( void* p ){ |
| 132 |
return &reinterpret_cast<Type*>( p )->get( NullType<Contained>() ); |
| 133 |
} |
| 134 |
}; |
| 135 |
|
| 136 |
|
| 137 |
#endif |