1 #ifndef __CPPLUGINS__PARAMETERS__H__
2 #define __CPPLUGINS__PARAMETERS__H__
4 #include <cpPlugins/Config.h>
12 #include <itkTimeStamp.h>
14 // Some forward declarations
21 // -------------------------------------------------------------------------
22 #define cpPlugins_Parameters_Configure( Y ) \
23 void ConfigureAs##Y( const std::string& name ) \
25 this->m_Parameters[ name ] = TParameter( Self::Y, "" ); \
28 bool Has##Y( const std::string& name ) const \
30 auto i = this->m_Parameters.find( name ); \
31 if( i != this->m_Parameters.end( ) ) \
32 return( i->second.first == Self::Y ); \
37 // -------------------------------------------------------------------------
38 #define cpPlugins_Parameters_GetSet( Y ) \
39 T##Y Get##Y( const std::string& name ) const \
41 auto i = this->m_Parameters.find( name ); \
42 if( i != this->m_Parameters.end( ) ) \
44 if( i->second.first == Self::Y ) \
46 if( typeid( T##Y ) != typeid( std::string ) ) \
48 std::istringstream tok_str( i->second.second ); \
56 reinterpret_cast< const T##Y* >( \
57 &( i->second.second ) \
63 return( T##Y( 0 ) ); \
65 void Set##Y( const std::string& name, const T##Y& v ) \
67 auto i = this->m_Parameters.find( name ); \
68 if( i != this->m_Parameters.end( ) ) \
70 if( i->second.first == Self::Y ) \
72 if( typeid( T##Y ) != typeid( std::string ) ) \
74 std::stringstream str; \
76 if( i->second.second != str.str( ) ) \
78 i->second.second = str.str( ); \
84 const std::string* str = \
85 reinterpret_cast< const std::string* >( &v ); \
86 if( i->second.second != *str ) \
88 i->second.second = *str; \
96 // -------------------------------------------------------------------------
97 #define cpPlugins_Parameters_GetSetList( Y ) \
98 std::vector< T##Y > Get##Y##List( const std::string& name ) const \
100 std::vector< T##Y > lst; \
101 std::vector< std::string >* slst = \
102 reinterpret_cast< std::vector< std::string >* >( &lst ); \
103 auto i = this->m_Parameters.find( name ); \
104 if( i != this->m_Parameters.end( ) ) \
106 if( i->second.first == Self::Y##List ) \
108 std::istringstream str( i->second.second ); \
110 while( std::getline( str, token, '#' ) ) \
112 if( typeid( T##Y ) != typeid( std::string ) ) \
114 std::istringstream tok_str( token ); \
117 lst.push_back( v ); \
120 slst->push_back( token ); \
126 void AddTo##Y##List( const std::string& name, const T##Y& v ) \
128 auto i = this->m_Parameters.find( name ); \
129 if( i != this->m_Parameters.end( ) ) \
131 if( i->second.first == Self::Y##List ) \
133 std::stringstream str; \
134 if( i->second.second != "" ) \
135 str << i->second.second << "#"; \
137 i->second.second = str.str( ); \
142 void Clear##Y##List( const std::string& name ) \
144 auto i = this->m_Parameters.find( name ); \
145 if( i != this->m_Parameters.end( ) ) \
147 if( i->second.first == Self::Y##List ) \
149 if( i->second.second != "" ) \
151 i->second.second = ""; \
160 // Forward declaration to improve Qt dialog execution
161 class ParametersQtDialog;
165 class cpPlugins_EXPORT Parameters
167 // Frienship with forward declaration to improve Qt dialog execution
168 friend class ParametersQtDialog;
169 friend std::ostream& operator<<( std::ostream& o, const Parameters& p )
172 auto i = p.m_Parameters.begin( );
173 i != p.m_Parameters.end( );
176 o << i->first << ": ("
177 << i->second.first << " | "
178 << i->second.second << ")"
184 typedef Parameters Self;
188 String , Bool , Int ,
189 Uint , Real , OpenFileName ,
190 SaveFileName , PathName , StringList ,
191 BoolList , IntList , UintList ,
192 RealList , OpenFileNameList , SaveFileNameList ,
193 PathNameList , Choices , NoType
198 typedef unsigned long TUint;
199 typedef double TReal;
200 typedef std::string TString;
201 typedef std::string TOpenFileName;
202 typedef std::string TSaveFileName;
203 typedef std::string TPathName;
205 typedef std::pair< Self::Type, std::string > TParameter;
206 typedef std::map< std::string, TParameter > TParameters;
209 cpPlugins_Parameters_Configure( String );
210 cpPlugins_Parameters_Configure( Bool );
211 cpPlugins_Parameters_Configure( Int );
212 cpPlugins_Parameters_Configure( Uint );
213 cpPlugins_Parameters_Configure( Real );
214 cpPlugins_Parameters_Configure( OpenFileName );
215 cpPlugins_Parameters_Configure( SaveFileName );
216 cpPlugins_Parameters_Configure( PathName );
217 cpPlugins_Parameters_Configure( StringList );
218 cpPlugins_Parameters_Configure( BoolList );
219 cpPlugins_Parameters_Configure( IntList );
220 cpPlugins_Parameters_Configure( UintList );
221 cpPlugins_Parameters_Configure( RealList );
222 cpPlugins_Parameters_Configure( OpenFileNameList );
223 cpPlugins_Parameters_Configure( SaveFileNameList );
224 cpPlugins_Parameters_Configure( PathNameList );
225 cpPlugins_Parameters_Configure( Choices );
227 cpPlugins_Parameters_GetSet( Bool );
228 cpPlugins_Parameters_GetSet( Int );
229 cpPlugins_Parameters_GetSet( Uint );
230 cpPlugins_Parameters_GetSet( Real );
231 cpPlugins_Parameters_GetSet( OpenFileName );
232 cpPlugins_Parameters_GetSet( SaveFileName );
233 cpPlugins_Parameters_GetSet( PathName );
235 cpPlugins_Parameters_GetSetList( String );
236 cpPlugins_Parameters_GetSetList( Bool );
237 cpPlugins_Parameters_GetSetList( Int );
238 cpPlugins_Parameters_GetSetList( Uint );
239 cpPlugins_Parameters_GetSetList( Real );
240 cpPlugins_Parameters_GetSetList( OpenFileName );
241 cpPlugins_Parameters_GetSetList( SaveFileName );
242 cpPlugins_Parameters_GetSetList( PathName );
246 virtual ~Parameters( );
248 virtual void Modified( ) const;
249 virtual itk::ModifiedTimeType GetMTime( ) const;
251 // Parameters container configuration
255 void GetNames( std::vector< std::string >& container ) const;
256 Type GetType( const std::string& name ) const;
257 std::string GetTypeAsString( const std::string& name ) const;
258 static Type GetTypeFromString( const std::string& t );
260 // Base string methods
261 std::string GetString(
262 const std::string& name, bool force = true
265 const std::string& name, const std::string& v, bool force = true
268 void ConfigureAsChoices(
269 const std::string& name, const std::vector< std::string >& choices
271 std::vector< std::string > GetChoices( const std::string& name ) const;
272 std::string GetSelectedChoice( const std::string& name ) const;
273 bool SetSelectedChoice(
274 const std::string& name, const std::string& choice
277 std::string GetAcceptedFileExtensions( const std::string& name ) const;
278 void SetAcceptedFileExtensions(
279 const std::string& name, const std::string& extensions
284 tinyxml2::XMLDocument* doc,
285 tinyxml2::XMLElement* parent_elem
287 bool FromXML( const tinyxml2::XMLElement* filter_elem );
290 TParameters& GetRawParameters( );
291 const TParameters& GetRawParameters( ) const;
294 // Purposely not implemented
295 Parameters( const Self& other );
296 Self& operator=( const Self& other );
299 mutable itk::TimeStamp m_TimeStamp;
300 TParameters m_Parameters;
301 std::map< std::string, std::string > m_AcceptedFileExtensions;
306 #endif // __CPPLUGINS__PARAMETERS__H__