Field3D
|
00001 //----------------------------------------------------------------------------// 00002 00003 /* 00004 * Copyright (c) 2009 Sony Pictures Imageworks Inc 00005 * 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * Redistributions in binary form must reproduce the above copyright 00015 * notice, this list of conditions and the following disclaimer in the 00016 * documentation and/or other materials provided with the 00017 * distribution. Neither the name of Sony Pictures Imageworks nor the 00018 * names of its contributors may be used to endorse or promote 00019 * products derived from this software without specific prior written 00020 * permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00028 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00029 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00030 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 00031 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00032 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 00033 * OF THE POSSIBILITY OF SUCH DAMAGE. 00034 */ 00035 00036 //----------------------------------------------------------------------------// 00037 00042 //----------------------------------------------------------------------------// 00043 00044 #include "ClassFactory.h" 00045 #include "PluginLoader.h" 00046 00047 //----------------------------------------------------------------------------// 00048 00049 using namespace std; 00050 00051 //----------------------------------------------------------------------------// 00052 00053 FIELD3D_NAMESPACE_OPEN 00054 00055 //----------------------------------------------------------------------------// 00056 // Static instances 00057 //----------------------------------------------------------------------------// 00058 00059 ClassFactory* ClassFactory::ms_instance = NULL; 00060 00061 //----------------------------------------------------------------------------// 00062 // ClassFactory implementations 00063 //----------------------------------------------------------------------------// 00064 00065 ClassFactory::ClassFactory() 00066 { 00067 PluginLoader::loadPlugins(); 00068 } 00069 00070 //----------------------------------------------------------------------------// 00071 00072 void ClassFactory::registerField(CreateFieldFnPtr createFunc) 00073 { 00074 // Make sure we don't add the same class twice 00075 00076 bool nameExists = false; 00077 00078 FieldRes::Ptr instance = createFunc(); 00079 00080 if (!instance) { 00081 Msg::print(Msg::SevWarning, 00082 "Unsuccessful attempt at registering Field class. " 00083 "(Creation function returned null pointer)"); 00084 return; 00085 } 00086 00087 string simpleClassName = instance->className(); 00088 string dataTypeName = instance->dataTypeString(); 00089 string className = simpleClassName + "<" + dataTypeName + ">"; 00090 00091 FieldFuncMap::const_iterator i = m_fields.find(className); 00092 if (i != m_fields.end()) 00093 nameExists = true; 00094 00095 if (!nameExists) { 00096 m_fields[className] = createFunc; 00097 // if the simple (untemplated) class name hasn't been registered 00098 // yet, add it to the list and print a message 00099 if (find(m_fieldNames.begin(), m_fieldNames.end(), 00100 simpleClassName) == m_fieldNames.end()) { 00101 m_fieldNames.push_back(simpleClassName); 00102 char *debugEnvVar = getenv("FIELD3D_DEBUG"); 00103 if (debugEnvVar) { 00104 Msg::print("Registered Field class " + simpleClassName); 00105 } 00106 } 00107 00108 } 00109 00110 } 00111 00112 //----------------------------------------------------------------------------// 00113 00114 FieldRes::Ptr 00115 ClassFactory::createField(const std::string &className) const 00116 { 00117 FieldFuncMap::const_iterator i = m_fields.find(className); 00118 if (i != m_fields.end()) 00119 return i->second(); 00120 else 00121 return FieldRes::Ptr(); 00122 } 00123 00124 //----------------------------------------------------------------------------// 00125 00126 void ClassFactory::registerFieldIO(CreateFieldIOFnPtr createFunc) 00127 { 00128 // Make sure we don't add the same class twice 00129 00130 bool nameExists = false; 00131 00132 FieldIO::Ptr instance = createFunc(); 00133 00134 if (!instance) { 00135 Msg::print(Msg::SevWarning, 00136 "Unsuccessful attempt at registering FieldIO class. " 00137 "(Creation function returned null pointer)"); 00138 return; 00139 } 00140 00141 string className = instance->className(); 00142 00143 FieldIOFuncMap::const_iterator i = m_fieldIOs.find(className); 00144 if (i != m_fieldIOs.end()) 00145 nameExists = true; 00146 00147 if (!nameExists) { 00148 m_fieldIOs[className] = createFunc; 00149 // if the simple (untemplated) class name hasn't been registered 00150 // yet, add it to the list and print a message 00151 if (find(m_fieldIONames.begin(), m_fieldIONames.end(), 00152 className) == m_fieldIONames.end()) { 00153 m_fieldIONames.push_back(className); 00154 char *debugEnvVar = getenv("FIELD3D_DEBUG"); 00155 if (debugEnvVar) { 00156 Msg::print("Registered FieldIO class " + className); 00157 } 00158 } 00159 00160 } 00161 00162 } 00163 00164 //----------------------------------------------------------------------------// 00165 00166 FieldIO::Ptr 00167 ClassFactory::createFieldIO(const std::string &className) const 00168 { 00169 FieldIOFuncMap::const_iterator i = m_fieldIOs.find(className); 00170 if (i != m_fieldIOs.end()) 00171 return i->second(); 00172 else 00173 return FieldIO::Ptr(); 00174 } 00175 00176 //----------------------------------------------------------------------------// 00177 00178 void ClassFactory::registerFieldMapping(CreateFieldMappingFnPtr createFunc) 00179 { 00180 // Make sure we don't add the same class twice 00181 00182 bool nameExists = false; 00183 00184 FieldMapping::Ptr instance = createFunc(); 00185 00186 if (!instance) { 00187 Msg::print(Msg::SevWarning, 00188 "Unsuccessful attempt at registering FieldMapping class. " 00189 "(Creation function returned null pointer)"); 00190 return; 00191 } 00192 00193 string className = instance->className(); 00194 00195 FieldMappingFuncMap::const_iterator i = m_mappings.find(className); 00196 if (i != m_mappings.end()) 00197 nameExists = true; 00198 00199 if (!nameExists) { 00200 m_mappings[className] = createFunc; 00201 // if the simple (untemplated) class name hasn't been registered 00202 // yet, add it to the list and print a message 00203 if (find(m_fieldMappingNames.begin(), m_fieldMappingNames.end(), 00204 className) == m_fieldMappingNames.end()) { 00205 m_fieldMappingNames.push_back(className); 00206 char *debugEnvVar = getenv("FIELD3D_DEBUG"); 00207 if (debugEnvVar) { 00208 Msg::print("Registered FieldMapping class " + className); 00209 } 00210 } 00211 } 00212 } 00213 00214 //----------------------------------------------------------------------------// 00215 00216 FieldMapping::Ptr 00217 ClassFactory::createFieldMapping(const std::string &className) const 00218 { 00219 FieldMappingFuncMap::const_iterator i = m_mappings.find(className); 00220 if (i != m_mappings.end()) 00221 return i->second(); 00222 else 00223 return FieldMapping::Ptr(); 00224 } 00225 00226 //----------------------------------------------------------------------------// 00227 00228 void ClassFactory::registerFieldMappingIO(CreateFieldMappingIOFnPtr createFunc) 00229 { 00230 // Make sure we don't add the same class twice 00231 00232 bool nameExists = false; 00233 00234 FieldMappingIO::Ptr instance = createFunc(); 00235 00236 if (!instance) { 00237 Msg::print(Msg::SevWarning, 00238 "Unsuccessful attempt at registering FieldMappingIO class. " 00239 "(Creation function returned null pointer)"); 00240 return; 00241 } 00242 00243 string className = instance->className(); 00244 00245 FieldMappingIOFuncMap::const_iterator i = m_mappingIOs.find(className); 00246 if (i != m_mappingIOs.end()) 00247 nameExists = true; 00248 00249 if (!nameExists) { 00250 m_mappingIOs[className] = createFunc; 00251 // if the simple (untemplated) class name hasn't been registered 00252 // yet, add it to the list and print a message 00253 if (find(m_fieldMappingNames.begin(), m_fieldMappingNames.end(), 00254 className) == m_fieldMappingNames.end()) { 00255 m_fieldMappingNames.push_back(className); 00256 char *debugEnvVar = getenv("FIELD3D_DEBUG"); 00257 if (debugEnvVar) { 00258 Msg::print("Registered FieldMappingIO class " + className); 00259 } 00260 } 00261 } 00262 } 00263 00264 //----------------------------------------------------------------------------// 00265 00266 FieldMappingIO::Ptr 00267 ClassFactory::createFieldMappingIO(const std::string &className) const 00268 { 00269 FieldMappingIOFuncMap::const_iterator i = m_mappingIOs.find(className); 00270 if (i != m_mappingIOs.end()) 00271 return i->second(); 00272 else 00273 return FieldMappingIO::Ptr(); 00274 } 00275 00276 //----------------------------------------------------------------------------// 00277 00278 ClassFactory& 00279 ClassFactory::singleton() 00280 { 00281 if (!ms_instance) 00282 ms_instance = new ClassFactory; 00283 return *ms_instance; 00284 } 00285 00286 //----------------------------------------------------------------------------// 00287 00288 FIELD3D_NAMESPACE_SOURCE_CLOSE 00289 00290 //----------------------------------------------------------------------------//