span.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. // IWYU pragma: private, include "opentelemetry/nostd/span.h"
  5. #include "opentelemetry/version.h"
  6. // Standard library implementation requires at least C++17 compiler.
  7. // Older C++14 compilers may provide support for __has_include as a
  8. // conforming extension.
  9. #if defined __has_include
  10. # if __has_include(<version>) // Check for __cpp_{feature}
  11. # include <version>
  12. # if defined(__cpp_lib_span) && __cplusplus > 201703L
  13. # define OPENTELEMETRY_HAVE_SPAN
  14. # endif
  15. # endif
  16. # if !defined(OPENTELEMETRY_HAVE_SPAN)
  17. # // Check for Visual Studio span
  18. # if defined(_MSVC_LANG) && _HAS_CXX20
  19. # define OPENTELEMETRY_HAVE_SPAN
  20. # endif
  21. # // Check for other compiler span implementation
  22. # if !defined(_MSVC_LANG) && __has_include(<span>) && __cplusplus > 201703L
  23. // This works as long as compiler standard is set to C++20
  24. # define OPENTELEMETRY_HAVE_SPAN
  25. # endif
  26. # endif
  27. # if !__has_include(<gsl/gsl>)
  28. # undef HAVE_GSL
  29. # endif
  30. #endif
  31. #if !defined(OPENTELEMETRY_HAVE_SPAN)
  32. # if defined(HAVE_GSL)
  33. # include <type_traits>
  34. // Guidelines Support Library provides an implementation of std::span
  35. # include <gsl/gsl>
  36. OPENTELEMETRY_BEGIN_NAMESPACE
  37. namespace nostd
  38. {
  39. using gsl::dynamic_extent;
  40. template <class ElementType, std::size_t Extent = gsl::dynamic_extent>
  41. using span = gsl::span<ElementType, Extent>;
  42. } // namespace nostd
  43. OPENTELEMETRY_END_NAMESPACE
  44. # define OPENTELEMETRY_HAVE_SPAN
  45. # else
  46. // No `gsl::span`, no `std::span`, fallback to `nostd::span`
  47. # endif
  48. #else // OPENTELEMETRY_HAVE_SPAN
  49. // Using std::span (https://wg21.link/P0122R7) from Standard Library available in C++20 :
  50. // - GCC libstdc++ 10+
  51. // - Clang libc++ 7
  52. // - MSVC Standard Library 19.26*
  53. // - Apple Clang 10.0.0*
  54. # include <limits>
  55. # include <span>
  56. OPENTELEMETRY_BEGIN_NAMESPACE
  57. namespace nostd
  58. {
  59. constexpr std::size_t dynamic_extent = (std::numeric_limits<std::size_t>::max)();
  60. template <class ElementType, std::size_t Extent = nostd::dynamic_extent>
  61. using span = std::span<ElementType, Extent>;
  62. } // namespace nostd
  63. OPENTELEMETRY_END_NAMESPACE
  64. #endif // if OPENTELEMETRY_HAVE_SPAN