Medical Imaging Interaction Toolkit  2024.12.99-0da743f6
Medical Imaging Interaction Toolkit
mitkLexicalCast.h
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 #ifndef mitkLexicalCast_h
14 #define mitkLexicalCast_h
15 
16 #include <boost/lexical_cast.hpp>
17 
18 namespace mitk
19 {
20  template <typename Target>
21  inline Target lexical_cast(const std::string &arg)
22  {
23  Target result = Target();
24 
25  // Let Boost try to do the lexical cast, which will most probably succeed!
26  if (!boost::conversion::detail::try_lexical_convert(arg, result))
27  {
28  // Fallback to our own conversion using std::istringstream. This happens with
29  // Apple LLVM version 9.1.0 (clang-902.0.39.1) on darwin17.5.0 and very small
30  // floating point numbers like 0.2225e-307.
31  std::istringstream stream(arg);
32  stream.exceptions(std::ios::badbit);
33 
34  try
35  {
36  stream.unsetf(std::ios::skipws);
37  stream.precision(boost::detail::lcast_get_precision<Target>());
38  stream >> result;
39  }
40  catch (const std::ios_base::failure &)
41  {
42  boost::conversion::detail::throw_bad_cast<std::string, Target>();
43  }
44  }
45 
46  return result;
47  }
48 }
49 
50 namespace boost
51 {
52  template <>
53  inline float lexical_cast<float, std::string>(const std::string &arg)
54  {
55  return mitk::lexical_cast<float>(arg);
56  }
57 
58  template <>
59  inline double lexical_cast<double, std::string>(const std::string &arg)
60  {
61  return mitk::lexical_cast<double>(arg);
62  }
63 
64  template <>
65  inline long double lexical_cast<long double, std::string>(const std::string &arg)
66  {
67  return mitk::lexical_cast<long double>(arg);
68  }
69 }
70 
71 #endif
boost
Definition: mitkLexicalCast.h:50
mitk::lexical_cast
Target lexical_cast(const std::string &arg)
Definition: mitkLexicalCast.h:21
mitk
Find image slices visible on a given plane.
Definition: RenderingTests.dox:1