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
YAML_PM::is_scalar_convertible::value
@ value
Definition: conversion.h:20
YAML_PM
Definition: aliasmanager.h:11
YAML_PM::is_scalar_convertible
Definition: conversion.h:20
YAML_PM::Convert
bool Convert(const std::string &input, std::string &output)
Definition: conversion.h:28
YAML_PM::IsNaN
bool IsNaN(const std::string &input)
Definition: conversion.h:44
testing::internal::string
::std::string string
Definition: gtest.h:1979
YAML_CPP_API
#define YAML_CPP_API
Definition: dll.h:25
YAML_PM::is_numeric
Definition: traits.h:12
null.h
YAML_PM::IsInfinity
bool IsInfinity(const std::string &input)
Definition: conversion.h:36
std
traits.h
YAML_PM::IsNegativeInfinity
bool IsNegativeInfinity(const std::string &input)
Definition: conversion.h:40
YAML_PM::enable_if
Definition: traits.h:42
YAML_PM::_Null
Definition: null.h:15


mp2p_icp
Author(s):
autogenerated on Wed Oct 23 2024 02:45:38