2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #if !defined(INCLUDED_TYPESYSTEM_H)
23 #define INCLUDED_TYPESYSTEM_H
28 #include "generic/callback.h"
29 #include "generic/static.h"
33 typedef std::list<Callback> Initialisers;
34 Initialisers m_initialisers;
35 mutable bool m_initialised;
37 InitialiserList() : m_initialised(false)
40 void addInitialiser(const Callback& callback)
42 m_initialisers.push_back(callback);
44 void initialise() const
50 for(Initialisers::const_iterator i = m_initialisers.begin(); i != m_initialisers.end(); ++i)
58 //--Type System-------------------
60 class TypeSystemInitialiser : public InitialiserList
64 typedef SmartStatic<TypeSystemInitialiser> StaticTypeSystemInitialiser;
66 class TypeSystemRef : public StaticTypeSystemInitialiser
71 StaticTypeSystemInitialiser::instance().initialise();
77 typedef std::size_t TypeId;
78 typedef void*(*TypeCast)(void*);
80 template<std::size_t SIZE>
83 TypeCast m_casts[SIZE];
87 std::uninitialized_fill(m_casts, m_casts + SIZE, TypeCast(0));
89 void install(TypeId typeId, TypeCast typeCast)
91 m_casts[typeId] = typeCast;
93 void* cast(TypeId typeId, void* p)
95 TypeCast typeCast = m_casts[typeId];
104 template<typename Type, typename Cast>
108 static void install(TypeCastTable<Type::SIZE>& table)
110 table.install(Type::getTypeId(), Cast::cast);
114 template<typename Type>
118 static void* cast(void* p)
124 template<typename Type, typename Base>
128 static void* cast(void* p)
130 return static_cast<Base*>(reinterpret_cast<Type*>(p));
134 template<typename Type>
139 template<typename Type, typename Contained>
143 static void* cast(void* p)
145 return &reinterpret_cast<Type*>(p)->get(NullType<Contained>());