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