-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathbytes_value.h
More file actions
335 lines (261 loc) · 11 KB
/
bytes_value.h
File metadata and controls
335 lines (261 loc) · 11 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright 2023 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/value.h"
// IWYU pragma: friend "common/value.h"
#ifndef THIRD_PARTY_CEL_CPP_COMMON_VALUES_BYTES_VALUE_H_
#define THIRD_PARTY_CEL_CPP_COMMON_VALUES_BYTES_VALUE_H_
#include <cstddef>
#include <ostream>
#include <string>
#include <type_traits>
#include <utility>
#include "absl/base/attributes.h"
#include "absl/base/nullability.h"
#include "absl/log/absl_check.h"
#include "absl/status/status.h"
#include "absl/strings/cord.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "common/allocator.h"
#include "common/arena.h"
#include "common/internal/byte_string.h"
#include "common/memory.h"
#include "common/type.h"
#include "common/value_kind.h"
#include "common/values/values.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"
namespace cel {
class Value;
class BytesValue;
class TypeManager;
class BytesValueInputStream;
class BytesValueOutputStream;
namespace common_internal {
absl::string_view LegacyBytesValue(const BytesValue& value, bool stable,
absl::Nonnull<google::protobuf::Arena*> arena);
} // namespace common_internal
// `BytesValue` represents values of the primitive `bytes` type.
class BytesValue final : private common_internal::ValueMixin<BytesValue> {
public:
static constexpr ValueKind kKind = ValueKind::kBytes;
static BytesValue From(absl::Nullable<const char*> value,
absl::Nonnull<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND);
static BytesValue From(absl::string_view value,
absl::Nonnull<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND);
static BytesValue From(const absl::Cord& value);
static BytesValue From(std::string&& value,
absl::Nonnull<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND);
static BytesValue Wrap(absl::string_view value,
absl::Nullable<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND);
static BytesValue Wrap(absl::string_view value);
static BytesValue Wrap(const absl::Cord& value);
static BytesValue Wrap(std::string&& value) = delete;
static BytesValue Wrap(std::string&& value,
absl::Nullable<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND) = delete;
static BytesValue Concat(const BytesValue& lhs, const BytesValue& rhs,
absl::Nonnull<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND);
ABSL_DEPRECATED("Use From")
explicit BytesValue(absl::Nullable<const char*> value) : value_(value) {}
ABSL_DEPRECATED("Use From")
explicit BytesValue(absl::string_view value) : value_(value) {}
ABSL_DEPRECATED("Use From")
explicit BytesValue(const absl::Cord& value) : value_(value) {}
ABSL_DEPRECATED("Use From")
explicit BytesValue(std::string&& value) : value_(std::move(value)) {}
ABSL_DEPRECATED("Use From")
BytesValue(Allocator<> allocator, absl::Nullable<const char*> value)
: value_(allocator, value) {}
ABSL_DEPRECATED("Use From")
BytesValue(Allocator<> allocator, absl::string_view value)
: value_(allocator, value) {}
ABSL_DEPRECATED("Use From")
BytesValue(Allocator<> allocator, const absl::Cord& value)
: value_(allocator, value) {}
ABSL_DEPRECATED("Use From")
BytesValue(Allocator<> allocator, std::string&& value)
: value_(allocator, std::move(value)) {}
ABSL_DEPRECATED("Use Wrap")
BytesValue(Borrower borrower, absl::string_view value)
: value_(borrower, value) {}
ABSL_DEPRECATED("Use Wrap")
BytesValue(Borrower borrower, const absl::Cord& value)
: value_(borrower, value) {}
BytesValue() = default;
BytesValue(const BytesValue&) = default;
BytesValue(BytesValue&&) = default;
BytesValue& operator=(const BytesValue&) = default;
BytesValue& operator=(BytesValue&&) = default;
constexpr ValueKind kind() const { return kKind; }
absl::string_view GetTypeName() const { return BytesType::kName; }
std::string DebugString() const;
// See Value::SerializeTo().
absl::Status SerializeTo(
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<absl::Cord*> value) const;
// See Value::ConvertToJson().
absl::Status ConvertToJson(
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Message*> json) const;
absl::Status Equal(
const Value& other,
absl::Nonnull<const google::protobuf::DescriptorPool*> descriptor_pool,
absl::Nonnull<google::protobuf::MessageFactory*> message_factory,
absl::Nonnull<google::protobuf::Arena*> arena, absl::Nonnull<Value*> result) const;
using ValueMixin::Equal;
bool IsZeroValue() const {
return NativeValue([](const auto& value) -> bool { return value.empty(); });
}
BytesValue Clone(absl::Nonnull<google::protobuf::Arena*> arena) const;
ABSL_DEPRECATED("Use ToString()")
std::string NativeString() const { return value_.ToString(); }
ABSL_DEPRECATED("Use ToStringView()")
absl::string_view NativeString(
std::string& scratch
ABSL_ATTRIBUTE_LIFETIME_BOUND) const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return value_.ToStringView(&scratch);
}
ABSL_DEPRECATED("Use ToCord()")
absl::Cord NativeCord() const { return value_.ToCord(); }
template <typename Visitor>
ABSL_DEPRECATED("Use TryFlat()")
std::common_type_t<
std::invoke_result_t<Visitor, absl::string_view>,
std::invoke_result_t<Visitor, const absl::Cord&>> NativeValue(Visitor&&
visitor)
const {
return value_.Visit(std::forward<Visitor>(visitor));
}
void swap(BytesValue& other) noexcept {
using std::swap;
swap(value_, other.value_);
}
size_t Size() const;
bool IsEmpty() const;
bool Equals(absl::string_view bytes) const;
bool Equals(const absl::Cord& bytes) const;
bool Equals(const BytesValue& bytes) const;
int Compare(absl::string_view bytes) const;
int Compare(const absl::Cord& bytes) const;
int Compare(const BytesValue& bytes) const;
absl::optional<absl::string_view> TryFlat() const
ABSL_ATTRIBUTE_LIFETIME_BOUND {
return value_.TryFlat();
}
std::string ToString() const { return value_.ToString(); }
void CopyToString(absl::Nonnull<std::string*> out) const {
value_.CopyToString(out);
}
void AppendToString(absl::Nonnull<std::string*> out) const {
value_.AppendToString(out);
}
absl::Cord ToCord() const { return value_.ToCord(); }
void CopyToCord(absl::Nonnull<absl::Cord*> out) const {
value_.CopyToCord(out);
}
void AppendToCord(absl::Nonnull<absl::Cord*> out) const {
value_.AppendToCord(out);
}
absl::string_view ToStringView(
absl::Nonnull<std::string*> scratch
ABSL_ATTRIBUTE_LIFETIME_BOUND) const ABSL_ATTRIBUTE_LIFETIME_BOUND {
return value_.ToStringView(scratch);
}
friend bool operator<(const BytesValue& lhs, const BytesValue& rhs) {
return lhs.value_ < rhs.value_;
}
private:
friend class common_internal::ValueMixin<BytesValue>;
friend class BytesValueInputStream;
friend class BytesValueOutputStream;
friend absl::string_view common_internal::LegacyBytesValue(
const BytesValue& value, bool stable,
absl::Nonnull<google::protobuf::Arena*> arena);
friend struct ArenaTraits<BytesValue>;
explicit BytesValue(common_internal::ByteString value) noexcept
: value_(std::move(value)) {}
common_internal::ByteString value_;
};
inline void swap(BytesValue& lhs, BytesValue& rhs) noexcept { lhs.swap(rhs); }
inline std::ostream& operator<<(std::ostream& out, const BytesValue& value) {
return out << value.DebugString();
}
inline bool operator==(const BytesValue& lhs, absl::string_view rhs) {
return lhs.Equals(rhs);
}
inline bool operator==(absl::string_view lhs, const BytesValue& rhs) {
return rhs == lhs;
}
inline bool operator!=(const BytesValue& lhs, absl::string_view rhs) {
return !lhs.Equals(rhs);
}
inline bool operator!=(absl::string_view lhs, const BytesValue& rhs) {
return rhs != lhs;
}
inline BytesValue BytesValue::From(absl::Nullable<const char*> value,
absl::Nonnull<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND) {
return From(absl::NullSafeStringView(value), arena);
}
inline BytesValue BytesValue::From(absl::string_view value,
absl::Nonnull<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND) {
ABSL_DCHECK(arena != nullptr);
return BytesValue(arena, value);
}
inline BytesValue BytesValue::From(const absl::Cord& value) {
return BytesValue(value);
}
inline BytesValue BytesValue::From(std::string&& value,
absl::Nonnull<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND) {
ABSL_DCHECK(arena != nullptr);
return BytesValue(arena, std::move(value));
}
inline BytesValue BytesValue::Wrap(absl::string_view value,
absl::Nullable<google::protobuf::Arena*> arena
ABSL_ATTRIBUTE_LIFETIME_BOUND) {
ABSL_DCHECK(arena != nullptr);
return BytesValue(Borrower::Arena(arena), value);
}
inline BytesValue BytesValue::Wrap(absl::string_view value) {
return Wrap(value, nullptr);
}
inline BytesValue BytesValue::Wrap(const absl::Cord& value) {
return BytesValue(value);
}
namespace common_internal {
inline absl::string_view LegacyBytesValue(const BytesValue& value, bool stable,
absl::Nonnull<google::protobuf::Arena*> arena) {
return LegacyByteString(value.value_, stable, arena);
}
} // namespace common_internal
template <>
struct ArenaTraits<BytesValue> {
using constructible = std::true_type;
static bool trivially_destructible(const BytesValue& value) {
return ArenaTraits<>::trivially_destructible(value.value_);
}
};
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_COMMON_VALUES_BYTES_VALUE_H_