//! //! \file //! \author Andre Prins //! \date 7 April 2010 //! #ifndef UTILITIES_SETTINGS_H #define UTILITIES_SETTINGS_H #include "Utilities_ExportDefinitions.h" #include #include #include #include namespace Utilities { //! //! \class Settings Settings.h //! \author Andre Prins //! \date 7 April 2010 //! //! \brief Small "options" class for loading "options" from the command-line. //! //! This options-class (using boost::program_options) can load settings from a //! configuration file (INI file) or from the command-line. //! Basic approach, subclass your own MySettings class, derived from Settings. //! Use Settings::add( ) to fill it with the necessary option-groups. //! //! \code //! Settings s; //! bool success = s.load( argc, argv ); //! if ( success ) //! // Continue with the application... //! else //! std::cerr << "Failed to load options." << std::endl; exit(1); //! \endcode //! class UTILITIES_EXPORT Settings { public: Settings( const std::string& name = "", const std::string& defaults = "" ); virtual ~Settings( ); //! \brief Add a new group with options. virtual void add( const boost::program_options::options_description& d ); //! \brief Add a searchpath for the default settings-file virtual void addpath( const std::string& p ); virtual void clearpaths( ); //! \brief Load or fill the options from command-line, stream, file or a default. virtual bool load( ); virtual bool load( int argc, char* argv[] ); virtual bool load( std::istream& in ); virtual bool load( const std::string& filename ); //! \brief Check whether the requested key is available. virtual bool contains( const std::string& key ) const; //! \brief Template-method for getting the value by requested type T. template< typename T > const T get( const std::string& key ) const; protected: // For output virtual void help( std::ostream& out ); virtual void save( std::ostream& out ); private: bool loadOptions( int argc, char* argv[] ); bool loadOptions( std::istream& in ); private: // The map with the eventual settings. boost::program_options::variables_map m_optionsMap; std::string m_name; std::string m_defaultOptions; std::string m_defaultFileName; // We also keep track of our "own" settings. boost::program_options::options_description m_commandLineOptions; boost::program_options::options_description m_configFileOptions; // Track the application-path (either the 'initial-dir', or later the actual app-path) std::vector< std::string > m_searchPaths; }; //! \brief Standard get method. template< typename T > const T Settings::get( const std::string& key ) const { try { return this->m_optionsMap[key].as< T >( ); } catch( std::exception& e ) { std::cerr << "Error catching: " << key << " - " << e.what( ) << std::endl; return T(); } } //! \brief Specialized get method, for converting a string value, e.g. "(1, 3)" //! to a vector (of doubles). template<> const std::vector< double > UTILITIES_EXPORT Settings::get< std::vector< double > >( const std::string& key ) const; template<> //! \brief Specialized get method, for converting a string value, e.g. "(1, 3)" //! to a vector (of ints). const std::vector< int > UTILITIES_EXPORT Settings::get< std::vector< int > >( const std::string& key ) const; //! \brief And a specialized method, for converting the testdatapath template<> const std::string UTILITIES_EXPORT Settings::get< std::string >( const std::string& key ) const; //! \brief A specialized method, for automatically returning a vector of strings template<> const std::vector< std::string > UTILITIES_EXPORT Settings::get< std::vector< std::string > >( const std::string& key ) const; } // Utilities namespace #endif // UTILITIES_SETTINGS_H