Coverage Report

Created: 2026-09-03 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
8.55M
  ABSL_MUST_USE_RESULT bool operator()(const From& from) const {
77
8.55M
    static_assert(!std::is_volatile_v<From>,
78
8.55M
                  "From must not be volatile qualified");
79
8.55M
    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
8.55M
    } else {
98
      // Something else.
99
8.55M
      return from.template Is<To>();
100
8.55M
    }
101
8.55M
  }
bool cel::common_internal::InstanceOfImpl<cel::StringValue>::operator()<cel::Value>(cel::Value const&) const
Line
Count
Source
76
296k
  ABSL_MUST_USE_RESULT bool operator()(const From& from) const {
77
296k
    static_assert(!std::is_volatile_v<From>,
78
296k
                  "From must not be volatile qualified");
79
296k
    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
296k
    } else {
98
      // Something else.
99
296k
      return from.template Is<To>();
100
296k
    }
101
296k
  }
bool cel::common_internal::InstanceOfImpl<cel::ErrorValue>::operator()<cel::Value>(cel::Value const&) const
Line
Count
Source
76
7.13M
  ABSL_MUST_USE_RESULT bool operator()(const From& from) const {
77
7.13M
    static_assert(!std::is_volatile_v<From>,
78
7.13M
                  "From must not be volatile qualified");
79
7.13M
    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
7.13M
    } else {
98
      // Something else.
99
7.13M
      return from.template Is<To>();
100
7.13M
    }
101
7.13M
  }
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
31.8k
  ABSL_MUST_USE_RESULT bool operator()(const From& from) const {
77
31.8k
    static_assert(!std::is_volatile_v<From>,
78
31.8k
                  "From must not be volatile qualified");
79
31.8k
    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
31.8k
    } else {
98
      // Something else.
99
31.8k
      return from.template Is<To>();
100
31.8k
    }
101
31.8k
  }
bool cel::common_internal::InstanceOfImpl<cel::ListValue>::operator()<cel::Value>(cel::Value const&) const
Line
Count
Source
76
1.08M
  ABSL_MUST_USE_RESULT bool operator()(const From& from) const {
77
1.08M
    static_assert(!std::is_volatile_v<From>,
78
1.08M
                  "From must not be volatile qualified");
79
1.08M
    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.08M
    } else {
98
      // Something else.
99
1.08M
      return from.template Is<To>();
100
1.08M
    }
101
1.08M
  }
bool cel::common_internal::InstanceOfImpl<cel::MapValue>::operator()<cel::Value>(cel::Value const&) const
Line
Count
Source
76
206
  ABSL_MUST_USE_RESULT bool operator()(const From& from) const {
77
206
    static_assert(!std::is_volatile_v<From>,
78
206
                  "From must not be volatile qualified");
79
206
    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
206
    } else {
98
      // Something else.
99
206
      return from.template Is<To>();
100
206
    }
101
206
  }
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
1.78M
  operator()(From&& from) const {
135
1.78M
    static_assert(!std::is_volatile_v<From>,
136
1.78M
                  "From must not be volatile qualified");
137
1.78M
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
1.78M
                  "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.78M
    } else {
163
      // Something else.
164
1.78M
      return std::forward<From>(from).template Get<To>();
165
1.78M
    }
166
1.78M
  }
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
334k
  operator()(From&& from) const {
135
334k
    static_assert(!std::is_volatile_v<From>,
136
334k
                  "From must not be volatile qualified");
137
334k
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
334k
                  "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
334k
    } else {
163
      // Something else.
164
334k
      return std::forward<From>(from).template Get<To>();
165
334k
    }
166
334k
  }
decltype(auto) cel::common_internal::CastImpl<cel::ListValue>::operator()<cel::Value const&>(cel::Value const&) const
Line
Count
Source
134
1.10M
  operator()(From&& from) const {
135
1.10M
    static_assert(!std::is_volatile_v<From>,
136
1.10M
                  "From must not be volatile qualified");
137
1.10M
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
1.10M
                  "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.10M
    } else {
163
      // Something else.
164
1.10M
      return std::forward<From>(from).template Get<To>();
165
1.10M
    }
166
1.10M
  }
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
296k
  operator()(From&& from) const {
135
296k
    static_assert(!std::is_volatile_v<From>,
136
296k
                  "From must not be volatile qualified");
137
296k
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
296k
                  "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
296k
    } else {
163
      // Something else.
164
296k
      return std::forward<From>(from).template Get<To>();
165
296k
    }
166
296k
  }
