#ifndef __CPPLUGINS__PARAMETERS__H__ #define __CPPLUGINS__PARAMETERS__H__ #include #include #include #include #include #include #include #include // Some forward declarations namespace tinyxml2 { class XMLElement; class XMLDocument; } // ------------------------------------------------------------------------- #define cpPlugins_Parameters_Configure( Y ) \ void ConfigureAs##Y( const std::string& name ) \ { \ this->m_Parameters[ name ] = TParameter( Self::Y, "" ); \ this->Modified( ); \ } \ bool Has##Y( const std::string& name ) const \ { \ auto i = this->m_Parameters.find( name ); \ if( i != this->m_Parameters.end( ) ) \ return( i->second.first == Self::Y ); \ else \ return( false ); \ } // ------------------------------------------------------------------------- #define cpPlugins_Parameters_GetSet( Y ) \ T##Y Get##Y( const std::string& name ) const \ { \ auto i = this->m_Parameters.find( name ); \ if( i != this->m_Parameters.end( ) ) \ { \ if( i->second.first == Self::Y ) \ { \ if( typeid( T##Y ) != typeid( std::string ) ) \ { \ std::istringstream tok_str( i->second.second ); \ T##Y v; \ tok_str >> v; \ return( v ); \ } \ else \ { \ const T##Y* ptr = \ reinterpret_cast< const T##Y* >( \ &( i->second.second ) \ ); \ return( *ptr ); \ } \ } \ } \ return( T##Y( 0 ) ); \ } \ void Set##Y( const std::string& name, const T##Y& v ) \ { \ auto i = this->m_Parameters.find( name ); \ if( i != this->m_Parameters.end( ) ) \ { \ if( i->second.first == Self::Y ) \ { \ if( typeid( T##Y ) != typeid( std::string ) ) \ { \ std::stringstream str; \ str << v; \ if( i->second.second != str.str( ) ) \ { \ i->second.second = str.str( ); \ this->Modified( ); \ } \ } \ else \ { \ const std::string* str = \ reinterpret_cast< const std::string* >( &v ); \ if( i->second.second != *str ) \ { \ i->second.second = *str; \ this->Modified( ); \ } \ } \ } \ } \ } // ------------------------------------------------------------------------- #define cpPlugins_Parameters_GetSetList( Y ) \ std::vector< T##Y > Get##Y##List( const std::string& name ) const \ { \ std::vector< T##Y > lst; \ std::vector< std::string >* slst = \ reinterpret_cast< std::vector< std::string >* >( &lst ); \ auto i = this->m_Parameters.find( name ); \ if( i != this->m_Parameters.end( ) ) \ { \ if( i->second.first == Self::Y##List ) \ { \ std::istringstream str( i->second.second ); \ std::string token; \ while( std::getline( str, token, '#' ) ) \ { \ if( typeid( T##Y ) != typeid( std::string ) ) \ { \ std::istringstream tok_str( token ); \ T##Y v; \ tok_str >> v; \ lst.push_back( v ); \ } \ else \ slst->push_back( token ); \ } \ } \ } \ return( lst ); \ } \ void AddTo##Y##List( const std::string& name, const T##Y& v ) \ { \ auto i = this->m_Parameters.find( name ); \ if( i != this->m_Parameters.end( ) ) \ { \ if( i->second.first == Self::Y##List ) \ { \ std::stringstream str; \ if( i->second.second != "" ) \ str << i->second.second << "#"; \ str << v; \ i->second.second = str.str( ); \ this->Modified( ); \ } \ } \ } \ void Clear##Y##List( const std::string& name ) \ { \ auto i = this->m_Parameters.find( name ); \ if( i != this->m_Parameters.end( ) ) \ { \ if( i->second.first == Self::Y##List ) \ { \ if( i->second.second != "" ) \ { \ i->second.second = ""; \ this->Modified( ); \ } \ } \ } \ } namespace cpPlugins { // Forward declaration to improve Qt dialog execution class ParametersQtDialog; /** */ class cpPlugins_EXPORT Parameters { // Frienship with forward declaration to improve Qt dialog execution friend class ParametersQtDialog; friend std::ostream& operator<<( std::ostream& o, const Parameters& p ) { for( auto i = p.m_Parameters.begin( ); i != p.m_Parameters.end( ); ++i ) o << i->first << ": (" << i->second.first << " | " << i->second.second << ")" << std::endl; return( o ); } public: typedef Parameters Self; enum Type { String , Bool , Int , Uint , Real , OpenFileName , SaveFileName , PathName , StringList , BoolList , IntList , UintList , RealList , OpenFileNameList , SaveFileNameList , PathNameList , Choices , NoType }; typedef bool TBool; typedef long TInt; typedef unsigned long TUint; typedef double TReal; typedef std::string TString; typedef std::string TOpenFileName; typedef std::string TSaveFileName; typedef std::string TPathName; typedef std::pair< Self::Type, std::string > TParameter; typedef std::map< std::string, TParameter > TParameters; public: cpPlugins_Parameters_Configure( String ); cpPlugins_Parameters_Configure( Bool ); cpPlugins_Parameters_Configure( Int ); cpPlugins_Parameters_Configure( Uint ); cpPlugins_Parameters_Configure( Real ); cpPlugins_Parameters_Configure( OpenFileName ); cpPlugins_Parameters_Configure( SaveFileName ); cpPlugins_Parameters_Configure( PathName ); cpPlugins_Parameters_Configure( StringList ); cpPlugins_Parameters_Configure( BoolList ); cpPlugins_Parameters_Configure( IntList ); cpPlugins_Parameters_Configure( UintList ); cpPlugins_Parameters_Configure( RealList ); cpPlugins_Parameters_Configure( OpenFileNameList ); cpPlugins_Parameters_Configure( SaveFileNameList ); cpPlugins_Parameters_Configure( PathNameList ); cpPlugins_Parameters_Configure( Choices ); cpPlugins_Parameters_GetSet( Bool ); cpPlugins_Parameters_GetSet( Int ); cpPlugins_Parameters_GetSet( Uint ); cpPlugins_Parameters_GetSet( Real ); cpPlugins_Parameters_GetSet( OpenFileName ); cpPlugins_Parameters_GetSet( SaveFileName ); cpPlugins_Parameters_GetSet( PathName ); cpPlugins_Parameters_GetSetList( String ); cpPlugins_Parameters_GetSetList( Bool ); cpPlugins_Parameters_GetSetList( Int ); cpPlugins_Parameters_GetSetList( Uint ); cpPlugins_Parameters_GetSetList( Real ); cpPlugins_Parameters_GetSetList( OpenFileName ); cpPlugins_Parameters_GetSetList( SaveFileName ); cpPlugins_Parameters_GetSetList( PathName ); public: Parameters( ); virtual ~Parameters( ); virtual void Modified( ) const; virtual itk::ModifiedTimeType GetMTime( ) const; // Parameters container configuration void Clear( ); // Get methods void GetNames( std::vector< std::string >& container ) const; Type GetType( const std::string& name ) const; std::string GetTypeAsString( const std::string& name ) const; static Type GetTypeFromString( const std::string& t ); // Base string methods std::string GetString( const std::string& name, bool force = true ) const; void SetString( const std::string& name, const std::string& v, bool force = true ); void ConfigureAsChoices( const std::string& name, const std::vector< std::string >& choices ); std::vector< std::string > GetChoices( const std::string& name ) const; std::string GetSelectedChoice( const std::string& name ) const; bool SetSelectedChoice( const std::string& name, const std::string& choice ); std::string GetAcceptedFileExtensions( const std::string& name ) const; void SetAcceptedFileExtensions( const std::string& name, const std::string& extensions ); // XML "streaming" bool ToXML( tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* parent_elem ) const; bool FromXML( const tinyxml2::XMLElement* filter_elem ); protected: TParameters& GetRawParameters( ); const TParameters& GetRawParameters( ) const; private: // Purposely not implemented Parameters( const Self& other ); Self& operator=( const Self& other ); protected: mutable itk::TimeStamp m_TimeStamp; TParameters m_Parameters; std::map< std::string, std::string > m_AcceptedFileExtensions; }; } // ecapseman #endif // __CPPLUGINS__PARAMETERS__H__ // eof - $RCSfile$