variant.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "opentelemetry/version.h"
  5. #if defined(OPENTELEMETRY_STL_VERSION)
  6. # if OPENTELEMETRY_STL_VERSION >= 2017
  7. # include "opentelemetry/std/variant.h"
  8. # define OPENTELEMETRY_HAVE_STD_VARIANT
  9. # endif
  10. #endif
  11. #if !defined(OPENTELEMETRY_HAVE_STD_VARIANT)
  12. // We use a LOCAL snapshot of Abseil that is known to compile with Visual Studio 2015.
  13. // Header-only. Without compiling the actual Abseil binary. As Abseil moves on to new
  14. // toolchains, it may drop support for Visual Studio 2015 in future versions.
  15. # if defined(__EXCEPTIONS)
  16. # include <exception>
  17. OPENTELEMETRY_BEGIN_NAMESPACE
  18. namespace nostd
  19. {
  20. class bad_variant_access : public std::exception
  21. {
  22. public:
  23. virtual const char *what() const noexcept override { return "bad_variant_access"; }
  24. };
  25. [[noreturn]] inline void throw_bad_variant_access()
  26. {
  27. throw bad_variant_access{};
  28. }
  29. } // namespace nostd
  30. OPENTELEMETRY_END_NAMESPACE
  31. # define THROW_BAD_VARIANT_ACCESS opentelemetry::nostd::throw_bad_variant_access()
  32. # else
  33. # define THROW_BAD_VARIANT_ACCESS std::terminate()
  34. # endif
  35. # ifdef _MSC_VER
  36. // Abseil variant implementation contains some benign non-impacting warnings
  37. // that should be suppressed if compiling with Visual Studio 2017 and above.
  38. # pragma warning(push)
  39. # pragma warning(disable : 4245) // conversion from int to const unsigned _int64
  40. # pragma warning(disable : 4127) // conditional expression is constant
  41. # endif
  42. # include "opentelemetry/nostd/internal/absl/base/options.h"
  43. namespace absl
  44. {
  45. namespace OTABSL_OPTION_NAMESPACE_NAME
  46. {
  47. template <class T>
  48. struct variant_size;
  49. template <typename... Ts>
  50. class variant;
  51. } // namespace OTABSL_OPTION_NAMESPACE_NAME
  52. } // namespace absl
  53. # include "opentelemetry/nostd/internal/absl/types/variant.h"
  54. # ifdef _MSC_VER
  55. # pragma warning(pop)
  56. # endif
  57. OPENTELEMETRY_BEGIN_NAMESPACE
  58. namespace nostd
  59. {
  60. using absl::OTABSL_OPTION_NAMESPACE_NAME::get;
  61. using absl::OTABSL_OPTION_NAMESPACE_NAME::get_if;
  62. using absl::OTABSL_OPTION_NAMESPACE_NAME::holds_alternative;
  63. using absl::OTABSL_OPTION_NAMESPACE_NAME::monostate;
  64. using absl::OTABSL_OPTION_NAMESPACE_NAME::variant;
  65. using absl::OTABSL_OPTION_NAMESPACE_NAME::variant_alternative_t;
  66. using absl::OTABSL_OPTION_NAMESPACE_NAME::variant_size;
  67. using absl::OTABSL_OPTION_NAMESPACE_NAME::visit;
  68. } // namespace nostd
  69. OPENTELEMETRY_END_NAMESPACE
  70. #endif /* OPENTELEMETRY_HAVE_STD_VARIANT */