Commit 906a86c8 authored by Guillaume Pasero's avatar Guillaume Pasero

ENH: improve Application loading

parent 1ebd2c4d
......@@ -51,6 +51,18 @@ public:
/** Run-time type information (and related methods). */
itkTypeMacro(ApplicationFactory, itk::ObjectFactoryBase);
void SetClassName(const char* name)
{
// remove namespace, only keep class name
std::string tmpName(name);
std::string::size_type pos = tmpName.rfind("::");
if (pos != std::string::npos)
{
tmpName = tmpName.substr(pos+2);
}
m_ClassName.assign(tmpName);
}
protected:
ApplicationFactory()
......@@ -68,9 +80,8 @@ protected:
* is not supported by the factory implementation. */
virtual LightObject::Pointer CreateObject(const char* itkclassname )
{
const std::string classname("otbWrapperApplication");
LightObject::Pointer ret;
if ( classname == itkclassname )
if ( m_ClassName == itkclassname)
ret = TApplication::New().GetPointer();
return ret;
......@@ -82,9 +93,10 @@ protected:
virtual std::list<LightObject::Pointer>
CreateAllObject(const char* itkclassname)
{
const std::string classname("otbWrapperApplication");
const std::string applicationClass("otbWrapperApplication");
std::list<LightObject::Pointer> list;
if ( classname == itkclassname )
if ( m_ClassName == itkclassname ||
applicationClass == itkclassname )
list.push_back(TApplication::New().GetPointer());
return list;
......@@ -93,6 +105,8 @@ protected:
private:
ApplicationFactory(const Self &); //purposely not implemented
void operator =(const Self&); //purposely not implemented
std::string m_ClassName;
};
} // end namespace Wrapper
......@@ -112,6 +126,7 @@ private:
OTB_APP_EXPORT itk::ObjectFactoryBase* itkLoad() \
{ \
staticFactory = ApplicationFactoryType::New(); \
staticFactory->SetClassName(#ApplicationType); \
return staticFactory; \
} \
}
......
......@@ -73,6 +73,14 @@ ApplicationRegistry::AddApplicationPath(std::string newpath)
// Reload factories to take into account new path
itk::ObjectFactoryBase::ReHash();
std::ostringstream resetEnvPath;
resetEnvPath << "ITK_AUTOLOAD_PATH=";
if (currentEnv)
{
resetEnvPath << currentEnv;
}
itksys::SystemTools::PutEnv(resetEnvPath.str().c_str());
}
......@@ -81,42 +89,22 @@ ApplicationRegistry::CreateApplication(const std::string& name)
{
ApplicationPointer appli;
std::list<ApplicationPointer> possibleApp;
std::list<LightObject::Pointer> allobjects = itk::ObjectFactoryBase::CreateAllInstance("otbWrapperApplication");
// Downcast and Sanity check
for (std::list<LightObject::Pointer>::iterator i = allobjects.begin(); i != allobjects.end(); ++i)
LightObject::Pointer possibleApp = itk::ObjectFactoryBase::CreateInstance(name.c_str());
if (possibleApp.IsNotNull())
{
Application* app = dynamic_cast<Application*> (i->GetPointer());
// Downcast
Application* app = dynamic_cast<Application*> (possibleApp.GetPointer());
if (app)
{
possibleApp.push_back(app);
appli = app;
}
else
{
otbMsgDevMacro( << "Error ApplicationRegistry factory did not return an Application: " << (*i)->GetNameOfClass() << std::endl );
}
}
// Return the app with the desired name
for(std::list<ApplicationPointer>::iterator k = possibleApp.begin();
k != possibleApp.end(); ++k)
{
try
{
(*k)->Init();
if ( (*k)->GetName() == name )
{
appli = *k;
break;
}
}
catch(...)
{
otbMsgDevMacro( << "Error a faulty Application has been detected: "<<(*k)->GetNameOfClass() << std::endl );
otbMsgDevMacro( << "Error ApplicationRegistry factory did not return an Application: " << possibleApp->GetNameOfClass() << std::endl );
}
}
return appli;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment