1 ////////////////////////////////////////////////////////////////////////////////
2 //    Scorched3D (c) 2000-2011
3 //
4 //    This file is part of Scorched3D.
5 //
6 //    Scorched3D is free software; you can redistribute it and/or modify
7 //    it under the terms of the GNU General Public License as published by
8 //    the Free Software Foundation; either version 2 of the License, or
9 //    (at your option) any later version.
10 //
11 //    Scorched3D is distributed in the hope that it will be useful,
12 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //    GNU General Public License for more details.
15 //
16 //    You should have received a copy of the GNU General Public License along
17 //    with this program; if not, write to the Free Software Foundation, Inc.,
18 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20 
21 #if !defined(__INCLUDE_MetaClassh_INCLUDE__)
22 #define __INCLUDE_MetaClassh_INCLUDE__
23 
24 #include <string>
25 #include <map>
26 
27 #define REGISTER_CLASS_HEADER(x) \
28 \
29 class METAFACTORY_##x : public MetaClassFactory \
30 { \
31 	virtual MetaClass *getClassCopy() { return new x ; } \
32 }; \
33 virtual const char *getClassName() { return #x ; } \
34 virtual unsigned int getMetaClassId() \
35 { \
36 	static unsigned int metaClassID = nextMetaClassId_++; return metaClassID; \
37 }
38 
39 #define REGISTER_CLASS_SOURCE(x) \
40 	struct META_##x { META_##x() { MetaClassRegistration::addMap(#x , new x::METAFACTORY_##x ); } }; \
41 	static META_##x META_IMPL_##x ;
42 
43 class MetaClass
44 {
45 public:
46 	MetaClass();
47 	virtual ~MetaClass();
48 
49 	// Automatically given by the
50 	// REGISTER_CLASS_HEADER and
51 	// REGISTER_CLASS_SOURCE macros
52 	virtual unsigned int getMetaClassId() = 0;
53 	virtual const char *getClassName() = 0;
54 protected:
55 	static unsigned int nextMetaClassId_;
56 };
57 
58 class MetaClassFactory
59 {
60 public:
61 	MetaClassFactory();
62 	virtual ~MetaClassFactory();
63 
64 	// Automatically given by the
65 	// REGISTER_CLASS_HEADER and
66 	// REGISTER_CLASS_SOURCE macros
67 	virtual MetaClass *getClassCopy() = 0;
68 };
69 
70 class MetaClassRegistration
71 {
72 public:
73 	static void addMap(const char *name, MetaClassFactory *mclass);
74 	static std::map<std::string, MetaClassFactory *> *classMap;
75 	static MetaClass *getNewClass(const char *name);
76 	static MetaClassFactory *getFactory(const char *name);
77 };
78 
79 #endif // __INCLUDE_MetaClassh_INCLUDE__
80