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