]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Parameters.cxx
More on parameters
[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( Int );
23 cpPlugins_Interface_Parameters_SetMacro( Uint );
24 cpPlugins_Interface_Parameters_SetMacro( Real );
25
26 // -------------------------------------------------------------------------
27 #define cpPlugins_Interface_Parameters_SetArrayMacro( TYPE, ATYPE )     \
28   void cpPlugins::Interface::Parameters::                               \
29   SetValueAs##TYPE( const TString& name, const TUint& n, ... )          \
30   {                                                                     \
31     TParameters::iterator pIt = this->m_Parameters.find( name );        \
32     if( pIt == this->m_Parameters.end( ) )                              \
33       return;                                                           \
34     if( pIt->second.first != Self::TYPE )                               \
35       return;                                                           \
36     va_list v_lst;                                                      \
37     va_start( v_lst, n );                                               \
38     std::stringstream ss;                                               \
39     for( TUint i = 0; i < n; ++i )                                      \
40       ss << va_arg( v_lst, ATYPE ) << ",";                              \
41     va_end( v_lst );                                                    \
42     pIt->second.second = ss.str( );                                     \
43   }
44
45 cpPlugins_Interface_Parameters_SetArrayMacro( Index, long );
46 cpPlugins_Interface_Parameters_SetArrayMacro( Point, double );
47
48 // -------------------------------------------------------------------------
49 cpPlugins::Interface::Parameters::
50 Parameters( )
51 {
52   this->Clear( );
53 }
54
55 // -------------------------------------------------------------------------
56 cpPlugins::Interface::Parameters::
57 Parameters( const Self& other )
58 {
59   this->m_Parameters = other.m_Parameters;
60 }
61
62 // -------------------------------------------------------------------------
63 cpPlugins::Interface::Parameters::
64 ~Parameters( )
65 {
66   this->Clear( );
67 }
68
69 // -------------------------------------------------------------------------
70 cpPlugins::Interface::Parameters::
71 Self& cpPlugins::Interface::Parameters::
72 operator=( const Self& other )
73 {
74   this->m_Parameters = other.m_Parameters;
75   return( *this );
76 }
77
78 // -------------------------------------------------------------------------
79 void cpPlugins::Interface::Parameters::
80 Clear( )
81 {
82   this->m_Parameters.clear( );
83 }
84
85 // -------------------------------------------------------------------------
86 void cpPlugins::Interface::Parameters::
87 Configure( const Self::Type& type, const TString& name )
88 {
89   this->m_Parameters[ name ] = TParameter( type, "" );
90 }
91
92 // -------------------------------------------------------------------------
93 std::vector< cpPlugins::Interface::Parameters::TString >
94 cpPlugins::Interface::Parameters::
95 GetParameters( ) const
96 {
97   std::vector< TString > parameters;
98   TParameters::const_iterator pIt = this->m_Parameters.begin( );
99   for( ; pIt != this->m_Parameters.end( ); ++pIt )
100     parameters.push_back( pIt->first );
101   return( parameters );
102 }
103
104 // -------------------------------------------------------------------------
105 cpPlugins::Interface::Parameters::
106 Type cpPlugins::Interface::Parameters::
107 GetParameterType( const TString& name ) const
108 {
109   TParameters::const_iterator pIt = this->m_Parameters.find( name );
110   if( pIt == this->m_Parameters.end( ) )
111     return( Self::NoType );
112   return( pIt->second.first );
113 }
114
115 // -------------------------------------------------------------------------
116 const cpPlugins::Interface::Parameters::
117 TString& cpPlugins::Interface::Parameters::
118 GetValueAsString( const TString& name ) const
119 {
120   static const TString null_str = "";
121   TParameters::const_iterator pIt = this->m_Parameters.find( name );
122   if( pIt == this->m_Parameters.end( ) )
123     return( null_str );
124   if( pIt->second.first != Self::String )
125     return( null_str );
126
127   return( pIt->second.second );
128 }
129
130 // -------------------------------------------------------------------------
131 cpPlugins::Interface::Parameters::
132 TInt cpPlugins::Interface::Parameters::
133 GetValueAsInt( const TString& name ) const
134 {
135   TParameters::const_iterator pIt = this->m_Parameters.find( name );
136   if( pIt == this->m_Parameters.end( ) )
137     return( TInt( 0 ) );
138   if( pIt->second.first != Self::Int )
139     return( TInt( 0 ) );
140   return( TInt( std::atoi( pIt->second.second.c_str( ) ) ) );
141 }
142
143 // -------------------------------------------------------------------------
144 cpPlugins::Interface::Parameters::
145 TUint cpPlugins::Interface::Parameters::
146 GetValueAsUint( const TString& name ) const
147 {
148   TParameters::const_iterator pIt = this->m_Parameters.find( name );
149   if( pIt == this->m_Parameters.end( ) )
150     return( TUint( 0 ) );
151   if( pIt->second.first != Self::Uint )
152     return( TUint( 0 ) );
153   return( TUint( std::atoi( pIt->second.second.c_str( ) ) ) );
154 }
155
156 // -------------------------------------------------------------------------
157 cpPlugins::Interface::Parameters::
158 TReal cpPlugins::Interface::Parameters::
159 GetValueAsReal( const TString& name ) const
160 {
161   TParameters::const_iterator pIt = this->m_Parameters.find( name );
162   if( pIt == this->m_Parameters.end( ) )
163     return( TReal( 0 ) );
164   if( pIt->second.first != Self::Real )
165     return( TReal( 0 ) );
166   return( TReal( std::atof( pIt->second.second.c_str( ) ) ) );
167 }
168
169 // -------------------------------------------------------------------------
170 void cpPlugins::Interface::Parameters::
171 GetValueAsStringList(
172   std::vector< TString >& lst, const TString& name
173   ) const
174 {
175 }
176
177 // -------------------------------------------------------------------------
178 void cpPlugins::Interface::Parameters::
179 GetValueAsIntList( std::vector< TInt >& lst, const TString& name ) const
180 {
181 }
182
183 // -------------------------------------------------------------------------
184 void cpPlugins::Interface::Parameters::
185 GetValueAsUintList( std::vector< TUint >& lst, const TString& name ) const
186 {
187 }
188
189 // -------------------------------------------------------------------------
190 void cpPlugins::Interface::Parameters::
191 GetValueAsRealList( std::vector< TReal >& lst, const TString& name ) const
192 {
193 }
194
195 // eof - $RCSfile$