1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2006 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 #include "OSGOSBElementFactory.h"
41 #include "OSGSingletonHolder.ins"
45 OSG_SINGLETON_INST(OSG::OSBElementFactorySingleton
, addPostFactoryExitFunction
)
47 template class SingletonHolder
<OSG::OSBElementFactorySingleton
>;
49 /*-------------------------------------------------------------------------*/
50 /* OSBElementCreatorBase */
51 /*-------------------------------------------------------------------------*/
53 /*-------------------------------------------------------------------------*/
56 OSBElementCreatorBase::~OSBElementCreatorBase(void)
60 /*-------------------------------------------------------------------------*/
61 /* OSBElementFactorySingleton */
62 /*-------------------------------------------------------------------------*/
64 /*! \class OSG::OSBElementFactory
66 Singleton registry of the read and write objects for specific object types.
67 Maps a type name (usually a field container type name) to an object derived
68 from OSBElementCreatorBase. The object is then used to create an object
69 derived from OSBElementBase that handles the reading/writing.
72 OSBElementFactory is only a typedef for
73 SingletonHolder\<OSBElementFactorySingleton\> which generates the
74 boilerplate code for the singleton.
78 /*-------------------------------------------------------------------------*/
82 OSBElementFactorySingleton::registerElement(
83 const std::string
&typeName
, OSBElementCreatorBase
*creator
)
85 bool returnValue
= true;
86 RegistryMapIt it
= _registry
.find(typeName
);
88 if(it
!= _registry
.end())
90 FWARNING(("OSBElementFactorySingleton::registerElement: "
91 "typeName: '%s' Registration failed, "
92 "an entry for this type already exists.\n",
98 _registry
.insert(it
, RegistryMap::value_type(typeName
, creator
));
105 OSBElementFactorySingleton::unregisterElement(const std::string
&typeName
)
107 bool returnValue
= true;
108 RegistryMapIt it
= _registry
.find(typeName
);
110 if(it
!= _registry
.end())
117 FWARNING(("OSBElementFactorySingleton::unregisterElement: "
118 "typeName: '%s' Attempt to unregister an unknown type.\n",
127 OSBElementFactorySingleton::registerDefault(OSBElementCreatorBase
*creator
)
129 bool returnValue
= true;
131 if(_defaultCreator
!= 0)
133 FWARNING(("OSBElementFactorySingleton::registerDefault: "
134 "Registration failed, a default creator exists already.\n"));
139 _defaultCreator
= creator
;
146 OSBElementFactorySingleton::unregisterDefault(void)
148 bool returnValue
= true;
150 if(_defaultCreator
== 0)
152 FWARNING(("OSBElementFactorySingleton::unregisterDefault: "
153 "Attempt to unregister empty default creator.\n"));
158 delete _defaultCreator
;
164 const OSBElementFactorySingleton::RegistryMap
&
165 OSBElementFactorySingleton::getRegistryMap(void) const
170 OSBElementFactorySingleton::RegistryMap
&
171 OSBElementFactorySingleton::editRegistryMap(void)
176 /*-------------------------------------------------------------------------*/
179 OSBElementFactorySingleton::OSBElementFactorySingleton(void)
185 /*-------------------------------------------------------------------------*/
188 OSBElementFactorySingleton::~OSBElementFactorySingleton(void)
190 // deleting 0 is harmless
191 delete _defaultCreator
;
193 RegistryMapIt regIt
= _registry
.begin();
194 RegistryMapIt regEnd
= _registry
.end ();
196 while(regIt
!= regEnd
)
198 delete (*regIt
).second
;