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