NVIDIA DeepStream SDK API Reference

9.2 Release
includes/opentelemetry/context/context.h
Go to the documentation of this file.
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #include <cstring>
7 #include <utility>
8 
9 #include "opentelemetry/context/context_value.h"
10 #include "opentelemetry/nostd/shared_ptr.h"
11 #include "opentelemetry/nostd/string_view.h"
12 #include "opentelemetry/nostd/variant.h"
13 #include "opentelemetry/version.h"
14 
16 namespace context
17 {
18 
19 // The context class provides a context identifier. Is built as a linked list
20 // of DataList nodes and each context holds a shared_ptr to a place within
21 // the list that determines which keys and values it has access to. All that
22 // come before and none that come after.
23 class Context
24 {
25 
26 public:
27  Context() = default;
28  // Creates a context object from a map of keys and identifiers, this will
29  // hold a shared_ptr to the head of the DataList linked list
30  template <class T>
31  Context(const T &keys_and_values) noexcept
32  : head_{nostd::shared_ptr<DataList>{new DataList(keys_and_values)}}
33  {}
34 
35  // Creates a context object from a key and value, this will
36  // hold a shared_ptr to the head of the DataList linked list
38  : head_{nostd::shared_ptr<DataList>{new DataList(key, value)}}
39  {}
40 
41  // Accepts a new iterable and then returns a new context that
42  // contains the new key and value data. It attaches the
43  // exisiting list to the end of the new list.
44  template <class T>
45  Context SetValues(T &values) noexcept
46  {
47  Context context = Context(values);
49  while (last->next_ != nullptr)
50  {
51  last = last->next_;
52  }
53  last->next_ = head_;
54  return context;
55  }
56 
57  // Accepts a new iterable and then returns a new context that
58  // contains the new key and value data. It attaches the
59  // exisiting list to the end of the new list.
61  {
62  Context context = Context(key, value);
63  context.head_->next_ = head_;
64  return context;
65  }
66 
67  // Returns the value associated with the passed in key.
69  {
70  for (DataList *data = head_.get(); data != nullptr; data = data->next_.get())
71  {
72  if (key.size() == data->key_length_)
73  {
74  if (std::memcmp(key.data(), data->key_, data->key_length_) == 0)
75  {
76  return data->value_;
77  }
78  }
79  }
80  return ContextValue{};
81  }
82 
83  // Checks for key and returns true if found
84  bool HasKey(const nostd::string_view key) const noexcept
85  {
86  return !nostd::holds_alternative<nostd::monostate>(GetValue(key));
87  }
88 
89  bool operator==(const Context &other) const noexcept { return (head_ == other.head_); }
90 
91 private:
92  // A linked list to contain the keys and values of this context node
93  struct DataList
94  {
95  char *key_ = nullptr;
96 
97  nostd::shared_ptr<DataList> next_{nullptr};
98 
99  size_t key_length_ = 0UL;
100 
101  ContextValue value_;
102 
103  DataList() = default;
104 
105  // Builds a data list off of a key and value iterable and returns the head
106  template <class T>
107  DataList(const T &keys_and_vals)
108  {
109  bool first = true;
110  auto *node = this;
111  for (auto &iter : keys_and_vals)
112  {
113  if (first)
114  {
115  *node = DataList(iter.first, iter.second);
116  first = false;
117  }
118  else
119  {
120  node->next_ = nostd::shared_ptr<DataList>(new DataList(iter.first, iter.second));
121  node = node->next_.get();
122  }
123  }
124  }
125 
126  // Builds a data list with just a key and value, so it will just be the head
127  // and returns that head.
128  DataList(nostd::string_view key, const ContextValue &value)
129  {
130  key_ = new char[key.size()];
131  key_length_ = key.size();
132  std::memcpy(key_, key.data(), key.size() * sizeof(char));
133  next_ = nostd::shared_ptr<DataList>{nullptr};
134  value_ = value;
135  }
136 
137  DataList(const DataList &other)
138  : key_(new char[other.key_length_]),
139  next_(other.next_),
140  key_length_(other.key_length_),
141  value_(other.value_)
142  {
143  std::memcpy(key_, other.key_, other.key_length_ * sizeof(char));
144  }
145 
146  DataList &operator=(DataList &&other) noexcept
147  {
148  key_length_ = other.key_length_;
149  value_ = std::move(other.value_);
150  next_ = std::move(other.next_);
151 
152  key_ = other.key_;
153  other.key_ = nullptr;
154 
155  return *this;
156  }
157 
158  ~DataList()
159  {
160  if (key_ != nullptr)
161  {
162  delete[] key_;
163  }
164  }
165  };
166 
167  // Head of the list which holds the keys and values of this context
169 };
170 } // namespace context
absl::OTABSL_OPTION_NAMESPACE_NAME::variant
Definition: includes/opentelemetry/nostd/variant.h:61
context::Context::Context
Context()=default
nostd::shared_ptr< DataList >
nostd::shared_ptr::get
pointer get() const noexcept
Definition: includes/opentelemetry/nostd/shared_ptr.h:146
context::Context::Context
Context(const T &keys_and_values) noexcept
Definition: includes/opentelemetry/context/context.h:31
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
context
Definition: includes/opentelemetry/context/context.h:16
context::ContextValue
nostd::variant< nostd::monostate, bool, int64_t, uint64_t, double, nostd::shared_ptr< trace::Span >, nostd::shared_ptr< trace::SpanContext >, nostd::shared_ptr< baggage::Baggage > > ContextValue
Definition: includes/opentelemetry/context/context_value.h:33
context::Context::SetValue
Context SetValue(nostd::string_view key, ContextValue value) noexcept
Definition: includes/opentelemetry/context/context.h:60
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
nostd::string_view::size
size_type size() const noexcept
Definition: includes/opentelemetry/nostd/string_view.h:62
context::Context::SetValues
Context SetValues(T &values) noexcept
Definition: includes/opentelemetry/context/context.h:45
absl::move
constexpr absl::OTABSL_OPTION_NAMESPACE_NAME::remove_reference_t< T > && move(T &&t) noexcept
Definition: includes/opentelemetry/nostd/internal/absl/utility/utility.h:199
context::Context::Context
Context(nostd::string_view key, ContextValue value) noexcept
Definition: includes/opentelemetry/context/context.h:37
nostd::string_view::data
const char * data() const noexcept
Definition: includes/opentelemetry/nostd/string_view.h:56
context::Context
Definition: includes/opentelemetry/context/context.h:23
data
JSAMPIMAGE data
Definition: sources/libs/nvll_batch_jpeg_encode/oss/jpeglib.h:967
context::Context::operator==
bool operator==(const Context &other) const noexcept
Definition: includes/opentelemetry/context/context.h:89
context::Context::GetValue
context::ContextValue GetValue(const nostd::string_view key) const noexcept
Definition: includes/opentelemetry/context/context.h:68
nostd::string_view
Back port of std::string_view to work with pre-cpp-17 compilers.
Definition: includes/opentelemetry/nostd/string_view.h:37
context::Context::HasKey
bool HasKey(const nostd::string_view key) const noexcept
Definition: includes/opentelemetry/context/context.h:84