1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2009 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 #if __GNUC__ >= 4 || __GNUC_MINOR__ >=3
40 #pragma GCC diagnostic ignored "-Wold-style-cast"
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"
55 #if !defined(OSG_DO_DOC)
56 OSG_SINGLETON_INST(OSG::ColladaElementFactorySingleton
,
57 addPostFactoryExitFunction
)
60 template class SingletonHolder
<OSG::ColladaElementFactorySingleton
>;
64 ColladaElementFactorySingleton::registerElement(
65 CreateFunctor createFunc
,
66 const std::string
&elemName
,
67 const std::string
&profile
,
72 if(profile
.empty() == false)
74 retVal
= doRegisterElement(createFunc
, elemName
,
75 _profileHandlerMap
[profile
],
80 retVal
= doRegisterElement(createFunc
, elemName
,
89 ColladaElementFactorySingleton::unregisterElement(
90 const std::string
&elemName
, const std::string
&profile
)
94 if(profile
.empty() == false)
96 retVal
= doUnregisterElement(elemName
, _profileHandlerMap
[profile
]);
100 retVal
= doUnregisterElement(elemName
, _defaultHandlerMap
);
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
;
131 ColladaElementFactorySingleton::getDefaultProfile(void) const
133 return _defaultProfile
;
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
]);
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();
182 lookupName
= elemName
;
185 if(lookupName
.empty() == false)
187 CreateFunctor createFunc
= getCreateFunc(lookupName
, profile
);
189 if(createFunc
.empty() == false)
191 retVal
= createFunc(daeElem
, global
);
196 retVal
->getDOMElement()->setUserData(retVal
.get());
198 global
->addElement (retVal
.get());
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() << "]."
223 ColladaElementFactorySingleton::doRegisterElement(
224 CreateFunctor createFunc
,
225 const std::string
&elemName
,
226 HandlerMap
&handlerMap
,
230 HandlerMapIt hIt
= handlerMap
.find(elemName
);
232 if(hIt
!= handlerMap
.end())
234 if(override
== false)
236 SWARNING
<< "ColladaElementFactorySingleton::doRegisterElement: "
237 << "elemName [" << elemName
<< "] already registered."
244 SWARNING
<< "ColladaElementFactorySingleton::doRegisterElement: "
245 << "overrode elemName [" << elemName
<< "] handler."
248 hIt
->second
= createFunc
;
253 handlerMap
.insert(hIt
, HandlerMap::value_type(elemName
, createFunc
));
260 ColladaElementFactorySingleton::doUnregisterElement(
261 const std::string
&elemName
, HandlerMap
&handlerMap
)
264 HandlerMapIt hIt
= handlerMap
.find(elemName
);
266 if(hIt
!= handlerMap
.end())
268 handlerMap
.erase(hIt
);
272 SWARNING
<< "ColladaElemnetFactorySingleton::doUnregisterElement: "
273 << "elemName [" << elemName
<< "] not registered."
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
;
297 ColladaElementFactorySingleton::ColladaElementFactorySingleton(void)
299 , _defaultHandlerMap()
300 , _profileHandlerMap()
304 ColladaElementFactorySingleton::~ColladaElementFactorySingleton(void)
310 #endif // OSG_WITH_COLLADA