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 
49 using namespace std;
50 
51 //----------------------------------------------------------------------------//
52 
54 
55 //----------------------------------------------------------------------------//
56 // Static instances
57 //----------------------------------------------------------------------------//
58 
60 
61 //----------------------------------------------------------------------------//
62 // ClassFactory implementations
63 //----------------------------------------------------------------------------//
64 
66 {
68 }
69 
70 //----------------------------------------------------------------------------//
71 
72 void ClassFactory::registerField(CreateFieldFnPtr createFunc)
73 {
74  // Make sure we don't add the same class twice
75 
76  bool nameExists = false;
77 
78  FieldRes::Ptr instance = createFunc();
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 
114 ClassFactory::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 
125 void ClassFactory::registerFieldIO(CreateFieldIOFnPtr createFunc)
126 {
127  // Make sure we don't add the same class twice
128 
129  bool nameExists = false;
130 
131  FieldIO::Ptr instance = createFunc();
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 
165 ClassFactory::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 
177 void ClassFactory::registerFieldMapping(CreateFieldMappingFnPtr createFunc)
178 {
179  // Make sure we don't add the same class twice
180 
181  bool nameExists = false;
182 
183  FieldMapping::Ptr instance = createFunc();
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
202  if (find(m_fieldMappingNames.begin(), m_fieldMappingNames.end(),
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 
216 ClassFactory::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 
227 void ClassFactory::registerFieldMappingIO(CreateFieldMappingIOFnPtr createFunc)
228 {
229  // Make sure we don't add the same class twice
230 
231  bool nameExists = false;
232 
233  FieldMappingIO::Ptr instance = createFunc();
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
252  if (find(m_fieldMappingNames.begin(), m_fieldMappingNames.end(),
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 
266 ClassFactory::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 
277 ClassFactory&
279 {
280  if (!ms_instance)
281  ms_instance = new ClassFactory;
282  return *ms_instance;
283 }
284 
285 //----------------------------------------------------------------------------//
286 
288 
289 //----------------------------------------------------------------------------//
boost::intrusive_ptr< FieldMappingIO > Ptr
void registerFieldIO(CreateFieldIOFnPtr createFunc)
Registers an IO class with the class pool.
#define FIELD3D_NAMESPACE_SOURCE_CLOSE
Definition: ns.h:60
static void loadPlugins()
Checks all paths in $FIELD3D_DSO_PATH and loads the plugins it finds.
static ClassFactory & singleton()
}
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:66
boost::intrusive_ptr< FieldRes > Ptr
Definition: Field.h:218
ClassFactory()
Standard constructor.
boost::intrusive_ptr< FieldMapping > Ptr
Definition: FieldMapping.h:92
FieldRes::Ptr createField(const std::string &className) const
Instances an object by name.
static ClassFactory * ms_instance
Pointer to static instance.
Definition: ClassFactory.h:164
Contains the PluginLoader class.
void registerFieldMappingIO(CreateFieldMappingIOFnPtr createFunc)
Registers an IO class with the class pool.
FieldMapping::Ptr createFieldMapping(const std::string &className) const
Instances an object by name.
Contains the ClassFactory class for registering Field3D classes.
FieldIO::Ptr createFieldIO(const std::string &className) const
Instances an IO object by name.
void registerFieldMapping(CreateFieldMappingFnPtr createFunc)
Registers a class with the class pool.
void registerField(CreateFieldFnPtr createFunc)
Registers a class with the class pool.
boost::intrusive_ptr< FieldIO > Ptr
Definition: FieldIO.h:90
FieldMappingIO::Ptr createFieldMappingIO(const std::string &className) const
Instances an IO object by name.