/proc/self/cwd/common/internal/casting.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/casting.h" |
16 | | |
17 | | #ifndef THIRD_PARTY_CEL_CPP_COMMON_INTERNAL_CASTING_H_ |
18 | | #define THIRD_PARTY_CEL_CPP_COMMON_INTERNAL_CASTING_H_ |
19 | | |
20 | | #include <memory> |
21 | | #include <type_traits> |
22 | | #include <utility> |
23 | | |
24 | | #include "absl/base/attributes.h" |
25 | | #include "absl/meta/type_traits.h" |
26 | | #include "absl/types/optional.h" |
27 | | #include "internal/casts.h" |
28 | | |
29 | | namespace cel { |
30 | | |
31 | | namespace common_internal { |
32 | | |
33 | | template <typename To, typename From> |
34 | | using propagate_const_t = |
35 | | std::conditional_t<std::is_const_v<std::remove_reference_t<From>>, |
36 | | std::add_const_t<To>, To>; |
37 | | |
38 | | template <typename To, typename From> |
39 | | using propagate_volatile_t = |
40 | | std::conditional_t<std::is_volatile_v<std::remove_reference_t<From>>, |
41 | | std::add_volatile_t<To>, To>; |
42 | | |
43 | | template <typename To, typename From> |
44 | | using propagate_reference_t = |
45 | | std::conditional_t<std::is_lvalue_reference_v<From>, |
46 | | std::add_lvalue_reference_t<To>, |
47 | | std::conditional_t<std::is_rvalue_reference_v<From>, |
48 | | std::add_rvalue_reference_t<To>, To>>; |
49 | | |
50 | | template <typename To, typename From> |
51 | | using propagate_cvref_t = propagate_reference_t< |
52 | | propagate_volatile_t<propagate_const_t<To, From>, From>, From>; |
53 | | |
54 | | } // namespace common_internal |
55 | | |
56 | | namespace common_internal { |
57 | | |
58 | | // Implementation of `cel::InstanceOf`. |
59 | | template <typename To> |
60 | | struct ABSL_DEPRECATED("Use Is member functions instead.") |
61 | | InstanceOfImpl final { |
62 | | static_assert(!std::is_pointer_v<To>, "To must not be a pointer"); |
63 | | static_assert(!std::is_array_v<To>, "To must not be an array"); |
64 | | static_assert(!std::is_lvalue_reference_v<To>, |
65 | | "To must not be a lvalue reference"); |
66 | | static_assert(!std::is_rvalue_reference_v<To>, |
67 | | "To must not be a lvalue reference"); |
68 | | static_assert(!std::is_const_v<To>, "To must not be const qualified"); |
69 | | static_assert(!std::is_volatile_v<To>, "To must not be volatile qualified"); |
70 | | static_assert(std::is_class_v<To>, "To must be a non-union class"); |
71 | | |
72 | | explicit InstanceOfImpl() = default; |
73 | | |
74 | | template <typename From> |
75 | | ABSL_DEPRECATED("Use Is member functions instead.") |
76 | 142k | ABSL_MUST_USE_RESULT bool operator()(const From& from) const { |
77 | 142k | static_assert(!std::is_volatile_v<From>, |
78 | 142k | "From must not be volatile qualified"); |
79 | 142k | static_assert(std::is_class_v<From>, "From must be a non-union class"); |
80 | | if constexpr (std::is_same_v<absl::remove_cvref_t<From>, To>) { |
81 | | // Same type. Separate from the next `else if` to work on in-complete |
82 | | // types. |
83 | | return true; |
84 | | } else if constexpr (std::is_polymorphic_v<To> && |
85 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && |
86 | | std::is_base_of_v<To, absl::remove_cvref_t<From>>) { |
87 | | // Polymorphic upcast. |
88 | | return true; |
89 | | } else if constexpr (!std::is_polymorphic_v<To> && |
90 | | !std::is_polymorphic_v<absl::remove_cvref_t<From>> && |
91 | | (std::is_convertible_v<const From&, To> || |
92 | | std::is_convertible_v<From&, To> || |
93 | | std::is_convertible_v<const From&&, To> || |
94 | | std::is_convertible_v<From&&, To>)) { |
95 | | // Implicitly convertible. |
96 | | return true; |
97 | 142k | } else { |
98 | | // Something else. |
99 | 142k | return from.template Is<To>(); |
100 | 142k | } |
101 | 142k | } bool cel::common_internal::InstanceOfImpl<cel::StringValue>::operator()<cel::Value>(cel::Value const&) const Line | Count | Source | 76 | 9.25k | ABSL_MUST_USE_RESULT bool operator()(const From& from) const { | 77 | 9.25k | static_assert(!std::is_volatile_v<From>, | 78 | 9.25k | "From must not be volatile qualified"); | 79 | 9.25k | static_assert(std::is_class_v<From>, "From must be a non-union class"); | 80 | | if constexpr (std::is_same_v<absl::remove_cvref_t<From>, To>) { | 81 | | // Same type. Separate from the next `else if` to work on in-complete | 82 | | // types. | 83 | | return true; | 84 | | } else if constexpr (std::is_polymorphic_v<To> && | 85 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 86 | | std::is_base_of_v<To, absl::remove_cvref_t<From>>) { | 87 | | // Polymorphic upcast. | 88 | | return true; | 89 | | } else if constexpr (!std::is_polymorphic_v<To> && | 90 | | !std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 91 | | (std::is_convertible_v<const From&, To> || | 92 | | std::is_convertible_v<From&, To> || | 93 | | std::is_convertible_v<const From&&, To> || | 94 | | std::is_convertible_v<From&&, To>)) { | 95 | | // Implicitly convertible. | 96 | | return true; | 97 | 9.25k | } else { | 98 | | // Something else. | 99 | 9.25k | return from.template Is<To>(); | 100 | 9.25k | } | 101 | 9.25k | } |
bool cel::common_internal::InstanceOfImpl<cel::ErrorValue>::operator()<cel::Value>(cel::Value const&) const Line | Count | Source | 76 | 124k | ABSL_MUST_USE_RESULT bool operator()(const From& from) const { | 77 | 124k | static_assert(!std::is_volatile_v<From>, | 78 | 124k | "From must not be volatile qualified"); | 79 | 124k | static_assert(std::is_class_v<From>, "From must be a non-union class"); | 80 | | if constexpr (std::is_same_v<absl::remove_cvref_t<From>, To>) { | 81 | | // Same type. Separate from the next `else if` to work on in-complete | 82 | | // types. | 83 | | return true; | 84 | | } else if constexpr (std::is_polymorphic_v<To> && | 85 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 86 | | std::is_base_of_v<To, absl::remove_cvref_t<From>>) { | 87 | | // Polymorphic upcast. | 88 | | return true; | 89 | | } else if constexpr (!std::is_polymorphic_v<To> && | 90 | | !std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 91 | | (std::is_convertible_v<const From&, To> || | 92 | | std::is_convertible_v<From&, To> || | 93 | | std::is_convertible_v<const From&&, To> || | 94 | | std::is_convertible_v<From&&, To>)) { | 95 | | // Implicitly convertible. | 96 | | return true; | 97 | 124k | } else { | 98 | | // Something else. | 99 | 124k | return from.template Is<To>(); | 100 | 124k | } | 101 | 124k | } |
Unexecuted instantiation: bool cel::common_internal::InstanceOfImpl<cel::UnknownValue>::operator()<cel::Value>(cel::Value const&) const Unexecuted instantiation: bool cel::common_internal::InstanceOfImpl<cel::StructValue>::operator()<cel::Value>(cel::Value const&) const Unexecuted instantiation: bool cel::common_internal::InstanceOfImpl<cel::IntValue>::operator()<cel::Value>(cel::Value const&) const Unexecuted instantiation: bool cel::common_internal::InstanceOfImpl<cel::OptionalValue>::operator()<cel::Value>(cel::Value const&) const bool cel::common_internal::InstanceOfImpl<cel::BytesValue>::operator()<cel::Value>(cel::Value const&) const Line | Count | Source | 76 | 1.92k | ABSL_MUST_USE_RESULT bool operator()(const From& from) const { | 77 | 1.92k | static_assert(!std::is_volatile_v<From>, | 78 | 1.92k | "From must not be volatile qualified"); | 79 | 1.92k | static_assert(std::is_class_v<From>, "From must be a non-union class"); | 80 | | if constexpr (std::is_same_v<absl::remove_cvref_t<From>, To>) { | 81 | | // Same type. Separate from the next `else if` to work on in-complete | 82 | | // types. | 83 | | return true; | 84 | | } else if constexpr (std::is_polymorphic_v<To> && | 85 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 86 | | std::is_base_of_v<To, absl::remove_cvref_t<From>>) { | 87 | | // Polymorphic upcast. | 88 | | return true; | 89 | | } else if constexpr (!std::is_polymorphic_v<To> && | 90 | | !std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 91 | | (std::is_convertible_v<const From&, To> || | 92 | | std::is_convertible_v<From&, To> || | 93 | | std::is_convertible_v<const From&&, To> || | 94 | | std::is_convertible_v<From&&, To>)) { | 95 | | // Implicitly convertible. | 96 | | return true; | 97 | 1.92k | } else { | 98 | | // Something else. | 99 | 1.92k | return from.template Is<To>(); | 100 | 1.92k | } | 101 | 1.92k | } |
bool cel::common_internal::InstanceOfImpl<cel::ListValue>::operator()<cel::Value>(cel::Value const&) const Line | Count | Source | 76 | 6.97k | ABSL_MUST_USE_RESULT bool operator()(const From& from) const { | 77 | 6.97k | static_assert(!std::is_volatile_v<From>, | 78 | 6.97k | "From must not be volatile qualified"); | 79 | 6.97k | static_assert(std::is_class_v<From>, "From must be a non-union class"); | 80 | | if constexpr (std::is_same_v<absl::remove_cvref_t<From>, To>) { | 81 | | // Same type. Separate from the next `else if` to work on in-complete | 82 | | // types. | 83 | | return true; | 84 | | } else if constexpr (std::is_polymorphic_v<To> && | 85 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 86 | | std::is_base_of_v<To, absl::remove_cvref_t<From>>) { | 87 | | // Polymorphic upcast. | 88 | | return true; | 89 | | } else if constexpr (!std::is_polymorphic_v<To> && | 90 | | !std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 91 | | (std::is_convertible_v<const From&, To> || | 92 | | std::is_convertible_v<From&, To> || | 93 | | std::is_convertible_v<const From&&, To> || | 94 | | std::is_convertible_v<From&&, To>)) { | 95 | | // Implicitly convertible. | 96 | | return true; | 97 | 6.97k | } else { | 98 | | // Something else. | 99 | 6.97k | return from.template Is<To>(); | 100 | 6.97k | } | 101 | 6.97k | } |
Unexecuted instantiation: bool cel::common_internal::InstanceOfImpl<cel::MapValue>::operator()<cel::Value>(cel::Value const&) const Unexecuted instantiation: bool cel::common_internal::InstanceOfImpl<cel::NullValue>::operator()<cel::Value>(cel::Value const&) const Unexecuted instantiation: bool cel::common_internal::InstanceOfImpl<cel::TypeValue>::operator()<cel::Value>(cel::Value const&) const |
102 | | |
103 | | template <typename From> |
104 | | ABSL_DEPRECATED("Use Is member functions instead.") |
105 | | ABSL_MUST_USE_RESULT bool operator()(const From* from) const { |
106 | | static_assert(!std::is_volatile_v<From>, |
107 | | "From must not be volatile qualified"); |
108 | | static_assert(std::is_class_v<From>, "From must be a non-union class"); |
109 | | return from != nullptr && (*this)(*from); |
110 | | } |
111 | | }; |
112 | | |
113 | | // Implementation of `cel::Cast`. |
114 | | template <typename To> |
115 | | struct ABSL_DEPRECATED( |
116 | | "Use explicit conversion functions instead through static_cast.") |
117 | | CastImpl final { |
118 | | static_assert(!std::is_pointer_v<To>, "To must not be a pointer"); |
119 | | static_assert(!std::is_array_v<To>, "To must not be an array"); |
120 | | static_assert(!std::is_lvalue_reference_v<To>, |
121 | | "To must not be a lvalue reference"); |
122 | | static_assert(!std::is_rvalue_reference_v<To>, |
123 | | "To must not be a lvalue reference"); |
124 | | static_assert(!std::is_const_v<To>, "To must not be const qualified"); |
125 | | static_assert(!std::is_volatile_v<To>, "To must not be volatile qualified"); |
126 | | static_assert(std::is_class_v<To>, "To must be a non-union class"); |
127 | | |
128 | | explicit CastImpl() = default; |
129 | | |
130 | | template <typename From> |
131 | | ABSL_DEPRECATED( |
132 | | "Use explicit conversion functions instead through static_cast.") |
133 | | ABSL_MUST_USE_RESULT decltype(auto) |
134 | 32.2k | operator()(From&& from) const { |
135 | 32.2k | static_assert(!std::is_volatile_v<From>, |
136 | 32.2k | "From must not be volatile qualified"); |
137 | 32.2k | static_assert(std::is_class_v<absl::remove_cvref_t<From>>, |
138 | 32.2k | "From must be a non-union class"); |
139 | | if constexpr (std::is_polymorphic_v<From>) { |
140 | | static_assert(std::is_lvalue_reference_v<From>, |
141 | | "polymorphic casts are only possible on lvalue references"); |
142 | | } |
143 | | if constexpr (std::is_same_v<absl::remove_cvref_t<From>, To>) { |
144 | | // Same type. Separate from the next `else if` to work on in-complete |
145 | | // types. |
146 | | return static_cast<propagate_cvref_t<To, From>>(from); |
147 | | } else if constexpr (std::is_polymorphic_v<To> && |
148 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && |
149 | | std::is_base_of_v<To, absl::remove_cvref_t<From>>) { |
150 | | // Polymorphic upcast. |
151 | | return static_cast<propagate_cvref_t<To, From>>(from); |
152 | | } else if constexpr (std::is_polymorphic_v<To> && |
153 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && |
154 | | std::is_base_of_v<absl::remove_cvref_t<From>, To>) { |
155 | | // Polymorphic downcast. |
156 | | return cel::internal::down_cast<propagate_cvref_t<To, From>>( |
157 | | std::forward<From>(from)); |
158 | | } else if constexpr (std::is_convertible_v<From, To> && |
159 | | !std::is_polymorphic_v<To> && |
160 | | !std::is_polymorphic_v<absl::remove_cvref_t<From>>) { |
161 | | return static_cast<To>(std::forward<From>(from)); |
162 | 32.2k | } else { |
163 | | // Something else. |
164 | 32.2k | return std::forward<From>(from).template Get<To>(); |
165 | 32.2k | } |
166 | 32.2k | } Unexecuted instantiation: decltype(auto) cel::common_internal::CastImpl<cel::StringValue>::operator()<cel::Value&>(cel::Value&) const Unexecuted instantiation: decltype(auto) cel::common_internal::CastImpl<cel::StructValue>::operator()<cel::Value&>(cel::Value&) const decltype(auto) cel::common_internal::CastImpl<cel::MapValue>::operator()<cel::Value const&>(cel::Value const&) const Line | Count | Source | 134 | 3.85k | operator()(From&& from) const { | 135 | 3.85k | static_assert(!std::is_volatile_v<From>, | 136 | 3.85k | "From must not be volatile qualified"); | 137 | 3.85k | static_assert(std::is_class_v<absl::remove_cvref_t<From>>, | 138 | 3.85k | "From must be a non-union class"); | 139 | | if constexpr (std::is_polymorphic_v<From>) { | 140 | | static_assert(std::is_lvalue_reference_v<From>, | 141 | | "polymorphic casts are only possible on lvalue references"); | 142 | | } | 143 | | if constexpr (std::is_same_v<absl::remove_cvref_t<From>, To>) { | 144 | | // Same type. Separate from the next `else if` to work on in-complete | 145 | | // types. | 146 | | return static_cast<propagate_cvref_t<To, From>>(from); | 147 | | } else if constexpr (std::is_polymorphic_v<To> && | 148 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 149 | | std::is_base_of_v<To, absl::remove_cvref_t<From>>) { | 150 | | // Polymorphic upcast. | 151 | | return static_cast<propagate_cvref_t<To, From>>(from); | 152 | | } else if constexpr (std::is_polymorphic_v<To> && | 153 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 154 | | std::is_base_of_v<absl::remove_cvref_t<From>, To>) { | 155 | | // Polymorphic downcast. | 156 | | return cel::internal::down_cast<propagate_cvref_t<To, From>>( | 157 | | std::forward<From>(from)); | 158 | | } else if constexpr (std::is_convertible_v<From, To> && | 159 | | !std::is_polymorphic_v<To> && | 160 | | !std::is_polymorphic_v<absl::remove_cvref_t<From>>) { | 161 | | return static_cast<To>(std::forward<From>(from)); | 162 | 3.85k | } else { | 163 | | // Something else. | 164 | 3.85k | return std::forward<From>(from).template Get<To>(); | 165 | 3.85k | } | 166 | 3.85k | } |
decltype(auto) cel::common_internal::CastImpl<cel::ListValue>::operator()<cel::Value const&>(cel::Value const&) const Line | Count | Source | 134 | 7.65k | operator()(From&& from) const { | 135 | 7.65k | static_assert(!std::is_volatile_v<From>, | 136 | 7.65k | "From must not be volatile qualified"); | 137 | 7.65k | static_assert(std::is_class_v<absl::remove_cvref_t<From>>, | 138 | 7.65k | "From must be a non-union class"); | 139 | | if constexpr (std::is_polymorphic_v<From>) { | 140 | | static_assert(std::is_lvalue_reference_v<From>, | 141 | | "polymorphic casts are only possible on lvalue references"); | 142 | | } | 143 | | if constexpr (std::is_same_v<absl::remove_cvref_t<From>, To>) { | 144 | | // Same type. Separate from the next `else if` to work on in-complete | 145 | | // types. | 146 | | return static_cast<propagate_cvref_t<To, From>>(from); | 147 | | } else if constexpr (std::is_polymorphic_v<To> && | 148 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 149 | | std::is_base_of_v<To, absl::remove_cvref_t<From>>) { | 150 | | // Polymorphic upcast. | 151 | | return static_cast<propagate_cvref_t<To, From>>(from); | 152 | | } else if constexpr (std::is_polymorphic_v<To> && | 153 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 154 | | std::is_base_of_v<absl::remove_cvref_t<From>, To>) { | 155 | | // Polymorphic downcast. | 156 | | return cel::internal::down_cast<propagate_cvref_t<To, From>>( | 157 | | std::forward<From>(from)); | 158 | | } else if constexpr (std::is_convertible_v<From, To> && | 159 | | !std::is_polymorphic_v<To> && | 160 | | !std::is_polymorphic_v<absl::remove_cvref_t<From>>) { | 161 | | return static_cast<To>(std::forward<From>(from)); | 162 | 7.65k | } else { | 163 | | // Something else. | 164 | 7.65k | return std::forward<From>(from).template Get<To>(); | 165 | 7.65k | } | 166 | 7.65k | } |
Unexecuted instantiation: decltype(auto) cel::common_internal::CastImpl<cel::BoolValue>::operator()<cel::Value&>(cel::Value&) const Unexecuted instantiation: decltype(auto) cel::common_internal::CastImpl<cel::UnknownValue>::operator()<cel::Value&>(cel::Value&) const decltype(auto) cel::common_internal::CastImpl<cel::StringValue>::operator()<cel::Value const&>(cel::Value const&) const Line | Count | Source | 134 | 9.25k | operator()(From&& from) const { | 135 | 9.25k | static_assert(!std::is_volatile_v<From>, | 136 | 9.25k | "From must not be volatile qualified"); | 137 | 9.25k | static_assert(std::is_class_v<absl::remove_cvref_t<From>>, | 138 | 9.25k | "From must be a non-union class"); | 139 | | if constexpr (std::is_polymorphic_v<From>) { | 140 | | static_assert(std::is_lvalue_reference_v<From>, | 141 | | "polymorphic casts are only possible on lvalue references"); | 142 | | } | 143 | | if constexpr (std::is_same_v<absl::remove_cvref_t<From>, To>) { | 144 | | // Same type. Separate from the next `else if` to work on in-complete | 145 | | // types. | 146 | | return static_cast<propagate_cvref_t<To, From>>(from); | 147 | | } else if constexpr (std::is_polymorphic_v<To> && | 148 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 149 | | std::is_base_of_v<To, absl::remove_cvref_t<From>>) { | 150 | | // Polymorphic upcast. | 151 | | return static_cast<propagate_cvref_t<To, From>>(from); | 152 | | } else if constexpr (std::is_polymorphic_v<To> && | 153 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 154 | | std::is_base_of_v<absl::remove_cvref_t<From>, To>) { | 155 | | // Polymorphic downcast. | 156 | | return cel::internal::down_cast<propagate_cvref_t<To, From>>( | 157 | | std::forward<From>(from)); | 158 | | } else if constexpr (std::is_convertible_v<From, To> && | 159 | | !std::is_polymorphic_v<To> && | 160 | | !std::is_polymorphic_v<absl::remove_cvref_t<From>>) { | 161 | | return static_cast<To>(std::forward<From>(from)); | 162 | 9.25k | } else { | 163 | | // Something else. | 164 | 9.25k | return std::forward<From>(from).template Get<To>(); | 165 | 9.25k | } | 166 | 9.25k | } |
decltype(auto) cel::common_internal::CastImpl<cel::BytesValue>::operator()<cel::Value const&>(cel::Value const&) const Line | Count | Source | 134 | 1.92k | operator()(From&& from) const { | 135 | 1.92k | static_assert(!std::is_volatile_v<From>, | 136 | 1.92k | "From must not be volatile qualified"); | 137 | 1.92k | static_assert(std::is_class_v<absl::remove_cvref_t<From>>, | 138 | 1.92k | "From must be a non-union class"); | 139 | | if constexpr (std::is_polymorphic_v<From>) { | 140 | | static_assert(std::is_lvalue_reference_v<From>, | 141 | | "polymorphic casts are only possible on lvalue references"); | 142 | | } | 143 | | if constexpr (std::is_same_v<absl::remove_cvref_t<From>, To>) { | 144 | | // Same type. Separate from the next `else if` to work on in-complete | 145 | | // types. | 146 | | return static_cast<propagate_cvref_t<To, From>>(from); | 147 | | } else if constexpr (std::is_polymorphic_v<To> && | 148 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 149 | | std::is_base_of_v<To, absl::remove_cvref_t<From>>) { | 150 | | // Polymorphic upcast. | 151 | | return static_cast<propagate_cvref_t<To, From>>(from); | 152 | | } else if constexpr (std::is_polymorphic_v<To> && | 153 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 154 | | std::is_base_of_v<absl::remove_cvref_t<From>, To>) { | 155 | | // Polymorphic downcast. | 156 | | return cel::internal::down_cast<propagate_cvref_t<To, From>>( | 157 | | std::forward<From>(from)); | 158 | | } else if constexpr (std::is_convertible_v<From, To> && | 159 | | !std::is_polymorphic_v<To> && | 160 | | !std::is_polymorphic_v<absl::remove_cvref_t<From>>) { | 161 | | return static_cast<To>(std::forward<From>(from)); | 162 | 1.92k | } else { | 163 | | // Something else. | 164 | 1.92k | return std::forward<From>(from).template Get<To>(); | 165 | 1.92k | } | 166 | 1.92k | } |
Unexecuted instantiation: decltype(auto) cel::common_internal::CastImpl<cel::NullValue>::operator()<cel::Value const&>(cel::Value const&) const decltype(auto) cel::common_internal::CastImpl<cel::TypeValue>::operator()<cel::Value const&>(cel::Value const&) const Line | Count | Source | 134 | 12 | operator()(From&& from) const { | 135 | 12 | static_assert(!std::is_volatile_v<From>, | 136 | 12 | "From must not be volatile qualified"); | 137 | 12 | static_assert(std::is_class_v<absl::remove_cvref_t<From>>, | 138 | 12 | "From must be a non-union class"); | 139 | | if constexpr (std::is_polymorphic_v<From>) { | 140 | | static_assert(std::is_lvalue_reference_v<From>, | 141 | | "polymorphic casts are only possible on lvalue references"); | 142 | | } | 143 | | if constexpr (std::is_same_v<absl::remove_cvref_t<From>, To>) { | 144 | | // Same type. Separate from the next `else if` to work on in-complete | 145 | | // types. | 146 | | return static_cast<propagate_cvref_t<To, From>>(from); | 147 | | } else if constexpr (std::is_polymorphic_v<To> && | 148 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 149 | | std::is_base_of_v<To, absl::remove_cvref_t<From>>) { | 150 | | // Polymorphic upcast. | 151 | | return static_cast<propagate_cvref_t<To, From>>(from); | 152 | | } else if constexpr (std::is_polymorphic_v<To> && | 153 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 154 | | std::is_base_of_v<absl::remove_cvref_t<From>, To>) { | 155 | | // Polymorphic downcast. | 156 | | return cel::internal::down_cast<propagate_cvref_t<To, From>>( | 157 | | std::forward<From>(from)); | 158 | | } else if constexpr (std::is_convertible_v<From, To> && | 159 | | !std::is_polymorphic_v<To> && | 160 | | !std::is_polymorphic_v<absl::remove_cvref_t<From>>) { | 161 | | return static_cast<To>(std::forward<From>(from)); | 162 | 12 | } else { | 163 | | // Something else. | 164 | 12 | return std::forward<From>(from).template Get<To>(); | 165 | 12 | } | 166 | 12 | } |
Unexecuted instantiation: decltype(auto) cel::common_internal::CastImpl<cel::StructValue>::operator()<cel::Value const&>(cel::Value const&) const decltype(auto) cel::common_internal::CastImpl<cel::BoolValue>::operator()<cel::Value const&>(cel::Value const&) const Line | Count | Source | 134 | 3.36k | operator()(From&& from) const { | 135 | 3.36k | static_assert(!std::is_volatile_v<From>, | 136 | 3.36k | "From must not be volatile qualified"); | 137 | 3.36k | static_assert(std::is_class_v<absl::remove_cvref_t<From>>, | 138 | 3.36k | "From must be a non-union class"); | 139 | | if constexpr (std::is_polymorphic_v<From>) { | 140 | | static_assert(std::is_lvalue_reference_v<From>, | 141 | | "polymorphic casts are only possible on lvalue references"); | 142 | | } | 143 | | if constexpr (std::is_same_v<absl::remove_cvref_t<From>, To>) { | 144 | | // Same type. Separate from the next `else if` to work on in-complete | 145 | | // types. | 146 | | return static_cast<propagate_cvref_t<To, From>>(from); | 147 | | } else if constexpr (std::is_polymorphic_v<To> && | 148 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 149 | | std::is_base_of_v<To, absl::remove_cvref_t<From>>) { | 150 | | // Polymorphic upcast. | 151 | | return static_cast<propagate_cvref_t<To, From>>(from); | 152 | | } else if constexpr (std::is_polymorphic_v<To> && | 153 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 154 | | std::is_base_of_v<absl::remove_cvref_t<From>, To>) { | 155 | | // Polymorphic downcast. | 156 | | return cel::internal::down_cast<propagate_cvref_t<To, From>>( | 157 | | std::forward<From>(from)); | 158 | | } else if constexpr (std::is_convertible_v<From, To> && | 159 | | !std::is_polymorphic_v<To> && | 160 | | !std::is_polymorphic_v<absl::remove_cvref_t<From>>) { | 161 | | return static_cast<To>(std::forward<From>(from)); | 162 | 3.36k | } else { | 163 | | // Something else. | 164 | 3.36k | return std::forward<From>(from).template Get<To>(); | 165 | 3.36k | } | 166 | 3.36k | } |
decltype(auto) cel::common_internal::CastImpl<cel::IntValue>::operator()<cel::Value const&>(cel::Value const&) const Line | Count | Source | 134 | 567 | operator()(From&& from) const { | 135 | 567 | static_assert(!std::is_volatile_v<From>, | 136 | 567 | "From must not be volatile qualified"); | 137 | 567 | static_assert(std::is_class_v<absl::remove_cvref_t<From>>, | 138 | 567 | "From must be a non-union class"); | 139 | | if constexpr (std::is_polymorphic_v<From>) { | 140 | | static_assert(std::is_lvalue_reference_v<From>, | 141 | | "polymorphic casts are only possible on lvalue references"); | 142 | | } | 143 | | if constexpr (std::is_same_v<absl::remove_cvref_t<From>, To>) { | 144 | | // Same type. Separate from the next `else if` to work on in-complete | 145 | | // types. | 146 | | return static_cast<propagate_cvref_t<To, From>>(from); | 147 | | } else if constexpr (std::is_polymorphic_v<To> && | 148 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 149 | | std::is_base_of_v<To, absl::remove_cvref_t<From>>) { | 150 | | // Polymorphic upcast. | 151 | | return static_cast<propagate_cvref_t<To, From>>(from); | 152 | | } else if constexpr (std::is_polymorphic_v<To> && | 153 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 154 | | std::is_base_of_v<absl::remove_cvref_t<From>, To>) { | 155 | | // Polymorphic downcast. | 156 | | return cel::internal::down_cast<propagate_cvref_t<To, From>>( | 157 | | std::forward<From>(from)); | 158 | | } else if constexpr (std::is_convertible_v<From, To> && | 159 | | !std::is_polymorphic_v<To> && | 160 | | !std::is_polymorphic_v<absl::remove_cvref_t<From>>) { | 161 | | return static_cast<To>(std::forward<From>(from)); | 162 | 567 | } else { | 163 | | // Something else. | 164 | 567 | return std::forward<From>(from).template Get<To>(); | 165 | 567 | } | 166 | 567 | } |
decltype(auto) cel::common_internal::CastImpl<cel::UintValue>::operator()<cel::Value const&>(cel::Value const&) const Line | Count | Source | 134 | 247 | operator()(From&& from) const { | 135 | 247 | static_assert(!std::is_volatile_v<From>, | 136 | 247 | "From must not be volatile qualified"); | 137 | 247 | static_assert(std::is_class_v<absl::remove_cvref_t<From>>, | 138 | 247 | "From must be a non-union class"); | 139 | | if constexpr (std::is_polymorphic_v<From>) { | 140 | | static_assert(std::is_lvalue_reference_v<From>, | 141 | | "polymorphic casts are only possible on lvalue references"); | 142 | | } | 143 | | if constexpr (std::is_same_v<absl::remove_cvref_t<From>, To>) { | 144 | | // Same type. Separate from the next `else if` to work on in-complete | 145 | | // types. | 146 | | return static_cast<propagate_cvref_t<To, From>>(from); | 147 | | } else if constexpr (std::is_polymorphic_v<To> && | 148 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 149 | | std::is_base_of_v<To, absl::remove_cvref_t<From>>) { | 150 | | // Polymorphic upcast. | 151 | | return static_cast<propagate_cvref_t<To, From>>(from); | 152 | | } else if constexpr (std::is_polymorphic_v<To> && | 153 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 154 | | std::is_base_of_v<absl::remove_cvref_t<From>, To>) { | 155 | | // Polymorphic downcast. | 156 | | return cel::internal::down_cast<propagate_cvref_t<To, From>>( | 157 | | std::forward<From>(from)); | 158 | | } else if constexpr (std::is_convertible_v<From, To> && | 159 | | !std::is_polymorphic_v<To> && | 160 | | !std::is_polymorphic_v<absl::remove_cvref_t<From>>) { | 161 | | return static_cast<To>(std::forward<From>(from)); | 162 | 247 | } else { | 163 | | // Something else. | 164 | 247 | return std::forward<From>(from).template Get<To>(); | 165 | 247 | } | 166 | 247 | } |
decltype(auto) cel::common_internal::CastImpl<cel::DoubleValue>::operator()<cel::Value const&>(cel::Value const&) const Line | Count | Source | 134 | 329 | operator()(From&& from) const { | 135 | 329 | static_assert(!std::is_volatile_v<From>, | 136 | 329 | "From must not be volatile qualified"); | 137 | 329 | static_assert(std::is_class_v<absl::remove_cvref_t<From>>, | 138 | 329 | "From must be a non-union class"); | 139 | | if constexpr (std::is_polymorphic_v<From>) { | 140 | | static_assert(std::is_lvalue_reference_v<From>, | 141 | | "polymorphic casts are only possible on lvalue references"); | 142 | | } | 143 | | if constexpr (std::is_same_v<absl::remove_cvref_t<From>, To>) { | 144 | | // Same type. Separate from the next `else if` to work on in-complete | 145 | | // types. | 146 | | return static_cast<propagate_cvref_t<To, From>>(from); | 147 | | } else if constexpr (std::is_polymorphic_v<To> && | 148 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 149 | | std::is_base_of_v<To, absl::remove_cvref_t<From>>) { | 150 | | // Polymorphic upcast. | 151 | | return static_cast<propagate_cvref_t<To, From>>(from); | 152 | | } else if constexpr (std::is_polymorphic_v<To> && | 153 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 154 | | std::is_base_of_v<absl::remove_cvref_t<From>, To>) { | 155 | | // Polymorphic downcast. | 156 | | return cel::internal::down_cast<propagate_cvref_t<To, From>>( | 157 | | std::forward<From>(from)); | 158 | | } else if constexpr (std::is_convertible_v<From, To> && | 159 | | !std::is_polymorphic_v<To> && | 160 | | !std::is_polymorphic_v<absl::remove_cvref_t<From>>) { | 161 | | return static_cast<To>(std::forward<From>(from)); | 162 | 329 | } else { | 163 | | // Something else. | 164 | 329 | return std::forward<From>(from).template Get<To>(); | 165 | 329 | } | 166 | 329 | } |
Unexecuted instantiation: decltype(auto) cel::common_internal::CastImpl<cel::UnknownValue>::operator()<cel::Value const&>(cel::Value const&) const decltype(auto) cel::common_internal::CastImpl<cel::ErrorValue>::operator()<cel::Value const&>(cel::Value const&) const Line | Count | Source | 134 | 5.05k | operator()(From&& from) const { | 135 | 5.05k | static_assert(!std::is_volatile_v<From>, | 136 | 5.05k | "From must not be volatile qualified"); | 137 | 5.05k | static_assert(std::is_class_v<absl::remove_cvref_t<From>>, | 138 | 5.05k | "From must be a non-union class"); | 139 | | if constexpr (std::is_polymorphic_v<From>) { | 140 | | static_assert(std::is_lvalue_reference_v<From>, | 141 | | "polymorphic casts are only possible on lvalue references"); | 142 | | } | 143 | | if constexpr (std::is_same_v<absl::remove_cvref_t<From>, To>) { | 144 | | // Same type. Separate from the next `else if` to work on in-complete | 145 | | // types. | 146 | | return static_cast<propagate_cvref_t<To, From>>(from); | 147 | | } else if constexpr (std::is_polymorphic_v<To> && | 148 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 149 | | std::is_base_of_v<To, absl::remove_cvref_t<From>>) { | 150 | | // Polymorphic upcast. | 151 | | return static_cast<propagate_cvref_t<To, From>>(from); | 152 | | } else if constexpr (std::is_polymorphic_v<To> && | 153 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 154 | | std::is_base_of_v<absl::remove_cvref_t<From>, To>) { | 155 | | // Polymorphic downcast. | 156 | | return cel::internal::down_cast<propagate_cvref_t<To, From>>( | 157 | | std::forward<From>(from)); | 158 | | } else if constexpr (std::is_convertible_v<From, To> && | 159 | | !std::is_polymorphic_v<To> && | 160 | | !std::is_polymorphic_v<absl::remove_cvref_t<From>>) { | 161 | | return static_cast<To>(std::forward<From>(from)); | 162 | 5.05k | } else { | 163 | | // Something else. | 164 | 5.05k | return std::forward<From>(from).template Get<To>(); | 165 | 5.05k | } | 166 | 5.05k | } |
decltype(auto) cel::common_internal::CastImpl<cel::DurationValue>::operator()<cel::Value const&>(cel::Value const&) const Line | Count | Source | 134 | 1 | operator()(From&& from) const { | 135 | 1 | static_assert(!std::is_volatile_v<From>, | 136 | 1 | "From must not be volatile qualified"); | 137 | 1 | static_assert(std::is_class_v<absl::remove_cvref_t<From>>, | 138 | 1 | "From must be a non-union class"); | 139 | | if constexpr (std::is_polymorphic_v<From>) { | 140 | | static_assert(std::is_lvalue_reference_v<From>, | 141 | | "polymorphic casts are only possible on lvalue references"); | 142 | | } | 143 | | if constexpr (std::is_same_v<absl::remove_cvref_t<From>, To>) { | 144 | | // Same type. Separate from the next `else if` to work on in-complete | 145 | | // types. | 146 | | return static_cast<propagate_cvref_t<To, From>>(from); | 147 | | } else if constexpr (std::is_polymorphic_v<To> && | 148 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 149 | | std::is_base_of_v<To, absl::remove_cvref_t<From>>) { | 150 | | // Polymorphic upcast. | 151 | | return static_cast<propagate_cvref_t<To, From>>(from); | 152 | | } else if constexpr (std::is_polymorphic_v<To> && | 153 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 154 | | std::is_base_of_v<absl::remove_cvref_t<From>, To>) { | 155 | | // Polymorphic downcast. | 156 | | return cel::internal::down_cast<propagate_cvref_t<To, From>>( | 157 | | std::forward<From>(from)); | 158 | | } else if constexpr (std::is_convertible_v<From, To> && | 159 | | !std::is_polymorphic_v<To> && | 160 | | !std::is_polymorphic_v<absl::remove_cvref_t<From>>) { | 161 | | return static_cast<To>(std::forward<From>(from)); | 162 | 1 | } else { | 163 | | // Something else. | 164 | 1 | return std::forward<From>(from).template Get<To>(); | 165 | 1 | } | 166 | 1 | } |
decltype(auto) cel::common_internal::CastImpl<cel::TimestampValue>::operator()<cel::Value const&>(cel::Value const&) const Line | Count | Source | 134 | 1 | operator()(From&& from) const { | 135 | 1 | static_assert(!std::is_volatile_v<From>, | 136 | 1 | "From must not be volatile qualified"); | 137 | 1 | static_assert(std::is_class_v<absl::remove_cvref_t<From>>, | 138 | 1 | "From must be a non-union class"); | 139 | | if constexpr (std::is_polymorphic_v<From>) { | 140 | | static_assert(std::is_lvalue_reference_v<From>, | 141 | | "polymorphic casts are only possible on lvalue references"); | 142 | | } | 143 | | if constexpr (std::is_same_v<absl::remove_cvref_t<From>, To>) { | 144 | | // Same type. Separate from the next `else if` to work on in-complete | 145 | | // types. | 146 | | return static_cast<propagate_cvref_t<To, From>>(from); | 147 | | } else if constexpr (std::is_polymorphic_v<To> && | 148 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 149 | | std::is_base_of_v<To, absl::remove_cvref_t<From>>) { | 150 | | // Polymorphic upcast. | 151 | | return static_cast<propagate_cvref_t<To, From>>(from); | 152 | | } else if constexpr (std::is_polymorphic_v<To> && | 153 | | std::is_polymorphic_v<absl::remove_cvref_t<From>> && | 154 | | std::is_base_of_v<absl::remove_cvref_t<From>, To>) { | 155 | | // Polymorphic downcast. | 156 | | return cel::internal::down_cast<propagate_cvref_t<To, From>>( | 157 | | std::forward<From>(from)); | 158 | | } else if constexpr (std::is_convertible_v<From, To> && | 159 | | !std::is_polymorphic_v<To> && | 160 | | !std::is_polymorphic_v<absl::remove_cvref_t<From>>) { | 161 | | return static_cast<To>(std::forward<From>(from)); | 162 | 1 | } else { | 163 | | // Something else. | 164 | 1 | return std::forward<From>(from).template Get<To>(); | 165 | 1 | } | 166 | 1 | } |
|
167 | | |
168 | | template <typename From> |
169 | | ABSL_DEPRECATED( |
170 | | "Use explicit conversion functions instead through static_cast.") |
171 | | ABSL_MUST_USE_RESULT decltype(auto) |
172 | | operator()(From* from) const { |
173 | | static_assert(!std::is_volatile_v<From>, |
174 | | "From must not be volatile qualified"); |
175 | | static_assert(std::is_class_v<From>, "From must be a non-union class"); |
176 | | using R = decltype((*this)(*from)); |
177 | | static_assert(std::is_lvalue_reference_v<R>); |
178 | | if (from == nullptr) { |
179 | | return static_cast<std::add_pointer_t<std::remove_reference_t<R>>>( |
180 | | nullptr); |
181 | | } |
182 | | return static_cast<std::add_pointer_t<std::remove_reference_t<R>>>( |
183 | | std::addressof((*this)(*from))); |
184 | | } |
185 | | }; |
186 | | |
187 | | // Implementation of `cel::As`. |
188 | | template <typename To> |
189 | | struct ABSL_DEPRECATED("Use As member functions instead.") AsImpl final { |
190 | | static_assert(!std::is_pointer_v<To>, "To must not be a pointer"); |
191 | | static_assert(!std::is_array_v<To>, "To must not be an array"); |
192 | | static_assert(!std::is_lvalue_reference_v<To>, |
193 | | "To must not be a lvalue reference"); |
194 | | static_assert(!std::is_rvalue_reference_v<To>, |
195 | | "To must not be a lvalue reference"); |
196 | | static_assert(!std::is_const_v<To>, "To must not be const qualified"); |
197 | | static_assert(!std::is_volatile_v<To>, "To must not be volatile qualified"); |
198 | | static_assert(std::is_class_v<To>, "To must be a non-union class"); |
199 | | |
200 | | explicit AsImpl() = default; |
201 | | |
202 | | template <typename From> |
203 | | ABSL_DEPRECATED("Use As member functions instead.") |
204 | 11.4k | ABSL_MUST_USE_RESULT decltype(auto) operator()(From&& from) const { |
205 | | // Returns either `absl::optional` or `cel::optional_ref` |
206 | | // depending on the return type of `CastTraits::Convert`. The use of these |
207 | | // two types is an implementation detail. |
208 | 11.4k | static_assert(!std::is_volatile_v<From>, |
209 | 11.4k | "From must not be volatile qualified"); |
210 | 11.4k | static_assert(std::is_class_v<absl::remove_cvref_t<From>>, |
211 | 11.4k | "From must be a non-union class"); |
212 | 11.4k | return std::forward<From>(from).template As<To>(); |
213 | 11.4k | } Unexecuted instantiation: decltype(auto) cel::common_internal::AsImpl<cel::ErrorValue>::operator()<cel::Value&>(cel::Value&) const Unexecuted instantiation: decltype(auto) cel::common_internal::AsImpl<cel::OptionalValue>::operator()<cel::Value const&>(cel::Value const&) const Unexecuted instantiation: decltype(auto) cel::common_internal::AsImpl<cel::BoolValue>::operator()<cel::Value const&>(cel::Value const&) const Unexecuted instantiation: decltype(auto) cel::common_internal::AsImpl<cel::IntValue>::operator()<cel::Value const&>(cel::Value const&) const Unexecuted instantiation: decltype(auto) cel::common_internal::AsImpl<cel::UintValue>::operator()<cel::Value const&>(cel::Value const&) const Unexecuted instantiation: decltype(auto) cel::common_internal::AsImpl<cel::DoubleValue>::operator()<cel::Value const&>(cel::Value const&) const Unexecuted instantiation: decltype(auto) cel::common_internal::AsImpl<cel::StringValue>::operator()<cel::Value const&>(cel::Value const&) const Unexecuted instantiation: decltype(auto) cel::common_internal::AsImpl<cel::BytesValue>::operator()<cel::Value const&>(cel::Value const&) const decltype(auto) cel::common_internal::AsImpl<cel::BoolValue>::operator()<cel::Value&>(cel::Value&) const Line | Count | Source | 204 | 11.4k | ABSL_MUST_USE_RESULT decltype(auto) operator()(From&& from) const { | 205 | | // Returns either `absl::optional` or `cel::optional_ref` | 206 | | // depending on the return type of `CastTraits::Convert`. The use of these | 207 | | // two types is an implementation detail. | 208 | 11.4k | static_assert(!std::is_volatile_v<From>, | 209 | 11.4k | "From must not be volatile qualified"); | 210 | 11.4k | static_assert(std::is_class_v<absl::remove_cvref_t<From>>, | 211 | 11.4k | "From must be a non-union class"); | 212 | 11.4k | return std::forward<From>(from).template As<To>(); | 213 | 11.4k | } |
|
214 | | |
215 | | // Returns a pointer. |
216 | | template <typename From> |
217 | | ABSL_DEPRECATED("Use As member functions instead.") |
218 | | ABSL_MUST_USE_RESULT decltype(auto) operator()(From* from) const { |
219 | | // Returns either `absl::optional` or `To*` depending on the return type of |
220 | | // `CastTraits::Convert`. The use of these two types is an implementation |
221 | | // detail. |
222 | | static_assert(!std::is_volatile_v<From>, |
223 | | "From must not be volatile qualified"); |
224 | | static_assert(std::is_class_v<From>, "From must be a non-union class"); |
225 | | using R = decltype(from->template As<To>()); |
226 | | if (from == nullptr) { |
227 | | return R{absl::nullopt}; |
228 | | } |
229 | | return from->template As<To>(); |
230 | | } |
231 | | }; |
232 | | |
233 | | } // namespace common_internal |
234 | | |
235 | | } // namespace cel |
236 | | |
237 | | #endif // THIRD_PARTY_CEL_CPP_COMMON_INTERNAL_CASTING_H_ |