]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Interface/Parameters.cxx
More on parameters simplification
[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->m_Parameters.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->m_Parameters.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 Configure( const Self::Type& type, const TString& name )
81 {
82   this->m_Parameters[ name ] = TParameter( type, "" );
83 }
84
85 // -------------------------------------------------------------------------
86 std::vector< cpPlugins::Interface::Parameters::TString >
87 cpPlugins::Interface::Parameters::
88 GetParameters( ) const
89 {
90   std::vector< TString > parameters;
91   TParameters::const_iterator pIt = this->m_Parameters.begin( );
92   for( ; pIt != this->m_Parameters.end( ); ++pIt )
93     parameters.push_back( pIt->first );
94   return( parameters );
95 }
96
97 // -------------------------------------------------------------------------
98 cpPlugins::Interface::Parameters::
99 Type cpPlugins::Interface::Parameters::
100 GetParameterType( const TString& name ) const
101 {
102   TParameters::const_iterator pIt = this->m_Parameters.find( name );
103   if( pIt == this->m_Parameters.end( ) )
104     return( Self::NoType );
105   return( pIt->second.first );
106 }
107
108 // -------------------------------------------------------------------------
109 const cpPlugins::Interface::Parameters::
110 TString& cpPlugins::Interface::Parameters::
111 GetValueAsString( const TString& name ) const
112 {
113   static const TString null_str = "";
114   TParameters::const_iterator pIt = this->m_Parameters.find( name );
115   if( pIt == this->m_Parameters.end( ) )
116     return( null_str );
117   if( pIt->second.first != Self::String )
118     return( null_str );
119
120   return( pIt->second.second );
121 }
122
123 // -------------------------------------------------------------------------
124 cpPlugins::Interface::Parameters::
125 TInt cpPlugins::Interface::Parameters::
126 GetValueAsInt( const TString& name ) const
127 {
128   TParameters::const_iterator pIt = this->m_Parameters.find( name );
129   if( pIt == this->m_Parameters.end( ) )
130     return( TInt( 0 ) );
131   if( pIt->second.first != Self::Int )
132     return( TInt( 0 ) );
133   return( TInt( std::atoi( pIt->second.second.c_str( ) ) ) );
134 }
135
136 // -------------------------------------------------------------------------
137 cpPlugins::Interface::Parameters::
138 TUint cpPlugins::Interface::Parameters::
139 GetValueAsUint( const TString& name ) const
140 {
141   TParameters::const_iterator pIt = this->m_Parameters.find( name );
142   if( pIt == this->m_Parameters.end( ) )
143     return( TUint( 0 ) );
144   if( pIt->second.first != Self::Uint )
145     return( TUint( 0 ) );
146   return( TUint( std::atoi( pIt->second.second.c_str( ) ) ) );
147 }
148
149 // -------------------------------------------------------------------------
150 cpPlugins::Interface::Parameters::
151 TReal cpPlugins::Interface::Parameters::
152 GetValueAsReal( const TString& name ) const
153 {
154   TParameters::const_iterator pIt = this->m_Parameters.find( name );
155   if( pIt == this->m_Parameters.end( ) )
156     return( TReal( 0 ) );
157   if( pIt->second.first != Self::Real )
158     return( TReal( 0 ) );
159   return( TReal( std::atof( pIt->second.second.c_str( ) ) ) );
160 }
161
162 // -------------------------------------------------------------------------
163 void cpPlugins::Interface::Parameters::
164 GetValueAsStringList(
165   std::vector< TString >& lst, const TString& name
166   ) const
167 {
168 }
169
170 // -------------------------------------------------------------------------
171 void cpPlugins::Interface::Parameters::
172 GetValueAsIntList( std::vector< TInt >& lst, const TString& name ) const
173 {
174 }
175
176 // -------------------------------------------------------------------------
177 void cpPlugins::Interface::Parameters::
178 GetValueAsUintList( std::vector< TUint >& lst, const TString& name ) const
179 {
180 }
181
182 // -------------------------------------------------------------------------
183 void cpPlugins::Interface::Parameters::
184 GetValueAsRealList( std::vector< TReal >& lst, const TString& name ) const
185 {
186 }
187
188 // eof - $RCSfile$