Field3D
ClassFactory.cpp
Go to the documentation of this file.
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              className) == m_fieldNames.end()) {
00101       m_fieldNames.push_back(className);
00102       char *debugEnvVar = getenv("FIELD3D_DEBUG");
00103       if (debugEnvVar) {
00104         Msg::print("Registered Field class " + className);
00105       }
00106     }
00107   } 
00108 
00109 }
00110 
00111 //----------------------------------------------------------------------------//
00112 
00113 FieldRes::Ptr 
00114 ClassFactory::createField(const std::string &className) const
00115 {
00116   FieldFuncMap::const_iterator i = m_fields.find(className);
00117   if (i != m_fields.end())
00118    return i->second();
00119   else
00120     return FieldRes::Ptr();
00121 }
00122 
00123 //----------------------------------------------------------------------------//
00124 
00125 void ClassFactory::registerFieldIO(CreateFieldIOFnPtr createFunc)
00126 {
00127   // Make sure we don't add the same class twice
00128 
00129   bool nameExists = false;
00130 
00131   FieldIO::Ptr instance = createFunc();
00132 
00133   if (!instance) {
00134     Msg::print(Msg::SevWarning,
00135                "Unsuccessful attempt at registering FieldIO class. "
00136                "(Creation function returned null pointer)");
00137     return;
00138   }
00139 
00140   string className = instance->className();
00141 
00142   FieldIOFuncMap::const_iterator i = m_fieldIOs.find(className);
00143   if (i != m_fieldIOs.end())
00144     nameExists = true;  
00145 
00146   if (!nameExists) {
00147     m_fieldIOs[className] = createFunc;
00148     // if the simple (untemplated) class name hasn't been registered
00149     // yet, add it to the list and print a message
00150     if (find(m_fieldIONames.begin(), m_fieldIONames.end(),
00151              className) == m_fieldIONames.end()) {
00152       m_fieldIONames.push_back(className);
00153       char *debugEnvVar = getenv("FIELD3D_DEBUG");
00154       if (debugEnvVar) {
00155         Msg::print("Registered FieldIO class " + className);
00156       }
00157     }
00158   } 
00159 
00160 }
00161 
00162 //----------------------------------------------------------------------------//
00163 
00164 FieldIO::Ptr 
00165 ClassFactory::createFieldIO(const std::string &className) const
00166 {
00167   FieldIOFuncMap::const_iterator m = m_fieldIOs.begin();
00168   FieldIOFuncMap::const_iterator i = m_fieldIOs.find(className);
00169   if (i != m_fieldIOs.end())
00170     return i->second();
00171   else
00172     return FieldIO::Ptr();
00173 }
00174 
00175 //----------------------------------------------------------------------------//
00176 
00177 void ClassFactory::registerFieldMapping(CreateFieldMappingFnPtr createFunc)
00178 {
00179   // Make sure we don't add the same class twice
00180 
00181   bool nameExists = false;
00182 
00183   FieldMapping::Ptr instance = createFunc();
00184 
00185   if (!instance) {
00186     Msg::print(Msg::SevWarning,
00187                "Unsuccessful attempt at registering FieldMapping class. "
00188                "(Creation function returned null pointer)");
00189     return;
00190   }
00191 
00192   string className = instance->className();
00193 
00194   FieldMappingFuncMap::const_iterator i = m_mappings.find(className);
00195   if (i != m_mappings.end())
00196     nameExists = true;  
00197 
00198   if (!nameExists) {
00199     m_mappings[className] = createFunc;
00200     // if the simple (untemplated) class name hasn't been registered
00201     // yet, add it to the list and print a message
00202     if (find(m_fieldMappingNames.begin(), m_fieldMappingNames.end(),
00203              className) == m_fieldMappingNames.end()) {
00204       m_fieldMappingNames.push_back(className);
00205       char *debugEnvVar = getenv("FIELD3D_DEBUG");
00206       if (debugEnvVar) {
00207         Msg::print("Registered FieldMapping class " + className);
00208       }
00209     }
00210   } 
00211 }
00212 
00213 //----------------------------------------------------------------------------//
00214 
00215 FieldMapping::Ptr 
00216 ClassFactory::createFieldMapping(const std::string &className) const
00217 {
00218   FieldMappingFuncMap::const_iterator i = m_mappings.find(className);
00219   if (i != m_mappings.end())
00220     return i->second();
00221   else
00222     return FieldMapping::Ptr();
00223 }
00224 
00225 //----------------------------------------------------------------------------//
00226 
00227 void ClassFactory::registerFieldMappingIO(CreateFieldMappingIOFnPtr createFunc)
00228 {
00229   // Make sure we don't add the same class twice
00230 
00231   bool nameExists = false;
00232 
00233   FieldMappingIO::Ptr instance = createFunc();
00234 
00235   if (!instance) {
00236     Msg::print(Msg::SevWarning,
00237                "Unsuccessful attempt at registering FieldMappingIO class. "
00238                "(Creation function returned null pointer)");
00239     return;
00240   }
00241 
00242   string className = instance->className();
00243 
00244   FieldMappingIOFuncMap::const_iterator i = m_mappingIOs.find(className);
00245   if (i != m_mappingIOs.end())
00246     nameExists = true;  
00247 
00248   if (!nameExists) {
00249     m_mappingIOs[className] = createFunc;
00250     // if the simple (untemplated) class name hasn't been registered
00251     // yet, add it to the list and print a message
00252     if (find(m_fieldMappingNames.begin(), m_fieldMappingNames.end(),
00253              className) == m_fieldMappingNames.end()) {
00254       m_fieldMappingNames.push_back(className);
00255       char *debugEnvVar = getenv("FIELD3D_DEBUG");
00256       if (debugEnvVar) {
00257         Msg::print("Registered FieldMappingIO class " + className);
00258       }
00259     }
00260   } 
00261 }
00262 
00263 //----------------------------------------------------------------------------//
00264 
00265 FieldMappingIO::Ptr 
00266 ClassFactory::createFieldMappingIO(const std::string &className) const
00267 {
00268   FieldMappingIOFuncMap::const_iterator i = m_mappingIOs.find(className);
00269   if (i != m_mappingIOs.end())
00270     return i->second();
00271   else
00272     return FieldMappingIO::Ptr();
00273 }
00274 
00275 //----------------------------------------------------------------------------//
00276 
00277 ClassFactory& 
00278 ClassFactory::singleton()
00279 { 
00280   if (!ms_instance)
00281     ms_instance = new ClassFactory;
00282   return *ms_instance;
00283 }
00284 
00285 //----------------------------------------------------------------------------//
00286 
00287 FIELD3D_NAMESPACE_SOURCE_CLOSE
00288 
00289 //----------------------------------------------------------------------------//