diff --git a/api/include/opentelemetry/context/context.h b/api/include/opentelemetry/context/context.h index 4d3384e288..318ba08dcb 100644 --- a/api/include/opentelemetry/context/context.h +++ b/api/include/opentelemetry/context/context.h @@ -72,23 +72,13 @@ class Context } } } - return (int64_t)0; + return ContextValue{}; } // Checks for key and returns true if found bool HasKey(const nostd::string_view key) const noexcept { - for (DataList *data = head_.get(); data != nullptr; data = data->next_.get()) - { - if (key.size() == data->key_length_) - { - if (std::memcmp(key.data(), data->key_, data->key_length_) == 0) - { - return true; - } - } - } - return false; + return !nostd::holds_alternative(GetValue(key)); } bool operator==(const Context &other) const noexcept { return (head_ == other.head_); } diff --git a/api/include/opentelemetry/context/context_value.h b/api/include/opentelemetry/context/context_value.h index d03e4289d4..b5fc2aee55 100644 --- a/api/include/opentelemetry/context/context_value.h +++ b/api/include/opentelemetry/context/context_value.h @@ -13,7 +13,8 @@ OPENTELEMETRY_BEGIN_NAMESPACE namespace context { -using ContextValue = nostd::variant; template using variant_size = std::variant_size<_Types...>; +using monostate = std::monostate; + #if defined(__APPLE__) && defined(_LIBCPP_USE_AVAILABILITY_APPLE) // Apple Platforms provide std::bad_variant_access only in newer versions of OS. // To keep API compatible with any version of OS - we are providing our own diff --git a/api/test/context/context_test.cc b/api/test/context/context_test.cc index ae3144443f..730563f842 100644 --- a/api/test/context/context_test.cc +++ b/api/test/context/context_test.cc @@ -58,7 +58,7 @@ TEST(ContextTest, ContextImmutability) context::Context context_test = context::Context(map_test); context::Context context_foo = context_test.SetValue("foo_key", (int64_t)456); - EXPECT_NE(nostd::get(context_test.GetValue("foo_key")), 456); + EXPECT_FALSE(nostd::holds_alternative(context_test.GetValue("foo_key"))); } // Tests that writing the same to a context overwrites the original value. @@ -89,8 +89,8 @@ TEST(ContextTest, ContextInheritance) EXPECT_EQ(nostd::get(foo_context.GetValue("other_key")), 789); EXPECT_EQ(nostd::get(foo_context.GetValue("another_key")), 987); - EXPECT_EQ(nostd::get(test_context.GetValue("other_key")), 0); - EXPECT_EQ(nostd::get(test_context.GetValue("another_key")), 0); + EXPECT_TRUE(nostd::holds_alternative(test_context.GetValue("other_key"))); + EXPECT_TRUE(nostd::holds_alternative(test_context.GetValue("another_key"))); } // Tests that copying a context copies the key value pairs as expected. diff --git a/sdk/src/common/core.cc b/sdk/src/common/core.cc index aaf013e2c6..c5025c2c95 100644 --- a/sdk/src/common/core.cc +++ b/sdk/src/common/core.cc @@ -1,7 +1,10 @@ -#include "opentelemetry/nostd/variant.h" +// clang-format off +// version.h should be included before nostd/variant.h. #include "opentelemetry/version.h" +#include "opentelemetry/nostd/variant.h" +// clang-format on -#if defined(HAVE_ABSEIL) +#if defined(HAVE_ABSEIL) && !defined(HAVE_ABSEIL_VARIANT) # if defined(__GNUC__) || defined(__GNUG__) # ifndef __cdecl