]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Parameters.cxx
Parameters given as lists were corrected to avoid conflicts with the MSWin file system.
[cpPlugins.git] / lib / cpPlugins / Interface / Parameters.cxx
1 #include <cpPlugins/Interface/Parameters.h>
2 #include <cstdarg>
3 #include <cstdlib>
4 #include <sstream>
5
6 // -------------------------------------------------------------------------
7 #define cpPlugins_Interface_Parameters_SetMacro( TYPE )                 \
8   void cpPlugins::Interface::Parameters::                               \
9   SetValueAs##TYPE( const TString& name, const T##TYPE& v )             \
10   {                                                                     \
11     TParameters::iterator pIt = this->m_Parameters.find( name );        \
12     if( pIt == this->m_Parameters.end( ) )                              \
13       return;                                                           \
14     if( pIt->second.first != Self::TYPE )                               \
15       return;                                                           \
16     std::stringstream ss;                                               \
17     ss << v;                                                            \
18     pIt->second.second = ss.str( );                                     \
19   }
20
21 cpPlugins_Interface_Parameters_SetMacro( String );
22 cpPlugins_Interface_Parameters_SetMacro( Bool );
23 cpPlugins_Interface_Parameters_SetMacro( Int );
24 cpPlugins_Interface_Parameters_SetMacro( Uint );
25 cpPlugins_Interface_Parameters_SetMacro( Real );
26
27 // -------------------------------------------------------------------------
28 #define cpPlugins_Interface_Parameters_SetArrayMacro( TYPE, ATYPE )     \
29   void cpPlugins::Interface::Parameters::                               \
30   SetValueAs##TYPE( const TString& name, const TUint& n, ... )          \
31   {                                                                     \
32     TParameters::iterator pIt = this->m_Parameters.find( name );        \
33     if( pIt == this->m_Parameters.end( ) )                              \
34       return;                                                           \
35     if( pIt->second.first != Self::TYPE )                               \
36       return;                                                           \
37     va_list v_lst;                                                      \
38     va_start( v_lst, n );                                               \
39     std::stringstream ss;                                               \
40     for( TUint i = 0; i < n; ++i )                                      \
41       ss << va_arg( v_lst, ATYPE ) << ",";                              \
42     va_end( v_lst );                                                    \
43     pIt->second.second = ss.str( );                                     \
44   }
45
46 cpPlugins_Interface_Parameters_SetArrayMacro( Index, int );
47 cpPlugins_Interface_Parameters_SetArrayMacro( Point, double );
48
49 // -------------------------------------------------------------------------
50 #define cpPlugins_Interface_Parameters_SetListMacro( TYPE )             \
51   void cpPlugins::Interface::Parameters::                               \
52   AddValueTo##TYPE##List( const TString& name, const T##TYPE& v )       \
53   {                                                                     \
54     TParameters::iterator pIt = this->m_Parameters.find( name );        \
55     if( pIt == this->m_Parameters.end( ) )                              \
56       return;                                                           \
57     if( pIt->second.first != Self::TYPE##List )                         \
58       return;                                                           \
59     std::stringstream ss;                                               \
60     ss << pIt->second.second << v << "#";                               \
61     pIt->second.second = ss.str( );                                     \
62   }
63
64 cpPlugins_Interface_Parameters_SetListMacro( String );
65 cpPlugins_Interface_Parameters_SetListMacro( Bool );
66 cpPlugins_Interface_Parameters_SetListMacro( Int );
67 cpPlugins_Interface_Parameters_SetListMacro( Uint );
68 cpPlugins_Interface_Parameters_SetListMacro( Real );
69
70 // -------------------------------------------------------------------------
71 #define cpPlugins_Interface_Parameters_SetArrayListMacro( TYPE, ATYPE ) \
72   void cpPlugins::Interface::Parameters::                               \
73   AddValueTo##TYPE##List( const TString& name, const TUint& n, ... )    \
74   {                                                                     \
75     TParameters::iterator pIt = this->m_Parameters.find( name );        \
76     if( pIt == this->m_Parameters.end( ) )                              \
77       return;                                                           \
78     if( pIt->second.first != Self::TYPE##List )                         \
79       return;                                                           \
80     va_list v_lst;                                                      \
81     va_start( v_lst, n );                                               \
82     std::stringstream ss;                                               \
83     ss << pIt->second.second;                                           \
84     for( TUint i = 0; i < n; ++i )                                      \
85       ss << va_arg( v_lst, ATYPE ) << ",";                              \
86     va_end( v_lst );                                                    \
87     ss << "#";                                                          \
88     pIt->second.second = ss.str( );                                     \
89   }
90
91 cpPlugins_Interface_Parameters_SetArrayListMacro( Index, int );
92 cpPlugins_Interface_Parameters_SetArrayListMacro( Point, double );
93
94 // -------------------------------------------------------------------------
95 #define cpPlugins_Interface_Parameters_ClearListMacro( TYPE )           \
96   void cpPlugins::Interface::Parameters::                               \
97   Clear##TYPE##List( const TString& name )                              \
98   {                                                                     \
99     TParameters::iterator pIt = this->m_Parameters.find( name );        \
100     if( pIt == this->m_Parameters.end( ) )                              \
101       return;                                                           \
102     if( pIt->second.first != Self::TYPE##List )                         \
103       return;                                                           \
104     pIt->second.second = "";                                            \
105   }
106
107 cpPlugins_Interface_Parameters_ClearListMacro( String );
108 cpPlugins_Interface_Parameters_ClearListMacro( Bool );
109 cpPlugins_Interface_Parameters_ClearListMacro( Int );
110 cpPlugins_Interface_Parameters_ClearListMacro( Uint );
111 cpPlugins_Interface_Parameters_ClearListMacro( Real );
112 cpPlugins_Interface_Parameters_ClearListMacro( Index );
113 cpPlugins_Interface_Parameters_ClearListMacro( Point );
114
115 // -------------------------------------------------------------------------
116 cpPlugins::Interface::Parameters::
117 Parameters( )
118 {
119   this->Clear( );
120 }
121
122 // -------------------------------------------------------------------------
123 cpPlugins::Interface::Parameters::
124 Parameters( const Self& other )
125 {
126   this->m_Parameters = other.m_Parameters;
127 }
128
129 // -------------------------------------------------------------------------
130 cpPlugins::Interface::Parameters::
131 ~Parameters( )
132 {
133   this->Clear( );
134 }
135
136 // -------------------------------------------------------------------------
137 cpPlugins::Interface::Parameters::
138 Self& cpPlugins::Interface::Parameters::
139 operator=( const Self& other )
140 {
141   this->m_Parameters = other.m_Parameters;
142   return( *this );
143 }
144
145 // -------------------------------------------------------------------------
146 void cpPlugins::Interface::Parameters::
147 Clear( )
148 {
149   this->m_Parameters.clear( );
150 }
151
152 // -------------------------------------------------------------------------
153 void cpPlugins::Interface::Parameters::
154 Configure( const Self::Type& type, const TString& name )
155 {
156   this->m_Parameters[ name ] = TParameter( type, "" );
157 }
158
159 // -------------------------------------------------------------------------
160 std::vector< cpPlugins::Interface::Parameters::TString >
161 cpPlugins::Interface::Parameters::
162 GetParameters( ) const
163 {
164   std::vector< TString > parameters;
165   TParameters::const_iterator pIt = this->m_Parameters.begin( );
166   for( ; pIt != this->m_Parameters.end( ); ++pIt )
167     parameters.push_back( pIt->first );
168   return( parameters );
169 }
170
171 // -------------------------------------------------------------------------
172 cpPlugins::Interface::Parameters::
173 Type cpPlugins::Interface::Parameters::
174 GetParameterType( const TString& name ) const
175 {
176   TParameters::const_iterator pIt = this->m_Parameters.find( name );
177   if( pIt == this->m_Parameters.end( ) )
178     return( Self::NoType );
179   return( pIt->second.first );
180 }
181
182 // -------------------------------------------------------------------------
183 const cpPlugins::Interface::Parameters::
184 TString& cpPlugins::Interface::Parameters::
185 GetRawValue( const TString& name ) const
186 {
187   static const TString null_str = "";
188   TParameters::const_iterator pIt = this->m_Parameters.find( name );
189   if( pIt == this->m_Parameters.end( ) )
190     return( null_str );
191   return( pIt->second.second );
192 }
193
194 // -------------------------------------------------------------------------
195 const cpPlugins::Interface::Parameters::
196 TString& cpPlugins::Interface::Parameters::
197 GetValueAsString( const TString& name ) const
198 {
199   static const TString null_str = "";
200   TParameters::const_iterator pIt = this->m_Parameters.find( name );
201   if( pIt == this->m_Parameters.end( ) )
202     return( null_str );
203   if( pIt->second.first != Self::String )
204     return( null_str );
205
206   return( pIt->second.second );
207 }
208
209 // -------------------------------------------------------------------------
210 cpPlugins::Interface::Parameters::
211 TBool cpPlugins::Interface::Parameters::
212 GetValueAsBool( const TString& name ) const
213 {
214   TParameters::const_iterator pIt = this->m_Parameters.find( name );
215   if( pIt == this->m_Parameters.end( ) )
216     return( TBool( false ) );
217   if( pIt->second.first != Self::Bool )
218     return( TBool( false ) );
219   return( TBool( std::atoi( pIt->second.second.c_str( ) ) == 1 ) );
220 }
221
222 // -------------------------------------------------------------------------
223 cpPlugins::Interface::Parameters::
224 TInt cpPlugins::Interface::Parameters::
225 GetValueAsInt( const TString& name ) const
226 {
227   TParameters::const_iterator pIt = this->m_Parameters.find( name );
228   if( pIt == this->m_Parameters.end( ) )
229     return( TInt( 0 ) );
230   if( pIt->second.first != Self::Int )
231     return( TInt( 0 ) );
232   return( TInt( std::atoi( pIt->second.second.c_str( ) ) ) );
233 }
234
235 // -------------------------------------------------------------------------
236 cpPlugins::Interface::Parameters::
237 TUint cpPlugins::Interface::Parameters::
238 GetValueAsUint( const TString& name ) const
239 {
240   TParameters::const_iterator pIt = this->m_Parameters.find( name );
241   if( pIt == this->m_Parameters.end( ) )
242     return( TUint( 0 ) );
243   if( pIt->second.first != Self::Uint )
244     return( TUint( 0 ) );
245   return( TUint( std::atoi( pIt->second.second.c_str( ) ) ) );
246 }
247
248 // -------------------------------------------------------------------------
249 cpPlugins::Interface::Parameters::
250 TReal cpPlugins::Interface::Parameters::
251 GetValueAsReal( const TString& name ) const
252 {
253   TParameters::const_iterator pIt = this->m_Parameters.find( name );
254   if( pIt == this->m_Parameters.end( ) )
255     return( TReal( 0 ) );
256   if( pIt->second.first != Self::Real )
257     return( TReal( 0 ) );
258   return( TReal( std::atof( pIt->second.second.c_str( ) ) ) );
259 }
260
261 // -------------------------------------------------------------------------
262 void cpPlugins::Interface::Parameters::
263 GetValueAsStringList(
264   std::vector< TString >& lst, const TString& name
265   ) const
266 {
267   lst.clear( );
268   TParameters::const_iterator pIt = this->m_Parameters.find( name );
269   if( pIt == this->m_Parameters.end( ) )
270     return;
271   if( pIt->second.first != Self::StringList )
272     return;
273
274   std::istringstream ss( pIt->second.second );
275   std::string token;
276   while( std::getline( ss, token, '#' ) )
277     if( token != "" )
278       lst.push_back( token );
279 }
280
281 // -------------------------------------------------------------------------
282 void cpPlugins::Interface::Parameters::
283 GetValueAsBoolList( std::vector< TBool >& lst, const TString& name ) const
284 {
285   lst.clear( );
286   TParameters::const_iterator pIt = this->m_Parameters.find( name );
287   if( pIt == this->m_Parameters.end( ) )
288     return;
289   if( pIt->second.first != Self::BoolList )
290     return;
291
292   std::istringstream ss( pIt->second.second );
293   std::string token;
294   while( std::getline( ss, token, '#' ) )
295     if( token != "" )
296       lst.push_back( TBool( std::atoi( token.c_str( ) ) == 1 ) );
297 }
298
299 // -------------------------------------------------------------------------
300 void cpPlugins::Interface::Parameters::
301 GetValueAsIntList( std::vector< TInt >& lst, const TString& name ) const
302 {
303   lst.clear( );
304   TParameters::const_iterator pIt = this->m_Parameters.find( name );
305   if( pIt == this->m_Parameters.end( ) )
306     return;
307   if( pIt->second.first != Self::IntList )
308     return;
309
310   std::istringstream ss( pIt->second.second );
311   std::string token;
312   while( std::getline( ss, token, '#' ) )
313     if( token != "" )
314       lst.push_back( TInt( std::atoi( token.c_str( ) ) ) );
315 }
316
317 // -------------------------------------------------------------------------
318 void cpPlugins::Interface::Parameters::
319 GetValueAsUintList( std::vector< TUint >& lst, const TString& name ) const
320 {
321   lst.clear( );
322   TParameters::const_iterator pIt = this->m_Parameters.find( name );
323   if( pIt == this->m_Parameters.end( ) )
324     return;
325   if( pIt->second.first != Self::UintList )
326     return;
327
328   std::istringstream ss( pIt->second.second );
329   std::string token;
330   while( std::getline( ss, token, '#' ) )
331     if( token != "" )
332       lst.push_back( TUint( std::atoi( token.c_str( ) ) ) );
333 }
334
335 // -------------------------------------------------------------------------
336 void cpPlugins::Interface::Parameters::
337 GetValueAsRealList( std::vector< TReal >& lst, const TString& name ) const
338 {
339   lst.clear( );
340   TParameters::const_iterator pIt = this->m_Parameters.find( name );
341   if( pIt == this->m_Parameters.end( ) )
342     return;
343   if( pIt->second.first != Self::RealList )
344     return;
345
346   std::istringstream ss( pIt->second.second );
347   std::string token;
348   while( std::getline( ss, token, '#' ) )
349     if( token != "" )
350       lst.push_back( TReal( std::atof( token.c_str( ) ) ) );
351 }
352
353 // eof - $RCSfile$