Coverage Report

Created: 2026-07-11 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/common/optional_ref.h
Line
Count
Source
1
// Copyright 2024 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#ifndef THIRD_PARTY_CEL_CPP_OPTIONAL_REF_H_
16
#define THIRD_PARTY_CEL_CPP_OPTIONAL_REF_H_
17
18
#include <memory>
19
#include <type_traits>
20
21
#include "absl/base/attributes.h"
22
#include "absl/base/macros.h"
23
#include "absl/base/nullability.h"
24
#include "absl/base/optimization.h"
25
#include "absl/types/optional.h"
26
#include "absl/utility/utility.h"
27
28
namespace cel {
29
30
// `optional_ref<T>` looks and feels like `absl::optional<T>`, but instead of
31
// owning the underlying value, it retains a reference to the value it accepts
32
// in its constructor.
33
template <typename T>
34
class optional_ref final {
35
 public:
36
  static_assert(!std::is_reference_v<T>, "T must not be a reference.");
37
  static_assert(!std::is_same_v<absl::nullopt_t, std::remove_cv_t<T>>,
38
                "optional_ref<absl::nullopt_t> is not allowed.");
39
  static_assert(!std::is_same_v<absl::in_place_t, std::remove_cv_t<T>>,
40
                "optional_ref<absl::in_place_t> is not allowed.");
41
42
  using value_type = T;
43
44
2.83M
  optional_ref() = default;
Unexecuted instantiation: cel::optional_ref<cel::CustomListValue const>::optional_ref()
Unexecuted instantiation: cel::optional_ref<cel::CustomMapValue const>::optional_ref()
Unexecuted instantiation: cel::optional_ref<cel::OptionalValue const>::optional_ref()
Unexecuted instantiation: cel::optional_ref<cel::ParsedMessageValue const>::optional_ref()
cel::optional_ref<cel::BytesValue const>::optional_ref()
Line
Count
Source
44
187
  optional_ref() = default;
cel::optional_ref<cel::ErrorValue const>::optional_ref()
Line
Count
Source
44
1.41M
  optional_ref() = default;
Unexecuted instantiation: cel::optional_ref<cel::OpaqueValue const>::optional_ref()
Unexecuted instantiation: cel::optional_ref<cel::ParsedJsonListValue const>::optional_ref()
Unexecuted instantiation: cel::optional_ref<cel::ParsedJsonMapValue const>::optional_ref()
Unexecuted instantiation: cel::optional_ref<cel::ParsedMapFieldValue const>::optional_ref()
Unexecuted instantiation: cel::optional_ref<cel::ParsedRepeatedFieldValue const>::optional_ref()
Unexecuted instantiation: cel::optional_ref<cel::CustomStructValue const>::optional_ref()
cel::optional_ref<cel::StringValue const>::optional_ref()
Line
Count
Source
44
92
  optional_ref() = default;
cel::optional_ref<cel::TypeValue const>::optional_ref()
Line
Count
Source
44
11
  optional_ref() = default;
cel::optional_ref<cel::UnknownValue const>::optional_ref()
Line
Count
Source
44
1.41M
  optional_ref() = default;
45
46
  // NOLINTNEXTLINE(google-explicit-constructor)
47
2.83M
  constexpr optional_ref(absl::nullopt_t) : optional_ref() {}
Unexecuted instantiation: cel::optional_ref<cel::CustomListValue const>::optional_ref(std::__1::nullopt_t)
Unexecuted instantiation: cel::optional_ref<cel::CustomMapValue const>::optional_ref(std::__1::nullopt_t)
Unexecuted instantiation: cel::optional_ref<cel::OptionalValue const>::optional_ref(std::__1::nullopt_t)
Unexecuted instantiation: cel::optional_ref<cel::ParsedMessageValue const>::optional_ref(std::__1::nullopt_t)
cel::optional_ref<cel::BytesValue const>::optional_ref(std::__1::nullopt_t)
Line
Count
Source
47
187
  constexpr optional_ref(absl::nullopt_t) : optional_ref() {}
cel::optional_ref<cel::ErrorValue const>::optional_ref(std::__1::nullopt_t)
Line
Count
Source
47
1.41M
  constexpr optional_ref(absl::nullopt_t) : optional_ref() {}
Unexecuted instantiation: cel::optional_ref<cel::OpaqueValue const>::optional_ref(std::__1::nullopt_t)
Unexecuted instantiation: cel::optional_ref<cel::ParsedJsonListValue const>::optional_ref(std::__1::nullopt_t)
Unexecuted instantiation: cel::optional_ref<cel::ParsedJsonMapValue const>::optional_ref(std::__1::nullopt_t)
Unexecuted instantiation: cel::optional_ref<cel::ParsedMapFieldValue const>::optional_ref(std::__1::nullopt_t)
Unexecuted instantiation: cel::optional_ref<cel::ParsedRepeatedFieldValue const>::optional_ref(std::__1::nullopt_t)
Unexecuted instantiation: cel::optional_ref<cel::CustomStructValue const>::optional_ref(std::__1::nullopt_t)
cel::optional_ref<cel::StringValue const>::optional_ref(std::__1::nullopt_t)
Line
Count
Source
47
92
  constexpr optional_ref(absl::nullopt_t) : optional_ref() {}
cel::optional_ref<cel::TypeValue const>::optional_ref(std::__1::nullopt_t)
Line
Count
Source
47
11
  constexpr optional_ref(absl::nullopt_t) : optional_ref() {}
cel::optional_ref<cel::UnknownValue const>::optional_ref(std::__1::nullopt_t)
Line
Count
Source
47
1.41M
  constexpr optional_ref(absl::nullopt_t) : optional_ref() {}
48
49
  // NOLINTNEXTLINE(google-explicit-constructor)
50
  constexpr optional_ref(T& value ABSL_ATTRIBUTE_LIFETIME_BOUND)
51
1.44k
      : value_(std::addressof(value)) {}
cel::optional_ref<cel::CustomListValue const>::optional_ref(cel::CustomListValue const&)
Line
Count
Source
51
217
      : value_(std::addressof(value)) {}
cel::optional_ref<cel::CustomMapValue const>::optional_ref(cel::CustomMapValue const&)
Line
Count
Source
51
34
      : value_(std::addressof(value)) {}
Unexecuted instantiation: cel::optional_ref<cel::OptionalValue const>::optional_ref(cel::OptionalValue const&)
Unexecuted instantiation: cel::optional_ref<cel::ParsedMessageValue const>::optional_ref(cel::ParsedMessageValue const&)
cel::optional_ref<cel::BytesValue const>::optional_ref(cel::BytesValue const&)
Line
Count
Source
51
807
      : value_(std::addressof(value)) {}
Unexecuted instantiation: cel::optional_ref<cel::ErrorValue const>::optional_ref(cel::ErrorValue const&)
Unexecuted instantiation: cel::optional_ref<cel::OpaqueValue const>::optional_ref(cel::OpaqueValue const&)
Unexecuted instantiation: cel::optional_ref<cel::ParsedJsonListValue const>::optional_ref(cel::ParsedJsonListValue const&)
Unexecuted instantiation: cel::optional_ref<cel::ParsedJsonMapValue const>::optional_ref(cel::ParsedJsonMapValue const&)
Unexecuted instantiation: cel::optional_ref<cel::ParsedMapFieldValue const>::optional_ref(cel::ParsedMapFieldValue const&)
Unexecuted instantiation: cel::optional_ref<cel::ParsedRepeatedFieldValue const>::optional_ref(cel::ParsedRepeatedFieldValue const&)
Unexecuted instantiation: cel::optional_ref<cel::CustomStructValue const>::optional_ref(cel::CustomStructValue const&)
cel::optional_ref<cel::StringValue const>::optional_ref(cel::StringValue const&)
Line
Count
Source
51
389
      : value_(std::addressof(value)) {}
Unexecuted instantiation: cel::optional_ref<cel::TypeValue const>::optional_ref(cel::TypeValue const&)
Unexecuted instantiation: cel::optional_ref<cel::UnknownValue const>::optional_ref(cel::UnknownValue const&)
52
53
  template <
54
      typename U,
55
      typename = std::enable_if_t<std::conjunction_v<
56
          std::is_const<T>, std::is_same<std::decay_t<U>, std::decay_t<T>>>>>
57
  // NOLINTNEXTLINE(google-explicit-constructor)
58
  constexpr optional_ref(
59
      const absl::optional<U>& value ABSL_ATTRIBUTE_LIFETIME_BOUND)
60
      : value_(value.has_value() ? std::addressof(*value) : nullptr) {}
61
62
  template <typename U, typename = std::enable_if_t<
63
                            std::is_same_v<std::decay_t<U>, std::decay_t<T>>>>
64
  // NOLINTNEXTLINE(google-explicit-constructor)
65
  constexpr optional_ref(absl::optional<U>& value ABSL_ATTRIBUTE_LIFETIME_BOUND)
66
      : value_(value.has_value() ? std::addressof(*value) : nullptr) {}
67
68
  template <
69
      typename U,
70
      typename = std::enable_if_t<std::conjunction_v<
71
          std::negation<std::is_same<U, T>>,
72
          std::is_convertible<std::add_pointer_t<U>, std::add_pointer_t<T>>>>>
73
  // NOLINTNEXTLINE(google-explicit-constructor)
74
  constexpr optional_ref(const optional_ref<U>& other) : value_(other.value_) {}
75
76
  optional_ref(const optional_ref<T>&) = default;
77
78
  optional_ref<T>& operator=(const optional_ref<T>&) = delete;
79
80
2.84M
  constexpr bool has_value() const { return value_ != nullptr; }
cel::optional_ref<cel::CustomListValue const>::has_value() const
Line
Count
Source
80
217
  constexpr bool has_value() const { return value_ != nullptr; }
cel::optional_ref<cel::CustomMapValue const>::has_value() const
Line
Count
Source
80
34
  constexpr bool has_value() const { return value_ != nullptr; }
Unexecuted instantiation: cel::optional_ref<cel::ParsedMessageValue const>::has_value() const
Unexecuted instantiation: cel::optional_ref<cel::OptionalValue const>::has_value() const
cel::optional_ref<cel::BytesValue const>::has_value() const
Line
Count
Source
80
994
  constexpr bool has_value() const { return value_ != nullptr; }
cel::optional_ref<cel::ErrorValue const>::has_value() const
Line
Count
Source
80
1.41M
  constexpr bool has_value() const { return value_ != nullptr; }
Unexecuted instantiation: cel::optional_ref<cel::OpaqueValue const>::has_value() const
Unexecuted instantiation: cel::optional_ref<cel::ParsedJsonListValue const>::has_value() const
Unexecuted instantiation: cel::optional_ref<cel::ParsedJsonMapValue const>::has_value() const
Unexecuted instantiation: cel::optional_ref<cel::ParsedMapFieldValue const>::has_value() const
Unexecuted instantiation: cel::optional_ref<cel::ParsedRepeatedFieldValue const>::has_value() const
Unexecuted instantiation: cel::optional_ref<cel::CustomStructValue const>::has_value() const
cel::optional_ref<cel::StringValue const>::has_value() const
Line
Count
Source
80
481
  constexpr bool has_value() const { return value_ != nullptr; }
cel::optional_ref<cel::TypeValue const>::has_value() const
Line
Count
Source
80
11
  constexpr bool has_value() const { return value_ != nullptr; }
cel::optional_ref<cel::UnknownValue const>::has_value() const
Line
Count
Source
80
1.41M
  constexpr bool has_value() const { return value_ != nullptr; }
81
82
2.83M
  constexpr explicit operator bool() const { return has_value(); }
cel::optional_ref<cel::CustomListValue const>::operator bool() const
Line
Count
Source
82
217
  constexpr explicit operator bool() const { return has_value(); }
cel::optional_ref<cel::CustomMapValue const>::operator bool() const
Line
Count
Source
82
34
  constexpr explicit operator bool() const { return has_value(); }
Unexecuted instantiation: cel::optional_ref<cel::ParsedMessageValue const>::operator bool() const
Unexecuted instantiation: cel::optional_ref<cel::OptionalValue const>::operator bool() const
Unexecuted instantiation: cel::optional_ref<cel::BytesValue const>::operator bool() const
cel::optional_ref<cel::ErrorValue const>::operator bool() const
Line
Count
Source
82
1.41M
  constexpr explicit operator bool() const { return has_value(); }
Unexecuted instantiation: cel::optional_ref<cel::OpaqueValue const>::operator bool() const
Unexecuted instantiation: cel::optional_ref<cel::ParsedJsonListValue const>::operator bool() const
Unexecuted instantiation: cel::optional_ref<cel::ParsedJsonMapValue const>::operator bool() const
Unexecuted instantiation: cel::optional_ref<cel::ParsedMapFieldValue const>::operator bool() const
Unexecuted instantiation: cel::optional_ref<cel::ParsedRepeatedFieldValue const>::operator bool() const
Unexecuted instantiation: cel::optional_ref<cel::CustomStructValue const>::operator bool() const
Unexecuted instantiation: cel::optional_ref<cel::StringValue const>::operator bool() const
Unexecuted instantiation: cel::optional_ref<cel::TypeValue const>::operator bool() const
cel::optional_ref<cel::UnknownValue const>::operator bool() const
Line
Count
Source
82
1.41M
  constexpr explicit operator bool() const { return has_value(); }
83
84
  constexpr T& value() const {
85
    return ABSL_PREDICT_TRUE(has_value())
86
               ? *value_
87
               // Replicate the same error logic as in `absl::optional`'s
88
               // `value()`. It either throws an exception or aborts the
89
               // program. We intentionally ignore the return value of
90
               // the constructed optional's value as we only need to run
91
               // the code for error checking.
92
               : ((void)absl::optional<T>().value(), *value_);
93
  }
94
95
34
  constexpr T& operator*() const {
96
34
    ABSL_ASSERT(has_value());
97
34
    return *value_;
98
34
  }
Unexecuted instantiation: cel::optional_ref<cel::CustomListValue const>::operator*() const
cel::optional_ref<cel::CustomMapValue const>::operator*() const
Line
Count
Source
95
34
  constexpr T& operator*() const {
96
34
    ABSL_ASSERT(has_value());
97
34
    return *value_;
98
34
  }
Unexecuted instantiation: cel::optional_ref<cel::ParsedMessageValue const>::operator*() const
Unexecuted instantiation: cel::optional_ref<cel::OptionalValue const>::operator*() const
Unexecuted instantiation: cel::optional_ref<cel::BytesValue const>::operator*() const
Unexecuted instantiation: cel::optional_ref<cel::ErrorValue const>::operator*() const
Unexecuted instantiation: cel::optional_ref<cel::OpaqueValue const>::operator*() const
Unexecuted instantiation: cel::optional_ref<cel::ParsedJsonListValue const>::operator*() const
Unexecuted instantiation: cel::optional_ref<cel::ParsedJsonMapValue const>::operator*() const
Unexecuted instantiation: cel::optional_ref<cel::ParsedMapFieldValue const>::operator*() const
Unexecuted instantiation: cel::optional_ref<cel::ParsedRepeatedFieldValue const>::operator*() const
Unexecuted instantiation: cel::optional_ref<cel::CustomStructValue const>::operator*() const
Unexecuted instantiation: cel::optional_ref<cel::StringValue const>::operator*() const
Unexecuted instantiation: cel::optional_ref<cel::TypeValue const>::operator*() const
Unexecuted instantiation: cel::optional_ref<cel::UnknownValue const>::operator*() const
99
100
1.66k
  constexpr T* absl_nonnull operator->() const {
101
1.66k
    ABSL_ASSERT(has_value());
102
1.66k
    return value_;
103
1.66k
  }
Unexecuted instantiation: cel::optional_ref<cel::OptionalValue const>::operator->() const
cel::optional_ref<cel::StringValue const>::operator->() const
Line
Count
Source
100
389
  constexpr T* absl_nonnull operator->() const {
101
389
    ABSL_ASSERT(has_value());
102
389
    return value_;
103
389
  }
cel::optional_ref<cel::BytesValue const>::operator->() const
Line
Count
Source
100
807
  constexpr T* absl_nonnull operator->() const {
101
807
    ABSL_ASSERT(has_value());
102
807
    return value_;
103
807
  }
cel::optional_ref<cel::CustomListValue const>::operator->() const
Line
Count
Source
100
434
  constexpr T* absl_nonnull operator->() const {
101
434
    ABSL_ASSERT(has_value());
102
434
    return value_;
103
434
  }
cel::optional_ref<cel::CustomMapValue const>::operator->() const
Line
Count
Source
100
34
  constexpr T* absl_nonnull operator->() const {
101
34
    ABSL_ASSERT(has_value());
102
34
    return value_;
103
34
  }
Unexecuted instantiation: cel::optional_ref<cel::ParsedRepeatedFieldValue const>::operator->() const
Unexecuted instantiation: cel::optional_ref<cel::ParsedMapFieldValue const>::operator->() const
Unexecuted instantiation: cel::optional_ref<cel::ParsedJsonMapValue const>::operator->() const
Unexecuted instantiation: cel::optional_ref<cel::ParsedJsonListValue const>::operator->() const
Unexecuted instantiation: cel::optional_ref<cel::ErrorValue const>::operator->() const
Unexecuted instantiation: cel::optional_ref<cel::ParsedMessageValue const>::operator->() const
Unexecuted instantiation: cel::optional_ref<cel::TypeValue const>::operator->() const
104
105
 private:
106
  template <typename U>
107
  friend class optional_ref;
108
109
  T* const value_ = nullptr;
110
};
111
112
template <typename T>
113
optional_ref(const T&) -> optional_ref<const T>;
114
115
template <typename T>
116
optional_ref(T&) -> optional_ref<T>;
117
118
template <typename T>
119
optional_ref(const absl::optional<T>&) -> optional_ref<const T>;
120
121
template <typename T>
122
optional_ref(absl::optional<T>&) -> optional_ref<T>;
123
124
template <typename T>
125
constexpr bool operator==(const optional_ref<T>& lhs, absl::nullopt_t) {
126
  return !lhs.has_value();
127
}
128
129
template <typename T>
130
constexpr bool operator==(absl::nullopt_t, const optional_ref<T>& rhs) {
131
  return !rhs.has_value();
132
}
133
134
template <typename T>
135
constexpr bool operator!=(const optional_ref<T>& lhs, absl::nullopt_t) {
136
  return !operator==(lhs, absl::nullopt);
137
}
138
139
template <typename T>
140
constexpr bool operator!=(absl::nullopt_t, const optional_ref<T>& rhs) {
141
  return !operator==(absl::nullopt, rhs);
142
}
143
144
namespace common_internal {
145
146
template <typename T>
147
0
absl::optional<std::decay_t<T>> AsOptional(optional_ref<T> ref) {
148
0
  if (ref) {
149
0
    return *ref;
150
0
  }
151
0
  return absl::nullopt;
152
0
}
Unexecuted instantiation: std::__1::optional<__decay, cel::CustomListValue const> cel::common_internal::AsOptional<cel::CustomListValue const>(cel::optional_ref<__decay>)
Unexecuted instantiation: std::__1::optional<__decay, cel::CustomMapValue const> cel::common_internal::AsOptional<cel::CustomMapValue const>(cel::optional_ref<__decay>)
Unexecuted instantiation: std::__1::optional<__decay, cel::ParsedMessageValue const> cel::common_internal::AsOptional<cel::ParsedMessageValue const>(cel::optional_ref<__decay>)
Unexecuted instantiation: std::__1::optional<__decay, cel::OptionalValue const> cel::common_internal::AsOptional<cel::OptionalValue const>(cel::optional_ref<__decay>)
Unexecuted instantiation: std::__1::optional<__decay, cel::BytesValue const> cel::common_internal::AsOptional<cel::BytesValue const>(cel::optional_ref<__decay>)
Unexecuted instantiation: std::__1::optional<__decay, cel::ErrorValue const> cel::common_internal::AsOptional<cel::ErrorValue const>(cel::optional_ref<__decay>)
Unexecuted instantiation: std::__1::optional<__decay, cel::OpaqueValue const> cel::common_internal::AsOptional<cel::OpaqueValue const>(cel::optional_ref<__decay>)
Unexecuted instantiation: std::__1::optional<__decay, cel::ParsedJsonListValue const> cel::common_internal::AsOptional<cel::ParsedJsonListValue const>(cel::optional_ref<__decay>)
Unexecuted instantiation: std::__1::optional<__decay, cel::ParsedJsonMapValue const> cel::common_internal::AsOptional<cel::ParsedJsonMapValue const>(cel::optional_ref<__decay>)
Unexecuted instantiation: std::__1::optional<__decay, cel::ParsedMapFieldValue const> cel::common_internal::AsOptional<cel::ParsedMapFieldValue const>(cel::optional_ref<__decay>)
Unexecuted instantiation: std::__1::optional<__decay, cel::ParsedRepeatedFieldValue const> cel::common_internal::AsOptional<cel::ParsedRepeatedFieldValue const>(cel::optional_ref<__decay>)
Unexecuted instantiation: std::__1::optional<__decay, cel::CustomStructValue const> cel::common_internal::AsOptional<cel::CustomStructValue const>(cel::optional_ref<__decay>)
Unexecuted instantiation: std::__1::optional<__decay, cel::StringValue const> cel::common_internal::AsOptional<cel::StringValue const>(cel::optional_ref<__decay>)
Unexecuted instantiation: std::__1::optional<__decay, cel::TypeValue const> cel::common_internal::AsOptional<cel::TypeValue const>(cel::optional_ref<__decay>)
Unexecuted instantiation: std::__1::optional<__decay, cel::UnknownValue const> cel::common_internal::AsOptional<cel::UnknownValue const>(cel::optional_ref<__decay>)
153
154
template <typename T>
155
0
absl::optional<T> AsOptional(absl::optional<T> opt) {
156
0
  return opt;
157
0
}
Unexecuted instantiation: std::__1::optional<cel::ListValue> cel::common_internal::AsOptional<cel::ListValue>(std::__1::optional<cel::ListValue>)
Unexecuted instantiation: std::__1::optional<cel::MapValue> cel::common_internal::AsOptional<cel::MapValue>(std::__1::optional<cel::MapValue>)
Unexecuted instantiation: std::__1::optional<cel::MessageValue> cel::common_internal::AsOptional<cel::MessageValue>(std::__1::optional<cel::MessageValue>)
Unexecuted instantiation: std::__1::optional<cel::StructValue> cel::common_internal::AsOptional<cel::StructValue>(std::__1::optional<cel::StructValue>)
158
159
}  // namespace common_internal
160
161
}  // namespace cel
162
163
#endif  // THIRD_PARTY_CEL_CPP_OPTIONAL_REF_H_