/proc/self/cwd/common/value_kind.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 | | #ifndef THIRD_PARTY_CEL_CPP_COMMON_VALUE_KIND_H_ |
16 | | #define THIRD_PARTY_CEL_CPP_COMMON_VALUE_KIND_H_ |
17 | | |
18 | | #include <cstdint> |
19 | | #include <type_traits> |
20 | | |
21 | | #include "absl/base/macros.h" |
22 | | #include "absl/strings/string_view.h" |
23 | | #include "common/kind.h" |
24 | | |
25 | | namespace cel { |
26 | | |
27 | | // `ValueKind` is a subset of `Kind`, representing all valid `Kind` for `Value`. |
28 | | // All `ValueKind` are valid `Kind`, but it is not guaranteed that all `Kind` |
29 | | // are valid `ValueKind`. |
30 | | enum class ValueKind : std::underlying_type_t<Kind> { |
31 | | kNull = static_cast<uint8_t>(Kind::kNull), |
32 | | kBool = static_cast<uint8_t>(Kind::kBool), |
33 | | kInt = static_cast<uint8_t>(Kind::kInt), |
34 | | kUint = static_cast<uint8_t>(Kind::kUint), |
35 | | kDouble = static_cast<uint8_t>(Kind::kDouble), |
36 | | kString = static_cast<uint8_t>(Kind::kString), |
37 | | kBytes = static_cast<uint8_t>(Kind::kBytes), |
38 | | kStruct = static_cast<uint8_t>(Kind::kStruct), |
39 | | kDuration = static_cast<uint8_t>(Kind::kDuration), |
40 | | kTimestamp = static_cast<uint8_t>(Kind::kTimestamp), |
41 | | kList = static_cast<uint8_t>(Kind::kList), |
42 | | kMap = static_cast<uint8_t>(Kind::kMap), |
43 | | kUnknown = static_cast<uint8_t>(Kind::kUnknown), |
44 | | kType = static_cast<uint8_t>(Kind::kType), |
45 | | kError = static_cast<uint8_t>(Kind::kError), |
46 | | kOpaque = static_cast<uint8_t>(Kind::kOpaque), |
47 | | |
48 | | // Legacy aliases, deprecated do not use. |
49 | | kNullType = kNull, |
50 | | kInt64 = kInt, |
51 | | kUint64 = kUint, |
52 | | kMessage = kStruct, |
53 | | kUnknownSet = kUnknown, |
54 | | kCelType = kType, |
55 | | |
56 | | // INTERNAL: Do not exceed 63. Implementation details rely on the fact that |
57 | | // we can store `Kind` using 6 bits. |
58 | | kNotForUseWithExhaustiveSwitchStatements = |
59 | | static_cast<uint8_t>(Kind::kNotForUseWithExhaustiveSwitchStatements), |
60 | | }; |
61 | | |
62 | 1.10M | constexpr Kind ValueKindToKind(ValueKind kind) { |
63 | 1.10M | return static_cast<Kind>( |
64 | 1.10M | static_cast<std::underlying_type_t<ValueKind>>(kind)); |
65 | 1.10M | } |
66 | | |
67 | 0 | constexpr bool KindIsValueKind(Kind kind) { |
68 | 0 | return kind != Kind::kBoolWrapper && kind != Kind::kIntWrapper && |
69 | 0 | kind != Kind::kUintWrapper && kind != Kind::kDoubleWrapper && |
70 | 0 | kind != Kind::kStringWrapper && kind != Kind::kBytesWrapper && |
71 | 0 | kind != Kind::kDyn && kind != Kind::kAny && kind != Kind::kTypeParam && |
72 | 0 | kind != Kind::kFunction; |
73 | 0 | } |
74 | | |
75 | 0 | constexpr bool operator==(Kind lhs, ValueKind rhs) { |
76 | 0 | return lhs == ValueKindToKind(rhs); |
77 | 0 | } |
78 | | |
79 | 1.09M | constexpr bool operator==(ValueKind lhs, Kind rhs) { |
80 | 1.09M | return ValueKindToKind(lhs) == rhs; |
81 | 1.09M | } |
82 | | |
83 | 0 | constexpr bool operator!=(Kind lhs, ValueKind rhs) { |
84 | 0 | return !operator==(lhs, rhs); |
85 | 0 | } |
86 | | |
87 | 1.09M | constexpr bool operator!=(ValueKind lhs, Kind rhs) { |
88 | 1.09M | return !operator==(lhs, rhs); |
89 | 1.09M | } |
90 | | |
91 | 348 | inline absl::string_view ValueKindToString(ValueKind kind) { |
92 | | // All ValueKind are valid Kind. |
93 | 348 | return KindToString(ValueKindToKind(kind)); |
94 | 348 | } |
95 | | |
96 | 0 | constexpr ValueKind KindToValueKind(Kind kind) { |
97 | 0 | ABSL_ASSERT(KindIsValueKind(kind)); |
98 | 0 | return static_cast<ValueKind>( |
99 | 0 | static_cast<std::underlying_type_t<Kind>>(kind)); |
100 | 0 | } |
101 | | |
102 | | } // namespace cel |
103 | | |
104 | | #endif // THIRD_PARTY_CEL_CPP_COMMON_VALUE_KIND_H_ |