xinfo.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /***************************************************************************
  2. * Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
  3. * Copyright (c) QuantStack *
  4. * *
  5. * Distributed under the terms of the BSD 3-Clause License. *
  6. * *
  7. * The full license is in the file LICENSE, distributed with this software. *
  8. ****************************************************************************/
  9. #ifndef XTENSOR_INFO_HPP
  10. #define XTENSOR_INFO_HPP
  11. #include <string>
  12. #include "xlayout.hpp"
  13. #ifndef _MSC_VER
  14. #if __cplusplus < 201103
  15. #define CONSTEXPR11_TN
  16. #define CONSTEXPR14_TN
  17. #define NOEXCEPT_TN
  18. #elif __cplusplus < 201402
  19. #define CONSTEXPR11_TN constexpr
  20. #define CONSTEXPR14_TN
  21. #define NOEXCEPT_TN noexcept
  22. #else
  23. #define CONSTEXPR11_TN constexpr
  24. #define CONSTEXPR14_TN constexpr
  25. #define NOEXCEPT_TN noexcept
  26. #endif
  27. #else // _MSC_VER
  28. #if _MSC_VER < 1900
  29. #define CONSTEXPR11_TN
  30. #define CONSTEXPR14_TN
  31. #define NOEXCEPT_TN
  32. #elif _MSC_VER < 2000
  33. #define CONSTEXPR11_TN constexpr
  34. #define CONSTEXPR14_TN
  35. #define NOEXCEPT_TN noexcept
  36. #else
  37. #define CONSTEXPR11_TN constexpr
  38. #define CONSTEXPR14_TN constexpr
  39. #define NOEXCEPT_TN noexcept
  40. #endif
  41. #endif
  42. namespace xt
  43. {
  44. // see http://stackoverflow.com/a/20170989
  45. struct static_string
  46. {
  47. template <std::size_t N>
  48. explicit CONSTEXPR11_TN static_string(const char (&a)[N]) NOEXCEPT_TN : data(a),
  49. size(N - 1)
  50. {
  51. }
  52. CONSTEXPR11_TN static_string(const char* a, const std::size_t sz) NOEXCEPT_TN : data(a),
  53. size(sz)
  54. {
  55. }
  56. const char* const data;
  57. const std::size_t size;
  58. };
  59. template <class T>
  60. CONSTEXPR14_TN static_string type_name()
  61. {
  62. #ifdef __clang__
  63. static_string p(__PRETTY_FUNCTION__);
  64. return static_string(p.data + 39, p.size - 39 - 1);
  65. #elif defined(__GNUC__)
  66. static_string p(__PRETTY_FUNCTION__);
  67. #if __cplusplus < 201402
  68. return static_string(p.data + 36, p.size - 36 - 1);
  69. #else
  70. return static_string(p.data + 54, p.size - 54 - 1);
  71. #endif
  72. #elif defined(_MSC_VER)
  73. static const static_string p(__FUNCSIG__);
  74. return static_string(p.data + 47, p.size - 47 - 7);
  75. #endif
  76. }
  77. template <class T>
  78. std::string type_to_string()
  79. {
  80. static_string static_name = type_name<T>();
  81. return std::string(static_name.data, static_name.size);
  82. }
  83. template <class T>
  84. std::string info(const T& t)
  85. {
  86. std::string s;
  87. s += "\nValue type: " + type_to_string<typename T::value_type>();
  88. s += "\nLayout: ";
  89. if (t.layout() == layout_type::row_major)
  90. {
  91. s += "row_major";
  92. }
  93. else if (t.layout() == layout_type::column_major)
  94. {
  95. s += "column_major";
  96. }
  97. else if (t.layout() == layout_type::dynamic)
  98. {
  99. s += "dynamic";
  100. }
  101. else
  102. {
  103. s += "any";
  104. }
  105. s += "\nShape: (";
  106. bool first = true;
  107. for (const auto& el : t.shape())
  108. {
  109. if (!first)
  110. {
  111. s += ", ";
  112. }
  113. first = false;
  114. s += std::to_string(el);
  115. }
  116. s += ")\nStrides: (";
  117. first = true;
  118. for (const auto& el : t.strides())
  119. {
  120. if (!first)
  121. {
  122. s += ", ";
  123. }
  124. first = false;
  125. s += std::to_string(el);
  126. }
  127. s += ")\nSize: " + std::to_string(t.size()) + "\n";
  128. return s;
  129. }
  130. }
  131. #endif