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) 2001-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.h
22 
23  \brief
24 
25  \ingroup layout
26 */
27 
28 #ifndef __TERRALIB_LAYOUT_INTERNAL_VARIANT_H
29 #define __TERRALIB_LAYOUT_INTERNAL_VARIANT_H
30 
31 // TerraLib
32 #include "AbstractType.h"
33 #include "../../../../color/RGBAColor.h"
34 
35 // STL
36 #include <string>
37 #include <map>
38 #include <vector>
39 #include <iostream>
40 using namespace std;
41 
42 namespace te
43 {
44  namespace layout
45  {
46  class Variant
47  {
48  public:
49 
50  Variant();
51  Variant(LayoutPropertyDataType type, const void* valueCopy);
52  virtual ~Variant();
53 
54  template <typename ValueType>
55  void setValue(typename ValueType value, LayoutPropertyDataType type);
56 
57  LayoutPropertyDataType getType();
58 
59  std::string toString();
60  double toDouble();
61  int toInt();
62  long toLong();
63  float toFloat();
64  bool toBool();
65  te::color::RGBAColor toColor();
66 
67  std::string convertToString();
68 
69  bool isNull();
70 
71  void clear();
72 
73  bool operator ==(const Variant& other);
74  bool operator !=(const Variant& other);
75 
76  protected:
77 
78  template <typename ValueType>
79  void variantSetValue(Variant &v, const typename ValueType& value, LayoutPropertyDataType type);
80 
81  void convertValue(const void* valueCopy);
82 
83  /* Check if a value passed, of type DataTypeInt and etc, is a std::string.
84  Ex.: value returned by a json file (boost). */
85  bool checkNumberAsString(const void* valueCopy);
86 
87  double string2Double(std::string str);
88 
89  int string2Int(std::string str);
90 
91  float string2Float(std::string str);
92 
93  long string2Long(std::string str);
94 
95  std::string m_sValue;
96  double m_dValue;
97  int m_iValue;
98  long m_lValue;
99  float m_fValue;
100  bool m_bValue;
103  bool m_null;
104  };
105 
106  template<typename ValueType>
107  inline void te::layout::Variant::setValue( typename ValueType value, LayoutPropertyDataType type )
108  {
109  typename ValueType v = value;
110  variantSetValue(*this, v, type);
111  }
112 
113  /* Is still necessary to revise this method.*/
114  template <typename ValueType>
116  const typename ValueType& value, LayoutPropertyDataType type )
117  {
118  v = Variant(type, &value);
119  }
120 
121  inline bool te::layout::Variant::operator ==(const Variant& other)
122  {
123  Variant& otherProp = const_cast<Variant&>(other);
124 
125  if(getType() == otherProp.getType())
126  {
127  if(m_sValue == otherProp.toString() &&
128  m_dValue == otherProp.toDouble() &&
129  m_iValue == toInt() &&
130  m_lValue == toLong() &&
131  m_fValue == toFloat() &&
132  m_bValue == toBool())
133  {
134  return true;
135  }
136  }
137  return false;
138  }
139 
140  inline bool te::layout::Variant::operator !=(const Variant& other)
141  {
142  Variant& otherProp = const_cast<Variant&>(other);
143 
144  if(getType() != otherProp.getType())
145  {
146  return true;
147  }
148 
149  if(getType() == otherProp.getType())
150  {
151  if(m_sValue != otherProp.toString() ||
152  m_dValue != otherProp.toDouble() ||
153  m_iValue != toInt() ||
154  m_lValue != toLong() ||
155  m_fValue != toFloat())
156  {
157  return true;
158  }
159  else
160  {
161  if(m_bValue != otherProp.toBool())
162  {
163  return true;
164  }
165  else
166  {
167  return false;
168  }
169  }
170  }
171  return true;
172  }
173  }
174 }
175 
176 #endif
bool operator!=(const Variant &other)
Definition: Variant.h:140
te::da::Expression * operator==(const te::da::Expression &e1, const te::da::Expression &e2)
Definition: Expression.cpp:38
void setValue(typename ValueType value, LayoutPropertyDataType type)
Definition: Variant.h:107
te::color::RGBAColor m_colorValue
Definition: Variant.h:101
LayoutPropertyDataType m_type
Definition: Variant.h:102
te::da::Expression * operator!=(const te::da::Expression &e1, const te::da::Expression &e2)
Definition: Expression.cpp:43
std::string m_sValue
Definition: Variant.h:95
bool operator==(const Variant &other)
Definition: Variant.h:121
LayoutPropertyDataType getType()
Definition: Variant.cpp:71
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
std::string toString()
Definition: Variant.cpp:237
void variantSetValue(Variant &v, const typename ValueType &value, LayoutPropertyDataType type)
Definition: Variant.h:115
LayoutPropertyDataType
Enum LayoutPropertyDataType.
Definition: AbstractType.h:131