ClassFactory.cpp
Go to the documentation of this file.
//----------------------------------------------------------------------------//
/*
* Copyright (c) 2009 Sony Pictures Imageworks Inc
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution. Neither the name of Sony Pictures Imageworks nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
//----------------------------------------------------------------------------//
//----------------------------------------------------------------------------//
#include "ClassFactory.h"
#include "PluginLoader.h"
//----------------------------------------------------------------------------//
using namespace std;
//----------------------------------------------------------------------------//
FIELD3D_NAMESPACE_OPEN
//----------------------------------------------------------------------------//
// Static instances
//----------------------------------------------------------------------------//
boost::scoped_ptr<ClassFactory> ClassFactory::ms_instance;
//----------------------------------------------------------------------------//
// ClassFactory implementations
//----------------------------------------------------------------------------//
ClassFactory::ClassFactory()
{
PluginLoader::loadPlugins();
}
//----------------------------------------------------------------------------//
void ClassFactory::registerField(CreateFieldFnPtr createFunc)
{
// Make sure we don't add the same class twice
bool nameExists = false;
FieldRes::Ptr instance = createFunc();
if (!instance) {
Msg::print(Msg::SevWarning,
"Unsuccessful attempt at registering Field class. "
"(Creation function returned null pointer)");
return;
}
string simpleClassName = instance->className();
string dataTypeName = instance->dataTypeString();
string className = simpleClassName + "<" + dataTypeName + ">";
FieldFuncMap::const_iterator i = m_fields.find(className);
if (i != m_fields.end())
nameExists = true;
if (!nameExists) {
m_fields[className] = createFunc;
// if the simple (untemplated) class name hasn't been registered
// yet, add it to the list and print a message
if (find(m_fieldNames.begin(), m_fieldNames.end(),
className) == m_fieldNames.end()) {
m_fieldNames.push_back(className);
char *debugEnvVar = getenv("FIELD3D_DEBUG");
if (debugEnvVar) {
Msg::print("Registered Field class " + className);
}
}
}
}
//----------------------------------------------------------------------------//
FieldRes::Ptr
ClassFactory::createField(const std::string &className) const
{
FieldFuncMap::const_iterator i = m_fields.find(className);
if (i != m_fields.end())
return i->second();
else
return FieldRes::Ptr();
}
//----------------------------------------------------------------------------//
void ClassFactory::registerFieldIO(CreateFieldIOFnPtr createFunc)
{
// Make sure we don't add the same class twice
bool nameExists = false;
FieldIO::Ptr instance = createFunc();
if (!instance) {
Msg::print(Msg::SevWarning,
"Unsuccessful attempt at registering FieldIO class. "
"(Creation function returned null pointer)");
return;
}
string className = instance->className();
FieldIOFuncMap::const_iterator i = m_fieldIOs.find(className);
if (i != m_fieldIOs.end())
nameExists = true;
if (!nameExists) {
m_fieldIOs[className] = createFunc;
// if the simple (untemplated) class name hasn't been registered
// yet, add it to the list and print a message
if (find(m_fieldIONames.begin(), m_fieldIONames.end(),
className) == m_fieldIONames.end()) {
m_fieldIONames.push_back(className);
char *debugEnvVar = getenv("FIELD3D_DEBUG");
if (debugEnvVar) {
Msg::print("Registered FieldIO class " + className);
}
}
}
}
//----------------------------------------------------------------------------//
FieldIO::Ptr
ClassFactory::createFieldIO(const std::string &className) const
{
// FieldIOFuncMap::const_iterator m = m_fieldIOs.begin();
FieldIOFuncMap::const_iterator i = m_fieldIOs.find(className);
if (i != m_fieldIOs.end())
return i->second();
else
return FieldIO::Ptr();
}
//----------------------------------------------------------------------------//
void ClassFactory::registerFieldMapping(CreateFieldMappingFnPtr createFunc)
{
// Make sure we don't add the same class twice
bool nameExists = false;
FieldMapping::Ptr instance = createFunc();
if (!instance) {
Msg::print(Msg::SevWarning,
"Unsuccessful attempt at registering FieldMapping class. "
"(Creation function returned null pointer)");
return;
}
string className = instance->className();
FieldMappingFuncMap::const_iterator i = m_mappings.find(className);
if (i != m_mappings.end())
nameExists = true;
if (!nameExists) {
m_mappings[className] = createFunc;
// if the simple (untemplated) class name hasn't been registered
// yet, add it to the list and print a message
if (find(m_fieldMappingNames.begin(), m_fieldMappingNames.end(),
className) == m_fieldMappingNames.end()) {
m_fieldMappingNames.push_back(className);
char *debugEnvVar = getenv("FIELD3D_DEBUG");
if (debugEnvVar) {
Msg::print("Registered FieldMapping class " + className);
}
}
}
}
//----------------------------------------------------------------------------//
FieldMapping::Ptr
ClassFactory::createFieldMapping(const std::string &className) const
{
FieldMappingFuncMap::const_iterator i = m_mappings.find(className);
if (i != m_mappings.end())
return i->second();
else
return FieldMapping::Ptr();
}
//----------------------------------------------------------------------------//
void ClassFactory::registerFieldMappingIO(CreateFieldMappingIOFnPtr createFunc)
{
// Make sure we don't add the same class twice
bool nameExists = false;
FieldMappingIO::Ptr instance = createFunc();
if (!instance) {
Msg::print(Msg::SevWarning,
"Unsuccessful attempt at registering FieldMappingIO class. "
"(Creation function returned null pointer)");
return;
}
string className = instance->className();
FieldMappingIOFuncMap::const_iterator i = m_mappingIOs.find(className);
if (i != m_mappingIOs.end())
nameExists = true;
if (!nameExists) {
m_mappingIOs[className] = createFunc;
// if the simple (untemplated) class name hasn't been registered
// yet, add it to the list and print a message
if (find(m_fieldMappingNames.begin(), m_fieldMappingNames.end(),
className) == m_fieldMappingNames.end()) {
m_fieldMappingNames.push_back(className);
char *debugEnvVar = getenv("FIELD3D_DEBUG");
if (debugEnvVar) {
Msg::print("Registered FieldMappingIO class " + className);
}
}
}
}
//----------------------------------------------------------------------------//
FieldMappingIO::Ptr
ClassFactory::createFieldMappingIO(const std::string &className) const
{
FieldMappingIOFuncMap::const_iterator i = m_mappingIOs.find(className);
if (i != m_mappingIOs.end())
return i->second();
else
return FieldMappingIO::Ptr();
}
//----------------------------------------------------------------------------//
ClassFactory&
ClassFactory::singleton()
{
if (ms_instance.get() == NULL) {
ms_instance.reset(new ClassFactory);
}
return *ms_instance;
}
//----------------------------------------------------------------------------//
FIELD3D_NAMESPACE_SOURCE_CLOSE
//----------------------------------------------------------------------------//
ClassFactory.h
Contains the ClassFactory class for registering Field3D classes.
PluginLoader.h
Contains the PluginLoader class.
ClassFactory
Definition ClassFactory.h:72
ClassFactory::CreateFieldIOFnPtr
FieldIO::Ptr(* CreateFieldIOFnPtr)()
Definition ClassFactory.h:79
ClassFactory::m_fieldIONames
NameVec m_fieldIONames
Definition ClassFactory.h:151
ClassFactory::createField
FieldRes::Ptr createField(const std::string &className) const
Instances an object by name.
Definition ClassFactory.cpp:114
ClassFactory::createFieldIO
FieldIO::Ptr createFieldIO(const std::string &className) const
Instances an IO object by name.
Definition ClassFactory.cpp:165
ClassFactory::singleton
static ClassFactory & singleton()
}
Definition ClassFactory.cpp:278
ClassFactory::ClassFactory
ClassFactory()
Standard constructor.
Definition ClassFactory.cpp:65
ClassFactory::CreateFieldMappingIOFnPtr
FieldMappingIO::Ptr(* CreateFieldMappingIOFnPtr)()
Definition ClassFactory.h:81
ClassFactory::m_mappingIOs
FieldMappingIOFuncMap m_mappingIOs
Map of create functions for FieldMapping IO classes. The key is the class name.
Definition ClassFactory.h:160
ClassFactory::createFieldMapping
FieldMapping::Ptr createFieldMapping(const std::string &className) const
Instances an object by name.
Definition ClassFactory.cpp:216
ClassFactory::createFieldMappingIO
FieldMappingIO::Ptr createFieldMappingIO(const std::string &className) const
Instances an IO object by name.
Definition ClassFactory.cpp:266
ClassFactory::ms_instance
static boost::scoped_ptr< ClassFactory > ms_instance
Pointer to static instance.
Definition ClassFactory.h:165
ClassFactory::CreateFieldFnPtr
FieldRes::Ptr(* CreateFieldFnPtr)()
Definition ClassFactory.h:78
ClassFactory::registerField
void registerField(CreateFieldFnPtr createFunc)
Registers a class with the class pool.
Definition ClassFactory.cpp:72
ClassFactory::registerFieldMapping
void registerFieldMapping(CreateFieldMappingFnPtr createFunc)
Registers a class with the class pool.
Definition ClassFactory.cpp:177
ClassFactory::registerFieldIO
void registerFieldIO(CreateFieldIOFnPtr createFunc)
Registers an IO class with the class pool.
Definition ClassFactory.cpp:125
ClassFactory::registerFieldMappingIO
void registerFieldMappingIO(CreateFieldMappingIOFnPtr createFunc)
Registers an IO class with the class pool.
Definition ClassFactory.cpp:227
ClassFactory::m_mappings
FieldMappingFuncMap m_mappings
Map of create functions for FieldMappings. The key is the class name.
Definition ClassFactory.h:154
ClassFactory::m_fields
FieldFuncMap m_fields
Map of create functions for Fields. The key is the class name.
Definition ClassFactory.h:144
ClassFactory::m_fieldNames
NameVec m_fieldNames
Definition ClassFactory.h:146
ClassFactory::m_fieldIOs
FieldIOFuncMap m_fieldIOs
Map of create functions for FieldIO classes. The key is the class name.
Definition ClassFactory.h:149
ClassFactory::CreateFieldMappingFnPtr
FieldMapping::Ptr(* CreateFieldMappingFnPtr)()
Definition ClassFactory.h:80
ClassFactory::m_fieldMappingNames
NameVec m_fieldMappingNames
Definition ClassFactory.h:156
FieldIO::Ptr
boost::intrusive_ptr< FieldIO > Ptr
Definition FieldIO.h:91
FieldMappingIO::Ptr
boost::intrusive_ptr< FieldMappingIO > Ptr
Definition FieldMappingIO.h:71
FieldMapping::Ptr
boost::intrusive_ptr< FieldMapping > Ptr
Definition FieldMapping.h:92
FieldRes::Ptr
boost::intrusive_ptr< FieldRes > Ptr
Definition Field.h:213
PluginLoader::loadPlugins
static void loadPlugins()
Checks all paths in $FIELD3D_DSO_PATH and loads the plugins it finds.
Definition PluginLoader.cpp:252
FIELD3D_NAMESPACE_OPEN
Definition FieldMapping.cpp:74
Msg::SevWarning
@ SevWarning
Definition Log.h:68
Msg::print
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
FIELD3D_NAMESPACE_SOURCE_CLOSE
#define FIELD3D_NAMESPACE_SOURCE_CLOSE
Definition ns.h:60