]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Parameters.h
3bf5d0140b8d068e36765a26208599dd0e284e6f
[cpPlugins.git] / lib / cpPlugins / Interface / Parameters.h
1 #ifndef __CPPLUGINS__INTERFACE__PARAMETERS__H__
2 #define __CPPLUGINS__INTERFACE__PARAMETERS__H__
3
4 #include <cpPlugins/Interface/cpPlugins_Interface_Export.h>
5
6 #include <map>
7 #include <ostream>
8 #include <sstream>
9 #include <string>
10 #include <vector>
11
12 #include <itkObject.h>
13 #include <itkObjectFactory.h>
14
15 // Some forward declarations
16 class TiXmlElement;
17
18 // -------------------------------------------------------------------------
19 #define cpPlugins_Parameters_Configure( Y )                     \
20   void ConfigureAs##Y( const std::string& name )                \
21   {                                                             \
22     this->m_Parameters[ name ] = TParameter( Self::Y, "" );     \
23     this->Modified( );                                          \
24   }                                                             \
25   bool Has##Y( const std::string& name ) const                  \
26   {                                                             \
27     auto i = this->m_Parameters.find( name );                   \
28     if( i != this->m_Parameters.end( ) )                        \
29       return( i->second.first == Self::Y );                     \
30     else                                                        \
31       return( false );                                          \
32   }
33
34 // -------------------------------------------------------------------------
35 #define cpPlugins_Parameters_GetSet( Y )                                \
36   T##Y Get##Y( const std::string& name ) const                          \
37   {                                                                     \
38     auto i = this->m_Parameters.find( name );                           \
39     if( i != this->m_Parameters.end( ) )                                \
40     {                                                                   \
41       if( i->second.first == Self::Y )                                  \
42       {                                                                 \
43         if( typeid( T##Y ) != typeid( std::string ) )                   \
44         {                                                               \
45           std::istringstream tok_str( i->second.second );               \
46           T##Y v;                                                       \
47           tok_str >> v;                                                 \
48           return( v );                                                  \
49         }                                                               \
50         else                                                            \
51         {                                                               \
52           const T##Y* ptr =                                             \
53             reinterpret_cast< const T##Y* >(                            \
54               &( i->second.second )                                     \
55               );                                                        \
56           return( *ptr );                                               \
57         }                                                               \
58       }                                                                 \
59     }                                                                   \
60     return( T##Y( 0 ) );                                                \
61   }                                                                     \
62   void Set##Y( const std::string& name, const T##Y& v )                 \
63   {                                                                     \
64     auto i = this->m_Parameters.find( name );                           \
65     if( i != this->m_Parameters.end( ) )                                \
66     {                                                                   \
67       if( i->second.first == Self::Y )                                  \
68       {                                                                 \
69         if( typeid( T##Y ) != typeid( std::string ) )                   \
70         {                                                               \
71           std::stringstream str;                                        \
72           str << v;                                                     \
73           if( i->second.second != str.str( ) )                          \
74           {                                                             \
75             i->second.second = str.str( );                              \
76             this->Modified( );                                          \
77           }                                                             \
78         }                                                               \
79         else                                                            \
80         {                                                               \
81           const std::string* str =                                      \
82             reinterpret_cast< const std::string* >( &v );               \
83           if( i->second.second != *str )                                \
84           {                                                             \
85             i->second.second = *str;                                    \
86             this->Modified( );                                          \
87           }                                                             \
88         }                                                               \
89       }                                                                 \
90     }                                                                   \
91   }
92
93 // -------------------------------------------------------------------------
94 #define cpPlugins_Parameters_GetSetList( Y )                            \
95   std::vector< T##Y > Get##Y##List( const std::string& name ) const     \
96   {                                                                     \
97     std::vector< T##Y > lst;                                            \
98     std::vector< std::string >* slst =                                  \
99       reinterpret_cast< std::vector< std::string >* >( &lst );          \
100     auto i = this->m_Parameters.find( name );                           \
101     if( i != this->m_Parameters.end( ) )                                \
102     {                                                                   \
103       if( i->second.first == Self::Y##List )                            \
104       {                                                                 \
105         std::istringstream str( i->second.second );                     \
106         std::string token;                                              \
107         while( std::getline( str, token, '#' ) )                        \
108         {                                                               \
109           if( typeid( T##Y ) != typeid( std::string ) )                 \
110           {                                                             \
111             std::istringstream tok_str( token );                        \
112             T##Y v;                                                     \
113             tok_str >> v;                                               \
114             lst.push_back( v );                                         \
115           }                                                             \
116           else                                                          \
117             slst->push_back( token );                                   \
118         }                                                               \
119       }                                                                 \
120     }                                                                   \
121     return( lst );                                                      \
122   }                                                                     \
123   void AddTo##Y##List( const std::string& name, const T##Y& v )         \
124   {                                                                     \
125     auto i = this->m_Parameters.find( name );                           \
126     if( i != this->m_Parameters.end( ) )                                \
127     {                                                                   \
128       if( i->second.first == Self::Y##List )                            \
129       {                                                                 \
130         std::stringstream str;                                          \
131         if( i->second.second != "" )                                    \
132           str << i->second.second << "#";                               \
133         str << v;                                                       \
134         i->second.second = str.str( );                                  \
135         this->Modified( );                                              \
136       }                                                                 \
137     }                                                                   \
138   }                                                                     \
139   void Clear##Y##List( const std::string& name )                        \
140   {                                                                     \
141     auto i = this->m_Parameters.find( name );                           \
142     if( i != this->m_Parameters.end( ) )                                \
143     {                                                                   \
144       if( i->second.first == Self::Y##List )                            \
145       {                                                                 \
146         if( i->second.second != "" )                                    \
147         {                                                               \
148           i->second.second = "";                                        \
149           this->Modified( );                                            \
150         }                                                               \
151       }                                                                 \
152     }                                                                   \
153   }
154
155 namespace cpPlugins
156 {
157   namespace Interface
158   {
159     // Some forward declarations
160     class ProcessObject;
161     class ParametersQtDialog;
162
163     /**
164      */
165     class cpPlugins_Interface_EXPORT Parameters
166       : public itk::Object
167     {
168       friend class ParametersQtDialog;
169
170     public:
171       typedef Parameters                      Self;
172       typedef itk::Object                     Superclass;
173       typedef itk::SmartPointer< Self >       Pointer;
174       typedef itk::SmartPointer< const Self > ConstPointer;
175
176       enum Type
177       {
178         String       , Bool             , Int              ,
179         Uint         , Real             , OpenFileName     ,
180         SaveFileName , PathName         , StringList       ,
181         BoolList     , IntList          , UintList         ,
182         RealList     , OpenFileNameList , SaveFileNameList ,
183         PathNameList , Choices          , NoType
184       };
185
186       typedef bool          TBool;
187       typedef long          TInt;
188       typedef unsigned long TUint;
189       typedef double        TReal;
190       typedef std::string   TString;
191       typedef std::string   TOpenFileName;
192       typedef std::string   TSaveFileName;
193       typedef std::string   TPathName;
194
195       typedef std::pair< Self::Type, std::string > TParameter;
196       typedef std::map< std::string, TParameter >  TParameters;
197
198     public:
199       itkNewMacro( Self );
200       itkTypeMacro( cpPlugins::Interface::Parameters, itk::Object );
201
202       cpPlugins_Parameters_Configure( String );
203       cpPlugins_Parameters_Configure( Bool );
204       cpPlugins_Parameters_Configure( Int );
205       cpPlugins_Parameters_Configure( Uint );
206       cpPlugins_Parameters_Configure( Real );
207       cpPlugins_Parameters_Configure( OpenFileName );
208       cpPlugins_Parameters_Configure( SaveFileName );
209       cpPlugins_Parameters_Configure( PathName );
210       cpPlugins_Parameters_Configure( StringList );
211       cpPlugins_Parameters_Configure( BoolList );
212       cpPlugins_Parameters_Configure( IntList );
213       cpPlugins_Parameters_Configure( UintList );
214       cpPlugins_Parameters_Configure( RealList );
215       cpPlugins_Parameters_Configure( OpenFileNameList );
216       cpPlugins_Parameters_Configure( SaveFileNameList );
217       cpPlugins_Parameters_Configure( PathNameList );
218       cpPlugins_Parameters_Configure( Choices );
219
220       cpPlugins_Parameters_GetSet( Bool );
221       cpPlugins_Parameters_GetSet( Int );
222       cpPlugins_Parameters_GetSet( Uint );
223       cpPlugins_Parameters_GetSet( Real );
224       cpPlugins_Parameters_GetSet( OpenFileName );
225       cpPlugins_Parameters_GetSet( SaveFileName );
226       cpPlugins_Parameters_GetSet( PathName );
227
228       cpPlugins_Parameters_GetSetList( String );
229       cpPlugins_Parameters_GetSetList( Bool );
230       cpPlugins_Parameters_GetSetList( Int );
231       cpPlugins_Parameters_GetSetList( Uint );
232       cpPlugins_Parameters_GetSetList( Real );
233       cpPlugins_Parameters_GetSetList( OpenFileName );
234       cpPlugins_Parameters_GetSetList( SaveFileName );
235       cpPlugins_Parameters_GetSetList( PathName );
236
237     public:
238       // To impact pipeline
239       virtual ProcessObject* GetProcessObject( );
240       virtual const ProcessObject* GetProcessObject( ) const;
241       virtual void SetProcessObject( ProcessObject* v );
242       virtual void Modified( ) const;
243
244       // Parameters container configuration
245       void Clear( );
246
247       // Get methods
248       void GetNames( std::vector< std::string >& container ) const;
249       Type GetType( const std::string& name ) const;
250       std::string GetTypeAsString( const std::string& name ) const;
251       static Type GetTypeFromString( const std::string& t );
252
253       // Base string methods
254       std::string GetString(
255         const std::string& name, bool force = true
256         ) const;
257       void SetString(
258         const std::string& name, const std::string& v, bool force = true
259         );
260
261       void ConfigureAsChoices(
262         const std::string& name, const std::vector< std::string >& choices
263         );
264       std::vector< std::string > GetChoices( const std::string& name ) const;
265       std::string GetSelectedChoice( const std::string& name ) const;
266       bool SetSelectedChoice(
267         const std::string& name, const std::string& choice
268         );
269
270       std::string GetAcceptedFileExtensions( const std::string& name ) const;
271       void SetAcceptedFileExtensions(
272         const std::string& name, const std::string& extensions
273         );
274
275       // XML "streaming"
276       bool ToXML( TiXmlElement* parent_elem ) const;
277       bool FromXML( const TiXmlElement* filter_elem );
278
279     protected:
280       Parameters( );
281       virtual ~Parameters( );
282       void PrintSelf( std::ostream& os, itk::Indent indent ) const;
283
284       TParameters& GetRawParameters( );
285       const TParameters& GetRawParameters( ) const;
286
287     private:
288       // Purposely not implemented
289       Parameters( const Self& other );
290       Self& operator=( const Self& other );
291
292     protected:
293       TParameters                          m_Parameters;
294       std::map< std::string, std::string > m_AcceptedFileExtensions;
295       ProcessObject*                       m_Process;
296     };
297
298   } // ecapseman
299
300 } // ecapseman
301
302 #endif // __CPPLUGINS__INTERFACE__PARAMETERS__H__
303
304 // eof - $RCSfile$