/proc/self/cwd/base/internal/unknown_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_INTERNAL_UNKNOWN_SET_H_ |
16 | | #define THIRD_PARTY_CEL_CPP_BASE_INTERNAL_UNKNOWN_SET_H_ |
17 | | |
18 | | #include <memory> |
19 | | #include <utility> |
20 | | |
21 | | #include "absl/base/attributes.h" |
22 | | #include "base/attribute_set.h" |
23 | | #include "base/function_result_set.h" |
24 | | |
25 | | namespace cel::base_internal { |
26 | | |
27 | | // For compatibility with the old API and to avoid unnecessary copying when |
28 | | // converting between the old and new representations, we store the historical |
29 | | // members of google::api::expr::runtime::UnknownSet in this struct for use with |
30 | | // std::shared_ptr. |
31 | | struct UnknownSetRep final { |
32 | 0 | UnknownSetRep() = default; |
33 | | |
34 | | UnknownSetRep(AttributeSet attributes, FunctionResultSet function_results) |
35 | 0 | : attributes(std::move(attributes)), |
36 | 0 | function_results(std::move(function_results)) {} |
37 | | |
38 | | explicit UnknownSetRep(AttributeSet attributes) |
39 | 0 | : attributes(std::move(attributes)) {} |
40 | | |
41 | | explicit UnknownSetRep(FunctionResultSet function_results) |
42 | 0 | : function_results(std::move(function_results)) {} |
43 | | |
44 | | AttributeSet attributes; |
45 | | FunctionResultSet function_results; |
46 | | }; |
47 | | |
48 | | const AttributeSet& EmptyAttributeSet(); |
49 | | |
50 | | const FunctionResultSet& EmptyFunctionResultSet(); |
51 | | |
52 | | struct UnknownSetAccess; |
53 | | |
54 | | class UnknownSet final { |
55 | | private: |
56 | | using Rep = UnknownSetRep; |
57 | | |
58 | | public: |
59 | | // Construct the empty set. |
60 | | // Uses singletons instead of allocating new containers. |
61 | 0 | UnknownSet() = default; |
62 | | |
63 | 0 | UnknownSet(const UnknownSet&) = default; |
64 | 0 | UnknownSet(UnknownSet&&) = default; |
65 | | UnknownSet& operator=(const UnknownSet&) = default; |
66 | 0 | UnknownSet& operator=(UnknownSet&&) = default; |
67 | | |
68 | | // Initialization specifying subcontainers |
69 | | explicit UnknownSet(AttributeSet attributes) |
70 | 0 | : rep_(std::make_shared<Rep>(std::move(attributes))) {} |
71 | | |
72 | | explicit UnknownSet(FunctionResultSet function_results) |
73 | 0 | : rep_(std::make_shared<Rep>(std::move(function_results))) {} |
74 | | |
75 | | UnknownSet(AttributeSet attributes, FunctionResultSet function_results) |
76 | 0 | : rep_(std::make_shared<Rep>(std::move(attributes), |
77 | 0 | std::move(function_results))) {} |
78 | | |
79 | | // Merge constructor |
80 | | UnknownSet(const UnknownSet& set1, const UnknownSet& set2) |
81 | | : UnknownSet( |
82 | | AttributeSet(set1.unknown_attributes(), set2.unknown_attributes()), |
83 | | FunctionResultSet(set1.unknown_function_results(), |
84 | 0 | set2.unknown_function_results())) {} |
85 | | |
86 | 0 | const AttributeSet& unknown_attributes() const { |
87 | 0 | return rep_ != nullptr ? rep_->attributes : EmptyAttributeSet(); |
88 | 0 | } |
89 | 0 | const FunctionResultSet& unknown_function_results() const { |
90 | 0 | return rep_ != nullptr ? rep_->function_results : EmptyFunctionResultSet(); |
91 | 0 | } |
92 | | |
93 | 0 | bool operator==(const UnknownSet& other) const { |
94 | 0 | return this == &other || |
95 | 0 | (unknown_attributes() == other.unknown_attributes() && |
96 | 0 | unknown_function_results() == other.unknown_function_results()); |
97 | 0 | } |
98 | | |
99 | 0 | bool operator!=(const UnknownSet& other) const { return !operator==(other); } |
100 | | |
101 | | private: |
102 | | friend struct UnknownSetAccess; |
103 | | |
104 | 0 | explicit UnknownSet(std::shared_ptr<Rep> impl) : rep_(std::move(impl)) {} |
105 | | |
106 | 0 | void Add(const UnknownSet& other) { |
107 | 0 | if (rep_ == nullptr) { |
108 | 0 | rep_ = std::make_shared<Rep>(); |
109 | 0 | } |
110 | 0 | rep_->attributes.Add(other.unknown_attributes()); |
111 | 0 | rep_->function_results.Add(other.unknown_function_results()); |
112 | 0 | } |
113 | | |
114 | | std::shared_ptr<Rep> rep_; |
115 | | }; |
116 | | |
117 | | struct UnknownSetAccess final { |
118 | 0 | static UnknownSet Construct(std::shared_ptr<UnknownSetRep> rep) { |
119 | 0 | return UnknownSet(std::move(rep)); |
120 | 0 | } |
121 | | |
122 | 0 | static void Add(UnknownSet& dest, const UnknownSet& src) { dest.Add(src); } |
123 | | |
124 | 0 | static const std::shared_ptr<UnknownSetRep>& Rep(const UnknownSet& value) { |
125 | 0 | return value.rep_; |
126 | 0 | } |
127 | | }; |
128 | | |
129 | | } // namespace cel::base_internal |
130 | | |
131 | | #endif // THIRD_PARTY_CEL_CPP_BASE_INTERNAL_UNKNOWN_SET_H_ |