arenastring.h 14 KB

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