/proc/self/cwd/runtime/activation.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 | | #ifndef THIRD_PARTY_CEL_CPP_RUNTIME_ACTIVATION_H_ |
16 | | #define THIRD_PARTY_CEL_CPP_RUNTIME_ACTIVATION_H_ |
17 | | |
18 | | #include <memory> |
19 | | #include <string> |
20 | | #include <utility> |
21 | | #include <vector> |
22 | | |
23 | | #include "absl/base/nullability.h" |
24 | | #include "absl/container/flat_hash_map.h" |
25 | | #include "absl/functional/any_invocable.h" |
26 | | #include "absl/status/statusor.h" |
27 | | #include "absl/strings/string_view.h" |
28 | | #include "absl/synchronization/mutex.h" |
29 | | #include "absl/types/optional.h" |
30 | | #include "absl/types/span.h" |
31 | | #include "base/attribute.h" |
32 | | #include "common/function_descriptor.h" |
33 | | #include "common/value.h" |
34 | | #include "runtime/activation_interface.h" |
35 | | #include "runtime/function.h" |
36 | | #include "runtime/function_overload_reference.h" |
37 | | #include "runtime/internal/attribute_matcher.h" |
38 | | #include "google/protobuf/arena.h" |
39 | | #include "google/protobuf/descriptor.h" |
40 | | #include "google/protobuf/message.h" |
41 | | |
42 | | namespace cel { |
43 | | |
44 | | namespace runtime_internal { |
45 | | class ActivationAttributeMatcherAccess; |
46 | | } |
47 | | |
48 | | // Thread-compatible implementation of a CEL Activation. |
49 | | // |
50 | | // Values can either be provided eagerly or via a provider. |
51 | | class Activation final : public ActivationInterface { |
52 | | public: |
53 | | // Definition for value providers. |
54 | | using ValueProvider = |
55 | | absl::AnyInvocable<absl::StatusOr<absl::optional<Value>>( |
56 | | absl::string_view, const google::protobuf::DescriptorPool* absl_nonnull, |
57 | | google::protobuf::MessageFactory* absl_nonnull, google::protobuf::Arena* absl_nonnull)>; |
58 | | |
59 | 0 | Activation() = default; |
60 | | |
61 | | // Move only. |
62 | | Activation(Activation&& other); |
63 | | |
64 | | Activation& operator=(Activation&& other); |
65 | | |
66 | | // Implements ActivationInterface. |
67 | | absl::StatusOr<bool> FindVariable( |
68 | | absl::string_view name, |
69 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
70 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
71 | | google::protobuf::Arena* absl_nonnull arena, |
72 | | Value* absl_nonnull result) const override; |
73 | | using ActivationInterface::FindVariable; |
74 | | |
75 | | std::vector<FunctionOverloadReference> FindFunctionOverloads( |
76 | | absl::string_view name) const override; |
77 | | |
78 | | absl::Span<const cel::AttributePattern> GetUnknownAttributes() |
79 | 0 | const override { |
80 | 0 | return unknown_patterns_; |
81 | 0 | } |
82 | | |
83 | | absl::Span<const cel::AttributePattern> GetMissingAttributes() |
84 | 0 | const override { |
85 | 0 | return missing_patterns_; |
86 | 0 | } |
87 | | |
88 | | // Bind a value to a named variable. |
89 | | // |
90 | | // Returns false if the entry for name was overwritten. |
91 | | bool InsertOrAssignValue(absl::string_view name, Value value); |
92 | | |
93 | | // Bind a provider to a named variable. The result of the provider may be |
94 | | // memoized by the activation. |
95 | | // |
96 | | // Returns false if the entry for name was overwritten. |
97 | | bool InsertOrAssignValueProvider(absl::string_view name, |
98 | | ValueProvider provider); |
99 | | |
100 | 0 | void AddUnknownPattern(cel::AttributePattern pattern) { |
101 | 0 | unknown_patterns_.push_back(std::move(pattern)); |
102 | 0 | } |
103 | | |
104 | 0 | void SetUnknownPatterns(std::vector<cel::AttributePattern> patterns) { |
105 | 0 | unknown_patterns_ = std::move(patterns); |
106 | 0 | } |
107 | | |
108 | 0 | void AddMissingPattern(cel::AttributePattern pattern) { |
109 | 0 | missing_patterns_.push_back(std::move(pattern)); |
110 | 0 | } |
111 | | |
112 | 0 | void SetMissingPatterns(std::vector<cel::AttributePattern> patterns) { |
113 | 0 | missing_patterns_ = std::move(patterns); |
114 | 0 | } |
115 | | |
116 | | // Returns true if the function was inserted (no other registered function has |
117 | | // a matching descriptor). |
118 | | bool InsertFunction(const cel::FunctionDescriptor& descriptor, |
119 | | std::unique_ptr<cel::Function> impl); |
120 | | |
121 | | private: |
122 | | struct ValueEntry { |
123 | | // If provider is present, then access must be synchronized to maintain |
124 | | // thread-compatible semantics for the lazily provided value. |
125 | | absl::optional<Value> value; |
126 | | absl::optional<ValueProvider> provider; |
127 | | }; |
128 | | |
129 | | struct FunctionEntry { |
130 | | std::unique_ptr<cel::FunctionDescriptor> descriptor; |
131 | | std::unique_ptr<cel::Function> implementation; |
132 | | }; |
133 | | |
134 | | friend class runtime_internal::ActivationAttributeMatcherAccess; |
135 | | |
136 | 0 | void SetAttributeMatcher(const runtime_internal::AttributeMatcher* matcher) { |
137 | 0 | attribute_matcher_ = matcher; |
138 | 0 | } |
139 | | |
140 | | void SetAttributeMatcher( |
141 | 0 | std::unique_ptr<const runtime_internal::AttributeMatcher> matcher) { |
142 | 0 | owned_attribute_matcher_ = std::move(matcher); |
143 | 0 | attribute_matcher_ = owned_attribute_matcher_.get(); |
144 | 0 | } |
145 | | |
146 | | const runtime_internal::AttributeMatcher* absl_nullable GetAttributeMatcher() |
147 | 0 | const override { |
148 | 0 | return attribute_matcher_; |
149 | 0 | } |
150 | | |
151 | 0 | friend void swap(Activation& a, Activation& b) { |
152 | 0 | using std::swap; |
153 | 0 | swap(a.values_, b.values_); |
154 | 0 | swap(a.functions_, b.functions_); |
155 | 0 | swap(a.unknown_patterns_, b.unknown_patterns_); |
156 | 0 | swap(a.missing_patterns_, b.missing_patterns_); |
157 | 0 | } |
158 | | |
159 | | // Internal getter for provided values. |
160 | | // Assumes entry for name is present and is a provided value. |
161 | | // Handles synchronization for caching the provided value. |
162 | | absl::StatusOr<bool> ProvideValue( |
163 | | absl::string_view name, |
164 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
165 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
166 | | google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) const; |
167 | | |
168 | | // mutex_ used for safe caching of provided variables |
169 | | mutable absl::Mutex mutex_; |
170 | | mutable absl::flat_hash_map<std::string, ValueEntry> values_; |
171 | | |
172 | | std::vector<cel::AttributePattern> unknown_patterns_; |
173 | | std::vector<cel::AttributePattern> missing_patterns_; |
174 | | |
175 | | const runtime_internal::AttributeMatcher* attribute_matcher_ = nullptr; |
176 | | std::unique_ptr<const runtime_internal::AttributeMatcher> |
177 | | owned_attribute_matcher_; |
178 | | |
179 | | absl::flat_hash_map<std::string, std::vector<FunctionEntry>> functions_; |
180 | | }; |
181 | | |
182 | | } // namespace cel |
183 | | |
184 | | #endif // THIRD_PARTY_CEL_CPP_RUNTIME_ACTIVATION_H_ |