/proc/self/cwd/common/values/map_value_builder.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_COMMON_VALUES_MAP_VALUE_BUILDER_H_ |
16 | | #define THIRD_PARTY_CEL_CPP_COMMON_VALUES_MAP_VALUE_BUILDER_H_ |
17 | | |
18 | | #include <cstddef> |
19 | | |
20 | | #include "absl/base/attributes.h" |
21 | | #include "absl/base/nullability.h" |
22 | | #include "absl/status/status.h" |
23 | | #include "absl/status/statusor.h" |
24 | | #include "common/native_type.h" |
25 | | #include "common/value.h" |
26 | | #include "eval/public/cel_value.h" |
27 | | #include "google/protobuf/arena.h" |
28 | | #include "google/protobuf/descriptor.h" |
29 | | #include "google/protobuf/message.h" |
30 | | |
31 | | namespace cel { |
32 | | |
33 | | class ValueFactory; |
34 | | |
35 | | namespace common_internal { |
36 | | |
37 | | // Special implementation of map which is both a modern map and legacy map. Do |
38 | | // not try this at home. This should only be implemented in |
39 | | // `map_value_builder.cc`. |
40 | | class CompatMapValue : public CustomMapValueInterface, |
41 | | public google::api::expr::runtime::CelMap { |
42 | | private: |
43 | 43 | NativeTypeId GetNativeTypeId() const final { |
44 | 43 | return NativeTypeId::For<CompatMapValue>(); |
45 | 43 | } |
46 | | }; |
47 | | |
48 | | const CompatMapValue* absl_nonnull EmptyCompatMapValue(); |
49 | | |
50 | | absl::StatusOr<const CompatMapValue* absl_nonnull> MakeCompatMapValue( |
51 | | const CustomMapValue& value, |
52 | | const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool, |
53 | | google::protobuf::MessageFactory* absl_nonnull message_factory, |
54 | | google::protobuf::Arena* absl_nonnull arena); |
55 | | |
56 | | // Extension of ParsedMapValueInterface which is also mutable. Accessing this |
57 | | // like a normal map before all entries are finished being inserted is a bug. |
58 | | // This is primarily used by the runtime to efficiently implement comprehensions |
59 | | // which accumulate results into a map. |
60 | | // |
61 | | // IMPORTANT: This type is only meant to be utilized by the runtime. |
62 | | class MutableMapValue : public CustomMapValueInterface { |
63 | | public: |
64 | | virtual absl::Status Put(Value key, Value value) const = 0; |
65 | | |
66 | 0 | virtual void Reserve(size_t capacity) const {} |
67 | | |
68 | | private: |
69 | 0 | NativeTypeId GetNativeTypeId() const override { |
70 | 0 | return NativeTypeId::For<MutableMapValue>(); |
71 | 0 | } |
72 | | }; |
73 | | |
74 | | // Special implementation of map which is both a modern map, legacy map, and |
75 | | // mutable. |
76 | | // |
77 | | // NOTE: We do not extend CompatMapValue to avoid having to use virtual |
78 | | // inheritance and `dynamic_cast`. |
79 | | class MutableCompatMapValue : public MutableMapValue, |
80 | | public google::api::expr::runtime::CelMap { |
81 | | private: |
82 | 0 | NativeTypeId GetNativeTypeId() const final { |
83 | 0 | return NativeTypeId::For<MutableCompatMapValue>(); |
84 | 0 | } |
85 | | }; |
86 | | |
87 | | MutableMapValue* absl_nonnull NewMutableMapValue( |
88 | | google::protobuf::Arena* absl_nonnull arena); |
89 | | |
90 | | bool IsMutableMapValue(const Value& value); |
91 | | bool IsMutableMapValue(const MapValue& value); |
92 | | |
93 | | const MutableMapValue* absl_nullable AsMutableMapValue( |
94 | | const Value& value ABSL_ATTRIBUTE_LIFETIME_BOUND); |
95 | | const MutableMapValue* absl_nullable AsMutableMapValue( |
96 | | const MapValue& value ABSL_ATTRIBUTE_LIFETIME_BOUND); |
97 | | |
98 | | const MutableMapValue& GetMutableMapValue( |
99 | | const Value& value ABSL_ATTRIBUTE_LIFETIME_BOUND); |
100 | | const MutableMapValue& GetMutableMapValue( |
101 | | const MapValue& value ABSL_ATTRIBUTE_LIFETIME_BOUND); |
102 | | |
103 | | absl_nonnull cel::MapValueBuilderPtr NewMapValueBuilder( |
104 | | google::protobuf::Arena* absl_nonnull arena); |
105 | | |
106 | | } // namespace common_internal |
107 | | |
108 | | } // namespace cel |
109 | | |
110 | | #endif // THIRD_PARTY_CEL_CPP_COMMON_VALUES_MAP_VALUE_BUILDER_H_ |