conversion.h
Go to the documentation of this file.
00001 #ifndef CONVERSION_H_62B23520_7C8E_11DE_8A39_0800200C9A66
00002 #define CONVERSION_H_62B23520_7C8E_11DE_8A39_0800200C9A66
00003 
00004 #if defined(_MSC_VER) || (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
00005 #pragma once
00006 #endif
00007 
00008 
00009 #include "yaml-cpp-pm/null.h"
00010 #include "yaml-cpp-pm/traits.h"
00011 #include <limits>
00012 #include <string>
00013 #include <sstream>
00014 
00015 namespace YAML_PM
00016 {
00017         // traits for conversion
00018         
00019         template<typename T>
00020         struct is_scalar_convertible { enum { value = is_numeric<T>::value }; };
00021         
00022         template<> struct is_scalar_convertible<std::string> { enum { value = true }; };
00023         template<> struct is_scalar_convertible<bool> { enum { value = true }; };
00024         template<> struct is_scalar_convertible<_Null> { enum { value = true }; };
00025 
00026         // actual conversion
00027         
00028         inline bool Convert(const std::string& input, std::string& output) {
00029                 output = input;
00030                 return true;
00031         }
00032         
00033         YAML_CPP_API bool Convert(const std::string& input, bool& output);
00034         YAML_CPP_API bool Convert(const std::string& input, _Null& output);
00035         
00036         inline bool IsInfinity(const std::string& input) {
00037                 return input == ".inf" || input == ".Inf" || input == ".INF" || input == "+.inf" || input == "+.Inf" || input == "+.INF";
00038         }
00039         
00040         inline bool IsNegativeInfinity(const std::string& input) {
00041                 return input == "-.inf" || input == "-.Inf" || input == "-.INF";
00042         }
00043         
00044         inline bool IsNaN(const std::string& input) {
00045                 return input == ".nan" || input == ".NaN" || input == ".NAN";
00046         }
00047 
00048 
00049         template <typename T> 
00050         inline bool Convert(const std::string& input, T& output, typename enable_if<is_numeric<T> >::type * = 0) {
00051                 std::stringstream stream(input);
00052                 stream.unsetf(std::ios::dec);
00053         if((stream >> output) && (stream >> std::ws).eof())
00054             return true;
00055                 
00056                 if(std::numeric_limits<T>::has_infinity) {
00057                         if(IsInfinity(input)) {
00058                                 output = std::numeric_limits<T>::infinity();
00059                                 return true;
00060                         } else if(IsNegativeInfinity(input)) {
00061                                 output = -std::numeric_limits<T>::infinity();
00062                                 return true;
00063                         }
00064                 }
00065                 
00066                 if(std::numeric_limits<T>::has_quiet_NaN && IsNaN(input)) {
00067                         output = std::numeric_limits<T>::quiet_NaN();
00068                         return true;
00069                 }
00070                 
00071                 return false;
00072         }
00073 }
00074 
00075 #endif // CONVERSION_H_62B23520_7C8E_11DE_8A39_0800200C9A66


upstream_src
Author(s):
autogenerated on Mon Oct 6 2014 10:27:39