fw4spl
Numeric.hxx
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 __FWATOMS_NUMERIC_HXX__
8 #define __FWATOMS_NUMERIC_HXX__
9 
10 #include "fwAtoms/Exception.hpp"
11 
12 #include <boost/mpl/and.hpp>
13 #include <boost/numeric/conversion/cast.hpp>
14 #include <boost/utility/enable_if.hpp>
15 #include <boost/variant/get.hpp>
16 
17 #include <type_traits>
18 
19 namespace fwAtoms
20 {
21 
23 {
24 friend class Numeric;
25 
26 //------------------------------------------------------------------------------
27 
28 template< typename T >
29 static void setValue( Numeric::ValueType& v,
30  typename ::boost::enable_if_c<
31  std::is_same<
32  typename ::boost::remove_reference< typename std::remove_const <T>::type >::type,
33  std::string
34  >::value
35 
36  , T>::type value )
37 {
38  v = Numeric::valueFromString(value);
39 }
40 
41 //------------------------------------------------------------------------------
42 
43 template< typename T >
44 static void setValue( Numeric::ValueType& v,
45  typename ::boost::enable_if_c<
46  ::boost::mpl::and_< std::is_signed<T>, std::is_integral<T> >::value
47  , T>::type value )
48 {
49  v = static_cast< std::int64_t>(value);
50 }
51 
52 //------------------------------------------------------------------------------
53 
54 template< typename T >
55 static void setValue( Numeric::ValueType& v,
56  typename ::boost::enable_if_c<
57  ::boost::mpl::and_< std::is_unsigned<T>, std::is_integral<T> >::value
58  , T>::type value )
59 {
60  v = static_cast< std::uint64_t>(value);
61 }
62 
63 //------------------------------------------------------------------------------
64 
65 template< typename T >
66 static void setValue( Numeric::ValueType& v,
67  typename ::boost::enable_if_c< std::is_floating_point<T>::value, T>::type value )
68 {
69  v = value;
70 }
71 
72 };
73 
74 //--------------------------------------------------------------------------
75 
76 template<typename T>
77 Numeric::sptr Numeric::New( T value )
78 {
79  Numeric::sptr valueSptr = Numeric::New();
80  NumericSetter::setValue<T>(valueSptr->m_value, value);
81  return valueSptr;
82 }
83 
84 //--------------------------------------------------------------------------
85 
86 template<typename T>
87 class get_casted_value : public boost::static_visitor<T>
88 {
89 public:
90 
91  //------------------------------------------------------------------------------
92 
93  T operator()( ::boost::blank& ) const
94  {
95  FW_RAISE_EXCEPTION(::fwAtoms::Exception("Unable to get value of an empty numeric"));
96  return T();
97  }
98  //------------------------------------------------------------------------------
99 
100  T operator()( const ::boost::blank& ) const
101  {
102  FW_RAISE_EXCEPTION(::fwAtoms::Exception("Unable to get value of an empty numeric"));
103  return T();
104  }
105 
106  //------------------------------------------------------------------------------
107 
108  template <typename U>
109  T operator()( U& value ) const
110  {
111  return ::boost::numeric_cast<T>(value);
112  }
113 
114 };
115 
116 //------------------------------------------------------------------------------
117 
118 template<typename T>
120 {
121  T val = boost::apply_visitor( get_casted_value<T>(), m_value );
122  return val;
123 }
124 
125 }
126 
127 #endif //__FWATOMS_NUMERIC_HXX__
128 
static FWATOMS_API ValueType valueFromString(const std::string &s, NumericType type=EMPTY)
Retuns a ValueType from a std::string.
Definition: Numeric.cpp:61
fwAtoms contains basic objects to represent any other kind of object
static Numeric::sptr New(T value)
Build a new numeric type.
Definition: Numeric.hxx:77
Storing a numeric value.
Definition: Numeric.hpp:25
T getValue() const
Returns the held value static_casted to T.
Definition: Numeric.hxx:119