-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathtype_factory.h
More file actions
219 lines (176 loc) · 6.87 KB
/
Copy pathtype_factory.h
File metadata and controls
219 lines (176 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_CEL_CPP_BASE_TYPE_FACTORY_H_
#define THIRD_PARTY_CEL_CPP_BASE_TYPE_FACTORY_H_
#include <type_traits>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_map.h"
#include "absl/status/statusor.h"
#include "absl/synchronization/mutex.h"
#include "base/handle.h"
#include "base/memory.h"
#include "base/types/any_type.h"
#include "base/types/bool_type.h"
#include "base/types/bytes_type.h"
#include "base/types/double_type.h"
#include "base/types/duration_type.h"
#include "base/types/dyn_type.h"
#include "base/types/enum_type.h"
#include "base/types/error_type.h"
#include "base/types/int_type.h"
#include "base/types/list_type.h"
#include "base/types/map_type.h"
#include "base/types/null_type.h"
#include "base/types/optional_type.h"
#include "base/types/string_type.h"
#include "base/types/struct_type.h"
#include "base/types/timestamp_type.h"
#include "base/types/type_type.h"
#include "base/types/uint_type.h"
#include "base/types/unknown_type.h"
#include "base/types/wrapper_type.h"
namespace cel {
// TypeFactory provides member functions to get and create type implementations
// of builtin types.
class TypeFactory final {
private:
template <typename T, typename U, typename V>
using EnableIfBaseOfT = std::enable_if_t<std::is_base_of_v<T, U>, V>;
public:
explicit TypeFactory(
MemoryManager& memory_manager ABSL_ATTRIBUTE_LIFETIME_BOUND);
TypeFactory(const TypeFactory&) = delete;
TypeFactory(TypeFactory&&) = delete;
TypeFactory& operator=(const TypeFactory&) = delete;
TypeFactory& operator=(TypeFactory&&) = delete;
const Handle<NullType>& GetNullType() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return NullType::Get();
}
const Handle<ErrorType>& GetErrorType() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return ErrorType::Get();
}
const Handle<DynType>& GetDynType() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return DynType::Get();
}
const Handle<AnyType>& GetAnyType() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return AnyType::Get();
}
const Handle<BoolType>& GetBoolType() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return BoolType::Get();
}
const Handle<IntType>& GetIntType() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return IntType::Get();
}
const Handle<UintType>& GetUintType() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return UintType::Get();
}
const Handle<DoubleType>& GetDoubleType() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return DoubleType::Get();
}
const Handle<StringType>& GetStringType() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return StringType::Get();
}
const Handle<BytesType>& GetBytesType() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return BytesType::Get();
}
const Handle<DurationType>& GetDurationType() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return DurationType::Get();
}
const Handle<TimestampType>& GetTimestampType()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return TimestampType::Get();
}
const Handle<TypeType>& GetTypeType() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return TypeType::Get();
}
const Handle<UnknownType>& GetUnknownType() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return UnknownType::Get();
}
const Handle<BoolWrapperType>& GetBoolWrapperType()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return BoolWrapperType::Get();
}
const Handle<BytesWrapperType>& GetBytesWrapperType()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return BytesWrapperType::Get();
}
const Handle<DoubleWrapperType>& GetDoubleWrapperType()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return DoubleWrapperType::Get();
}
const Handle<IntWrapperType>& GetIntWrapperType()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return IntWrapperType::Get();
}
const Handle<StringWrapperType>& GetStringWrapperType()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return StringWrapperType::Get();
}
const Handle<UintWrapperType>& GetUintWrapperType()
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return UintWrapperType::Get();
}
const Handle<Type>& GetJsonValueType() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return GetDynType().As<Type>();
}
const Handle<ListType>& GetJsonListType() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return json_list_type_;
}
const Handle<MapType>& GetJsonMapType() ABSL_ATTRIBUTE_LIFETIME_BOUND {
return json_map_type_;
}
template <typename T, typename... Args>
EnableIfBaseOfT<EnumType, T, absl::StatusOr<Handle<T>>> CreateEnumType(
Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return base_internal::HandleFactory<T>::template Make<T>(
memory_manager(), std::forward<Args>(args)...);
}
template <typename T, typename... Args>
EnableIfBaseOfT<StructType, T, absl::StatusOr<Handle<T>>> CreateStructType(
Args&&... args) ABSL_ATTRIBUTE_LIFETIME_BOUND {
return base_internal::HandleFactory<T>::template Make<T>(
memory_manager(), std::forward<Args>(args)...);
}
absl::StatusOr<Handle<ListType>> CreateListType(const Handle<Type>& element)
ABSL_ATTRIBUTE_LIFETIME_BOUND;
absl::StatusOr<Handle<MapType>> CreateMapType(const Handle<Type>& key,
const Handle<Type>& value)
ABSL_ATTRIBUTE_LIFETIME_BOUND;
absl::StatusOr<Handle<OptionalType>> CreateOptionalType(
const Handle<Type>& type) ABSL_ATTRIBUTE_LIFETIME_BOUND;
MemoryManager& memory_manager() const { return memory_manager_; }
private:
MemoryManager& memory_manager_;
Handle<ListType> json_list_type_;
Handle<MapType> json_map_type_;
absl::Mutex list_types_mutex_;
// Mapping from list element types to the list type. This allows us to cache
// list types and avoid re-creating the same type.
absl::flat_hash_map<Handle<Type>, Handle<ListType>> list_types_
ABSL_GUARDED_BY(list_types_mutex_);
absl::Mutex map_types_mutex_;
// Mapping from map key and value types to the map type. This allows us to
// cache map types and avoid re-creating the same type.
absl::flat_hash_map<std::pair<Handle<Type>, Handle<Type>>, Handle<MapType>>
map_types_ ABSL_GUARDED_BY(map_types_mutex_);
absl::Mutex optional_types_mutex_;
// Mapping from type to the optional type. This allows us to cache optional
// types and avoid re-creating the same type.
absl::flat_hash_map<Handle<Type>, Handle<OptionalType>> optional_types_
ABSL_GUARDED_BY(optional_types_mutex_);
};
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_BASE_TYPE_FACTORY_H_