/proc/self/cwd/common/values/custom_value.h
Line | Count | Source |
1 | | // Copyright 2023 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | // IWYU pragma: private, include "common/value.h" |
16 | | // IWYU pragma: friend "common/value.h" |
17 | | |
18 | | #ifndef THIRD_PARTY_CEL_CPP_COMMON_VALUES_CUSTOM_VALUE_H_ |
19 | | #define THIRD_PARTY_CEL_CPP_COMMON_VALUES_CUSTOM_VALUE_H_ |
20 | | |
21 | | #include <cstddef> |
22 | | #include <cstring> |
23 | | #include <memory> |
24 | | #include <type_traits> |
25 | | |
26 | | namespace cel { |
27 | | |
28 | | // CustomValueContent is an opaque 16-byte trivially copyable value. The format |
29 | | // of the data stored within is unknown to everything except the the caller |
30 | | // which creates it. Do not try to interpret it otherwise. |
31 | | class CustomValueContent final { |
32 | | public: |
33 | 26.3k | static CustomValueContent Zero() { |
34 | 26.3k | CustomValueContent content; |
35 | 26.3k | std::memset(&content, 0, sizeof(content)); |
36 | 26.3k | return content; |
37 | 26.3k | } |
38 | | |
39 | | template <typename T> |
40 | 26.3k | static CustomValueContent From(T value) { |
41 | 26.3k | static_assert(std::is_trivially_copyable_v<T>, |
42 | 26.3k | "T must be trivially copyable"); |
43 | 26.3k | static_assert(sizeof(T) <= 16, "sizeof(T) must be no greater than 16"); |
44 | | |
45 | 26.3k | CustomValueContent content; |
46 | 26.3k | std::memcpy(content.raw_, std::addressof(value), sizeof(T)); |
47 | 26.3k | return content; |
48 | 26.3k | } cel::CustomValueContent cel::CustomValueContent::From<cel::CustomListValueInterface::Content>(cel::CustomListValueInterface::Content) Line | Count | Source | 40 | 18.1k | static CustomValueContent From(T value) { | 41 | 18.1k | static_assert(std::is_trivially_copyable_v<T>, | 42 | 18.1k | "T must be trivially copyable"); | 43 | 18.1k | static_assert(sizeof(T) <= 16, "sizeof(T) must be no greater than 16"); | 44 | | | 45 | 18.1k | CustomValueContent content; | 46 | 18.1k | std::memcpy(content.raw_, std::addressof(value), sizeof(T)); | 47 | 18.1k | return content; | 48 | 18.1k | } |
cel::CustomValueContent cel::CustomValueContent::From<cel::CustomMapValueInterface::Content>(cel::CustomMapValueInterface::Content) Line | Count | Source | 40 | 8.13k | static CustomValueContent From(T value) { | 41 | 8.13k | static_assert(std::is_trivially_copyable_v<T>, | 42 | 8.13k | "T must be trivially copyable"); | 43 | 8.13k | static_assert(sizeof(T) <= 16, "sizeof(T) must be no greater than 16"); | 44 | | | 45 | 8.13k | CustomValueContent content; | 46 | 8.13k | std::memcpy(content.raw_, std::addressof(value), sizeof(T)); | 47 | 8.13k | return content; | 48 | 8.13k | } |
Unexecuted instantiation: cel::CustomValueContent cel::CustomValueContent::From<cel::CustomStructValueInterface::Content>(cel::CustomStructValueInterface::Content) Unexecuted instantiation: cel::CustomValueContent cel::CustomValueContent::From<cel::OpaqueValueInterface::Content>(cel::OpaqueValueInterface::Content) Unexecuted instantiation: optional_value.cc:cel::CustomValueContent cel::CustomValueContent::From<cel::(anonymous namespace)::OptionalValueContent>(cel::(anonymous namespace)::OptionalValueContent) Unexecuted instantiation: cel::CustomValueContent cel::CustomValueContent::From<bool>(bool) Unexecuted instantiation: cel::CustomValueContent cel::CustomValueContent::From<long>(long) Unexecuted instantiation: cel::CustomValueContent cel::CustomValueContent::From<unsigned long>(unsigned long) Unexecuted instantiation: cel::CustomValueContent cel::CustomValueContent::From<double>(double) Unexecuted instantiation: cel::CustomValueContent cel::CustomValueContent::From<absl::lts_20260107::Duration>(absl::lts_20260107::Duration) Unexecuted instantiation: cel::CustomValueContent cel::CustomValueContent::From<absl::lts_20260107::Time>(absl::lts_20260107::Time) |
49 | | |
50 | | template <typename T, size_t N> |
51 | | static CustomValueContent From(const T (&array)[N]) { |
52 | | static_assert(std::is_trivially_copyable_v<T>, |
53 | | "T must be trivially copyable"); |
54 | | static_assert((sizeof(T) * N) <= 16, |
55 | | "sizeof(T[N]) must be no greater than 16"); |
56 | | |
57 | | CustomValueContent content; |
58 | | std::memcpy(content.raw_, array, sizeof(T) * N); |
59 | | return content; |
60 | | } |
61 | | |
62 | | template <typename T> |
63 | 128k | T To() const { |
64 | 128k | static_assert(std::is_trivially_copyable_v<T>, |
65 | 128k | "T must be trivially copyable"); |
66 | 128k | static_assert(sizeof(T) <= 16, "sizeof(T) must be no greater than 16"); |
67 | | |
68 | 128k | T value; |
69 | 128k | std::memcpy(std::addressof(value), raw_, sizeof(T)); |
70 | 128k | return value; |
71 | 128k | } cel::CustomListValueInterface::Content cel::CustomValueContent::To<cel::CustomListValueInterface::Content>() const Line | Count | Source | 63 | 122k | T To() const { | 64 | 122k | static_assert(std::is_trivially_copyable_v<T>, | 65 | 122k | "T must be trivially copyable"); | 66 | 122k | static_assert(sizeof(T) <= 16, "sizeof(T) must be no greater than 16"); | 67 | | | 68 | 122k | T value; | 69 | 122k | std::memcpy(std::addressof(value), raw_, sizeof(T)); | 70 | 122k | return value; | 71 | 122k | } |
cel::CustomMapValueInterface::Content cel::CustomValueContent::To<cel::CustomMapValueInterface::Content>() const Line | Count | Source | 63 | 6.04k | T To() const { | 64 | 6.04k | static_assert(std::is_trivially_copyable_v<T>, | 65 | 6.04k | "T must be trivially copyable"); | 66 | 6.04k | static_assert(sizeof(T) <= 16, "sizeof(T) must be no greater than 16"); | 67 | | | 68 | 6.04k | T value; | 69 | 6.04k | std::memcpy(std::addressof(value), raw_, sizeof(T)); | 70 | 6.04k | return value; | 71 | 6.04k | } |
Unexecuted instantiation: cel::CustomStructValueInterface::Content cel::CustomValueContent::To<cel::CustomStructValueInterface::Content>() const Unexecuted instantiation: cel::OpaqueValueInterface::Content cel::CustomValueContent::To<cel::OpaqueValueInterface::Content>() const Unexecuted instantiation: bool cel::CustomValueContent::To<bool>() const Unexecuted instantiation: long cel::CustomValueContent::To<long>() const Unexecuted instantiation: unsigned long cel::CustomValueContent::To<unsigned long>() const Unexecuted instantiation: double cel::CustomValueContent::To<double>() const Unexecuted instantiation: absl::lts_20260107::Duration cel::CustomValueContent::To<absl::lts_20260107::Duration>() const Unexecuted instantiation: absl::lts_20260107::Time cel::CustomValueContent::To<absl::lts_20260107::Time>() const Unexecuted instantiation: optional_value.cc:cel::(anonymous namespace)::OptionalValueContent cel::CustomValueContent::To<cel::(anonymous namespace)::OptionalValueContent>() const |
72 | | |
73 | 0 | bool IsZero() const { |
74 | 0 | static const CustomValueContent kZero = Zero(); |
75 | 0 | return std::memcmp(raw_, kZero.raw_, sizeof(raw_)) == 0; |
76 | 0 | } |
77 | | |
78 | | private: |
79 | | alignas(void*) std::byte raw_[16]; |
80 | | }; |
81 | | |
82 | | } // namespace cel |
83 | | |
84 | | #endif // THIRD_PARTY_CEL_CPP_COMMON_VALUES_CUSTOM_VALUE_H_ |