Debian Codegraph @

extra/protobuf/protobuf-24.4/src/google/protobuf/inlined_string_field.h

raw
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 #ifndef GOOGLE_PROTOBUF_INLINED_STRING_FIELD_H__
32 #define GOOGLE_PROTOBUF_INLINED_STRING_FIELD_H__
33
34 #include <string>
35 #include <utility>
36
37 #include "google/protobuf/port.h"
38 #include "absl/log/absl_check.h"
39 #include "absl/strings/string_view.h"
40 #include "google/protobuf/arenastring.h"
41 #include "google/protobuf/message_lite.h"
42
43 // Must be included last.
44 #include "google/protobuf/port_def.inc"
45
46 #ifdef SWIG
47 #error "You cannot SWIG proto headers"
48 #endif
49
50 namespace google {
51 namespace protobuf {
52
53 class Arena;
54
55 namespace internal {
56
57 // InlinedStringField wraps a std::string instance and exposes an API similar to
58 // ArenaStringPtr's wrapping of a std::string* instance.
59 //
60 // default_value parameters are taken for consistency with ArenaStringPtr, but
61 // are not used for most methods. With inlining, these should be removed from
62 // the generated binary.
63 //
64 // InlinedStringField has a donating mechanism that allows string buffer
65 // allocated on arena. A string is donated means both the string container and
66 // the data buffer are on arena. The donating mechanism here is similar to the
67 // one in ArenaStringPtr with some differences:
68 //
69 // When an InlinedStringField is constructed, the donating state is true. This
70 // is because the string container is directly stored in the message on the
71 // arena:
72 //
73 // Construction: donated=true
74 // Arena:
75 // +-----------------------+
76 // |Message foo: |
77 // | +-------------------+ |
78 // | |InlinedStringField:| |
79 // | | +-----+ | |
80 // | | | | | | | |
81 // | | +-----+ | |
82 // | +-------------------+ |
83 // +-----------------------+
84 //
85 // When lvalue Set is called, the donating state is still true. String data will
86 // be allocated on the arena:
87 //
88 // Lvalue Set: donated=true
89 // Arena:
90 // +-----------------------+
91 // |Message foo: |
92 // | +-------------------+ |
93 // | |InlinedStringField:| |
94 // | | +-----+ | |
95 // | | | | | | | |
96 // | | +|----+ | |
97 // | +--|----------------+ |
98 // | V |
99 // | +----------------+ |
100 // | |'f','o','o',... | |
101 // | +----------------+ |
102 // +-----------------------+
103 //
104 // Some operations will undonate a donated string, including: Mutable,
105 // SetAllocated, Rvalue Set, and Swap with a non-donated string.
106 //
107 // For more details of the donating states transitions, go/pd-inlined-string.
108 class PROTOBUF_EXPORT InlinedStringField {
109 public:
110 InlinedStringField() { Init(); }
111 inline void Init() { new (get_mutable()) std::string(); }
112 // Add the dummy parameter just to make InlinedStringField(nullptr)
113 // unambiguous.
114 constexpr InlinedStringField(
115 const ExplicitlyConstructed<std::string>* /*default_value*/,
116 bool /*dummy*/)
117 : value_{} {}
118 explicit InlinedStringField(const std::string& default_value);
119 explicit InlinedStringField(Arena* arena);
120 ~InlinedStringField() { Destruct(); }
121
122 // Lvalue Set. To save space, we pack the donating states of multiple
123 // InlinedStringFields into an uint32_t `donating_states`. The `mask`
124 // indicates the position of the bit for this InlinedStringField. `donated` is
125 // whether this field is donated.
126 //
127 // The caller should guarantee that:
128 //
129 // `donated == ((donating_states & ~mask) != 0)`
130 //
131 // This method never changes the `donating_states`.
132 void Set(absl::string_view value, Arena* arena, bool donated,
133 uint32_t* donating_states, uint32_t mask, MessageLite* msg);
134
135 // Rvalue Set. If this field is donated, this method will undonate this field
136 // by mutating the `donating_states` according to `mask`.
137 void Set(std::string&& value, Arena* arena, bool donated,
138 uint32_t* donating_states, uint32_t mask, MessageLite* msg);
139
140 void Set(const char* str, ::google::protobuf::Arena* arena, bool donated,
141 uint32_t* donating_states, uint32_t mask, MessageLite* msg);
142
143 void Set(const char* str, size_t size, ::google::protobuf::Arena* arena, bool donated,
144 uint32_t* donating_states, uint32_t mask, MessageLite* msg);
145
146 template <typename RefWrappedType>
147 void Set(std::reference_wrapper<RefWrappedType> const_string_ref,
148 ::google::protobuf::Arena* arena, bool donated, uint32_t* donating_states,
149 uint32_t mask, MessageLite* msg);
150
151 void SetBytes(absl::string_view value, Arena* arena, bool donated,
152 uint32_t* donating_states, uint32_t mask, MessageLite* msg);
153
154 void SetBytes(std::string&& value, Arena* arena, bool donated,
155 uint32_t* donating_states, uint32_t mask, MessageLite* msg);
156
157 void SetBytes(const char* str, ::google::protobuf::Arena* arena, bool donated,
158 uint32_t* donating_states, uint32_t mask, MessageLite* msg);
159
160 void SetBytes(const void* p, size_t size, ::google::protobuf::Arena* arena,
161 bool donated, uint32_t* donating_states, uint32_t mask,
162 MessageLite* msg);
163
164 template <typename RefWrappedType>
165 void SetBytes(std::reference_wrapper<RefWrappedType> const_string_ref,
166 ::google::protobuf::Arena* arena, bool donated, uint32_t* donating_states,
167 uint32_t mask, MessageLite* msg);
168
169 PROTOBUF_NDEBUG_INLINE void SetNoArena(absl::string_view value);
170 PROTOBUF_NDEBUG_INLINE void SetNoArena(std::string&& value);
171
172 // Basic accessors.
173 PROTOBUF_NDEBUG_INLINE const std::string& Get() const { return GetNoArena(); }
174 PROTOBUF_NDEBUG_INLINE const std::string& GetNoArena() const;
175
176 // Mutable returns a std::string* instance that is heap-allocated. If this
177 // field is donated, this method undonates this field by mutating the
178 // `donating_states` according to `mask`, and copies the content of the
179 // original string to the returning string.
180 std::string* Mutable(Arena* arena, bool donated, uint32_t* donating_states,
181 uint32_t mask, MessageLite* msg);
182 std::string* Mutable(const LazyString& default_value, Arena* arena,
183 bool donated, uint32_t* donating_states, uint32_t mask,
184 MessageLite* msg);
185
186 // Mutable(nullptr_t) is an overload to explicitly support Mutable(nullptr)
187 // calls used by the internal parser logic. This provides API equivalence with
188 // ArenaStringPtr, while still protecting against calls with arena pointers.
189 std::string* Mutable(std::nullptr_t);
190 std::string* MutableNoCopy(std::nullptr_t);
191
192 // Takes a std::string that is heap-allocated, and takes ownership. The
193 // std::string's destructor is registered with the arena. Used to implement
194 // set_allocated_<field> in generated classes.
195 //
196 // If this field is donated, this method undonates this field by mutating the
197 // `donating_states` according to `mask`.
198 void SetAllocated(const std::string* default_value, std::string* value,
199 Arena* arena, bool donated, uint32_t* donating_states,
200 uint32_t mask, MessageLite* msg);
201
202 void SetAllocatedNoArena(const std::string* default_value,
203 std::string* value);
204
205 // Release returns a std::string* instance that is heap-allocated and is not
206 // Own()'d by any arena. If the field is not set, this returns nullptr. The
207 // caller retains ownership. Clears this field back to nullptr state. Used to
208 // implement release_<field>() methods on generated classes.
209 PROTOBUF_NODISCARD std::string* Release(Arena* arena, bool donated);
210 PROTOBUF_NODISCARD std::string* Release();
211
212 // --------------------------------------------------------
213 // Below functions will be removed in subsequent code change
214 // --------------------------------------------------------
215 #ifdef DEPRECATED_METHODS_TO_BE_DELETED
216 PROTOBUF_NODISCARD std::string* Release(const std::string*, Arena* arena,
217 bool donated) {
218 return Release(arena, donated);
219 }
220
221 PROTOBUF_NODISCARD std::string* ReleaseNonDefault(const std::string*,
222 Arena* arena) {
223 return Release();
224 }
225
226 std::string* ReleaseNonDefaultNoArena(const std::string* default_value) {
227 return Release();
228 }
229
230 void Set(const std::string*, absl::string_view value, Arena* arena,
231 bool donated, uint32_t* donating_states, uint32_t mask,
232 MessageLite* msg) {
233 Set(value, arena, donated, donating_states, mask, msg);
234 }
235
236 void Set(const std::string*, std::string&& value, Arena* arena, bool donated,
237 uint32_t* donating_states, uint32_t mask, MessageLite* msg) {
238 Set(std::move(value), arena, donated, donating_states, mask, msg);
239 }
240
241
242 template <typename FirstParam>
243 void Set(FirstParam, const char* str, ::google::protobuf::Arena* arena, bool donated,
244 uint32_t* donating_states, uint32_t mask, MessageLite* msg) {
245 Set(str, arena, donated, donating_states, mask, msg);
246 }
247
248 template <typename FirstParam>
249 void Set(FirstParam p1, const char* str, size_t size, ::google::protobuf::Arena* arena,
250 bool donated, uint32_t* donating_states, uint32_t mask,
251 MessageLite* msg) {
252 Set(str, size, arena, donated, donating_states, mask, msg);
253 }
254
255 template <typename FirstParam, typename RefWrappedType>
256 void Set(FirstParam p1,
257 std::reference_wrapper<RefWrappedType> const_string_ref,
258 ::google::protobuf::Arena* arena, bool donated, uint32_t* donating_states,
259 uint32_t mask, MessageLite* msg) {
260 Set(const_string_ref, arena, donated, donating_states, mask, msg);
261 }
262
263 void SetBytes(const std::string*, absl::string_view value, Arena* arena,
264 bool donated, uint32_t* donating_states, uint32_t mask,
265 MessageLite* msg) {
266 Set(value, arena, donated, donating_states, mask, msg);
267 }
268
269
270 void SetBytes(const std::string*, std::string&& value, Arena* arena,
271 bool donated, uint32_t* donating_states, uint32_t mask,
272 MessageLite* msg) {
273 Set(std::move(value), arena, donated, donating_states, mask, msg);
274 }
275
276 template <typename FirstParam>
277 void SetBytes(FirstParam p1, const char* str, ::google::protobuf::Arena* arena,
278 bool donated, uint32_t* donating_states, uint32_t mask,
279 MessageLite* msg) {
280 SetBytes(str, arena, donated, donating_states, mask, msg);
281 }
282
283 template <typename FirstParam>
284 void SetBytes(FirstParam p1, const void* p, size_t size,
285 ::google::protobuf::Arena* arena, bool donated, uint32_t* donating_states,
286 uint32_t mask, MessageLite* msg) {
287 SetBytes(p, size, arena, donated, donating_states, mask, msg);
288 }
289
290 template <typename FirstParam, typename RefWrappedType>
291 void SetBytes(FirstParam p1,
292 std::reference_wrapper<RefWrappedType> const_string_ref,
293 ::google::protobuf::Arena* arena, bool donated, uint32_t* donating_states,
294 uint32_t mask, MessageLite* msg) {
295 SetBytes(const_string_ref.get(), arena, donated, donating_states, mask,
296 msg);
297 }
298
299 void SetNoArena(const std::string*, absl::string_view value) {
300 SetNoArena(value);
301 }
302 void SetNoArena(const std::string*, std::string&& value) {
303 SetNoArena(std::move(value));
304 }
305
306 std::string* Mutable(ArenaStringPtr::EmptyDefault, Arena* arena, bool donated,
307 uint32_t* donating_states, uint32_t mask,
308 MessageLite* msg) {
309 return Mutable(arena, donated, donating_states, mask, msg);
310 }
311
312 PROTOBUF_NDEBUG_INLINE std::string* MutableNoArenaNoDefault(
313 const std::string* /*default_value*/) {
314 return MutableNoCopy(nullptr);
315 }
316
317 #endif // DEPRECATED_METHODS_TO_BE_DELETED
318
319 // Arena-safety semantics: this is guarded by the logic in
320 // Swap()/UnsafeArenaSwap() at the message level, so this method is
321 // 'unsafe' if called directly.
322 inline PROTOBUF_NDEBUG_INLINE static void InternalSwap(
323 InlinedStringField* lhs, Arena* lhs_arena, bool lhs_arena_dtor_registered,
324 MessageLite* lhs_msg, //
325 InlinedStringField* rhs, Arena* rhs_arena, bool rhs_arena_dtor_registered,
326 MessageLite* rhs_msg);
327
328 // Frees storage (if not on an arena).
329 PROTOBUF_NDEBUG_INLINE void Destroy(const std::string* default_value,
330 Arena* arena) {
331 if (arena == nullptr) {
332 DestroyNoArena(default_value);
333 }
334 }
335 PROTOBUF_NDEBUG_INLINE void DestroyNoArena(const std::string* default_value);
336
337 // Clears content, but keeps allocated std::string, to avoid the overhead of
338 // heap operations. After this returns, the content (as seen by the user) will
339 // always be the empty std::string.
340 PROTOBUF_NDEBUG_INLINE void ClearToEmpty() { ClearNonDefaultToEmpty(); }
341 PROTOBUF_NDEBUG_INLINE void ClearNonDefaultToEmpty() {
342 get_mutable()->clear();
343 }
344
345 // Clears content, but keeps allocated std::string if arena != nullptr, to
346 // avoid the overhead of heap operations. After this returns, the content (as
347 // seen by the user) will always be equal to |default_value|.
348 void ClearToDefault(const LazyString& default_value, Arena* arena,
349 bool donated);
350
351 // Generated code / reflection only! Returns a mutable pointer to the string.
352 PROTOBUF_NDEBUG_INLINE std::string* UnsafeMutablePointer();
353
354 // InlinedStringField doesn't have things like the `default_value` pointer in
355 // ArenaStringPtr.
356 static constexpr bool IsDefault() { return false; }
357 static constexpr bool IsDefault(const std::string*) { return false; }
358
359 private:
360 void Destruct() { get_mutable()->~basic_string(); }
361
362 PROTOBUF_NDEBUG_INLINE std::string* get_mutable();
363 PROTOBUF_NDEBUG_INLINE const std::string* get_const() const;
364
365 alignas(std::string) char value_[sizeof(std::string)];
366
367 std::string* MutableSlow(::google::protobuf::Arena* arena, bool donated,
368 uint32_t* donating_states, uint32_t mask,
369 MessageLite* msg);
370
371
372 // When constructed in an Arena, we want our destructor to be skipped.
373 friend class ::google::protobuf::Arena;
374 typedef void InternalArenaConstructable_;
375 typedef void DestructorSkippable_;
376 };
377
378 inline std::string* InlinedStringField::get_mutable() {
379 return reinterpret_cast<std::string*>(&value_);
380 }
381
382 inline const std::string* InlinedStringField::get_const() const {
383 return reinterpret_cast<const std::string*>(&value_);
384 }
385
386 inline InlinedStringField::InlinedStringField(
387 const std::string& default_value) {
388 new (get_mutable()) std::string(default_value);
389 }
390
391
392 inline InlinedStringField::InlinedStringField(Arena* /*arena*/) { Init(); }
393
394 inline const std::string& InlinedStringField::GetNoArena() const {
395 return *get_const();
396 }
397
398 inline void InlinedStringField::SetAllocatedNoArena(
399 const std::string* /*default_value*/, std::string* value) {
400 if (value == nullptr) {
401 // Currently, inlined string field can't have non empty default.
402 get_mutable()->clear();
403 } else {
404 get_mutable()->assign(std::move(*value));
405 delete value;
406 }
407 }
408
409 inline void InlinedStringField::DestroyNoArena(const std::string*) {
410 // This is invoked from the generated message's ArenaDtor, which is used to
411 // clean up objects not allocated on the Arena.
412 this->~InlinedStringField();
413 }
414
415 inline void InlinedStringField::SetNoArena(absl::string_view value) {
416 get_mutable()->assign(value.data(), value.length());
417 }
418
419 inline void InlinedStringField::SetNoArena(std::string&& value) {
420 get_mutable()->assign(std::move(value));
421 }
422
423 // Caller should make sure rhs_arena allocated rhs, and lhs_arena allocated lhs.
424 inline PROTOBUF_NDEBUG_INLINE void InlinedStringField::InternalSwap(
425 InlinedStringField* lhs, Arena* lhs_arena, bool lhs_arena_dtor_registered,
426 MessageLite* lhs_msg, //
427 InlinedStringField* rhs, Arena* rhs_arena, bool rhs_arena_dtor_registered,
428 MessageLite* rhs_msg) {
429 #ifdef GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL_INLINE
430 lhs->get_mutable()->swap(*rhs->get_mutable());
431 if (!lhs_arena_dtor_registered && rhs_arena_dtor_registered) {
432 lhs_msg->OnDemandRegisterArenaDtor(lhs_arena);
433 } else if (lhs_arena_dtor_registered && !rhs_arena_dtor_registered) {
434 rhs_msg->OnDemandRegisterArenaDtor(rhs_arena);
435 }
436 #else
437 (void)lhs_arena;
438 (void)rhs_arena;
439 (void)lhs_arena_dtor_registered;
440 (void)rhs_arena_dtor_registered;
441 (void)lhs_msg;
442 (void)rhs_msg;
443 lhs->get_mutable()->swap(*rhs->get_mutable());
444 #endif
445 }
446
447 inline void InlinedStringField::Set(absl::string_view value, Arena* arena,
448 bool donated, uint32_t* /*donating_states*/,
449 uint32_t /*mask*/, MessageLite* /*msg*/) {
450 (void)arena;
451 (void)donated;
452 SetNoArena(value);
453 }
454
455 inline void InlinedStringField::Set(const char* str, ::google::protobuf::Arena* arena,
456 bool donated, uint32_t* donating_states,
457 uint32_t mask, MessageLite* msg) {
458 Set(absl::string_view(str), arena, donated, donating_states, mask, msg);
459 }
460
461 inline void InlinedStringField::Set(const char* str, size_t size,
462 ::google::protobuf::Arena* arena, bool donated,
463 uint32_t* donating_states, uint32_t mask,
464 MessageLite* msg) {
465 Set(absl::string_view{str, size}, arena, donated, donating_states, mask, msg);
466 }
467
468 inline void InlinedStringField::SetBytes(absl::string_view value, Arena* arena,
469 bool donated,
470 uint32_t* donating_states,
471 uint32_t mask, MessageLite* msg) {
472 Set(value, arena, donated, donating_states, mask, msg);
473 }
474
475 inline void InlinedStringField::SetBytes(std::string&& value, Arena* arena,
476 bool donated,
477 uint32_t* donating_states,
478 uint32_t mask, MessageLite* msg) {
479 Set(std::move(value), arena, donated, donating_states, mask, msg);
480 }
481
482 inline void InlinedStringField::SetBytes(const char* str,
483 ::google::protobuf::Arena* arena, bool donated,
484 uint32_t* donating_states,
485 uint32_t mask, MessageLite* msg) {
486 Set(str, arena, donated, donating_states, mask, msg);
487 }
488
489 inline void InlinedStringField::SetBytes(const void* p, size_t size,
490 ::google::protobuf::Arena* arena, bool donated,
491 uint32_t* donating_states,
492 uint32_t mask, MessageLite* msg) {
493 Set(static_cast<const char*>(p), size, arena, donated, donating_states, mask,
494 msg);
495 }
496
497 template <typename RefWrappedType>
498 inline void InlinedStringField::Set(
499 std::reference_wrapper<RefWrappedType> const_string_ref,
500 ::google::protobuf::Arena* arena, bool donated, uint32_t* donating_states,
501 uint32_t mask, MessageLite* msg) {
502 Set(const_string_ref.get(), arena, donated, donating_states, mask, msg);
503 }
504
505 template <typename RefWrappedType>
506 inline void InlinedStringField::SetBytes(
507 std::reference_wrapper<RefWrappedType> const_string_ref,
508 ::google::protobuf::Arena* arena, bool donated, uint32_t* donating_states,
509 uint32_t mask, MessageLite* msg) {
510 Set(const_string_ref.get(), arena, donated, donating_states, mask, msg);
511 }
512
513 inline std::string* InlinedStringField::UnsafeMutablePointer() {
514 return get_mutable();
515 }
516
517 inline std::string* InlinedStringField::Mutable(std::nullptr_t) {
518 return get_mutable();
519 }
520
521 inline std::string* InlinedStringField::MutableNoCopy(std::nullptr_t) {
522 return get_mutable();
523 }
524
525 } // namespace internal
526 } // namespace protobuf
527 } // namespace google
528
529 #include "google/protobuf/port_undef.inc"
530
531 #endif // GOOGLE_PROTOBUF_INLINED_STRING_FIELD_H__