]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Parameters.h
First dump for version 0.1.0
[cpPlugins.git] / lib / cpPlugins / Parameters.h
1 #ifndef __CPPLUGINS__PARAMETERS__H__
2 #define __CPPLUGINS__PARAMETERS__H__
3
4 #include <cpPlugins/Config.h>
5
6 #include <typeinfo>
7 #include <map>
8 #include <ostream>
9 #include <sstream>
10 #include <string>
11 #include <vector>
12 #include <itkTimeStamp.h>
13
14 // Some forward declarations
15 namespace tinyxml2
16 {
17   class XMLElement;
18   class XMLDocument;
19 }
20
21 // -------------------------------------------------------------------------
22 #define cpPlugins_Parameters_Configure( Y )                     \
23   void ConfigureAs##Y( const std::string& name )                \
24   {                                                             \
25     this->m_Parameters[ name ] = TParameter( Self::Y, "" );     \
26     this->Modified( );                                          \
27   }                                                             \
28   bool Has##Y( const std::string& name ) const                  \
29   {                                                             \
30     auto i = this->m_Parameters.find( name );                   \
31     if( i != this->m_Parameters.end( ) )                        \
32       return( i->second.first == Self::Y );                     \
33     else                                                        \
34       return( false );                                          \
35   }
36
37 // -------------------------------------------------------------------------
38 #define cpPlugins_Parameters_GetSet( Y )                        \
39   T##Y Get##Y( const std::string& name ) const                  \
40   {                                                             \
41     auto i = this->m_Parameters.find( name );                   \
42     if( i != this->m_Parameters.end( ) )                        \
43     {                                                           \
44       if( i->second.first == Self::Y )                          \
45       {                                                         \
46         if( typeid( T##Y ) != typeid( std::string ) )           \
47         {                                                       \
48           std::istringstream tok_str( i->second.second );       \
49           T##Y v;                                               \
50           tok_str >> v;                                         \
51           return( v );                                          \
52         }                                                       \
53         else                                                    \
54         {                                                       \
55           const T##Y* ptr =                                     \
56             reinterpret_cast< const T##Y* >(                    \
57               &( i->second.second )                             \
58               );                                                \
59           return( *ptr );                                       \
60         }                                                       \
61       }                                                         \
62     }                                                           \
63     return( T##Y( 0 ) );                                        \
64   }                                                             \
65   void Set##Y( const std::string& name, const T##Y& v )         \
66   {                                                             \
67     auto i = this->m_Parameters.find( name );                   \
68     if( i != this->m_Parameters.end( ) )                        \
69     {                                                           \
70       if( i->second.first == Self::Y )                          \
71       {                                                         \
72         if( typeid( T##Y ) != typeid( std::string ) )           \
73         {                                                       \
74           std::stringstream str;                                \
75           str << v;                                             \
76           if( i->second.second != str.str( ) )                  \
77           {                                                     \
78             i->second.second = str.str( );                      \
79             this->Modified( );                                  \
80           }                                                     \
81         }                                                       \
82         else                                                    \
83         {                                                       \
84           const std::string* str =                              \
85             reinterpret_cast< const std::string* >( &v );       \
86           if( i->second.second != *str )                        \
87           {                                                     \
88             i->second.second = *str;                            \
89             this->Modified( );                                  \
90           }                                                     \
91         }                                                       \
92       }                                                         \
93     }                                                           \
94   }
95
96 // -------------------------------------------------------------------------
97 #define cpPlugins_Parameters_GetSetList( Y )                            \
98   std::vector< T##Y > Get##Y##List( const std::string& name ) const     \
99   {                                                                     \
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( ) )                                \
105     {                                                                   \
106       if( i->second.first == Self::Y##List )                            \
107       {                                                                 \
108         std::istringstream str( i->second.second );                     \
109         std::string token;                                              \
110         while( std::getline( str, token, '#' ) )                        \
111         {                                                               \
112           if( typeid( T##Y ) != typeid( std::string ) )                 \
113           {                                                             \
114             std::istringstream tok_str( token );                        \
115             T##Y v;                                                     \
116             tok_str >> v;                                               \
117             lst.push_back( v );                                         \
118           }                                                             \
119           else                                                          \
120             slst->push_back( token );                                   \
121         }                                                               \
122       }                                                                 \
123     }                                                                   \
124     return( lst );                                                      \
125   }                                                                     \
126   void AddTo##Y##List( const std::string& name, const T##Y& v )         \
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       {                                                                 \
133         std::stringstream str;                                          \
134         if( i->second.second != "" )                                    \
135           str << i->second.second << "#";                               \
136         str << v;                                                       \
137         i->second.second = str.str( );                                  \
138         this->Modified( );                                              \
139       }                                                                 \
140     }                                                                   \
141   }                                                                     \
142   void Clear##Y##List( const std::string& name )                        \
143   {                                                                     \
144     auto i = this->m_Parameters.find( name );                           \
145     if( i != this->m_Parameters.end( ) )                                \
146     {                                                                   \
147       if( i->second.first == Self::Y##List )                            \
148       {                                                                 \
149         if( i->second.second != "" )                                    \
150         {                                                               \
151           i->second.second = "";                                        \
152           this->Modified( );                                            \
153         }                                                               \
154       }                                                                 \
155     }                                                                   \
156   }
157
158 namespace cpPlugins
159 {
160   // Forward declaration to improve Qt dialog execution
161   class ParametersQtDialog;
162
163   /**
164    */
165   class cpPlugins_EXPORT Parameters
166   {
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 )
170     {
171       for(
172         auto i = p.m_Parameters.begin( );
173         i != p.m_Parameters.end( );
174         ++i
175         )
176         o << i->first << ": ("
177           << i->second.first << " | "
178           << i->second.second << ")"
179           << std::endl;
180       return( o );
181     }
182
183   public:
184     typedef Parameters Self;
185
186     enum Type
187     {
188       String       , Bool             , Int              ,
189       Uint         , Real             , OpenFileName     ,
190       SaveFileName , PathName         , StringList       ,
191       BoolList     , IntList          , UintList         ,
192       RealList     , OpenFileNameList , SaveFileNameList ,
193       PathNameList , Choices          , NoType
194     };
195
196     typedef bool          TBool;
197     typedef long          TInt;
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;
204
205     typedef std::pair< Self::Type, std::string > TParameter;
206     typedef std::map< std::string, TParameter >  TParameters;
207
208   public:
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 );
226
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 );
234
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 );
243
244   public:
245     Parameters( );
246     virtual ~Parameters( );
247
248     virtual void Modified( ) const;
249     virtual itk::ModifiedTimeType GetMTime( ) const;
250
251     // Parameters container configuration
252     void Clear( );
253
254     // Get methods
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 );
259
260     // Base string methods
261     std::string GetString(
262       const std::string& name, bool force = true
263       ) const;
264     void SetString(
265       const std::string& name, const std::string& v, bool force = true
266       );
267
268     void ConfigureAsChoices(
269       const std::string& name, const std::vector< std::string >& choices
270       );
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
275       );
276
277     std::string GetAcceptedFileExtensions( const std::string& name ) const;
278     void SetAcceptedFileExtensions(
279       const std::string& name, const std::string& extensions
280       );
281
282     // XML "streaming"
283     bool ToXML(
284       tinyxml2::XMLDocument* doc,
285       tinyxml2::XMLElement* parent_elem
286       ) const;
287     bool FromXML( const tinyxml2::XMLElement* filter_elem );
288
289   protected:
290     TParameters& GetRawParameters( );
291     const TParameters& GetRawParameters( ) const;
292
293   private:
294     // Purposely not implemented
295     Parameters( const Self& other );
296     Self& operator=( const Self& other );
297
298   protected:
299     mutable itk::TimeStamp               m_TimeStamp;
300     TParameters                          m_Parameters;
301     std::map< std::string, std::string > m_AcceptedFileExtensions;
302   };
303
304 } // ecapseman
305
306 #endif // __CPPLUGINS__PARAMETERS__H__
307
308 // eof - $RCSfile$