#include #include #include #include // ------------------------------------------------------------------------- cpPlugins::BaseObjects::Parameters:: Parameters( ) : m_ProcessObject( NULL ) { this->Clear( ); } // ------------------------------------------------------------------------- cpPlugins::BaseObjects::Parameters:: ~Parameters( ) { } // ------------------------------------------------------------------------- cpPlugins::BaseObjects::ProcessObject* cpPlugins::BaseObjects::Parameters:: GetProcessObject( ) { return( this->m_ProcessObject ); } // ------------------------------------------------------------------------- const cpPlugins::BaseObjects::ProcessObject* cpPlugins::BaseObjects::Parameters:: GetProcessObject( ) const { return( this->m_ProcessObject ); } // ------------------------------------------------------------------------- void cpPlugins::BaseObjects::Parameters:: SetProcessObject( cpPlugins::BaseObjects::ProcessObject* po ) { this->m_ProcessObject = po; } // ------------------------------------------------------------------------- void cpPlugins::BaseObjects::Parameters:: Modified( ) const { if( this->m_ProcessObject != NULL ) this->m_ProcessObject->Modified( ); } // ------------------------------------------------------------------------- void cpPlugins::BaseObjects::Parameters:: Clear( ) { this->m_Parameters.clear( ); this->Modified( ); } // ------------------------------------------------------------------------- void cpPlugins::BaseObjects::Parameters:: GetNames( std::vector< std::string >& container ) const { container.clear( ); TParameters::const_iterator i = this->m_Parameters.begin( ); for( ; i != this->m_Parameters.end( ); ++i ) container.push_back( i->first ); } // ------------------------------------------------------------------------- cpPlugins::BaseObjects::Parameters:: Type cpPlugins::BaseObjects::Parameters:: GetType( const std::string& name ) const { auto i = this->m_Parameters.find( name ); if( i != this->m_Parameters.end( ) ) return( i->second.first ); else return( Self::NoType ); } // ------------------------------------------------------------------------- #define cpPlugins_BaseObjects_Parameters_TypeAsString( Y ) \ if( i->second.first == Self::Y ) \ return( #Y ) std::string cpPlugins::BaseObjects::Parameters:: GetTypeAsString( const std::string& name ) const { auto i = this->m_Parameters.find( name ); cpPlugins_BaseObjects_Parameters_TypeAsString( String ); else cpPlugins_BaseObjects_Parameters_TypeAsString( Bool ); else cpPlugins_BaseObjects_Parameters_TypeAsString( Int ); else cpPlugins_BaseObjects_Parameters_TypeAsString( Uint ); else cpPlugins_BaseObjects_Parameters_TypeAsString( Real ); else cpPlugins_BaseObjects_Parameters_TypeAsString( OpenFileName ); else cpPlugins_BaseObjects_Parameters_TypeAsString( SaveFileName ); else cpPlugins_BaseObjects_Parameters_TypeAsString( PathName ); else cpPlugins_BaseObjects_Parameters_TypeAsString( StringList ); else cpPlugins_BaseObjects_Parameters_TypeAsString( BoolList ); else cpPlugins_BaseObjects_Parameters_TypeAsString( IntList ); else cpPlugins_BaseObjects_Parameters_TypeAsString( UintList ); else cpPlugins_BaseObjects_Parameters_TypeAsString( RealList ); else cpPlugins_BaseObjects_Parameters_TypeAsString( OpenFileNameList ); else cpPlugins_BaseObjects_Parameters_TypeAsString( SaveFileNameList ); else cpPlugins_BaseObjects_Parameters_TypeAsString( PathNameList ); else cpPlugins_BaseObjects_Parameters_TypeAsString( Choices ); else return( "NoType" ); } // ------------------------------------------------------------------------- #define cpPlugins_BaseObjects_Parameters_TypeFromString( Y, str ) \ if( str == std::string( #Y ) ) \ return( Self::Y ) cpPlugins::BaseObjects::Parameters:: Type cpPlugins::BaseObjects::Parameters:: GetTypeFromString( const std::string& t ) { cpPlugins_BaseObjects_Parameters_TypeFromString( String, t ); else cpPlugins_BaseObjects_Parameters_TypeFromString( Bool, t ); else cpPlugins_BaseObjects_Parameters_TypeFromString( Int, t ); else cpPlugins_BaseObjects_Parameters_TypeFromString( Uint, t ); else cpPlugins_BaseObjects_Parameters_TypeFromString( Real, t ); else cpPlugins_BaseObjects_Parameters_TypeFromString( OpenFileName, t ); else cpPlugins_BaseObjects_Parameters_TypeFromString( SaveFileName, t ); else cpPlugins_BaseObjects_Parameters_TypeFromString( PathName, t ); else cpPlugins_BaseObjects_Parameters_TypeFromString( StringList, t ); else cpPlugins_BaseObjects_Parameters_TypeFromString( BoolList, t ); else cpPlugins_BaseObjects_Parameters_TypeFromString( IntList, t ); else cpPlugins_BaseObjects_Parameters_TypeFromString( UintList, t ); else cpPlugins_BaseObjects_Parameters_TypeFromString( RealList, t ); else cpPlugins_BaseObjects_Parameters_TypeFromString( OpenFileNameList, t ); else cpPlugins_BaseObjects_Parameters_TypeFromString( SaveFileNameList, t ); else cpPlugins_BaseObjects_Parameters_TypeFromString( PathNameList, t ); else cpPlugins_BaseObjects_Parameters_TypeFromString( Choices, t ); else return( Self::NoType ); } // ------------------------------------------------------------------------- std::string cpPlugins::BaseObjects::Parameters:: GetString( const std::string& name, bool force ) const { auto i = this->m_Parameters.find( name ); if( i != this->m_Parameters.end( ) ) { if( i->second.first == Self::String || force ) return( i->second.second ); else return( "" ); } else return( "" ); } // ------------------------------------------------------------------------- void cpPlugins::BaseObjects::Parameters:: SetString( const std::string& name, const std::string& v, bool force ) { auto i = this->m_Parameters.find( name ); if( i != this->m_Parameters.end( ) ) { if( i->second.first == Self::String || force ) { if( i->second.second != v ) { i->second.second = v; this->Modified( ); } // fi } // fi } // fi } // ------------------------------------------------------------------------- void cpPlugins::BaseObjects::Parameters:: ConfigureAsChoices( const std::string& name, const std::vector< std::string >& choices ) { // It is invalid not to give choices when configuring if( choices.size( ) == 0 ) return; std::stringstream str_choices; str_choices << choices[ 0 ]; for( unsigned int i = 1; i < choices.size( ); ++i ) str_choices << "#" << choices[ i ]; str_choices << "@"; this->m_Parameters[ name ] = TParameter( Self::Choices, str_choices.str( ) ); this->Modified( ); } // ------------------------------------------------------------------------- void cpPlugins::BaseObjects::Parameters:: ConfigureAsRealTypesChoices( const std::string& name ) { std::vector< std::string > choices; #ifdef cpPlugins_CONFIG_REAL_TYPES_float choices.push_back( "float" ); #endif // cpPlugins_CONFIG_REAL_TYPES_float #ifdef cpPlugins_CONFIG_REAL_TYPES_double choices.push_back( "double" ); #endif // cpPlugins_CONFIG_REAL_TYPES_double this->ConfigureAsChoices( name, choices ); } // ------------------------------------------------------------------------- void cpPlugins::BaseObjects::Parameters:: ConfigureAsIntTypesChoices( const std::string& name ) { std::vector< std::string > choices; #ifdef cpPlugins_CONFIG_INTEGER_TYPES_char choices.push_back( "char" ); choices.push_back( "uchar" ); #endif // cpPlugins_CONFIG_INTEGER_TYPES_char #ifdef cpPlugins_CONFIG_INTEGER_TYPES_short choices.push_back( "short" ); choices.push_back( "ushort" ); #endif // cpPlugins_CONFIG_INTEGER_TYPES_short #ifdef cpPlugins_CONFIG_INTEGER_TYPES_int choices.push_back( "int" ); choices.push_back( "uint" ); #endif // cpPlugins_CONFIG_INTEGER_TYPES_int #ifdef cpPlugins_CONFIG_INTEGER_TYPES_long choices.push_back( "long" ); choices.push_back( "ulong" ); #endif // cpPlugins_CONFIG_INTEGER_TYPES_long this->ConfigureAsChoices( name, choices ); } // ------------------------------------------------------------------------- void cpPlugins::BaseObjects::Parameters:: ConfigureAsScalarTypesChoices( const std::string& name ) { std::vector< std::string > choices; #ifdef cpPlugins_CONFIG_INTEGER_TYPES_char choices.push_back( "char" ); choices.push_back( "uchar" ); #endif // cpPlugins_CONFIG_INTEGER_TYPES_char #ifdef cpPlugins_CONFIG_INTEGER_TYPES_short choices.push_back( "short" ); choices.push_back( "ushort" ); #endif // cpPlugins_CONFIG_INTEGER_TYPES_short #ifdef cpPlugins_CONFIG_INTEGER_TYPES_int choices.push_back( "int" ); choices.push_back( "uint" ); #endif // cpPlugins_CONFIG_INTEGER_TYPES_int #ifdef cpPlugins_CONFIG_INTEGER_TYPES_long choices.push_back( "long" ); choices.push_back( "ulong" ); #endif // cpPlugins_CONFIG_INTEGER_TYPES_long #ifdef cpPlugins_CONFIG_REAL_TYPES_float choices.push_back( "float" ); #endif // cpPlugins_CONFIG_REAL_TYPES_float #ifdef cpPlugins_CONFIG_REAL_TYPES_double choices.push_back( "double" ); #endif // cpPlugins_CONFIG_REAL_TYPES_double this->ConfigureAsChoices( name, choices ); } // ------------------------------------------------------------------------- std::vector< std::string > cpPlugins::BaseObjects::Parameters:: GetChoices( const std::string& name ) const { std::vector< std::string > choices; TParameters::const_iterator i = this->m_Parameters.find( name ); if( i != this->m_Parameters.end( ) ) { if( i->second.first == Self::Choices ) { std::istringstream str_choices( i->second.second ); std::string real_choices; std::getline( str_choices, real_choices, '@' ); std::istringstream str( real_choices ); std::string token; while( std::getline( str, token, '#' ) ) choices.push_back( token ); } // fi } // fi return( choices ); } // ------------------------------------------------------------------------- std::string cpPlugins::BaseObjects::Parameters:: GetSelectedChoice( const std::string& name ) const { auto i = this->m_Parameters.find( name ); if( i != this->m_Parameters.end( ) ) { if( i->second.first == Self::Choices ) { std::istringstream str_choices( i->second.second ); std::string real_choice; std::getline( str_choices, real_choice, '@' ); std::getline( str_choices, real_choice, '@' ); return( real_choice ); } else return( "" ); } else return( "" ); } // ------------------------------------------------------------------------- bool cpPlugins::BaseObjects::Parameters:: SetSelectedChoice( const std::string& name, const std::string& choice ) { auto i = this->m_Parameters.find( name ); if( i != this->m_Parameters.end( ) ) { if( i->second.first == Self::Choices ) { std::istringstream str_choices( i->second.second ); std::string choices; std::getline( str_choices, choices, '@' ); if( choices.find( choice ) != std::string::npos ) { std::stringstream new_choices; new_choices << choices << "@" << choice; i->second.second = new_choices.str( ); this->Modified( ); return( true ); } else return( false ); } else return( false ); } else return( false ); } // ------------------------------------------------------------------------- std::string cpPlugins::BaseObjects::Parameters:: GetAcceptedFileExtensions( const std::string& name ) const { auto i = this->m_AcceptedFileExtensions.find( name ); if( i != this->m_AcceptedFileExtensions.end( ) ) return( i->second ); else return( "" ); } // ------------------------------------------------------------------------- void cpPlugins::BaseObjects::Parameters:: SetAcceptedFileExtensions( const std::string& name, const std::string& extensions ) { auto i = this->m_Parameters.find( name ); if( i != this->m_Parameters.end( ) ) { bool is_valid = ( i->second.first == Self::OpenFileName ); is_valid |= ( i->second.first == Self::SaveFileName ); is_valid |= ( i->second.first == Self::OpenFileNameList ); is_valid |= ( i->second.first == Self::SaveFileNameList ); if( is_valid ) this->m_AcceptedFileExtensions[ name ] = extensions; } // fi } // ------------------------------------------------------------------------- bool cpPlugins::BaseObjects::Parameters:: ToXML( tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* parent_elem ) const { if( parent_elem == NULL ) return( false ); auto pIt = this->m_Parameters.begin( ); for( ; pIt != this->m_Parameters.end( ); ++pIt ) { tinyxml2::XMLElement* p = doc->NewElement( "Parameter" ); p->SetAttribute( "Name", pIt->first.c_str( ) ); p->SetAttribute( "Value", pIt->second.second.c_str( ) ); p->SetAttribute( "Type", this->GetTypeAsString( pIt->first ).c_str( ) ); parent_elem->InsertEndChild( p ); } // rof return( true ); } // ------------------------------------------------------------------------- bool cpPlugins::BaseObjects::Parameters:: FromXML( const tinyxml2::XMLElement* filter_elem ) { const tinyxml2::XMLElement* param = filter_elem->FirstChildElement( "Parameter" ); bool ret = false; while( param != NULL ) { const char* param_name = param->Attribute( "Name" ); const char* param_type = param->Attribute( "Type" ); if( param_name != NULL && param_type != NULL ) { TParameter value; value.second = param->Attribute( "Value" ); value.first = Self::GetTypeFromString( param_type ); this->m_Parameters[ param_name ] = value; } // fi param = param->NextSiblingElement( "Parameter" ); ret = true; } // elihw this->Modified( ); return( ret ); } // ------------------------------------------------------------------------- cpPlugins::BaseObjects::Parameters:: TParameters& cpPlugins::BaseObjects::Parameters:: GetRawParameters( ) { return( this->m_Parameters ); } // ------------------------------------------------------------------------- const cpPlugins::BaseObjects::Parameters:: TParameters& cpPlugins::BaseObjects::Parameters:: GetRawParameters( ) const { return( this->m_Parameters ); } // ------------------------------------------------------------------------- #define cpPlugins_BaseObjects_Parameters_Configure_Code( Y ) \ void cpPlugins::BaseObjects::Parameters:: \ ConfigureAs##Y( const std::string& name, const T##Y& init ) \ { \ this->_Configure< Y >( name ); \ this->Set##Y( name, init ); \ } \ bool cpPlugins::BaseObjects::Parameters:: \ Has##Y( const std::string& name ) const \ { return( this->_Has< Y >( name ) ); } // ------------------------------------------------------------------------- #define cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Y ) \ void cpPlugins::BaseObjects::Parameters:: \ ConfigureAs##Y##List( const std::string& name ) \ { this->_Configure< Y##List >( name ); } \ bool cpPlugins::BaseObjects::Parameters:: \ Has##Y##List( const std::string& name ) const \ { return( this->_Has< Y##List >( name ) ); } // ------------------------------------------------------------------------- #define cpPlugins_BaseObjects_Parameters_GetSet_Code( Y ) \ cpPlugins::BaseObjects::Parameters::T##Y \ cpPlugins::BaseObjects::Parameters:: \ Get##Y( const std::string& name ) const \ { return( this->_Get< T##Y, Y >( name ) ); } \ void cpPlugins::BaseObjects::Parameters::Set##Y( \ const std::string& name, const T##Y& v \ ) \ { this->_Set< T##Y, Y >( name, v ); } // ------------------------------------------------------------------------- #define cpPlugins_BaseObjects_Parameters_GetSetList_Code( Y ) \ std::vector< cpPlugins::BaseObjects::Parameters::T##Y > \ cpPlugins::BaseObjects::Parameters:: \ Get##Y##List( const std::string& name ) const \ { return( this->_GetList< T##Y, Y##List >( name ) ); } \ void cpPlugins::BaseObjects::Parameters::AddTo##Y##List( \ const std::string& name, \ const cpPlugins::BaseObjects::Parameters::T##Y& v \ ) \ { this->_AddToList< T##Y, Y##List >( name, v ); } \ void cpPlugins::BaseObjects::Parameters:: \ Clear##Y##List( const std::string& name ) \ { this->_ClearList< Y##List >( name ); } // ------------------------------------------------------------------------- cpPlugins_BaseObjects_Parameters_Configure_Code( String ); cpPlugins_BaseObjects_Parameters_Configure_Code( Bool ); cpPlugins_BaseObjects_Parameters_Configure_Code( Int ); cpPlugins_BaseObjects_Parameters_Configure_Code( Uint ); cpPlugins_BaseObjects_Parameters_Configure_Code( Real ); cpPlugins_BaseObjects_Parameters_Configure_Code( OpenFileName ); cpPlugins_BaseObjects_Parameters_Configure_Code( SaveFileName ); cpPlugins_BaseObjects_Parameters_Configure_Code( PathName ); cpPlugins_BaseObjects_Parameters_ConfigureList_Code( String ); cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Bool ); cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Int ); cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Uint ); cpPlugins_BaseObjects_Parameters_ConfigureList_Code( Real ); cpPlugins_BaseObjects_Parameters_ConfigureList_Code( OpenFileName ); cpPlugins_BaseObjects_Parameters_ConfigureList_Code( SaveFileName ); cpPlugins_BaseObjects_Parameters_ConfigureList_Code( PathName ); cpPlugins_BaseObjects_Parameters_GetSet_Code( Bool ); cpPlugins_BaseObjects_Parameters_GetSet_Code( Int ); cpPlugins_BaseObjects_Parameters_GetSet_Code( Uint ); cpPlugins_BaseObjects_Parameters_GetSet_Code( Real ); cpPlugins_BaseObjects_Parameters_GetSet_Code( OpenFileName ); cpPlugins_BaseObjects_Parameters_GetSet_Code( SaveFileName ); cpPlugins_BaseObjects_Parameters_GetSet_Code( PathName ); cpPlugins_BaseObjects_Parameters_GetSetList_Code( String ); cpPlugins_BaseObjects_Parameters_GetSetList_Code( Bool ); cpPlugins_BaseObjects_Parameters_GetSetList_Code( Int ); cpPlugins_BaseObjects_Parameters_GetSetList_Code( Uint ); cpPlugins_BaseObjects_Parameters_GetSetList_Code( Real ); cpPlugins_BaseObjects_Parameters_GetSetList_Code( PathName ); // ------------------------------------------------------------------------- std::vector< cpPlugins::BaseObjects::Parameters::TOpenFileName > cpPlugins::BaseObjects::Parameters:: GetOpenFileNameList( const std::string& name ) const { return( this->_GetList< TOpenFileName, OpenFileNameList >( name ) ); } // ------------------------------------------------------------------------- void cpPlugins::BaseObjects::Parameters:: AddToOpenFileNameList( const std::string& name, const cpPlugins::BaseObjects::Parameters::TOpenFileName& v ) { auto i = this->m_Parameters.find( name ); if( i != this->m_Parameters.end( ) ) { if( i->second.first == OpenFileNameList ) { auto pos = v.find_last_of( "/\\" ); if( i->second.second == "" ) i->second.second = v.substr( 0, pos ); i->second.second += std::string( "#" ); i->second.second += v.substr( pos + 1 ); this->Modified( ); } // fi } // fi } // ------------------------------------------------------------------------- void cpPlugins::BaseObjects::Parameters:: ClearOpenFileNameList( const std::string& name ) { this->_ClearList< OpenFileNameList >( name ); } // ------------------------------------------------------------------------- std::vector< cpPlugins::BaseObjects::Parameters::TSaveFileName > cpPlugins::BaseObjects::Parameters:: GetSaveFileNameList( const std::string& name ) const { return( this->_GetList< TSaveFileName, SaveFileNameList >( name ) ); } // ------------------------------------------------------------------------- void cpPlugins::BaseObjects::Parameters:: AddToSaveFileNameList( const std::string& name, const cpPlugins::BaseObjects::Parameters::TSaveFileName& v ) { auto i = this->m_Parameters.find( name ); if( i != this->m_Parameters.end( ) ) { if( i->second.first == SaveFileNameList ) { auto pos = v.find_last_of( "/\\" ); if( i->second.second == "" ) i->second.second = v.substr( 0, pos ); i->second.second += std::string( "#" ); i->second.second += v.substr( pos + 1 ); this->Modified( ); } // fi } // fi } // ------------------------------------------------------------------------- void cpPlugins::BaseObjects::Parameters:: ClearSaveFileNameList( const std::string& name ) { this->_ClearList< SaveFileNameList >( name ); } // ------------------------------------------------------------------------- template< unsigned int _Enum > void cpPlugins::BaseObjects::Parameters:: _Configure( const std::string& name ) { this->m_Parameters[ name ] = TParameter( ( Self::Type )( _Enum ), "" ); this->Modified( ); } // ------------------------------------------------------------------------- template< unsigned int _Enum > bool cpPlugins::BaseObjects::Parameters:: _Has( const std::string& name ) const { auto i = this->m_Parameters.find( name ); if( i != this->m_Parameters.end( ) ) return( i->second.first == ( Self::Type )( _Enum ) ); else return( false ); } // ------------------------------------------------------------------------- template< class _Type, unsigned int _Enum > _Type cpPlugins::BaseObjects::Parameters:: _Get( const std::string& name ) const { auto i = this->m_Parameters.find( name ); if( i != this->m_Parameters.end( ) ) { if( i->second.first == ( Self::Type )( _Enum ) ) { if( typeid( _Type ) != typeid( std::string ) ) { std::istringstream tok_str( i->second.second ); _Type v; tok_str >> v; return( v ); } else { const _Type* ptr = reinterpret_cast< const _Type* >( &( i->second.second ) ); return( *ptr ); } // fi } // fi } // fi return( _Type( 0 ) ); } // ------------------------------------------------------------------------- template< class _Type, unsigned int _Enum > void cpPlugins::BaseObjects::Parameters:: _Set( const std::string& name, const _Type& v ) { auto i = this->m_Parameters.find( name ); if( i != this->m_Parameters.end( ) ) { if( i->second.first == ( Self::Type )( _Enum ) ) { if( typeid( _Type ) != typeid( std::string ) ) { std::stringstream str; str << v; if( i->second.second != str.str( ) ) { i->second.second = str.str( ); this->Modified( ); } // fi } else { const std::string* str = reinterpret_cast< const std::string* >( &v ); if( i->second.second != *str ) { i->second.second = *str; this->Modified( ); } // fi } // fi } // fi } // fi } // ------------------------------------------------------------------------- template< class _Type, unsigned int _Enum > std::vector< _Type > cpPlugins::BaseObjects::Parameters:: _GetList( const std::string& name ) const { std::vector< _Type > 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::Type )( _Enum ) ) { std::vector< std::string > tokens; cpPlugins::Tokenize( tokens, i->second.second, "#" ); for( auto t = tokens.begin( ); t != tokens.end( ); ++t ) { if( typeid( _Type ) != typeid( std::string ) ) { std::istringstream tok_str( *t ); _Type v; tok_str >> v; lst.push_back( v ); } else slst->push_back( *t ); } // rof } // fi } // fi return( lst ); } // ------------------------------------------------------------------------- template< class _Type, unsigned int _Enum > void cpPlugins::BaseObjects::Parameters:: _AddToList( const std::string& name, const _Type& v ) { auto i = this->m_Parameters.find( name ); if( i != this->m_Parameters.end( ) ) { if( i->second.first == ( Self::Type )( _Enum ) ) { std::stringstream str; if( i->second.second != "" ) str << i->second.second << "#"; str << v; i->second.second = str.str( ); this->Modified( ); } // fi } // fi } // ------------------------------------------------------------------------- template< unsigned int _Enum > void cpPlugins::BaseObjects::Parameters:: _ClearList( const std::string& name ) { auto i = this->m_Parameters.find( name ); if( i != this->m_Parameters.end( ) ) { if( i->second.first == ( Self::Type )( _Enum ) ) { if( i->second.second != "" ) { i->second.second = ""; this->Modified( ); } // fi } // fi } // fi } // eof - $RCSfile$