forked from protocolbuffers/upb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepeated_field.h
309 lines (267 loc) · 11.7 KB
/
repeated_field.h
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
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google LLC nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef UPB_PROTOS_REPEATED_FIELD_H_
#define UPB_PROTOS_REPEATED_FIELD_H_
#include <cstddef>
#include <iterator>
#include <type_traits>
#include "absl/base/attributes.h"
#include "absl/strings/string_view.h"
#include "protos/protos.h"
#include "protos/protos_traits.h"
#include "protos/repeated_field_iterator.h"
#include "upb/base/string_view.h"
#include "upb/collections/array.h"
#include "upb/collections/internal/array.h"
#include "upb/mem/arena.h"
#include "upb/message/copy.h"
// Must be last:
#include "upb/port/def.inc"
namespace protos {
namespace internal {
// Shared implementation of repeated fields for absl::string_view and
// message types for mutable and immutable variants.
//
// Immutable (const accessor), constructs this class with a nullptr upb_Array*
// when the underlying array in the message is empty.
//
// Mutable accessors on the other hand, will allocate a new empty non-null
// upb_Array* for the message when the RepeatedFieldProxy is constructed.
template <class T>
class RepeatedFieldProxyBase {
using Array = add_const_if_T_is_const<T, upb_Array>;
public:
explicit RepeatedFieldProxyBase(Array* arr, upb_Arena* arena)
: arr_(arr), arena_(arena) {}
size_t size() const { return arr_ != nullptr ? upb_Array_Size(arr_) : 0; }
bool empty() const { return size() == 0; }
protected:
// Returns upb_Array message member.
inline upb_Message* GetMessage(size_t n) const;
Array* arr_;
upb_Arena* arena_;
};
template <class T>
upb_Message* RepeatedFieldProxyBase<T>::GetMessage(size_t n) const {
auto** messages =
static_cast<upb_Message**>(upb_Array_MutableDataPtr(this->arr_));
return messages[n];
}
template <class T>
class RepeatedFieldProxyMutableBase : public RepeatedFieldProxyBase<T> {
public:
RepeatedFieldProxyMutableBase(upb_Array* arr, upb_Arena* arena)
: RepeatedFieldProxyBase<T>(arr, arena) {}
void clear() { upb_Array_Resize(this->arr_, 0, this->arena_); }
};
// RepeatedField proxy for repeated messages.
template <class T>
class RepeatedFieldProxy
: public std::conditional_t<std::is_const_v<T>, RepeatedFieldProxyBase<T>,
RepeatedFieldProxyMutableBase<T>> {
static_assert(!std::is_same_v<T, absl::string_view>, "");
static_assert(!std::is_same_v<T, const absl::string_view>, "");
static_assert(!std::is_arithmetic_v<T>, "");
static constexpr bool kIsConst = std::is_const_v<T>;
public:
explicit RepeatedFieldProxy(const upb_Array* arr, upb_Arena* arena)
: RepeatedFieldProxyBase<T>(arr, arena) {}
RepeatedFieldProxy(upb_Array* arr, upb_Arena* arena)
: RepeatedFieldProxyMutableBase<T>(arr, arena) {}
// Constructor used by ::protos::Ptr.
RepeatedFieldProxy(const RepeatedFieldProxy&) = default;
// T::CProxy [] operator specialization.
typename T::CProxy operator[](size_t n) const {
upb_MessageValue message_value = upb_Array_Get(this->arr_, n);
return ::protos::internal::CreateMessage<typename std::remove_const_t<T>>(
(upb_Message*)message_value.msg_val, this->arena_);
}
// TODO(b:/280069986) : Audit/Finalize based on Iterator Design.
// T::Proxy [] operator specialization.
template <int&... DeductionBlocker, bool b = !kIsConst,
typename = std::enable_if_t<b>>
typename T::Proxy operator[](size_t n) {
return ::protos::internal::CreateMessageProxy<T>(this->GetMessage(n),
this->arena_);
}
// Mutable message reference specialization.
template <int&... DeductionBlocker, bool b = !kIsConst,
typename = std::enable_if_t<b>>
void push_back(const T& t) {
upb_MessageValue message_value;
message_value.msg_val = upb_Message_DeepClone(
PrivateAccess::GetInternalMsg(&t), ::protos::internal::GetMiniTable(&t),
this->arena_);
upb_Array_Append(this->arr_, message_value, this->arena_);
}
// Mutable message add using move.
template <int&... DeductionBlocker, bool b = !kIsConst,
typename = std::enable_if_t<b>>
void push_back(T&& msg) {
upb_MessageValue message_value;
message_value.msg_val = PrivateAccess::GetInternalMsg(&msg);
upb_Arena_Fuse(GetArena(&msg), this->arena_);
upb_Array_Append(this->arr_, message_value, this->arena_);
T moved_msg = std::move(msg);
}
private:
friend class ::protos::Ptr<T>;
};
// RepeatedField proxy for repeated strings.
template <class T>
class RepeatedFieldStringProxy
: public std::conditional_t<std::is_const_v<T>, RepeatedFieldProxyBase<T>,
RepeatedFieldProxyMutableBase<T>> {
static_assert(std::is_same_v<T, absl::string_view> ||
std::is_same_v<T, const absl::string_view>,
"");
static constexpr bool kIsConst = std::is_const_v<T>;
public:
using value_type = std::remove_const_t<T>;
using size_type = size_t;
using difference_type = ptrdiff_t;
using iterator = internal::Iterator<StringIteratorPolicy<T>>;
using reference = typename iterator::reference;
using pointer = typename iterator::pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
// Immutable constructor.
explicit RepeatedFieldStringProxy(const upb_Array* arr, upb_Arena* arena)
: RepeatedFieldProxyBase<T>(arr, arena) {}
// Mutable constructor.
RepeatedFieldStringProxy(upb_Array* arr, upb_Arena* arena)
: RepeatedFieldProxyMutableBase<T>(arr, arena) {}
// Constructor used by ::protos::Ptr.
RepeatedFieldStringProxy(const RepeatedFieldStringProxy&) = default;
reference operator[](size_t n) const { return begin()[n]; }
template <int&... DeductionBlocker, bool b = !kIsConst,
typename = std::enable_if_t<b>>
void push_back(T t) {
upb_MessageValue message_value;
// Copy string to arena.
UPB_ASSERT(this->arena_);
char* data = (char*)upb_Arena_Malloc(this->arena_, t.size());
UPB_ASSERT(data);
memcpy(data, t.data(), t.size());
message_value.str_val = upb_StringView_FromDataAndSize(data, t.size());
upb_Array_Append(this->arr_, message_value, this->arena_);
}
iterator begin() const { return iterator({this->arr_, this->arena_, 0}); }
iterator end() const {
return iterator({this->arr_, this->arena_, this->size()});
}
reverse_iterator rbegin() const { return reverse_iterator(end()); }
reverse_iterator rend() const { return reverse_iterator(begin()); }
};
// RepeatedField proxy for repeated scalar types.
template <typename T>
class RepeatedFieldScalarProxy
: public std::conditional_t<std::is_const_v<T>, RepeatedFieldProxyBase<T>,
RepeatedFieldProxyMutableBase<T>> {
static_assert(std::is_arithmetic_v<T>, "");
static constexpr bool kIsConst = std::is_const_v<T>;
public:
using value_type = std::remove_const_t<T>;
using size_type = size_t;
using difference_type = ptrdiff_t;
using iterator = internal::Iterator<ScalarIteratorPolicy<T>>;
using reference = typename iterator::reference;
using pointer = typename iterator::pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
explicit RepeatedFieldScalarProxy(const upb_Array* arr, upb_Arena* arena)
: RepeatedFieldProxyBase<T>(arr, arena) {}
RepeatedFieldScalarProxy(upb_Array* arr, upb_Arena* arena)
: RepeatedFieldProxyMutableBase<T>(arr, arena) {}
// Constructor used by ::protos::Ptr.
RepeatedFieldScalarProxy(const RepeatedFieldScalarProxy&) = default;
T operator[](size_t n) const {
upb_MessageValue message_value = upb_Array_Get(this->arr_, n);
typename std::remove_const_t<T> val;
memcpy(&val, &message_value, sizeof(T));
return val;
}
template <int&... DeductionBlocker, bool b = !kIsConst,
typename = std::enable_if_t<b>>
void push_back(T t) {
upb_MessageValue message_value;
memcpy(&message_value, &t, sizeof(T));
upb_Array_Append(this->arr_, message_value, this->arena_);
}
iterator begin() const { return iterator({unsafe_array()}); }
iterator cbegin() const { return begin(); }
iterator end() const { return iterator({unsafe_array() + this->size()}); }
iterator cend() const { return end(); }
// Reverse iterator support.
reverse_iterator rbegin() const { return reverse_iterator(end()); }
reverse_iterator rend() const { return reverse_iterator(begin()); }
reverse_iterator crbegin() const { return reverse_iterator(end()); }
reverse_iterator crend() const { return reverse_iterator(begin()); }
private:
T* unsafe_array() const {
if (kIsConst) {
const void* unsafe_ptr = ::upb_Array_DataPtr(this->arr_);
return static_cast<T*>(const_cast<void*>(unsafe_ptr));
}
if (!kIsConst) {
void* unsafe_ptr =
::upb_Array_MutableDataPtr(const_cast<upb_Array*>(this->arr_));
return static_cast<T*>(unsafe_ptr);
}
}
};
} // namespace internal
template <typename T>
class RepeatedField {
static constexpr bool kIsString = std::is_same_v<T, absl::string_view>;
static constexpr bool kIsScalar = std::is_arithmetic_v<T>;
public:
using Proxy = std::conditional_t<
kIsScalar, internal::RepeatedFieldScalarProxy<T>,
std::conditional_t<kIsString, internal::RepeatedFieldStringProxy<T>,
internal::RepeatedFieldProxy<T>>>;
using CProxy = std::conditional_t<
kIsScalar, internal::RepeatedFieldScalarProxy<const T>,
std::conditional_t<kIsString, internal::RepeatedFieldStringProxy<const T>,
internal::RepeatedFieldProxy<const T>>>;
// TODO(b/286451125): T supports incomplete type from fwd.h forwarding headers
// We would like to reference T::CProxy. Validate forwarding header design.
using ValueProxy = std::conditional_t<
kIsScalar, T,
std::conditional_t<kIsString, absl::string_view, ::protos::Ptr<T>>>;
using ValueCProxy = std::conditional_t<
kIsScalar, const T,
std::conditional_t<kIsString, absl::string_view, ::protos::Ptr<const T>>>;
using Access = std::conditional_t<
kIsScalar, internal::RepeatedFieldScalarProxy<T>,
std::conditional_t<kIsString, internal::RepeatedFieldStringProxy<T>,
internal::RepeatedFieldProxy<T>>>;
};
} // namespace protos
#include "upb/port/undef.inc"
#endif // UPB_PROTOS_REPEATED_FIELD_H_