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) 2008 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 "../enum/Enums.h"
31 
32 // STL
33 #include <sstream>
34 #include <string>
35 #include <exception>
36 #include <stdexcept>
37 #include <cctype>
38 #include <iostream>
39 #include <stdlib.h>
40 
42  m_sValue("unknown"),
43  m_dValue(-1000.),
44  m_iValue(-1000),
45  m_lValue(-1000),
46  m_fValue(-1000.),
47  m_bValue(false),
48  m_type(0),
49  m_null(true),
50  m_complex(false)
51 {
52  m_type = Enums::getInstance().getEnumDataType()->getDataTypeNone();
53 }
54 
55 te::layout::Variant::Variant(te::layout::EnumType* type, const void* valueCopy) :
56  m_sValue("unknown"),
57  m_dValue(-1000.),
58  m_iValue(-1000),
59  m_lValue(-1000),
60  m_fValue(-1000.),
61  m_bValue(false),
62  m_type(type),
63  m_null(true),
64  m_complex(false)
65 {
66  if(valueCopy)
67  {
68  convertValue(valueCopy);
69  }
70 }
71 
73 {
74 
75 }
76 
78 {
79  return m_type;
80 }
81 
82 void te::layout::Variant::convertValue( const void* valueCopy )
83 {
84  void* value = const_cast<void*>(valueCopy);
85  bool null = true;
86 
87  //init variables
88  std::string* sp = 0;
89  double* dValue = 0;
90  float* fValue = 0;
91  long* lValue = 0;
92  int* iValue = 0;
93  bool* bValue = 0;
94  te::color::RGBAColor* colorValue = 0;
95  Font* fontValue = 0;
96 
98 
99  if(!m_type || !dataType)
100  {
101  return;
102  }
103 
104  try
105  {
106  if(m_type == dataType->getDataTypeString())
107  {
108  // Cast it back to a string pointer.
109  sp = static_cast<std::string*>(value);
110  if(sp)
111  {
112  null = false;
113  m_sValue = *sp;
114  }
115  }
116  else if(m_type == dataType->getDataTypeStringList())
117  {
118  // Cast it back to a string pointer.
119  sp = static_cast<std::string*>(value);
120  if(sp)
121  {
122  null = false;
123  m_sValue = *sp;
124  }
125  }
126  else if(m_type == dataType->getDataTypeDouble())
127  {
128  dValue = static_cast<double*>(value);
129  if(dValue)
130  {
131  null = false;
132  m_dValue = *dValue;
133  }
134  }
135  else if(m_type == dataType->getDataTypeFloat())
136  {
137  fValue = static_cast<float*>(value);
138  if(fValue)
139  {
140  null = false;
141  m_fValue = *fValue;
142  }
143  }
144  else if(m_type == dataType->getDataTypeLong())
145  {
146  lValue = static_cast<long*>(value);
147  if(lValue)
148  {
149  null = false;
150  m_lValue = *lValue;
151  }
152 
153  }
154  else if(m_type == dataType->getDataTypeInt())
155  {
156  iValue = static_cast<int*>(value);
157  if(iValue)
158  {
159  null = false;
160  m_iValue = *iValue;
161  }
162  }
163  else if(m_type == dataType->getDataTypeBool())
164  {
165  bValue = static_cast<bool*>(value);
166  if(bValue)
167  {
168  null = false;
169  m_bValue = *bValue;
170  }
171  }
172  else if(m_type == dataType->getDataTypeColor())
173  {
174  // Cast it back to a string pointer.
175  colorValue = static_cast<te::color::RGBAColor*>(value);
176  if(colorValue)
177  {
178  null = false;
179  m_colorValue = *colorValue;
180  m_complex = true;
181  }
182  }
183  else if(m_type == dataType->getDataTypeFont())
184  {
185  // Cast it back to a string pointer.
186  fontValue = static_cast<Font*>(value);
187  if(fontValue)
188  {
189  null = false;
190  m_fontValue = *fontValue;
191  m_complex = true;
192  }
193  }
194  else // Any remaining data will be by default "std::string"
195  {
196  // Cast it back to a string pointer.
197  sp = static_cast<std::string*>(value);
198  if(sp)
199  {
200  null = false;
201  m_sValue = *sp;
202  }
203  }
204  }
205  catch (std::exception const& e)
206  {
207  std::string s_type = m_type->getName();
208  std::cerr << e.what() << "Failed - te::layout::Variant: convert to " << s_type << std::endl;
209  }
210 
211  m_null = null;
212 }
213 
214 void te::layout::Variant::fromPtree( boost::property_tree::ptree tree, EnumType* type )
215 {
217 
218  bool null = true;
219 
220  if(!dataType)
221  {
222  return;
223  }
224 
225  if(!type)
226  {
227  return;
228  }
229 
230  /* the ptree boost returns data with string type */
231 
232  try
233  {
234  if(type == dataType->getDataTypeString())
235  {
236  m_sValue = tree.data();
237  null = false;
238  }
239  else if(type == dataType->getDataTypeDouble())
240  {
241  m_dValue = std::atof(tree.data().c_str());
242  null = false;
243  }
244  else if(type == dataType->getDataTypeInt())
245  {
246  m_iValue = std::atoi(tree.data().c_str());
247  null = false;
248  }
249  else if(type == dataType->getDataTypeLong())
250  {
251  m_lValue = std::atol(tree.data().c_str());
252  null = false;
253  }
254  else if(type == dataType->getDataTypeFloat())
255  {
256  m_fValue = (float)std::atof(tree.data().c_str());
257  null = false;
258  }
259  else if(type == dataType->getDataTypeBool())
260  {
261  m_bValue = toBool(tree.data());
262  null = false;
263  }
264  else if(type == dataType->getDataTypeColor())
265  {
266  std::string color = tree.data();
267 
268  std::vector<std::string> strings;
269  std::istringstream f(color);
270  std::string s;
271  while (std::getline(f, s, ','))
272  {
273  strings.push_back(s);
274  }
275 
276  if(strings.empty() || strings.size() > 4)
277  return;
278 
279  int r = std::atoi(strings[0].c_str());
280  int g = std::atoi(strings[1].c_str());
281  int b = std::atoi(strings[2].c_str());
282  int a = std::atoi(strings[3].c_str());
283 
284  m_colorValue.setColor(r,g,b,a);
285 
286  m_complex = true;
287  null = false;
288  }
289  else if(type == dataType->getDataTypeFont())
290  {
291  std::string font = tree.data();
292  m_fontValue.fromString(font);
293  m_complex = true;
294  null = false;
295  }
296  else // Any remaining data will be by default "std::string"
297  {
298  m_sValue = tree.data();
299  null = false;
300  }
301  }
302  catch (std::exception const& e)
303  {
304  std::string s_type = type->getName();
305  std::cerr << e.what() << "Failed - te::layout::Variant: convert to " << s_type << std::endl;
306  }
307 
308  m_null = null;
309 }
310 
312 {
313  return m_sValue;
314 }
315 
317 {
318  return m_dValue;
319 }
320 
322 {
323  return m_iValue;
324 }
325 
327 {
328  return m_lValue;
329 }
330 
332 {
333  return m_fValue;
334 }
335 
337 {
338  return m_bValue;
339 }
340 
342 {
343  return m_colorValue;
344 }
345 
347 {
348  return m_fontValue;
349 }
350 
352 {
353  return m_null;
354 }
355 
357 {
358  m_sValue = "unknown";
359  m_dValue = -1000.;
360  m_iValue = -1000;
361  m_lValue = -1000;
362  m_fValue = -1000.;
363  m_bValue = false;
365  m_null = true;
366 }
367 
369 {
370  std::stringstream ss;//create a stringstream
371  std::string s_convert;
372 
373  if(m_null)
374  return s_convert;
375 
377 
378  if(m_type == dataType->getDataTypeNone())
379  return s_convert;
380 
381  if(m_type == dataType->getDataTypeString())
382  {
383  s_convert = m_sValue;
384  }
385  else if(m_type == dataType->getDataTypeDouble())
386  {
387  ss << m_dValue;//add number to the stream
388  s_convert = ss.str();
389  }
390  else if(m_type == dataType->getDataTypeInt())
391  {
392  ss << m_iValue;//add number to the stream
393  s_convert = ss.str();
394  }
395  else if(m_type == dataType->getDataTypeLong())
396  {
397  ss << m_lValue;//add number to the stream
398  s_convert = ss.str();
399  }
400  else if(m_type == dataType->getDataTypeFloat())
401  {
402  ss << m_fValue;//add number to the stream
403  s_convert = ss.str();
404  }
405  else if(m_type == dataType->getDataTypeColor())
406  {
407  s_convert = toString(m_colorValue.getRed());
408  s_convert += "," + toString(m_colorValue.getGreen());
409  s_convert += "," + toString(m_colorValue.getBlue());
410  s_convert += "," + toString(m_colorValue.getAlpha());
411  }
412  else if(m_type == dataType->getDataTypeFont())
413  {
414  s_convert = m_fontValue.toString();
415  }
416  else if(m_type == dataType->getDataTypeBool())
417  {
418  s_convert = m_bValue ? "true" : "false";
419  }
420 
421  return s_convert;
422 }
423 
424 double te::layout::Variant::string2Double( std::string str )
425 {
426  // Convert a string representation of a number into a double value.
427 
428  double result;
429 
430  // Get rid of any trailing whitespace
431  str.erase( str.find_last_not_of( " \f\n\r\t\v" ) + 1 );
432 
433  // Read it into the target type
434  std::istringstream ss( str );
435  ss >> result;
436 
437  // Check to see that there is nothing left over
438  if (!ss.eof())
439  {
440  throw std::runtime_error("Failed: convert string to double.");
441  }
442 
443  return result;
444 }
445 
446 int te::layout::Variant::string2Int( std::string str )
447 {
448  // Convert a string representation of a number into a int value.
449 
450  int result;
451 
452  // Get rid of any trailing whitespace
453  str.erase( str.find_last_not_of( " \f\n\r\t\v" ) + 1 );
454 
455  // Read it into the target type
456  std::istringstream ss( str );
457  ss >> result;
458 
459  // Check to see that there is nothing left over
460  if (!ss.eof())
461  {
462  throw std::runtime_error("Failed: convert string to int.");
463  }
464 
465  return result;
466 }
467 
468 float te::layout::Variant::string2Float( std::string str )
469 {
470  // Convert a string representation of a number into a floating point value.
471 
472  float result;
473 
474  // Get rid of any trailing whitespace
475  str.erase( str.find_last_not_of( " \f\n\r\t\v" ) + 1 );
476 
477  // Read it into the target type
478  std::istringstream ss( str );
479  ss >> result;
480 
481  // Check to see that there is nothing left over
482  if (!ss.eof())
483  {
484  throw std::runtime_error("Failed: convert string to float.");
485  }
486 
487  return result;
488 }
489 
490 long te::layout::Variant::string2Long( std::string str )
491 {
492  // Convert a string representation of a number into a long value.
493 
494  long result;
495 
496  // Get rid of any trailing whitespace
497  str.erase( str.find_last_not_of( " \f\n\r\t\v" ) + 1 );
498 
499  // Read it into the target type
500  std::istringstream ss( str );
501  ss >> result;
502 
503  // Check to see that there is nothing left over
504  if (!ss.eof())
505  {
506  throw std::runtime_error("Failed: convert string to long.");
507  }
508 
509  return result;
510 }
511 
513 {
514  return m_complex;
515 }
516 
517 std::string te::layout::Variant::toString( int value )
518 {
519  std::stringstream ss;//create a stringstream
520  ss << value;//add number to the stream
521 
522  return ss.str();
523 }
524 
525 bool te::layout::Variant::toBool( std::string str )
526 {
527  if(str.compare("true") == 0)
528  {
529  return true;
530  }
531  else
532  {
533  return false;
534  }
535 }
536 
537 
538 
Class specifies a font.
Definition: Font.h:46
EnumType * getType()
Returns data type of this object.
Definition: Variant.cpp:77
virtual EnumDataType * getEnumDataType()
Returns data type enumeration.
Definition: Enums.cpp:52
std::string getName()
Returns name.
Definition: EnumType.cpp:54
virtual EnumType * getDataTypeBool() const
Returns value that represents type bool belonging to enumeration.
virtual std::string convertToString()
Converts the value to a string.
Definition: Variant.cpp:368
virtual EnumType * getDataTypeDouble() const
Returns value that represents type double belonging to enumeration.
virtual EnumType * getDataTypeInt() const
Returns value that represents type integer belonging to enumeration.
virtual EnumType * getDataTypeNone() const
Returns value that represents type none belonging to enumeration.
virtual bool isComplex()
Return true if value is not of common C++ data type, false otherwise.
Definition: Variant.cpp:512
Class acts like a union for some C++/TerraLib5 data types. Responsible for storing the value...
Variant()
Constructor.
Definition: Variant.cpp:41
float toFloat()
Returns the value of float type. (The setValue method received a float)
Definition: Variant.cpp:331
virtual ~Variant()
Destructor.
Definition: Variant.cpp:72
virtual void convertValue(const void *valueCopy)
Discovers the type of the value and sets for the corresponding attribute (storage). Any type of data, not included in this class, it will be by default "std::string".
Definition: Variant.cpp:82
Font toFont()
Returns the value of te::layout::Font type. (The setValue method received a te::layout::Font). Complex type.
Definition: Variant.cpp:346
EnumType * m_type
data type of this object
Definition: Variant.h:282
virtual void fromPtree(boost::property_tree::ptree tree, EnumType *type)
Definition: Variant.cpp:214
static Enums & getInstance()
It returns a reference to the singleton instance.
virtual float string2Float(std::string str)
Convert a string representation of a number into a float value.
Definition: Variant.cpp:468
virtual EnumType * getDataTypeFloat() const
Returns value that represents type float belonging to enumeration.
Class to represent a data type enumeration. Ex.: int, double, bool, te::color::RGBAColor (color)...
Definition: EnumDataType.h:48
virtual void clear()
Reset state of object. Null state.
Definition: Variant.cpp:356
bool isNull()
Returns true if no value has been set, false otherwise.
Definition: Variant.cpp:351
virtual double string2Double(std::string str)
Convert a string representation of a number into a double value.
Definition: Variant.cpp:424
virtual EnumType * getDataTypeColor() const
Returns value that represents type te::color::RGBAColor** (color) belonging to enumeration.
virtual int string2Int(std::string str)
Convert a string representation of a number into a int value.
Definition: Variant.cpp:446
double toDouble()
Returns the value of double type. (The setValue method received a double)
Definition: Variant.cpp:316
virtual EnumType * getDataTypeLong() const
Returns value that represents type long belonging to enumeration.
Class that represents the value of an enumeration. An enumeration is made of "1..n" objects EnumType...
Definition: EnumType.h:48
virtual EnumType * getDataTypeFont() const
Returns value that represents type Font belonging to enumeration.
virtual EnumType * getDataTypeStringList() const
Returns value that represents type StringList (string) belonging to enumeration.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
virtual long string2Long(std::string str)
Convert a string representation of a number into a long value.
Definition: Variant.cpp:490
std::string toString()
Returns the value of string type. (The setValue method received a string)
Definition: Variant.cpp:311
te::color::RGBAColor toColor()
Returns the value of te::color::RGBAColor type. (The setValue method received a te::color::RGBAColor)...
Definition: Variant.cpp:341
long toLong()
Returns the value of long type. (The setValue method received a long)
Definition: Variant.cpp:326
virtual EnumType * getDataTypeString() const
Returns value that represents type string belonging to enumeration.
int toInt()
Returns the value of int type. (The setValue method received a int)
Definition: Variant.cpp:321
bool toBool()
Returns the value of boolean type. (The setValue method received a boolean)
Definition: Variant.cpp:336