诸暨麻将添加redis
您不能選擇超過 %s 個話題 話題必須以字母或數字為開頭,可包含連接號 ('-') 且最長為 35 個字
 
 
 
 
 
 

405 行
15 KiB

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