-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathmessage_type.h
More file actions
200 lines (151 loc) · 5.72 KB
/
message_type.h
File metadata and controls
200 lines (151 loc) · 5.72 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
// Copyright 2024 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.
// IWYU pragma: private, include "common/type.h"
// IWYU pragma: friend "common/types/struct_type.h"
#ifndef THIRD_PARTY_CEL_CPP_COMMON_TYPES_MESSAGE_TYPE_H_
#define THIRD_PARTY_CEL_CPP_COMMON_TYPES_MESSAGE_TYPE_H_
#include <cstddef>
#include <cstdint>
#include <memory>
#include <ostream>
#include <string>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/base/nullability.h"
#include "absl/log/absl_check.h"
#include "absl/strings/string_view.h"
#include "common/type_kind.h"
#include "google/protobuf/descriptor.h"
namespace cel {
class Type;
class TypeParameters;
bool IsWellKnownMessageType(const google::protobuf::Descriptor* absl_nonnull descriptor);
class MessageTypeField;
class MessageType final {
public:
using element_type = const google::protobuf::Descriptor;
static constexpr TypeKind kKind = TypeKind::kStruct;
// Constructs `MessageType` from a pointer to `google::protobuf::Descriptor`. The
// `google::protobuf::Descriptor` must not be one of the well known message types we
// treat specially, if it is behavior is undefined. If you are unsure, you
// should use `Type::Message`.
explicit MessageType(const google::protobuf::Descriptor* absl_nullable descriptor)
: descriptor_(descriptor) {
ABSL_DCHECK(descriptor == nullptr || !IsWellKnownMessageType(descriptor))
<< descriptor->full_name();
}
// Constructs a `MessageType` in an empty state.
//
// Most operations on an empty `MessageType` result in undefined behavior. Use
// `operator bool` to test if a `MessageType` is empty.
MessageType() = default;
MessageType(const MessageType&) = default;
MessageType(MessageType&&) = default;
MessageType& operator=(const MessageType&) = default;
MessageType& operator=(MessageType&&) = default;
static TypeKind kind() { return kKind; }
absl::string_view name() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return (*this)->full_name();
}
std::string DebugString() const;
static TypeParameters GetParameters();
const google::protobuf::Descriptor& operator*() const {
ABSL_DCHECK(*this);
return *descriptor_;
}
const google::protobuf::Descriptor* absl_nonnull operator->() const {
ABSL_DCHECK(*this);
return descriptor_;
}
explicit operator bool() const { return descriptor_ != nullptr; }
private:
friend struct std::pointer_traits<MessageType>;
const google::protobuf::Descriptor* absl_nullable descriptor_ = nullptr;
};
inline bool operator==(MessageType lhs, MessageType rhs) {
return static_cast<bool>(lhs) == static_cast<bool>(rhs) &&
(!static_cast<bool>(lhs) || lhs.name() == rhs.name());
}
inline bool operator!=(MessageType lhs, MessageType rhs) {
return !operator==(lhs, rhs);
}
template <typename H>
H AbslHashValue(H state, MessageType message_type) {
return H::combine(std::move(state), static_cast<bool>(message_type)
? message_type.name()
: absl::string_view());
}
inline std::ostream& operator<<(std::ostream& out, MessageType type) {
return out << type.DebugString();
}
} // namespace cel
namespace std {
template <>
struct pointer_traits<cel::MessageType> {
using pointer = cel::MessageType;
using element_type = typename cel::MessageType::element_type;
using difference_type = ptrdiff_t;
static element_type* to_address(const pointer& p) noexcept {
return p.descriptor_;
}
};
} // namespace std
namespace cel {
class MessageTypeField final {
public:
using element_type = const google::protobuf::FieldDescriptor;
explicit MessageTypeField(
const google::protobuf::FieldDescriptor* absl_nullable descriptor)
: descriptor_(descriptor) {}
MessageTypeField() = default;
MessageTypeField(const MessageTypeField&) = default;
MessageTypeField(MessageTypeField&&) = default;
MessageTypeField& operator=(const MessageTypeField&) = default;
MessageTypeField& operator=(MessageTypeField&&) = default;
std::string DebugString() const;
absl::string_view name() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return (*this)->is_extension() ? (*this)->full_name() : (*this)->name();
}
int32_t number() const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return (*this)->number();
}
Type GetType() const;
const google::protobuf::FieldDescriptor& operator*() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
ABSL_DCHECK(*this);
return *descriptor_;
}
const google::protobuf::FieldDescriptor* absl_nonnull operator->() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
ABSL_DCHECK(*this);
return descriptor_;
}
explicit operator bool() const { return descriptor_ != nullptr; }
private:
friend struct std::pointer_traits<MessageTypeField>;
const google::protobuf::FieldDescriptor* absl_nullable descriptor_ = nullptr;
};
} // namespace cel
namespace std {
template <>
struct pointer_traits<cel::MessageTypeField> {
using pointer = cel::MessageTypeField;
using element_type = typename cel::MessageTypeField::element_type;
using difference_type = ptrdiff_t;
static element_type* to_address(const pointer& p) noexcept {
return p.descriptor_;
}
};
} // namespace std
#endif // THIRD_PARTY_CEL_CPP_COMMON_TYPES_MESSAGE_TYPE_H_