1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #include "google/protobuf/inlined_string_field.h"
9
10 #include <cstddef>
11 #include <cstdint>
12 #include <cstring>
13 #include <string>
14 #include <utility>
15
16 #include "absl/base/optimization.h"
17 #include "absl/log/absl_check.h"
18 #include "absl/strings/internal/resize_uninitialized.h"
19 #include "absl/strings/string_view.h"
20 #include "google/protobuf/arena.h"
21 #include "google/protobuf/arena_align.h"
22 #include "google/protobuf/arenastring.h"
23 #include "google/protobuf/generated_message_util.h"
24 #include "google/protobuf/message_lite.h"
25 #include "google/protobuf/parse_context.h"
26
27
28 // clang-format off
29 #include "google/protobuf/port_def.inc"
30 // clang-format on
31
32 namespace google {
33 namespace protobuf {
34 namespace internal {
35
36 #if defined(NDEBUG) || !defined(GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL_INLINE)
37
38 class InlinedStringField::ScopedCheckInvariants {
39 public:
ScopedCheckInvariants(const InlinedStringField *)40 constexpr explicit ScopedCheckInvariants(const InlinedStringField*) {}
41 };
42
43 #endif // NDEBUG || !GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL_INLINE
44
45
Mutable(const LazyString &,Arena * arena,bool donated,uint32_t * donating_states,uint32_t mask,MessageLite * msg)46 std::string* InlinedStringField::Mutable(const LazyString& /*default_value*/,
47 Arena* arena, bool donated,
48 uint32_t* donating_states,
49 uint32_t mask, MessageLite* msg) {
50 ScopedCheckInvariants invariants(this);
51 if (arena == nullptr || !donated) {
52 return UnsafeMutablePointer();
53 }
54 return MutableSlow(arena, donated, donating_states, mask, msg);
55 }
56
Mutable(Arena * arena,bool donated,uint32_t * donating_states,uint32_t mask,MessageLite * msg)57 std::string* InlinedStringField::Mutable(Arena* arena, bool donated,
58 uint32_t* donating_states,
59 uint32_t mask, MessageLite* msg) {
60 ScopedCheckInvariants invariants(this);
61 if (arena == nullptr || !donated) {
62 return UnsafeMutablePointer();
63 }
64 return MutableSlow(arena, donated, donating_states, mask, msg);
65 }
66
MutableSlow(::google::protobuf::Arena * arena,bool donated,uint32_t * donating_states,uint32_t mask,MessageLite * msg)67 std::string* InlinedStringField::MutableSlow(::google::protobuf::Arena* arena,
68 bool donated,
69 uint32_t* donating_states,
70 uint32_t mask, MessageLite* msg) {
71 (void)mask;
72 (void)msg;
73 return UnsafeMutablePointer();
74 }
75
SetAllocated(const std::string * default_value,std::string * value,Arena * arena,bool donated,uint32_t * donating_states,uint32_t mask,MessageLite * msg)76 void InlinedStringField::SetAllocated(const std::string* default_value,
77 std::string* value, Arena* arena,
78 bool donated, uint32_t* donating_states,
79 uint32_t mask, MessageLite* msg) {
80 (void)mask;
81 (void)msg;
82 SetAllocatedNoArena(default_value, value);
83 }
84
Set(std::string && value,Arena * arena,bool donated,uint32_t * donating_states,uint32_t mask,MessageLite * msg)85 void InlinedStringField::Set(std::string&& value, Arena* arena, bool donated,
86 uint32_t* donating_states, uint32_t mask,
87 MessageLite* msg) {
88 (void)donating_states;
89 (void)mask;
90 (void)msg;
91 SetNoArena(std::move(value));
92 }
93
Release()94 std::string* InlinedStringField::Release() {
95 auto* released = new std::string(std::move(*get_mutable()));
96 get_mutable()->clear();
97 return released;
98 }
99
Release(Arena * arena,bool donated)100 std::string* InlinedStringField::Release(Arena* arena, bool donated) {
101 // We can not steal donated arena strings.
102 std::string* released = (arena != nullptr && donated)
103 ? new std::string(*get_mutable())
104 : new std::string(std::move(*get_mutable()));
105 get_mutable()->clear();
106 return released;
107 }
108
ClearToDefault(const LazyString & default_value,Arena * arena,bool donated)109 void InlinedStringField::ClearToDefault(const LazyString& default_value,
110 Arena* arena, bool donated) {
111 (void)arena;
112 get_mutable()->assign(default_value.get());
113 }
114
115
116 } // namespace internal
117 } // namespace protobuf
118 } // namespace google
119
120 #include "google/protobuf/port_undef.inc"
121