key_value_iterable.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "opentelemetry/common/attribute_value.h"
  5. #include "opentelemetry/nostd/function_ref.h"
  6. #include "opentelemetry/nostd/string_view.h"
  7. #include "opentelemetry/version.h"
  8. OPENTELEMETRY_BEGIN_NAMESPACE
  9. namespace common
  10. {
  11. /**
  12. * Supports internal iteration over a collection of key-value pairs.
  13. */
  14. class KeyValueIterable
  15. {
  16. public:
  17. virtual ~KeyValueIterable() = default;
  18. /**
  19. * Iterate over key-value pairs
  20. * @param callback a callback to invoke for each key-value. If the callback returns false,
  21. * the iteration is aborted.
  22. * @return true if every key-value pair was iterated over
  23. */
  24. virtual bool ForEachKeyValue(nostd::function_ref<bool(nostd::string_view, common::AttributeValue)>
  25. callback) const noexcept = 0;
  26. /**
  27. * @return the number of key-value pairs
  28. */
  29. virtual size_t size() const noexcept = 0;
  30. };
  31. /**
  32. * Supports internal iteration over a collection of key-value pairs.
  33. */
  34. class NoopKeyValueIterable : public KeyValueIterable
  35. {
  36. public:
  37. ~NoopKeyValueIterable() override = default;
  38. /**
  39. * Iterate over key-value pairs
  40. * @param callback a callback to invoke for each key-value. If the callback returns false,
  41. * the iteration is aborted.
  42. * @return true if every key-value pair was iterated over
  43. */
  44. bool ForEachKeyValue(
  45. nostd::function_ref<bool(nostd::string_view, common::AttributeValue)>) const noexcept override
  46. {
  47. return true;
  48. }
  49. /**
  50. * @return the number of key-value pairs
  51. */
  52. size_t size() const noexcept override { return 0; }
  53. };
  54. } // namespace common
  55. OPENTELEMETRY_END_NAMESPACE