Field3D
ClassFactory.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------//
2
3/*
4 * Copyright (c) 2009 Sony Pictures Imageworks Inc
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the
17 * distribution. Neither the name of Sony Pictures Imageworks nor the
18 * names of its contributors may be used to endorse or promote
19 * products derived from this software without specific prior written
20 * permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33 * OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36//----------------------------------------------------------------------------//
37
42//----------------------------------------------------------------------------//
43
44#include "ClassFactory.h"
45#include "PluginLoader.h"
46
47//----------------------------------------------------------------------------//
48
49using namespace std;
50
51//----------------------------------------------------------------------------//
52
54
55//----------------------------------------------------------------------------//
56// Static instances
57//----------------------------------------------------------------------------//
58
59boost::scoped_ptr<ClassFactory> ClassFactory::ms_instance;
60
61//----------------------------------------------------------------------------//
62// ClassFactory implementations
63//----------------------------------------------------------------------------//
64
69
70//----------------------------------------------------------------------------//
71
73{
74 // Make sure we don't add the same class twice
75
76 bool nameExists = false;
77
79
80 if (!instance) {
82 "Unsuccessful attempt at registering Field class. "
83 "(Creation function returned null pointer)");
84 return;
85 }
86
87 string simpleClassName = instance->className();
88 string dataTypeName = instance->dataTypeString();
89 string className = simpleClassName + "<" + dataTypeName + ">";
90
91 FieldFuncMap::const_iterator i = m_fields.find(className);
92 if (i != m_fields.end())
93 nameExists = true;
94
95 if (!nameExists) {
96 m_fields[className] = createFunc;
97 // if the simple (untemplated) class name hasn't been registered
98 // yet, add it to the list and print a message
99 if (find(m_fieldNames.begin(), m_fieldNames.end(),
100 className) == m_fieldNames.end()) {
101 m_fieldNames.push_back(className);
102 char *debugEnvVar = getenv("FIELD3D_DEBUG");
103 if (debugEnvVar) {
104 Msg::print("Registered Field class " + className);
105 }
106 }
107 }
108
109}
110
111//----------------------------------------------------------------------------//
112
114ClassFactory::createField(const std::string &className) const
115{
116 FieldFuncMap::const_iterator i = m_fields.find(className);
117 if (i != m_fields.end())
118 return i->second();
119 else
120 return FieldRes::Ptr();
121}
122
123//----------------------------------------------------------------------------//
124
126{
127 // Make sure we don't add the same class twice
128
129 bool nameExists = false;
130
132
133 if (!instance) {
135 "Unsuccessful attempt at registering FieldIO class. "
136 "(Creation function returned null pointer)");
137 return;
138 }
139
140 string className = instance->className();
141
142 FieldIOFuncMap::const_iterator i = m_fieldIOs.find(className);
143 if (i != m_fieldIOs.end())
144 nameExists = true;
145
146 if (!nameExists) {
147 m_fieldIOs[className] = createFunc;
148 // if the simple (untemplated) class name hasn't been registered
149 // yet, add it to the list and print a message
150 if (find(m_fieldIONames.begin(), m_fieldIONames.end(),
151 className) == m_fieldIONames.end()) {
152 m_fieldIONames.push_back(className);
153 char *debugEnvVar = getenv("FIELD3D_DEBUG");
154 if (debugEnvVar) {
155 Msg::print("Registered FieldIO class " + className);
156 }
157 }
158 }
159
160}
161
162//----------------------------------------------------------------------------//
163
165ClassFactory::createFieldIO(const std::string &className) const
166{
167// FieldIOFuncMap::const_iterator m = m_fieldIOs.begin();
168 FieldIOFuncMap::const_iterator i = m_fieldIOs.find(className);
169 if (i != m_fieldIOs.end())
170 return i->second();
171 else
172 return FieldIO::Ptr();
173}
174
175//----------------------------------------------------------------------------//
176
178{
179 // Make sure we don't add the same class twice
180
181 bool nameExists = false;
182
184
185 if (!instance) {
187 "Unsuccessful attempt at registering FieldMapping class. "
188 "(Creation function returned null pointer)");
189 return;
190 }
191
192 string className = instance->className();
193
194 FieldMappingFuncMap::const_iterator i = m_mappings.find(className);
195 if (i != m_mappings.end())
196 nameExists = true;
197
198 if (!nameExists) {
199 m_mappings[className] = createFunc;
200 // if the simple (untemplated) class name hasn't been registered
201 // yet, add it to the list and print a message
203 className) == m_fieldMappingNames.end()) {
204 m_fieldMappingNames.push_back(className);
205 char *debugEnvVar = getenv("FIELD3D_DEBUG");
206 if (debugEnvVar) {
207 Msg::print("Registered FieldMapping class " + className);
208 }
209 }
210 }
211}
212
213//----------------------------------------------------------------------------//
214
216ClassFactory::createFieldMapping(const std::string &className) const
217{
218 FieldMappingFuncMap::const_iterator i = m_mappings.find(className);
219 if (i != m_mappings.end())
220 return i->second();
221 else
222 return FieldMapping::Ptr();
223}
224
225//----------------------------------------------------------------------------//
226
228{
229 // Make sure we don't add the same class twice
230
231 bool nameExists = false;
232
234
235 if (!instance) {
237 "Unsuccessful attempt at registering FieldMappingIO class. "
238 "(Creation function returned null pointer)");
239 return;
240 }
241
242 string className = instance->className();
243
244 FieldMappingIOFuncMap::const_iterator i = m_mappingIOs.find(className);
245 if (i != m_mappingIOs.end())
246 nameExists = true;
247
248 if (!nameExists) {
249 m_mappingIOs[className] = createFunc;
250 // if the simple (untemplated) class name hasn't been registered
251 // yet, add it to the list and print a message
253 className) == m_fieldMappingNames.end()) {
254 m_fieldMappingNames.push_back(className);
255 char *debugEnvVar = getenv("FIELD3D_DEBUG");
256 if (debugEnvVar) {
257 Msg::print("Registered FieldMappingIO class " + className);
258 }
259 }
260 }
261}
262
263//----------------------------------------------------------------------------//
264
266ClassFactory::createFieldMappingIO(const std::string &className) const
267{
268 FieldMappingIOFuncMap::const_iterator i = m_mappingIOs.find(className);
269 if (i != m_mappingIOs.end())
270 return i->second();
271 else
272 return FieldMappingIO::Ptr();
273}
274
275//----------------------------------------------------------------------------//
276
279{
280 if (ms_instance.get() == NULL) {
281 ms_instance.reset(new ClassFactory);
282 }
283 return *ms_instance;
284}
285
286//----------------------------------------------------------------------------//
287
289
290//----------------------------------------------------------------------------//
Contains the ClassFactory class for registering Field3D classes.
Contains the PluginLoader class.
#define FIELD3D_MTX_T
Definition StdMathLib.h:99
NameVec m_fieldIONames
FieldRes::Ptr createField(const std::string &className) const
Instances an object by name.
FieldIO::Ptr createFieldIO(const std::string &className) const
Instances an IO object by name.
static ClassFactory & singleton()
}
ClassFactory()
Standard constructor.
FieldMappingIOFuncMap m_mappingIOs
Map of create functions for FieldMapping IO classes. The key is the class name.
FieldMapping::Ptr createFieldMapping(const std::string &className) const
Instances an object by name.
FieldMappingIO::Ptr createFieldMappingIO(const std::string &className) const
Instances an IO object by name.
static boost::scoped_ptr< ClassFactory > ms_instance
Pointer to static instance.
void registerField(CreateFieldFnPtr createFunc)
Registers a class with the class pool.
void registerFieldMapping(CreateFieldMappingFnPtr createFunc)
Registers a class with the class pool.
void registerFieldIO(CreateFieldIOFnPtr createFunc)
Registers an IO class with the class pool.
void registerFieldMappingIO(CreateFieldMappingIOFnPtr createFunc)
Registers an IO class with the class pool.
FieldMappingFuncMap m_mappings
Map of create functions for FieldMappings. The key is the class name.
FieldFuncMap m_fields
Map of create functions for Fields. The key is the class name.
NameVec m_fieldNames
FieldIOFuncMap m_fieldIOs
Map of create functions for FieldIO classes. The key is the class name.
NameVec m_fieldMappingNames
boost::intrusive_ptr< FieldIO > Ptr
Definition FieldIO.h:91
boost::intrusive_ptr< FieldMappingIO > Ptr
boost::intrusive_ptr< FieldMapping > Ptr
boost::intrusive_ptr< FieldRes > Ptr
Definition Field.h:213
static void loadPlugins()
Checks all paths in $FIELD3D_DSO_PATH and loads the plugins it finds.
@ SevWarning
Definition Log.h:68
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity.
Definition Log.cpp:70
#define FIELD3D_NAMESPACE_SOURCE_CLOSE
Definition ns.h:60