arenastring.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #ifndef GOOGLE_PROTOBUF_ARENASTRING_H__
  32. #define GOOGLE_PROTOBUF_ARENASTRING_H__
  33. #include <string>
  34. #include <type_traits>
  35. #include <utility>
  36. #include <google/protobuf/stubs/logging.h>
  37. #include <google/protobuf/stubs/common.h>
  38. #include <google/protobuf/stubs/fastmem.h>
  39. #include <google/protobuf/arena.h>
  40. #include <google/protobuf/port.h>
  41. #include <google/protobuf/port_def.inc>
  42. #ifdef SWIG
  43. #error "You cannot SWIG proto headers"
  44. #endif
  45. // This is the implementation of arena string fields written for the open-source
  46. // release. The ArenaStringPtr struct below is an internal implementation class
  47. // and *should not be used* by user code. It is used to collect string
  48. // operations together into one place and abstract away the underlying
  49. // string-field pointer representation, so that (for example) an alternate
  50. // implementation that knew more about ::std::string's internals could integrate
  51. // more closely with the arena allocator.
  52. namespace google {
  53. namespace protobuf {
  54. namespace internal {
  55. template <typename T>
  56. class TaggedPtr {
  57. public:
  58. void Set(T* p) { ptr_ = reinterpret_cast<uintptr_t>(p); }
  59. T* Get() const { return reinterpret_cast<T*>(ptr_); }
  60. bool IsNull() { return ptr_ == 0; }
  61. private:
  62. uintptr_t ptr_;
  63. };
  64. struct PROTOBUF_EXPORT ArenaStringPtr {
  65. inline void Set(const ::std::string* default_value,
  66. const ::std::string& value, Arena* arena) {
  67. if (ptr_ == default_value) {
  68. CreateInstance(arena, &value);
  69. } else {
  70. *ptr_ = value;
  71. }
  72. }
  73. inline void SetLite(const ::std::string* default_value,
  74. const ::std::string& value, Arena* arena) {
  75. Set(default_value, value, arena);
  76. }
  77. // Basic accessors.
  78. inline const ::std::string& Get() const { return *ptr_; }
  79. inline ::std::string* Mutable(const ::std::string* default_value,
  80. Arena* arena) {
  81. if (ptr_ == default_value) {
  82. CreateInstance(arena, default_value);
  83. }
  84. return ptr_;
  85. }
  86. // Release returns a ::std::string* instance that is heap-allocated and is not
  87. // Own()'d by any arena. If the field was not set, it returns NULL. The caller
  88. // retains ownership. Clears this field back to NULL state. Used to implement
  89. // release_<field>() methods on generated classes.
  90. inline ::std::string* Release(const ::std::string* default_value,
  91. Arena* arena) {
  92. if (ptr_ == default_value) {
  93. return NULL;
  94. }
  95. return ReleaseNonDefault(default_value, arena);
  96. }
  97. // Similar to Release, but ptr_ cannot be the default_value.
  98. inline ::std::string* ReleaseNonDefault(const ::std::string* default_value,
  99. Arena* arena) {
  100. GOOGLE_DCHECK(!IsDefault(default_value));
  101. ::std::string* released = NULL;
  102. if (arena != NULL) {
  103. // ptr_ is owned by the arena.
  104. released = new ::std::string;
  105. released->swap(*ptr_);
  106. } else {
  107. released = ptr_;
  108. }
  109. ptr_ = const_cast< ::std::string*>(default_value);
  110. return released;
  111. }
  112. // UnsafeArenaRelease returns a ::std::string*, but it may be arena-owned
  113. // (i.e. have its destructor already registered) if arena != NULL. If the
  114. // field was not set, this returns NULL. This method clears this field back to
  115. // NULL state. Used to implement unsafe_arena_release_<field>() methods on
  116. // generated classes.
  117. inline ::std::string* UnsafeArenaRelease(const ::std::string* default_value,
  118. Arena* /* arena */) {
  119. if (ptr_ == default_value) {
  120. return NULL;
  121. }
  122. ::std::string* released = ptr_;
  123. ptr_ = const_cast< ::std::string*>(default_value);
  124. return released;
  125. }
  126. // Takes a string that is heap-allocated, and takes ownership. The string's
  127. // destructor is registered with the arena. Used to implement
  128. // set_allocated_<field> in generated classes.
  129. inline void SetAllocated(const ::std::string* default_value,
  130. ::std::string* value, Arena* arena) {
  131. if (arena == NULL && ptr_ != default_value) {
  132. Destroy(default_value, arena);
  133. }
  134. if (value != NULL) {
  135. ptr_ = value;
  136. if (arena != NULL) {
  137. arena->Own(value);
  138. }
  139. } else {
  140. ptr_ = const_cast< ::std::string*>(default_value);
  141. }
  142. }
  143. // Takes a string that has lifetime equal to the arena's lifetime. The arena
  144. // must be non-null. It is safe only to pass this method a value returned by
  145. // UnsafeArenaRelease() on another field of a message in the same arena. Used
  146. // to implement unsafe_arena_set_allocated_<field> in generated classes.
  147. inline void UnsafeArenaSetAllocated(const ::std::string* default_value,
  148. ::std::string* value,
  149. Arena* /* arena */) {
  150. if (value != NULL) {
  151. ptr_ = value;
  152. } else {
  153. ptr_ = const_cast< ::std::string*>(default_value);
  154. }
  155. }
  156. // Swaps internal pointers. Arena-safety semantics: this is guarded by the
  157. // logic in Swap()/UnsafeArenaSwap() at the message level, so this method is
  158. // 'unsafe' if called directly.
  159. PROTOBUF_ALWAYS_INLINE void Swap(ArenaStringPtr* other) {
  160. std::swap(ptr_, other->ptr_);
  161. }
  162. PROTOBUF_ALWAYS_INLINE void Swap(ArenaStringPtr* other,
  163. const ::std::string* default_value,
  164. Arena* arena) {
  165. #ifndef NDEBUG
  166. // For debug builds, we swap the contents of the string, rather than the
  167. // string instances themselves. This invalidates previously taken const
  168. // references that are (per our documentation) invalidated by calling Swap()
  169. // on the message.
  170. //
  171. // If both strings are the default_value, swapping is uninteresting.
  172. // Otherwise, we use ArenaStringPtr::Mutable() to access the string, to
  173. // ensure that we do not try to mutate default_value itself.
  174. if (IsDefault(default_value) && other->IsDefault(default_value)) {
  175. return;
  176. }
  177. ::std::string* this_ptr = Mutable(default_value, arena);
  178. ::std::string* other_ptr = other->Mutable(default_value, arena);
  179. this_ptr->swap(*other_ptr);
  180. #else
  181. std::swap(ptr_, other->ptr_);
  182. (void)default_value;
  183. (void)arena;
  184. #endif
  185. }
  186. // Frees storage (if not on an arena).
  187. inline void Destroy(const ::std::string* default_value, Arena* arena) {
  188. if (arena == NULL && ptr_ != default_value) {
  189. delete ptr_;
  190. }
  191. }
  192. // Clears content, but keeps allocated string if arena != NULL, to avoid the
  193. // overhead of heap operations. After this returns, the content (as seen by
  194. // the user) will always be the empty string. Assumes that |default_value|
  195. // is an empty string.
  196. inline void ClearToEmpty(const ::std::string* default_value,
  197. Arena* /* arena */) {
  198. if (ptr_ == default_value) {
  199. // Already set to default (which is empty) -- do nothing.
  200. } else {
  201. ptr_->clear();
  202. }
  203. }
  204. // Clears content, assuming that the current value is not the empty string
  205. // default.
  206. inline void ClearNonDefaultToEmpty() { ptr_->clear(); }
  207. inline void ClearNonDefaultToEmptyNoArena() { ptr_->clear(); }
  208. // Clears content, but keeps allocated string if arena != NULL, to avoid the
  209. // overhead of heap operations. After this returns, the content (as seen by
  210. // the user) will always be equal to |default_value|.
  211. inline void ClearToDefault(const ::std::string* default_value,
  212. Arena* /* arena */) {
  213. if (ptr_ == default_value) {
  214. // Already set to default -- do nothing.
  215. } else {
  216. // Have another allocated string -- rather than throwing this away and
  217. // resetting ptr_ to the canonical default string instance, we just reuse
  218. // this instance.
  219. *ptr_ = *default_value;
  220. }
  221. }
  222. // Called from generated code / reflection runtime only. Resets value to point
  223. // to a default string pointer, with the semantics that this ArenaStringPtr
  224. // does not own the pointed-to memory. Disregards initial value of ptr_ (so
  225. // this is the *ONLY* safe method to call after construction or when
  226. // reinitializing after becoming the active field in a oneof union).
  227. inline void UnsafeSetDefault(const ::std::string* default_value) {
  228. // Casting away 'const' is safe here: accessors ensure that ptr_ is only
  229. // returned as a const if it is equal to default_value.
  230. ptr_ = const_cast< ::std::string*>(default_value);
  231. }
  232. // The 'NoArena' variants of methods below assume arena == NULL and are
  233. // optimized to provide very little overhead relative to a raw string pointer
  234. // (while still being in-memory compatible with other code that assumes
  235. // ArenaStringPtr). Note the invariant that a class instance that has only
  236. // ever been mutated by NoArena methods must *only* be in the String state
  237. // (i.e., tag bits are not used), *NEVER* ArenaString. This allows all
  238. // tagged-pointer manipulations to be avoided.
  239. inline void SetNoArena(const ::std::string* default_value,
  240. const ::std::string& value) {
  241. if (ptr_ == default_value) {
  242. CreateInstanceNoArena(&value);
  243. } else {
  244. *ptr_ = value;
  245. }
  246. }
  247. void SetNoArena(const ::std::string* default_value, ::std::string&& value) {
  248. if (IsDefault(default_value)) {
  249. ptr_ = new ::std::string(std::move(value));
  250. } else {
  251. *ptr_ = std::move(value);
  252. }
  253. }
  254. void AssignWithDefault(const ::std::string* default_value,
  255. ArenaStringPtr value);
  256. inline const ::std::string& GetNoArena() const { return *ptr_; }
  257. inline ::std::string* MutableNoArena(const ::std::string* default_value) {
  258. if (ptr_ == default_value) {
  259. CreateInstanceNoArena(default_value);
  260. }
  261. return ptr_;
  262. }
  263. inline ::std::string* ReleaseNoArena(const ::std::string* default_value) {
  264. if (ptr_ == default_value) {
  265. return NULL;
  266. } else {
  267. return ReleaseNonDefaultNoArena(default_value);
  268. }
  269. }
  270. inline ::std::string* ReleaseNonDefaultNoArena(
  271. const ::std::string* default_value) {
  272. GOOGLE_DCHECK(!IsDefault(default_value));
  273. ::std::string* released = ptr_;
  274. ptr_ = const_cast< ::std::string*>(default_value);
  275. return released;
  276. }
  277. inline void SetAllocatedNoArena(const ::std::string* default_value,
  278. ::std::string* value) {
  279. if (ptr_ != default_value) {
  280. delete ptr_;
  281. }
  282. if (value != NULL) {
  283. ptr_ = value;
  284. } else {
  285. ptr_ = const_cast< ::std::string*>(default_value);
  286. }
  287. }
  288. inline void DestroyNoArena(const ::std::string* default_value) {
  289. if (ptr_ != default_value) {
  290. delete ptr_;
  291. }
  292. }
  293. inline void ClearToEmptyNoArena(const ::std::string* default_value) {
  294. if (ptr_ == default_value) {
  295. // Nothing: already equal to default (which is the empty string).
  296. } else {
  297. ptr_->clear();
  298. }
  299. }
  300. inline void ClearToDefaultNoArena(const ::std::string* default_value) {
  301. if (ptr_ == default_value) {
  302. // Nothing: already set to default.
  303. } else {
  304. // Reuse existing allocated instance.
  305. *ptr_ = *default_value;
  306. }
  307. }
  308. // Internal accessor used only at parse time to provide direct access to the
  309. // raw pointer from the shared parse routine (in the non-arenas case). The
  310. // parse routine does the string allocation in order to save code size in the
  311. // generated parsing code.
  312. inline ::std::string** UnsafeRawStringPointer() { return &ptr_; }
  313. inline bool IsDefault(const ::std::string* default_value) const {
  314. return ptr_ == default_value;
  315. }
  316. // Internal accessors!!!!
  317. void UnsafeSetTaggedPointer(TaggedPtr< ::std::string> value) {
  318. ptr_ = value.Get();
  319. }
  320. // Generated code only! An optimization, in certain cases the generated
  321. // code is certain we can obtain a string with no default checks and
  322. // tag tests.
  323. ::std::string* UnsafeMutablePointer() { return ptr_; }
  324. private:
  325. ::std::string* ptr_;
  326. PROTOBUF_NOINLINE
  327. void CreateInstance(Arena* arena, 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. PROTOBUF_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(
  343. const ::std::string* default_value, 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. #include <google/protobuf/port_undef.inc>
  355. #endif // GOOGLE_PROTOBUF_ARENASTRING_H__
  356. #else
  357. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  358. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)