Mass++ Common Libraries v2.7.5
 All Classes Namespaces Files Functions Variables Enumerations Macros
SettingParameterValues.cpp
Go to the documentation of this file.
1 
12 #include "stdafx.h"
13 #include "SettingParameterValues.h"
14 
15 
16 using namespace kome::objects;
17 
18 
19 #include <crtdbg.h>
20 #ifdef _DEBUG
21  #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
22  #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
23 #endif // _DEBUG
24 
25 
26 
27 // constructor
29 }
30 
31 // copy constructor
33  // copy values
34  unsigned int valNum = other.m_values.size();
35  if( valNum > 0 ) {
36  m_values.reserve( valNum );
37 
38  for( unsigned int i = 0; i < valNum; i++ ) {
39  m_values.push_back( other.m_values[ i ] );
40  }
41  }
42 
43  // copy subitems
44  unsigned int itmNum = other.m_itemValues.size();
45  if( itmNum > 0 ) {
46  m_itemValues.reserve( itmNum );
47 
48  for( unsigned int i = 0; i < itmNum; i++ ) {
49  m_itemValues.push_back(
50  std::make_pair( other.m_itemValues[ i ].first, new SettingParameterValues( *( other.m_itemValues[ i ].second ) ))
51  );
52  }
53  }
54 }
55 
56 // assignment operator
58  // copy values
59  unsigned int valNum = other.m_values.size();
60  if( valNum > 0 ) {
61  m_values.reserve( valNum );
62 
63  for( unsigned int i = 0; i < valNum; i++ ) {
64  m_values.push_back( other.m_values[ i ] );
65  }
66  }
67 
68  // copy subitems
69  unsigned int itmNum = other.m_itemValues.size();
70  if( itmNum > 0 ) {
71  m_itemValues.reserve( itmNum );
72 
73  for( unsigned int i = 0; i < itmNum; i++ ) {
74  m_itemValues.push_back(
75  std::make_pair(
76  other.m_itemValues[ i ].first,
77  new SettingParameterValues( *( other.m_itemValues[ i ].second ) )
78  )
79  );
80  }
81  }
82 
83  return *this;
84 }
85 
86 // destructor
88  clear();
89 }
90 
91 // clear parameters
93  m_values.clear();
94 
95  for( unsigned int i = 0; i < m_itemValues.size(); i++ ) {
96  delete m_itemValues[ i ].second;
97  }
98  m_itemValues.clear();
99 }
100 
101 // get parameter index
102 int SettingParameterValues::getIndex( const char* name ) {
103  // string object
104  std::string s = NVL( name, "" );
105 
106  // search
107  int idx = -1;
108  for( unsigned int i = 0; i < m_values.size() && idx < 0; i++ ) {
109  if( s.compare( m_values[ i ].first ) == 0 ) {
110  idx = (int)i;
111  }
112  }
113 
114  return idx;
115 }
116 
117 // set value
118 void SettingParameterValues::setValue( const unsigned int index, const char* value ) {
119  // check the index
120  if( index >= m_values.size() ) {
121  return;
122  }
123 
124  // value
125  std::string v = NVL( value, "" );
126  if( v.compare( MSPP_SETTINGS_NONE_VALUE ) == 0 ) {
127  v = "";
128  }
129 
130  // set value
131  m_values[ index ].second = v;
132 }
133 
134 // set value
135 void SettingParameterValues::setValue( const char* name, const char* value ) {
136  // index
137  int idx = getIndex( name );
138 
139  // value
140  std::string v = NVL( value, "" );
141  if( v.compare( MSPP_SETTINGS_NONE_VALUE ) == 0 ) {
142  v = "";
143  }
144 
145  // set value
146  if( idx < 0 ) {
147  m_values.push_back( std::make_pair ( NVL( name, "" ), v ) );
148  }
149  else {
150  setValue( idx, v.c_str() );
151  }
152 }
153 
154 // get the number of parameters
156  return m_values.size();
157 }
158 
159 // get parameter name
160 const char* SettingParameterValues::getParameterName( const unsigned int index ) {
161  if( index >= m_values.size() ) {
162  return NULL;
163  }
164  return m_values[ index ].first.c_str();
165 }
166 
167 // get parameter value
168 const char* SettingParameterValues::getParameterValue( const unsigned int index ) {
169  if( index >= m_values.size() ) {
170  return NULL;
171  }
172  return m_values[ index ].second.c_str(); //最終的にはこれがgetParameterValue( name )から返る
173 }
174 
175 // get parameter value
176 const char* SettingParameterValues::getParameterValue( const char* name ) {
177  // index
178  int idx = getIndex( name );
179  if( idx < 0 ) {
180  return NULL;
181  }
182 
183  // value
184  return getParameterValue( idx );
185 }
186 
187 // get boolean value
188 bool SettingParameterValues::getBoolValue( const unsigned int index, const bool df ) {
189  // value
190  const char* value = getParameterValue( index );
191  const bool ret = tobool( value, df );
192 
193  return ret;
194 }
195 
196 // get boolean value
197 bool SettingParameterValues::getBoolValue( const char* name, const bool df ) {
198  // value
199  const char* value = getParameterValue( name );
200  const bool ret = tobool( value, df );
201 
202  return ret;
203 }
204 
205 // get integer value
206 int SettingParameterValues::getIntValue( const unsigned int index, const int df ) {
207  // value
208  const char* value = getParameterValue( index );
209  const int ret = toint( value, 10, df );
210 
211  return ret;
212 }
213 
214 // get integer value
215 int SettingParameterValues::getIntValue( const char* name, const int df ) {
216  // value
217  const char* value = getParameterValue( name );
218  const int ret = toint( value, 10, df );
219 
220  return ret;
221 }
222 
223 // get double value
224 double SettingParameterValues::getDoubleValue( const unsigned int index, const double df ) {
225  // value
226  const char* value = getParameterValue( index );
227  const double ret = todouble( value, df );
228 
229  return ret;
230 }
231 
232 // get double value
233 double SettingParameterValues::getDoubleValue( const char* name, const double df ) {
234  // value
235  const char* value = getParameterValue( name );
236  const double ret = todouble( value, df );
237 
238  return ret;
239 }
240 
241 // get sub parameter values object
243  // parameters
244  if( val == NULL ) {
245  val = getParameterValue( key );
246  }
247 
248  // key, value
249  std::string k = NVL( key, "" );
250  if( val == NULL ) {
251  val = getParameterValue( key );
252  }
253  std::string v = NVL( val, "" );
254 
255  // search
256  int itemIdx = -1;
257  for( unsigned int i = 0; i < m_itemValues.size() && itemIdx < 0; i++ ) {
258  if( k.compare( m_itemValues[ i ].first.key ) == 0
259  && v.compare( m_itemValues[ i ].first.value ) == 0 ) {
260  itemIdx = (int)i;
261  }
262  }
263 
264  // create new sub settings
265  if( itemIdx < 0 ) {
266  itemIdx = (int)m_itemValues.size();
268 
269  m_itemValues.resize( m_itemValues.size() + 1 );
270 
271  m_itemValues.back().first.key = k;
272  m_itemValues.back().first.value = v;
273  m_itemValues.back().second = subVals;
274  }
275 
276  return m_itemValues[ itemIdx ].second;
277 }
278 
279 // check sub parameters
280 bool SettingParameterValues::hasSubParameterValues( const char* key, const char* val ) {
281  // parameters
282  if( val == NULL ) {
283  val = getParameterValue( key );
284  }
285 
286  // key, value
287  std::string k = NVL( key, "" );
288  if( val == NULL ) {
289  val = getParameterValue( key );
290  }
291  std::string v = NVL( val, "" );
292 
293  // search
294  int itemIdx = -1;
295  for( unsigned int i = 0; i < m_itemValues.size() && itemIdx < 0; i++ ) {
296  if( k.compare( m_itemValues[ i ].first.key ) == 0
297  && v.compare( m_itemValues[ i ].first.value ) == 0 ) {
298  itemIdx = (int)i;
299  }
300  }
301 
302  return ( itemIdx >= 0 );
303 }
304 
305 // write all child parameters
308  kome::core::Properties& props,
309  const char* prefix
310 ) {
311  // prefix
312  std::string p = NVL( prefix, "" );
313 
314  // write
315  for( unsigned int i = 0; i < settings.getNumberOfParameters(); i++ ) {
316  std::string name = settings.getParameterName( i );
317  std::string value = settings.getParameterValue( i );
318  std::string propName = name;
319  if( !p.empty() ) {
320  propName = FMT( "%s : %s", p.c_str(), name.c_str() );
321  }
322 
323  props.setValue( propName.c_str(), value.c_str() );
324 
325  // child
326  if( settings.hasSubParameterValues( name.c_str(), value.c_str() ) ) {
327  kome::objects::SettingParameterValues* child = settings.getSubParameterValues( name.c_str(), value.c_str() );
328  writeAllChildParameters( *child, props, propName.c_str() );
329  }
330  }
331 }
SettingParameterValues & operator=(const SettingParameterValues &other)
the definition of assignment operator
double getDoubleValue(const unsigned int index, const double df)
gets double value
void setValue(const unsigned int index, const char *value)
sets parameter value
bool getBoolValue(const unsigned int index, const bool df)
gets boolean value
setting parameter values management class
bool hasSubParameterValues(const char *key, const char *val=NULL)
check the specified parameter has sub parameters
keys and values management class
Definition: Properties.h:29
void setValue(const char *key, const char *value)
sets parameter value
Definition: Properties.cpp:39
bool tobool(const char *s, bool dfVal)
get true or false from character string.
int getIntValue(const unsigned int index, const int df)
gets integer value
int toint(const char *s, const int radix, const int dfval)
convert string into integer
interfaces of SettingParameterValues class
#define NVL(checkVal, replaceVal)
Definition: CoreMacros.h:99
std::vector< std::pair< SubSettingsKey, SettingParameterValues * > > m_itemValues
#define NULL
Definition: CoreMacros.h:18
int getIndex(const char *name)
gets parameter index from name
const char * getParameterValue(const unsigned int index)
gets parameter value
const char * getParameterName(const unsigned int index)
gets parameter name
unsigned int getNumberOfParameters()
gets the number of parameters
double todouble(const char *s, const double dfval)
convert string into double
static void writeAllChildParameters(kome::objects::SettingParameterValues &settings, kome::core::Properties &props, const char *prefix)
write all child parameters
std::vector< std::pair< std::string, std::string > > m_values
SettingParameterValues * getSubParameterValues(const char *key, const char *val=NULL)
gets sub parameter values