xref: /aosp_15_r20/external/protobuf/src/google/protobuf/inlined_string_field.cc (revision 1b3f573f81763fcece89efc2b6a5209149e44ab8)
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 #include <google/protobuf/inlined_string_field.h>
32 
33 #include <google/protobuf/arena.h>
34 #include <google/protobuf/arenastring.h>
35 #include <google/protobuf/generated_message_util.h>
36 #include <google/protobuf/message_lite.h>
37 #include <google/protobuf/parse_context.h>
38 
39 // clang-format off
40 #include <google/protobuf/port_def.inc>
41 // clang-format on
42 
43 namespace google {
44 namespace protobuf {
45 namespace internal {
46 
47 
Mutable(const LazyString &,Arena * arena,bool donated,uint32_t * donating_states,uint32_t mask,MessageLite * msg)48 std::string* InlinedStringField::Mutable(const LazyString& /*default_value*/,
49                                          Arena* arena, bool donated,
50                                          uint32_t* donating_states,
51                                          uint32_t mask, MessageLite* msg) {
52   if (arena == nullptr || !donated) {
53     return UnsafeMutablePointer();
54   }
55   return MutableSlow(arena, donated, donating_states, mask, msg);
56 }
57 
Mutable(Arena * arena,bool donated,uint32_t * donating_states,uint32_t mask,MessageLite * msg)58 std::string* InlinedStringField::Mutable(Arena* arena, bool donated,
59                                          uint32_t* donating_states,
60                                          uint32_t mask, MessageLite* msg) {
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