inlined_string_field.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 <utility>
  34. #include <google/protobuf/stubs/logging.h>
  35. #include <google/protobuf/stubs/common.h>
  36. #include <google/protobuf/arenastring.h>
  37. #include <google/protobuf/message_lite.h>
  38. #include <google/protobuf/port.h>
  39. #include <google/protobuf/stubs/strutil.h>
  40. // Must be included last.
  41. #include <google/protobuf/port_def.inc>
  42. #ifdef SWIG
  43. #error "You cannot SWIG proto headers"
  44. #endif
  45. namespace google {
  46. namespace protobuf {
  47. class Arena;
  48. namespace internal {
  49. // InlinedStringField wraps a std::string instance and exposes an API similar to
  50. // ArenaStringPtr's wrapping of a std::string* instance.
  51. //
  52. // default_value parameters are taken for consistency with ArenaStringPtr, but
  53. // are not used for most methods. With inlining, these should be removed from
  54. // the generated binary.
  55. //
  56. // InlinedStringField has a donating mechanism that allows string buffer
  57. // allocated on arena. A string is donated means both the string container and
  58. // the data buffer are on arena. The donating mechanism here is similar to the
  59. // one in ArenaStringPtr with some differences:
  60. //
  61. // When an InlinedStringField is constructed, the donating state is true. This
  62. // is because the string container is directly stored in the message on the
  63. // arena:
  64. //
  65. // Construction: donated=true
  66. // Arena:
  67. // +-----------------------+
  68. // |Message foo: |
  69. // | +-------------------+ |
  70. // | |InlinedStringField:| |
  71. // | | +-----+ | |
  72. // | | | | | | | |
  73. // | | +-----+ | |
  74. // | +-------------------+ |
  75. // +-----------------------+
  76. //
  77. // When lvalue Set is called, the donating state is still true. String data will
  78. // be allocated on the arena:
  79. //
  80. // Lvalue Set: donated=true
  81. // Arena:
  82. // +-----------------------+
  83. // |Message foo: |
  84. // | +-------------------+ |
  85. // | |InlinedStringField:| |
  86. // | | +-----+ | |
  87. // | | | | | | | |
  88. // | | +|----+ | |
  89. // | +--|----------------+ |
  90. // | V |
  91. // | +----------------+ |
  92. // | |'f','o','o',... | |
  93. // | +----------------+ |
  94. // +-----------------------+
  95. //
  96. // Some operations will undonate a donated string, including: Mutable,
  97. // SetAllocated, Rvalue Set, and Swap with a non-donated string.
  98. //
  99. // For more details of the donating states transitions, go/pd-inlined-string.
  100. class PROTOBUF_EXPORT InlinedStringField {
  101. public:
  102. InlinedStringField() { Init(); }
  103. inline void Init() { new (get_mutable()) std::string(); }
  104. // Add the dummy parameter just to make InlinedStringField(nullptr)
  105. // unambiguous.
  106. constexpr InlinedStringField(
  107. const ExplicitlyConstructed<std::string>* /*default_value*/,
  108. bool /*dummy*/)
  109. : value_{} {}
  110. explicit InlinedStringField(const std::string& default_value);
  111. explicit InlinedStringField(Arena* arena);
  112. ~InlinedStringField() { Destruct(); }
  113. // Lvalue Set. To save space, we pack the donating states of multiple
  114. // InlinedStringFields into an uint32_t `donating_states`. The `mask`
  115. // indicates the position of the bit for this InlinedStringField. `donated` is
  116. // whether this field is donated.
  117. //
  118. // The caller should guarantee that:
  119. //
  120. // `donated == ((donating_states & ~mask) != 0)`
  121. //
  122. // This method never changes the `donating_states`.
  123. void Set(const std::string* default_value, ConstStringParam value,
  124. Arena* arena, bool donated, uint32_t* /*donating_states*/,
  125. uint32_t /*mask*/) {
  126. (void)arena;
  127. (void)donated;
  128. SetNoArena(default_value, value);
  129. }
  130. // Rvalue Set. If this field is donated, this method will undonate this field
  131. // by mutating the `donating_states` according to `mask`.
  132. void Set(const std::string* default_value, std::string&& value, Arena* arena,
  133. bool donated, uint32_t* donating_states, uint32_t mask);
  134. template <typename FirstParam>
  135. void Set(FirstParam p1, const char* str, ::google::protobuf::Arena* arena, bool donated,
  136. uint32_t* donating_states, uint32_t mask) {
  137. Set(p1, ConstStringParam(str), arena, donated, donating_states, mask);
  138. }
  139. template <typename FirstParam>
  140. void Set(FirstParam p1, const char* str, size_t size, ::google::protobuf::Arena* arena,
  141. bool donated, uint32_t* donating_states, uint32_t mask) {
  142. ConstStringParam sp{str, size}; // for string_view and `const string &`
  143. Set(p1, sp, arena, donated, donating_states, mask);
  144. }
  145. template <typename FirstParam, typename RefWrappedType>
  146. void Set(FirstParam p1,
  147. std::reference_wrapper<RefWrappedType> const_string_ref,
  148. ::google::protobuf::Arena* arena, bool donated, uint32_t* donating_states,
  149. uint32_t mask) {
  150. Set(p1, const_string_ref.get(), arena, donated, donating_states, mask);
  151. }
  152. template <typename FirstParam, typename SecondParam>
  153. void SetBytes(FirstParam p1, SecondParam&& p2, ::google::protobuf::Arena* arena,
  154. bool donated, uint32_t* donating_states, uint32_t mask) {
  155. Set(p1, static_cast<SecondParam&&>(p2), arena, donated, donating_states,
  156. mask);
  157. }
  158. template <typename FirstParam>
  159. void SetBytes(FirstParam p1, const void* str, size_t size,
  160. ::google::protobuf::Arena* arena, bool donated, uint32_t* donating_states,
  161. uint32_t mask) {
  162. // Must work whether ConstStringParam is string_view or `const string &`
  163. ConstStringParam sp{static_cast<const char*>(str), size};
  164. Set(p1, sp, arena, donated, donating_states, mask);
  165. }
  166. PROTOBUF_NDEBUG_INLINE void SetNoArena(const std::string* default_value,
  167. StringPiece value);
  168. PROTOBUF_NDEBUG_INLINE void SetNoArena(const std::string* default_value,
  169. std::string&& value);
  170. // Basic accessors.
  171. PROTOBUF_NDEBUG_INLINE const std::string& Get() const { return GetNoArena(); }
  172. PROTOBUF_NDEBUG_INLINE const std::string& GetNoArena() const;
  173. // Mutable returns a std::string* instance that is heap-allocated. If this
  174. // field is donated, this method undonates this field by mutating the
  175. // `donating_states` according to `mask`, and copies the content of the
  176. // original string to the returning string.
  177. std::string* Mutable(const LazyString& default_value, Arena* arena,
  178. bool donated, uint32_t* donating_states, uint32_t mask);
  179. std::string* Mutable(ArenaStringPtr::EmptyDefault, Arena* arena, bool donated,
  180. uint32_t* donating_states, uint32_t mask);
  181. // Release returns a std::string* instance that is heap-allocated and is not
  182. // Own()'d by any arena. If the field is not set, this returns NULL. The
  183. // caller retains ownership. Clears this field back to NULL state. Used to
  184. // implement release_<field>() methods on generated classes.
  185. PROTOBUF_MUST_USE_RESULT std::string* Release(
  186. const std::string* default_value, Arena* arena, bool donated);
  187. PROTOBUF_MUST_USE_RESULT std::string* ReleaseNonDefault(
  188. const std::string* default_value, Arena* arena);
  189. std::string* ReleaseNonDefaultNoArena(const std::string* default_value);
  190. // Takes a std::string that is heap-allocated, and takes ownership. The
  191. // std::string's destructor is registered with the arena. Used to implement
  192. // set_allocated_<field> in generated classes.
  193. //
  194. // If this field is donated, this method undonates this field by mutating the
  195. // `donating_states` according to `mask`.
  196. void SetAllocated(const std::string* default_value, std::string* value,
  197. Arena* arena, bool donated, uint32_t* donating_states,
  198. uint32_t mask);
  199. void SetAllocatedNoArena(const std::string* default_value,
  200. std::string* value);
  201. // When one of `this` and `from` is donated and the other is not donated, this
  202. // method will undonate the donated one and swap the two heap-allocated
  203. // strings.
  204. PROTOBUF_NDEBUG_INLINE void Swap(InlinedStringField* from,
  205. const std::string* default_value,
  206. Arena* arena, bool donated,
  207. bool from_donated, uint32_t* donating_states,
  208. uint32_t* from_donating_states,
  209. uint32_t mask);
  210. // Frees storage (if not on an arena).
  211. PROTOBUF_NDEBUG_INLINE void Destroy(const std::string* default_value,
  212. Arena* arena) {
  213. if (arena == nullptr) {
  214. DestroyNoArena(default_value);
  215. }
  216. }
  217. PROTOBUF_NDEBUG_INLINE void DestroyNoArena(const std::string* default_value);
  218. // Clears content, but keeps allocated std::string, to avoid the overhead of
  219. // heap operations. After this returns, the content (as seen by the user) will
  220. // always be the empty std::string.
  221. PROTOBUF_NDEBUG_INLINE void ClearToEmpty() { ClearNonDefaultToEmpty(); }
  222. PROTOBUF_NDEBUG_INLINE void ClearNonDefaultToEmpty() {
  223. get_mutable()->clear();
  224. }
  225. // Clears content, but keeps allocated std::string if arena != NULL, to avoid
  226. // the overhead of heap operations. After this returns, the content (as seen
  227. // by the user) will always be equal to |default_value|.
  228. void ClearToDefault(const LazyString& default_value, Arena* arena,
  229. bool donated);
  230. // Returns a mutable pointer, but doesn't initialize the string to the
  231. // default value.
  232. PROTOBUF_NDEBUG_INLINE std::string* MutableNoArenaNoDefault(
  233. const std::string* /*default_value*/);
  234. // Generated code / reflection only! Returns a mutable pointer to the string.
  235. PROTOBUF_NDEBUG_INLINE std::string* UnsafeMutablePointer();
  236. // InlinedStringField doesn't have things like the `default_value` pointer in
  237. // ArenaStringPtr.
  238. bool IsDefault(const std::string* /*default_value*/) const { return false; }
  239. private:
  240. void Destruct() { get_mutable()->~basic_string(); }
  241. PROTOBUF_NDEBUG_INLINE std::string* get_mutable();
  242. PROTOBUF_NDEBUG_INLINE const std::string* get_const() const;
  243. alignas(std::string) char value_[sizeof(std::string)];
  244. std::string* MutableSlow(::google::protobuf::Arena* arena, bool donated,
  245. uint32_t* donating_states, uint32_t mask);
  246. // When constructed in an Arena, we want our destructor to be skipped.
  247. friend class ::google::protobuf::Arena;
  248. typedef void InternalArenaConstructable_;
  249. typedef void DestructorSkippable_;
  250. };
  251. inline std::string* InlinedStringField::get_mutable() {
  252. return reinterpret_cast<std::string*>(&value_);
  253. }
  254. inline const std::string* InlinedStringField::get_const() const {
  255. return reinterpret_cast<const std::string*>(&value_);
  256. }
  257. inline InlinedStringField::InlinedStringField(
  258. const std::string& default_value) {
  259. new (get_mutable()) std::string(default_value);
  260. }
  261. inline InlinedStringField::InlinedStringField(Arena* arena) {
  262. Init();
  263. if (arena != nullptr) {
  264. arena->OwnDestructor(get_mutable());
  265. }
  266. }
  267. inline const std::string& InlinedStringField::GetNoArena() const {
  268. return *get_const();
  269. }
  270. inline void InlinedStringField::SetAllocatedNoArena(
  271. const std::string* /*default_value*/, std::string* value) {
  272. if (value == nullptr) {
  273. // Currently, inlined string field can't have non empty default.
  274. get_mutable()->clear();
  275. } else {
  276. get_mutable()->assign(std::move(*value));
  277. delete value;
  278. }
  279. }
  280. inline void InlinedStringField::DestroyNoArena(const std::string*) {
  281. // This is invoked from the generated message's ArenaDtor, which is used to
  282. // clean up objects not allocated on the Arena.
  283. this->~InlinedStringField();
  284. }
  285. inline std::string* InlinedStringField::ReleaseNonDefaultNoArena(
  286. const std::string* /*default_value*/) {
  287. // Currently, inlined string field can't have non empty default.
  288. auto* released = new std::string();
  289. get_mutable()->swap(*released);
  290. return released;
  291. }
  292. inline void InlinedStringField::SetNoArena(const std::string* /*default_value*/,
  293. StringPiece value) {
  294. get_mutable()->assign(value.data(), value.length());
  295. }
  296. inline void InlinedStringField::SetNoArena(const std::string* /*default_value*/,
  297. std::string&& value) {
  298. get_mutable()->assign(std::move(value));
  299. }
  300. inline void InlinedStringField::Swap(
  301. InlinedStringField* from, const std::string* /*default_value*/,
  302. Arena* arena, bool donated, bool from_donated, uint32_t* donating_states,
  303. uint32_t* from_donating_states, uint32_t mask) {
  304. #if GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL_INLINE
  305. // If one is donated and the other is not, undonate the donated one.
  306. if (donated && !from_donated) {
  307. MutableSlow(arena, donated, donating_states, mask);
  308. } else if (!donated && from_donated) {
  309. from->MutableSlow(arena, from_donated, from_donating_states, mask);
  310. }
  311. // Then, swap the two undonated strings.
  312. #else
  313. (void)arena;
  314. (void)donated;
  315. (void)from_donated;
  316. (void)donating_states;
  317. (void)from_donating_states;
  318. (void)mask;
  319. #endif
  320. get_mutable()->swap(*from->get_mutable());
  321. }
  322. inline std::string* InlinedStringField::MutableNoArenaNoDefault(
  323. const std::string*) {
  324. return get_mutable();
  325. }
  326. inline std::string* InlinedStringField::UnsafeMutablePointer() {
  327. return get_mutable();
  328. }
  329. } // namespace internal
  330. } // namespace protobuf
  331. } // namespace google
  332. #include <google/protobuf/port_undef.inc>
  333. #endif // GOOGLE_PROTOBUF_INLINED_STRING_FIELD_H__