| 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_ARENASTRING_H__ |
| 32 | #define GOOGLE_PROTOBUF_ARENASTRING_H__ |
| 33 | |
| 34 | #include <string> |
| 35 | #include <type_traits> |
| 36 | #include <utility> |
| 37 | |
| 38 | #include <google/protobuf/stubs/logging.h> |
| 39 | #include <google/protobuf/stubs/common.h> |
| 40 | #include <google/protobuf/stubs/fastmem.h> |
| 41 | #include <google/protobuf/arena.h> |
| 42 | #include <google/protobuf/port.h> |
| 43 | |
| 44 | #include <google/protobuf/port_def.inc> |
| 45 | |
| 46 | #ifdef SWIG |
| 47 | #error "You cannot SWIG proto headers" |
| 48 | #endif |
| 49 | |
| 50 | |
| 51 | // This is the implementation of arena string fields written for the open-source |
| 52 | // release. The ArenaStringPtr struct below is an internal implementation class |
| 53 | // and *should not be used* by user code. It is used to collect string |
| 54 | // operations together into one place and abstract away the underlying |
| 55 | // string-field pointer representation, so that (for example) an alternate |
| 56 | // implementation that knew more about ::std::string's internals could integrate |
| 57 | // more closely with the arena allocator. |
| 58 | |
| 59 | namespace google { |
| 60 | namespace protobuf { |
| 61 | namespace internal { |
| 62 | |
| 63 | template <typename T> |
| 64 | class TaggedPtr { |
| 65 | public: |
| 66 | void Set(T* p) { ptr_ = reinterpret_cast<uintptr_t>(p); } |
| 67 | T* Get() const { return reinterpret_cast<T*>(ptr_); } |
| 68 | |
| 69 | bool IsNull() { return ptr_ == 0; } |
| 70 | |
| 71 | private: |
| 72 | uintptr_t ptr_; |
| 73 | }; |
| 74 | |
| 75 | struct PROTOBUF_EXPORT ArenaStringPtr { |
| 76 | inline void Set(const ::std::string* default_value, |
| 77 | const ::std::string& value, Arena* arena) { |
| 78 | if (ptr_ == default_value) { |
| 79 | CreateInstance(arena, initial_value: &value); |
| 80 | } else { |
| 81 | *ptr_ = value; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | inline void SetLite(const ::std::string* default_value, |
| 86 | const ::std::string& value, Arena* arena) { |
| 87 | Set(default_value, value, arena); |
| 88 | } |
| 89 | |
| 90 | // Basic accessors. |
| 91 | inline const ::std::string& Get() const { return *ptr_; } |
| 92 | |
| 93 | inline ::std::string* Mutable(const ::std::string* default_value, |
| 94 | Arena* arena) { |
| 95 | if (ptr_ == default_value) { |
| 96 | CreateInstance(arena, initial_value: default_value); |
| 97 | } |
| 98 | return ptr_; |
| 99 | } |
| 100 | |
| 101 | // Release returns a ::std::string* instance that is heap-allocated and is not |
| 102 | // Own()'d by any arena. If the field was not set, it returns NULL. The caller |
| 103 | // retains ownership. Clears this field back to NULL state. Used to implement |
| 104 | // release_<field>() methods on generated classes. |
| 105 | inline ::std::string* Release(const ::std::string* default_value, |
| 106 | Arena* arena) { |
| 107 | if (ptr_ == default_value) { |
| 108 | return NULL; |
| 109 | } |
| 110 | return ReleaseNonDefault(default_value, arena); |
| 111 | } |
| 112 | |
| 113 | // Similar to Release, but ptr_ cannot be the default_value. |
| 114 | inline ::std::string* ReleaseNonDefault(const ::std::string* default_value, |
| 115 | Arena* arena) { |
| 116 | GOOGLE_DCHECK(!IsDefault(default_value)); |
| 117 | ::std::string* released = NULL; |
| 118 | if (arena != NULL) { |
| 119 | // ptr_ is owned by the arena. |
| 120 | released = new ::std::string; |
| 121 | released->swap(s&: *ptr_); |
| 122 | } else { |
| 123 | released = ptr_; |
| 124 | } |
| 125 | ptr_ = const_cast< ::std::string*>(default_value); |
| 126 | return released; |
| 127 | } |
| 128 | |
| 129 | // UnsafeArenaRelease returns a ::std::string*, but it may be arena-owned |
| 130 | // (i.e. have its destructor already registered) if arena != NULL. If the |
| 131 | // field was not set, this returns NULL. This method clears this field back to |
| 132 | // NULL state. Used to implement unsafe_arena_release_<field>() methods on |
| 133 | // generated classes. |
| 134 | inline ::std::string* UnsafeArenaRelease(const ::std::string* default_value, |
| 135 | Arena* /* arena */) { |
| 136 | if (ptr_ == default_value) { |
| 137 | return NULL; |
| 138 | } |
| 139 | ::std::string* released = ptr_; |
| 140 | ptr_ = const_cast< ::std::string*>(default_value); |
| 141 | return released; |
| 142 | } |
| 143 | |
| 144 | // Takes a string that is heap-allocated, and takes ownership. The string's |
| 145 | // destructor is registered with the arena. Used to implement |
| 146 | // set_allocated_<field> in generated classes. |
| 147 | inline void SetAllocated(const ::std::string* default_value, |
| 148 | ::std::string* value, Arena* arena) { |
| 149 | if (arena == NULL && ptr_ != default_value) { |
| 150 | Destroy(default_value, arena); |
| 151 | } |
| 152 | if (value != NULL) { |
| 153 | ptr_ = value; |
| 154 | if (arena != NULL) { |
| 155 | arena->Own(object: value); |
| 156 | } |
| 157 | } else { |
| 158 | ptr_ = const_cast< ::std::string*>(default_value); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // Takes a string that has lifetime equal to the arena's lifetime. The arena |
| 163 | // must be non-null. It is safe only to pass this method a value returned by |
| 164 | // UnsafeArenaRelease() on another field of a message in the same arena. Used |
| 165 | // to implement unsafe_arena_set_allocated_<field> in generated classes. |
| 166 | inline void UnsafeArenaSetAllocated(const ::std::string* default_value, |
| 167 | ::std::string* value, |
| 168 | Arena* /* arena */) { |
| 169 | if (value != NULL) { |
| 170 | ptr_ = value; |
| 171 | } else { |
| 172 | ptr_ = const_cast< ::std::string*>(default_value); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // Swaps internal pointers. Arena-safety semantics: this is guarded by the |
| 177 | // logic in Swap()/UnsafeArenaSwap() at the message level, so this method is |
| 178 | // 'unsafe' if called directly. |
| 179 | PROTOBUF_ALWAYS_INLINE void Swap(ArenaStringPtr* other) { |
| 180 | std::swap(a&: ptr_, b&: other->ptr_); |
| 181 | } |
| 182 | PROTOBUF_ALWAYS_INLINE void Swap(ArenaStringPtr* other, |
| 183 | const ::std::string* default_value, |
| 184 | Arena* arena) { |
| 185 | #ifndef NDEBUG |
| 186 | // For debug builds, we swap the contents of the string, rather than the |
| 187 | // string instances themselves. This invalidates previously taken const |
| 188 | // references that are (per our documentation) invalidated by calling Swap() |
| 189 | // on the message. |
| 190 | // |
| 191 | // If both strings are the default_value, swapping is uninteresting. |
| 192 | // Otherwise, we use ArenaStringPtr::Mutable() to access the string, to |
| 193 | // ensure that we do not try to mutate default_value itself. |
| 194 | if (IsDefault(default_value) && other->IsDefault(default_value)) { |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | ::std::string* this_ptr = Mutable(default_value, arena); |
| 199 | ::std::string* other_ptr = other->Mutable(default_value, arena); |
| 200 | |
| 201 | this_ptr->swap(s&: *other_ptr); |
| 202 | #else |
| 203 | std::swap(ptr_, other->ptr_); |
| 204 | (void)default_value; |
| 205 | (void)arena; |
| 206 | #endif |
| 207 | } |
| 208 | |
| 209 | // Frees storage (if not on an arena). |
| 210 | inline void Destroy(const ::std::string* default_value, Arena* arena) { |
| 211 | if (arena == NULL && ptr_ != default_value) { |
| 212 | delete ptr_; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // Clears content, but keeps allocated string if arena != NULL, to avoid the |
| 217 | // overhead of heap operations. After this returns, the content (as seen by |
| 218 | // the user) will always be the empty string. Assumes that |default_value| |
| 219 | // is an empty string. |
| 220 | inline void ClearToEmpty(const ::std::string* default_value, |
| 221 | Arena* /* arena */) { |
| 222 | if (ptr_ == default_value) { |
| 223 | // Already set to default (which is empty) -- do nothing. |
| 224 | } else { |
| 225 | ptr_->clear(); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // Clears content, assuming that the current value is not the empty string |
| 230 | // default. |
| 231 | inline void ClearNonDefaultToEmpty() { ptr_->clear(); } |
| 232 | inline void ClearNonDefaultToEmptyNoArena() { ptr_->clear(); } |
| 233 | |
| 234 | // Clears content, but keeps allocated string if arena != NULL, to avoid the |
| 235 | // overhead of heap operations. After this returns, the content (as seen by |
| 236 | // the user) will always be equal to |default_value|. |
| 237 | inline void ClearToDefault(const ::std::string* default_value, |
| 238 | Arena* /* arena */) { |
| 239 | if (ptr_ == default_value) { |
| 240 | // Already set to default -- do nothing. |
| 241 | } else { |
| 242 | // Have another allocated string -- rather than throwing this away and |
| 243 | // resetting ptr_ to the canonical default string instance, we just reuse |
| 244 | // this instance. |
| 245 | *ptr_ = *default_value; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | // Called from generated code / reflection runtime only. Resets value to point |
| 250 | // to a default string pointer, with the semantics that this ArenaStringPtr |
| 251 | // does not own the pointed-to memory. Disregards initial value of ptr_ (so |
| 252 | // this is the *ONLY* safe method to call after construction or when |
| 253 | // reinitializing after becoming the active field in a oneof union). |
| 254 | inline void UnsafeSetDefault(const ::std::string* default_value) { |
| 255 | // Casting away 'const' is safe here: accessors ensure that ptr_ is only |
| 256 | // returned as a const if it is equal to default_value. |
| 257 | ptr_ = const_cast< ::std::string*>(default_value); |
| 258 | } |
| 259 | |
| 260 | // The 'NoArena' variants of methods below assume arena == NULL and are |
| 261 | // optimized to provide very little overhead relative to a raw string pointer |
| 262 | // (while still being in-memory compatible with other code that assumes |
| 263 | // ArenaStringPtr). Note the invariant that a class instance that has only |
| 264 | // ever been mutated by NoArena methods must *only* be in the String state |
| 265 | // (i.e., tag bits are not used), *NEVER* ArenaString. This allows all |
| 266 | // tagged-pointer manipulations to be avoided. |
| 267 | inline void SetNoArena(const ::std::string* default_value, |
| 268 | const ::std::string& value) { |
| 269 | if (ptr_ == default_value) { |
| 270 | CreateInstanceNoArena(initial_value: &value); |
| 271 | } else { |
| 272 | *ptr_ = value; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | void SetNoArena(const ::std::string* default_value, ::std::string&& value) { |
| 277 | if (IsDefault(default_value)) { |
| 278 | ptr_ = new ::std::string(std::move(value)); |
| 279 | } else { |
| 280 | *ptr_ = std::move(value); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | void AssignWithDefault(const ::std::string* default_value, |
| 285 | ArenaStringPtr value); |
| 286 | |
| 287 | inline const ::std::string& GetNoArena() const { return *ptr_; } |
| 288 | |
| 289 | inline ::std::string* MutableNoArena(const ::std::string* default_value) { |
| 290 | if (ptr_ == default_value) { |
| 291 | CreateInstanceNoArena(initial_value: default_value); |
| 292 | } |
| 293 | return ptr_; |
| 294 | } |
| 295 | |
| 296 | inline ::std::string* ReleaseNoArena(const ::std::string* default_value) { |
| 297 | if (ptr_ == default_value) { |
| 298 | return NULL; |
| 299 | } else { |
| 300 | return ReleaseNonDefaultNoArena(default_value); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | inline ::std::string* ReleaseNonDefaultNoArena( |
| 305 | const ::std::string* default_value) { |
| 306 | GOOGLE_DCHECK(!IsDefault(default_value)); |
| 307 | ::std::string* released = ptr_; |
| 308 | ptr_ = const_cast< ::std::string*>(default_value); |
| 309 | return released; |
| 310 | } |
| 311 | |
| 312 | inline void SetAllocatedNoArena(const ::std::string* default_value, |
| 313 | ::std::string* value) { |
| 314 | if (ptr_ != default_value) { |
| 315 | delete ptr_; |
| 316 | } |
| 317 | if (value != NULL) { |
| 318 | ptr_ = value; |
| 319 | } else { |
| 320 | ptr_ = const_cast< ::std::string*>(default_value); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | inline void DestroyNoArena(const ::std::string* default_value) { |
| 325 | if (ptr_ != default_value) { |
| 326 | delete ptr_; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | inline void ClearToEmptyNoArena(const ::std::string* default_value) { |
| 331 | if (ptr_ == default_value) { |
| 332 | // Nothing: already equal to default (which is the empty string). |
| 333 | } else { |
| 334 | ptr_->clear(); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | inline void ClearToDefaultNoArena(const ::std::string* default_value) { |
| 339 | if (ptr_ == default_value) { |
| 340 | // Nothing: already set to default. |
| 341 | } else { |
| 342 | // Reuse existing allocated instance. |
| 343 | *ptr_ = *default_value; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // Internal accessor used only at parse time to provide direct access to the |
| 348 | // raw pointer from the shared parse routine (in the non-arenas case). The |
| 349 | // parse routine does the string allocation in order to save code size in the |
| 350 | // generated parsing code. |
| 351 | inline ::std::string** UnsafeRawStringPointer() { return &ptr_; } |
| 352 | |
| 353 | inline bool IsDefault(const ::std::string* default_value) const { |
| 354 | return ptr_ == default_value; |
| 355 | } |
| 356 | |
| 357 | // Internal accessors!!!! |
| 358 | void UnsafeSetTaggedPointer(TaggedPtr< ::std::string> value) { |
| 359 | ptr_ = value.Get(); |
| 360 | } |
| 361 | // Generated code only! An optimization, in certain cases the generated |
| 362 | // code is certain we can obtain a string with no default checks and |
| 363 | // tag tests. |
| 364 | ::std::string* UnsafeMutablePointer() { return ptr_; } |
| 365 | |
| 366 | private: |
| 367 | ::std::string* ptr_; |
| 368 | |
| 369 | PROTOBUF_NOINLINE |
| 370 | void CreateInstance(Arena* arena, const ::std::string* initial_value) { |
| 371 | GOOGLE_DCHECK(initial_value != NULL); |
| 372 | // uses "new ::std::string" when arena is nullptr |
| 373 | ptr_ = Arena::Create< ::std::string>(arena, args: *initial_value); |
| 374 | } |
| 375 | PROTOBUF_NOINLINE |
| 376 | void CreateInstanceNoArena(const ::std::string* initial_value) { |
| 377 | GOOGLE_DCHECK(initial_value != NULL); |
| 378 | ptr_ = new ::std::string(*initial_value); |
| 379 | } |
| 380 | }; |
| 381 | |
| 382 | } // namespace internal |
| 383 | } // namespace protobuf |
| 384 | |
| 385 | namespace protobuf { |
| 386 | namespace internal { |
| 387 | |
| 388 | inline void ArenaStringPtr::AssignWithDefault( |
| 389 | const ::std::string* default_value, ArenaStringPtr value) { |
| 390 | const ::std::string* me = *UnsafeRawStringPointer(); |
| 391 | const ::std::string* other = *value.UnsafeRawStringPointer(); |
| 392 | // If the pointers are the same then do nothing. |
| 393 | if (me != other) { |
| 394 | SetNoArena(default_value, value: value.GetNoArena()); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | } // namespace internal |
| 399 | } // namespace protobuf |
| 400 | } // namespace google |
| 401 | |
| 402 | |
| 403 | #include <google/protobuf/port_undef.inc> |
| 404 | |
| 405 | #endif // GOOGLE_PROTOBUF_ARENASTRING_H__ |
| 406 | |