fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FileIO / Collada / OSGColladaElementFactory.cpp
blobdca55d757230315d59fa7bc6ceb2e7c2e3f8524c
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2009 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 #if __GNUC__ >= 4 || __GNUC_MINOR__ >=3
40 #pragma GCC diagnostic ignored "-Wold-style-cast"
41 #endif
43 #include "OSGColladaElementFactory.h"
45 #if defined(OSG_WITH_COLLADA) || defined(OSG_DO_DOC)
47 #include "OSGBaseInitFunctions.h"
48 #include "OSGColladaLog.h"
49 #include "OSGColladaGlobal.h"
50 #include "OSGSingletonHolder.ins"
53 OSG_BEGIN_NAMESPACE
55 #if !defined(OSG_DO_DOC)
56 OSG_SINGLETON_INST(OSG::ColladaElementFactorySingleton,
57 addPostFactoryExitFunction)
58 #endif
60 template class SingletonHolder<OSG::ColladaElementFactorySingleton>;
63 bool
64 ColladaElementFactorySingleton::registerElement(
65 CreateFunctor createFunc,
66 const std::string &elemName,
67 const std::string &profile,
68 bool override)
70 bool retVal = true;
72 if(profile.empty() == false)
74 retVal = doRegisterElement(createFunc, elemName,
75 _profileHandlerMap[profile],
76 override );
78 else
80 retVal = doRegisterElement(createFunc, elemName,
81 _defaultHandlerMap,
82 override );
85 return retVal;
88 bool
89 ColladaElementFactorySingleton::unregisterElement(
90 const std::string &elemName, const std::string &profile)
92 bool retVal = true;
94 if(profile.empty() == false)
96 retVal = doUnregisterElement(elemName, _profileHandlerMap[profile]);
98 else
100 retVal = doUnregisterElement(elemName, _defaultHandlerMap );
103 return retVal;
106 const ColladaElementFactorySingleton::HandlerMap &
107 ColladaElementFactorySingleton::getDefaultHandlerMap(void) const
109 return _defaultHandlerMap;
112 ColladaElementFactorySingleton::HandlerMap &
113 ColladaElementFactorySingleton::editDefaultHandlerMap(void)
115 return _defaultHandlerMap;
118 const ColladaElementFactorySingleton::ProfileHandlerMap &
119 ColladaElementFactorySingleton::getProfileHandlerMap(void) const
121 return _profileHandlerMap;
124 ColladaElementFactorySingleton::ProfileHandlerMap &
125 ColladaElementFactorySingleton::editProfileHandlerMap(void)
127 return _profileHandlerMap;
130 const std::string &
131 ColladaElementFactorySingleton::getDefaultProfile(void) const
133 return _defaultProfile;
136 void
137 ColladaElementFactorySingleton::setDefaultProfile(const std::string &profile)
139 _defaultProfile = profile;
142 ColladaElementFactorySingleton::CreateFunctor
143 ColladaElementFactorySingleton::getCreateFunc(const std::string &elemName,
144 const std::string &profile )
146 if(profile.empty() == false)
148 return doGetCreateFunc(elemName, _profileHandlerMap[profile] );
150 else if(_defaultProfile.empty() == false)
152 return doGetCreateFunc(elemName, _profileHandlerMap[_defaultProfile]);
154 else
156 return doGetCreateFunc(elemName, _defaultHandlerMap );
160 ColladaElementTransitPtr
161 ColladaElementFactorySingleton::create(daeElement *daeElem,
162 ColladaGlobal *global,
163 const std::string &elemName,
164 const std::string &profile )
166 ColladaElementTransitPtr retVal;
167 std::string lookupName;
169 if(elemName.empty() == true)
171 if(daeElem->getElementName() != NULL)
173 lookupName = daeElem->getElementName();
175 else if(daeElem->getTypeName() != NULL)
177 lookupName = daeElem->getTypeName();
180 else
182 lookupName = elemName;
185 if(lookupName.empty() == false)
187 CreateFunctor createFunc = getCreateFunc(lookupName, profile);
189 if(createFunc.empty() == false)
191 retVal = createFunc(daeElem, global);
194 if(retVal != NULL)
196 retVal->getDOMElement()->setUserData(retVal.get());
198 global->addElement (retVal.get());
200 else
202 daeString dsElemName =
203 daeElem->getElementName() != NULL ?
204 daeElem->getElementName() : "??";
205 daeString dsTypeName =
206 daeElem->getTypeName () != NULL ?
207 daeElem->getTypeName () : "??";
209 SWARNING << "ColladaElementFactory::create: No element registered "
210 << "for [" << lookupName
211 << "] - elemName [" << dsElemName
212 << "] typeName [" << dsTypeName
213 << "] id [" << daeElem->typeID() << "]."
214 << std::endl;
218 return retVal;
222 bool
223 ColladaElementFactorySingleton::doRegisterElement(
224 CreateFunctor createFunc,
225 const std::string &elemName,
226 HandlerMap &handlerMap,
227 bool override )
229 bool retVal = true;
230 HandlerMapIt hIt = handlerMap.find(elemName);
232 if(hIt != handlerMap.end())
234 if(override == false)
236 SWARNING << "ColladaElementFactorySingleton::doRegisterElement: "
237 << "elemName [" << elemName << "] already registered."
238 << std::endl;
240 retVal = false;
242 else
244 SWARNING << "ColladaElementFactorySingleton::doRegisterElement: "
245 << "overrode elemName [" << elemName << "] handler."
246 << std::endl;
248 hIt->second = createFunc;
251 else
253 handlerMap.insert(hIt, HandlerMap::value_type(elemName, createFunc));
256 return retVal;
259 bool
260 ColladaElementFactorySingleton::doUnregisterElement(
261 const std::string &elemName, HandlerMap &handlerMap)
263 bool retVal = true;
264 HandlerMapIt hIt = handlerMap.find(elemName);
266 if(hIt != handlerMap.end())
268 handlerMap.erase(hIt);
270 else
272 SWARNING << "ColladaElemnetFactorySingleton::doUnregisterElement: "
273 << "elemName [" << elemName << "] not registered."
274 << std::endl;
276 retVal = false;
279 return retVal;
282 ColladaElementFactorySingleton::CreateFunctor
283 ColladaElementFactorySingleton::doGetCreateFunc(
284 const std::string &elemName, HandlerMap &handlerMap)
286 CreateFunctor retVal;
287 HandlerMapIt hIt = handlerMap.find(elemName);
289 if(hIt != handlerMap.end())
291 retVal = hIt->second;
294 return retVal;
297 ColladaElementFactorySingleton::ColladaElementFactorySingleton(void)
298 : _defaultProfile ()
299 , _defaultHandlerMap()
300 , _profileHandlerMap()
304 ColladaElementFactorySingleton::~ColladaElementFactorySingleton(void)
308 OSG_END_NAMESPACE
310 #endif // OSG_WITH_COLLADA