Coverage Report

Created: 2026-07-11 06:47

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
5.21M
  ABSL_MUST_USE_RESULT bool operator()(const From& from) const {
77
5.21M
    static_assert(!std::is_volatile_v<From>,
78
5.21M
                  "From must not be volatile qualified");
79
5.21M
    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
5.21M
    } else {
98
      // Something else.
99
5.21M
      return from.template Is<To>();
100
5.21M
    }
101
5.21M
  }
bool cel::common_internal::InstanceOfImpl<cel::StringValue>::operator()<cel::Value>(cel::Value const&) const
Line
Count
Source
76
39.1k
  ABSL_MUST_USE_RESULT bool operator()(const From& from) const {
77
39.1k
    static_assert(!std::is_volatile_v<From>,
78
39.1k
                  "From must not be volatile qualified");
79
39.1k
    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
39.1k
    } else {
98
      // Something else.
99
39.1k
      return from.template Is<To>();
100
39.1k
    }
101
39.1k
  }
bool cel::common_internal::InstanceOfImpl<cel::ErrorValue>::operator()<cel::Value>(cel::Value const&) const
Line
Count
Source
76
4.96M
  ABSL_MUST_USE_RESULT bool operator()(const From& from) const {
77
4.96M
    static_assert(!std::is_volatile_v<From>,
78
4.96M
                  "From must not be volatile qualified");
79
4.96M
    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
4.96M
    } else {
98
      // Something else.
99
4.96M
      return from.template Is<To>();
100
4.96M
    }
101
4.96M
  }
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
174k
  ABSL_MUST_USE_RESULT bool operator()(const From& from) const {
77
174k
    static_assert(!std::is_volatile_v<From>,
78
174k
                  "From must not be volatile qualified");
79
174k
    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
174k
    } else {
98
      // Something else.
99
174k
      return from.template Is<To>();
100
174k
    }
101
174k
  }
bool cel::common_internal::InstanceOfImpl<cel::ListValue>::operator()<cel::Value>(cel::Value const&) const
Line
Count
Source
76
34.7k
  ABSL_MUST_USE_RESULT bool operator()(const From& from) const {
77
34.7k
    static_assert(!std::is_volatile_v<From>,
78
34.7k
                  "From must not be volatile qualified");
79
34.7k
    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
34.7k
    } else {
98
      // Something else.
99
34.7k
      return from.template Is<To>();
100
34.7k
    }
101
34.7k
  }
bool cel::common_internal::InstanceOfImpl<cel::MapValue>::operator()<cel::Value>(cel::Value const&) const
Line
Count
Source
76
86
  ABSL_MUST_USE_RESULT bool operator()(const From& from) const {
77
86
    static_assert(!std::is_volatile_v<From>,
78
86
                  "From must not be volatile qualified");
79
86
    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
86
    } else {
98
      // Something else.
99
86
      return from.template Is<To>();
100
86
    }
101
86
  }
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
470k
  operator()(From&& from) const {
135
470k
    static_assert(!std::is_volatile_v<From>,
136
470k
                  "From must not be volatile qualified");
137
470k
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
470k
                  "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
470k
    } else {
163
      // Something else.
164
470k
      return std::forward<From>(from).template Get<To>();
165
470k
    }
166
470k
  }
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
114k
  operator()(From&& from) const {
135
114k
    static_assert(!std::is_volatile_v<From>,
136
114k
                  "From must not be volatile qualified");
137
114k
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
114k
                  "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
114k
    } else {
163
      // Something else.
164
114k
      return std::forward<From>(from).template Get<To>();
165
114k
    }
166
114k
  }
decltype(auto) cel::common_internal::CastImpl<cel::ListValue>::operator()<cel::Value const&>(cel::Value const&) const
Line
Count
Source
134
38.3k
  operator()(From&& from) const {
135
38.3k
    static_assert(!std::is_volatile_v<From>,
136
38.3k
                  "From must not be volatile qualified");
137
38.3k
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
38.3k
                  "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
38.3k
    } else {
163
      // Something else.
164
38.3k
      return std::forward<From>(from).template Get<To>();
165
38.3k
    }
166
38.3k
  }
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
39.1k
  operator()(From&& from) const {
135
39.1k
    static_assert(!std::is_volatile_v<From>,
136
39.1k
                  "From must not be volatile qualified");
137
39.1k
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
39.1k
                  "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
39.1k
    } else {
163
      // Something else.
164
39.1k
      return std::forward<From>(from).template Get<To>();
165
39.1k
    }
