inlined_string_field.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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_INLINED_STRING_FIELD_H__
  31. #define GOOGLE_PROTOBUF_INLINED_STRING_FIELD_H__
  32. #include <string>
  33. #include <google/protobuf/stubs/port.h>
  34. #include <google/protobuf/stubs/stringpiece.h>
  35. namespace google {
  36. namespace protobuf {
  37. class Arena;
  38. namespace internal {
  39. // InlinedStringField wraps a ::std::string instance and exposes an API similar to
  40. // ArenaStringPtr's wrapping of a ::std::string* instance. As ::std::string is never
  41. // allocated on the Arena, we expose only the *NoArena methods of
  42. // ArenaStringPtr.
  43. //
  44. // default_value parameters are taken for consistency with ArenaStringPtr, but
  45. // are not used for most methods. With inlining, these should be removed from
  46. // the generated binary.
  47. class LIBPROTOBUF_EXPORT InlinedStringField {
  48. public:
  49. InlinedStringField()
  50. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE;
  51. explicit InlinedStringField(const ::std::string& default_value);
  52. void AssignWithDefault(const ::std::string* default_value,
  53. const InlinedStringField& from)
  54. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE;
  55. void ClearToEmpty(const ::std::string* default_value, Arena* arena)
  56. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE {
  57. ClearToEmptyNoArena(default_value);
  58. }
  59. void ClearNonDefaultToEmpty() GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE {
  60. ClearNonDefaultToEmptyNoArena();
  61. }
  62. void ClearToEmptyNoArena(const ::std::string* default_value)
  63. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE {
  64. ClearNonDefaultToEmptyNoArena();
  65. }
  66. void ClearNonDefaultToEmptyNoArena()
  67. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE;
  68. void ClearToDefault(const ::std::string* default_value, Arena* arena)
  69. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE {
  70. ClearToDefaultNoArena(default_value);
  71. }
  72. void ClearToDefaultNoArena(const ::std::string* default_value)
  73. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE;
  74. void Destroy(const ::std::string* default_value, Arena* arena)
  75. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE {
  76. DestroyNoArena(default_value);
  77. }
  78. void DestroyNoArena(const ::std::string* default_value)
  79. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE;
  80. const ::std::string& Get() const GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE {
  81. return GetNoArena();
  82. }
  83. const ::std::string& GetNoArena() const GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE;
  84. ::std::string* Mutable(const ::std::string* default_value, Arena* arena)
  85. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE {
  86. return MutableNoArena(default_value);
  87. }
  88. ::std::string* MutableNoArena(const ::std::string* default_value)
  89. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE;
  90. ::std::string* Release(const ::std::string* default_value, Arena* arena) {
  91. return ReleaseNoArena(default_value);
  92. }
  93. ::std::string* ReleaseNonDefault(const ::std::string* default_value, Arena* arena) {
  94. return ReleaseNonDefaultNoArena(default_value);
  95. }
  96. ::std::string* ReleaseNoArena(const ::std::string* default_value) {
  97. return ReleaseNonDefaultNoArena(default_value);
  98. }
  99. ::std::string* ReleaseNonDefaultNoArena(const ::std::string* default_value);
  100. void Set(const ::std::string* default_value,
  101. StringPiece value,
  102. Arena* arena) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE {
  103. SetNoArena(default_value, value);
  104. }
  105. void SetLite(const ::std::string* default_value,
  106. StringPiece value,
  107. Arena* arena) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE {
  108. SetNoArena(default_value, value);
  109. }
  110. void SetNoArena(const ::std::string* default_value,
  111. StringPiece value) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE;
  112. void Set(const ::std::string* default_value,
  113. const ::std::string& value,
  114. Arena* arena) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE {
  115. SetNoArena(default_value, value);
  116. }
  117. void SetLite(const ::std::string* default_value,
  118. const ::std::string& value,
  119. Arena* arena) GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE {
  120. SetNoArena(default_value, value);
  121. }
  122. void SetNoArena(const ::std::string* default_value,
  123. const ::std::string& value)
  124. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE;
  125. #if LANG_CXX11
  126. void SetNoArena(const ::std::string* default_value,
  127. ::std::string&& value)
  128. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE;
  129. #endif
  130. void SetAllocated(const ::std::string* default_value,
  131. ::std::string* value,
  132. Arena* arena) {
  133. SetAllocatedNoArena(default_value, value);
  134. }
  135. void SetAllocatedNoArena(const ::std::string* default_value,
  136. ::std::string* value);
  137. void Swap(InlinedStringField* from)
  138. GOOGLE_PROTOBUF_ATTRIBUTE_ALWAYS_INLINE;
  139. ::std::string* UnsafeMutablePointer();
  140. void UnsafeSetDefault(const ::std::string* default_value);
  141. ::std::string* UnsafeArenaRelease(const ::std::string* default_value, Arena* arena);
  142. void UnsafeArenaSetAllocated(
  143. const ::std::string* default_value, ::std::string* value, Arena* arena);
  144. bool IsDefault(const ::std::string* default_value) {
  145. return false;
  146. }
  147. private:
  148. ::std::string value_;
  149. };
  150. inline InlinedStringField::InlinedStringField() {}
  151. inline InlinedStringField::InlinedStringField(const ::std::string& default_value) :
  152. value_(default_value) {}
  153. inline void InlinedStringField::AssignWithDefault(
  154. const ::std::string* default_value, const InlinedStringField& from) {
  155. value_ = from.value_;
  156. }
  157. inline const ::std::string& InlinedStringField::GetNoArena() const {
  158. return value_;
  159. }
  160. inline ::std::string* InlinedStringField::MutableNoArena(const ::std::string*) {
  161. return &value_;
  162. }
  163. inline void InlinedStringField::SetAllocatedNoArena(
  164. const ::std::string* default_value, ::std::string* value) {
  165. if (value == NULL) {
  166. value_.assign(*default_value);
  167. } else {
  168. #if LANG_CXX11
  169. value_.assign(std::move(*value));
  170. #else
  171. value_.swap(*value);
  172. #endif
  173. delete value;
  174. }
  175. }
  176. inline void InlinedStringField::DestroyNoArena(const ::std::string*) {
  177. // This is invoked from the generated message's ArenaDtor, which is used to
  178. // clean up objects not allocated on the Arena.
  179. this->~InlinedStringField();
  180. }
  181. inline void InlinedStringField::ClearNonDefaultToEmptyNoArena() {
  182. value_.clear();
  183. }
  184. inline void InlinedStringField::ClearToDefaultNoArena(
  185. const ::std::string* default_value) {
  186. value_.assign(*default_value);
  187. }
  188. inline ::std::string* InlinedStringField::ReleaseNonDefaultNoArena(
  189. const ::std::string* default_value) {
  190. ::std::string* released = new ::std::string(*default_value);
  191. value_.swap(*released);
  192. return released;
  193. }
  194. inline void InlinedStringField::SetNoArena(
  195. const ::std::string* default_value, StringPiece value) {
  196. value_.assign(value.data(), value.length());
  197. }
  198. inline void InlinedStringField::SetNoArena(
  199. const ::std::string* default_value, const ::std::string& value) {
  200. value_.assign(value);
  201. }
  202. #if LANG_CXX11
  203. inline void InlinedStringField::SetNoArena(
  204. const ::std::string* default_value, ::std::string&& value) {
  205. value_.assign(std::move(value));
  206. }
  207. #endif
  208. inline void InlinedStringField::Swap(InlinedStringField* from) {
  209. value_.swap(from->value_);
  210. }
  211. inline ::std::string* InlinedStringField::UnsafeMutablePointer() {
  212. return &value_;
  213. }
  214. inline void InlinedStringField::UnsafeSetDefault(
  215. const ::std::string* default_value) {
  216. value_.assign(*default_value);
  217. }
  218. inline ::std::string* InlinedStringField::UnsafeArenaRelease(
  219. const ::std::string* default_value, Arena* arena) {
  220. return ReleaseNoArena(default_value);
  221. }
  222. inline void InlinedStringField::UnsafeArenaSetAllocated(
  223. const ::std::string* default_value, ::std::string* value, Arena* arena) {
  224. if (value == NULL) {
  225. value_.assign(*default_value);
  226. } else {
  227. value_.assign(*value);
  228. }
  229. }
  230. } // namespace internal
  231. } // namespace protobuf
  232. } // namespace google
  233. #endif // GOOGLE_PROTOBUF_INLINED_STRING_FIELD_H__