All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Variant.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2014-2014 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file Variant.cpp
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 // TerraLib
29 #include "Variant.h"
30 #include "EnumUtils.h"
31 
32 // STL
33 #include <sstream>
34 #include <string>
35 #include <exception>
36 
38  m_sValue("unknown"),
39  m_dValue(-1000.),
40  m_iValue(-1000),
41  m_lValue(-1000),
42  m_fValue(-1000.),
43  m_bValue(false),
44  m_type(DataTypeNone),
45  m_null(true)
46 {
47 
48 }
49 
51  m_sValue("unknown"),
52  m_dValue(-1000.),
53  m_iValue(-1000),
54  m_lValue(-1000),
55  m_fValue(-1000.),
56  m_bValue(false),
57  m_type(type),
58  m_null(true)
59 {
60  if(valueCopy)
61  {
62  convertValue(valueCopy);
63  }
64 }
65 
67 {
68 
69 }
70 
72 {
73  return m_type;
74 }
75 
76 void te::layout::Variant::convertValue( const void* valueCopy )
77 {
78  void* value = const_cast<void*>(valueCopy);
79  bool null = true;
80 
81  //init variables
82  std::string* sp = 0;
83  double* dValue = 0;
84  float* fValue = 0;
85  long* lValue = 0;
86  int* iValue = 0;
87  bool* bValue = 0;
88  te::color::RGBAColor* colorValue = 0;
89 
90  try
91  {
92  switch(m_type)
93  {
94  case DataTypeString:
95  // Cast it back to a string pointer.
96  sp = static_cast<std::string*>(value);
97  if(sp)
98  {
99  null = false;
100  m_sValue = *sp;
101  //return throw_exception("Cast failure! Wrong type.");
102  }
103  break;
104  case DataTypeStringList:
105  // Cast it back to a string pointer.
106  sp = static_cast<std::string*>(value);
107  if(sp)
108  {
109  null = false;
110  m_sValue = *sp;
111  }
112  break;
113  case DataTypeDouble:
114  if(checkNumberAsString(value))
115  {
116  // Cast it back to a string pointer.
117  sp = static_cast<std::string*>(value);
118  if(sp)
119  {
120  m_dValue = string2Double(*sp);
121  null = false;
122  }
123  }
124  else
125  {
126  dValue = static_cast<double*>(value);
127  if(dValue)
128  {
129  null = false;
130  m_dValue = *dValue;
131  }
132  }
133  break;
134  case DataTypeFloat:
135  if(checkNumberAsString(value))
136  {
137  // Cast it back to a string pointer.
138  sp = static_cast<std::string*>(value);
139  if(sp)
140  {
141  m_fValue = string2Float(*sp);
142  null = false;
143  }
144  }
145  else
146  {
147  fValue = static_cast<float*>(value);
148  if(fValue)
149  {
150  null = false;
151  m_fValue = *fValue;
152  }
153  }
154  break;
155  case DataTypeLong:
156  if(checkNumberAsString(value))
157  {
158  // Cast it back to a string pointer.
159  sp = static_cast<std::string*>(value);
160  if(sp)
161  {
162  m_lValue = string2Long(*sp);
163  null = false;
164  }
165  }
166  else
167  {
168  lValue = static_cast<long*>(value);
169  if(lValue)
170  {
171  null = false;
172  m_lValue = *lValue;
173  }
174  }
175  break;
176  case DataTypeInt:
177  if(checkNumberAsString(value))
178  {
179  // Cast it back to a string pointer.
180  sp = static_cast<std::string*>(value);
181  if(sp)
182  {
183  m_iValue = string2Int(*sp);
184  null = false;
185  }
186  }
187  else
188  {
189  iValue = static_cast<int*>(value);
190  if(iValue)
191  {
192  null = false;
193  m_iValue = *iValue;
194  }
195  }
196  break;
197  case DataTypeBool:
198  bValue = static_cast<bool*>(value);
199  if(bValue)
200  {
201  null = false;
202  m_bValue = *bValue;
203  }
204  break;
206  // Cast it back to a string pointer.
207  sp = static_cast<std::string*>(value);
208  if(sp)
209  {
210  null = false;
211  m_sValue = *sp;
212  }
213  break;
214  case DataTypeColor:
215  // Cast it back to a string pointer.
216  colorValue = static_cast<te::color::RGBAColor*>(value);
217  if(colorValue)
218  {
219  null = false;
220  m_colorValue = *colorValue;
221  }
222  break;
223  default:
224  null = true;
225  break;
226  }
227  }
228  catch (std::exception const& e)
229  {
230  std::string s_type = te::layout::getLayoutPropertyDataType(m_type);
231  std::cerr << e.what() << "Failed - te::layout::Variant: convert to " << s_type << std::endl;
232  }
233 
234  m_null = null;
235 }
236 
238 {
239  return m_sValue;
240 }
241 
243 {
244  return m_dValue;
245 }
246 
248 {
249  return m_iValue;
250 }
251 
253 {
254  return m_lValue;
255 }
256 
258 {
259  return m_fValue;
260 }
261 
263 {
264  return m_bValue;
265 }
266 
268 {
269  return m_colorValue;
270 }
271 
273 {
274  return m_null;
275 }
276 
278 {
279  m_sValue = "unknown";
280  m_dValue = -1000.;
281  m_iValue = -1000;
282  m_lValue = -1000;
283  m_fValue = -1000.;
284  m_bValue = false;
285  m_type = DataTypeNone;
286  m_null = true;
287 }
288 
290 {
291  std::string s_convert;
292  std::stringstream ss;//create a stringstream
293 
294  if(m_null)
295  return s_convert;
296 
297  if(m_type == DataTypeNone)
298  return s_convert;
299 
300  if(m_sValue.compare("unknown") != 0)
301  {
302  s_convert = m_sValue;
303  }
304  else if(m_dValue != -1000.)
305  {
306  ss << m_dValue;//add number to the stream
307  s_convert = ss.str();
308  }
309  else if(m_iValue != -1000)
310  {
311  ss << m_iValue;//add number to the stream
312  s_convert = ss.str();
313  }
314  else if(m_lValue != -1000)
315  {
316  ss << m_iValue;//add number to the stream
317  s_convert = ss.str();
318  }
319  else if(m_fValue != -1000.)
320  {
321  ss << m_fValue;//add number to the stream
322  s_convert = ss.str();
323  }
324  else
325  {
326  if(m_bValue)
327  {
328  s_convert = "true";
329  }
330  else
331  {
332  s_convert = "false";
333  }
334  }
335 
336  return s_convert;
337 }
338 
339 bool te::layout::Variant::checkNumberAsString( const void* valueCopy )
340 {
341  void* value = const_cast<void*>(valueCopy);
342 
343  std::string* sp = 0;
344  bool result = false;
345 
346  try
347  {
348  // Cast it back to a string pointer.
349  sp = static_cast<std::string*>(value);
350 
351  if(sp)
352  {
353  std::string res = (std::string)*sp;
354 
355  /* Verification because the result of static_cast<std::string*>
356  may be garbage, if the value is not a string. */
357  std::istringstream buffer(res);
358  int value = 0;
359  buffer >> value;
360 
361  if (buffer.fail())
362  {
363  result = false;
364  }
365  else
366  {
367  result = true;
368  }
369  }
370  }
371  catch(const std::exception& e)
372  {
373  result = false;
374  }
375 
376  return result;
377 }
378 
379 double te::layout::Variant::string2Double( std::string str )
380 {
381  // Convert a string representation of a number into a double value.
382 
383  double result;
384 
385  // Get rid of any trailing whitespace
386  str.erase( str.find_last_not_of( " \f\n\r\t\v" ) + 1 );
387 
388  // Read it into the target type
389  std::istringstream ss( str );
390  ss >> result;
391 
392  // Check to see that there is nothing left over
393  if (!ss.eof())
394  {
395  throw std::runtime_error("Failed: convert string to double.");
396  }
397 
398  return result;
399 }
400 
401 int te::layout::Variant::string2Int( std::string str )
402 {
403  // Convert a string representation of a number into a int value.
404 
405  int result;
406 
407  // Get rid of any trailing whitespace
408  str.erase( str.find_last_not_of( " \f\n\r\t\v" ) + 1 );
409 
410  // Read it into the target type
411  std::istringstream ss( str );
412  ss >> result;
413 
414  // Check to see that there is nothing left over
415  if (!ss.eof())
416  {
417  throw std::runtime_error("Failed: convert string to int.");
418  }
419 
420  return result;
421 }
422 
423 float te::layout::Variant::string2Float( std::string str )
424 {
425  // Convert a string representation of a number into a floating point value.
426 
427  float result;
428 
429  // Get rid of any trailing whitespace
430  str.erase( str.find_last_not_of( " \f\n\r\t\v" ) + 1 );
431 
432  // Read it into the target type
433  std::istringstream ss( str );
434  ss >> result;
435 
436  // Check to see that there is nothing left over
437  if (!ss.eof())
438  {
439  throw std::runtime_error("Failed: convert string to float.");
440  }
441 
442  return result;
443 }
444 
445 long te::layout::Variant::string2Long( std::string str )
446 {
447  // Convert a string representation of a number into a long value.
448 
449  long result;
450 
451  // Get rid of any trailing whitespace
452  str.erase( str.find_last_not_of( " \f\n\r\t\v" ) + 1 );
453 
454  // Read it into the target type
455  std::istringstream ss( str );
456  ss >> result;
457 
458  // Check to see that there is nothing left over
459  if (!ss.eof())
460  {
461  throw std::runtime_error("Failed: convert string to long.");
462  }
463 
464  return result;
465 }
466 
std::string convertToString()
Definition: Variant.cpp:289
virtual ~Variant()
Definition: Variant.cpp:66
void convertValue(const void *valueCopy)
Definition: Variant.cpp:76
float string2Float(std::string str)
Definition: Variant.cpp:423
double string2Double(std::string str)
Definition: Variant.cpp:379
std::string getLayoutPropertyDataType(int enumVal)
Definition: EnumUtils.cpp:119
int string2Int(std::string str)
Definition: Variant.cpp:401
LayoutPropertyDataType getType()
Definition: Variant.cpp:71
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
long string2Long(std::string str)
Definition: Variant.cpp:445
std::string toString()
Definition: Variant.cpp:237
te::color::RGBAColor toColor()
Definition: Variant.cpp:267
LayoutPropertyDataType
Enum LayoutPropertyDataType.
Definition: AbstractType.h:131
bool checkNumberAsString(const void *valueCopy)
Definition: Variant.cpp:339