Skip to content

Commit

Permalink
Make ContextValue default constructible with monostate (#731)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomsonTan authored May 13, 2021
1 parent 834bba6 commit 414a731
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 18 deletions.
14 changes: 2 additions & 12 deletions api/include/opentelemetry/context/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<nostd::monostate>(GetValue(key));
}

bool operator==(const Context &other) const noexcept { return (head_ == other.head_); }
Expand Down
3 changes: 2 additions & 1 deletion api/include/opentelemetry/context/context_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
OPENTELEMETRY_BEGIN_NAMESPACE
namespace context
{
using ContextValue = nostd::variant<bool,
using ContextValue = nostd::variant<nostd::monostate,
bool,
int64_t,
uint64_t,
double,
Expand Down
2 changes: 2 additions & 0 deletions api/include/opentelemetry/nostd/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ namespace nostd
{
using absl::get;
using absl::holds_alternative;
using absl::monostate;
using absl::variant;
using absl::variant_size;
using absl::visit;

// nostd::bad_variant_access
Expand Down
2 changes: 2 additions & 0 deletions api/include/opentelemetry/std/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ using variant = std::variant<_Types...>;
template <class... _Types>
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
Expand Down
6 changes: 3 additions & 3 deletions api/test/context/context_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64_t>(context_test.GetValue("foo_key")), 456);
EXPECT_FALSE(nostd::holds_alternative<int64_t>(context_test.GetValue("foo_key")));
}

// Tests that writing the same to a context overwrites the original value.
Expand Down Expand Up @@ -89,8 +89,8 @@ TEST(ContextTest, ContextInheritance)
EXPECT_EQ(nostd::get<int64_t>(foo_context.GetValue("other_key")), 789);
EXPECT_EQ(nostd::get<int64_t>(foo_context.GetValue("another_key")), 987);

EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("other_key")), 0);
EXPECT_EQ(nostd::get<int64_t>(test_context.GetValue("another_key")), 0);
EXPECT_TRUE(nostd::holds_alternative<nostd::monostate>(test_context.GetValue("other_key")));
EXPECT_TRUE(nostd::holds_alternative<nostd::monostate>(test_context.GetValue("another_key")));
}

// Tests that copying a context copies the key value pairs as expected.
Expand Down
7 changes: 5 additions & 2 deletions sdk/src/common/core.cc
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 414a731

Please sign in to comment.