]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // Copyright The OpenTelemetry Authors |
2 | // SPDX-License-Identifier: Apache-2.0 | |
3 | ||
4 | #pragma once | |
5 | ||
6 | #include <iterator> | |
7 | #include <type_traits> | |
8 | #include <utility> | |
9 | ||
10 | #include "opentelemetry/common/key_value_iterable.h" | |
11 | #include "opentelemetry/nostd/utility.h" | |
12 | #include "opentelemetry/version.h" | |
13 | ||
14 | OPENTELEMETRY_BEGIN_NAMESPACE | |
15 | namespace common | |
16 | { | |
17 | // NOTE - code within `detail` namespace implements internal details, and not part | |
18 | // of the public interface. | |
19 | namespace detail | |
20 | { | |
21 | inline void take_key_value(nostd::string_view, common::AttributeValue) {} | |
22 | ||
23 | template <class T> | |
24 | auto is_key_value_iterable_impl(T iterable) | |
25 | -> decltype(take_key_value(std::begin(iterable)->first, std::begin(iterable)->second), | |
26 | nostd::size(iterable), | |
27 | std::true_type{}); | |
28 | ||
29 | std::false_type is_key_value_iterable_impl(...); | |
30 | ||
31 | template <class T> | |
32 | struct is_key_value_iterable | |
33 | { | |
34 | static const bool value = decltype(detail::is_key_value_iterable_impl(std::declval<T>()))::value; | |
35 | }; | |
36 | } // namespace detail | |
37 | ||
38 | /** | |
39 | * @brief Container for key-value pairs that can transform every value in it to one of types | |
40 | * listed in common::AttributeValue. It may contain value types that are not directly map'able | |
41 | * to primitive value types. In that case the `ForEachKeyValue` method acts as a transform to | |
42 | * convert the value type to one listed under AtributeValue (bool, int32_t, int64_t, uint32_t, | |
43 | * uint64_t, double, nostd::string_view, or arrays of primite types). For example, if UUID, | |
44 | * GUID, or UTF-16 string type is passed as one of values stored inside this container, the | |
45 | * container itself may provide a custom implementation of `ForEachKeyValue` to transform the | |
46 | * 'non-standard' type to one of the standard types. | |
47 | */ | |
48 | template <class T> | |
49 | class KeyValueIterableView final : public KeyValueIterable | |
50 | { | |
51 | ||
52 | public: | |
53 | explicit KeyValueIterableView(const T &container) noexcept : container_{&container} {} | |
54 | ||
55 | // KeyValueIterable | |
56 | bool ForEachKeyValue(nostd::function_ref<bool(nostd::string_view, common::AttributeValue)> | |
57 | callback) const noexcept override | |
58 | { | |
59 | auto iter = std::begin(*container_); | |
60 | auto last = std::end(*container_); | |
61 | for (; iter != last; ++iter) | |
62 | { | |
63 | if (!callback(iter->first, iter->second)) | |
64 | { | |
65 | return false; | |
66 | } | |
67 | } | |
68 | return true; | |
69 | } | |
70 | ||
71 | size_t size() const noexcept override { return nostd::size(*container_); } | |
72 | ||
73 | private: | |
74 | const T *container_; | |
75 | }; | |
76 | } // namespace common | |
77 | OPENTELEMETRY_END_NAMESPACE |