decltype(auto) cel::common_internal::CastImpl<cel::BytesValue>::operator()<cel::Value const&>(cel::Value const&) const
Line
Count
Source
134
31.8k
  operator()(From&& from) const {
135
31.8k
    static_assert(!std::is_volatile_v<From>,
136
31.8k
                  "From must not be volatile qualified");
137
31.8k
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
31.8k
                  "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
31.8k
    } else {
163
      // Something else.
164
31.8k
      return std::forward<From>(from).template Get<To>();
165
31.8k
    }
166
31.8k
  }
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
17
  operator()(From&& from) const {
135
17
    static_assert(!std::is_volatile_v<From>,
136
17
                  "From must not be volatile qualified");
137
17
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
17
                  "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
17
    } else {
163
      // Something else.
164
17
      return std::forward<From>(from).template Get<To>();
165
17
    }
166
17
  }
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
4.42k
  operator()(From&& from) const {
135
4.42k
    static_assert(!std::is_volatile_v<From>,
136
4.42k
                  "From must not be volatile qualified");
137
4.42k
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
4.42k
                  "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
4.42k
    } else {
163
      // Something else.
164
4.42k
      return std::forward<From>(from).template Get<To>();
165
4.42k
    }
166
4.42k
  }
decltype(auto) cel::common_internal::CastImpl<cel::IntValue>::operator()<cel::Value const&>(cel::Value const&) const
Line
Count
Source
134
595
  operator()(From&& from) const {
135
595
    static_assert(!std::is_volatile_v<From>,
136
595
                  "From must not be volatile qualified");
137
595
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
595
                  "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
595
    } else {
163
      // Something else.
164
595
      return std::forward<From>(from).template Get<To>();
165
595
    }
166
595
  }
decltype(auto) cel::common_internal::CastImpl<cel::UintValue>::operator()<cel::Value const&>(cel::Value const&) const
Line
Count
Source
134
161
  operator()(From&& from) const {
135
161
    static_assert(!std::is_volatile_v<From>,
136
161
                  "From must not be volatile qualified");
137
161
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
161
                  "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
161
    } else {
163
      // Something else.
164
161
      return std::forward<From>(from).template Get<To>();
165
161
    }
166
161
  }
decltype(auto) cel::common_internal::CastImpl<cel::DoubleValue>::operator()<cel::Value const&>(cel::Value const&) const
Line
Count
Source
134
666
  operator()(From&& from) const {
135
666
    static_assert(!std::is_volatile_v<From>,
136
666
                  "From must not be volatile qualified");
137
666
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
666
                  "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
666
    } else {
163
      // Something else.
164
666
      return std::forward<From>(from).template Get<To>();
165
666
    }
166
666
  }
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
9.59k
  operator()(From&& from) const {
135
9.59k
    static_assert(!std::is_volatile_v<From>,
136
9.59k
                  "From must not be volatile qualified");
137
9.59k
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
9.59k
                  "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.59k
    } else {
163
      // Something else.
164
9.59k
      return std::forward<From>(from).template Get<To>();
165
9.59k
    }
166
9.59k
  }
decltype(auto) cel::common_internal::CastImpl<cel::DurationValue>::operator()<cel::Value const&>(cel::Value const&) const
Line
Count
Source
134
376
  operator()(From&& from) const {
135
376
    static_assert(!std::is_volatile_v<From>,
136
376
                  "From must not be volatile qualified");
137
376
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
376
                  "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
376
    } else {
163
      // Something else.
164
376
      return std::forward<From>(from).template Get<To>();
165
376
    }
166
376
  }
decltype(auto) cel::common_internal::CastImpl<cel::TimestampValue>::operator()<cel::Value const&>(cel::Value const&) const
Line
Count
Source
134
30
  operator()(From&& from) const {
135
30
    static_assert(!std::is_volatile_v<From>,
136
30
                  "From must not be volatile qualified");
137
30
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
30
                  "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
30
    } else {
163
      // Something else.
164
30
      return std::forward<From>(from).template Get<To>();
165
30
    }
166
30
  }
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
136k
  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
136k
    static_assert(!std::is_volatile_v<From>,
209
136k
                  "From must not be volatile qualified");
210
136k
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
211
136k
                  "From must be a non-union class");
212
136k
    return std::forward<From>(from).template As<To>();
213
136k
  }
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
136k
  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
136k
    static_assert(!std::is_volatile_v<From>,
209
136k
                  "From must not be volatile qualified");
210
136k
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
211
136k
                  "From must be a non-union class");
212
136k
    return std::forward<From>(from).template As<To>();
213
136k
  }
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_