-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathmap_type.h
More file actions
143 lines (107 loc) · 3.83 KB
/
Copy pathmap_type.h
File metadata and controls
143 lines (107 loc) · 3.83 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
// 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_TYPES_MAP_TYPE_H_
#define THIRD_PARTY_CEL_CPP_BASE_TYPES_MAP_TYPE_H_
#include <cstddef>
#include <cstdint>
#include <string>
#include "absl/log/absl_check.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "base/internal/data.h"
#include "base/kind.h"
#include "base/memory.h"
#include "base/type.h"
namespace cel {
class MemoryManager;
class TypeFactory;
class MapValue;
// MapType represents a map type. A map is container of key and value pairs
// where each key appears at most once.
class MapType : public Type {
public:
static constexpr Kind kKind = Kind::kMap;
static bool Is(const Type& type) { return type.kind() == kKind; }
using Type::Is;
static const MapType& Cast(const Type& type) {
ABSL_DCHECK(Is(type)) << "cannot cast " << type.name() << " to map";
return static_cast<const MapType&>(type);
}
Kind kind() const { return kKind; }
absl::string_view name() const { return KindToString(kind()); }
std::string DebugString() const;
// Returns the type of the keys in the map.
const Handle<Type>& key() const;
// Returns the type of the values in the map.
const Handle<Type>& value() const;
private:
friend class Type;
friend class MemoryManager;
friend class TypeFactory;
friend class base_internal::TypeHandle;
friend class base_internal::LegacyMapType;
friend class base_internal::ModernMapType;
// See Type::aliases().
absl::Span<const absl::string_view> aliases() const;
MapType() = default;
};
CEL_INTERNAL_TYPE_DECL(MapType);
namespace base_internal {
// LegacyMapType is used by LegacymapValue for compatibility with the legacy
// API. It's key and value are always the dynamic type regardless of whether the
// the expression is checked or not.
class LegacyMapType final : public MapType, public InlineData {
public:
const Handle<Type>& key() const;
const Handle<Type>& value() const;
private:
friend class MemoryManager;
friend class TypeFactory;
friend class cel::MapType;
friend class base_internal::TypeHandle;
template <size_t Size, size_t Align>
friend struct AnyData;
static constexpr uintptr_t kMetadata =
base_internal::kStoredInline | base_internal::kTrivial |
(static_cast<uintptr_t>(kKind) << base_internal::kKindShift);
LegacyMapType() : MapType(), InlineData(kMetadata) {}
};
class ModernMapType final : public MapType, public HeapData {
public:
const Handle<Type>& key() const { return key_; }
const Handle<Type>& value() const { return value_; }
private:
friend class cel::MemoryManager;
friend class TypeFactory;
friend class cel::MapType;
friend class base_internal::TypeHandle;
// Called by Arena-based memory managers to determine whether we actually need
// our destructor called.
CEL_INTERNAL_IS_DESTRUCTOR_SKIPPABLE() {
return Metadata::IsDestructorSkippable(*key()) &&
Metadata::IsDestructorSkippable(*value());
}
explicit ModernMapType(Handle<Type> key, Handle<Type> value);
const Handle<Type> key_;
const Handle<Type> value_;
};
} // namespace base_internal
namespace base_internal {
template <>
struct TypeTraits<MapType> {
using value_type = MapValue;
};
} // namespace base_internal
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_BASE_TYPES_MAP_TYPE_H_