git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d.git] / src / common / engine / MetaClass.h
blob7058ebc386889bcc1b4f82b6cd2826664e4e6627
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
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.
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.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #if !defined(__INCLUDE_MetaClassh_INCLUDE__)
22 #define __INCLUDE_MetaClassh_INCLUDE__
24 #include <string>
25 #include <map>
27 #define REGISTER_CLASS_HEADER(x) \
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; \
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 ;
43 class MetaClass
45 public:
46 MetaClass();
47 virtual ~MetaClass();
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_;
58 class MetaClassFactory
60 public:
61 MetaClassFactory();
62 virtual ~MetaClassFactory();
64 // Automatically given by the
65 // REGISTER_CLASS_HEADER and
66 // REGISTER_CLASS_SOURCE macros
67 virtual MetaClass *getClassCopy() = 0;
70 class MetaClassRegistration
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);
79 #endif // __INCLUDE_MetaClassh_INCLUDE__