From dabd9008d9b3610ee1c6d1833d0bc5a32736dd73 Mon Sep 17 00:00:00 2001 From: Manuel Grizonnet <manuel.grizonnet@gmail.com> Date: Tue, 13 Oct 2009 18:13:30 +0200 Subject: [PATCH] ENH: add otbconfigurationfile.cxx in common with generic getparam and singleton --- Code/Common/otbConfigurationFile.cxx | 68 ++++++++++++++++++++ Code/Common/otbConfigurationFile.h | 45 ++++++------- Testing/Code/Common/otbConfigurationTest.cxx | 9 ++- 3 files changed, 92 insertions(+), 30 deletions(-) create mode 100644 Code/Common/otbConfigurationFile.cxx diff --git a/Code/Common/otbConfigurationFile.cxx b/Code/Common/otbConfigurationFile.cxx new file mode 100644 index 0000000000..d8678888be --- /dev/null +++ b/Code/Common/otbConfigurationFile.cxx @@ -0,0 +1,68 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + Some parts of this code are derived from ITK. See ITKCopyright.txt + for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#include "otbConfigurationFile.h" + +namespace otb +{ +/** Initialize the singleton */ +ConfigurationFile::Pointer ConfigurationFile::Instance = NULL; + +ConfigurationFile +::ConfigurationFile() +{ + std::string OTBBinDir(OTB_CONFIG); + try + { + m_OTBConfig = new ConfigFile(OTBBinDir + "/otb.conf"); + } + catch (ConfigFile::file_not_found& e) + { + itkExceptionMacro(<< "Error - File '" << e.filename << "' not found."); + } +} + +ConfigurationFile +::~ConfigurationFile() +{ +} + +ConfigurationFile::Pointer +ConfigurationFile +::GetInstance() +{ + if (!Instance) + { + Instance = Self::New(); + } + return Instance; +}; + + +void +ConfigurationFile +::PrintSelf(std::ostream& os, itk::Indent indent) const +{ + Superclass::PrintSelf(os, indent); + os << indent; + os << (*m_OTBConfig); + +} +} // end namespace otb diff --git a/Code/Common/otbConfigurationFile.h b/Code/Common/otbConfigurationFile.h index 45ecdaed66..0f327be6d4 100644 --- a/Code/Common/otbConfigurationFile.h +++ b/Code/Common/otbConfigurationFile.h @@ -45,46 +45,41 @@ namespace otb /** Standard macro */ itkTypeMacro(ConfigurationFile,Object); - /** This is protected for the singleton. Use GetInstance() instead. */ - itkNewMacro(Self); + /** Get the unique instanc1e of the model */ -// static Pointer GetInstance() -// { -// if (!Instance) -// { -// Instance = Self::New(); -// } -// return Instance; -// }; + static Pointer GetInstance(); ConfigFile * GetOTBConfig() { return m_OTBConfig; }; - - std::string GetLanguage() - { - return m_OTBConfig->read<std::string>( "OTB_LANG" ); + + /** Get parameter*/ + template<typename T> T GetParameter(const std::string & key) const { + try + { + return m_OTBConfig->read<T>( key ); + } + catch( ConfigFile::key_not_found& e ) { + itkExceptionMacro(<< "Error - Key '" << e.key << "' not found."); + } + }; -// std::string lib(OTB_CONFIG); protected: - + /** This is protected for the singleton. Use GetInstance() instead. */ + itkNewMacro(Self); /** Constructor */ - ConfigurationFile() - { - std::string OTBBinDir(OTB_CONFIG); - m_OTBConfig = new ConfigFile(OTBBinDir + "/otb.conf"); - } - ; + ConfigurationFile(); + /** Destructor */ - ~ConfigurationFile(){}; + ~ConfigurationFile(); /** PrintSelf method */ - void PrintSelf(std::ostream& os, itk::Indent indent) const {}; + void PrintSelf(std::ostream& os, itk::Indent indent) const ; private: /** The instance singleton */ -// static Pointer Instance = NULL; + static Pointer Instance; ConfigFile * m_OTBConfig; }; }// end namespace diff --git a/Testing/Code/Common/otbConfigurationTest.cxx b/Testing/Code/Common/otbConfigurationTest.cxx index 64818c88aa..5ddcf80a50 100644 --- a/Testing/Code/Common/otbConfigurationTest.cxx +++ b/Testing/Code/Common/otbConfigurationTest.cxx @@ -27,14 +27,13 @@ int otbConfigurationTest(int argc, char * argv[]) //Instantiation // ConfigurationType::Pointer conf = ConfigurationType::GetInstance(); - ConfigurationType::Pointer conf = ConfigurationType::New(); + ConfigurationType::Pointer conf = ConfigurationType::GetInstance(); // conf->Load(); + std::string lang = conf->GetParameter<std::string>("OTB_LANG"); - std::string lang = conf->GetLanguage(); + std::cout << conf << std::endl; - std::cout << "config language " << lang << std::endl; - - if (lang != "fr") + if (lang != "fr_FR.UTF-8") return EXIT_FAILURE; return EXIT_SUCCESS; -- GitLab