Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / nel / src / misc / class_registry.cpp
blobc703e45d0637ef99558fde417493de0a60fff96f
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "stdmisc.h"
19 #include "nel/misc/class_registry.h"
20 #include "nel/misc/debug.h"
22 using namespace std;
24 #ifdef DEBUG_NEW
25 #define new DEBUG_NEW
26 #endif
28 namespace NLMISC
32 // ======================================================================================================
33 CClassRegistry::TClassMap *CClassRegistry::RegistredClasses = NULL;
36 // ======================================================================================================
37 void CClassRegistry::init()
39 if (RegistredClasses == NULL)
40 RegistredClasses = new TClassMap;
43 // ======================================================================================================
44 void CClassRegistry::release()
46 if( RegistredClasses )
47 delete RegistredClasses;
48 RegistredClasses = NULL;
51 // ======================================================================================================
52 IClassable *CClassRegistry::create(const string &className)
54 init();
56 TClassMap::iterator it;
58 it=RegistredClasses->find(className);
60 if(it==RegistredClasses->end())
61 return NULL;
62 else
64 IClassable *ptr;
65 ptr=it->second.Creator();
66 #ifdef NL_DEBUG
67 nlassert(CClassRegistry::checkObject(ptr));
68 #endif
69 return ptr;
74 // ======================================================================================================
75 void CClassRegistry::registerClass(const string &className, IClassable* (*creator)(), const string &typeidCheck)
77 init();
79 CClassNode node;
80 node.Creator=creator;
81 node.TypeIdCheck= typeidCheck;
82 std::pair<TClassMap::iterator, bool> result;
83 result = RegistredClasses->insert(TClassMap::value_type(className, node));
84 if(!result.second)
86 nlstop;
87 throw ERegisteredClass();
91 // ======================================================================================================
92 bool CClassRegistry::checkObject(IClassable* obj)
94 init();
96 TClassMap::iterator it;
97 it=RegistredClasses->find(obj->getClassName());
98 if(it==RegistredClasses->end())
99 return false;
101 if( it->second.TypeIdCheck != string(typeid(*obj).name()) )
102 return false;
104 return true;