NVIDIA DeepStream SDK API Reference

9.2 Release
9.2/includes/opentelemetry/common/kv_properties.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 "opentelemetry/common/key_value_iterable_view.h"
7 #include "opentelemetry/common/string_util.h"
8 #include "opentelemetry/nostd/function_ref.h"
9 #include "opentelemetry/nostd/string_view.h"
10 #include "opentelemetry/nostd/unique_ptr.h"
11 #include "opentelemetry/version.h"
12 
13 #include <cstring>
14 #include <string>
15 #include <type_traits>
16 
18 namespace common
19 {
20 
21 // Constructor parameter for KeyValueStringTokenizer
22 struct KeyValueStringTokenizerOptions
23 {
24  char member_separator = ',';
25  char key_value_separator = '=';
26  bool ignore_empty_members = true;
27 };
28 
29 // Tokenizer for key-value headers
30 class KeyValueStringTokenizer
31 {
32 public:
36  : str_(str), opts_(opts), index_(0)
37  {}
38 
40  {
41  static std::string default_str = "";
42  return default_str;
43  }
44 
45  // Returns next key value in the string header
46  // @param valid_kv : if the found kv pair is valid or not
47  // @param key : key in kv pair
48  // @param key : value in kv pair
49  // @returns true if next kv pair was found, false otherwise.
50  bool next(bool &valid_kv, nostd::string_view &key, nostd::string_view &value) noexcept
51  {
52  valid_kv = true;
53  while (index_ < str_.size())
54  {
55  bool is_empty_pair = false;
56  size_t end = str_.find(opts_.member_separator, index_);
57  if (end == std::string::npos)
58  {
59  end = str_.size() - 1;
60  }
61  else if (end == index_) // empty pair. do not update end
62  {
63  is_empty_pair = true;
64  }
65  else
66  {
67  end--;
68  }
69 
70  auto list_member = StringUtil::Trim(str_, index_, end);
71  if (list_member.size() == 0 || is_empty_pair)
72  {
73  // empty list member
74  index_ = end + 2 - is_empty_pair;
75  if (opts_.ignore_empty_members)
76  {
77  continue;
78  }
79 
80  valid_kv = true;
81  key = GetDefaultKeyOrValue();
82  value = GetDefaultKeyOrValue();
83  return true;
84  }
85 
86  auto key_end_pos = list_member.find(opts_.key_value_separator);
87  if (key_end_pos == std::string::npos)
88  {
89  // invalid member
90  valid_kv = false;
91  }
92  else
93  {
94  key = list_member.substr(0, key_end_pos);
95  value = list_member.substr(key_end_pos + 1);
96  }
97 
98  index_ = end + 2;
99 
100  return true;
101  }
102 
103  // no more entries remaining
104  return false;
105  }
106 
107  // Returns total number of tokens in header string
108  size_t NumTokens() const noexcept
109  {
110  size_t cnt = 0, begin = 0;
111  while (begin < str_.size())
112  {
113  ++cnt;
114  size_t end = str_.find(opts_.member_separator, begin);
115  if (end == std::string::npos)
116  {
117  break;
118  }
119 
120  begin = end + 1;
121  }
122 
123  return cnt;
124  }
125 
126  // Resets the iterator
127  void reset() noexcept { index_ = 0; }
128 
129 private:
130  nostd::string_view str_;
132  size_t index_;
133 };
134 
135 // Class to store fixed size array of key-value pairs of string type
136 class KeyValueProperties
137 {
138  // Class to store key-value pairs of string types
139 public:
140  class Entry
141  {
142  public:
143  Entry() : key_(nullptr), value_(nullptr) {}
144 
145  // Copy constructor
146  Entry(const Entry &copy)
147  {
148  key_ = CopyStringToPointer(copy.key_.get());
149  value_ = CopyStringToPointer(copy.value_.get());
150  }
151 
152  // Copy assignment operator
154  {
155  key_ = CopyStringToPointer(other.key_.get());
156  value_ = CopyStringToPointer(other.value_.get());
157  return *this;
158  }
159 
160  // Move contructor and assignment operator
161  Entry(Entry &&other) = default;
162  Entry &operator=(Entry &&other) = default;
163 
164  // Creates an Entry for a given key-value pair.
166  {
167  key_ = CopyStringToPointer(key);
168  value_ = CopyStringToPointer(value);
169  }
170 
171  // Gets the key associated with this entry.
172  nostd::string_view GetKey() const noexcept { return key_.get(); }
173 
174  // Gets the value associated with this entry.
175  nostd::string_view GetValue() const noexcept { return value_.get(); }
176 
177  // Sets the value for this entry. This overrides the previous value.
178  void SetValue(nostd::string_view value) noexcept { value_ = CopyStringToPointer(value); }
179 
180  private:
181  // Store key and value as raw char pointers to avoid using std::string.
184 
185  // Copies string into a buffer and returns a unique_ptr to the buffer.
186  // This is a workaround for the fact that memcpy doesn't accept a const destination.
187  nostd::unique_ptr<const char[]> CopyStringToPointer(nostd::string_view str)
188  {
189  char *temp = new char[str.size() + 1];
190  memcpy(temp, str.data(), str.size());
191  temp[str.size()] = '\0';
192  return nostd::unique_ptr<const char[]>(temp);
193  }
194  };
195 
196  // Maintain the number of entries in entries_.
197  size_t num_entries_;
198 
199  // Max size of allocated array
200  size_t max_num_entries_;
201 
202  // Store entries in a C-style array to avoid using std::array or std::vector.
204 
205 public:
206  // Create Key-value list of given size
207  // @param size : Size of list.
208  KeyValueProperties(size_t size) noexcept
210  {}
211 
212  // Create Empty Key-Value list
213  KeyValueProperties() noexcept : num_entries_(0), max_num_entries_(0), entries_(nullptr) {}
214 
215  template <class T, class = typename std::enable_if<detail::is_key_value_iterable<T>::value>::type>
216  KeyValueProperties(const T &keys_and_values) noexcept
217  : num_entries_(0),
218  max_num_entries_(keys_and_values.size()),
220  {
221  for (auto &e : keys_and_values)
222  {
223  Entry entry(e.first, e.second);
224  (entries_.get())[num_entries_++] = std::move(entry);
225  }
226  }
227 
228  // Adds new kv pair into kv properties
230  {
232  {
233  Entry entry(key, value);
234  (entries_.get())[num_entries_++] = std::move(entry);
235  }
236  }
237 
238  // Returns all kv pair entries
240  nostd::function_ref<bool(nostd::string_view, nostd::string_view)> callback) const noexcept
241  {
242  for (size_t i = 0; i < num_entries_; i++)
243  {
244  auto &entry = (entries_.get())[i];
245  if (!callback(entry.GetKey(), entry.GetValue()))
246  {
247  return false;
248  }
249  }
250  return true;
251  }
252 
253  // Return value for key if exists, return false otherwise
254  bool GetValue(nostd::string_view key, std::string &value) const noexcept
255  {
256  for (size_t i = 0; i < num_entries_; i++)
257  {
258  auto &entry = (entries_.get())[i];
259  if (entry.GetKey() == key)
260  {
261  const auto &entry_value = entry.GetValue();
262  value = std::string(entry_value.data(), entry_value.size());
263  return true;
264  }
265  }
266  return false;
267  }
268 
269  size_t Size() const noexcept { return num_entries_; }
270 };
271 } // namespace common
common::KeyValueProperties::entries_
nostd::unique_ptr< Entry[]> entries_
Definition: includes/opentelemetry/common/kv_properties.h:203
common::KeyValueProperties::Entry::Entry
Entry()
Definition: 9.2/includes/opentelemetry/common/kv_properties.h:143
common::KeyValueStringTokenizerOptions
Definition: includes/opentelemetry/common/kv_properties.h:22
nostd::function_ref
Definition: includes/opentelemetry/nostd/function_ref.h:17
common::KeyValueProperties::GetValue
bool GetValue(nostd::string_view key, std::string &value) const noexcept
Definition: 9.2/includes/opentelemetry/common/kv_properties.h:254
common::KeyValueProperties::Entry
Definition: includes/opentelemetry/common/kv_properties.h:140
common::KeyValueStringTokenizer::KeyValueStringTokenizer
KeyValueStringTokenizer(nostd::string_view str, const KeyValueStringTokenizerOptions &opts=KeyValueStringTokenizerOptions()) noexcept
Definition: 9.2/includes/opentelemetry/common/kv_properties.h:33
common::KeyValueProperties::Entry::SetValue
void SetValue(nostd::string_view value) noexcept
Definition: 9.2/includes/opentelemetry/common/kv_properties.h:178
common::KeyValueProperties::Entry::operator=
Entry & operator=(Entry &other)
Definition: 9.2/includes/opentelemetry/common/kv_properties.h:153
nostd::unique_ptr< const char[]>
common::KeyValueProperties::num_entries_
size_t num_entries_
Definition: includes/opentelemetry/common/kv_properties.h:197
nostd::unique_ptr::get
pointer get() const noexcept
Definition: includes/opentelemetry/nostd/unique_ptr.h:107
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
common::KeyValueProperties::Entry::Entry
Entry(nostd::string_view key, nostd::string_view value)
Definition: 9.2/includes/opentelemetry/common/kv_properties.h:165
common::KeyValueStringTokenizer::reset
void reset() noexcept
Definition: 9.2/includes/opentelemetry/common/kv_properties.h:127
common::KeyValueProperties::Size
size_t Size() const noexcept
Definition: 9.2/includes/opentelemetry/common/kv_properties.h:269
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
common::KeyValueStringTokenizerOptions::key_value_separator
char key_value_separator
Definition: includes/opentelemetry/common/kv_properties.h:25
nostd::string_view::size
size_type size() const noexcept
Definition: includes/opentelemetry/nostd/string_view.h:62
common::KeyValueProperties::AddEntry
void AddEntry(nostd::string_view key, nostd::string_view value) noexcept
Definition: 9.2/includes/opentelemetry/common/kv_properties.h:229
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
common::KeyValueStringTokenizerOptions::member_separator
char member_separator
Definition: includes/opentelemetry/common/kv_properties.h:24
common::KeyValueProperties::KeyValueProperties
KeyValueProperties() noexcept
Definition: 9.2/includes/opentelemetry/common/kv_properties.h:213
common::KeyValueProperties::Entry::GetValue
nostd::string_view GetValue() const noexcept
Definition: 9.2/includes/opentelemetry/common/kv_properties.h:175
common::KeyValueStringTokenizer::next
bool next(bool &valid_kv, nostd::string_view &key, nostd::string_view &value) noexcept
Definition: 9.2/includes/opentelemetry/common/kv_properties.h:50
pydeepstreamdoc::tensor::TensorDoc::size
constexpr const char * size
Definition: service-maker/sources/python/src/bindings/docstrings/tensordoc.h:46
common::KeyValueProperties::Entry::GetKey
nostd::string_view GetKey() const noexcept
Definition: 9.2/includes/opentelemetry/common/kv_properties.h:172
common::KeyValueProperties::KeyValueProperties
KeyValueProperties(size_t size) noexcept
Definition: 9.2/includes/opentelemetry/common/kv_properties.h:208
common::KeyValueProperties::max_num_entries_
size_t max_num_entries_
Definition: includes/opentelemetry/common/kv_properties.h:200
common::KeyValueProperties::KeyValueProperties
KeyValueProperties(const T &keys_and_values) noexcept
Definition: 9.2/includes/opentelemetry/common/kv_properties.h:216
common::KeyValueProperties::GetAllEntries
bool GetAllEntries(nostd::function_ref< bool(nostd::string_view, nostd::string_view)> callback) const noexcept
Definition: 9.2/includes/opentelemetry/common/kv_properties.h:239
common::StringUtil::Trim
static nostd::string_view Trim(nostd::string_view str, size_t left, size_t right) noexcept
Definition: includes/opentelemetry/common/string_util.h:18
nostd::string_view::data
const char * data() const noexcept
Definition: includes/opentelemetry/nostd/string_view.h:56
nostd::string_view::find
size_type find(char ch, size_type pos=0) const noexcept
Definition: includes/opentelemetry/nostd/string_view.h:119
common::KeyValueStringTokenizerOptions::ignore_empty_members
bool ignore_empty_members
Definition: includes/opentelemetry/common/kv_properties.h:26
common::KeyValueStringTokenizer::GetDefaultKeyOrValue
static nostd::string_view GetDefaultKeyOrValue()
Definition: 9.2/includes/opentelemetry/common/kv_properties.h:39
common::KeyValueStringTokenizer::NumTokens
size_t NumTokens() const noexcept
Definition: 9.2/includes/opentelemetry/common/kv_properties.h:108
common::KeyValueProperties::Entry::Entry
Entry(const Entry &copy)
Definition: 9.2/includes/opentelemetry/common/kv_properties.h:146
common
Definition: includes/opentelemetry/common/attribute_value.h:14
pydeepstreamdoc::osd::ArrowHeadDoc::end
constexpr const char * end
Definition: service-maker/sources/python/src/bindings/docstrings/osddoc.h:34
nostd::string_view
Back port of std::string_view to work with pre-cpp-17 compilers.
Definition: includes/opentelemetry/nostd/string_view.h:37