fw4spl
Type.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwTools/Type.hpp"
8 
9 namespace std
10 {
11 //------------------------------------------------------------------------------
12 
13 std::ostream& operator<< (std::ostream& os, const ::fwTools::Type& type)
14 {
15  os << type.string();
16  return os;
17 }
18 }
19 
20 namespace fwTools
21 {
22 
23 const std::string Type::s_UNSPECIFIED_TYPENAME("UNSPECIFIED TYPE");
24 
25 const std::string Type::s_INT8_TYPENAME("int8");
26 const std::string Type::s_INT16_TYPENAME("int16");
27 const std::string Type::s_INT32_TYPENAME("int32");
28 const std::string Type::s_INT64_TYPENAME("int64");
29 
30 const std::string Type::s_UINT8_TYPENAME("uint8");
31 const std::string Type::s_UINT16_TYPENAME("uint16");
32 const std::string Type::s_UINT32_TYPENAME("uint32");
33 const std::string Type::s_UINT64_TYPENAME("uint64");
34 
35 const std::string Type::s_FLOAT_TYPENAME("float");
36 const std::string Type::s_DOUBLE_TYPENAME("double");
37 
38 const Type::TypeMapType Type::s_TYPEMAP = {{
39  {Type::s_INT8_TYPENAME, Type::create< ::fwTools::Type::Int8Type >()},
40  {Type::s_INT16_TYPENAME, Type::create< ::fwTools::Type::Int16Type >()},
41  {Type::s_INT32_TYPENAME, Type::create< ::fwTools::Type::Int32Type >()},
42  {Type::s_INT64_TYPENAME, Type::create< ::fwTools::Type::Int64Type >()},
43 
44  {Type::s_UINT8_TYPENAME, Type::create< ::fwTools::Type::UInt8Type >()},
45  {Type::s_UINT16_TYPENAME, Type::create< ::fwTools::Type::UInt16Type >()},
46  {Type::s_UINT32_TYPENAME, Type::create< ::fwTools::Type::UInt32Type >()},
47  {Type::s_UINT64_TYPENAME, Type::create< ::fwTools::Type::UInt64Type >()},
48 
49  {Type::s_FLOAT_TYPENAME, Type::create< ::fwTools::Type::FloatType >()},
50  {Type::s_DOUBLE_TYPENAME, Type::create< ::fwTools::Type::DoubleType >()}
51  }};
52 
53 const Type Type::s_UNSPECIFIED_TYPE;
54 
55 const Type Type::s_INT8 = ::fwTools::Type::create< ::fwTools::Type::Int8Type >();
56 const Type Type::s_INT16 = ::fwTools::Type::create< ::fwTools::Type::Int16Type >();
57 const Type Type::s_INT32 = ::fwTools::Type::create< ::fwTools::Type::Int32Type >();
58 const Type Type::s_INT64 = ::fwTools::Type::create< ::fwTools::Type::Int64Type >();
59 
60 const Type Type::s_UINT8 = ::fwTools::Type::create< ::fwTools::Type::UInt8Type >();
61 const Type Type::s_UINT16 = ::fwTools::Type::create< ::fwTools::Type::UInt16Type >();
62 const Type Type::s_UINT32 = ::fwTools::Type::create< ::fwTools::Type::UInt32Type >();
63 const Type Type::s_UINT64 = ::fwTools::Type::create< ::fwTools::Type::UInt64Type >();
64 
65 const Type Type::s_FLOAT = ::fwTools::Type::create< ::fwTools::Type::FloatType >();
66 const Type Type::s_DOUBLE = ::fwTools::Type::create< ::fwTools::Type::DoubleType >();
67 
68 //------------------------------------------------------------------------------
69 
70 template<> const std::string& Type::traitsToString< 1, true, true > ()
71 {
72  return Type::s_INT8_TYPENAME;
73 }
74 //------------------------------------------------------------------------------
75 
76 template<> const std::string& Type::traitsToString< 2, true, true > ()
77 {
78  return Type::s_INT16_TYPENAME;
79 }
80 //------------------------------------------------------------------------------
81 
82 template<> const std::string& Type::traitsToString< 4, true, true > ()
83 {
84  return Type::s_INT32_TYPENAME;
85 }
86 //------------------------------------------------------------------------------
87 
88 template<> const std::string& Type::traitsToString< 8, true, true > ()
89 {
90  return Type::s_INT64_TYPENAME;
91 }
92 
93 //------------------------------------------------------------------------------
94 
95 template<> const std::string& Type::traitsToString< 1, false, true > ()
96 {
97  return Type::s_UINT8_TYPENAME;
98 }
99 //------------------------------------------------------------------------------
100 
101 template<> const std::string& Type::traitsToString< 2, false, true > ()
102 {
103  return Type::s_UINT16_TYPENAME;
104 }
105 //------------------------------------------------------------------------------
106 
107 template<> const std::string& Type::traitsToString< 4, false, true > ()
108 {
109  return Type::s_UINT32_TYPENAME;
110 }
111 //------------------------------------------------------------------------------
112 
113 template<> const std::string& Type::traitsToString< 8, false, true > ()
114 {
115  return Type::s_UINT64_TYPENAME;
116 }
117 
118 //------------------------------------------------------------------------------
119 
120 template<> const std::string& Type::traitsToString< 4, true, false > ()
121 {
122  return Type::s_FLOAT_TYPENAME;
123 }
124 //------------------------------------------------------------------------------
125 
126 template<> const std::string& Type::traitsToString< 8, true, false > ()
127 {
128  return Type::s_DOUBLE_TYPENAME;
129 }
130 
131 //------------------------------------------------------------------------------
132 
133 Type::Type()
134 {
135  Type::setType<void>();
136 }
137 
138 //------------------------------------------------------------------------------
139 
140 Type::Type(const std::string& type)
141 {
142  *this = Type::create(type);
143 }
144 
145 //------------------------------------------------------------------------------
146 
147 bool Type::operator==(const Type& _other) const
148 {
149  return m_name == _other.m_name;
150 }
151 
152 //------------------------------------------------------------------------------
153 
154 bool Type::operator!=(const Type& _other) const
155 {
156  return m_name != _other.m_name;
157 }
158 
159 //------------------------------------------------------------------------------
160 
161 bool Type::operator<( const Type& _other) const
162 {
163  return m_name < _other.m_name;
164 }
165 
166 //------------------------------------------------------------------------------
167 
168 unsigned char Type::sizeOf() const
169 {
170  return m_sizeof;
171 }
172 
173 //------------------------------------------------------------------------------
174 
175 const std::string& Type::string() const
176 {
177  return m_name;
178 }
179 
180 //------------------------------------------------------------------------------
181 
182 const std::type_info& Type::typeId() const
183 {
184  return m_tool->m_typeinfo;
185 }
186 
187 //------------------------------------------------------------------------------
188 
189 bool Type::isFixedPrecision() const
190 {
191  return m_isFixedPrecision;
192 }
193 
194 //------------------------------------------------------------------------------
195 
196 bool Type::isSigned() const
197 {
198  return m_isSigned;
199 }
200 
201 //-----------------------------------------------------------------------------
202 
203 Type Type::create(std::string name)
204 {
205  TypeMapType::const_iterator iter = s_TYPEMAP.find(name);
206  if (iter != s_TYPEMAP.end())
207  {
208  return iter->second;
209  }
210  return s_UNSPECIFIED_TYPE;
211 }
212 
213 //-----------------------------------------------------------------------------
214 
215 std::string Type::toString(const void* value) const
216 {
217  return m_tool->toString(value);
218 }
219 
220 //-----------------------------------------------------------------------------
221 
222 Type::ToolBase::ToolBase() :
223  m_typeinfo(typeid(void))
224 {
225 }
226 
227 //-----------------------------------------------------------------------------
228 
229 Type::ToolBase::ToolBase(const std::type_info& typeinfo) :
230  m_typeinfo(typeinfo)
231 {
232 }
233 
234 //-----------------------------------------------------------------------------
235 
236 std::string Type::ToolBase::toString(::boost::any value) const
237 {
238  FwCoreNotUsedMacro(value);
239  SLM_ASSERT("unable to convert an unspecified type value", 0);
240  return "";
241 }
242 
243 //-----------------------------------------------------------------------------
244 
245 std::string Type::ToolBase::toString(const void* value) const
246 {
247  FwCoreNotUsedMacro(value);
248  SLM_ASSERT("unable to convert an unspecified type value", 0);
249  return "";
250 }
251 
252 //-----------------------------------------------------------------------------
253 
254 template <>
255 void Type::setType<char>()
256 {
257  this->setType<signed char>();
258 }
259 
260 //-----------------------------------------------------------------------------
261 
262 #if (defined(linux) || defined(__linux)) && !defined(ANDROID)
263 
264 template <>
265 void Type::setType<std::int64_t>()
266 {
267  this->setType<long long>();
268 }
269 
270 //-----------------------------------------------------------------------------
271 
272 template <>
273 void Type::setType<std::uint64_t>()
274 {
275  this->setType<unsigned long long>();
276 }
277 
278 #endif
279 
280 //-----------------------------------------------------------------------------
281 
282 template <>
283 void Type::setType<void>()
284 {
285  m_name = s_UNSPECIFIED_TYPENAME;
286  m_sizeof = 0;
287  m_isSigned = false;
288  m_isFixedPrecision = false;
289 
290  m_tool = SPTR(ToolBase)(new Type::ToolBase());
291 
292  m_min = 0;
293  m_max = 0;
294 }
295 
296 } // namespace fwTools
297 
#define SPTR(_cls_)
The namespace fwTools contains several tools like UUID, factory, dispatche, stringizer, macros, helper.
STL namespace.
STL class.
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
Class describing an elementary C++ type aka unsigned char, signed char, .... int, float...
Definition: Type.hpp:32