]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Parameters.h
dc0ea52478d2c24115a581bb4a0c012cefcbed49
[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   bool Has##Y( const std::string& name ) const
25
26 // -------------------------------------------------------------------------
27 #define cpPlugins_Parameters_GetSet( Y )                        \
28   T##Y Get##Y( const std::string& name ) const;                 \
29   void Set##Y( const std::string& name, const T##Y& v )
30
31 // -------------------------------------------------------------------------
32 #define cpPlugins_Parameters_GetSetList( Y )                            \
33   std::vector< T##Y > Get##Y##List( const std::string& name ) const;    \
34   void AddTo##Y##List( const std::string& name, const T##Y& v );        \
35   void Clear##Y##List( const std::string& name )
36
37 namespace cpPlugins
38 {
39   // Forward declaration to improve Qt dialog execution
40   class ParametersQtDialog;
41
42   /**
43    */
44   class cpPlugins_EXPORT Parameters
45   {
46     // Frienship with forward declaration to improve Qt dialog execution
47     friend class ParametersQtDialog;
48     friend std::ostream& operator<<( std::ostream& o, const Parameters& p )
49     {
50       for(
51         auto i = p.m_Parameters.begin( );
52         i != p.m_Parameters.end( );
53         ++i
54         )
55         o << i->first << ": ("
56           << i->second.first << " | "
57           << i->second.second << ")"
58           << std::endl;
59       return( o );
60     }
61
62   public:
63     typedef Parameters Self;
64
65     enum Type
66     {
67       String       , Bool             , Int              ,
68       Uint         , Real             , OpenFileName     ,
69       SaveFileName , PathName         , StringList       ,
70       BoolList     , IntList          , UintList         ,
71       RealList     , OpenFileNameList , SaveFileNameList ,
72       PathNameList , Choices          , NoType
73     };
74
75     typedef bool          TBool;
76     typedef long          TInt;
77     typedef unsigned long TUint;
78     typedef double        TReal;
79     typedef std::string   TString;
80     typedef std::string   TOpenFileName;
81     typedef std::string   TSaveFileName;
82     typedef std::string   TPathName;
83
84     typedef std::pair< Self::Type, std::string > TParameter;
85     typedef std::map< std::string, TParameter >  TParameters;
86
87   public:
88     cpPlugins_Parameters_Configure( String );
89     cpPlugins_Parameters_Configure( Bool );
90     cpPlugins_Parameters_Configure( Int );
91     cpPlugins_Parameters_Configure( Uint );
92     cpPlugins_Parameters_Configure( Real );
93     cpPlugins_Parameters_Configure( OpenFileName );
94     cpPlugins_Parameters_Configure( SaveFileName );
95     cpPlugins_Parameters_Configure( PathName );
96     cpPlugins_Parameters_Configure( StringList );
97     cpPlugins_Parameters_Configure( BoolList );
98     cpPlugins_Parameters_Configure( IntList );
99     cpPlugins_Parameters_Configure( UintList );
100     cpPlugins_Parameters_Configure( RealList );
101     cpPlugins_Parameters_Configure( OpenFileNameList );
102     cpPlugins_Parameters_Configure( SaveFileNameList );
103     cpPlugins_Parameters_Configure( PathNameList );
104     cpPlugins_Parameters_Configure( Choices );
105
106     cpPlugins_Parameters_GetSet( Bool );
107     cpPlugins_Parameters_GetSet( Int );
108     cpPlugins_Parameters_GetSet( Uint );
109     cpPlugins_Parameters_GetSet( Real );
110     cpPlugins_Parameters_GetSet( OpenFileName );
111     cpPlugins_Parameters_GetSet( SaveFileName );
112     cpPlugins_Parameters_GetSet( PathName );
113
114     cpPlugins_Parameters_GetSetList( String );
115     cpPlugins_Parameters_GetSetList( Bool );
116     cpPlugins_Parameters_GetSetList( Int );
117     cpPlugins_Parameters_GetSetList( Uint );
118     cpPlugins_Parameters_GetSetList( Real );
119     cpPlugins_Parameters_GetSetList( OpenFileName );
120     cpPlugins_Parameters_GetSetList( SaveFileName );
121     cpPlugins_Parameters_GetSetList( PathName );
122
123   public:
124     Parameters( );
125     virtual ~Parameters( );
126
127     virtual void Modified( ) const;
128     virtual itk::ModifiedTimeType GetMTime( ) const;
129
130     // Parameters container configuration
131     void Clear( );
132
133     // Get methods
134     void GetNames( std::vector< std::string >& container ) const;
135     Type GetType( const std::string& name ) const;
136     std::string GetTypeAsString( const std::string& name ) const;
137     static Type GetTypeFromString( const std::string& t );
138
139     // Base string methods
140     std::string GetString(
141       const std::string& name, bool force = true
142       ) const;
143     void SetString(
144       const std::string& name, const std::string& v, bool force = true
145       );
146
147     void ConfigureAsChoices(
148       const std::string& name, const std::vector< std::string >& choices
149       );
150     std::vector< std::string > GetChoices( const std::string& name ) const;
151     std::string GetSelectedChoice( const std::string& name ) const;
152     bool SetSelectedChoice(
153       const std::string& name, const std::string& choice
154       );
155
156     std::string GetAcceptedFileExtensions( const std::string& name ) const;
157     void SetAcceptedFileExtensions(
158       const std::string& name, const std::string& extensions
159       );
160
161     // XML "streaming"
162     bool ToXML(
163       tinyxml2::XMLDocument* doc,
164       tinyxml2::XMLElement* parent_elem
165       ) const;
166     bool FromXML( const tinyxml2::XMLElement* filter_elem );
167
168   protected:
169     TParameters& GetRawParameters( );
170     const TParameters& GetRawParameters( ) const;
171
172     template< unsigned int _Enum >
173       inline void _Configure( const std::string& name );
174
175     template< unsigned int _Enum >
176       inline bool _Has( const std::string& name ) const;
177
178     template< class _Type, unsigned int _Enum >
179       inline _Type _Get( const std::string& name ) const;
180
181     template< class _Type, unsigned int _Enum >
182       inline void _Set( const std::string& name, const _Type& v );
183
184     template< class _Type, unsigned int _Enum >
185       inline std::vector< _Type > _GetList( const std::string& name ) const;
186
187     template< class _Type, unsigned int _Enum >
188       inline void _AddToList( const std::string& name, const _Type& v );
189
190     template< unsigned int _Enum >
191       inline void _ClearList( const std::string& name );
192
193   private:
194     // Purposely not implemented
195     Parameters( const Self& other );
196     Self& operator=( const Self& other );
197
198   protected:
199     mutable itk::TimeStamp               m_TimeStamp;
200     TParameters                          m_Parameters;
201     std::map< std::string, std::string > m_AcceptedFileExtensions;
202   };
203
204 } // ecapseman
205
206 #endif // __CPPLUGINS__PARAMETERS__H__
207
208 // eof - $RCSfile$