traits.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef TRAITS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  2. #define TRAITS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
  3. #if defined(_MSC_VER) || \
  4. (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
  5. (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
  6. #pragma once
  7. #endif
  8. #include <type_traits>
  9. #include <utility>
  10. #include <string>
  11. #include <sstream>
  12. namespace YAML {
  13. template <typename>
  14. struct is_numeric {
  15. enum { value = false };
  16. };
  17. template <>
  18. struct is_numeric<char> {
  19. enum { value = true };
  20. };
  21. template <>
  22. struct is_numeric<unsigned char> {
  23. enum { value = true };
  24. };
  25. template <>
  26. struct is_numeric<int> {
  27. enum { value = true };
  28. };
  29. template <>
  30. struct is_numeric<unsigned int> {
  31. enum { value = true };
  32. };
  33. template <>
  34. struct is_numeric<long int> {
  35. enum { value = true };
  36. };
  37. template <>
  38. struct is_numeric<unsigned long int> {
  39. enum { value = true };
  40. };
  41. template <>
  42. struct is_numeric<short int> {
  43. enum { value = true };
  44. };
  45. template <>
  46. struct is_numeric<unsigned short int> {
  47. enum { value = true };
  48. };
  49. #if defined(_MSC_VER) && (_MSC_VER < 1310)
  50. template <>
  51. struct is_numeric<__int64> {
  52. enum { value = true };
  53. };
  54. template <>
  55. struct is_numeric<unsigned __int64> {
  56. enum { value = true };
  57. };
  58. #else
  59. template <>
  60. struct is_numeric<long long> {
  61. enum { value = true };
  62. };
  63. template <>
  64. struct is_numeric<unsigned long long> {
  65. enum { value = true };
  66. };
  67. #endif
  68. template <>
  69. struct is_numeric<float> {
  70. enum { value = true };
  71. };
  72. template <>
  73. struct is_numeric<double> {
  74. enum { value = true };
  75. };
  76. template <>
  77. struct is_numeric<long double> {
  78. enum { value = true };
  79. };
  80. template <bool, class T = void>
  81. struct enable_if_c {
  82. using type = T;
  83. };
  84. template <class T>
  85. struct enable_if_c<false, T> {};
  86. template <class Cond, class T = void>
  87. struct enable_if : public enable_if_c<Cond::value, T> {};
  88. template <bool, class T = void>
  89. struct disable_if_c {
  90. using type = T;
  91. };
  92. template <class T>
  93. struct disable_if_c<true, T> {};
  94. template <class Cond, class T = void>
  95. struct disable_if : public disable_if_c<Cond::value, T> {};
  96. }
  97. template <typename S, typename T>
  98. struct is_streamable {
  99. template <typename SS, typename TT>
  100. static auto test(int)
  101. -> decltype(std::declval<SS&>() << std::declval<TT>(), std::true_type());
  102. template <typename, typename>
  103. static auto test(...) -> std::false_type;
  104. static const bool value = decltype(test<S, T>(0))::value;
  105. };
  106. template<typename Key, bool Streamable>
  107. struct streamable_to_string {
  108. static std::string impl(const Key& key) {
  109. std::stringstream ss;
  110. ss << key;
  111. return ss.str();
  112. }
  113. };
  114. template<typename Key>
  115. struct streamable_to_string<Key, false> {
  116. static std::string impl(const Key&) {
  117. return "";
  118. }
  119. };
  120. #endif // TRAITS_H_62B23520_7C8E_11DE_8A39_0800200C9A66