Coverage Report

Created: 2026-10-09 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/proc/self/cwd/base/attribute_set.h
Line
Count
Source
1
// Copyright 2022 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_BASE_ATTRIBUTE_SET_H_
16
#define THIRD_PARTY_CEL_CPP_BASE_ATTRIBUTE_SET_H_
17
18
#include <memory>
19
20
#include "absl/base/no_destructor.h"
21
#include "absl/container/btree_set.h"
22
#include "absl/types/span.h"
23
#include "base/attribute.h"
24
25
namespace google::api::expr::runtime {
26
class AttributeUtility;
27
}  // namespace google::api::expr::runtime
28
29
namespace cel {
30
31
class UnknownValue;
32
namespace base_internal {
33
class UnknownSet;
34
}
35
36
// AttributeSet is a container for CEL attributes that are identified as
37
// unknown during expression evaluation.
38
class AttributeSet final {
39
 private:
40
  using Container = absl::btree_set<Attribute>;
41
42
 public:
43
  using value_type = typename Container::value_type;
44
  using size_type = typename Container::size_type;
45
  using iterator = typename Container::const_iterator;
46
  using const_iterator = typename Container::const_iterator;
47
48
0
  AttributeSet() = default;
49
0
  AttributeSet(const AttributeSet&) = default;
50
0
  AttributeSet(AttributeSet&&) = default;
51
  AttributeSet& operator=(const AttributeSet&) = default;
52
  AttributeSet& operator=(AttributeSet&&) = default;
53
54
0
  explicit AttributeSet(absl::Span<const Attribute> attributes) {
55
0
    for (const auto& attr : attributes) {
56
0
      Add(attr);
57
0
    }
58
0
  }
59
60
0
  AttributeSet(const AttributeSet& set1, const AttributeSet& set2) {
61
0
    Add(set1);
62
0
    Add(set2);
63
0
  }
64
65
0
  iterator begin() const {
66
0
    return attributes_ != nullptr ? attributes_->begin()
67
0
                                  : EmptyContainer().begin();
68
0
  }
69
70
0
  const_iterator cbegin() const {
71
0
    return attributes_ != nullptr ? attributes_->cbegin()
72
0
                                  : EmptyContainer().cbegin();
73
0
  }
74
75
0
  iterator end() const {
76
0
    return attributes_ != nullptr ? attributes_->end() : EmptyContainer().end();
77
0
  }
78
79
0
  const_iterator cend() const {
80
0
    return attributes_ != nullptr ? attributes_->cend()
81
0
                                  : EmptyContainer().cend();
82
0
  }
83
84
0
  size_type size() const {
85
0
    return attributes_ != nullptr ? attributes_->size() : 0;
86
0
  }
87
88
0
  bool empty() const { return attributes_ == nullptr || attributes_->empty(); }
89
90
0
  bool operator==(const AttributeSet& other) const {
91
0
    if (attributes_ == other.attributes_) {
92
0
      return true;
93
0
    }
94
0
    return (attributes_ != nullptr ? *attributes_ : EmptyContainer()) ==
95
0
           (other.attributes_ != nullptr ? *other.attributes_
96
0
                                         : EmptyContainer());
97
0
  }
98
99
0
  bool operator!=(const AttributeSet& other) const {
100
0
    return !operator==(other);
101
0
  }
102
103
  static AttributeSet Merge(const AttributeSet& set1,
104
0
                            const AttributeSet& set2) {
105
0
    return AttributeSet(set1, set2);
106
0
  }
107
108
 private:
109
  friend class google::api::expr::runtime::AttributeUtility;
110
  friend class UnknownValue;
111
  friend class base_internal::UnknownSet;
112
113
0
  static const Container& EmptyContainer() {
114
0
    static const absl::NoDestructor<Container> container;
115
0
    return *container;
116
0
  }
117
118
0
  void Add(const Attribute& attribute) {
119
0
    if (attributes_ == nullptr) {
120
0
      attributes_ = std::make_shared<Container>();
121
0
    }
122
0
    attributes_->insert(attribute);
123
0
  }
124
125
0
  void Add(const AttributeSet& other) {
126
0
    if (!other.empty()) {
127
0
      if (attributes_ == nullptr) {
128
0
        attributes_ = std::make_shared<Container>();
129
0
      }
130
0
      for (const auto& attribute : other) {
131
0
        attributes_->insert(attribute);
132
0
      }
133
0
    }
134
0
  }
135
136
  // Attribute container.
137
  std::shared_ptr<Container> attributes_;
138
};
139
140
}  // namespace cel
141
142
#endif  // THIRD_PARTY_CEL_CPP_BASE_ATTRIBUTE_SET_H_