arenastring.h 14 KB

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