conversion.h
Go to the documentation of this file.
1 #ifndef CONVERSION_H_62B23520_7C8E_11DE_8A39_0800200C9A66_PM
2 #define CONVERSION_H_62B23520_7C8E_11DE_8A39_0800200C9A66_PM
3 
4 #if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
5 #pragma once
6 #endif
7 
8 
9 #include "yaml-cpp-pm/null.h"
10 #include "yaml-cpp-pm/traits.h"
11 #include <limits>
12 #include <string>
13 #include <sstream>
14 
15 namespace YAML_PM
16 {
17  // traits for conversion
18 
19  template<typename T>
21 
22  template<> struct is_scalar_convertible<std::string> { enum { value = true }; };
23  template<> struct is_scalar_convertible<bool> { enum { value = true }; };
24  template<> struct is_scalar_convertible<_Null> { enum { value = true }; };
25 
26  // actual conversion
27 
28  inline bool Convert(const std::string& input, std::string& output) {
29  output = input;
30  return true;
31  }
32 
33  YAML_CPP_API bool Convert(const std::string& input, bool& output);
34  YAML_CPP_API bool Convert(const std::string& input, _Null& output);
35 
36  inline bool IsInfinity(const std::string& input) {
37  return input == ".inf" || input == ".Inf" || input == ".INF" || input == "+.inf" || input == "+.Inf" || input == "+.INF";
38  }
39 
40  inline bool IsNegativeInfinity(const std::string& input) {
41  return input == "-.inf" || input == "-.Inf" || input == "-.INF";
42  }
43 
44  inline bool IsNaN(const std::string& input) {
45  return input == ".nan" || input == ".NaN" || input == ".NAN";
46  }
47 
48 
49  template <typename T>
50  inline bool Convert(const std::string& input, T& output, typename enable_if<is_numeric<T> >::type * = 0) {
51  std::stringstream stream(input);
52  stream.unsetf(std::ios::dec);
53  if((stream >> output) && (stream >> std::ws).eof())
54  return true;
55 
56  if(std::numeric_limits<T>::has_infinity) {
57  if(IsInfinity(input)) {
58  output = std::numeric_limits<T>::infinity();
59  return true;
60  } else if(IsNegativeInfinity(input)) {
61  output = -std::numeric_limits<T>::infinity();
62  return true;
63  }
64  }
65 
66  if(std::numeric_limits<T>::has_quiet_NaN && IsNaN(input)) {
67  output = std::numeric_limits<T>::quiet_NaN();
68  return true;
69  }
70 
71  return false;
72  }
73 }
74 
75 #endif // CONVERSION_H_62B23520_7C8E_11DE_8A39_0800200C9A66_PM
::std::string string
Definition: gtest.h:1979
bool Convert(const std::string &input, std::string &output)
Definition: conversion.h:28
#define YAML_CPP_API
Definition: dll.h:25
bool IsInfinity(const std::string &input)
Definition: conversion.h:36
bool IsNaN(const std::string &input)
Definition: conversion.h:44
bool IsNegativeInfinity(const std::string &input)
Definition: conversion.h:40


libpointmatcher
Author(s):
autogenerated on Sat May 27 2023 02:36:30