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 <google/protobuf/stubs/stringpiece.h>
39
40 #include <google/protobuf/port_def.inc>
41
42 #ifdef SWIG
43 #error "You cannot SWIG proto headers"
44 #endif
45
46 namespace google {
47 namespace protobuf {
48
49 class Arena;
50
51 namespace internal {
52
53 // InlinedStringField wraps a std::string instance and exposes an API similar to
54 // ArenaStringPtr's wrapping of a std::string* instance. As std::string is
55 // never allocated on the Arena, we expose only the *NoArena methods of
56 // ArenaStringPtr.
57 //
58 // default_value parameters are taken for consistency with ArenaStringPtr, but
59 // are not used for most methods. With inlining, these should be removed from
60 // the generated binary.
61 class PROTOBUF_EXPORT InlinedStringField {
62 public:
63 InlinedStringField() PROTOBUF_ALWAYS_INLINE;
64 explicit InlinedStringField(const std::string& default_value);
65
66 void AssignWithDefault(const std::string* default_value,
67 const InlinedStringField& from) PROTOBUF_ALWAYS_INLINE;
68
ClearToEmpty(const std::string * default_value,Arena *)69 void ClearToEmpty(const std::string* default_value,
70 Arena* /*arena*/) PROTOBUF_ALWAYS_INLINE {
71 ClearToEmptyNoArena(default_value);
72 }
ClearNonDefaultToEmpty()73 void ClearNonDefaultToEmpty() PROTOBUF_ALWAYS_INLINE {
74 ClearNonDefaultToEmptyNoArena();
75 }
ClearToEmptyNoArena(const std::string *)76 void ClearToEmptyNoArena(const std::string* /*default_value*/)
77 PROTOBUF_ALWAYS_INLINE {
78 ClearNonDefaultToEmptyNoArena();
79 }
80 void ClearNonDefaultToEmptyNoArena() PROTOBUF_ALWAYS_INLINE;
81
ClearToDefault(const std::string * default_value,Arena *)82 void ClearToDefault(const std::string* default_value,
83 Arena* /*arena*/) PROTOBUF_ALWAYS_INLINE {
84 ClearToDefaultNoArena(default_value);
85 }
86 void ClearToDefaultNoArena(const std::string* default_value)
87 PROTOBUF_ALWAYS_INLINE;
88
Destroy(const std::string * default_value,Arena *)89 void Destroy(const std::string* default_value,
90 Arena* /*arena*/) PROTOBUF_ALWAYS_INLINE {
91 DestroyNoArena(default_value);
92 }
93 void DestroyNoArena(const std::string* default_value) PROTOBUF_ALWAYS_INLINE;
94
Get()95 const std::string& Get() const PROTOBUF_ALWAYS_INLINE { return GetNoArena(); }
96 const std::string& GetNoArena() const PROTOBUF_ALWAYS_INLINE;
97
Mutable(const std::string * default_value,Arena *)98 std::string* Mutable(const std::string* default_value,
99 Arena* /*arena*/) PROTOBUF_ALWAYS_INLINE {
100 return MutableNoArena(default_value);
101 }
102 std::string* MutableNoArena(const std::string* default_value)
103 PROTOBUF_ALWAYS_INLINE;
104
Release(const std::string * default_value,Arena *)105 std::string* Release(const std::string* default_value, Arena* /*arena*/) {
106 return ReleaseNoArena(default_value);
107 }
ReleaseNonDefault(const std::string * default_value,Arena *)108 std::string* ReleaseNonDefault(const std::string* default_value,
109 Arena* /*arena*/) {
110 return ReleaseNonDefaultNoArena(default_value);
111 }
ReleaseNoArena(const std::string * default_value)112 std::string* ReleaseNoArena(const std::string* default_value) {
113 return ReleaseNonDefaultNoArena(default_value);
114 }
115 std::string* ReleaseNonDefaultNoArena(const std::string* default_value);
116
Set(const std::string * default_value,StringPiece value,Arena *)117 void Set(const std::string* default_value, StringPiece value,
118 Arena* /*arena*/) PROTOBUF_ALWAYS_INLINE {
119 SetNoArena(default_value, value);
120 }
SetLite(const std::string * default_value,StringPiece value,Arena *)121 void SetLite(const std::string* default_value, StringPiece value,
122 Arena* /*arena*/) PROTOBUF_ALWAYS_INLINE {
123 SetNoArena(default_value, value);
124 }
125 void SetNoArena(const std::string* default_value,
126 StringPiece value) PROTOBUF_ALWAYS_INLINE;
127
Set(const std::string * default_value,const std::string & value,Arena *)128 void Set(const std::string* default_value, const std::string& value,
129 Arena* /*arena*/) PROTOBUF_ALWAYS_INLINE {
130 SetNoArena(default_value, value);
131 }
SetLite(const std::string * default_value,const std::string & value,Arena *)132 void SetLite(const std::string* default_value, const std::string& value,
133 Arena* /*arena*/) PROTOBUF_ALWAYS_INLINE {
134 SetNoArena(default_value, value);
135 }
136 void SetNoArena(const std::string* default_value,
137 const std::string& value) PROTOBUF_ALWAYS_INLINE;
138
139 void SetNoArena(const std::string* default_value,
140 std::string&& value) PROTOBUF_ALWAYS_INLINE;
SetAllocated(const std::string * default_value,std::string * value,Arena *)141 void SetAllocated(const std::string* default_value, std::string* value,
142 Arena* /*arena*/) {
143 SetAllocatedNoArena(default_value, value);
144 }
145 void SetAllocatedNoArena(const std::string* default_value,
146 std::string* value);
147 void Swap(InlinedStringField* from) PROTOBUF_ALWAYS_INLINE;
148 std::string* UnsafeMutablePointer();
149 void UnsafeSetDefault(const std::string* default_value);
150 std::string* UnsafeArenaRelease(const std::string* default_value,
151 Arena* arena);
152 void UnsafeArenaSetAllocated(const std::string* default_value,
153 std::string* value, Arena* arena);
154
IsDefault(const std::string *)155 bool IsDefault(const std::string* /*default_value*/) { return false; }
156
157 private:
158 std::string value_;
159 };
160
InlinedStringField()161 inline InlinedStringField::InlinedStringField() {}
162
InlinedStringField(const std::string & default_value)163 inline InlinedStringField::InlinedStringField(const std::string& default_value)
164 : value_(default_value) {}
165
AssignWithDefault(const std::string *,const InlinedStringField & from)166 inline void InlinedStringField::AssignWithDefault(
167 const std::string* /*default_value*/, const InlinedStringField& from) {
168 value_ = from.value_;
169 }
170
GetNoArena()171 inline const std::string& InlinedStringField::GetNoArena() const {
172 return value_;
173 }
174
MutableNoArena(const std::string *)175 inline std::string* InlinedStringField::MutableNoArena(const std::string*) {
176 return &value_;
177 }
178
SetAllocatedNoArena(const std::string * default_value,std::string * value)179 inline void InlinedStringField::SetAllocatedNoArena(
180 const std::string* default_value, std::string* value) {
181 if (value == NULL) {
182 value_.assign(*default_value);
183 } else {
184 value_.assign(std::move(*value));
185 delete value;
186 }
187 }
188
DestroyNoArena(const std::string *)189 inline void InlinedStringField::DestroyNoArena(const std::string*) {
190 // This is invoked from the generated message's ArenaDtor, which is used to
191 // clean up objects not allocated on the Arena.
192 this->~InlinedStringField();
193 }
194
ClearNonDefaultToEmptyNoArena()195 inline void InlinedStringField::ClearNonDefaultToEmptyNoArena() {
196 value_.clear();
197 }
198
ClearToDefaultNoArena(const std::string * default_value)199 inline void InlinedStringField::ClearToDefaultNoArena(
200 const std::string* default_value) {
201 value_.assign(*default_value);
202 }
203
ReleaseNonDefaultNoArena(const std::string * default_value)204 inline std::string* InlinedStringField::ReleaseNonDefaultNoArena(
205 const std::string* default_value) {
206 std::string* released = new std::string(*default_value);
207 value_.swap(*released);
208 return released;
209 }
210
SetNoArena(const std::string *,StringPiece value)211 inline void InlinedStringField::SetNoArena(const std::string* /*default_value*/,
212 StringPiece value) {
213 value_.assign(value.data(), value.length());
214 }
215
SetNoArena(const std::string *,const std::string & value)216 inline void InlinedStringField::SetNoArena(const std::string* /*default_value*/,
217 const std::string& value) {
218 value_.assign(value);
219 }
220
SetNoArena(const std::string *,std::string && value)221 inline void InlinedStringField::SetNoArena(const std::string* /*default_value*/,
222 std::string&& value) {
223 value_.assign(std::move(value));
224 }
225
Swap(InlinedStringField * from)226 inline void InlinedStringField::Swap(InlinedStringField* from) {
227 value_.swap(from->value_);
228 }
229
UnsafeMutablePointer()230 inline std::string* InlinedStringField::UnsafeMutablePointer() {
231 return &value_;
232 }
233
UnsafeSetDefault(const std::string * default_value)234 inline void InlinedStringField::UnsafeSetDefault(
235 const std::string* default_value) {
236 value_.assign(*default_value);
237 }
238
UnsafeArenaRelease(const std::string * default_value,Arena *)239 inline std::string* InlinedStringField::UnsafeArenaRelease(
240 const std::string* default_value, Arena* /*arena*/) {
241 return ReleaseNoArena(default_value);
242 }
243
UnsafeArenaSetAllocated(const std::string * default_value,std::string * value,Arena *)244 inline void InlinedStringField::UnsafeArenaSetAllocated(
245 const std::string* default_value, std::string* value, Arena* /*arena*/) {
246 if (value == NULL) {
247 value_.assign(*default_value);
248 } else {
249 value_.assign(*value);
250 }
251 }
252
253 } // namespace internal
254 } // namespace protobuf
255 } // namespace google
256
257 #include <google/protobuf/port_undef.inc>
258
259 #endif // GOOGLE_PROTOBUF_INLINED_STRING_FIELD_H__
260