]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/BaseObjects/Parameters.h
338c8afe44f3842bf81f42acff93e3628ea25425
[cpPlugins.git] / lib / cpPlugins / BaseObjects / Parameters.h
1 #ifndef __cpPlugins__BaseObjects__Parameters__h__
2 #define __cpPlugins__BaseObjects__Parameters__h__
3
4 #include <cpPlugins/Config.h>
5
6 #include <map>
7 #include <ostream>
8 #include <sstream>
9 #include <typeinfo>
10 #include <itkTimeStamp.h>
11
12 // Some forward declarations
13 namespace tinyxml2
14 {
15   class XMLElement;
16   class XMLDocument;
17 }
18
19 // -------------------------------------------------------------------------
20 #define cpPlugins_BaseObjects_Parameters_Configure( Y )         \
21   void ConfigureAs##Y( const std::string& name );               \
22   bool Has##Y( const std::string& name ) const
23
24 // -------------------------------------------------------------------------
25 #define cpPlugins_BaseObjects_Parameters_GetSet( Y )            \
26   T##Y Get##Y( const std::string& name ) const;                 \
27   void Set##Y( const std::string& name, const T##Y& v )
28
29 // -------------------------------------------------------------------------
30 #define cpPlugins_BaseObjects_Parameters_GetSetList( Y )                \
31   std::vector< T##Y > Get##Y##List( const std::string& name ) const;    \
32   void AddTo##Y##List( const std::string& name, const T##Y& v );        \
33   void Clear##Y##List( const std::string& name )
34
35 // -------------------------------------------------------------------------
36 namespace cpPlugins
37 {
38   namespace QT { class ParametersDialog; }
39
40   namespace BaseObjects
41   {
42     /**
43      */
44     class cpPlugins_EXPORT Parameters
45     {
46       // Frienship with forward declaration to improve Qt dialog execution
47       friend class cpPlugins::QT::ParametersDialog;
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_BaseObjects_Parameters_Configure( String );
89       cpPlugins_BaseObjects_Parameters_Configure( Bool );
90       cpPlugins_BaseObjects_Parameters_Configure( Int );
91       cpPlugins_BaseObjects_Parameters_Configure( Uint );
92       cpPlugins_BaseObjects_Parameters_Configure( Real );
93       cpPlugins_BaseObjects_Parameters_Configure( OpenFileName );
94       cpPlugins_BaseObjects_Parameters_Configure( SaveFileName );
95       cpPlugins_BaseObjects_Parameters_Configure( PathName );
96       cpPlugins_BaseObjects_Parameters_Configure( StringList );
97       cpPlugins_BaseObjects_Parameters_Configure( BoolList );
98       cpPlugins_BaseObjects_Parameters_Configure( IntList );
99       cpPlugins_BaseObjects_Parameters_Configure( UintList );
100       cpPlugins_BaseObjects_Parameters_Configure( RealList );
101       cpPlugins_BaseObjects_Parameters_Configure( OpenFileNameList );
102       cpPlugins_BaseObjects_Parameters_Configure( SaveFileNameList );
103       cpPlugins_BaseObjects_Parameters_Configure( PathNameList );
104       cpPlugins_BaseObjects_Parameters_Configure( Choices );
105
106       cpPlugins_BaseObjects_Parameters_GetSet( Bool );
107       cpPlugins_BaseObjects_Parameters_GetSet( Int );
108       cpPlugins_BaseObjects_Parameters_GetSet( Uint );
109       cpPlugins_BaseObjects_Parameters_GetSet( Real );
110       cpPlugins_BaseObjects_Parameters_GetSet( OpenFileName );
111       cpPlugins_BaseObjects_Parameters_GetSet( SaveFileName );
112       cpPlugins_BaseObjects_Parameters_GetSet( PathName );
113
114       cpPlugins_BaseObjects_Parameters_GetSetList( String );
115       cpPlugins_BaseObjects_Parameters_GetSetList( Bool );
116       cpPlugins_BaseObjects_Parameters_GetSetList( Int );
117       cpPlugins_BaseObjects_Parameters_GetSetList( Uint );
118       cpPlugins_BaseObjects_Parameters_GetSetList( Real );
119       cpPlugins_BaseObjects_Parameters_GetSetList( OpenFileName );
120       cpPlugins_BaseObjects_Parameters_GetSetList( SaveFileName );
121       cpPlugins_BaseObjects_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 } // ecapseman
207
208 #endif // __cpPlugins__BaseObjects__Parameters__h__
209
210 // eof - $RCSfile$