Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / external_libraries / yaml-cpp-0.3.0 / include / yaml-cpp / conversion.h
blob1b557b564ef03da578e45c43873c83391cd4919e
1 #ifndef CONVERSION_H_62B23520_7C8E_11DE_8A39_0800200C9A66
2 #define CONVERSION_H_62B23520_7C8E_11DE_8A39_0800200C9A66
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
9 #include "yaml-cpp/null.h"
10 #include "yaml-cpp/traits.h"
11 #include <limits>
12 #include <string>
13 #include <sstream>
15 namespace YAML
17 // traits for conversion
19 template<typename T>
20 struct is_scalar_convertible { enum { value = is_numeric<T>::value }; };
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 }; };
26 // actual conversion
28 inline bool Convert(const std::string& input, std::string& output) {
29 output = input;
30 return true;
33 YAML_CPP_API bool Convert(const std::string& input, bool& output);
34 YAML_CPP_API bool Convert(const std::string& input, _Null& output);
36 inline bool IsInfinity(const std::string& input) {
37 return input == ".inf" || input == ".Inf" || input == ".INF" || input == "+.inf" || input == "+.Inf" || input == "+.INF";
40 inline bool IsNegativeInfinity(const std::string& input) {
41 return input == "-.inf" || input == "-.Inf" || input == "-.INF";
44 inline bool IsNaN(const std::string& input) {
45 return input == ".nan" || input == ".NaN" || input == ".NAN";
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;
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;
66 if(std::numeric_limits<T>::has_quiet_NaN && IsNaN(input)) {
67 output = std::numeric_limits<T>::quiet_NaN();
68 return true;
71 return false;
75 #endif // CONVERSION_H_62B23520_7C8E_11DE_8A39_0800200C9A66