repeated_ptr_field.h 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // RepeatedField and RepeatedPtrField are used by generated protocol message
  35. // classes to manipulate repeated fields. These classes are very similar to
  36. // STL's vector, but include a number of optimizations found to be useful
  37. // specifically in the case of Protocol Buffers. RepeatedPtrField is
  38. // particularly different from STL vector as it manages ownership of the
  39. // pointers that it contains.
  40. //
  41. // Typically, clients should not need to access RepeatedField objects directly,
  42. // but should instead use the accessor functions generated automatically by the
  43. // protocol compiler.
  44. //
  45. // This header covers RepeatedPtrField.
  46. #ifndef GOOGLE_PROTOBUF_REPEATED_PTR_FIELD_H__
  47. #define GOOGLE_PROTOBUF_REPEATED_PTR_FIELD_H__
  48. #include <utility>
  49. #ifdef _MSC_VER
  50. // This is required for min/max on VS2013 only.
  51. #include <algorithm>
  52. #endif
  53. #include <iterator>
  54. #include <limits>
  55. #include <string>
  56. #include <type_traits>
  57. #include <google/protobuf/stubs/logging.h>
  58. #include <google/protobuf/stubs/common.h>
  59. #include <google/protobuf/arena.h>
  60. #include <google/protobuf/message_lite.h>
  61. #include <google/protobuf/port.h>
  62. // Must be included last.
  63. #include <google/protobuf/port_def.inc>
  64. #ifdef SWIG
  65. #error "You cannot SWIG proto headers"
  66. #endif
  67. namespace google {
  68. namespace protobuf {
  69. class Message;
  70. class Reflection;
  71. template <typename T>
  72. struct WeakRepeatedPtrField;
  73. namespace internal {
  74. class MergePartialFromCodedStreamHelper;
  75. class SwapFieldHelper;
  76. } // namespace internal
  77. namespace internal {
  78. template <typename It>
  79. class RepeatedPtrIterator;
  80. template <typename It, typename VoidPtr>
  81. class RepeatedPtrOverPtrsIterator;
  82. } // namespace internal
  83. namespace internal {
  84. // type-traits helper for RepeatedPtrFieldBase: we only want to invoke
  85. // arena-related "copy if on different arena" behavior if the necessary methods
  86. // exist on the contained type. In particular, we rely on MergeFrom() existing
  87. // as a general proxy for the fact that a copy will work, and we also provide a
  88. // specific override for std::string*.
  89. template <typename T>
  90. struct TypeImplementsMergeBehaviorProbeForMergeFrom {
  91. typedef char HasMerge;
  92. typedef long HasNoMerge;
  93. // We accept either of:
  94. // - void MergeFrom(const T& other)
  95. // - bool MergeFrom(const T& other)
  96. //
  97. // We mangle these names a bit to avoid compatibility issues in 'unclean'
  98. // include environments that may have, e.g., "#define test ..." (yes, this
  99. // exists).
  100. template <typename U, typename RetType, RetType (U::*)(const U& arg)>
  101. struct CheckType;
  102. template <typename U>
  103. static HasMerge Check(CheckType<U, void, &U::MergeFrom>*);
  104. template <typename U>
  105. static HasMerge Check(CheckType<U, bool, &U::MergeFrom>*);
  106. template <typename U>
  107. static HasNoMerge Check(...);
  108. // Resolves to either std::true_type or std::false_type.
  109. typedef std::integral_constant<bool,
  110. (sizeof(Check<T>(0)) == sizeof(HasMerge))>
  111. type;
  112. };
  113. template <typename T, typename = void>
  114. struct TypeImplementsMergeBehavior
  115. : TypeImplementsMergeBehaviorProbeForMergeFrom<T> {};
  116. template <>
  117. struct TypeImplementsMergeBehavior<std::string> {
  118. typedef std::true_type type;
  119. };
  120. template <typename T>
  121. struct IsMovable
  122. : std::integral_constant<bool, std::is_move_constructible<T>::value &&
  123. std::is_move_assignable<T>::value> {};
  124. // This is the common base class for RepeatedPtrFields. It deals only in void*
  125. // pointers. Users should not use this interface directly.
  126. //
  127. // The methods of this interface correspond to the methods of RepeatedPtrField,
  128. // but may have a template argument called TypeHandler. Its signature is:
  129. // class TypeHandler {
  130. // public:
  131. // typedef MyType Type;
  132. // static Type* New();
  133. // static Type* NewFromPrototype(const Type* prototype,
  134. // Arena* arena);
  135. // static void Delete(Type*);
  136. // static void Clear(Type*);
  137. // static void Merge(const Type& from, Type* to);
  138. //
  139. // // Only needs to be implemented if SpaceUsedExcludingSelf() is called.
  140. // static int SpaceUsedLong(const Type&);
  141. // };
  142. class PROTOBUF_EXPORT RepeatedPtrFieldBase {
  143. protected:
  144. constexpr RepeatedPtrFieldBase();
  145. explicit RepeatedPtrFieldBase(Arena* arena);
  146. ~RepeatedPtrFieldBase() {
  147. #ifndef NDEBUG
  148. // Try to trigger segfault / asan failure in non-opt builds. If arena_
  149. // lifetime has ended before the destructor.
  150. if (arena_) (void)arena_->SpaceAllocated();
  151. #endif
  152. }
  153. // Must be called from destructor.
  154. template <typename TypeHandler>
  155. void Destroy();
  156. bool NeedsDestroy() const { return rep_ != nullptr && arena_ == nullptr; }
  157. void DestroyProtos();
  158. bool empty() const;
  159. int size() const;
  160. template <typename TypeHandler>
  161. const typename TypeHandler::Type& at(int index) const;
  162. template <typename TypeHandler>
  163. typename TypeHandler::Type& at(int index);
  164. template <typename TypeHandler>
  165. typename TypeHandler::Type* Mutable(int index);
  166. template <typename TypeHandler>
  167. void Delete(int index);
  168. template <typename TypeHandler>
  169. typename TypeHandler::Type* Add(
  170. typename TypeHandler::Type* prototype = nullptr);
  171. public:
  172. // The next few methods are public so that they can be called from generated
  173. // code when implicit weak fields are used, but they should never be called by
  174. // application code.
  175. template <typename TypeHandler>
  176. const typename TypeHandler::Type& Get(int index) const;
  177. // Creates and adds an element using the given prototype, without introducing
  178. // a link-time dependency on the concrete message type. This method is used to
  179. // implement implicit weak fields. The prototype may be nullptr, in which case
  180. // an ImplicitWeakMessage will be used as a placeholder.
  181. MessageLite* AddWeak(const MessageLite* prototype);
  182. template <typename TypeHandler>
  183. void Clear();
  184. template <typename TypeHandler>
  185. void MergeFrom(const RepeatedPtrFieldBase& other);
  186. inline void InternalSwap(RepeatedPtrFieldBase*);
  187. protected:
  188. template <
  189. typename TypeHandler,
  190. typename std::enable_if<TypeHandler::Movable::value>::type* = nullptr>
  191. void Add(typename TypeHandler::Type&& value);
  192. template <typename TypeHandler>
  193. void RemoveLast();
  194. template <typename TypeHandler>
  195. void CopyFrom(const RepeatedPtrFieldBase& other);
  196. void CloseGap(int start, int num);
  197. void Reserve(int new_size);
  198. int Capacity() const;
  199. template <typename TypeHandler>
  200. static inline typename TypeHandler::Type* copy(
  201. typename TypeHandler::Type* value) {
  202. auto* new_value = TypeHandler::NewFromPrototype(value, nullptr);
  203. TypeHandler::Merge(*value, new_value);
  204. return new_value;
  205. }
  206. // Used for constructing iterators.
  207. void* const* raw_data() const;
  208. void** raw_mutable_data() const;
  209. template <typename TypeHandler>
  210. typename TypeHandler::Type** mutable_data();
  211. template <typename TypeHandler>
  212. const typename TypeHandler::Type* const* data() const;
  213. template <typename TypeHandler>
  214. PROTOBUF_NDEBUG_INLINE void Swap(RepeatedPtrFieldBase* other);
  215. void SwapElements(int index1, int index2);
  216. template <typename TypeHandler>
  217. size_t SpaceUsedExcludingSelfLong() const;
  218. // Advanced memory management --------------------------------------
  219. // Like Add(), but if there are no cleared objects to use, returns nullptr.
  220. template <typename TypeHandler>
  221. typename TypeHandler::Type* AddFromCleared();
  222. template <typename TypeHandler>
  223. void AddAllocated(typename TypeHandler::Type* value) {
  224. typename TypeImplementsMergeBehavior<typename TypeHandler::Type>::type t;
  225. AddAllocatedInternal<TypeHandler>(value, t);
  226. }
  227. template <typename TypeHandler>
  228. void UnsafeArenaAddAllocated(typename TypeHandler::Type* value);
  229. template <typename TypeHandler>
  230. PROTOBUF_NODISCARD typename TypeHandler::Type* ReleaseLast() {
  231. typename TypeImplementsMergeBehavior<typename TypeHandler::Type>::type t;
  232. return ReleaseLastInternal<TypeHandler>(t);
  233. }
  234. // Releases last element and returns it, but does not do out-of-arena copy.
  235. // And just returns the raw pointer to the contained element in the arena.
  236. template <typename TypeHandler>
  237. typename TypeHandler::Type* UnsafeArenaReleaseLast();
  238. int ClearedCount() const;
  239. template <typename TypeHandler>
  240. void AddCleared(typename TypeHandler::Type* value);
  241. template <typename TypeHandler>
  242. PROTOBUF_NODISCARD typename TypeHandler::Type* ReleaseCleared();
  243. template <typename TypeHandler>
  244. void AddAllocatedInternal(typename TypeHandler::Type* value, std::true_type);
  245. template <typename TypeHandler>
  246. void AddAllocatedInternal(typename TypeHandler::Type* value, std::false_type);
  247. template <typename TypeHandler>
  248. PROTOBUF_NOINLINE void AddAllocatedSlowWithCopy(
  249. typename TypeHandler::Type* value, Arena* value_arena, Arena* my_arena);
  250. template <typename TypeHandler>
  251. PROTOBUF_NOINLINE void AddAllocatedSlowWithoutCopy(
  252. typename TypeHandler::Type* value);
  253. template <typename TypeHandler>
  254. typename TypeHandler::Type* ReleaseLastInternal(std::true_type);
  255. template <typename TypeHandler>
  256. typename TypeHandler::Type* ReleaseLastInternal(std::false_type);
  257. template <typename TypeHandler>
  258. PROTOBUF_NOINLINE void SwapFallback(RepeatedPtrFieldBase* other);
  259. inline Arena* GetArena() const { return arena_; }
  260. private:
  261. static constexpr int kInitialSize = 0;
  262. // A few notes on internal representation:
  263. //
  264. // We use an indirected approach, with struct Rep, to keep
  265. // sizeof(RepeatedPtrFieldBase) equivalent to what it was before arena support
  266. // was added, namely, 3 8-byte machine words on x86-64. An instance of Rep is
  267. // allocated only when the repeated field is non-empty, and it is a
  268. // dynamically-sized struct (the header is directly followed by elements[]).
  269. // We place arena_ and current_size_ directly in the object to avoid cache
  270. // misses due to the indirection, because these fields are checked frequently.
  271. // Placing all fields directly in the RepeatedPtrFieldBase instance costs
  272. // significant performance for memory-sensitive workloads.
  273. Arena* arena_;
  274. int current_size_;
  275. int total_size_;
  276. struct Rep {
  277. int allocated_size;
  278. // Here we declare a huge array as a way of approximating C's "flexible
  279. // array member" feature without relying on undefined behavior.
  280. void* elements[(std::numeric_limits<int>::max() - 2 * sizeof(int)) /
  281. sizeof(void*)];
  282. };
  283. static constexpr size_t kRepHeaderSize = offsetof(Rep, elements);
  284. Rep* rep_;
  285. template <typename TypeHandler>
  286. static inline typename TypeHandler::Type* cast(void* element) {
  287. return reinterpret_cast<typename TypeHandler::Type*>(element);
  288. }
  289. template <typename TypeHandler>
  290. static inline const typename TypeHandler::Type* cast(const void* element) {
  291. return reinterpret_cast<const typename TypeHandler::Type*>(element);
  292. }
  293. // Non-templated inner function to avoid code duplication. Takes a function
  294. // pointer to the type-specific (templated) inner allocate/merge loop.
  295. void MergeFromInternal(const RepeatedPtrFieldBase& other,
  296. void (RepeatedPtrFieldBase::*inner_loop)(void**,
  297. void**, int,
  298. int));
  299. template <typename TypeHandler>
  300. PROTOBUF_NOINLINE void MergeFromInnerLoop(void** our_elems,
  301. void** other_elems, int length,
  302. int already_allocated);
  303. // Internal helper: extend array space if necessary to contain |extend_amount|
  304. // more elements, and return a pointer to the element immediately following
  305. // the old list of elements. This interface factors out common behavior from
  306. // Reserve() and MergeFrom() to reduce code size. |extend_amount| must be > 0.
  307. void** InternalExtend(int extend_amount);
  308. // Internal helper for Add: add "obj" as the next element in the
  309. // array, including potentially resizing the array with Reserve if
  310. // needed
  311. void* AddOutOfLineHelper(void* obj);
  312. // The reflection implementation needs to call protected methods directly,
  313. // reinterpreting pointers as being to Message instead of a specific Message
  314. // subclass.
  315. friend class ::PROTOBUF_NAMESPACE_ID::Reflection;
  316. friend class ::PROTOBUF_NAMESPACE_ID::internal::SwapFieldHelper;
  317. // ExtensionSet stores repeated message extensions as
  318. // RepeatedPtrField<MessageLite>, but non-lite ExtensionSets need to implement
  319. // SpaceUsedLong(), and thus need to call SpaceUsedExcludingSelfLong()
  320. // reinterpreting MessageLite as Message. ExtensionSet also needs to make use
  321. // of AddFromCleared(), which is not part of the public interface.
  322. friend class ExtensionSet;
  323. // The MapFieldBase implementation needs to call protected methods directly,
  324. // reinterpreting pointers as being to Message instead of a specific Message
  325. // subclass.
  326. friend class MapFieldBase;
  327. friend class MapFieldBaseStub;
  328. // The table-driven MergePartialFromCodedStream implementation needs to
  329. // operate on RepeatedPtrField<MessageLite>.
  330. friend class MergePartialFromCodedStreamHelper;
  331. friend class AccessorHelper;
  332. template <typename T>
  333. friend struct google::protobuf::WeakRepeatedPtrField;
  334. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedPtrFieldBase);
  335. };
  336. template <typename GenericType>
  337. class GenericTypeHandler {
  338. public:
  339. typedef GenericType Type;
  340. using Movable = IsMovable<GenericType>;
  341. static inline GenericType* New(Arena* arena) {
  342. return Arena::CreateMaybeMessage<Type>(arena);
  343. }
  344. static inline GenericType* New(Arena* arena, GenericType&& value) {
  345. return Arena::Create<GenericType>(arena, std::move(value));
  346. }
  347. static inline GenericType* NewFromPrototype(const GenericType* prototype,
  348. Arena* arena = nullptr);
  349. static inline void Delete(GenericType* value, Arena* arena) {
  350. if (arena == nullptr) {
  351. delete value;
  352. }
  353. }
  354. static inline Arena* GetOwningArena(GenericType* value) {
  355. return Arena::GetOwningArena<Type>(value);
  356. }
  357. static inline void Clear(GenericType* value) { value->Clear(); }
  358. PROTOBUF_NOINLINE
  359. static void Merge(const GenericType& from, GenericType* to);
  360. static inline size_t SpaceUsedLong(const GenericType& value) {
  361. return value.SpaceUsedLong();
  362. }
  363. };
  364. template <typename GenericType>
  365. GenericType* GenericTypeHandler<GenericType>::NewFromPrototype(
  366. const GenericType* /* prototype */, Arena* arena) {
  367. return New(arena);
  368. }
  369. template <typename GenericType>
  370. void GenericTypeHandler<GenericType>::Merge(const GenericType& from,
  371. GenericType* to) {
  372. to->MergeFrom(from);
  373. }
  374. // NewFromPrototype() and Merge() are not defined inline here, as we will need
  375. // to do a virtual function dispatch anyways to go from Message* to call
  376. // New/Merge.
  377. template <>
  378. MessageLite* GenericTypeHandler<MessageLite>::NewFromPrototype(
  379. const MessageLite* prototype, Arena* arena);
  380. template <>
  381. inline Arena* GenericTypeHandler<MessageLite>::GetOwningArena(
  382. MessageLite* value) {
  383. return value->GetOwningArena();
  384. }
  385. template <>
  386. void GenericTypeHandler<MessageLite>::Merge(const MessageLite& from,
  387. MessageLite* to);
  388. template <>
  389. inline void GenericTypeHandler<std::string>::Clear(std::string* value) {
  390. value->clear();
  391. }
  392. template <>
  393. void GenericTypeHandler<std::string>::Merge(const std::string& from,
  394. std::string* to);
  395. // Message specialization bodies defined in message.cc. This split is necessary
  396. // to allow proto2-lite (which includes this header) to be independent of
  397. // Message.
  398. template <>
  399. PROTOBUF_EXPORT Message* GenericTypeHandler<Message>::NewFromPrototype(
  400. const Message* prototype, Arena* arena);
  401. template <>
  402. PROTOBUF_EXPORT Arena* GenericTypeHandler<Message>::GetOwningArena(
  403. Message* value);
  404. class StringTypeHandler {
  405. public:
  406. typedef std::string Type;
  407. using Movable = IsMovable<Type>;
  408. static inline std::string* New(Arena* arena) {
  409. return Arena::Create<std::string>(arena);
  410. }
  411. static inline std::string* New(Arena* arena, std::string&& value) {
  412. return Arena::Create<std::string>(arena, std::move(value));
  413. }
  414. static inline std::string* NewFromPrototype(const std::string*,
  415. Arena* arena) {
  416. return New(arena);
  417. }
  418. static inline Arena* GetOwningArena(std::string*) { return nullptr; }
  419. static inline void Delete(std::string* value, Arena* arena) {
  420. if (arena == nullptr) {
  421. delete value;
  422. }
  423. }
  424. static inline void Clear(std::string* value) { value->clear(); }
  425. static inline void Merge(const std::string& from, std::string* to) {
  426. *to = from;
  427. }
  428. static size_t SpaceUsedLong(const std::string& value) {
  429. return sizeof(value) + StringSpaceUsedExcludingSelfLong(value);
  430. }
  431. };
  432. } // namespace internal
  433. // RepeatedPtrField is like RepeatedField, but used for repeated strings or
  434. // Messages.
  435. template <typename Element>
  436. class RepeatedPtrField final : private internal::RepeatedPtrFieldBase {
  437. public:
  438. constexpr RepeatedPtrField();
  439. explicit RepeatedPtrField(Arena* arena);
  440. RepeatedPtrField(const RepeatedPtrField& other);
  441. template <typename Iter,
  442. typename = typename std::enable_if<std::is_constructible<
  443. Element, decltype(*std::declval<Iter>())>::value>::type>
  444. RepeatedPtrField(Iter begin, Iter end);
  445. ~RepeatedPtrField();
  446. RepeatedPtrField& operator=(const RepeatedPtrField& other);
  447. RepeatedPtrField(RepeatedPtrField&& other) noexcept;
  448. RepeatedPtrField& operator=(RepeatedPtrField&& other) noexcept;
  449. bool empty() const;
  450. int size() const;
  451. const Element& Get(int index) const;
  452. Element* Mutable(int index);
  453. Element* Add();
  454. void Add(Element&& value);
  455. // Append elements in the range [begin, end) after reserving
  456. // the appropriate number of elements.
  457. template <typename Iter>
  458. void Add(Iter begin, Iter end);
  459. const Element& operator[](int index) const { return Get(index); }
  460. Element& operator[](int index) { return *Mutable(index); }
  461. const Element& at(int index) const;
  462. Element& at(int index);
  463. // Remove the last element in the array.
  464. // Ownership of the element is retained by the array.
  465. void RemoveLast();
  466. // Delete elements with indices in the range [start .. start+num-1].
  467. // Caution: implementation moves all elements with indices [start+num .. ].
  468. // Calling this routine inside a loop can cause quadratic behavior.
  469. void DeleteSubrange(int start, int num);
  470. PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear();
  471. void MergeFrom(const RepeatedPtrField& other);
  472. PROTOBUF_ATTRIBUTE_REINITIALIZES void CopyFrom(const RepeatedPtrField& other);
  473. // Replaces the contents with RepeatedPtrField(begin, end).
  474. template <typename Iter>
  475. PROTOBUF_ATTRIBUTE_REINITIALIZES void Assign(Iter begin, Iter end);
  476. // Reserve space to expand the field to at least the given size. This only
  477. // resizes the pointer array; it doesn't allocate any objects. If the
  478. // array is grown, it will always be at least doubled in size.
  479. void Reserve(int new_size);
  480. int Capacity() const;
  481. // Gets the underlying array. This pointer is possibly invalidated by
  482. // any add or remove operation.
  483. Element** mutable_data();
  484. const Element* const* data() const;
  485. // Swap entire contents with "other". If they are on separate arenas, then
  486. // copies data.
  487. void Swap(RepeatedPtrField* other);
  488. // Swap entire contents with "other". Caller should guarantee that either both
  489. // fields are on the same arena or both are on the heap. Swapping between
  490. // different arenas with this function is disallowed and is caught via
  491. // GOOGLE_DCHECK.
  492. void UnsafeArenaSwap(RepeatedPtrField* other);
  493. // Swap two elements.
  494. void SwapElements(int index1, int index2);
  495. // STL-like iterator support
  496. typedef internal::RepeatedPtrIterator<Element> iterator;
  497. typedef internal::RepeatedPtrIterator<const Element> const_iterator;
  498. typedef Element value_type;
  499. typedef value_type& reference;
  500. typedef const value_type& const_reference;
  501. typedef value_type* pointer;
  502. typedef const value_type* const_pointer;
  503. typedef int size_type;
  504. typedef ptrdiff_t difference_type;
  505. iterator begin();
  506. const_iterator begin() const;
  507. const_iterator cbegin() const;
  508. iterator end();
  509. const_iterator end() const;
  510. const_iterator cend() const;
  511. // Reverse iterator support
  512. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  513. typedef std::reverse_iterator<iterator> reverse_iterator;
  514. reverse_iterator rbegin() { return reverse_iterator(end()); }
  515. const_reverse_iterator rbegin() const {
  516. return const_reverse_iterator(end());
  517. }
  518. reverse_iterator rend() { return reverse_iterator(begin()); }
  519. const_reverse_iterator rend() const {
  520. return const_reverse_iterator(begin());
  521. }
  522. // Custom STL-like iterator that iterates over and returns the underlying
  523. // pointers to Element rather than Element itself.
  524. typedef internal::RepeatedPtrOverPtrsIterator<Element*, void*>
  525. pointer_iterator;
  526. typedef internal::RepeatedPtrOverPtrsIterator<const Element* const,
  527. const void* const>
  528. const_pointer_iterator;
  529. pointer_iterator pointer_begin();
  530. const_pointer_iterator pointer_begin() const;
  531. pointer_iterator pointer_end();
  532. const_pointer_iterator pointer_end() const;
  533. // Returns (an estimate of) the number of bytes used by the repeated field,
  534. // excluding sizeof(*this).
  535. size_t SpaceUsedExcludingSelfLong() const;
  536. int SpaceUsedExcludingSelf() const {
  537. return internal::ToIntSize(SpaceUsedExcludingSelfLong());
  538. }
  539. // Advanced memory management --------------------------------------
  540. // When hardcore memory management becomes necessary -- as it sometimes
  541. // does here at Google -- the following methods may be useful.
  542. // Add an already-allocated object, passing ownership to the
  543. // RepeatedPtrField.
  544. //
  545. // Note that some special behavior occurs with respect to arenas:
  546. //
  547. // (i) if this field holds submessages, the new submessage will be copied if
  548. // the original is in an arena and this RepeatedPtrField is either in a
  549. // different arena, or on the heap.
  550. // (ii) if this field holds strings, the passed-in string *must* be
  551. // heap-allocated, not arena-allocated. There is no way to dynamically check
  552. // this at runtime, so User Beware.
  553. void AddAllocated(Element* value);
  554. // Remove the last element and return it, passing ownership to the caller.
  555. // Requires: size() > 0
  556. //
  557. // If this RepeatedPtrField is on an arena, an object copy is required to pass
  558. // ownership back to the user (for compatible semantics). Use
  559. // UnsafeArenaReleaseLast() if this behavior is undesired.
  560. PROTOBUF_NODISCARD Element* ReleaseLast();
  561. // Add an already-allocated object, skipping arena-ownership checks. The user
  562. // must guarantee that the given object is in the same arena as this
  563. // RepeatedPtrField.
  564. // It is also useful in legacy code that uses temporary ownership to avoid
  565. // copies. Example:
  566. // RepeatedPtrField<T> temp_field;
  567. // temp_field.UnsafeArenaAddAllocated(new T);
  568. // ... // Do something with temp_field
  569. // temp_field.UnsafeArenaExtractSubrange(0, temp_field.size(), nullptr);
  570. // If you put temp_field on the arena this fails, because the ownership
  571. // transfers to the arena at the "AddAllocated" call and is not released
  572. // anymore causing a double delete. UnsafeArenaAddAllocated prevents this.
  573. void UnsafeArenaAddAllocated(Element* value);
  574. // Remove the last element and return it. Unlike ReleaseLast, the returned
  575. // pointer is always to the original object. This may be in an arena, and
  576. // therefore have the arena's lifetime.
  577. // Requires: current_size_ > 0
  578. Element* UnsafeArenaReleaseLast();
  579. // Extract elements with indices in the range "[start .. start+num-1]".
  580. // The caller assumes ownership of the extracted elements and is responsible
  581. // for deleting them when they are no longer needed.
  582. // If "elements" is non-nullptr, then pointers to the extracted elements
  583. // are stored in "elements[0 .. num-1]" for the convenience of the caller.
  584. // If "elements" is nullptr, then the caller must use some other mechanism
  585. // to perform any further operations (like deletion) on these elements.
  586. // Caution: implementation also moves elements with indices [start+num ..].
  587. // Calling this routine inside a loop can cause quadratic behavior.
  588. //
  589. // Memory copying behavior is identical to ReleaseLast(), described above: if
  590. // this RepeatedPtrField is on an arena, an object copy is performed for each
  591. // returned element, so that all returned element pointers are to
  592. // heap-allocated copies. If this copy is not desired, the user should call
  593. // UnsafeArenaExtractSubrange().
  594. void ExtractSubrange(int start, int num, Element** elements);
  595. // Identical to ExtractSubrange() described above, except that no object
  596. // copies are ever performed. Instead, the raw object pointers are returned.
  597. // Thus, if on an arena, the returned objects must not be freed, because they
  598. // will not be heap-allocated objects.
  599. void UnsafeArenaExtractSubrange(int start, int num, Element** elements);
  600. // When elements are removed by calls to RemoveLast() or Clear(), they
  601. // are not actually freed. Instead, they are cleared and kept so that
  602. // they can be reused later. This can save lots of CPU time when
  603. // repeatedly reusing a protocol message for similar purposes.
  604. //
  605. // Hardcore programs may choose to manipulate these cleared objects
  606. // to better optimize memory management using the following routines.
  607. // Get the number of cleared objects that are currently being kept
  608. // around for reuse.
  609. int ClearedCount() const;
  610. #ifndef PROTOBUF_FUTURE_BREAKING_CHANGES
  611. // Add an element to the pool of cleared objects, passing ownership to
  612. // the RepeatedPtrField. The element must be cleared prior to calling
  613. // this method.
  614. //
  615. // This method cannot be called when the repeated field is on an arena or when
  616. // |value| is; both cases will trigger a GOOGLE_DCHECK-failure.
  617. void AddCleared(Element* value);
  618. // Remove a single element from the cleared pool and return it, passing
  619. // ownership to the caller. The element is guaranteed to be cleared.
  620. // Requires: ClearedCount() > 0
  621. //
  622. //
  623. // This method cannot be called when the repeated field is on an arena; doing
  624. // so will trigger a GOOGLE_DCHECK-failure.
  625. PROTOBUF_NODISCARD Element* ReleaseCleared();
  626. #endif // !PROTOBUF_FUTURE_BREAKING_CHANGES
  627. // Removes the element referenced by position.
  628. //
  629. // Returns an iterator to the element immediately following the removed
  630. // element.
  631. //
  632. // Invalidates all iterators at or after the removed element, including end().
  633. iterator erase(const_iterator position);
  634. // Removes the elements in the range [first, last).
  635. //
  636. // Returns an iterator to the element immediately following the removed range.
  637. //
  638. // Invalidates all iterators at or after the removed range, including end().
  639. iterator erase(const_iterator first, const_iterator last);
  640. // Gets the arena on which this RepeatedPtrField stores its elements.
  641. inline Arena* GetArena() const;
  642. // For internal use only.
  643. //
  644. // This is public due to it being called by generated code.
  645. void InternalSwap(RepeatedPtrField* other) {
  646. internal::RepeatedPtrFieldBase::InternalSwap(other);
  647. }
  648. private:
  649. // Note: RepeatedPtrField SHOULD NOT be subclassed by users.
  650. class TypeHandler;
  651. // Implementations for ExtractSubrange(). The copying behavior must be
  652. // included only if the type supports the necessary operations (e.g.,
  653. // MergeFrom()), so we must resolve this at compile time. ExtractSubrange()
  654. // uses SFINAE to choose one of the below implementations.
  655. void ExtractSubrangeInternal(int start, int num, Element** elements,
  656. std::true_type);
  657. void ExtractSubrangeInternal(int start, int num, Element** elements,
  658. std::false_type);
  659. friend class Arena;
  660. template <typename T>
  661. friend struct WeakRepeatedPtrField;
  662. typedef void InternalArenaConstructable_;
  663. };
  664. // implementation ====================================================
  665. namespace internal {
  666. constexpr RepeatedPtrFieldBase::RepeatedPtrFieldBase()
  667. : arena_(nullptr), current_size_(0), total_size_(0), rep_(nullptr) {}
  668. inline RepeatedPtrFieldBase::RepeatedPtrFieldBase(Arena* arena)
  669. : arena_(arena), current_size_(0), total_size_(0), rep_(nullptr) {}
  670. template <typename TypeHandler>
  671. void RepeatedPtrFieldBase::Destroy() {
  672. if (rep_ != nullptr && arena_ == nullptr) {
  673. int n = rep_->allocated_size;
  674. void* const* elements = rep_->elements;
  675. for (int i = 0; i < n; i++) {
  676. TypeHandler::Delete(cast<TypeHandler>(elements[i]), nullptr);
  677. }
  678. #if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation)
  679. const size_t size = total_size_ * sizeof(elements[0]) + kRepHeaderSize;
  680. ::operator delete(static_cast<void*>(rep_), size);
  681. #else
  682. ::operator delete(static_cast<void*>(rep_));
  683. #endif
  684. }
  685. rep_ = nullptr;
  686. }
  687. template <typename TypeHandler>
  688. inline void RepeatedPtrFieldBase::Swap(RepeatedPtrFieldBase* other) {
  689. #ifdef PROTOBUF_FORCE_COPY_IN_SWAP
  690. if (GetArena() != nullptr && GetArena() == other->GetArena()) {
  691. #else // PROTOBUF_FORCE_COPY_IN_SWAP
  692. if (GetArena() == other->GetArena()) {
  693. #endif // !PROTOBUF_FORCE_COPY_IN_SWAP
  694. InternalSwap(other);
  695. } else {
  696. SwapFallback<TypeHandler>(other);
  697. }
  698. }
  699. template <typename TypeHandler>
  700. void RepeatedPtrFieldBase::SwapFallback(RepeatedPtrFieldBase* other) {
  701. #ifdef PROTOBUF_FORCE_COPY_IN_SWAP
  702. GOOGLE_DCHECK(GetArena() == nullptr || other->GetArena() != GetArena());
  703. #else // PROTOBUF_FORCE_COPY_IN_SWAP
  704. GOOGLE_DCHECK(other->GetArena() != GetArena());
  705. #endif // !PROTOBUF_FORCE_COPY_IN_SWAP
  706. // Copy semantics in this case. We try to improve efficiency by placing the
  707. // temporary on |other|'s arena so that messages are copied twice rather than
  708. // three times.
  709. RepeatedPtrFieldBase temp(other->GetArena());
  710. temp.MergeFrom<TypeHandler>(*this);
  711. this->Clear<TypeHandler>();
  712. this->MergeFrom<TypeHandler>(*other);
  713. other->InternalSwap(&temp);
  714. temp.Destroy<TypeHandler>(); // Frees rep_ if `other` had no arena.
  715. }
  716. inline bool RepeatedPtrFieldBase::empty() const { return current_size_ == 0; }
  717. inline int RepeatedPtrFieldBase::size() const { return current_size_; }
  718. template <typename TypeHandler>
  719. inline const typename TypeHandler::Type& RepeatedPtrFieldBase::Get(
  720. int index) const {
  721. GOOGLE_DCHECK_GE(index, 0);
  722. GOOGLE_DCHECK_LT(index, current_size_);
  723. return *cast<TypeHandler>(rep_->elements[index]);
  724. }
  725. template <typename TypeHandler>
  726. inline const typename TypeHandler::Type& RepeatedPtrFieldBase::at(
  727. int index) const {
  728. GOOGLE_CHECK_GE(index, 0);
  729. GOOGLE_CHECK_LT(index, current_size_);
  730. return *cast<TypeHandler>(rep_->elements[index]);
  731. }
  732. template <typename TypeHandler>
  733. inline typename TypeHandler::Type& RepeatedPtrFieldBase::at(int index) {
  734. GOOGLE_CHECK_GE(index, 0);
  735. GOOGLE_CHECK_LT(index, current_size_);
  736. return *cast<TypeHandler>(rep_->elements[index]);
  737. }
  738. template <typename TypeHandler>
  739. inline typename TypeHandler::Type* RepeatedPtrFieldBase::Mutable(int index) {
  740. GOOGLE_DCHECK_GE(index, 0);
  741. GOOGLE_DCHECK_LT(index, current_size_);
  742. return cast<TypeHandler>(rep_->elements[index]);
  743. }
  744. template <typename TypeHandler>
  745. inline void RepeatedPtrFieldBase::Delete(int index) {
  746. GOOGLE_DCHECK_GE(index, 0);
  747. GOOGLE_DCHECK_LT(index, current_size_);
  748. TypeHandler::Delete(cast<TypeHandler>(rep_->elements[index]), arena_);
  749. }
  750. template <typename TypeHandler>
  751. inline typename TypeHandler::Type* RepeatedPtrFieldBase::Add(
  752. typename TypeHandler::Type* prototype) {
  753. if (rep_ != nullptr && current_size_ < rep_->allocated_size) {
  754. return cast<TypeHandler>(rep_->elements[current_size_++]);
  755. }
  756. typename TypeHandler::Type* result =
  757. TypeHandler::NewFromPrototype(prototype, arena_);
  758. return reinterpret_cast<typename TypeHandler::Type*>(
  759. AddOutOfLineHelper(result));
  760. }
  761. template <typename TypeHandler,
  762. typename std::enable_if<TypeHandler::Movable::value>::type*>
  763. inline void RepeatedPtrFieldBase::Add(typename TypeHandler::Type&& value) {
  764. if (rep_ != nullptr && current_size_ < rep_->allocated_size) {
  765. *cast<TypeHandler>(rep_->elements[current_size_++]) = std::move(value);
  766. return;
  767. }
  768. if (!rep_ || rep_->allocated_size == total_size_) {
  769. Reserve(total_size_ + 1);
  770. }
  771. ++rep_->allocated_size;
  772. typename TypeHandler::Type* result =
  773. TypeHandler::New(arena_, std::move(value));
  774. rep_->elements[current_size_++] = result;
  775. }
  776. template <typename TypeHandler>
  777. inline void RepeatedPtrFieldBase::RemoveLast() {
  778. GOOGLE_DCHECK_GT(current_size_, 0);
  779. TypeHandler::Clear(cast<TypeHandler>(rep_->elements[--current_size_]));
  780. }
  781. template <typename TypeHandler>
  782. void RepeatedPtrFieldBase::Clear() {
  783. const int n = current_size_;
  784. GOOGLE_DCHECK_GE(n, 0);
  785. if (n > 0) {
  786. void* const* elements = rep_->elements;
  787. int i = 0;
  788. do {
  789. TypeHandler::Clear(cast<TypeHandler>(elements[i++]));
  790. } while (i < n);
  791. current_size_ = 0;
  792. }
  793. }
  794. // To avoid unnecessary code duplication and reduce binary size, we use a
  795. // layered approach to implementing MergeFrom(). The toplevel method is
  796. // templated, so we get a small thunk per concrete message type in the binary.
  797. // This calls a shared implementation with most of the logic, passing a function
  798. // pointer to another type-specific piece of code that calls the object-allocate
  799. // and merge handlers.
  800. template <typename TypeHandler>
  801. inline void RepeatedPtrFieldBase::MergeFrom(const RepeatedPtrFieldBase& other) {
  802. GOOGLE_DCHECK_NE(&other, this);
  803. if (other.current_size_ == 0) return;
  804. MergeFromInternal(other,
  805. &RepeatedPtrFieldBase::MergeFromInnerLoop<TypeHandler>);
  806. }
  807. inline void RepeatedPtrFieldBase::MergeFromInternal(
  808. const RepeatedPtrFieldBase& other,
  809. void (RepeatedPtrFieldBase::*inner_loop)(void**, void**, int, int)) {
  810. // Note: wrapper has already guaranteed that other.rep_ != nullptr here.
  811. int other_size = other.current_size_;
  812. void** other_elements = other.rep_->elements;
  813. void** new_elements = InternalExtend(other_size);
  814. int allocated_elems = rep_->allocated_size - current_size_;
  815. (this->*inner_loop)(new_elements, other_elements, other_size,
  816. allocated_elems);
  817. current_size_ += other_size;
  818. if (rep_->allocated_size < current_size_) {
  819. rep_->allocated_size = current_size_;
  820. }
  821. }
  822. // Merges other_elems to our_elems.
  823. template <typename TypeHandler>
  824. void RepeatedPtrFieldBase::MergeFromInnerLoop(void** our_elems,
  825. void** other_elems, int length,
  826. int already_allocated) {
  827. if (already_allocated < length) {
  828. Arena* arena = GetArena();
  829. typename TypeHandler::Type* elem_prototype =
  830. reinterpret_cast<typename TypeHandler::Type*>(other_elems[0]);
  831. for (int i = already_allocated; i < length; i++) {
  832. // Allocate a new empty element that we'll merge into below
  833. typename TypeHandler::Type* new_elem =
  834. TypeHandler::NewFromPrototype(elem_prototype, arena);
  835. our_elems[i] = new_elem;
  836. }
  837. }
  838. // Main loop that does the actual merging
  839. for (int i = 0; i < length; i++) {
  840. // Already allocated: use existing element.
  841. typename TypeHandler::Type* other_elem =
  842. reinterpret_cast<typename TypeHandler::Type*>(other_elems[i]);
  843. typename TypeHandler::Type* new_elem =
  844. reinterpret_cast<typename TypeHandler::Type*>(our_elems[i]);
  845. TypeHandler::Merge(*other_elem, new_elem);
  846. }
  847. }
  848. template <typename TypeHandler>
  849. inline void RepeatedPtrFieldBase::CopyFrom(const RepeatedPtrFieldBase& other) {
  850. if (&other == this) return;
  851. RepeatedPtrFieldBase::Clear<TypeHandler>();
  852. RepeatedPtrFieldBase::MergeFrom<TypeHandler>(other);
  853. }
  854. inline int RepeatedPtrFieldBase::Capacity() const { return total_size_; }
  855. inline void* const* RepeatedPtrFieldBase::raw_data() const {
  856. return rep_ ? rep_->elements : nullptr;
  857. }
  858. inline void** RepeatedPtrFieldBase::raw_mutable_data() const {
  859. return rep_ ? const_cast<void**>(rep_->elements) : nullptr;
  860. }
  861. template <typename TypeHandler>
  862. inline typename TypeHandler::Type** RepeatedPtrFieldBase::mutable_data() {
  863. // TODO(kenton): Breaks C++ aliasing rules. We should probably remove this
  864. // method entirely.
  865. return reinterpret_cast<typename TypeHandler::Type**>(raw_mutable_data());
  866. }
  867. template <typename TypeHandler>
  868. inline const typename TypeHandler::Type* const* RepeatedPtrFieldBase::data()
  869. const {
  870. // TODO(kenton): Breaks C++ aliasing rules. We should probably remove this
  871. // method entirely.
  872. return reinterpret_cast<const typename TypeHandler::Type* const*>(raw_data());
  873. }
  874. inline void RepeatedPtrFieldBase::SwapElements(int index1, int index2) {
  875. using std::swap; // enable ADL with fallback
  876. swap(rep_->elements[index1], rep_->elements[index2]);
  877. }
  878. template <typename TypeHandler>
  879. inline size_t RepeatedPtrFieldBase::SpaceUsedExcludingSelfLong() const {
  880. size_t allocated_bytes = static_cast<size_t>(total_size_) * sizeof(void*);
  881. if (rep_ != nullptr) {
  882. for (int i = 0; i < rep_->allocated_size; ++i) {
  883. allocated_bytes +=
  884. TypeHandler::SpaceUsedLong(*cast<TypeHandler>(rep_->elements[i]));
  885. }
  886. allocated_bytes += kRepHeaderSize;
  887. }
  888. return allocated_bytes;
  889. }
  890. template <typename TypeHandler>
  891. inline typename TypeHandler::Type* RepeatedPtrFieldBase::AddFromCleared() {
  892. if (rep_ != nullptr && current_size_ < rep_->allocated_size) {
  893. return cast<TypeHandler>(rep_->elements[current_size_++]);
  894. } else {
  895. return nullptr;
  896. }
  897. }
  898. // AddAllocated version that implements arena-safe copying behavior.
  899. template <typename TypeHandler>
  900. void RepeatedPtrFieldBase::AddAllocatedInternal(
  901. typename TypeHandler::Type* value, std::true_type) {
  902. Arena* element_arena =
  903. reinterpret_cast<Arena*>(TypeHandler::GetOwningArena(value));
  904. Arena* arena = GetArena();
  905. if (arena == element_arena && rep_ && rep_->allocated_size < total_size_) {
  906. // Fast path: underlying arena representation (tagged pointer) is equal to
  907. // our arena pointer, and we can add to array without resizing it (at least
  908. // one slot that is not allocated).
  909. void** elems = rep_->elements;
  910. if (current_size_ < rep_->allocated_size) {
  911. // Make space at [current] by moving first allocated element to end of
  912. // allocated list.
  913. elems[rep_->allocated_size] = elems[current_size_];
  914. }
  915. elems[current_size_] = value;
  916. current_size_ = current_size_ + 1;
  917. rep_->allocated_size = rep_->allocated_size + 1;
  918. } else {
  919. AddAllocatedSlowWithCopy<TypeHandler>(value, element_arena, arena);
  920. }
  921. }
  922. // Slowpath handles all cases, copying if necessary.
  923. template <typename TypeHandler>
  924. void RepeatedPtrFieldBase::AddAllocatedSlowWithCopy(
  925. // Pass value_arena and my_arena to avoid duplicate virtual call (value) or
  926. // load (mine).
  927. typename TypeHandler::Type* value, Arena* value_arena, Arena* my_arena) {
  928. // Ensure that either the value is in the same arena, or if not, we do the
  929. // appropriate thing: Own() it (if it's on heap and we're in an arena) or copy
  930. // it to our arena/heap (otherwise).
  931. if (my_arena != nullptr && value_arena == nullptr) {
  932. my_arena->Own(value);
  933. } else if (my_arena != value_arena) {
  934. typename TypeHandler::Type* new_value =
  935. TypeHandler::NewFromPrototype(value, my_arena);
  936. TypeHandler::Merge(*value, new_value);
  937. TypeHandler::Delete(value, value_arena);
  938. value = new_value;
  939. }
  940. UnsafeArenaAddAllocated<TypeHandler>(value);
  941. }
  942. // AddAllocated version that does not implement arena-safe copying behavior.
  943. template <typename TypeHandler>
  944. void RepeatedPtrFieldBase::AddAllocatedInternal(
  945. typename TypeHandler::Type* value, std::false_type) {
  946. if (rep_ && rep_->allocated_size < total_size_) {
  947. // Fast path: underlying arena representation (tagged pointer) is equal to
  948. // our arena pointer, and we can add to array without resizing it (at least
  949. // one slot that is not allocated).
  950. void** elems = rep_->elements;
  951. if (current_size_ < rep_->allocated_size) {
  952. // Make space at [current] by moving first allocated element to end of
  953. // allocated list.
  954. elems[rep_->allocated_size] = elems[current_size_];
  955. }
  956. elems[current_size_] = value;
  957. current_size_ = current_size_ + 1;
  958. ++rep_->allocated_size;
  959. } else {
  960. UnsafeArenaAddAllocated<TypeHandler>(value);
  961. }
  962. }
  963. template <typename TypeHandler>
  964. void RepeatedPtrFieldBase::UnsafeArenaAddAllocated(
  965. typename TypeHandler::Type* value) {
  966. // Make room for the new pointer.
  967. if (!rep_ || current_size_ == total_size_) {
  968. // The array is completely full with no cleared objects, so grow it.
  969. Reserve(total_size_ + 1);
  970. ++rep_->allocated_size;
  971. } else if (rep_->allocated_size == total_size_) {
  972. // There is no more space in the pointer array because it contains some
  973. // cleared objects awaiting reuse. We don't want to grow the array in this
  974. // case because otherwise a loop calling AddAllocated() followed by Clear()
  975. // would leak memory.
  976. TypeHandler::Delete(cast<TypeHandler>(rep_->elements[current_size_]),
  977. arena_);
  978. } else if (current_size_ < rep_->allocated_size) {
  979. // We have some cleared objects. We don't care about their order, so we
  980. // can just move the first one to the end to make space.
  981. rep_->elements[rep_->allocated_size] = rep_->elements[current_size_];
  982. ++rep_->allocated_size;
  983. } else {
  984. // There are no cleared objects.
  985. ++rep_->allocated_size;
  986. }
  987. rep_->elements[current_size_++] = value;
  988. }
  989. // ReleaseLast() for types that implement merge/copy behavior.
  990. template <typename TypeHandler>
  991. inline typename TypeHandler::Type* RepeatedPtrFieldBase::ReleaseLastInternal(
  992. std::true_type) {
  993. // First, release an element.
  994. typename TypeHandler::Type* result = UnsafeArenaReleaseLast<TypeHandler>();
  995. // Now perform a copy if we're on an arena.
  996. Arena* arena = GetArena();
  997. typename TypeHandler::Type* new_result;
  998. #ifdef PROTOBUF_FORCE_COPY_IN_RELEASE
  999. new_result = copy<TypeHandler>(result);
  1000. if (arena == nullptr) delete result;
  1001. #else // PROTOBUF_FORCE_COPY_IN_RELEASE
  1002. new_result = (arena == nullptr) ? result : copy<TypeHandler>(result);
  1003. #endif // !PROTOBUF_FORCE_COPY_IN_RELEASE
  1004. return new_result;
  1005. }
  1006. // ReleaseLast() for types that *do not* implement merge/copy behavior -- this
  1007. // is the same as UnsafeArenaReleaseLast(). Note that we GOOGLE_DCHECK-fail if we're on
  1008. // an arena, since the user really should implement the copy operation in this
  1009. // case.
  1010. template <typename TypeHandler>
  1011. inline typename TypeHandler::Type* RepeatedPtrFieldBase::ReleaseLastInternal(
  1012. std::false_type) {
  1013. GOOGLE_DCHECK(GetArena() == nullptr)
  1014. << "ReleaseLast() called on a RepeatedPtrField that is on an arena, "
  1015. << "with a type that does not implement MergeFrom. This is unsafe; "
  1016. << "please implement MergeFrom for your type.";
  1017. return UnsafeArenaReleaseLast<TypeHandler>();
  1018. }
  1019. template <typename TypeHandler>
  1020. inline typename TypeHandler::Type*
  1021. RepeatedPtrFieldBase::UnsafeArenaReleaseLast() {
  1022. GOOGLE_DCHECK_GT(current_size_, 0);
  1023. typename TypeHandler::Type* result =
  1024. cast<TypeHandler>(rep_->elements[--current_size_]);
  1025. --rep_->allocated_size;
  1026. if (current_size_ < rep_->allocated_size) {
  1027. // There are cleared elements on the end; replace the removed element
  1028. // with the last allocated element.
  1029. rep_->elements[current_size_] = rep_->elements[rep_->allocated_size];
  1030. }
  1031. return result;
  1032. }
  1033. inline int RepeatedPtrFieldBase::ClearedCount() const {
  1034. return rep_ ? (rep_->allocated_size - current_size_) : 0;
  1035. }
  1036. template <typename TypeHandler>
  1037. inline void RepeatedPtrFieldBase::AddCleared(
  1038. typename TypeHandler::Type* value) {
  1039. GOOGLE_DCHECK(GetArena() == nullptr)
  1040. << "AddCleared() can only be used on a RepeatedPtrField not on an arena.";
  1041. GOOGLE_DCHECK(TypeHandler::GetOwningArena(value) == nullptr)
  1042. << "AddCleared() can only accept values not on an arena.";
  1043. if (!rep_ || rep_->allocated_size == total_size_) {
  1044. Reserve(total_size_ + 1);
  1045. }
  1046. rep_->elements[rep_->allocated_size++] = value;
  1047. }
  1048. template <typename TypeHandler>
  1049. inline typename TypeHandler::Type* RepeatedPtrFieldBase::ReleaseCleared() {
  1050. GOOGLE_DCHECK(GetArena() == nullptr)
  1051. << "ReleaseCleared() can only be used on a RepeatedPtrField not on "
  1052. << "an arena.";
  1053. GOOGLE_DCHECK(GetArena() == nullptr);
  1054. GOOGLE_DCHECK(rep_ != nullptr);
  1055. GOOGLE_DCHECK_GT(rep_->allocated_size, current_size_);
  1056. return cast<TypeHandler>(rep_->elements[--rep_->allocated_size]);
  1057. }
  1058. } // namespace internal
  1059. // -------------------------------------------------------------------
  1060. template <typename Element>
  1061. class RepeatedPtrField<Element>::TypeHandler
  1062. : public internal::GenericTypeHandler<Element> {};
  1063. template <>
  1064. class RepeatedPtrField<std::string>::TypeHandler
  1065. : public internal::StringTypeHandler {};
  1066. template <typename Element>
  1067. constexpr RepeatedPtrField<Element>::RepeatedPtrField()
  1068. : RepeatedPtrFieldBase() {}
  1069. template <typename Element>
  1070. inline RepeatedPtrField<Element>::RepeatedPtrField(Arena* arena)
  1071. : RepeatedPtrFieldBase(arena) {}
  1072. template <typename Element>
  1073. inline RepeatedPtrField<Element>::RepeatedPtrField(
  1074. const RepeatedPtrField& other)
  1075. : RepeatedPtrFieldBase() {
  1076. MergeFrom(other);
  1077. }
  1078. template <typename Element>
  1079. template <typename Iter, typename>
  1080. inline RepeatedPtrField<Element>::RepeatedPtrField(Iter begin, Iter end) {
  1081. Add(begin, end);
  1082. }
  1083. template <typename Element>
  1084. RepeatedPtrField<Element>::~RepeatedPtrField() {
  1085. #ifdef __cpp_if_constexpr
  1086. if constexpr (std::is_base_of<MessageLite, Element>::value) {
  1087. #else
  1088. if (std::is_base_of<MessageLite, Element>::value) {
  1089. #endif
  1090. if (NeedsDestroy()) DestroyProtos();
  1091. } else {
  1092. Destroy<TypeHandler>();
  1093. }
  1094. }
  1095. template <typename Element>
  1096. inline RepeatedPtrField<Element>& RepeatedPtrField<Element>::operator=(
  1097. const RepeatedPtrField& other) {
  1098. if (this != &other) CopyFrom(other);
  1099. return *this;
  1100. }
  1101. template <typename Element>
  1102. inline RepeatedPtrField<Element>::RepeatedPtrField(
  1103. RepeatedPtrField&& other) noexcept
  1104. : RepeatedPtrField() {
  1105. #ifdef PROTOBUF_FORCE_COPY_IN_MOVE
  1106. CopyFrom(other);
  1107. #else // PROTOBUF_FORCE_COPY_IN_MOVE
  1108. // We don't just call Swap(&other) here because it would perform 3 copies if
  1109. // other is on an arena. This field can't be on an arena because arena
  1110. // construction always uses the Arena* accepting constructor.
  1111. if (other.GetArena()) {
  1112. CopyFrom(other);
  1113. } else {
  1114. InternalSwap(&other);
  1115. }
  1116. #endif // !PROTOBUF_FORCE_COPY_IN_MOVE
  1117. }
  1118. template <typename Element>
  1119. inline RepeatedPtrField<Element>& RepeatedPtrField<Element>::operator=(
  1120. RepeatedPtrField&& other) noexcept {
  1121. // We don't just call Swap(&other) here because it would perform 3 copies if
  1122. // the two fields are on different arenas.
  1123. if (this != &other) {
  1124. if (GetArena() != other.GetArena()
  1125. #ifdef PROTOBUF_FORCE_COPY_IN_MOVE
  1126. || GetArena() == nullptr
  1127. #endif // !PROTOBUF_FORCE_COPY_IN_MOVE
  1128. ) {
  1129. CopyFrom(other);
  1130. } else {
  1131. InternalSwap(&other);
  1132. }
  1133. }
  1134. return *this;
  1135. }
  1136. template <typename Element>
  1137. inline bool RepeatedPtrField<Element>::empty() const {
  1138. return RepeatedPtrFieldBase::empty();
  1139. }
  1140. template <typename Element>
  1141. inline int RepeatedPtrField<Element>::size() const {
  1142. return RepeatedPtrFieldBase::size();
  1143. }
  1144. template <typename Element>
  1145. inline const Element& RepeatedPtrField<Element>::Get(int index) const {
  1146. return RepeatedPtrFieldBase::Get<TypeHandler>(index);
  1147. }
  1148. template <typename Element>
  1149. inline const Element& RepeatedPtrField<Element>::at(int index) const {
  1150. return RepeatedPtrFieldBase::at<TypeHandler>(index);
  1151. }
  1152. template <typename Element>
  1153. inline Element& RepeatedPtrField<Element>::at(int index) {
  1154. return RepeatedPtrFieldBase::at<TypeHandler>(index);
  1155. }
  1156. template <typename Element>
  1157. inline Element* RepeatedPtrField<Element>::Mutable(int index) {
  1158. return RepeatedPtrFieldBase::Mutable<TypeHandler>(index);
  1159. }
  1160. template <typename Element>
  1161. inline Element* RepeatedPtrField<Element>::Add() {
  1162. return RepeatedPtrFieldBase::Add<TypeHandler>();
  1163. }
  1164. template <typename Element>
  1165. inline void RepeatedPtrField<Element>::Add(Element&& value) {
  1166. RepeatedPtrFieldBase::Add<TypeHandler>(std::move(value));
  1167. }
  1168. template <typename Element>
  1169. template <typename Iter>
  1170. inline void RepeatedPtrField<Element>::Add(Iter begin, Iter end) {
  1171. if (std::is_base_of<
  1172. std::forward_iterator_tag,
  1173. typename std::iterator_traits<Iter>::iterator_category>::value) {
  1174. int reserve = std::distance(begin, end);
  1175. Reserve(size() + reserve);
  1176. }
  1177. for (; begin != end; ++begin) {
  1178. *Add() = *begin;
  1179. }
  1180. }
  1181. template <typename Element>
  1182. inline void RepeatedPtrField<Element>::RemoveLast() {
  1183. RepeatedPtrFieldBase::RemoveLast<TypeHandler>();
  1184. }
  1185. template <typename Element>
  1186. inline void RepeatedPtrField<Element>::DeleteSubrange(int start, int num) {
  1187. GOOGLE_DCHECK_GE(start, 0);
  1188. GOOGLE_DCHECK_GE(num, 0);
  1189. GOOGLE_DCHECK_LE(start + num, size());
  1190. for (int i = 0; i < num; ++i) {
  1191. RepeatedPtrFieldBase::Delete<TypeHandler>(start + i);
  1192. }
  1193. UnsafeArenaExtractSubrange(start, num, nullptr);
  1194. }
  1195. template <typename Element>
  1196. inline void RepeatedPtrField<Element>::ExtractSubrange(int start, int num,
  1197. Element** elements) {
  1198. typename internal::TypeImplementsMergeBehavior<
  1199. typename TypeHandler::Type>::type t;
  1200. ExtractSubrangeInternal(start, num, elements, t);
  1201. }
  1202. // ExtractSubrange() implementation for types that implement merge/copy
  1203. // behavior.
  1204. template <typename Element>
  1205. inline void RepeatedPtrField<Element>::ExtractSubrangeInternal(
  1206. int start, int num, Element** elements, std::true_type) {
  1207. GOOGLE_DCHECK_GE(start, 0);
  1208. GOOGLE_DCHECK_GE(num, 0);
  1209. GOOGLE_DCHECK_LE(start + num, size());
  1210. if (num == 0) return;
  1211. GOOGLE_DCHECK_NE(elements, nullptr)
  1212. << "Releasing elements without transferring ownership is an unsafe "
  1213. "operation. Use UnsafeArenaExtractSubrange.";
  1214. if (elements == nullptr) {
  1215. CloseGap(start, num);
  1216. return;
  1217. }
  1218. Arena* arena = GetArena();
  1219. #ifdef PROTOBUF_FORCE_COPY_IN_RELEASE
  1220. // Always copy.
  1221. for (int i = 0; i < num; ++i) {
  1222. elements[i] = copy<TypeHandler>(
  1223. RepeatedPtrFieldBase::Mutable<TypeHandler>(i + start));
  1224. }
  1225. if (arena == nullptr) {
  1226. for (int i = 0; i < num; ++i) {
  1227. delete RepeatedPtrFieldBase::Mutable<TypeHandler>(i + start);
  1228. }
  1229. }
  1230. #else // PROTOBUF_FORCE_COPY_IN_RELEASE
  1231. // If we're on an arena, we perform a copy for each element so that the
  1232. // returned elements are heap-allocated. Otherwise, just forward it.
  1233. if (arena != nullptr) {
  1234. for (int i = 0; i < num; ++i) {
  1235. elements[i] = copy<TypeHandler>(
  1236. RepeatedPtrFieldBase::Mutable<TypeHandler>(i + start));
  1237. }
  1238. } else {
  1239. for (int i = 0; i < num; ++i) {
  1240. elements[i] = RepeatedPtrFieldBase::Mutable<TypeHandler>(i + start);
  1241. }
  1242. }
  1243. #endif // !PROTOBUF_FORCE_COPY_IN_RELEASE
  1244. CloseGap(start, num);
  1245. }
  1246. // ExtractSubrange() implementation for types that do not implement merge/copy
  1247. // behavior.
  1248. template <typename Element>
  1249. inline void RepeatedPtrField<Element>::ExtractSubrangeInternal(
  1250. int start, int num, Element** elements, std::false_type) {
  1251. // This case is identical to UnsafeArenaExtractSubrange(). However, since
  1252. // ExtractSubrange() must return heap-allocated objects by contract, and we
  1253. // cannot fulfill this contract if we are an on arena, we must GOOGLE_DCHECK() that
  1254. // we are not on an arena.
  1255. GOOGLE_DCHECK(GetArena() == nullptr)
  1256. << "ExtractSubrange() when arena is non-nullptr is only supported when "
  1257. << "the Element type supplies a MergeFrom() operation to make copies.";
  1258. UnsafeArenaExtractSubrange(start, num, elements);
  1259. }
  1260. template <typename Element>
  1261. inline void RepeatedPtrField<Element>::UnsafeArenaExtractSubrange(
  1262. int start, int num, Element** elements) {
  1263. GOOGLE_DCHECK_GE(start, 0);
  1264. GOOGLE_DCHECK_GE(num, 0);
  1265. GOOGLE_DCHECK_LE(start + num, size());
  1266. if (num > 0) {
  1267. // Save the values of the removed elements if requested.
  1268. if (elements != nullptr) {
  1269. for (int i = 0; i < num; ++i) {
  1270. elements[i] = RepeatedPtrFieldBase::Mutable<TypeHandler>(i + start);
  1271. }
  1272. }
  1273. CloseGap(start, num);
  1274. }
  1275. }
  1276. template <typename Element>
  1277. inline void RepeatedPtrField<Element>::Clear() {
  1278. RepeatedPtrFieldBase::Clear<TypeHandler>();
  1279. }
  1280. template <typename Element>
  1281. inline void RepeatedPtrField<Element>::MergeFrom(
  1282. const RepeatedPtrField& other) {
  1283. RepeatedPtrFieldBase::MergeFrom<TypeHandler>(other);
  1284. }
  1285. template <typename Element>
  1286. inline void RepeatedPtrField<Element>::CopyFrom(const RepeatedPtrField& other) {
  1287. RepeatedPtrFieldBase::CopyFrom<TypeHandler>(other);
  1288. }
  1289. template <typename Element>
  1290. template <typename Iter>
  1291. inline void RepeatedPtrField<Element>::Assign(Iter begin, Iter end) {
  1292. Clear();
  1293. Add(begin, end);
  1294. }
  1295. template <typename Element>
  1296. inline typename RepeatedPtrField<Element>::iterator
  1297. RepeatedPtrField<Element>::erase(const_iterator position) {
  1298. return erase(position, position + 1);
  1299. }
  1300. template <typename Element>
  1301. inline typename RepeatedPtrField<Element>::iterator
  1302. RepeatedPtrField<Element>::erase(const_iterator first, const_iterator last) {
  1303. size_type pos_offset = std::distance(cbegin(), first);
  1304. size_type last_offset = std::distance(cbegin(), last);
  1305. DeleteSubrange(pos_offset, last_offset - pos_offset);
  1306. return begin() + pos_offset;
  1307. }
  1308. template <typename Element>
  1309. inline Element** RepeatedPtrField<Element>::mutable_data() {
  1310. return RepeatedPtrFieldBase::mutable_data<TypeHandler>();
  1311. }
  1312. template <typename Element>
  1313. inline const Element* const* RepeatedPtrField<Element>::data() const {
  1314. return RepeatedPtrFieldBase::data<TypeHandler>();
  1315. }
  1316. template <typename Element>
  1317. inline void RepeatedPtrField<Element>::Swap(RepeatedPtrField* other) {
  1318. if (this == other) return;
  1319. RepeatedPtrFieldBase::Swap<TypeHandler>(other);
  1320. }
  1321. template <typename Element>
  1322. inline void RepeatedPtrField<Element>::UnsafeArenaSwap(
  1323. RepeatedPtrField* other) {
  1324. if (this == other) return;
  1325. RepeatedPtrFieldBase::InternalSwap(other);
  1326. }
  1327. template <typename Element>
  1328. inline void RepeatedPtrField<Element>::SwapElements(int index1, int index2) {
  1329. RepeatedPtrFieldBase::SwapElements(index1, index2);
  1330. }
  1331. template <typename Element>
  1332. inline Arena* RepeatedPtrField<Element>::GetArena() const {
  1333. return RepeatedPtrFieldBase::GetArena();
  1334. }
  1335. template <typename Element>
  1336. inline size_t RepeatedPtrField<Element>::SpaceUsedExcludingSelfLong() const {
  1337. return RepeatedPtrFieldBase::SpaceUsedExcludingSelfLong<TypeHandler>();
  1338. }
  1339. template <typename Element>
  1340. inline void RepeatedPtrField<Element>::AddAllocated(Element* value) {
  1341. RepeatedPtrFieldBase::AddAllocated<TypeHandler>(value);
  1342. }
  1343. template <typename Element>
  1344. inline void RepeatedPtrField<Element>::UnsafeArenaAddAllocated(Element* value) {
  1345. RepeatedPtrFieldBase::UnsafeArenaAddAllocated<TypeHandler>(value);
  1346. }
  1347. template <typename Element>
  1348. inline Element* RepeatedPtrField<Element>::ReleaseLast() {
  1349. return RepeatedPtrFieldBase::ReleaseLast<TypeHandler>();
  1350. }
  1351. template <typename Element>
  1352. inline Element* RepeatedPtrField<Element>::UnsafeArenaReleaseLast() {
  1353. return RepeatedPtrFieldBase::UnsafeArenaReleaseLast<TypeHandler>();
  1354. }
  1355. template <typename Element>
  1356. inline int RepeatedPtrField<Element>::ClearedCount() const {
  1357. return RepeatedPtrFieldBase::ClearedCount();
  1358. }
  1359. #ifndef PROTOBUF_FUTURE_BREAKING_CHANGES
  1360. template <typename Element>
  1361. inline void RepeatedPtrField<Element>::AddCleared(Element* value) {
  1362. return RepeatedPtrFieldBase::AddCleared<TypeHandler>(value);
  1363. }
  1364. template <typename Element>
  1365. inline Element* RepeatedPtrField<Element>::ReleaseCleared() {
  1366. return RepeatedPtrFieldBase::ReleaseCleared<TypeHandler>();
  1367. }
  1368. #endif // !PROTOBUF_FUTURE_BREAKING_CHANGES
  1369. template <typename Element>
  1370. inline void RepeatedPtrField<Element>::Reserve(int new_size) {
  1371. return RepeatedPtrFieldBase::Reserve(new_size);
  1372. }
  1373. template <typename Element>
  1374. inline int RepeatedPtrField<Element>::Capacity() const {
  1375. return RepeatedPtrFieldBase::Capacity();
  1376. }
  1377. // -------------------------------------------------------------------
  1378. namespace internal {
  1379. // STL-like iterator implementation for RepeatedPtrField. You should not
  1380. // refer to this class directly; use RepeatedPtrField<T>::iterator instead.
  1381. //
  1382. // The iterator for RepeatedPtrField<T>, RepeatedPtrIterator<T>, is
  1383. // very similar to iterator_ptr<T**> in util/gtl/iterator_adaptors.h,
  1384. // but adds random-access operators and is modified to wrap a void** base
  1385. // iterator (since RepeatedPtrField stores its array as a void* array and
  1386. // casting void** to T** would violate C++ aliasing rules).
  1387. //
  1388. // This code based on net/proto/proto-array-internal.h by Jeffrey Yasskin
  1389. // (jyasskin@google.com).
  1390. template <typename Element>
  1391. class RepeatedPtrIterator {
  1392. public:
  1393. using iterator = RepeatedPtrIterator<Element>;
  1394. using iterator_category = std::random_access_iterator_tag;
  1395. using value_type = typename std::remove_const<Element>::type;
  1396. using difference_type = std::ptrdiff_t;
  1397. using pointer = Element*;
  1398. using reference = Element&;
  1399. RepeatedPtrIterator() : it_(nullptr) {}
  1400. explicit RepeatedPtrIterator(void* const* it) : it_(it) {}
  1401. // Allow "upcasting" from RepeatedPtrIterator<T**> to
  1402. // RepeatedPtrIterator<const T*const*>.
  1403. template <typename OtherElement>
  1404. RepeatedPtrIterator(const RepeatedPtrIterator<OtherElement>& other)
  1405. : it_(other.it_) {
  1406. // Force a compiler error if the other type is not convertible to ours.
  1407. if (false) {
  1408. static_cast<void>([](OtherElement* from) -> Element* { return from; });
  1409. }
  1410. }
  1411. // dereferenceable
  1412. reference operator*() const { return *reinterpret_cast<Element*>(*it_); }
  1413. pointer operator->() const { return &(operator*()); }
  1414. // {inc,dec}rementable
  1415. iterator& operator++() {
  1416. ++it_;
  1417. return *this;
  1418. }
  1419. iterator operator++(int) { return iterator(it_++); }
  1420. iterator& operator--() {
  1421. --it_;
  1422. return *this;
  1423. }
  1424. iterator operator--(int) { return iterator(it_--); }
  1425. // equality_comparable
  1426. bool operator==(const iterator& x) const { return it_ == x.it_; }
  1427. bool operator!=(const iterator& x) const { return it_ != x.it_; }
  1428. // less_than_comparable
  1429. bool operator<(const iterator& x) const { return it_ < x.it_; }
  1430. bool operator<=(const iterator& x) const { return it_ <= x.it_; }
  1431. bool operator>(const iterator& x) const { return it_ > x.it_; }
  1432. bool operator>=(const iterator& x) const { return it_ >= x.it_; }
  1433. // addable, subtractable
  1434. iterator& operator+=(difference_type d) {
  1435. it_ += d;
  1436. return *this;
  1437. }
  1438. friend iterator operator+(iterator it, const difference_type d) {
  1439. it += d;
  1440. return it;
  1441. }
  1442. friend iterator operator+(const difference_type d, iterator it) {
  1443. it += d;
  1444. return it;
  1445. }
  1446. iterator& operator-=(difference_type d) {
  1447. it_ -= d;
  1448. return *this;
  1449. }
  1450. friend iterator operator-(iterator it, difference_type d) {
  1451. it -= d;
  1452. return it;
  1453. }
  1454. // indexable
  1455. reference operator[](difference_type d) const { return *(*this + d); }
  1456. // random access iterator
  1457. difference_type operator-(const iterator& x) const { return it_ - x.it_; }
  1458. private:
  1459. template <typename OtherElement>
  1460. friend class RepeatedPtrIterator;
  1461. // The internal iterator.
  1462. void* const* it_;
  1463. };
  1464. // Provide an iterator that operates on pointers to the underlying objects
  1465. // rather than the objects themselves as RepeatedPtrIterator does.
  1466. // Consider using this when working with stl algorithms that change
  1467. // the array.
  1468. // The VoidPtr template parameter holds the type-agnostic pointer value
  1469. // referenced by the iterator. It should either be "void *" for a mutable
  1470. // iterator, or "const void* const" for a constant iterator.
  1471. template <typename Element, typename VoidPtr>
  1472. class RepeatedPtrOverPtrsIterator {
  1473. public:
  1474. using iterator = RepeatedPtrOverPtrsIterator<Element, VoidPtr>;
  1475. using iterator_category = std::random_access_iterator_tag;
  1476. using value_type = typename std::remove_const<Element>::type;
  1477. using difference_type = std::ptrdiff_t;
  1478. using pointer = Element*;
  1479. using reference = Element&;
  1480. RepeatedPtrOverPtrsIterator() : it_(nullptr) {}
  1481. explicit RepeatedPtrOverPtrsIterator(VoidPtr* it) : it_(it) {}
  1482. // dereferenceable
  1483. reference operator*() const { return *reinterpret_cast<Element*>(it_); }
  1484. pointer operator->() const { return &(operator*()); }
  1485. // {inc,dec}rementable
  1486. iterator& operator++() {
  1487. ++it_;
  1488. return *this;
  1489. }
  1490. iterator operator++(int) { return iterator(it_++); }
  1491. iterator& operator--() {
  1492. --it_;
  1493. return *this;
  1494. }
  1495. iterator operator--(int) { return iterator(it_--); }
  1496. // equality_comparable
  1497. bool operator==(const iterator& x) const { return it_ == x.it_; }
  1498. bool operator!=(const iterator& x) const { return it_ != x.it_; }
  1499. // less_than_comparable
  1500. bool operator<(const iterator& x) const { return it_ < x.it_; }
  1501. bool operator<=(const iterator& x) const { return it_ <= x.it_; }
  1502. bool operator>(const iterator& x) const { return it_ > x.it_; }
  1503. bool operator>=(const iterator& x) const { return it_ >= x.it_; }
  1504. // addable, subtractable
  1505. iterator& operator+=(difference_type d) {
  1506. it_ += d;
  1507. return *this;
  1508. }
  1509. friend iterator operator+(iterator it, difference_type d) {
  1510. it += d;
  1511. return it;
  1512. }
  1513. friend iterator operator+(difference_type d, iterator it) {
  1514. it += d;
  1515. return it;
  1516. }
  1517. iterator& operator-=(difference_type d) {
  1518. it_ -= d;
  1519. return *this;
  1520. }
  1521. friend iterator operator-(iterator it, difference_type d) {
  1522. it -= d;
  1523. return it;
  1524. }
  1525. // indexable
  1526. reference operator[](difference_type d) const { return *(*this + d); }
  1527. // random access iterator
  1528. difference_type operator-(const iterator& x) const { return it_ - x.it_; }
  1529. private:
  1530. template <typename OtherElement>
  1531. friend class RepeatedPtrIterator;
  1532. // The internal iterator.
  1533. VoidPtr* it_;
  1534. };
  1535. void RepeatedPtrFieldBase::InternalSwap(RepeatedPtrFieldBase* rhs) {
  1536. GOOGLE_DCHECK(this != rhs);
  1537. // Swap all fields at once.
  1538. auto temp = std::make_tuple(rhs->arena_, rhs->current_size_, rhs->total_size_,
  1539. rhs->rep_);
  1540. std::tie(rhs->arena_, rhs->current_size_, rhs->total_size_, rhs->rep_) =
  1541. std::make_tuple(arena_, current_size_, total_size_, rep_);
  1542. std::tie(arena_, current_size_, total_size_, rep_) = temp;
  1543. }
  1544. } // namespace internal
  1545. template <typename Element>
  1546. inline typename RepeatedPtrField<Element>::iterator
  1547. RepeatedPtrField<Element>::begin() {
  1548. return iterator(raw_data());
  1549. }
  1550. template <typename Element>
  1551. inline typename RepeatedPtrField<Element>::const_iterator
  1552. RepeatedPtrField<Element>::begin() const {
  1553. return iterator(raw_data());
  1554. }
  1555. template <typename Element>
  1556. inline typename RepeatedPtrField<Element>::const_iterator
  1557. RepeatedPtrField<Element>::cbegin() const {
  1558. return begin();
  1559. }
  1560. template <typename Element>
  1561. inline typename RepeatedPtrField<Element>::iterator
  1562. RepeatedPtrField<Element>::end() {
  1563. return iterator(raw_data() + size());
  1564. }
  1565. template <typename Element>
  1566. inline typename RepeatedPtrField<Element>::const_iterator
  1567. RepeatedPtrField<Element>::end() const {
  1568. return iterator(raw_data() + size());
  1569. }
  1570. template <typename Element>
  1571. inline typename RepeatedPtrField<Element>::const_iterator
  1572. RepeatedPtrField<Element>::cend() const {
  1573. return end();
  1574. }
  1575. template <typename Element>
  1576. inline typename RepeatedPtrField<Element>::pointer_iterator
  1577. RepeatedPtrField<Element>::pointer_begin() {
  1578. return pointer_iterator(raw_mutable_data());
  1579. }
  1580. template <typename Element>
  1581. inline typename RepeatedPtrField<Element>::const_pointer_iterator
  1582. RepeatedPtrField<Element>::pointer_begin() const {
  1583. return const_pointer_iterator(const_cast<const void* const*>(raw_data()));
  1584. }
  1585. template <typename Element>
  1586. inline typename RepeatedPtrField<Element>::pointer_iterator
  1587. RepeatedPtrField<Element>::pointer_end() {
  1588. return pointer_iterator(raw_mutable_data() + size());
  1589. }
  1590. template <typename Element>
  1591. inline typename RepeatedPtrField<Element>::const_pointer_iterator
  1592. RepeatedPtrField<Element>::pointer_end() const {
  1593. return const_pointer_iterator(
  1594. const_cast<const void* const*>(raw_data() + size()));
  1595. }
  1596. // Iterators and helper functions that follow the spirit of the STL
  1597. // std::back_insert_iterator and std::back_inserter but are tailor-made
  1598. // for RepeatedField and RepeatedPtrField. Typical usage would be:
  1599. //
  1600. // std::copy(some_sequence.begin(), some_sequence.end(),
  1601. // RepeatedFieldBackInserter(proto.mutable_sequence()));
  1602. //
  1603. // Ported by johannes from util/gtl/proto-array-iterators.h
  1604. namespace internal {
  1605. // A back inserter for RepeatedPtrField objects.
  1606. template <typename T>
  1607. class RepeatedPtrFieldBackInsertIterator {
  1608. public:
  1609. using iterator_category = std::output_iterator_tag;
  1610. using value_type = T;
  1611. using pointer = void;
  1612. using reference = void;
  1613. using difference_type = std::ptrdiff_t;
  1614. RepeatedPtrFieldBackInsertIterator(RepeatedPtrField<T>* const mutable_field)
  1615. : field_(mutable_field) {}
  1616. RepeatedPtrFieldBackInsertIterator<T>& operator=(const T& value) {
  1617. *field_->Add() = value;
  1618. return *this;
  1619. }
  1620. RepeatedPtrFieldBackInsertIterator<T>& operator=(
  1621. const T* const ptr_to_value) {
  1622. *field_->Add() = *ptr_to_value;
  1623. return *this;
  1624. }
  1625. RepeatedPtrFieldBackInsertIterator<T>& operator=(T&& value) {
  1626. *field_->Add() = std::move(value);
  1627. return *this;
  1628. }
  1629. RepeatedPtrFieldBackInsertIterator<T>& operator*() { return *this; }
  1630. RepeatedPtrFieldBackInsertIterator<T>& operator++() { return *this; }
  1631. RepeatedPtrFieldBackInsertIterator<T>& operator++(int /* unused */) {
  1632. return *this;
  1633. }
  1634. private:
  1635. RepeatedPtrField<T>* field_;
  1636. };
  1637. // A back inserter for RepeatedPtrFields that inserts by transferring ownership
  1638. // of a pointer.
  1639. template <typename T>
  1640. class AllocatedRepeatedPtrFieldBackInsertIterator {
  1641. public:
  1642. using iterator_category = std::output_iterator_tag;
  1643. using value_type = T;
  1644. using pointer = void;
  1645. using reference = void;
  1646. using difference_type = std::ptrdiff_t;
  1647. explicit AllocatedRepeatedPtrFieldBackInsertIterator(
  1648. RepeatedPtrField<T>* const mutable_field)
  1649. : field_(mutable_field) {}
  1650. AllocatedRepeatedPtrFieldBackInsertIterator<T>& operator=(
  1651. T* const ptr_to_value) {
  1652. field_->AddAllocated(ptr_to_value);
  1653. return *this;
  1654. }
  1655. AllocatedRepeatedPtrFieldBackInsertIterator<T>& operator*() { return *this; }
  1656. AllocatedRepeatedPtrFieldBackInsertIterator<T>& operator++() { return *this; }
  1657. AllocatedRepeatedPtrFieldBackInsertIterator<T>& operator++(int /* unused */) {
  1658. return *this;
  1659. }
  1660. private:
  1661. RepeatedPtrField<T>* field_;
  1662. };
  1663. // Almost identical to AllocatedRepeatedPtrFieldBackInsertIterator. This one
  1664. // uses the UnsafeArenaAddAllocated instead.
  1665. template <typename T>
  1666. class UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator {
  1667. public:
  1668. using iterator_category = std::output_iterator_tag;
  1669. using value_type = T;
  1670. using pointer = void;
  1671. using reference = void;
  1672. using difference_type = std::ptrdiff_t;
  1673. explicit UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator(
  1674. RepeatedPtrField<T>* const mutable_field)
  1675. : field_(mutable_field) {}
  1676. UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator<T>& operator=(
  1677. T const* const ptr_to_value) {
  1678. field_->UnsafeArenaAddAllocated(const_cast<T*>(ptr_to_value));
  1679. return *this;
  1680. }
  1681. UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator<T>& operator*() {
  1682. return *this;
  1683. }
  1684. UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator<T>& operator++() {
  1685. return *this;
  1686. }
  1687. UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator<T>& operator++(
  1688. int /* unused */) {
  1689. return *this;
  1690. }
  1691. private:
  1692. RepeatedPtrField<T>* field_;
  1693. };
  1694. } // namespace internal
  1695. // Provides a back insert iterator for RepeatedPtrField instances,
  1696. // similar to std::back_inserter().
  1697. template <typename T>
  1698. internal::RepeatedPtrFieldBackInsertIterator<T> RepeatedPtrFieldBackInserter(
  1699. RepeatedPtrField<T>* const mutable_field) {
  1700. return internal::RepeatedPtrFieldBackInsertIterator<T>(mutable_field);
  1701. }
  1702. // Special back insert iterator for RepeatedPtrField instances, just in
  1703. // case someone wants to write generic template code that can access both
  1704. // RepeatedFields and RepeatedPtrFields using a common name.
  1705. template <typename T>
  1706. internal::RepeatedPtrFieldBackInsertIterator<T> RepeatedFieldBackInserter(
  1707. RepeatedPtrField<T>* const mutable_field) {
  1708. return internal::RepeatedPtrFieldBackInsertIterator<T>(mutable_field);
  1709. }
  1710. // Provides a back insert iterator for RepeatedPtrField instances
  1711. // similar to std::back_inserter() which transfers the ownership while
  1712. // copying elements.
  1713. template <typename T>
  1714. internal::AllocatedRepeatedPtrFieldBackInsertIterator<T>
  1715. AllocatedRepeatedPtrFieldBackInserter(
  1716. RepeatedPtrField<T>* const mutable_field) {
  1717. return internal::AllocatedRepeatedPtrFieldBackInsertIterator<T>(
  1718. mutable_field);
  1719. }
  1720. // Similar to AllocatedRepeatedPtrFieldBackInserter, using
  1721. // UnsafeArenaAddAllocated instead of AddAllocated.
  1722. // This is slightly faster if that matters. It is also useful in legacy code
  1723. // that uses temporary ownership to avoid copies. Example:
  1724. // RepeatedPtrField<T> temp_field;
  1725. // temp_field.UnsafeArenaAddAllocated(new T);
  1726. // ... // Do something with temp_field
  1727. // temp_field.UnsafeArenaExtractSubrange(0, temp_field.size(), nullptr);
  1728. // If you put temp_field on the arena this fails, because the ownership
  1729. // transfers to the arena at the "AddAllocated" call and is not released anymore
  1730. // causing a double delete. Using UnsafeArenaAddAllocated prevents this.
  1731. template <typename T>
  1732. internal::UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator<T>
  1733. UnsafeArenaAllocatedRepeatedPtrFieldBackInserter(
  1734. RepeatedPtrField<T>* const mutable_field) {
  1735. return internal::UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator<T>(
  1736. mutable_field);
  1737. }
  1738. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE
  1739. RepeatedPtrField<std::string>;
  1740. } // namespace protobuf
  1741. } // namespace google
  1742. #include <google/protobuf/port_undef.inc>
  1743. #endif // GOOGLE_PROTOBUF_REPEATED_PTR_FIELD_H__