All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
IntegerConverters.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 terralib/datatype/IntegerConverters.cpp
22 
23  \brief A set of function that convert an Integer type to other types.
24 */
25 
26 // TerraLib
27 #include "../common/Translator.h"
28 #include "Enums.h"
29 #include "Exception.h"
30 #include "IntegerConverters.h"
31 #include "SimpleData.h"
32 
33 // Boost
34 #include <boost/lexical_cast.hpp>
35 
36 // Int32
38 {
39  AssertInt32(d);
40  return new Char(boost::lexical_cast<char>(static_cast<const Int32*>(d)->getValue()));
41 }
42 
44 {
45  AssertInt32(d);
46  return new UChar(boost::lexical_cast<unsigned char>(static_cast<const Int32*>(d)->getValue()));
47 }
48 
50 {
51  AssertInt32(d);
52  return new Int16(boost::lexical_cast<int16_t>(static_cast<const Int32*>(d)->getValue()));
53 }
54 
56 {
57  AssertInt32(d);
58  return new UInt16(boost::lexical_cast<uint16_t>(static_cast<const Int32*>(d)->getValue()));
59 }
60 
62 {
63  AssertInt32(d);
64  return new UInt32(static_cast<uint32_t>(static_cast<const Int32*>(d)->getValue()));
65 }
66 
68 {
69  AssertInt32(d);
70  return new Int64(boost::lexical_cast<int64_t>(static_cast<const Int32*>(d)->getValue()));
71 }
72 
74 {
75  AssertInt32(d);
76  return new UInt64(static_cast<uint64_t>(static_cast<const Int32*>(d)->getValue()));
77 }
78 
80 {
81  AssertInt32(d);
82 
83  bool nval = true;
84  if(static_cast<const Int32*>(d)->getValue() == 0)
85  nval = false;
86 
87  return new Boolean(nval);
88 }
89 
91 {
92  AssertInt32(d);
93  return new Float(boost::lexical_cast<float>(static_cast<const Int32*>(d)->getValue()));
94 }
95 
97 {
98  AssertInt32(d);
99  return new Double(boost::lexical_cast<double>(static_cast<const Int32*>(d)->getValue()));
100 }
101 
103 {
104  AssertInt32(d);
105  return new Numeric(boost::lexical_cast<std::string>(static_cast<const Int32*>(d)->getValue()));
106 }
107 
109 {
110  AssertInt32(d);
111  return new String(boost::lexical_cast<std::string>(static_cast<const Int32*>(d)->getValue()));
112 }
113 
114 void te::dt::AssertInt32(AbstractData* d) throw(Exception)
115 {
116  if(d->getTypeCode() != INT32_TYPE)
117  throw Exception(TE_TR("The input data type is not a 32-bit integer!"));
118 }
119 
120 // UInt32
122 {
123  AssertUInt32(d);
124  return new Char(boost::lexical_cast<char>(static_cast<const UInt32*>(d)->getValue()));
125 }
126 
128 {
129  AssertUInt32(d);
130  return new UChar(boost::lexical_cast<unsigned char>(static_cast<const UInt32*>(d)->getValue()));
131 }
132 
134 {
135  AssertUInt32(d);
136  return new Int16(boost::lexical_cast<int16_t>(static_cast<const UInt32*>(d)->getValue()));
137 }
138 
140 {
141  AssertUInt32(d);
142  return new UInt16(boost::lexical_cast<uint16_t>(static_cast<const UInt32*>(d)->getValue()));
143 }
144 
146 {
147  AssertUInt32(d);
148  return new Int32(boost::lexical_cast<int32_t>(static_cast<const UInt32*>(d)->getValue()));
149 }
150 
152 {
153  AssertUInt32(d);
154  return new Int64(boost::lexical_cast<int64_t>(static_cast<const UInt32*>(d)->getValue()));
155 }
156 
158 {
159  AssertUInt32(d);
160  return new UInt64(static_cast<uint64_t>(static_cast<const UInt32*>(d)->getValue()));
161 }
162 
164 {
165  AssertUInt32(d);
166 
167  bool nval = true;
168  if(static_cast<const UInt32*>(d)->getValue() == 0)
169  nval = false;
170 
171  return new Boolean(nval);
172 }
173 
175 {
176  AssertUInt32(d);
177  return new Float(boost::lexical_cast<float>(static_cast<const UInt32*>(d)->getValue()));
178 }
179 
181 {
182  AssertUInt32(d);
183  return new Double(boost::lexical_cast<double>(static_cast<const UInt32*>(d)->getValue()));
184 }
185 
187 {
188  AssertUInt32(d);
189  return new Numeric(boost::lexical_cast<std::string>(static_cast<const UInt32*>(d)->getValue()));
190 }
191 
193 {
194  AssertUInt32(d);
195  return new String(boost::lexical_cast<std::string>(static_cast<const UInt32*>(d)->getValue()));
196 }
197 
198 void te::dt::AssertUInt32(AbstractData* d) throw(Exception)
199 {
200  if(d->getTypeCode() != UINT32_TYPE)
201  throw Exception(TE_TR("The input data type is not an unsigned 32-bit integer!"));
202 }
SimpleData< std::string, STRING_TYPE > String
Definition: SimpleData.h:229
SimpleData< unsigned char, UCHAR_TYPE > UChar
Definition: SimpleData.h:218
An exception class for the DataType module.
void AssertInt32(AbstractData *d)
Auxiliary method that asserts that the given data is an Int32 type.
SimpleData< boost::int64_t, INT64_TYPE > Int64
Definition: SimpleData.h:223
AbstractData * Int32ToBooleanConverter(AbstractData *d)
It converts an Int32 data value to a boolean data value.
SimpleData< boost::int32_t, INT32_TYPE > Int32
Definition: SimpleData.h:221
AbstractData * UInt32ToBooleanConverter(AbstractData *d)
It converts an unsigned Int32 data value to a boolean data value.
AbstractData * UInt32ToStringConverter(AbstractData *d)
It converts an unsigned Int32 data value to a string data value.
SimpleData< float, FLOAT_TYPE > Float
Definition: SimpleData.h:226
AbstractData * Int32ToStringConverter(AbstractData *d)
It converts an Int32 data value to a string data value.
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
AbstractData * Int32ToCharConverter(AbstractData *d)
It converts an Int32 data value to a char data value.
AbstractData * UInt32ToUInt16Converter(AbstractData *d)
It converts an unsigned Int32 data value to an unsigned Int16 data value.
SimpleData< std::string, NUMERIC_TYPE > Numeric
Definition: SimpleData.h:228
AbstractData * Int32ToDoubleConverter(AbstractData *d)
It converts an Int32 data value to a double value.
AbstractData * UInt32ToInt32Converter(AbstractData *d)
It converts an unsigned Int32 data value to an Int32 data value.
AbstractData * UInt32ToFloatConverter(AbstractData *d)
It converts an unsigned Int32 data value to a float value.
SimpleData< boost::uint64_t, UINT64_TYPE > UInt64
Definition: SimpleData.h:224
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
AbstractData * UInt32ToNumericConverter(AbstractData *d)
It converts an unsigned Int32 data value to Numeric data value.
AbstractData * Int32ToUInt32Converter(AbstractData *d)
It converts an Int32 data value to an unsigned Int32 data value.
AbstractData * UInt32ToInt16Converter(AbstractData *d)
It converts an unsigned Int32 data value to a Int16 data value.
AbstractData * UInt32ToCharConverter(AbstractData *d)
It converts an unsigned Int32 data value to a char data value.
AbstractData * Int32ToNumericConverter(AbstractData *d)
It converts an Int32 data value to Numeric data value.
A set of function that convert an Integer type to other types.
AbstractData * Int32ToInt64Converter(AbstractData *d)
It converts an Int32 data value to a Int64 data value.
void AssertUInt32(AbstractData *d)
Auxiliary method that asserts that the given data is an unsigned Int32 type.
SimpleData< bool, BOOLEAN_TYPE > Boolean
Definition: SimpleData.h:225
AbstractData * UInt32ToUInt64Converter(AbstractData *d)
It converts an unsigned Int32 data value to an unsigned Int64 data value.
SimpleData< double, DOUBLE_TYPE > Double
Definition: SimpleData.h:227
SimpleData< char, CHAR_TYPE > Char
Definition: SimpleData.h:217
AbstractData * Int32ToFloatConverter(AbstractData *d)
It converts an Int32 data value to a float value.
AbstractData * UInt32ToDoubleConverter(AbstractData *d)
It converts an unsigned Int32 data value to a double value.
SimpleData< boost::uint16_t, UINT16_TYPE > UInt16
Definition: SimpleData.h:220
General enumerations for the data type module.
AbstractData * Int32ToUInt16Converter(AbstractData *d)
It converts an Int32 data value to an unsigned Int16 data value.
This file contains several implementations for atomic data types (integers, floats, strings and others).
SimpleData< boost::int16_t, INT16_TYPE > Int16
Definition: SimpleData.h:219
AbstractData * Int32ToInt16Converter(AbstractData *d)
It converts an Int32 data value to a Int16 data value.
SimpleData< boost::uint32_t, UINT32_TYPE > UInt32
Definition: SimpleData.h:222
AbstractData * Int32ToUInt64Converter(AbstractData *d)
It converts an Int32 data value to an unsigned Int64 data value.
AbstractData * UInt32ToUCharConverter(AbstractData *d)
It converts an unsigned Int32 data value to an unsigned char data value.
AbstractData * UInt32ToInt64Converter(AbstractData *d)
It converts an unsigned Int32 data value to a Int64 data value.
AbstractData * Int32ToUCharConverter(AbstractData *d)
It converts an Int32 data value to an unsigned char data value.