]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Parameters.cxx
b8d358b5103891ed27eed7bf590cfd50865604da
[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, long );
47 cpPlugins_Interface_Parameters_SetArrayMacro( Point, double );
48
49 // -------------------------------------------------------------------------
50 cpPlugins::Interface::Parameters::
51 Parameters( )
52 {
53   this->Clear( );
54 }
55
56 // -------------------------------------------------------------------------
57 cpPlugins::Interface::Parameters::
58 Parameters( const Self& other )
59 {
60   this->m_Parameters = other.m_Parameters;
61 }
62
63 // -------------------------------------------------------------------------
64 cpPlugins::Interface::Parameters::
65 ~Parameters( )
66 {
67   this->Clear( );
68 }
69
70 // -------------------------------------------------------------------------
71 cpPlugins::Interface::Parameters::
72 Self& cpPlugins::Interface::Parameters::
73 operator=( const Self& other )
74 {
75   this->m_Parameters = other.m_Parameters;
76   return( *this );
77 }
78
79 // -------------------------------------------------------------------------
80 void cpPlugins::Interface::Parameters::
81 Clear( )
82 {
83   this->m_Parameters.clear( );
84 }
85
86 // -------------------------------------------------------------------------
87 void cpPlugins::Interface::Parameters::
88 Configure( const Self::Type& type, const TString& name )
89 {
90   this->m_Parameters[ name ] = TParameter( type, "" );
91 }
92
93 // -------------------------------------------------------------------------
94 std::vector< cpPlugins::Interface::Parameters::TString >
95 cpPlugins::Interface::Parameters::
96 GetParameters( ) const
97 {
98   std::vector< TString > parameters;
99   TParameters::const_iterator pIt = this->m_Parameters.begin( );
100   for( ; pIt != this->m_Parameters.end( ); ++pIt )
101     parameters.push_back( pIt->first );
102   return( parameters );
103 }
104
105 // -------------------------------------------------------------------------
106 cpPlugins::Interface::Parameters::
107 Type cpPlugins::Interface::Parameters::
108 GetParameterType( const TString& name ) const
109 {
110   TParameters::const_iterator pIt = this->m_Parameters.find( name );
111   if( pIt == this->m_Parameters.end( ) )
112     return( Self::NoType );
113   return( pIt->second.first );
114 }
115
116 // -------------------------------------------------------------------------
117 const cpPlugins::Interface::Parameters::
118 TString& cpPlugins::Interface::Parameters::
119 GetValueAsString( const TString& name ) const
120 {
121   static const TString null_str = "";
122   TParameters::const_iterator pIt = this->m_Parameters.find( name );
123   if( pIt == this->m_Parameters.end( ) )
124     return( null_str );
125   if( pIt->second.first != Self::String )
126     return( null_str );
127
128   return( pIt->second.second );
129 }
130
131 // -------------------------------------------------------------------------
132 cpPlugins::Interface::Parameters::
133 TBool cpPlugins::Interface::Parameters::
134 GetValueAsBool( const TString& name ) const
135 {
136   TParameters::const_iterator pIt = this->m_Parameters.find( name );
137   if( pIt == this->m_Parameters.end( ) )
138     return( TBool( false ) );
139   if( pIt->second.first != Self::Int )
140     return( TBool( false ) );
141   return( TBool( std::atoi( pIt->second.second.c_str( ) ) == 1 ) );
142 }
143
144 // -------------------------------------------------------------------------
145 cpPlugins::Interface::Parameters::
146 TInt cpPlugins::Interface::Parameters::
147 GetValueAsInt( const TString& name ) const
148 {
149   TParameters::const_iterator pIt = this->m_Parameters.find( name );
150   if( pIt == this->m_Parameters.end( ) )
151     return( TInt( 0 ) );
152   if( pIt->second.first != Self::Int )
153     return( TInt( 0 ) );
154   return( TInt( std::atoi( pIt->second.second.c_str( ) ) ) );
155 }
156
157 // -------------------------------------------------------------------------
158 cpPlugins::Interface::Parameters::
159 TUint cpPlugins::Interface::Parameters::
160 GetValueAsUint( const TString& name ) const
161 {
162   TParameters::const_iterator pIt = this->m_Parameters.find( name );
163   if( pIt == this->m_Parameters.end( ) )
164     return( TUint( 0 ) );
165   if( pIt->second.first != Self::Uint )
166     return( TUint( 0 ) );
167   return( TUint( std::atoi( pIt->second.second.c_str( ) ) ) );
168 }
169
170 // -------------------------------------------------------------------------
171 cpPlugins::Interface::Parameters::
172 TReal cpPlugins::Interface::Parameters::
173 GetValueAsReal( const TString& name ) const
174 {
175   TParameters::const_iterator pIt = this->m_Parameters.find( name );
176   if( pIt == this->m_Parameters.end( ) )
177     return( TReal( 0 ) );
178   if( pIt->second.first != Self::Real )
179     return( TReal( 0 ) );
180   return( TReal( std::atof( pIt->second.second.c_str( ) ) ) );
181 }
182
183 // -------------------------------------------------------------------------
184 void cpPlugins::Interface::Parameters::
185 GetValueAsStringList(
186   std::vector< TString >& lst, const TString& name
187   ) const
188 {
189 }
190
191 // -------------------------------------------------------------------------
192 void cpPlugins::Interface::Parameters::
193 GetValueAsBoolList( std::vector< TBool >& lst, const TString& name ) const
194 {
195 }
196
197 // -------------------------------------------------------------------------
198 void cpPlugins::Interface::Parameters::
199 GetValueAsIntList( std::vector< TInt >& lst, const TString& name ) const
200 {
201 }
202
203 // -------------------------------------------------------------------------
204 void cpPlugins::Interface::Parameters::
205 GetValueAsUintList( std::vector< TUint >& lst, const TString& name ) const
206 {
207 }
208
209 // -------------------------------------------------------------------------
210 void cpPlugins::Interface::Parameters::
211 GetValueAsRealList( std::vector< TReal >& lst, const TString& name ) const
212 {
213 }
214
215 // eof - $RCSfile$