fw4spl
Type.hpp
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 #ifndef __FWTOOLS_TYPE_HPP__
8 #define __FWTOOLS_TYPE_HPP__
9 
10 #include "fwTools/config.hpp"
11 #include "fwTools/Stringizer.hpp"
12 
13 #include <fwCore/base.hpp>
14 
15 #include <boost/any.hpp>
16 #include <boost/cstdint.hpp>
17 #include <boost/lexical_cast.hpp>
18 #include <boost/type_traits/is_signed.hpp>
19 
20 #include <limits>
21 #include <map>
22 #include <string>
23 #include <type_traits>
24 #include <typeinfo>
25 
26 namespace fwTools
27 {
28 
32 class FWTOOLS_CLASS_API Type
33 {
34 
35 public:
36  struct FWTOOLS_CLASS_API ToolBase
37  {
38  FWTOOLS_API ToolBase();
39  FWTOOLS_API virtual ~ToolBase()
40  {
41  }
42  FWTOOLS_API ToolBase(const std::type_info& typeinfo);
43  FWTOOLS_API virtual std::string toString( ::boost::any value ) const;
44  FWTOOLS_API virtual std::string toString( const void* ) const;
45 
46  const std::type_info& m_typeinfo;
47  };
48 
49  template<typename T>
50  struct Tool : public ToolBase
51  {
52  Tool();
53  virtual ~Tool()
54  {
55  }
56  virtual std::string toString( ::boost::any value ) const;
57  virtual std::string toString( const void* ) const;
58  };
59 
60  typedef std::map<std::string, Type> TypeMapType;
61 
63  FWTOOLS_API Type();
64  FWTOOLS_API Type(const std::string& type);
65 
69  FWTOOLS_API bool operator==(const Type&) const;
70 
74  FWTOOLS_API bool operator!=(const Type&) const;
75 
79  FWTOOLS_API bool operator<( const Type& ) const;
80 
85  template< class TYPE>
86  void setType();
87 
92  template< class TYPE>
93  bool isOfType() const;
94 
98  FWTOOLS_API unsigned char sizeOf() const;
99 
101  FWTOOLS_API const std::string& string() const;
102 
104  FWTOOLS_API const std::type_info& typeId() const;
105 
109  template<class T>
110  const std::pair<T, T> minMax() const;
111 
113  FWTOOLS_API bool isFixedPrecision() const;
114 
116  FWTOOLS_API bool isSigned() const;
117 
118  FWTOOLS_API std::string toString( const void* ) const;
119 
120  template <int SIZEOF, bool SIGNED, bool ISINTEGRAL>
121  static const std::string& traitsToString();
122 
123  template <typename T>
124  static Type create();
125 
126  FWTOOLS_API static Type create(std::string name);
127 
128 protected:
129 
130  std::string m_name;
131  unsigned char m_sizeof;
132  bool m_isSigned;
133  bool m_isFixedPrecision;
134  ::boost::any m_min;
135  ::boost::any m_max;
136 
137  SPTR(ToolBase) m_tool;
138 
140 
141  FWTOOLS_API static const TypeMapType s_TYPEMAP;
142 
143 public:
144 
145  FWTOOLS_API static const Type s_UNSPECIFIED_TYPE;
146 
147  FWTOOLS_API static const Type s_INT8;
148  FWTOOLS_API static const Type s_INT16;
149  FWTOOLS_API static const Type s_INT32;
150  FWTOOLS_API static const Type s_INT64;
151 
152  FWTOOLS_API static const Type s_UINT8;
153  FWTOOLS_API static const Type s_UINT16;
154  FWTOOLS_API static const Type s_UINT32;
155  FWTOOLS_API static const Type s_UINT64;
156 
157  FWTOOLS_API static const Type s_FLOAT;
158  FWTOOLS_API static const Type s_DOUBLE;
159 
160  FWTOOLS_API static const std::string s_UNSPECIFIED_TYPENAME;
161 
162  FWTOOLS_API static const std::string s_INT8_TYPENAME;
163  FWTOOLS_API static const std::string s_INT16_TYPENAME;
164  FWTOOLS_API static const std::string s_INT32_TYPENAME;
165  FWTOOLS_API static const std::string s_INT64_TYPENAME;
166 
167  FWTOOLS_API static const std::string s_UINT8_TYPENAME;
168  FWTOOLS_API static const std::string s_UINT16_TYPENAME;
169  FWTOOLS_API static const std::string s_UINT32_TYPENAME;
170  FWTOOLS_API static const std::string s_UINT64_TYPENAME;
171 
172  FWTOOLS_API static const std::string s_FLOAT_TYPENAME;
173  FWTOOLS_API static const std::string s_DOUBLE_TYPENAME;
174 
175  typedef std::int8_t Int8Type;
176  typedef std::int16_t Int16Type;
177  typedef std::int32_t Int32Type;
178  typedef std::int64_t Int64Type;
179 
180  typedef std::uint8_t UInt8Type;
181  typedef std::uint16_t UInt16Type;
182  typedef std::uint32_t UInt32Type;
183  typedef std::uint64_t UInt64Type;
184 
185  typedef float FloatType;
186  typedef double DoubleType;
187 
188 };
189 
190 //-----------------------------------------------------------------------------
191 
192 template< typename T >
193 std::string Type::Tool<T>::toString(::boost::any value) const
194 {
195  return ::fwTools::getString( boost::any_cast<const T> (value));
196 }
197 
198 //-----------------------------------------------------------------------------
199 
200 template< typename T >
201 std::string Type::Tool<T>::toString(const void* value) const
202 {
203  const T& v = *(static_cast< const T* > (value));
204  return ::fwTools::getString( v );
205 }
206 //-----------------------------------------------------------------------------
207 
208 template< typename T >
210  Type::ToolBase(typeid(T))
211 {
212 }
213 
214 //-----------------------------------------------------------------------------
215 
216 template <typename T>
217 Type Type::create()
218 {
219  Type t;
220  t.setType<T>();
221  return t;
222 }
223 
224 //-----------------------------------------------------------------------------
225 
226 template <typename T>
227 bool Type::isOfType() const
228 {
229  return *this == create<T>();
230 }
231 
232 //-----------------------------------------------------------------------------
233 
234 template <typename T>
236 {
237  m_name = Type::traitsToString< sizeof(T), std::is_signed<T>::value, std::is_integral<T>::value >();
238 
239  m_sizeof = sizeof(T);
240  m_isSigned = std::is_signed<T>::value;
241  m_isFixedPrecision = std::is_integral<T>::value;
242 
243  m_tool = SPTR(ToolBase)(new Type::Tool<T>());
244 
245  T min = static_cast< T >( std::numeric_limits< T >::lowest() );
246  T max = static_cast< T >( std::numeric_limits< T >::max() );
247 
248  m_min = min;
249  m_max = max;
250 }
251 
252 //-----------------------------------------------------------------------------
253 
254 template <>
255 FWTOOLS_API void Type::setType< void >();
256 
257 //-----------------------------------------------------------------------------
258 
259 template <>
260 FWTOOLS_API void Type::setType< char >();
261 
262 //-----------------------------------------------------------------------------
263 
264 #ifdef linux
265 
266 template <>
267 FWTOOLS_API void Type::setType< std::int64_t >();
268 
269 //-----------------------------------------------------------------------------
270 
271 template <>
272 FWTOOLS_API void Type::setType< std::uint64_t >();
273 
274 #endif
275 
276 //-----------------------------------------------------------------------------
277 
278 template <int SIZEOF, bool SIGNED, bool ISINTEGRAL>
279 const std::string& Type::traitsToString()
280 {
281  OSLM_ERROR("unknown " << (SIGNED ? "signed" : "unsigned")
282  << " " << (ISINTEGRAL ? "integral" : "floating")
283  << " type with size : " << SIZEOF);
284  return Type::s_UNSPECIFIED_TYPENAME;
285 }
286 
287 //-----------------------------------------------------------------------------
288 
289 template <typename T>
290 const std::pair<T, T> Type::minMax() const
291 {
292  return std::pair<T, T>(
293  boost::any_cast< T >(m_min),
294  boost::any_cast< T >(m_max)
295  );
296 }
297 
298 //-----------------------------------------------------------------------------
299 
300 template<> FWTOOLS_API const std::string& Type::traitsToString< 1, true, true > ();
301 template<> FWTOOLS_API const std::string& Type::traitsToString< 2, true, true > ();
302 template<> FWTOOLS_API const std::string& Type::traitsToString< 4, true, true > ();
303 template<> FWTOOLS_API const std::string& Type::traitsToString< 8, true, true > ();
304 
305 template<> FWTOOLS_API const std::string& Type::traitsToString< 1, false, true > ();
306 template<> FWTOOLS_API const std::string& Type::traitsToString< 2, false, true > ();
307 template<> FWTOOLS_API const std::string& Type::traitsToString< 4, false, true > ();
308 template<> FWTOOLS_API const std::string& Type::traitsToString< 8, false, true > ();
309 
310 template<> FWTOOLS_API const std::string& Type::traitsToString< 4, true, false > ();
311 template<> FWTOOLS_API const std::string& Type::traitsToString< 8, true, false > ();
312 
313 } //end namespace fwTools
314 
315 namespace std
316 {
317 FWTOOLS_API std::ostream& operator<< (std::ostream& os, const ::fwTools::Type& type);
318 } // namespace std
319 
320 #endif /*__FWTOOLS_TYPE_HPP__*/
321 
#define SPTR(_cls_)
The namespace fwTools contains several tools like UUID, factory, dispatche, stringizer, macros, helper.
bool isOfType() const
Return true iff the Type value represents the TYPE.
Definition: Type.hpp:227
STL namespace.
void setType()
Set Type value according given template.
Definition: Type.hpp:235
#define OSLM_ERROR(message)
Definition: spyLog.hpp:274
Class describing an elementary C++ type aka unsigned char, signed char, .... int, float...
Definition: Type.hpp:32
static FWTOOLS_API const TypeMapType s_TYPEMAP
Value for not specified type.
Definition: Type.hpp:141
const std::pair< T, T > minMax() const
return the min and max storable in the Type. take care that min/max value are casted into template T ...
Definition: Type.hpp:290