]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Parameters.h
...
[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           i->second.second = str.str( );                                \
74         }                                                               \
75         else                                                            \
76           i->second.second =                                            \
77             *( reinterpret_cast< const std::string* >( &v ) );          \
78         this->Modified( );                                              \
79       }                                                                 \
80     }                                                                   \
81   }
82
83 // -------------------------------------------------------------------------
84 #define cpPlugins_Parameters_GetSetList( Y )                            \
85   std::vector< T##Y > Get##Y##List( const std::string& name ) const     \
86   {                                                                     \
87     std::vector< T##Y > lst;                                            \
88     std::vector< std::string >* slst =                                  \
89       reinterpret_cast< std::vector< std::string >* >( &lst );          \
90     auto i = this->m_Parameters.find( name );                           \
91     if( i != this->m_Parameters.end( ) )                                \
92     {                                                                   \
93       if( i->second.first == Self::Y##List )                            \
94       {                                                                 \
95         std::istringstream str( i->second.second );                     \
96         std::string token;                                              \
97         while( std::getline( str, token, '#' ) )                        \
98         {                                                               \
99           if( typeid( T##Y ) != typeid( std::string ) )                 \
100           {                                                             \
101             std::istringstream tok_str( token );                        \
102             T##Y v;                                                     \
103             tok_str >> v;                                               \
104             lst.push_back( v );                                         \
105           }                                                             \
106           else                                                          \
107             slst->push_back( token );                                   \
108         }                                                               \
109       }                                                                 \
110     }                                                                   \
111     return( lst );                                                      \
112   }                                                                     \
113   void AddTo##Y##List( const std::string& name, const T##Y& v )         \
114   {                                                                     \
115     auto i = this->m_Parameters.find( name );                           \
116     if( i != this->m_Parameters.end( ) )                                \
117     {                                                                   \
118       if( i->second.first == Self::Y##List )                            \
119       {                                                                 \
120         std::stringstream str;                                          \
121         if( i->second.second != "" )                                    \
122           str << i->second.second << "#";                               \
123         str << v;                                                       \
124         i->second.second = str.str( );                                  \
125       }                                                                 \
126     }                                                                   \
127   }                                                                     \
128   void Clear##Y##List( const std::string& name )                        \
129   {                                                                     \
130     auto i = this->m_Parameters.find( name );                           \
131     if( i != this->m_Parameters.end( ) )                                \
132     {                                                                   \
133       if( i->second.first == Self::Y##List )                            \
134         i->second.second = "";                                          \
135     }                                                                   \
136   }
137
138 namespace cpPlugins
139 {
140   namespace Interface
141   {
142     // Some forward declarations
143     class ProcessObject;
144     class ParametersQtDialog;
145
146     /**
147      */
148     class cpPlugins_Interface_EXPORT Parameters
149       : public itk::Object
150     {
151       friend class ParametersQtDialog;
152
153     public:
154       typedef Parameters                      Self;
155       typedef itk::Object                     Superclass;
156       typedef itk::SmartPointer< Self >       Pointer;
157       typedef itk::SmartPointer< const Self > ConstPointer;
158
159       enum Type
160       {
161         String       , Bool             , Int              ,
162         Uint         , Real             , Index            ,
163         Point        , Vector           , OpenFileName     ,
164         SaveFileName , PathName         , StringList       ,
165         BoolList     , IntList          , UintList         ,
166         RealList     , IndexList        , PointList        ,
167         VectorList   , OpenFileNameList , SaveFileNameList ,
168         PathNameList , Choices          , NoType
169       };
170
171       typedef bool          TBool;
172       typedef long          TInt;
173       typedef unsigned long TUint;
174       typedef double        TReal;
175       typedef std::string   TString;
176       typedef std::string   TOpenFileName;
177       typedef std::string   TSaveFileName;
178       typedef std::string   TPathName;
179
180       typedef std::pair< Self::Type, std::string > TParameter;
181       typedef std::map< std::string, TParameter >  TParameters;
182
183     public:
184       itkNewMacro( Self );
185       itkTypeMacro( cpPlugins::Interface::Parameters, itk::Object );
186
187       cpPlugins_Parameters_Configure( String );
188       cpPlugins_Parameters_Configure( Bool );
189       cpPlugins_Parameters_Configure( Int );
190       cpPlugins_Parameters_Configure( Uint );
191       cpPlugins_Parameters_Configure( Real );
192       cpPlugins_Parameters_Configure( Index );
193       cpPlugins_Parameters_Configure( Point );
194       cpPlugins_Parameters_Configure( Vector );
195       cpPlugins_Parameters_Configure( OpenFileName );
196       cpPlugins_Parameters_Configure( SaveFileName );
197       cpPlugins_Parameters_Configure( PathName );
198       cpPlugins_Parameters_Configure( StringList );
199       cpPlugins_Parameters_Configure( BoolList );
200       cpPlugins_Parameters_Configure( IntList );
201       cpPlugins_Parameters_Configure( UintList );
202       cpPlugins_Parameters_Configure( RealList );
203       cpPlugins_Parameters_Configure( IndexList );
204       cpPlugins_Parameters_Configure( PointList );
205       cpPlugins_Parameters_Configure( VectorList );
206       cpPlugins_Parameters_Configure( OpenFileNameList );
207       cpPlugins_Parameters_Configure( SaveFileNameList );
208       cpPlugins_Parameters_Configure( PathNameList );
209       cpPlugins_Parameters_Configure( Choices );
210
211       cpPlugins_Parameters_GetSet( Bool );
212       cpPlugins_Parameters_GetSet( Int );
213       cpPlugins_Parameters_GetSet( Uint );
214       cpPlugins_Parameters_GetSet( Real );
215       cpPlugins_Parameters_GetSet( OpenFileName );
216       cpPlugins_Parameters_GetSet( SaveFileName );
217       cpPlugins_Parameters_GetSet( PathName );
218
219       cpPlugins_Parameters_GetSetList( String );
220       cpPlugins_Parameters_GetSetList( Bool );
221       cpPlugins_Parameters_GetSetList( Int );
222       cpPlugins_Parameters_GetSetList( Uint );
223       cpPlugins_Parameters_GetSetList( Real );
224       cpPlugins_Parameters_GetSetList( OpenFileName );
225       cpPlugins_Parameters_GetSetList( SaveFileName );
226       cpPlugins_Parameters_GetSetList( PathName );
227
228     public:
229       // To impact pipeline
230       virtual ProcessObject* GetProcessObject( );
231       virtual const ProcessObject* GetProcessObject( ) const;
232       virtual void SetProcessObject( ProcessObject* v );
233       virtual void Modified( ) const;
234
235       // Parameters container configuration
236       void Clear( );
237
238       // Get methods
239       void GetNames( std::vector< std::string >& container ) const;
240       Type GetType( const std::string& name ) const;
241       std::string GetTypeAsString( const std::string& name ) const;
242       static Type GetTypeFromString( const std::string& t );
243
244       // Base string methods
245       std::string GetString(
246         const std::string& name, bool force = true
247         ) const;
248       void SetString(
249         const std::string& name, const std::string& v, bool force = true
250         );
251
252       void ConfigureAsChoices(
253         const std::string& name, const std::vector< std::string >& choices
254         );
255       std::vector< std::string > GetChoices( const std::string& name ) const;
256       std::string GetSelectedChoice( const std::string& name ) const;
257       bool SetSelectedChoice(
258         const std::string& name, const std::string& choice
259         );
260
261       // Some templated methods
262       template< class I >
263         I GetIndex(
264           const std::string& name, const unsigned int& dim
265           ) const;
266       template< class P >
267         P GetPoint(
268           const std::string& name, const unsigned int& dim
269           ) const;
270       template< class V >
271         V GetVector(
272           const std::string& name, const unsigned int& dim
273           ) const;
274
275       template< class I >
276         void SetIndex(
277           const std::string& name, const unsigned int& dim, const I& v
278           );
279       template< class P >
280         void SetPoint(
281           const std::string& name, const unsigned int& dim, const P& v
282           );
283       template< class V >
284         void SetVector(
285           const std::string& name, const unsigned int& dim, const V& v
286           );
287
288       template< class I >
289         std::vector< I > GetIndexList(
290           const std::string& name, const unsigned int& dim
291           ) const;
292       template< class P >
293         std::vector< P > GetPointList(
294           const std::string& name, const unsigned int& dim
295           ) const;
296       template< class V >
297         std::vector< V > GetVectorList(
298           const std::string& name, const unsigned int& dim
299           ) const;
300
301       template< class I >
302         void AddToIndexList(
303           const std::string& name, const unsigned int& dim, const I& v
304           );
305       template< class P >
306         void AddToPointList(
307           const std::string& name, const unsigned int& dim, const P& v
308           );
309       template< class P >
310         void AddToVectorList(
311           const std::string& name, const unsigned int& dim, const P& v
312           );
313
314       void ClearIndexList( const std::string& name );
315       void ClearPointList( const std::string& name );
316       void ClearVectorList( const std::string& name );
317
318       // XML "streaming"
319       bool ToXML( TiXmlElement* parent_elem ) const;
320       bool FromXML( const TiXmlElement* filter_elem );
321
322     protected:
323       Parameters( );
324       virtual ~Parameters( );
325       void PrintSelf( std::ostream& os, itk::Indent indent ) const;
326
327       TParameters& GetRawParameters( );
328       const TParameters& GetRawParameters( ) const;
329
330     private:
331       // Purposely not implemented
332       Parameters( const Self& other );
333       Self& operator=( const Self& other );
334
335     protected:
336       TParameters m_Parameters;
337       ProcessObject* m_Process;
338     };
339
340   } // ecapseman
341
342 } // ecapseman
343
344 #include <cpPlugins/Interface/Parameters.hxx>
345
346 #endif // __CPPLUGINS__INTERFACE__PARAMETERS__H__
347
348 // eof - $RCSfile$