166
39.1k
  }
decltype(auto) cel::common_internal::CastImpl<cel::BytesValue>::operator()<cel::Value const&>(cel::Value const&) const
Line
Count
Source
134
174k
  operator()(From&& from) const {
135
174k
    static_assert(!std::is_volatile_v<From>,
136
174k
                  "From must not be volatile qualified");
137
174k
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
174k
                  "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
174k
    } else {
163
      // Something else.
164
174k
      return std::forward<From>(from).template Get<To>();
165
174k
    }
166
174k
  }
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
15
  operator()(From&& from) const {
135
15
    static_assert(!std::is_volatile_v<From>,
136
15
                  "From must not be volatile qualified");
137
15
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
15
                  "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
15
    } else {
163
      // Something else.
164
15
      return std::forward<From>(from).template Get<To>();
165
15
    }
166
15
  }
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.78k
  operator()(From&& from) const {
135
3.78k
    static_assert(!std::is_volatile_v<From>,
136
3.78k
                  "From must not be volatile qualified");
137
3.78k
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
3.78k
                  "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.78k
    } else {
163
      // Something else.
164
3.78k
      return std::forward<From>(from).template Get<To>();
165
3.78k
    }
166
3.78k
  }
decltype(auto) cel::common_internal::CastImpl<cel::IntValue>::operator()<cel::Value const&>(cel::Value const&) const
Line
Count
Source
134
910
  operator()(From&& from) const {
135
910
    static_assert(!std::is_volatile_v<From>,
136
910
                  "From must not be volatile qualified");
137
910
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
910
                  "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
910
    } else {
163
      // Something else.
164
910
      return std::forward<From>(from).template Get<To>();
165
910
    }
166
910
  }
decltype(auto) cel::common_internal::CastImpl<cel::UintValue>::operator()<cel::Value const&>(cel::Value const&) const
Line
Count
Source
134
93.2k
  operator()(From&& from) const {
135
93.2k
    static_assert(!std::is_volatile_v<From>,
136
93.2k
                  "From must not be volatile qualified");
137
93.2k
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
93.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
93.2k
    } else {
163
      // Something else.
164
93.2k
      return std::forward<From>(from).template Get<To>();
165
93.2k
    }
166
93.2k
  }
decltype(auto) cel::common_internal::CastImpl<cel::DoubleValue>::operator()<cel::Value const&>(cel::Value const&) const
Line
Count
Source
134
451
  operator()(From&& from) const {
135
451
    static_assert(!std::is_volatile_v<From>,
136
451
                  "From must not be volatile qualified");
137
451
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
451
                  "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
451
    } else {
163
      // Something else.
164
451
      return std::forward<From>(from).template Get<To>();
165
451
    }
166
451
  }
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.28k
  operator()(From&& from) const {
135
5.28k
    static_assert(!std::is_volatile_v<From>,
136
5.28k
                  "From must not be volatile qualified");
137
5.28k
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
5.28k
                  "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.28k
    } else {
163
      // Something else.
164
5.28k
      return std::forward<From>(from).template Get<To>();
165
5.28k
    }
166
5.28k
  }
decltype(auto) cel::common_internal::CastImpl<cel::DurationValue>::operator()<cel::Value const&>(cel::Value const&) const
Line
Count
Source
134
65
  operator()(From&& from) const {
135
65
    static_assert(!std::is_volatile_v<From>,
136
65
                  "From must not be volatile qualified");
137
65
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
138
65
                  "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
65
    } else {
163
      // Something else.
164
65
      return std::forward<From>(from).template Get<To>();
165
65
    }
166
65
  }
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
17.5k
  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
17.5k
    static_assert(!std::is_volatile_v<From>,
209
17.5k
                  "From must not be volatile qualified");
210
17.5k
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
211
17.5k
                  "From must be a non-union class");
212
17.5k
    return std::forward<From>(from).template As<To>();
213
17.5k
  }
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
17.5k
  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
17.5k
    static_assert(!std::is_volatile_v<From>,
209
17.5k
                  "From must not be volatile qualified");
210
17.5k
    static_assert(std::is_class_v<absl::remove_cvref_t<From>>,
211
17.5k
                  "From must be a non-union class");
212
17.5k
    return std::forward<From>(from).template As<To>();
213
17.5k
  }
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_