arenastring.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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/stubs/logging.h>
  34. #include <google/protobuf/stubs/common.h>
  35. #include <google/protobuf/stubs/fastmem.h>
  36. #include <google/protobuf/arena.h>
  37. #include <google/protobuf/generated_message_util.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. struct LIBPROTOBUF_EXPORT ArenaStringPtr {
  49. inline void Set(const ::std::string* default_value,
  50. const ::std::string& value, ::google::protobuf::Arena* arena) {
  51. if (ptr_ == default_value) {
  52. CreateInstance(arena, &value);
  53. } else {
  54. *ptr_ = value;
  55. }
  56. }
  57. // Basic accessors.
  58. inline const ::std::string& Get(const ::std::string* default_value) const {
  59. return *ptr_;
  60. }
  61. inline ::std::string* Mutable(const ::std::string* default_value,
  62. ::google::protobuf::Arena* arena) {
  63. if (ptr_ == default_value) {
  64. CreateInstance(arena, default_value);
  65. }
  66. return ptr_;
  67. }
  68. // Release returns a ::std::string* instance that is heap-allocated and is not
  69. // Own()'d by any arena. If the field was not set, it returns NULL. The caller
  70. // retains ownership. Clears this field back to NULL state. Used to implement
  71. // release_<field>() methods on generated classes.
  72. inline ::std::string* Release(const ::std::string* default_value,
  73. ::google::protobuf::Arena* arena) {
  74. if (ptr_ == default_value) {
  75. return NULL;
  76. }
  77. ::std::string* released = NULL;
  78. if (arena != NULL) {
  79. // ptr_ is owned by the arena -- we need to return a copy.
  80. released = new ::std::string(*ptr_);
  81. } else {
  82. released = ptr_;
  83. }
  84. ptr_ = const_cast< ::std::string* >(default_value);
  85. return released;
  86. }
  87. // UnsafeArenaRelease returns a ::std::string*, but it may be arena-owned (i.e.
  88. // have its destructor already registered) if arena != NULL. If the field was
  89. // not set, this returns NULL. This method clears this field back to NULL
  90. // state. Used to implement unsafe_arena_release_<field>() methods on
  91. // generated classes.
  92. inline ::std::string* UnsafeArenaRelease(const ::std::string* default_value,
  93. ::google::protobuf::Arena* arena) {
  94. if (ptr_ == default_value) {
  95. return NULL;
  96. }
  97. ::std::string* released = ptr_;
  98. ptr_ = const_cast< ::std::string* >(default_value);
  99. return released;
  100. }
  101. // Takes a string that is heap-allocated, and takes ownership. The string's
  102. // destructor is registered with the arena. Used to implement
  103. // set_allocated_<field> in generated classes.
  104. inline void SetAllocated(const ::std::string* default_value,
  105. ::std::string* value, ::google::protobuf::Arena* arena) {
  106. if (arena == NULL && ptr_ != default_value) {
  107. Destroy(default_value, arena);
  108. }
  109. if (value != NULL) {
  110. ptr_ = value;
  111. if (arena != NULL) {
  112. arena->Own(value);
  113. }
  114. } else {
  115. ptr_ = const_cast< ::std::string* >(default_value);
  116. }
  117. }
  118. // Takes a string that has lifetime equal to the arena's lifetime. The arena
  119. // must be non-null. It is safe only to pass this method a value returned by
  120. // UnsafeArenaRelease() on another field of a message in the same arena. Used
  121. // to implement unsafe_arena_set_allocated_<field> in generated classes.
  122. inline void UnsafeArenaSetAllocated(const ::std::string* default_value,
  123. ::std::string* value, ::google::protobuf::Arena* arena) {
  124. if (value != NULL) {
  125. ptr_ = value;
  126. } else {
  127. ptr_ = const_cast< ::std::string* >(default_value);
  128. }
  129. }
  130. // Swaps internal pointers. Arena-safety semantics: this is guarded by the
  131. // logic in Swap()/UnsafeArenaSwap() at the message level, so this method is
  132. // 'unsafe' if called directly.
  133. GOOGLE_ATTRIBUTE_ALWAYS_INLINE void Swap(ArenaStringPtr* other) {
  134. std::swap(ptr_, other->ptr_);
  135. }
  136. // Frees storage (if not on an arena) and sets field to default value.
  137. inline void Destroy(const ::std::string* default_value,
  138. ::google::protobuf::Arena* arena) {
  139. if (arena == NULL && ptr_ != default_value) {
  140. delete ptr_;
  141. }
  142. ptr_ = const_cast< ::std::string* >(default_value);
  143. }
  144. // Clears content, but keeps allocated string if arena != NULL, to avoid the
  145. // overhead of heap operations. After this returns, the content (as seen by
  146. // the user) will always be the empty string. Assumes that |default_value|
  147. // is an empty string.
  148. inline void ClearToEmpty(const ::std::string* default_value,
  149. ::google::protobuf::Arena* arena) {
  150. if (ptr_ == default_value) {
  151. // Already set to default (which is empty) -- do nothing.
  152. } else {
  153. ptr_->clear();
  154. }
  155. }
  156. // Clears content, but keeps allocated string if arena != NULL, to avoid the
  157. // overhead of heap operations. After this returns, the content (as seen by
  158. // the user) will always be equal to |default_value|.
  159. inline void ClearToDefault(const ::std::string* default_value,
  160. ::google::protobuf::Arena* arena) {
  161. if (ptr_ == default_value) {
  162. // Already set to default -- do nothing.
  163. } else {
  164. // Have another allocated string -- rather than throwing this away and
  165. // resetting ptr_ to the canonical default string instance, we just reuse
  166. // this instance.
  167. *ptr_ = *default_value;
  168. }
  169. }
  170. // Called from generated code / reflection runtime only. Resets value to point
  171. // to a default string pointer, with the semantics that this ArenaStringPtr
  172. // does not own the pointed-to memory. Disregards initial value of ptr_ (so
  173. // this is the *ONLY* safe method to call after construction or when
  174. // reinitializing after becoming the active field in a oneof union).
  175. inline void UnsafeSetDefault(const ::std::string* default_value) {
  176. // Casting away 'const' is safe here: accessors ensure that ptr_ is only
  177. // returned as a const if it is equal to default_value.
  178. ptr_ = const_cast< ::std::string* >(default_value);
  179. }
  180. // The 'NoArena' variants of methods below assume arena == NULL and are
  181. // optimized to provide very little overhead relative to a raw string pointer
  182. // (while still being in-memory compatible with other code that assumes
  183. // ArenaStringPtr). Note the invariant that a class instance that has only
  184. // ever been mutated by NoArena methods must *only* be in the String state
  185. // (i.e., tag bits are not used), *NEVER* ArenaString. This allows all
  186. // tagged-pointer manipulations to be avoided.
  187. inline void SetNoArena(const ::std::string* default_value,
  188. const ::std::string& value) {
  189. if (ptr_ == default_value) {
  190. CreateInstanceNoArena(&value);
  191. } else {
  192. *ptr_ = value;
  193. }
  194. }
  195. void AssignWithDefault(const ::std::string* default_value, ArenaStringPtr value);
  196. inline const ::std::string& GetNoArena(const ::std::string* default_value) const {
  197. return *ptr_;
  198. }
  199. inline ::std::string* MutableNoArena(const ::std::string* default_value) {
  200. if (ptr_ == default_value) {
  201. CreateInstanceNoArena(default_value);
  202. }
  203. return ptr_;
  204. }
  205. inline ::std::string* ReleaseNoArena(const ::std::string* default_value) {
  206. if (ptr_ == default_value) {
  207. return NULL;
  208. } else {
  209. ::std::string* released = ptr_;
  210. ptr_ = const_cast< ::std::string* >(default_value);
  211. return released;
  212. }
  213. }
  214. inline void SetAllocatedNoArena(const ::std::string* default_value,
  215. ::std::string* value) {
  216. if (ptr_ != default_value) {
  217. delete ptr_;
  218. }
  219. if (value != NULL) {
  220. ptr_ = value;
  221. } else {
  222. ptr_ = const_cast< ::std::string* >(default_value);
  223. }
  224. }
  225. inline void DestroyNoArena(const ::std::string* default_value) {
  226. if (ptr_ != default_value) {
  227. delete ptr_;
  228. }
  229. ptr_ = NULL;
  230. }
  231. inline void ClearToEmptyNoArena(const ::std::string* default_value) {
  232. if (ptr_ == default_value) {
  233. // Nothing: already equal to default (which is the empty string).
  234. } else {
  235. ptr_->clear();
  236. }
  237. }
  238. inline void ClearToDefaultNoArena(const ::std::string* default_value) {
  239. if (ptr_ == default_value) {
  240. // Nothing: already set to default.
  241. } else {
  242. // Reuse existing allocated instance.
  243. *ptr_ = *default_value;
  244. }
  245. }
  246. // Internal accessor used only at parse time to provide direct access to the
  247. // raw pointer from the shared parse routine (in the non-arenas case). The
  248. // parse routine does the string allocation in order to save code size in the
  249. // generated parsing code.
  250. inline ::std::string** UnsafeRawStringPointer() {
  251. return &ptr_;
  252. }
  253. private:
  254. ::std::string* ptr_;
  255. GOOGLE_ATTRIBUTE_NOINLINE void CreateInstance(::google::protobuf::Arena* arena,
  256. const ::std::string* initial_value) {
  257. // Assumes ptr_ is not NULL.
  258. if (initial_value != NULL) {
  259. ptr_ = new ::std::string(*initial_value);
  260. } else {
  261. ptr_ = new ::std::string();
  262. }
  263. if (arena != NULL) {
  264. arena->Own(ptr_);
  265. }
  266. }
  267. GOOGLE_ATTRIBUTE_NOINLINE void CreateInstanceNoArena(const ::std::string* initial_value) {
  268. if (initial_value != NULL) {
  269. ptr_ = new ::std::string(*initial_value);
  270. } else {
  271. ptr_ = new ::std::string();
  272. }
  273. }
  274. };
  275. } // namespace internal
  276. } // namespace protobuf
  277. } // namespace google
  278. #endif // GOOGLE_PROTOBUF_ARENASTRING_H__