fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FileIO / OSB / OSGOSBElementFactory.cpp
blob313d13eb66e95016c41433b3880cda64eea959ca
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2006 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 #include "OSGOSBElementFactory.h"
41 #include "OSGSingletonHolder.ins"
43 OSG_BEGIN_NAMESPACE
45 OSG_SINGLETON_INST(OSG::OSBElementFactorySingleton, addPostFactoryExitFunction)
47 template class SingletonHolder<OSG::OSBElementFactorySingleton>;
49 /*-------------------------------------------------------------------------*/
50 /* OSBElementCreatorBase */
51 /*-------------------------------------------------------------------------*/
53 /*-------------------------------------------------------------------------*/
54 /* Destructor */
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.
71 \dev
72 OSBElementFactory is only a typedef for
73 SingletonHolder\<OSBElementFactorySingleton\> which generates the
74 boilerplate code for the singleton.
75 \enddev
78 /*-------------------------------------------------------------------------*/
79 /* Registration */
81 bool
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",
93 typeName.c_str() ));
94 returnValue = false;
96 else
98 _registry.insert(it, RegistryMap::value_type(typeName, creator));
101 return returnValue;
104 bool
105 OSBElementFactorySingleton::unregisterElement(const std::string &typeName)
107 bool returnValue = true;
108 RegistryMapIt it = _registry.find(typeName);
110 if(it != _registry.end())
112 delete it->second;
113 _registry.erase(it);
115 else
117 FWARNING(("OSBElementFactorySingleton::unregisterElement: "
118 "typeName: '%s' Attempt to unregister an unknown type.\n",
119 typeName.c_str() ));
120 returnValue = false;
123 return returnValue;
126 bool
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"));
135 returnValue = false;
137 else
139 _defaultCreator = creator;
142 return returnValue;
145 bool
146 OSBElementFactorySingleton::unregisterDefault(void)
148 bool returnValue = true;
150 if(_defaultCreator == 0)
152 FWARNING(("OSBElementFactorySingleton::unregisterDefault: "
153 "Attempt to unregister empty default creator.\n"));
154 returnValue = false;
156 else
158 delete _defaultCreator;
159 _defaultCreator = 0;
161 return returnValue;
164 const OSBElementFactorySingleton::RegistryMap &
165 OSBElementFactorySingleton::getRegistryMap(void) const
167 return _registry;
170 OSBElementFactorySingleton::RegistryMap &
171 OSBElementFactorySingleton::editRegistryMap(void)
173 return _registry;
176 /*-------------------------------------------------------------------------*/
177 /* Constructor */
179 OSBElementFactorySingleton::OSBElementFactorySingleton(void)
180 : _registry ( ),
181 _defaultCreator(0)
185 /*-------------------------------------------------------------------------*/
186 /* Destructor */
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;
200 ++regIt;
204 OSG_END_NAMESPACE