All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Variant.h
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.h
22 
23  \brief Class acts like a union for some C++/TerraLib5 data types. Responsible for storing the value.
24  Any data type, not included in the convertValue method in this class, it will be by default "std::string".
25  Storing value types:
26 
27  - std::string
28  - double
29  - int
30  - long
31  - float
32  - bool
33  - te::color::RGBAColor
34  - te::layout::Font
35 
36  \ingroup layout
37 */
38 
39 #ifndef __TERRALIB_LAYOUT_INTERNAL_VARIANT_H
40 #define __TERRALIB_LAYOUT_INTERNAL_VARIANT_H
41 
42 // TerraLib
43 #include "../enum/AbstractType.h"
44 #include "../../../color/RGBAColor.h"
45 #include "../Font.h"
46 #include "../enum/EnumType.h"
47 #include "../Config.h"
48 
49 // STL
50 #include <string>
51 #include <map>
52 #include <vector>
53 #include <iostream>
54 
55 // Boost
56 #include <boost/property_tree/ptree.hpp>
57 
58 using namespace std;
59 
60 namespace te
61 {
62  namespace layout
63  {
64  /*!
65  \brief Class acts like a union for some C++/TerraLib5 data types. Responsible for storing the value.
66  Any data type, not included in the convertValue method in this class, it will be by default "std::string".
67  Storing value types:
68 
69  - std::string
70  - double
71  - int
72  - long
73  - float
74  - bool
75  - te::color::RGBAColor
76  - te::layout::Font
77 
78  \ingroup layout
79  */
81  {
82  public:
83 
84  /*!
85  \brief Constructor
86  */
87  Variant();
88 
89  /*!
90  \brief Constructor
91 
92  \type data type
93  \valueCopy value pointer (real data)
94  */
95  Variant(EnumType* type, const void* valueCopy);
96 
97  /*!
98  \brief Destructor
99  */
100  virtual ~Variant();
101 
102  /*!
103  \brief Stores a copy of value.
104 
105  \param value copies the value to be stored
106  \param type data type
107  */
108  template <typename ValueType>
109  void setValue(ValueType value, EnumType* type);
110 
111  /*!
112  \brief
113  */
114  virtual void fromPtree(boost::property_tree::ptree tree, EnumType* type);
115 
116  /*!
117  \brief Returns data type of this object.
118  */
119  EnumType* getType();
120 
121  /*!
122  \brief Return true if value is not of common C++ data type, false otherwise.
123 
124  \param true if value is not of common C++ data type, false otherwise
125  */
126  virtual bool isComplex();
127 
128  /*!
129  \brief Returns the value of string type. (The setValue method received a string)
130 
131  \return value of string type
132  */
133  std::string toString();
134 
135  /*!
136  \brief Returns the value of double type. (The setValue method received a double)
137 
138  \return value of double type
139  */
140  double toDouble();
141 
142  /*!
143  \brief Returns the value of int type. (The setValue method received a int)
144 
145  \return value of int type
146  */
147  int toInt();
148 
149  /*!
150  \brief Returns the value of long type. (The setValue method received a long)
151 
152  \return value of long type
153  */
154  long toLong();
155 
156  /*!
157  \brief Returns the value of float type. (The setValue method received a float)
158 
159  \return value of float type
160  */
161  float toFloat();
162 
163  /*!
164  \brief Returns the value of boolean type. (The setValue method received a boolean)
165 
166  \return value of boolean type
167  */
168  bool toBool();
169 
170  /*!
171  \brief Returns the value of te::color::RGBAColor type. (The setValue method received a te::color::RGBAColor). Complex type.
172 
173  \return value of te::color::RGBAColor type
174  */
175  te::color::RGBAColor toColor();
176 
177  /*!
178  \brief Returns the value of te::layout::Font type. (The setValue method received a te::layout::Font). Complex type.
179 
180  \return value of te::layout::Font type
181  */
182  Font toFont();
183 
184  /*!
185  \brief Converts the value to a string.
186 
187  \return Value as a string
188  */
189  virtual std::string convertToString();
190 
191  /*!
192  \brief Returns true if no value has been set, false otherwise.
193 
194  \return true if no value has been set, false otherwise
195  */
196  bool isNull();
197 
198  /*!
199  \brief Reset state of object. Null state.
200  */
201  virtual void clear();
202 
203  bool operator ==(const Variant& other);
204  bool operator !=(const Variant& other);
205 
206  protected:
207 
208  /*!
209  \brief Stores a copy of value.
210 
211  \param v this object
212  \param value copies the value to be stored
213  \param type data type
214  */
215  template <typename ValueType>
216  void variantSetValue(Variant &v, const ValueType& value, EnumType* type);
217 
218  /*!
219  \brief Discovers the type of the value and sets for the corresponding attribute (storage).
220  Any type of data, not included in this class, it will be by default "std::string"
221 
222  \param valueCopy pointer of the value to be stored
223  */
224  virtual void convertValue(const void* valueCopy);
225 
226  /*!
227  \brief Convert a string representation of a number into a double value.
228 
229  \param str string representation of a number
230  \return double value
231  */
232  virtual double string2Double(std::string str);
233 
234  /*!
235  \brief Convert a string representation of a number into a int value.
236 
237  \param str string representation of a number
238  \return int value
239  */
240  virtual int string2Int(std::string str);
241 
242  /*!
243  \brief Convert a string representation of a number into a float value.
244 
245  \param str string representation of a number
246  \return float value
247  */
248  virtual float string2Float(std::string str);
249 
250  /*!
251  \brief Convert a string representation of a number into a long value.
252 
253  \param str string representation of a number
254  \return long value
255  */
256  virtual long string2Long(std::string str);
257 
258  /*!
259  \brief Convert a int value into a string representation of a number.
260 
261  \param value int value
262  \return string representation of a number
263  */
264  virtual std::string toString(int value);
265 
266  /*!
267  \brief Convert a string value into a boolean representation of a string. Ex.: true, false.
268 
269  \param str string value
270  \return boolean representation of a string. Ex.: true, false
271  */
272  virtual bool toBool(std::string str);
273 
274  std::string m_sValue; //!< value of string type
275  double m_dValue; //!< value of double type
276  int m_iValue; //!< value of int type
277  long m_lValue; //!< value of long type
278  float m_fValue; //!< value of float type
279  bool m_bValue; //!< value of boolean type
280  te::color::RGBAColor m_colorValue; //!< value of te::color::RGBAColor type
281  Font m_fontValue; //!< value of te::layout::Font type
282  EnumType* m_type; //!< data type of this object
283  bool m_null; //!< true if no value has been set, false otherwise
284  bool m_complex; //!< true if value is not of common C++ data type, false otherwise
285  };
286 
287  template<typename ValueType>
288  inline void te::layout::Variant::setValue( ValueType value, EnumType* type )
289  {
290  ValueType v = value;
291  variantSetValue(*this, v, type);
292  }
293 
294  /* Is still necessary to revise this method.*/
295  template <typename ValueType>
297  const ValueType& value, EnumType* type )
298  {
299  v = Variant(type, &value);
300  }
301 
302  inline bool te::layout::Variant::operator ==(const Variant& other)
303  {
304  Variant& otherProp = const_cast<Variant&>(other);
305 
306  if(getType() == otherProp.getType())
307  {
308  if(m_sValue == otherProp.toString() &&
309  m_dValue == otherProp.toDouble() &&
310  m_iValue == otherProp.toInt() &&
311  m_lValue == otherProp.toLong() &&
312  m_fValue == otherProp.toFloat() &&
313  m_bValue == otherProp.toBool() &&
314  m_colorValue == otherProp.toColor() /*&&
315  m_fontValue == otherProp.toFont()*/)
316  {
317  return true;
318  }
319  }
320  return false;
321  }
322 
323  inline bool te::layout::Variant::operator !=(const Variant& other)
324  {
325  Variant& otherProp = const_cast<Variant&>(other);
326 
327  if(getType() != otherProp.getType())
328  {
329  return true;
330  }
331 
332  if(getType() == otherProp.getType())
333  {
334  if(m_sValue != otherProp.toString() ||
335  m_dValue != otherProp.toDouble() ||
336  m_iValue != otherProp.toInt() ||
337  m_lValue != otherProp.toLong() ||
338  m_fValue != otherProp.toFloat() ||
339  m_colorValue != otherProp.toColor() /*||
340  m_fontValue != otherProp.toFont()*/)
341  {
342  return true;
343  }
344  else
345  {
346  if(m_bValue != otherProp.toBool())
347  {
348  return true;
349  }
350  else
351  {
352  return false;
353  }
354  }
355  }
356  return true;
357  }
358  }
359 }
360 
361 #endif
Class specifies a font.
Definition: Font.h:46
float m_fValue
value of float type
Definition: Variant.h:278
EnumType * getType()
Returns data type of this object.
Definition: Variant.cpp:77
#define TELAYOUTEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:99
bool operator!=(const Variant &other)
Definition: Variant.h:323
te::da::Expression * operator==(const te::da::Expression &e1, const te::da::Expression &e2)
Definition: Expression.cpp:38
bool m_complex
true if value is not of common C++ data type, false otherwise
Definition: Variant.h:284
float toFloat()
Returns the value of float type. (The setValue method received a float)
Definition: Variant.cpp:331
EnumType * m_type
data type of this object
Definition: Variant.h:282
te::color::RGBAColor m_colorValue
value of te::color::RGBAColor type
Definition: Variant.h:280
Class acts like a union for some C++/TerraLib5 data types. Responsible for storing the value...
Definition: Variant.h:80
te::da::Expression * operator!=(const te::da::Expression &e1, const te::da::Expression &e2)
Definition: Expression.cpp:43
std::string m_sValue
value of string type
Definition: Variant.h:274
double toDouble()
Returns the value of double type. (The setValue method received a double)
Definition: Variant.cpp:316
bool m_null
true if no value has been set, false otherwise
Definition: Variant.h:283
void variantSetValue(Variant &v, const ValueType &value, EnumType *type)
Stores a copy of value.
Definition: Variant.h:296
bool operator==(const Variant &other)
Definition: Variant.h:302
double m_dValue
value of double type
Definition: Variant.h:275
Class that represents the value of an enumeration. An enumeration is made of "1..n" objects EnumType...
Definition: EnumType.h:48
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
std::string toString()
Returns the value of string type. (The setValue method received a string)
Definition: Variant.cpp:311
long m_lValue
value of long type
Definition: Variant.h:277
Font m_fontValue
value of te::layout::Font type
Definition: Variant.h:281
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
bool m_bValue
value of boolean type
Definition: Variant.h:279
int m_iValue
value of int type
Definition: Variant.h:276
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
void setValue(ValueType value, EnumType *type)
Stores a copy of value.
Definition: Variant.h:288