fw4spl
Numeric.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 "fwAtoms/Numeric.hpp"
8 
9 #include "fwAtoms/Exception.hpp"
10 #include "fwAtoms/Numeric.hxx"
11 #include "fwAtoms/registry/macros.hpp"
12 
13 #include <fwCore/spyLog.hpp>
14 
15 #include <boost/lexical_cast.hpp>
16 
17 fwAtomsRegisterMacro( ::fwAtoms::Numeric );
18 
19 namespace fwAtoms
20 {
21 
22 //------------------------------------------------------------------------------
23 
24 Base::sptr Numeric::clone() const
25 {
26  Numeric::sptr clone = Numeric::New();
27  clone->m_value = m_value;
28  return clone;
29 }
30 
31 //------------------------------------------------------------------------------
32 
33 std::string Numeric::getString() const
34 {
35  FW_RAISE_EXCEPTION_IF( ::fwAtoms::Exception("Empty numeric atom"), m_value.which() == 0);
36  return ::boost::lexical_cast<std::string>(m_value);
37 }
38 
39 //------------------------------------------------------------------------------
40 
41 template < typename T >
42 bool lexicalCast( Numeric::ValueType& v, const std::string& s )
43 {
44  bool ok = false;
45 
46  try
47  {
48  T val = ::boost::lexical_cast< T >( s );
49  v = val;
50  ok = true;
51  }
52  catch ( const ::boost::bad_lexical_cast&)
53  {
54  OSLM_TRACE("lexicalCast failed for '" << s << "'" << " to type: " << typeid(T).name() );
55  }
56  return ok;
57 }
58 
59 //------------------------------------------------------------------------------
60 
61 Numeric::ValueType Numeric::valueFromString(const std::string& s, Numeric::NumericType type)
62 {
63  Numeric::ValueType res;
64  SLM_ASSERT("Invalid variant type requested", EMPTY <= type && type <= DOUBLE);
65 
66  if ( type == EMPTY)
67  {
68  FW_RAISE_EXCEPTION_IF(
69  ::fwAtoms::Exception( std::string("Unable to get numeric from '") + s + "'"),
70  !(lexicalCast< std::int64_t >(res, s)
71  || lexicalCast< std::uint64_t >(res, s)
72  || lexicalCast< double >(res, s))
73  );
74  }
75  else if ( type == INT)
76  {
77  FW_RAISE_EXCEPTION_IF(
78  ::fwAtoms::Exception( std::string("Unable to get int64 numeric from '") + s + "'"),
79  !lexicalCast< std::int64_t >(res, s)
80  );
81  }
82  else if ( type == UINT)
83  {
84  FW_RAISE_EXCEPTION_IF(
85  ::fwAtoms::Exception( std::string("Unable to get uint64 numeric from '") + s + "'"),
86  !lexicalCast< std::uint64_t >(res, s)
87  );
88  }
89  else if ( type == DOUBLE)
90  {
91  FW_RAISE_EXCEPTION_IF(
92  ::fwAtoms::Exception( std::string("Unable to get double numeric from '") + s + "'"),
93  !lexicalCast< double >(res, s)
94  );
95  }
96 
97  return res;
98 }
99 
100 //------------------------------------------------------------------------------
101 
102 void Numeric::setFromString(const std::string& s, Numeric::NumericType type)
103 {
104  m_value = Numeric::valueFromString(s, type);
105 }
106 
107 }
108 
FWATOMS_API void setFromString(const std::string &s, NumericType type=EMPTY)
Sets Numeric&#39;s value from given string, using Numeric::valueFromString.
Definition: Numeric.cpp:102
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
#define OSLM_TRACE(message)
Definition: spyLog.hpp:230
static Numeric::sptr New(T value)
Build a new numeric type.
Definition: Numeric.hxx:77
#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
virtual FWATOMS_API Base::sptr clone() const override
Returns a clone object.
Definition: Numeric.cpp:24
virtual FWATOMS_API std::string getString() const override
Returns a string representing the currently held numeric value.
Definition: Numeric.cpp:33
Storing a numeric value.
Definition: Numeric.hpp:25
This file defines SpyLog macros. These macros are used to log messages to a file or to the console du...