repeated_field.h 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950
  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. #ifndef GOOGLE_PROTOBUF_REPEATED_FIELD_H__
  45. #define GOOGLE_PROTOBUF_REPEATED_FIELD_H__
  46. #include <utility>
  47. #ifdef _MSC_VER
  48. // This is required for min/max on VS2013 only.
  49. #include <algorithm>
  50. #endif
  51. #include <iterator>
  52. #include <limits>
  53. #include <string>
  54. #include <type_traits>
  55. #include <google/protobuf/stubs/logging.h>
  56. #include <google/protobuf/stubs/common.h>
  57. #include <google/protobuf/arena.h>
  58. #include <google/protobuf/message_lite.h>
  59. #include <google/protobuf/port.h>
  60. #include <google/protobuf/stubs/casts.h>
  61. #include <type_traits>
  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. // kRepeatedFieldLowerClampLimit is the smallest size that will be allocated
  77. // when growing a repeated field.
  78. constexpr int kRepeatedFieldLowerClampLimit = 4;
  79. // kRepeatedFieldUpperClampLimit is the lowest signed integer value that
  80. // overflows when multiplied by 2 (which is undefined behavior). Sizes above
  81. // this will clamp to the maximum int value instead of following exponential
  82. // growth when growing a repeated field.
  83. constexpr int kRepeatedFieldUpperClampLimit =
  84. (std::numeric_limits<int>::max() / 2) + 1;
  85. // A utility function for logging that doesn't need any template types.
  86. void LogIndexOutOfBounds(int index, int size);
  87. template <typename Iter>
  88. inline int CalculateReserve(Iter begin, Iter end, std::forward_iterator_tag) {
  89. return static_cast<int>(std::distance(begin, end));
  90. }
  91. template <typename Iter>
  92. inline int CalculateReserve(Iter /*begin*/, Iter /*end*/,
  93. std::input_iterator_tag /*unused*/) {
  94. return -1;
  95. }
  96. template <typename Iter>
  97. inline int CalculateReserve(Iter begin, Iter end) {
  98. typedef typename std::iterator_traits<Iter>::iterator_category Category;
  99. return CalculateReserve(begin, end, Category());
  100. }
  101. // Swaps two blocks of memory of size sizeof(T).
  102. template <typename T>
  103. inline void SwapBlock(char* p, char* q) {
  104. T tmp;
  105. memcpy(&tmp, p, sizeof(T));
  106. memcpy(p, q, sizeof(T));
  107. memcpy(q, &tmp, sizeof(T));
  108. }
  109. // Swaps two blocks of memory of size kSize:
  110. // template <int kSize> void memswap(char* p, char* q);
  111. template <int kSize>
  112. inline typename std::enable_if<(kSize == 0), void>::type memswap(char*, char*) {
  113. }
  114. #define PROTO_MEMSWAP_DEF_SIZE(reg_type, max_size) \
  115. template <int kSize> \
  116. typename std::enable_if<(kSize >= sizeof(reg_type) && kSize < (max_size)), \
  117. void>::type \
  118. memswap(char* p, char* q) { \
  119. SwapBlock<reg_type>(p, q); \
  120. memswap<kSize - sizeof(reg_type)>(p + sizeof(reg_type), \
  121. q + sizeof(reg_type)); \
  122. }
  123. PROTO_MEMSWAP_DEF_SIZE(uint8_t, 2)
  124. PROTO_MEMSWAP_DEF_SIZE(uint16_t, 4)
  125. PROTO_MEMSWAP_DEF_SIZE(uint32_t, 8)
  126. #ifdef __SIZEOF_INT128__
  127. PROTO_MEMSWAP_DEF_SIZE(uint64_t, 16)
  128. PROTO_MEMSWAP_DEF_SIZE(__uint128_t, (1u << 31))
  129. #else
  130. PROTO_MEMSWAP_DEF_SIZE(uint64_t, (1u << 31))
  131. #endif
  132. #undef PROTO_MEMSWAP_DEF_SIZE
  133. } // namespace internal
  134. // RepeatedField is used to represent repeated fields of a primitive type (in
  135. // other words, everything except strings and nested Messages). Most users will
  136. // not ever use a RepeatedField directly; they will use the get-by-index,
  137. // set-by-index, and add accessors that are generated for all repeated fields.
  138. template <typename Element>
  139. class RepeatedField final {
  140. static_assert(
  141. alignof(Arena) >= alignof(Element),
  142. "We only support types that have an alignment smaller than Arena");
  143. public:
  144. constexpr RepeatedField();
  145. explicit RepeatedField(Arena* arena);
  146. RepeatedField(const RepeatedField& other);
  147. template <typename Iter,
  148. typename = typename std::enable_if<std::is_constructible<
  149. Element, decltype(*std::declval<Iter>())>::value>::type>
  150. RepeatedField(Iter begin, Iter end);
  151. ~RepeatedField();
  152. RepeatedField& operator=(const RepeatedField& other);
  153. RepeatedField(RepeatedField&& other) noexcept;
  154. RepeatedField& operator=(RepeatedField&& other) noexcept;
  155. bool empty() const;
  156. int size() const;
  157. const Element& Get(int index) const;
  158. Element* Mutable(int index);
  159. const Element& operator[](int index) const { return Get(index); }
  160. Element& operator[](int index) { return *Mutable(index); }
  161. const Element& at(int index) const;
  162. Element& at(int index);
  163. void Set(int index, const Element& value);
  164. void Add(const Element& value);
  165. // Appends a new element and return a pointer to it.
  166. // The new element is uninitialized if |Element| is a POD type.
  167. Element* Add();
  168. // Append elements in the range [begin, end) after reserving
  169. // the appropriate number of elements.
  170. template <typename Iter>
  171. void Add(Iter begin, Iter end);
  172. // Remove the last element in the array.
  173. void RemoveLast();
  174. // Extract elements with indices in "[start .. start+num-1]".
  175. // Copy them into "elements[0 .. num-1]" if "elements" is not NULL.
  176. // Caution: implementation also moves elements with indices [start+num ..].
  177. // Calling this routine inside a loop can cause quadratic behavior.
  178. void ExtractSubrange(int start, int num, Element* elements);
  179. PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear();
  180. void MergeFrom(const RepeatedField& other);
  181. PROTOBUF_ATTRIBUTE_REINITIALIZES void CopyFrom(const RepeatedField& other);
  182. // Replaces the contents with RepeatedField(begin, end).
  183. template <typename Iter>
  184. PROTOBUF_ATTRIBUTE_REINITIALIZES void Assign(Iter begin, Iter end);
  185. // Reserve space to expand the field to at least the given size. If the
  186. // array is grown, it will always be at least doubled in size.
  187. void Reserve(int new_size);
  188. // Resize the RepeatedField to a new, smaller size. This is O(1).
  189. void Truncate(int new_size);
  190. void AddAlreadyReserved(const Element& value);
  191. // Appends a new element and return a pointer to it.
  192. // The new element is uninitialized if |Element| is a POD type.
  193. // Should be called only if Capacity() > Size().
  194. Element* AddAlreadyReserved();
  195. Element* AddNAlreadyReserved(int elements);
  196. int Capacity() const;
  197. // Like STL resize. Uses value to fill appended elements.
  198. // Like Truncate() if new_size <= size(), otherwise this is
  199. // O(new_size - size()).
  200. void Resize(int new_size, const Element& value);
  201. // Gets the underlying array. This pointer is possibly invalidated by
  202. // any add or remove operation.
  203. Element* mutable_data();
  204. const Element* data() const;
  205. // Swap entire contents with "other". If they are separate arenas then, copies
  206. // data between each other.
  207. void Swap(RepeatedField* other);
  208. // Swap entire contents with "other". Should be called only if the caller can
  209. // guarantee that both repeated fields are on the same arena or are on the
  210. // heap. Swapping between different arenas is disallowed and caught by a
  211. // GOOGLE_DCHECK (see API docs for details).
  212. void UnsafeArenaSwap(RepeatedField* other);
  213. // Swap two elements.
  214. void SwapElements(int index1, int index2);
  215. // STL-like iterator support
  216. typedef Element* iterator;
  217. typedef const Element* const_iterator;
  218. typedef Element value_type;
  219. typedef value_type& reference;
  220. typedef const value_type& const_reference;
  221. typedef value_type* pointer;
  222. typedef const value_type* const_pointer;
  223. typedef int size_type;
  224. typedef ptrdiff_t difference_type;
  225. iterator begin();
  226. const_iterator begin() const;
  227. const_iterator cbegin() const;
  228. iterator end();
  229. const_iterator end() const;
  230. const_iterator cend() const;
  231. // Reverse iterator support
  232. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  233. typedef std::reverse_iterator<iterator> reverse_iterator;
  234. reverse_iterator rbegin() { return reverse_iterator(end()); }
  235. const_reverse_iterator rbegin() const {
  236. return const_reverse_iterator(end());
  237. }
  238. reverse_iterator rend() { return reverse_iterator(begin()); }
  239. const_reverse_iterator rend() const {
  240. return const_reverse_iterator(begin());
  241. }
  242. // Returns the number of bytes used by the repeated field, excluding
  243. // sizeof(*this)
  244. size_t SpaceUsedExcludingSelfLong() const;
  245. int SpaceUsedExcludingSelf() const {
  246. return internal::ToIntSize(SpaceUsedExcludingSelfLong());
  247. }
  248. // Removes the element referenced by position.
  249. //
  250. // Returns an iterator to the element immediately following the removed
  251. // element.
  252. //
  253. // Invalidates all iterators at or after the removed element, including end().
  254. iterator erase(const_iterator position);
  255. // Removes the elements in the range [first, last).
  256. //
  257. // Returns an iterator to the element immediately following the removed range.
  258. //
  259. // Invalidates all iterators at or after the removed range, including end().
  260. iterator erase(const_iterator first, const_iterator last);
  261. // Get the Arena on which this RepeatedField stores its elements.
  262. inline Arena* GetArena() const {
  263. return (total_size_ == 0) ? static_cast<Arena*>(arena_or_elements_)
  264. : rep()->arena;
  265. }
  266. // For internal use only.
  267. //
  268. // This is public due to it being called by generated code.
  269. inline void InternalSwap(RepeatedField* other);
  270. private:
  271. static constexpr int kInitialSize = 0;
  272. // A note on the representation here (see also comment below for
  273. // RepeatedPtrFieldBase's struct Rep):
  274. //
  275. // We maintain the same sizeof(RepeatedField) as before we added arena support
  276. // so that we do not degrade performance by bloating memory usage. Directly
  277. // adding an arena_ element to RepeatedField is quite costly. By using
  278. // indirection in this way, we keep the same size when the RepeatedField is
  279. // empty (common case), and add only an 8-byte header to the elements array
  280. // when non-empty. We make sure to place the size fields directly in the
  281. // RepeatedField class to avoid costly cache misses due to the indirection.
  282. int current_size_;
  283. int total_size_;
  284. struct Rep {
  285. Arena* arena;
  286. // Here we declare a huge array as a way of approximating C's "flexible
  287. // array member" feature without relying on undefined behavior.
  288. Element elements[(std::numeric_limits<int>::max() - 2 * sizeof(Arena*)) /
  289. sizeof(Element)];
  290. };
  291. static constexpr size_t kRepHeaderSize = offsetof(Rep, elements);
  292. // If total_size_ == 0 this points to an Arena otherwise it points to the
  293. // elements member of a Rep struct. Using this invariant allows the storage of
  294. // the arena pointer without an extra allocation in the constructor.
  295. void* arena_or_elements_;
  296. // Return pointer to elements array.
  297. // pre-condition: the array must have been allocated.
  298. Element* elements() const {
  299. GOOGLE_DCHECK_GT(total_size_, 0);
  300. // Because of above pre-condition this cast is safe.
  301. return unsafe_elements();
  302. }
  303. // Return pointer to elements array if it exists otherwise either null or
  304. // a invalid pointer is returned. This only happens for empty repeated fields,
  305. // where you can't dereference this pointer anyway (it's empty).
  306. Element* unsafe_elements() const {
  307. return static_cast<Element*>(arena_or_elements_);
  308. }
  309. // Return pointer to the Rep struct.
  310. // pre-condition: the Rep must have been allocated, ie elements() is safe.
  311. Rep* rep() const {
  312. char* addr = reinterpret_cast<char*>(elements()) - offsetof(Rep, elements);
  313. return reinterpret_cast<Rep*>(addr);
  314. }
  315. friend class Arena;
  316. typedef void InternalArenaConstructable_;
  317. // Move the contents of |from| into |to|, possibly clobbering |from| in the
  318. // process. For primitive types this is just a memcpy(), but it could be
  319. // specialized for non-primitive types to, say, swap each element instead.
  320. void MoveArray(Element* to, Element* from, int size);
  321. // Copy the elements of |from| into |to|.
  322. void CopyArray(Element* to, const Element* from, int size);
  323. // Internal helper to delete all elements and deallocate the storage.
  324. void InternalDeallocate(Rep* rep, int size) {
  325. if (rep != NULL) {
  326. Element* e = &rep->elements[0];
  327. if (!std::is_trivial<Element>::value) {
  328. Element* limit = &rep->elements[size];
  329. for (; e < limit; e++) {
  330. e->~Element();
  331. }
  332. }
  333. if (rep->arena == NULL) {
  334. #if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation)
  335. const size_t bytes = size * sizeof(*e) + kRepHeaderSize;
  336. ::operator delete(static_cast<void*>(rep), bytes);
  337. #else
  338. ::operator delete(static_cast<void*>(rep));
  339. #endif
  340. }
  341. }
  342. }
  343. // This class is a performance wrapper around RepeatedField::Add(const T&)
  344. // function. In general unless a RepeatedField is a local stack variable LLVM
  345. // has a hard time optimizing Add. The machine code tends to be
  346. // loop:
  347. // mov %size, dword ptr [%repeated_field] // load
  348. // cmp %size, dword ptr [%repeated_field + 4]
  349. // jae fallback
  350. // mov %buffer, qword ptr [%repeated_field + 8]
  351. // mov dword [%buffer + %size * 4], %value
  352. // inc %size // increment
  353. // mov dword ptr [%repeated_field], %size // store
  354. // jmp loop
  355. //
  356. // This puts a load/store in each iteration of the important loop variable
  357. // size. It's a pretty bad compile that happens even in simple cases, but
  358. // largely the presence of the fallback path disturbs the compilers mem-to-reg
  359. // analysis.
  360. //
  361. // This class takes ownership of a repeated field for the duration of it's
  362. // lifetime. The repeated field should not be accessed during this time, ie.
  363. // only access through this class is allowed. This class should always be a
  364. // function local stack variable. Intended use
  365. //
  366. // void AddSequence(const int* begin, const int* end, RepeatedField<int>* out)
  367. // {
  368. // RepeatedFieldAdder<int> adder(out); // Take ownership of out
  369. // for (auto it = begin; it != end; ++it) {
  370. // adder.Add(*it);
  371. // }
  372. // }
  373. //
  374. // Typically due to the fact adder is a local stack variable. The compiler
  375. // will be successful in mem-to-reg transformation and the machine code will
  376. // be loop: cmp %size, %capacity jae fallback mov dword ptr [%buffer + %size *
  377. // 4], %val inc %size jmp loop
  378. //
  379. // The first version executes at 7 cycles per iteration while the second
  380. // version near 1 or 2 cycles.
  381. template <int = 0, bool = std::is_trivial<Element>::value>
  382. class FastAdderImpl {
  383. public:
  384. explicit FastAdderImpl(RepeatedField* rf) : repeated_field_(rf) {
  385. index_ = repeated_field_->current_size_;
  386. capacity_ = repeated_field_->total_size_;
  387. buffer_ = repeated_field_->unsafe_elements();
  388. }
  389. ~FastAdderImpl() { repeated_field_->current_size_ = index_; }
  390. void Add(Element val) {
  391. if (index_ == capacity_) {
  392. repeated_field_->current_size_ = index_;
  393. repeated_field_->Reserve(index_ + 1);
  394. capacity_ = repeated_field_->total_size_;
  395. buffer_ = repeated_field_->unsafe_elements();
  396. }
  397. buffer_[index_++] = val;
  398. }
  399. private:
  400. RepeatedField* repeated_field_;
  401. int index_;
  402. int capacity_;
  403. Element* buffer_;
  404. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FastAdderImpl);
  405. };
  406. // FastAdder is a wrapper for adding fields. The specialization above handles
  407. // POD types more efficiently than RepeatedField.
  408. template <int I>
  409. class FastAdderImpl<I, false> {
  410. public:
  411. explicit FastAdderImpl(RepeatedField* rf) : repeated_field_(rf) {}
  412. void Add(const Element& val) { repeated_field_->Add(val); }
  413. private:
  414. RepeatedField* repeated_field_;
  415. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FastAdderImpl);
  416. };
  417. using FastAdder = FastAdderImpl<>;
  418. friend class TestRepeatedFieldHelper;
  419. friend class ::google::protobuf::internal::ParseContext;
  420. };
  421. namespace internal {
  422. template <typename It>
  423. class RepeatedPtrIterator;
  424. template <typename It, typename VoidPtr>
  425. class RepeatedPtrOverPtrsIterator;
  426. } // namespace internal
  427. namespace internal {
  428. // This is a helper template to copy an array of elements efficiently when they
  429. // have a trivial copy constructor, and correctly otherwise. This really
  430. // shouldn't be necessary, but our compiler doesn't optimize std::copy very
  431. // effectively.
  432. template <typename Element,
  433. bool HasTrivialCopy = std::is_trivial<Element>::value>
  434. struct ElementCopier {
  435. void operator()(Element* to, const Element* from, int array_size);
  436. };
  437. } // namespace internal
  438. namespace internal {
  439. // type-traits helper for RepeatedPtrFieldBase: we only want to invoke
  440. // arena-related "copy if on different arena" behavior if the necessary methods
  441. // exist on the contained type. In particular, we rely on MergeFrom() existing
  442. // as a general proxy for the fact that a copy will work, and we also provide a
  443. // specific override for std::string*.
  444. template <typename T>
  445. struct TypeImplementsMergeBehaviorProbeForMergeFrom {
  446. typedef char HasMerge;
  447. typedef long HasNoMerge;
  448. // We accept either of:
  449. // - void MergeFrom(const T& other)
  450. // - bool MergeFrom(const T& other)
  451. //
  452. // We mangle these names a bit to avoid compatibility issues in 'unclean'
  453. // include environments that may have, e.g., "#define test ..." (yes, this
  454. // exists).
  455. template <typename U, typename RetType, RetType (U::*)(const U& arg)>
  456. struct CheckType;
  457. template <typename U>
  458. static HasMerge Check(CheckType<U, void, &U::MergeFrom>*);
  459. template <typename U>
  460. static HasMerge Check(CheckType<U, bool, &U::MergeFrom>*);
  461. template <typename U>
  462. static HasNoMerge Check(...);
  463. // Resolves to either std::true_type or std::false_type.
  464. typedef std::integral_constant<bool,
  465. (sizeof(Check<T>(0)) == sizeof(HasMerge))>
  466. type;
  467. };
  468. template <typename T, typename = void>
  469. struct TypeImplementsMergeBehavior
  470. : TypeImplementsMergeBehaviorProbeForMergeFrom<T> {};
  471. template <>
  472. struct TypeImplementsMergeBehavior<std::string> {
  473. typedef std::true_type type;
  474. };
  475. template <typename T>
  476. struct IsMovable
  477. : std::integral_constant<bool, std::is_move_constructible<T>::value &&
  478. std::is_move_assignable<T>::value> {};
  479. // This is the common base class for RepeatedPtrFields. It deals only in void*
  480. // pointers. Users should not use this interface directly.
  481. //
  482. // The methods of this interface correspond to the methods of RepeatedPtrField,
  483. // but may have a template argument called TypeHandler. Its signature is:
  484. // class TypeHandler {
  485. // public:
  486. // typedef MyType Type;
  487. // static Type* New();
  488. // static Type* NewFromPrototype(const Type* prototype,
  489. // Arena* arena);
  490. // static void Delete(Type*);
  491. // static void Clear(Type*);
  492. // static void Merge(const Type& from, Type* to);
  493. //
  494. // // Only needs to be implemented if SpaceUsedExcludingSelf() is called.
  495. // static int SpaceUsedLong(const Type&);
  496. // };
  497. class PROTOBUF_EXPORT RepeatedPtrFieldBase {
  498. protected:
  499. constexpr RepeatedPtrFieldBase();
  500. explicit RepeatedPtrFieldBase(Arena* arena);
  501. ~RepeatedPtrFieldBase() {
  502. #ifndef NDEBUG
  503. // Try to trigger segfault / asan failure in non-opt builds. If arena_
  504. // lifetime has ended before the destructor.
  505. if (arena_) (void)arena_->SpaceAllocated();
  506. #endif
  507. }
  508. // Must be called from destructor.
  509. template <typename TypeHandler>
  510. void Destroy();
  511. bool empty() const;
  512. int size() const;
  513. template <typename TypeHandler>
  514. const typename TypeHandler::Type& at(int index) const;
  515. template <typename TypeHandler>
  516. typename TypeHandler::Type& at(int index);
  517. template <typename TypeHandler>
  518. typename TypeHandler::Type* Mutable(int index);
  519. template <typename TypeHandler>
  520. void Delete(int index);
  521. template <typename TypeHandler>
  522. typename TypeHandler::Type* Add(typename TypeHandler::Type* prototype = NULL);
  523. public:
  524. // The next few methods are public so that they can be called from generated
  525. // code when implicit weak fields are used, but they should never be called by
  526. // application code.
  527. template <typename TypeHandler>
  528. const typename TypeHandler::Type& Get(int index) const;
  529. // Creates and adds an element using the given prototype, without introducing
  530. // a link-time dependency on the concrete message type. This method is used to
  531. // implement implicit weak fields. The prototype may be NULL, in which case an
  532. // ImplicitWeakMessage will be used as a placeholder.
  533. MessageLite* AddWeak(const MessageLite* prototype);
  534. template <typename TypeHandler>
  535. void Clear();
  536. template <typename TypeHandler>
  537. void MergeFrom(const RepeatedPtrFieldBase& other);
  538. inline void InternalSwap(RepeatedPtrFieldBase* other);
  539. protected:
  540. template <
  541. typename TypeHandler,
  542. typename std::enable_if<TypeHandler::Movable::value>::type* = nullptr>
  543. void Add(typename TypeHandler::Type&& value);
  544. template <typename TypeHandler>
  545. void RemoveLast();
  546. template <typename TypeHandler>
  547. void CopyFrom(const RepeatedPtrFieldBase& other);
  548. void CloseGap(int start, int num);
  549. void Reserve(int new_size);
  550. int Capacity() const;
  551. template <typename TypeHandler>
  552. static inline typename TypeHandler::Type* copy(
  553. typename TypeHandler::Type* value) {
  554. auto* new_value = TypeHandler::NewFromPrototype(value, nullptr);
  555. TypeHandler::Merge(*value, new_value);
  556. return new_value;
  557. }
  558. // Used for constructing iterators.
  559. void* const* raw_data() const;
  560. void** raw_mutable_data() const;
  561. template <typename TypeHandler>
  562. typename TypeHandler::Type** mutable_data();
  563. template <typename TypeHandler>
  564. const typename TypeHandler::Type* const* data() const;
  565. template <typename TypeHandler>
  566. PROTOBUF_NDEBUG_INLINE void Swap(RepeatedPtrFieldBase* other);
  567. void SwapElements(int index1, int index2);
  568. template <typename TypeHandler>
  569. size_t SpaceUsedExcludingSelfLong() const;
  570. // Advanced memory management --------------------------------------
  571. // Like Add(), but if there are no cleared objects to use, returns NULL.
  572. template <typename TypeHandler>
  573. typename TypeHandler::Type* AddFromCleared();
  574. template <typename TypeHandler>
  575. void AddAllocated(typename TypeHandler::Type* value) {
  576. typename TypeImplementsMergeBehavior<typename TypeHandler::Type>::type t;
  577. AddAllocatedInternal<TypeHandler>(value, t);
  578. }
  579. template <typename TypeHandler>
  580. void UnsafeArenaAddAllocated(typename TypeHandler::Type* value);
  581. template <typename TypeHandler>
  582. PROTOBUF_MUST_USE_RESULT typename TypeHandler::Type* ReleaseLast() {
  583. typename TypeImplementsMergeBehavior<typename TypeHandler::Type>::type t;
  584. return ReleaseLastInternal<TypeHandler>(t);
  585. }
  586. // Releases last element and returns it, but does not do out-of-arena copy.
  587. // And just returns the raw pointer to the contained element in the arena.
  588. template <typename TypeHandler>
  589. typename TypeHandler::Type* UnsafeArenaReleaseLast();
  590. int ClearedCount() const;
  591. template <typename TypeHandler>
  592. void AddCleared(typename TypeHandler::Type* value);
  593. template <typename TypeHandler>
  594. PROTOBUF_MUST_USE_RESULT typename TypeHandler::Type* ReleaseCleared();
  595. template <typename TypeHandler>
  596. void AddAllocatedInternal(typename TypeHandler::Type* value, std::true_type);
  597. template <typename TypeHandler>
  598. void AddAllocatedInternal(typename TypeHandler::Type* value, std::false_type);
  599. template <typename TypeHandler>
  600. PROTOBUF_NOINLINE void AddAllocatedSlowWithCopy(
  601. typename TypeHandler::Type* value, Arena* value_arena, Arena* my_arena);
  602. template <typename TypeHandler>
  603. PROTOBUF_NOINLINE void AddAllocatedSlowWithoutCopy(
  604. typename TypeHandler::Type* value);
  605. template <typename TypeHandler>
  606. typename TypeHandler::Type* ReleaseLastInternal(std::true_type);
  607. template <typename TypeHandler>
  608. typename TypeHandler::Type* ReleaseLastInternal(std::false_type);
  609. template <typename TypeHandler>
  610. PROTOBUF_NOINLINE void SwapFallback(RepeatedPtrFieldBase* other);
  611. inline Arena* GetArena() const { return arena_; }
  612. private:
  613. static constexpr int kInitialSize = 0;
  614. // A few notes on internal representation:
  615. //
  616. // We use an indirected approach, with struct Rep, to keep
  617. // sizeof(RepeatedPtrFieldBase) equivalent to what it was before arena support
  618. // was added, namely, 3 8-byte machine words on x86-64. An instance of Rep is
  619. // allocated only when the repeated field is non-empty, and it is a
  620. // dynamically-sized struct (the header is directly followed by elements[]).
  621. // We place arena_ and current_size_ directly in the object to avoid cache
  622. // misses due to the indirection, because these fields are checked frequently.
  623. // Placing all fields directly in the RepeatedPtrFieldBase instance costs
  624. // significant performance for memory-sensitive workloads.
  625. Arena* arena_;
  626. int current_size_;
  627. int total_size_;
  628. struct Rep {
  629. int allocated_size;
  630. // Here we declare a huge array as a way of approximating C's "flexible
  631. // array member" feature without relying on undefined behavior.
  632. void* elements[(std::numeric_limits<int>::max() - 2 * sizeof(int)) /
  633. sizeof(void*)];
  634. };
  635. static constexpr size_t kRepHeaderSize = offsetof(Rep, elements);
  636. Rep* rep_;
  637. template <typename TypeHandler>
  638. static inline typename TypeHandler::Type* cast(void* element) {
  639. return reinterpret_cast<typename TypeHandler::Type*>(element);
  640. }
  641. template <typename TypeHandler>
  642. static inline const typename TypeHandler::Type* cast(const void* element) {
  643. return reinterpret_cast<const typename TypeHandler::Type*>(element);
  644. }
  645. // Non-templated inner function to avoid code duplication. Takes a function
  646. // pointer to the type-specific (templated) inner allocate/merge loop.
  647. void MergeFromInternal(const RepeatedPtrFieldBase& other,
  648. void (RepeatedPtrFieldBase::*inner_loop)(void**,
  649. void**, int,
  650. int));
  651. template <typename TypeHandler>
  652. PROTOBUF_NOINLINE void MergeFromInnerLoop(void** our_elems,
  653. void** other_elems, int length,
  654. int already_allocated);
  655. // Internal helper: extend array space if necessary to contain |extend_amount|
  656. // more elements, and return a pointer to the element immediately following
  657. // the old list of elements. This interface factors out common behavior from
  658. // Reserve() and MergeFrom() to reduce code size. |extend_amount| must be > 0.
  659. void** InternalExtend(int extend_amount);
  660. // Internal helper for Add: add "obj" as the next element in the
  661. // array, including potentially resizing the array with Reserve if
  662. // needed
  663. void* AddOutOfLineHelper(void* obj);
  664. // The reflection implementation needs to call protected methods directly,
  665. // reinterpreting pointers as being to Message instead of a specific Message
  666. // subclass.
  667. friend class ::PROTOBUF_NAMESPACE_ID::Reflection;
  668. friend class ::PROTOBUF_NAMESPACE_ID::internal::SwapFieldHelper;
  669. // ExtensionSet stores repeated message extensions as
  670. // RepeatedPtrField<MessageLite>, but non-lite ExtensionSets need to implement
  671. // SpaceUsedLong(), and thus need to call SpaceUsedExcludingSelfLong()
  672. // reinterpreting MessageLite as Message. ExtensionSet also needs to make use
  673. // of AddFromCleared(), which is not part of the public interface.
  674. friend class ExtensionSet;
  675. // The MapFieldBase implementation needs to call protected methods directly,
  676. // reinterpreting pointers as being to Message instead of a specific Message
  677. // subclass.
  678. friend class MapFieldBase;
  679. friend class MapFieldBaseStub;
  680. // The table-driven MergePartialFromCodedStream implementation needs to
  681. // operate on RepeatedPtrField<MessageLite>.
  682. friend class MergePartialFromCodedStreamHelper;
  683. friend class AccessorHelper;
  684. template <typename T>
  685. friend struct google::protobuf::WeakRepeatedPtrField;
  686. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedPtrFieldBase);
  687. };
  688. template <typename GenericType>
  689. class GenericTypeHandler {
  690. public:
  691. typedef GenericType Type;
  692. using Movable = IsMovable<GenericType>;
  693. static inline GenericType* New(Arena* arena) {
  694. return Arena::CreateMaybeMessage<Type>(arena);
  695. }
  696. static inline GenericType* New(Arena* arena, GenericType&& value) {
  697. return Arena::Create<GenericType>(arena, std::move(value));
  698. }
  699. static inline GenericType* NewFromPrototype(const GenericType* prototype,
  700. Arena* arena = NULL);
  701. static inline void Delete(GenericType* value, Arena* arena) {
  702. if (arena == NULL) {
  703. delete value;
  704. }
  705. }
  706. static inline Arena* GetOwningArena(GenericType* value) {
  707. return Arena::GetOwningArena<Type>(value);
  708. }
  709. static inline void Clear(GenericType* value) { value->Clear(); }
  710. PROTOBUF_NOINLINE
  711. static void Merge(const GenericType& from, GenericType* to);
  712. static inline size_t SpaceUsedLong(const GenericType& value) {
  713. return value.SpaceUsedLong();
  714. }
  715. };
  716. template <typename GenericType>
  717. GenericType* GenericTypeHandler<GenericType>::NewFromPrototype(
  718. const GenericType* /* prototype */, Arena* arena) {
  719. return New(arena);
  720. }
  721. template <typename GenericType>
  722. void GenericTypeHandler<GenericType>::Merge(const GenericType& from,
  723. GenericType* to) {
  724. to->MergeFrom(from);
  725. }
  726. // NewFromPrototype() and Merge() are not defined inline here, as we will need
  727. // to do a virtual function dispatch anyways to go from Message* to call
  728. // New/Merge.
  729. template <>
  730. MessageLite* GenericTypeHandler<MessageLite>::NewFromPrototype(
  731. const MessageLite* prototype, Arena* arena);
  732. template <>
  733. inline Arena* GenericTypeHandler<MessageLite>::GetOwningArena(
  734. MessageLite* value) {
  735. return value->GetOwningArena();
  736. }
  737. template <>
  738. void GenericTypeHandler<MessageLite>::Merge(const MessageLite& from,
  739. MessageLite* to);
  740. template <>
  741. inline void GenericTypeHandler<std::string>::Clear(std::string* value) {
  742. value->clear();
  743. }
  744. template <>
  745. void GenericTypeHandler<std::string>::Merge(const std::string& from,
  746. std::string* to);
  747. // Message specialization bodies defined in message.cc. This split is necessary
  748. // to allow proto2-lite (which includes this header) to be independent of
  749. // Message.
  750. template <>
  751. PROTOBUF_EXPORT Message* GenericTypeHandler<Message>::NewFromPrototype(
  752. const Message* prototype, Arena* arena);
  753. template <>
  754. PROTOBUF_EXPORT Arena* GenericTypeHandler<Message>::GetOwningArena(
  755. Message* value);
  756. class StringTypeHandler {
  757. public:
  758. typedef std::string Type;
  759. using Movable = IsMovable<Type>;
  760. static inline std::string* New(Arena* arena) {
  761. return Arena::Create<std::string>(arena);
  762. }
  763. static inline std::string* New(Arena* arena, std::string&& value) {
  764. return Arena::Create<std::string>(arena, std::move(value));
  765. }
  766. static inline std::string* NewFromPrototype(const std::string*,
  767. Arena* arena) {
  768. return New(arena);
  769. }
  770. static inline Arena* GetOwningArena(std::string*) { return nullptr; }
  771. static inline void Delete(std::string* value, Arena* arena) {
  772. if (arena == NULL) {
  773. delete value;
  774. }
  775. }
  776. static inline void Clear(std::string* value) { value->clear(); }
  777. static inline void Merge(const std::string& from, std::string* to) {
  778. *to = from;
  779. }
  780. static size_t SpaceUsedLong(const std::string& value) {
  781. return sizeof(value) + StringSpaceUsedExcludingSelfLong(value);
  782. }
  783. };
  784. } // namespace internal
  785. // RepeatedPtrField is like RepeatedField, but used for repeated strings or
  786. // Messages.
  787. template <typename Element>
  788. class RepeatedPtrField final : private internal::RepeatedPtrFieldBase {
  789. public:
  790. constexpr RepeatedPtrField();
  791. explicit RepeatedPtrField(Arena* arena);
  792. RepeatedPtrField(const RepeatedPtrField& other);
  793. template <typename Iter,
  794. typename = typename std::enable_if<std::is_constructible<
  795. Element, decltype(*std::declval<Iter>())>::value>::type>
  796. RepeatedPtrField(Iter begin, Iter end);
  797. ~RepeatedPtrField();
  798. RepeatedPtrField& operator=(const RepeatedPtrField& other);
  799. RepeatedPtrField(RepeatedPtrField&& other) noexcept;
  800. RepeatedPtrField& operator=(RepeatedPtrField&& other) noexcept;
  801. bool empty() const;
  802. int size() const;
  803. const Element& Get(int index) const;
  804. Element* Mutable(int index);
  805. Element* Add();
  806. void Add(Element&& value);
  807. // Append elements in the range [begin, end) after reserving
  808. // the appropriate number of elements.
  809. template <typename Iter>
  810. void Add(Iter begin, Iter end);
  811. const Element& operator[](int index) const { return Get(index); }
  812. Element& operator[](int index) { return *Mutable(index); }
  813. const Element& at(int index) const;
  814. Element& at(int index);
  815. // Remove the last element in the array.
  816. // Ownership of the element is retained by the array.
  817. void RemoveLast();
  818. // Delete elements with indices in the range [start .. start+num-1].
  819. // Caution: implementation moves all elements with indices [start+num .. ].
  820. // Calling this routine inside a loop can cause quadratic behavior.
  821. void DeleteSubrange(int start, int num);
  822. PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear();
  823. void MergeFrom(const RepeatedPtrField& other);
  824. PROTOBUF_ATTRIBUTE_REINITIALIZES void CopyFrom(const RepeatedPtrField& other);
  825. // Replaces the contents with RepeatedPtrField(begin, end).
  826. template <typename Iter>
  827. PROTOBUF_ATTRIBUTE_REINITIALIZES void Assign(Iter begin, Iter end);
  828. // Reserve space to expand the field to at least the given size. This only
  829. // resizes the pointer array; it doesn't allocate any objects. If the
  830. // array is grown, it will always be at least doubled in size.
  831. void Reserve(int new_size);
  832. int Capacity() const;
  833. // Gets the underlying array. This pointer is possibly invalidated by
  834. // any add or remove operation.
  835. Element** mutable_data();
  836. const Element* const* data() const;
  837. // Swap entire contents with "other". If they are on separate arenas, then
  838. // copies data.
  839. void Swap(RepeatedPtrField* other);
  840. // Swap entire contents with "other". Caller should guarantee that either both
  841. // fields are on the same arena or both are on the heap. Swapping between
  842. // different arenas with this function is disallowed and is caught via
  843. // GOOGLE_DCHECK.
  844. void UnsafeArenaSwap(RepeatedPtrField* other);
  845. // Swap two elements.
  846. void SwapElements(int index1, int index2);
  847. // STL-like iterator support
  848. typedef internal::RepeatedPtrIterator<Element> iterator;
  849. typedef internal::RepeatedPtrIterator<const Element> const_iterator;
  850. typedef Element value_type;
  851. typedef value_type& reference;
  852. typedef const value_type& const_reference;
  853. typedef value_type* pointer;
  854. typedef const value_type* const_pointer;
  855. typedef int size_type;
  856. typedef ptrdiff_t difference_type;
  857. iterator begin();
  858. const_iterator begin() const;
  859. const_iterator cbegin() const;
  860. iterator end();
  861. const_iterator end() const;
  862. const_iterator cend() const;
  863. // Reverse iterator support
  864. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  865. typedef std::reverse_iterator<iterator> reverse_iterator;
  866. reverse_iterator rbegin() { return reverse_iterator(end()); }
  867. const_reverse_iterator rbegin() const {
  868. return const_reverse_iterator(end());
  869. }
  870. reverse_iterator rend() { return reverse_iterator(begin()); }
  871. const_reverse_iterator rend() const {
  872. return const_reverse_iterator(begin());
  873. }
  874. // Custom STL-like iterator that iterates over and returns the underlying
  875. // pointers to Element rather than Element itself.
  876. typedef internal::RepeatedPtrOverPtrsIterator<Element*, void*>
  877. pointer_iterator;
  878. typedef internal::RepeatedPtrOverPtrsIterator<const Element* const,
  879. const void* const>
  880. const_pointer_iterator;
  881. pointer_iterator pointer_begin();
  882. const_pointer_iterator pointer_begin() const;
  883. pointer_iterator pointer_end();
  884. const_pointer_iterator pointer_end() const;
  885. // Returns (an estimate of) the number of bytes used by the repeated field,
  886. // excluding sizeof(*this).
  887. size_t SpaceUsedExcludingSelfLong() const;
  888. int SpaceUsedExcludingSelf() const {
  889. return internal::ToIntSize(SpaceUsedExcludingSelfLong());
  890. }
  891. // Advanced memory management --------------------------------------
  892. // When hardcore memory management becomes necessary -- as it sometimes
  893. // does here at Google -- the following methods may be useful.
  894. // Add an already-allocated object, passing ownership to the
  895. // RepeatedPtrField.
  896. //
  897. // Note that some special behavior occurs with respect to arenas:
  898. //
  899. // (i) if this field holds submessages, the new submessage will be copied if
  900. // the original is in an arena and this RepeatedPtrField is either in a
  901. // different arena, or on the heap.
  902. // (ii) if this field holds strings, the passed-in string *must* be
  903. // heap-allocated, not arena-allocated. There is no way to dynamically check
  904. // this at runtime, so User Beware.
  905. void AddAllocated(Element* value);
  906. // Remove the last element and return it, passing ownership to the caller.
  907. // Requires: size() > 0
  908. //
  909. // If this RepeatedPtrField is on an arena, an object copy is required to pass
  910. // ownership back to the user (for compatible semantics). Use
  911. // UnsafeArenaReleaseLast() if this behavior is undesired.
  912. PROTOBUF_MUST_USE_RESULT Element* ReleaseLast();
  913. // Add an already-allocated object, skipping arena-ownership checks. The user
  914. // must guarantee that the given object is in the same arena as this
  915. // RepeatedPtrField.
  916. // It is also useful in legacy code that uses temporary ownership to avoid
  917. // copies. Example:
  918. // RepeatedPtrField<T> temp_field;
  919. // temp_field.UnsafeArenaAddAllocated(new T);
  920. // ... // Do something with temp_field
  921. // temp_field.UnsafeArenaExtractSubrange(0, temp_field.size(), nullptr);
  922. // If you put temp_field on the arena this fails, because the ownership
  923. // transfers to the arena at the "AddAllocated" call and is not released
  924. // anymore causing a double delete. UnsafeArenaAddAllocated prevents this.
  925. void UnsafeArenaAddAllocated(Element* value);
  926. // Remove the last element and return it. Unlike ReleaseLast, the returned
  927. // pointer is always to the original object. This may be in an arena, and
  928. // therefore have the arena's lifetime.
  929. // Requires: current_size_ > 0
  930. Element* UnsafeArenaReleaseLast();
  931. // Extract elements with indices in the range "[start .. start+num-1]".
  932. // The caller assumes ownership of the extracted elements and is responsible
  933. // for deleting them when they are no longer needed.
  934. // If "elements" is non-NULL, then pointers to the extracted elements
  935. // are stored in "elements[0 .. num-1]" for the convenience of the caller.
  936. // If "elements" is NULL, then the caller must use some other mechanism
  937. // to perform any further operations (like deletion) on these elements.
  938. // Caution: implementation also moves elements with indices [start+num ..].
  939. // Calling this routine inside a loop can cause quadratic behavior.
  940. //
  941. // Memory copying behavior is identical to ReleaseLast(), described above: if
  942. // this RepeatedPtrField is on an arena, an object copy is performed for each
  943. // returned element, so that all returned element pointers are to
  944. // heap-allocated copies. If this copy is not desired, the user should call
  945. // UnsafeArenaExtractSubrange().
  946. void ExtractSubrange(int start, int num, Element** elements);
  947. // Identical to ExtractSubrange() described above, except that no object
  948. // copies are ever performed. Instead, the raw object pointers are returned.
  949. // Thus, if on an arena, the returned objects must not be freed, because they
  950. // will not be heap-allocated objects.
  951. void UnsafeArenaExtractSubrange(int start, int num, Element** elements);
  952. // When elements are removed by calls to RemoveLast() or Clear(), they
  953. // are not actually freed. Instead, they are cleared and kept so that
  954. // they can be reused later. This can save lots of CPU time when
  955. // repeatedly reusing a protocol message for similar purposes.
  956. //
  957. // Hardcore programs may choose to manipulate these cleared objects
  958. // to better optimize memory management using the following routines.
  959. // Get the number of cleared objects that are currently being kept
  960. // around for reuse.
  961. int ClearedCount() const;
  962. // Add an element to the pool of cleared objects, passing ownership to
  963. // the RepeatedPtrField. The element must be cleared prior to calling
  964. // this method.
  965. //
  966. // This method cannot be called when the repeated field is on an arena or when
  967. // |value| is; both cases will trigger a GOOGLE_DCHECK-failure.
  968. void AddCleared(Element* value);
  969. // Remove a single element from the cleared pool and return it, passing
  970. // ownership to the caller. The element is guaranteed to be cleared.
  971. // Requires: ClearedCount() > 0
  972. //
  973. //
  974. // This method cannot be called when the repeated field is on an arena; doing
  975. // so will trigger a GOOGLE_DCHECK-failure.
  976. PROTOBUF_MUST_USE_RESULT Element* ReleaseCleared();
  977. // Removes the element referenced by position.
  978. //
  979. // Returns an iterator to the element immediately following the removed
  980. // element.
  981. //
  982. // Invalidates all iterators at or after the removed element, including end().
  983. iterator erase(const_iterator position);
  984. // Removes the elements in the range [first, last).
  985. //
  986. // Returns an iterator to the element immediately following the removed range.
  987. //
  988. // Invalidates all iterators at or after the removed range, including end().
  989. iterator erase(const_iterator first, const_iterator last);
  990. // Gets the arena on which this RepeatedPtrField stores its elements.
  991. inline Arena* GetArena() const;
  992. // For internal use only.
  993. //
  994. // This is public due to it being called by generated code.
  995. void InternalSwap(RepeatedPtrField* other) {
  996. internal::RepeatedPtrFieldBase::InternalSwap(other);
  997. }
  998. private:
  999. // Note: RepeatedPtrField SHOULD NOT be subclassed by users.
  1000. class TypeHandler;
  1001. // Implementations for ExtractSubrange(). The copying behavior must be
  1002. // included only if the type supports the necessary operations (e.g.,
  1003. // MergeFrom()), so we must resolve this at compile time. ExtractSubrange()
  1004. // uses SFINAE to choose one of the below implementations.
  1005. void ExtractSubrangeInternal(int start, int num, Element** elements,
  1006. std::true_type);
  1007. void ExtractSubrangeInternal(int start, int num, Element** elements,
  1008. std::false_type);
  1009. friend class Arena;
  1010. template <typename T>
  1011. friend struct WeakRepeatedPtrField;
  1012. typedef void InternalArenaConstructable_;
  1013. };
  1014. // implementation ====================================================
  1015. template <typename Element>
  1016. constexpr RepeatedField<Element>::RepeatedField()
  1017. : current_size_(0), total_size_(0), arena_or_elements_(nullptr) {}
  1018. template <typename Element>
  1019. inline RepeatedField<Element>::RepeatedField(Arena* arena)
  1020. : current_size_(0), total_size_(0), arena_or_elements_(arena) {}
  1021. template <typename Element>
  1022. inline RepeatedField<Element>::RepeatedField(const RepeatedField& other)
  1023. : current_size_(0), total_size_(0), arena_or_elements_(nullptr) {
  1024. if (other.current_size_ != 0) {
  1025. Reserve(other.size());
  1026. AddNAlreadyReserved(other.size());
  1027. CopyArray(Mutable(0), &other.Get(0), other.size());
  1028. }
  1029. }
  1030. template <typename Element>
  1031. template <typename Iter, typename>
  1032. RepeatedField<Element>::RepeatedField(Iter begin, Iter end)
  1033. : current_size_(0), total_size_(0), arena_or_elements_(nullptr) {
  1034. Add(begin, end);
  1035. }
  1036. template <typename Element>
  1037. RepeatedField<Element>::~RepeatedField() {
  1038. #ifndef NDEBUG
  1039. // Try to trigger segfault / asan failure in non-opt builds. If arena_
  1040. // lifetime has ended before the destructor.
  1041. auto arena = GetArena();
  1042. if (arena) (void)arena->SpaceAllocated();
  1043. #endif
  1044. if (total_size_ > 0) {
  1045. InternalDeallocate(rep(), total_size_);
  1046. }
  1047. }
  1048. template <typename Element>
  1049. inline RepeatedField<Element>& RepeatedField<Element>::operator=(
  1050. const RepeatedField& other) {
  1051. if (this != &other) CopyFrom(other);
  1052. return *this;
  1053. }
  1054. template <typename Element>
  1055. inline RepeatedField<Element>::RepeatedField(RepeatedField&& other) noexcept
  1056. : RepeatedField() {
  1057. #ifdef PROTOBUF_FORCE_COPY_IN_MOVE
  1058. CopyFrom(other);
  1059. #else // PROTOBUF_FORCE_COPY_IN_MOVE
  1060. // We don't just call Swap(&other) here because it would perform 3 copies if
  1061. // other is on an arena. This field can't be on an arena because arena
  1062. // construction always uses the Arena* accepting constructor.
  1063. if (other.GetArena()) {
  1064. CopyFrom(other);
  1065. } else {
  1066. InternalSwap(&other);
  1067. }
  1068. #endif // !PROTOBUF_FORCE_COPY_IN_MOVE
  1069. }
  1070. template <typename Element>
  1071. inline RepeatedField<Element>& RepeatedField<Element>::operator=(
  1072. RepeatedField&& other) noexcept {
  1073. // We don't just call Swap(&other) here because it would perform 3 copies if
  1074. // the two fields are on different arenas.
  1075. if (this != &other) {
  1076. if (GetArena() != other.GetArena()
  1077. #ifdef PROTOBUF_FORCE_COPY_IN_MOVE
  1078. || GetArena() == nullptr
  1079. #endif // !PROTOBUF_FORCE_COPY_IN_MOVE
  1080. ) {
  1081. CopyFrom(other);
  1082. } else {
  1083. InternalSwap(&other);
  1084. }
  1085. }
  1086. return *this;
  1087. }
  1088. template <typename Element>
  1089. inline bool RepeatedField<Element>::empty() const {
  1090. return current_size_ == 0;
  1091. }
  1092. template <typename Element>
  1093. inline int RepeatedField<Element>::size() const {
  1094. return current_size_;
  1095. }
  1096. template <typename Element>
  1097. inline int RepeatedField<Element>::Capacity() const {
  1098. return total_size_;
  1099. }
  1100. template <typename Element>
  1101. inline void RepeatedField<Element>::AddAlreadyReserved(const Element& value) {
  1102. GOOGLE_DCHECK_LT(current_size_, total_size_);
  1103. elements()[current_size_++] = value;
  1104. }
  1105. template <typename Element>
  1106. inline Element* RepeatedField<Element>::AddAlreadyReserved() {
  1107. GOOGLE_DCHECK_LT(current_size_, total_size_);
  1108. return &elements()[current_size_++];
  1109. }
  1110. template <typename Element>
  1111. inline Element* RepeatedField<Element>::AddNAlreadyReserved(int n) {
  1112. GOOGLE_DCHECK_GE(total_size_ - current_size_, n)
  1113. << total_size_ << ", " << current_size_;
  1114. // Warning: sometimes people call this when n == 0 and total_size_ == 0. In
  1115. // this case the return pointer points to a zero size array (n == 0). Hence
  1116. // we can just use unsafe_elements(), because the user cannot dereference the
  1117. // pointer anyway.
  1118. Element* ret = unsafe_elements() + current_size_;
  1119. current_size_ += n;
  1120. return ret;
  1121. }
  1122. template <typename Element>
  1123. inline void RepeatedField<Element>::Resize(int new_size, const Element& value) {
  1124. GOOGLE_DCHECK_GE(new_size, 0);
  1125. if (new_size > current_size_) {
  1126. Reserve(new_size);
  1127. std::fill(&elements()[current_size_], &elements()[new_size], value);
  1128. }
  1129. current_size_ = new_size;
  1130. }
  1131. template <typename Element>
  1132. inline const Element& RepeatedField<Element>::Get(int index) const {
  1133. GOOGLE_DCHECK_GE(index, 0);
  1134. GOOGLE_DCHECK_LT(index, current_size_);
  1135. return elements()[index];
  1136. }
  1137. template <typename Element>
  1138. inline const Element& RepeatedField<Element>::at(int index) const {
  1139. GOOGLE_CHECK_GE(index, 0);
  1140. GOOGLE_CHECK_LT(index, current_size_);
  1141. return elements()[index];
  1142. }
  1143. template <typename Element>
  1144. inline Element& RepeatedField<Element>::at(int index) {
  1145. GOOGLE_CHECK_GE(index, 0);
  1146. GOOGLE_CHECK_LT(index, current_size_);
  1147. return elements()[index];
  1148. }
  1149. template <typename Element>
  1150. inline Element* RepeatedField<Element>::Mutable(int index) {
  1151. GOOGLE_DCHECK_GE(index, 0);
  1152. GOOGLE_DCHECK_LT(index, current_size_);
  1153. return &elements()[index];
  1154. }
  1155. template <typename Element>
  1156. inline void RepeatedField<Element>::Set(int index, const Element& value) {
  1157. GOOGLE_DCHECK_GE(index, 0);
  1158. GOOGLE_DCHECK_LT(index, current_size_);
  1159. elements()[index] = value;
  1160. }
  1161. template <typename Element>
  1162. inline void RepeatedField<Element>::Add(const Element& value) {
  1163. uint32_t size = current_size_;
  1164. if (static_cast<int>(size) == total_size_) {
  1165. // value could reference an element of the array. Reserving new space will
  1166. // invalidate the reference. So we must make a copy first.
  1167. auto tmp = value;
  1168. Reserve(total_size_ + 1);
  1169. elements()[size] = std::move(tmp);
  1170. } else {
  1171. elements()[size] = value;
  1172. }
  1173. current_size_ = size + 1;
  1174. }
  1175. template <typename Element>
  1176. inline Element* RepeatedField<Element>::Add() {
  1177. uint32_t size = current_size_;
  1178. if (static_cast<int>(size) == total_size_) Reserve(total_size_ + 1);
  1179. auto ptr = &elements()[size];
  1180. current_size_ = size + 1;
  1181. return ptr;
  1182. }
  1183. template <typename Element>
  1184. template <typename Iter>
  1185. inline void RepeatedField<Element>::Add(Iter begin, Iter end) {
  1186. int reserve = internal::CalculateReserve(begin, end);
  1187. if (reserve != -1) {
  1188. if (reserve == 0) {
  1189. return;
  1190. }
  1191. Reserve(reserve + size());
  1192. // TODO(ckennelly): The compiler loses track of the buffer freshly
  1193. // allocated by Reserve() by the time we call elements, so it cannot
  1194. // guarantee that elements does not alias [begin(), end()).
  1195. //
  1196. // If restrict is available, annotating the pointer obtained from elements()
  1197. // causes this to lower to memcpy instead of memmove.
  1198. std::copy(begin, end, elements() + size());
  1199. current_size_ = reserve + size();
  1200. } else {
  1201. FastAdder fast_adder(this);
  1202. for (; begin != end; ++begin) fast_adder.Add(*begin);
  1203. }
  1204. }
  1205. template <typename Element>
  1206. inline void RepeatedField<Element>::RemoveLast() {
  1207. GOOGLE_DCHECK_GT(current_size_, 0);
  1208. current_size_--;
  1209. }
  1210. template <typename Element>
  1211. void RepeatedField<Element>::ExtractSubrange(int start, int num,
  1212. Element* elements) {
  1213. GOOGLE_DCHECK_GE(start, 0);
  1214. GOOGLE_DCHECK_GE(num, 0);
  1215. GOOGLE_DCHECK_LE(start + num, this->current_size_);
  1216. // Save the values of the removed elements if requested.
  1217. if (elements != NULL) {
  1218. for (int i = 0; i < num; ++i) elements[i] = this->Get(i + start);
  1219. }
  1220. // Slide remaining elements down to fill the gap.
  1221. if (num > 0) {
  1222. for (int i = start + num; i < this->current_size_; ++i)
  1223. this->Set(i - num, this->Get(i));
  1224. this->Truncate(this->current_size_ - num);
  1225. }
  1226. }
  1227. template <typename Element>
  1228. inline void RepeatedField<Element>::Clear() {
  1229. current_size_ = 0;
  1230. }
  1231. template <typename Element>
  1232. inline void RepeatedField<Element>::MergeFrom(const RepeatedField& other) {
  1233. GOOGLE_DCHECK_NE(&other, this);
  1234. if (other.current_size_ != 0) {
  1235. int existing_size = size();
  1236. Reserve(existing_size + other.size());
  1237. AddNAlreadyReserved(other.size());
  1238. CopyArray(Mutable(existing_size), &other.Get(0), other.size());
  1239. }
  1240. }
  1241. template <typename Element>
  1242. inline void RepeatedField<Element>::CopyFrom(const RepeatedField& other) {
  1243. if (&other == this) return;
  1244. Clear();
  1245. MergeFrom(other);
  1246. }
  1247. template <typename Element>
  1248. template <typename Iter>
  1249. inline void RepeatedField<Element>::Assign(Iter begin, Iter end) {
  1250. Clear();
  1251. Add(begin, end);
  1252. }
  1253. template <typename Element>
  1254. inline typename RepeatedField<Element>::iterator RepeatedField<Element>::erase(
  1255. const_iterator position) {
  1256. return erase(position, position + 1);
  1257. }
  1258. template <typename Element>
  1259. inline typename RepeatedField<Element>::iterator RepeatedField<Element>::erase(
  1260. const_iterator first, const_iterator last) {
  1261. size_type first_offset = first - cbegin();
  1262. if (first != last) {
  1263. Truncate(std::copy(last, cend(), begin() + first_offset) - cbegin());
  1264. }
  1265. return begin() + first_offset;
  1266. }
  1267. template <typename Element>
  1268. inline Element* RepeatedField<Element>::mutable_data() {
  1269. return unsafe_elements();
  1270. }
  1271. template <typename Element>
  1272. inline const Element* RepeatedField<Element>::data() const {
  1273. return unsafe_elements();
  1274. }
  1275. template <typename Element>
  1276. inline void RepeatedField<Element>::InternalSwap(RepeatedField* other) {
  1277. GOOGLE_DCHECK(this != other);
  1278. // Swap all fields at once.
  1279. static_assert(std::is_standard_layout<RepeatedField<Element>>::value,
  1280. "offsetof() requires standard layout before c++17");
  1281. internal::memswap<offsetof(RepeatedField, arena_or_elements_) +
  1282. sizeof(this->arena_or_elements_) -
  1283. offsetof(RepeatedField, current_size_)>(
  1284. reinterpret_cast<char*>(this) + offsetof(RepeatedField, current_size_),
  1285. reinterpret_cast<char*>(other) + offsetof(RepeatedField, current_size_));
  1286. }
  1287. template <typename Element>
  1288. void RepeatedField<Element>::Swap(RepeatedField* other) {
  1289. if (this == other) return;
  1290. #ifdef PROTOBUF_FORCE_COPY_IN_SWAP
  1291. if (GetArena() != nullptr && GetArena() == other->GetArena()) {
  1292. #else // PROTOBUF_FORCE_COPY_IN_SWAP
  1293. if (GetArena() == other->GetArena()) {
  1294. #endif // !PROTOBUF_FORCE_COPY_IN_SWAP
  1295. InternalSwap(other);
  1296. } else {
  1297. RepeatedField<Element> temp(other->GetArena());
  1298. temp.MergeFrom(*this);
  1299. CopyFrom(*other);
  1300. other->UnsafeArenaSwap(&temp);
  1301. }
  1302. }
  1303. template <typename Element>
  1304. void RepeatedField<Element>::UnsafeArenaSwap(RepeatedField* other) {
  1305. if (this == other) return;
  1306. InternalSwap(other);
  1307. }
  1308. template <typename Element>
  1309. void RepeatedField<Element>::SwapElements(int index1, int index2) {
  1310. using std::swap; // enable ADL with fallback
  1311. swap(elements()[index1], elements()[index2]);
  1312. }
  1313. template <typename Element>
  1314. inline typename RepeatedField<Element>::iterator
  1315. RepeatedField<Element>::begin() {
  1316. return unsafe_elements();
  1317. }
  1318. template <typename Element>
  1319. inline typename RepeatedField<Element>::const_iterator
  1320. RepeatedField<Element>::begin() const {
  1321. return unsafe_elements();
  1322. }
  1323. template <typename Element>
  1324. inline typename RepeatedField<Element>::const_iterator
  1325. RepeatedField<Element>::cbegin() const {
  1326. return unsafe_elements();
  1327. }
  1328. template <typename Element>
  1329. inline typename RepeatedField<Element>::iterator RepeatedField<Element>::end() {
  1330. return unsafe_elements() + current_size_;
  1331. }
  1332. template <typename Element>
  1333. inline typename RepeatedField<Element>::const_iterator
  1334. RepeatedField<Element>::end() const {
  1335. return unsafe_elements() + current_size_;
  1336. }
  1337. template <typename Element>
  1338. inline typename RepeatedField<Element>::const_iterator
  1339. RepeatedField<Element>::cend() const {
  1340. return unsafe_elements() + current_size_;
  1341. }
  1342. template <typename Element>
  1343. inline size_t RepeatedField<Element>::SpaceUsedExcludingSelfLong() const {
  1344. return total_size_ > 0 ? (total_size_ * sizeof(Element) + kRepHeaderSize) : 0;
  1345. }
  1346. namespace internal {
  1347. // Returns the new size for a reserved field based on its 'total_size' and the
  1348. // requested 'new_size'. The result is clamped to the closed interval:
  1349. // [internal::kMinRepeatedFieldAllocationSize,
  1350. // std::numeric_limits<int>::max()]
  1351. // Requires:
  1352. // new_size > total_size &&
  1353. // (total_size == 0 ||
  1354. // total_size >= kRepeatedFieldLowerClampLimit)
  1355. inline int CalculateReserveSize(int total_size, int new_size) {
  1356. if (new_size < kRepeatedFieldLowerClampLimit) {
  1357. // Clamp to smallest allowed size.
  1358. return kRepeatedFieldLowerClampLimit;
  1359. }
  1360. if (total_size < kRepeatedFieldUpperClampLimit) {
  1361. return std::max(total_size * 2, new_size);
  1362. } else {
  1363. // Clamp to largest allowed size.
  1364. GOOGLE_DCHECK_GT(new_size, kRepeatedFieldUpperClampLimit);
  1365. return std::numeric_limits<int>::max();
  1366. }
  1367. }
  1368. } // namespace internal
  1369. // Avoid inlining of Reserve(): new, copy, and delete[] lead to a significant
  1370. // amount of code bloat.
  1371. template <typename Element>
  1372. void RepeatedField<Element>::Reserve(int new_size) {
  1373. if (total_size_ >= new_size) return;
  1374. Rep* old_rep = total_size_ > 0 ? rep() : NULL;
  1375. Rep* new_rep;
  1376. Arena* arena = GetArena();
  1377. new_size = internal::CalculateReserveSize(total_size_, new_size);
  1378. GOOGLE_DCHECK_LE(
  1379. static_cast<size_t>(new_size),
  1380. (std::numeric_limits<size_t>::max() - kRepHeaderSize) / sizeof(Element))
  1381. << "Requested size is too large to fit into size_t.";
  1382. size_t bytes =
  1383. kRepHeaderSize + sizeof(Element) * static_cast<size_t>(new_size);
  1384. if (arena == NULL) {
  1385. new_rep = static_cast<Rep*>(::operator new(bytes));
  1386. } else {
  1387. new_rep = reinterpret_cast<Rep*>(Arena::CreateArray<char>(arena, bytes));
  1388. }
  1389. new_rep->arena = arena;
  1390. int old_total_size = total_size_;
  1391. // Already known: new_size >= internal::kMinRepeatedFieldAllocationSize
  1392. // Maintain invariant:
  1393. // total_size_ == 0 ||
  1394. // total_size_ >= internal::kMinRepeatedFieldAllocationSize
  1395. total_size_ = new_size;
  1396. arena_or_elements_ = new_rep->elements;
  1397. // Invoke placement-new on newly allocated elements. We shouldn't have to do
  1398. // this, since Element is supposed to be POD, but a previous version of this
  1399. // code allocated storage with "new Element[size]" and some code uses
  1400. // RepeatedField with non-POD types, relying on constructor invocation. If
  1401. // Element has a trivial constructor (e.g., int32_t), gcc (tested with -O2)
  1402. // completely removes this loop because the loop body is empty, so this has no
  1403. // effect unless its side-effects are required for correctness.
  1404. // Note that we do this before MoveArray() below because Element's copy
  1405. // assignment implementation will want an initialized instance first.
  1406. Element* e = &elements()[0];
  1407. Element* limit = e + total_size_;
  1408. for (; e < limit; e++) {
  1409. new (e) Element;
  1410. }
  1411. if (current_size_ > 0) {
  1412. MoveArray(&elements()[0], old_rep->elements, current_size_);
  1413. }
  1414. // Likewise, we need to invoke destructors on the old array.
  1415. InternalDeallocate(old_rep, old_total_size);
  1416. }
  1417. template <typename Element>
  1418. inline void RepeatedField<Element>::Truncate(int new_size) {
  1419. GOOGLE_DCHECK_LE(new_size, current_size_);
  1420. if (current_size_ > 0) {
  1421. current_size_ = new_size;
  1422. }
  1423. }
  1424. template <typename Element>
  1425. inline void RepeatedField<Element>::MoveArray(Element* to, Element* from,
  1426. int array_size) {
  1427. CopyArray(to, from, array_size);
  1428. }
  1429. template <typename Element>
  1430. inline void RepeatedField<Element>::CopyArray(Element* to, const Element* from,
  1431. int array_size) {
  1432. internal::ElementCopier<Element>()(to, from, array_size);
  1433. }
  1434. namespace internal {
  1435. template <typename Element, bool HasTrivialCopy>
  1436. void ElementCopier<Element, HasTrivialCopy>::operator()(Element* to,
  1437. const Element* from,
  1438. int array_size) {
  1439. std::copy(from, from + array_size, to);
  1440. }
  1441. template <typename Element>
  1442. struct ElementCopier<Element, true> {
  1443. void operator()(Element* to, const Element* from, int array_size) {
  1444. memcpy(to, from, static_cast<size_t>(array_size) * sizeof(Element));
  1445. }
  1446. };
  1447. } // namespace internal
  1448. // -------------------------------------------------------------------
  1449. namespace internal {
  1450. constexpr RepeatedPtrFieldBase::RepeatedPtrFieldBase()
  1451. : arena_(NULL), current_size_(0), total_size_(0), rep_(NULL) {}
  1452. inline RepeatedPtrFieldBase::RepeatedPtrFieldBase(Arena* arena)
  1453. : arena_(arena), current_size_(0), total_size_(0), rep_(NULL) {}
  1454. template <typename TypeHandler>
  1455. void RepeatedPtrFieldBase::Destroy() {
  1456. if (rep_ != NULL && arena_ == NULL) {
  1457. int n = rep_->allocated_size;
  1458. void* const* elements = rep_->elements;
  1459. for (int i = 0; i < n; i++) {
  1460. TypeHandler::Delete(cast<TypeHandler>(elements[i]), NULL);
  1461. }
  1462. #if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation)
  1463. const size_t size = total_size_ * sizeof(elements[0]) + kRepHeaderSize;
  1464. ::operator delete(static_cast<void*>(rep_), size);
  1465. #else
  1466. ::operator delete(static_cast<void*>(rep_));
  1467. #endif
  1468. }
  1469. rep_ = NULL;
  1470. }
  1471. template <typename TypeHandler>
  1472. inline void RepeatedPtrFieldBase::Swap(RepeatedPtrFieldBase* other) {
  1473. #ifdef PROTOBUF_FORCE_COPY_IN_SWAP
  1474. if (GetArena() != nullptr && GetArena() == other->GetArena()) {
  1475. #else // PROTOBUF_FORCE_COPY_IN_SWAP
  1476. if (GetArena() == other->GetArena()) {
  1477. #endif // !PROTOBUF_FORCE_COPY_IN_SWAP
  1478. InternalSwap(other);
  1479. } else {
  1480. SwapFallback<TypeHandler>(other);
  1481. }
  1482. }
  1483. template <typename TypeHandler>
  1484. void RepeatedPtrFieldBase::SwapFallback(RepeatedPtrFieldBase* other) {
  1485. #ifdef PROTOBUF_FORCE_COPY_IN_SWAP
  1486. GOOGLE_DCHECK(GetArena() == nullptr || other->GetArena() != GetArena());
  1487. #else // PROTOBUF_FORCE_COPY_IN_SWAP
  1488. GOOGLE_DCHECK(other->GetArena() != GetArena());
  1489. #endif // !PROTOBUF_FORCE_COPY_IN_SWAP
  1490. // Copy semantics in this case. We try to improve efficiency by placing the
  1491. // temporary on |other|'s arena so that messages are copied twice rather than
  1492. // three times.
  1493. RepeatedPtrFieldBase temp(other->GetArena());
  1494. temp.MergeFrom<TypeHandler>(*this);
  1495. this->Clear<TypeHandler>();
  1496. this->MergeFrom<TypeHandler>(*other);
  1497. other->InternalSwap(&temp);
  1498. temp.Destroy<TypeHandler>(); // Frees rep_ if `other` had no arena.
  1499. }
  1500. inline bool RepeatedPtrFieldBase::empty() const { return current_size_ == 0; }
  1501. inline int RepeatedPtrFieldBase::size() const { return current_size_; }
  1502. template <typename TypeHandler>
  1503. inline const typename TypeHandler::Type& RepeatedPtrFieldBase::Get(
  1504. int index) const {
  1505. GOOGLE_DCHECK_GE(index, 0);
  1506. GOOGLE_DCHECK_LT(index, current_size_);
  1507. return *cast<TypeHandler>(rep_->elements[index]);
  1508. }
  1509. template <typename TypeHandler>
  1510. inline const typename TypeHandler::Type& RepeatedPtrFieldBase::at(
  1511. int index) const {
  1512. GOOGLE_CHECK_GE(index, 0);
  1513. GOOGLE_CHECK_LT(index, current_size_);
  1514. return *cast<TypeHandler>(rep_->elements[index]);
  1515. }
  1516. template <typename TypeHandler>
  1517. inline typename TypeHandler::Type& RepeatedPtrFieldBase::at(int index) {
  1518. GOOGLE_CHECK_GE(index, 0);
  1519. GOOGLE_CHECK_LT(index, current_size_);
  1520. return *cast<TypeHandler>(rep_->elements[index]);
  1521. }
  1522. template <typename TypeHandler>
  1523. inline typename TypeHandler::Type* RepeatedPtrFieldBase::Mutable(int index) {
  1524. GOOGLE_DCHECK_GE(index, 0);
  1525. GOOGLE_DCHECK_LT(index, current_size_);
  1526. return cast<TypeHandler>(rep_->elements[index]);
  1527. }
  1528. template <typename TypeHandler>
  1529. inline void RepeatedPtrFieldBase::Delete(int index) {
  1530. GOOGLE_DCHECK_GE(index, 0);
  1531. GOOGLE_DCHECK_LT(index, current_size_);
  1532. TypeHandler::Delete(cast<TypeHandler>(rep_->elements[index]), arena_);
  1533. }
  1534. template <typename TypeHandler>
  1535. inline typename TypeHandler::Type* RepeatedPtrFieldBase::Add(
  1536. typename TypeHandler::Type* prototype) {
  1537. if (rep_ != NULL && current_size_ < rep_->allocated_size) {
  1538. return cast<TypeHandler>(rep_->elements[current_size_++]);
  1539. }
  1540. typename TypeHandler::Type* result =
  1541. TypeHandler::NewFromPrototype(prototype, arena_);
  1542. return reinterpret_cast<typename TypeHandler::Type*>(
  1543. AddOutOfLineHelper(result));
  1544. }
  1545. template <typename TypeHandler,
  1546. typename std::enable_if<TypeHandler::Movable::value>::type*>
  1547. inline void RepeatedPtrFieldBase::Add(typename TypeHandler::Type&& value) {
  1548. if (rep_ != NULL && current_size_ < rep_->allocated_size) {
  1549. *cast<TypeHandler>(rep_->elements[current_size_++]) = std::move(value);
  1550. return;
  1551. }
  1552. if (!rep_ || rep_->allocated_size == total_size_) {
  1553. Reserve(total_size_ + 1);
  1554. }
  1555. ++rep_->allocated_size;
  1556. typename TypeHandler::Type* result =
  1557. TypeHandler::New(arena_, std::move(value));
  1558. rep_->elements[current_size_++] = result;
  1559. }
  1560. template <typename TypeHandler>
  1561. inline void RepeatedPtrFieldBase::RemoveLast() {
  1562. GOOGLE_DCHECK_GT(current_size_, 0);
  1563. TypeHandler::Clear(cast<TypeHandler>(rep_->elements[--current_size_]));
  1564. }
  1565. template <typename TypeHandler>
  1566. void RepeatedPtrFieldBase::Clear() {
  1567. const int n = current_size_;
  1568. GOOGLE_DCHECK_GE(n, 0);
  1569. if (n > 0) {
  1570. void* const* elements = rep_->elements;
  1571. int i = 0;
  1572. do {
  1573. TypeHandler::Clear(cast<TypeHandler>(elements[i++]));
  1574. } while (i < n);
  1575. current_size_ = 0;
  1576. }
  1577. }
  1578. // To avoid unnecessary code duplication and reduce binary size, we use a
  1579. // layered approach to implementing MergeFrom(). The toplevel method is
  1580. // templated, so we get a small thunk per concrete message type in the binary.
  1581. // This calls a shared implementation with most of the logic, passing a function
  1582. // pointer to another type-specific piece of code that calls the object-allocate
  1583. // and merge handlers.
  1584. template <typename TypeHandler>
  1585. inline void RepeatedPtrFieldBase::MergeFrom(const RepeatedPtrFieldBase& other) {
  1586. GOOGLE_DCHECK_NE(&other, this);
  1587. if (other.current_size_ == 0) return;
  1588. MergeFromInternal(other,
  1589. &RepeatedPtrFieldBase::MergeFromInnerLoop<TypeHandler>);
  1590. }
  1591. inline void RepeatedPtrFieldBase::MergeFromInternal(
  1592. const RepeatedPtrFieldBase& other,
  1593. void (RepeatedPtrFieldBase::*inner_loop)(void**, void**, int, int)) {
  1594. // Note: wrapper has already guaranteed that other.rep_ != NULL here.
  1595. int other_size = other.current_size_;
  1596. void** other_elements = other.rep_->elements;
  1597. void** new_elements = InternalExtend(other_size);
  1598. int allocated_elems = rep_->allocated_size - current_size_;
  1599. (this->*inner_loop)(new_elements, other_elements, other_size,
  1600. allocated_elems);
  1601. current_size_ += other_size;
  1602. if (rep_->allocated_size < current_size_) {
  1603. rep_->allocated_size = current_size_;
  1604. }
  1605. }
  1606. // Merges other_elems to our_elems.
  1607. template <typename TypeHandler>
  1608. void RepeatedPtrFieldBase::MergeFromInnerLoop(void** our_elems,
  1609. void** other_elems, int length,
  1610. int already_allocated) {
  1611. if (already_allocated < length) {
  1612. Arena* arena = GetArena();
  1613. typename TypeHandler::Type* elem_prototype =
  1614. reinterpret_cast<typename TypeHandler::Type*>(other_elems[0]);
  1615. for (int i = already_allocated; i < length; i++) {
  1616. // Allocate a new empty element that we'll merge into below
  1617. typename TypeHandler::Type* new_elem =
  1618. TypeHandler::NewFromPrototype(elem_prototype, arena);
  1619. our_elems[i] = new_elem;
  1620. }
  1621. }
  1622. // Main loop that does the actual merging
  1623. for (int i = 0; i < length; i++) {
  1624. // Already allocated: use existing element.
  1625. typename TypeHandler::Type* other_elem =
  1626. reinterpret_cast<typename TypeHandler::Type*>(other_elems[i]);
  1627. typename TypeHandler::Type* new_elem =
  1628. reinterpret_cast<typename TypeHandler::Type*>(our_elems[i]);
  1629. TypeHandler::Merge(*other_elem, new_elem);
  1630. }
  1631. }
  1632. template <typename TypeHandler>
  1633. inline void RepeatedPtrFieldBase::CopyFrom(const RepeatedPtrFieldBase& other) {
  1634. if (&other == this) return;
  1635. RepeatedPtrFieldBase::Clear<TypeHandler>();
  1636. RepeatedPtrFieldBase::MergeFrom<TypeHandler>(other);
  1637. }
  1638. inline int RepeatedPtrFieldBase::Capacity() const { return total_size_; }
  1639. inline void* const* RepeatedPtrFieldBase::raw_data() const {
  1640. return rep_ ? rep_->elements : NULL;
  1641. }
  1642. inline void** RepeatedPtrFieldBase::raw_mutable_data() const {
  1643. return rep_ ? const_cast<void**>(rep_->elements) : NULL;
  1644. }
  1645. template <typename TypeHandler>
  1646. inline typename TypeHandler::Type** RepeatedPtrFieldBase::mutable_data() {
  1647. // TODO(kenton): Breaks C++ aliasing rules. We should probably remove this
  1648. // method entirely.
  1649. return reinterpret_cast<typename TypeHandler::Type**>(raw_mutable_data());
  1650. }
  1651. template <typename TypeHandler>
  1652. inline const typename TypeHandler::Type* const* RepeatedPtrFieldBase::data()
  1653. const {
  1654. // TODO(kenton): Breaks C++ aliasing rules. We should probably remove this
  1655. // method entirely.
  1656. return reinterpret_cast<const typename TypeHandler::Type* const*>(raw_data());
  1657. }
  1658. inline void RepeatedPtrFieldBase::SwapElements(int index1, int index2) {
  1659. using std::swap; // enable ADL with fallback
  1660. swap(rep_->elements[index1], rep_->elements[index2]);
  1661. }
  1662. template <typename TypeHandler>
  1663. inline size_t RepeatedPtrFieldBase::SpaceUsedExcludingSelfLong() const {
  1664. size_t allocated_bytes = static_cast<size_t>(total_size_) * sizeof(void*);
  1665. if (rep_ != NULL) {
  1666. for (int i = 0; i < rep_->allocated_size; ++i) {
  1667. allocated_bytes +=
  1668. TypeHandler::SpaceUsedLong(*cast<TypeHandler>(rep_->elements[i]));
  1669. }
  1670. allocated_bytes += kRepHeaderSize;
  1671. }
  1672. return allocated_bytes;
  1673. }
  1674. template <typename TypeHandler>
  1675. inline typename TypeHandler::Type* RepeatedPtrFieldBase::AddFromCleared() {
  1676. if (rep_ != NULL && current_size_ < rep_->allocated_size) {
  1677. return cast<TypeHandler>(rep_->elements[current_size_++]);
  1678. } else {
  1679. return NULL;
  1680. }
  1681. }
  1682. // AddAllocated version that implements arena-safe copying behavior.
  1683. template <typename TypeHandler>
  1684. void RepeatedPtrFieldBase::AddAllocatedInternal(
  1685. typename TypeHandler::Type* value, std::true_type) {
  1686. Arena* element_arena =
  1687. reinterpret_cast<Arena*>(TypeHandler::GetOwningArena(value));
  1688. Arena* arena = GetArena();
  1689. if (arena == element_arena && rep_ && rep_->allocated_size < total_size_) {
  1690. // Fast path: underlying arena representation (tagged pointer) is equal to
  1691. // our arena pointer, and we can add to array without resizing it (at least
  1692. // one slot that is not allocated).
  1693. void** elems = rep_->elements;
  1694. if (current_size_ < rep_->allocated_size) {
  1695. // Make space at [current] by moving first allocated element to end of
  1696. // allocated list.
  1697. elems[rep_->allocated_size] = elems[current_size_];
  1698. }
  1699. elems[current_size_] = value;
  1700. current_size_ = current_size_ + 1;
  1701. rep_->allocated_size = rep_->allocated_size + 1;
  1702. } else {
  1703. AddAllocatedSlowWithCopy<TypeHandler>(value, element_arena, arena);
  1704. }
  1705. }
  1706. // Slowpath handles all cases, copying if necessary.
  1707. template <typename TypeHandler>
  1708. void RepeatedPtrFieldBase::AddAllocatedSlowWithCopy(
  1709. // Pass value_arena and my_arena to avoid duplicate virtual call (value) or
  1710. // load (mine).
  1711. typename TypeHandler::Type* value, Arena* value_arena, Arena* my_arena) {
  1712. // Ensure that either the value is in the same arena, or if not, we do the
  1713. // appropriate thing: Own() it (if it's on heap and we're in an arena) or copy
  1714. // it to our arena/heap (otherwise).
  1715. if (my_arena != NULL && value_arena == NULL) {
  1716. my_arena->Own(value);
  1717. } else if (my_arena != value_arena) {
  1718. typename TypeHandler::Type* new_value =
  1719. TypeHandler::NewFromPrototype(value, my_arena);
  1720. TypeHandler::Merge(*value, new_value);
  1721. TypeHandler::Delete(value, value_arena);
  1722. value = new_value;
  1723. }
  1724. UnsafeArenaAddAllocated<TypeHandler>(value);
  1725. }
  1726. // AddAllocated version that does not implement arena-safe copying behavior.
  1727. template <typename TypeHandler>
  1728. void RepeatedPtrFieldBase::AddAllocatedInternal(
  1729. typename TypeHandler::Type* value, std::false_type) {
  1730. if (rep_ && rep_->allocated_size < total_size_) {
  1731. // Fast path: underlying arena representation (tagged pointer) is equal to
  1732. // our arena pointer, and we can add to array without resizing it (at least
  1733. // one slot that is not allocated).
  1734. void** elems = rep_->elements;
  1735. if (current_size_ < rep_->allocated_size) {
  1736. // Make space at [current] by moving first allocated element to end of
  1737. // allocated list.
  1738. elems[rep_->allocated_size] = elems[current_size_];
  1739. }
  1740. elems[current_size_] = value;
  1741. current_size_ = current_size_ + 1;
  1742. ++rep_->allocated_size;
  1743. } else {
  1744. UnsafeArenaAddAllocated<TypeHandler>(value);
  1745. }
  1746. }
  1747. template <typename TypeHandler>
  1748. void RepeatedPtrFieldBase::UnsafeArenaAddAllocated(
  1749. typename TypeHandler::Type* value) {
  1750. // Make room for the new pointer.
  1751. if (!rep_ || current_size_ == total_size_) {
  1752. // The array is completely full with no cleared objects, so grow it.
  1753. Reserve(total_size_ + 1);
  1754. ++rep_->allocated_size;
  1755. } else if (rep_->allocated_size == total_size_) {
  1756. // There is no more space in the pointer array because it contains some
  1757. // cleared objects awaiting reuse. We don't want to grow the array in this
  1758. // case because otherwise a loop calling AddAllocated() followed by Clear()
  1759. // would leak memory.
  1760. TypeHandler::Delete(cast<TypeHandler>(rep_->elements[current_size_]),
  1761. arena_);
  1762. } else if (current_size_ < rep_->allocated_size) {
  1763. // We have some cleared objects. We don't care about their order, so we
  1764. // can just move the first one to the end to make space.
  1765. rep_->elements[rep_->allocated_size] = rep_->elements[current_size_];
  1766. ++rep_->allocated_size;
  1767. } else {
  1768. // There are no cleared objects.
  1769. ++rep_->allocated_size;
  1770. }
  1771. rep_->elements[current_size_++] = value;
  1772. }
  1773. // ReleaseLast() for types that implement merge/copy behavior.
  1774. template <typename TypeHandler>
  1775. inline typename TypeHandler::Type* RepeatedPtrFieldBase::ReleaseLastInternal(
  1776. std::true_type) {
  1777. // First, release an element.
  1778. typename TypeHandler::Type* result = UnsafeArenaReleaseLast<TypeHandler>();
  1779. // Now perform a copy if we're on an arena.
  1780. Arena* arena = GetArena();
  1781. typename TypeHandler::Type* new_result;
  1782. #ifdef PROTOBUF_FORCE_COPY_IN_RELEASE
  1783. new_result = copy<TypeHandler>(result);
  1784. if (arena == nullptr) delete result;
  1785. #else // PROTOBUF_FORCE_COPY_IN_RELEASE
  1786. new_result = (arena == nullptr) ? result : copy<TypeHandler>(result);
  1787. #endif // !PROTOBUF_FORCE_COPY_IN_RELEASE
  1788. return new_result;
  1789. }
  1790. // ReleaseLast() for types that *do not* implement merge/copy behavior -- this
  1791. // is the same as UnsafeArenaReleaseLast(). Note that we GOOGLE_DCHECK-fail if we're on
  1792. // an arena, since the user really should implement the copy operation in this
  1793. // case.
  1794. template <typename TypeHandler>
  1795. inline typename TypeHandler::Type* RepeatedPtrFieldBase::ReleaseLastInternal(
  1796. std::false_type) {
  1797. GOOGLE_DCHECK(GetArena() == nullptr)
  1798. << "ReleaseLast() called on a RepeatedPtrField that is on an arena, "
  1799. << "with a type that does not implement MergeFrom. This is unsafe; "
  1800. << "please implement MergeFrom for your type.";
  1801. return UnsafeArenaReleaseLast<TypeHandler>();
  1802. }
  1803. template <typename TypeHandler>
  1804. inline typename TypeHandler::Type*
  1805. RepeatedPtrFieldBase::UnsafeArenaReleaseLast() {
  1806. GOOGLE_DCHECK_GT(current_size_, 0);
  1807. typename TypeHandler::Type* result =
  1808. cast<TypeHandler>(rep_->elements[--current_size_]);
  1809. --rep_->allocated_size;
  1810. if (current_size_ < rep_->allocated_size) {
  1811. // There are cleared elements on the end; replace the removed element
  1812. // with the last allocated element.
  1813. rep_->elements[current_size_] = rep_->elements[rep_->allocated_size];
  1814. }
  1815. return result;
  1816. }
  1817. inline int RepeatedPtrFieldBase::ClearedCount() const {
  1818. return rep_ ? (rep_->allocated_size - current_size_) : 0;
  1819. }
  1820. template <typename TypeHandler>
  1821. inline void RepeatedPtrFieldBase::AddCleared(
  1822. typename TypeHandler::Type* value) {
  1823. GOOGLE_DCHECK(GetArena() == NULL)
  1824. << "AddCleared() can only be used on a RepeatedPtrField not on an arena.";
  1825. GOOGLE_DCHECK(TypeHandler::GetOwningArena(value) == nullptr)
  1826. << "AddCleared() can only accept values not on an arena.";
  1827. if (!rep_ || rep_->allocated_size == total_size_) {
  1828. Reserve(total_size_ + 1);
  1829. }
  1830. rep_->elements[rep_->allocated_size++] = value;
  1831. }
  1832. template <typename TypeHandler>
  1833. inline typename TypeHandler::Type* RepeatedPtrFieldBase::ReleaseCleared() {
  1834. GOOGLE_DCHECK(GetArena() == NULL)
  1835. << "ReleaseCleared() can only be used on a RepeatedPtrField not on "
  1836. << "an arena.";
  1837. GOOGLE_DCHECK(GetArena() == NULL);
  1838. GOOGLE_DCHECK(rep_ != NULL);
  1839. GOOGLE_DCHECK_GT(rep_->allocated_size, current_size_);
  1840. return cast<TypeHandler>(rep_->elements[--rep_->allocated_size]);
  1841. }
  1842. } // namespace internal
  1843. // -------------------------------------------------------------------
  1844. template <typename Element>
  1845. class RepeatedPtrField<Element>::TypeHandler
  1846. : public internal::GenericTypeHandler<Element> {};
  1847. template <>
  1848. class RepeatedPtrField<std::string>::TypeHandler
  1849. : public internal::StringTypeHandler {};
  1850. template <typename Element>
  1851. constexpr RepeatedPtrField<Element>::RepeatedPtrField()
  1852. : RepeatedPtrFieldBase() {}
  1853. template <typename Element>
  1854. inline RepeatedPtrField<Element>::RepeatedPtrField(Arena* arena)
  1855. : RepeatedPtrFieldBase(arena) {}
  1856. template <typename Element>
  1857. inline RepeatedPtrField<Element>::RepeatedPtrField(
  1858. const RepeatedPtrField& other)
  1859. : RepeatedPtrFieldBase() {
  1860. MergeFrom(other);
  1861. }
  1862. template <typename Element>
  1863. template <typename Iter, typename>
  1864. inline RepeatedPtrField<Element>::RepeatedPtrField(Iter begin, Iter end) {
  1865. Add(begin, end);
  1866. }
  1867. template <typename Element>
  1868. RepeatedPtrField<Element>::~RepeatedPtrField() {
  1869. Destroy<TypeHandler>();
  1870. }
  1871. template <typename Element>
  1872. inline RepeatedPtrField<Element>& RepeatedPtrField<Element>::operator=(
  1873. const RepeatedPtrField& other) {
  1874. if (this != &other) CopyFrom(other);
  1875. return *this;
  1876. }
  1877. template <typename Element>
  1878. inline RepeatedPtrField<Element>::RepeatedPtrField(
  1879. RepeatedPtrField&& other) noexcept
  1880. : RepeatedPtrField() {
  1881. #ifdef PROTOBUF_FORCE_COPY_IN_MOVE
  1882. CopyFrom(other);
  1883. #else // PROTOBUF_FORCE_COPY_IN_MOVE
  1884. // We don't just call Swap(&other) here because it would perform 3 copies if
  1885. // other is on an arena. This field can't be on an arena because arena
  1886. // construction always uses the Arena* accepting constructor.
  1887. if (other.GetArena()) {
  1888. CopyFrom(other);
  1889. } else {
  1890. InternalSwap(&other);
  1891. }
  1892. #endif // !PROTOBUF_FORCE_COPY_IN_MOVE
  1893. }
  1894. template <typename Element>
  1895. inline RepeatedPtrField<Element>& RepeatedPtrField<Element>::operator=(
  1896. RepeatedPtrField&& other) noexcept {
  1897. // We don't just call Swap(&other) here because it would perform 3 copies if
  1898. // the two fields are on different arenas.
  1899. if (this != &other) {
  1900. if (GetArena() != other.GetArena()
  1901. #ifdef PROTOBUF_FORCE_COPY_IN_MOVE
  1902. || GetArena() == nullptr
  1903. #endif // !PROTOBUF_FORCE_COPY_IN_MOVE
  1904. ) {
  1905. CopyFrom(other);
  1906. } else {
  1907. InternalSwap(&other);
  1908. }
  1909. }
  1910. return *this;
  1911. }
  1912. template <typename Element>
  1913. inline bool RepeatedPtrField<Element>::empty() const {
  1914. return RepeatedPtrFieldBase::empty();
  1915. }
  1916. template <typename Element>
  1917. inline int RepeatedPtrField<Element>::size() const {
  1918. return RepeatedPtrFieldBase::size();
  1919. }
  1920. template <typename Element>
  1921. inline const Element& RepeatedPtrField<Element>::Get(int index) const {
  1922. return RepeatedPtrFieldBase::Get<TypeHandler>(index);
  1923. }
  1924. template <typename Element>
  1925. inline const Element& RepeatedPtrField<Element>::at(int index) const {
  1926. return RepeatedPtrFieldBase::at<TypeHandler>(index);
  1927. }
  1928. template <typename Element>
  1929. inline Element& RepeatedPtrField<Element>::at(int index) {
  1930. return RepeatedPtrFieldBase::at<TypeHandler>(index);
  1931. }
  1932. template <typename Element>
  1933. inline Element* RepeatedPtrField<Element>::Mutable(int index) {
  1934. return RepeatedPtrFieldBase::Mutable<TypeHandler>(index);
  1935. }
  1936. template <typename Element>
  1937. inline Element* RepeatedPtrField<Element>::Add() {
  1938. return RepeatedPtrFieldBase::Add<TypeHandler>();
  1939. }
  1940. template <typename Element>
  1941. inline void RepeatedPtrField<Element>::Add(Element&& value) {
  1942. RepeatedPtrFieldBase::Add<TypeHandler>(std::move(value));
  1943. }
  1944. template <typename Element>
  1945. template <typename Iter>
  1946. inline void RepeatedPtrField<Element>::Add(Iter begin, Iter end) {
  1947. int reserve = internal::CalculateReserve(begin, end);
  1948. if (reserve != -1) {
  1949. Reserve(size() + reserve);
  1950. }
  1951. for (; begin != end; ++begin) {
  1952. *Add() = *begin;
  1953. }
  1954. }
  1955. template <typename Element>
  1956. inline void RepeatedPtrField<Element>::RemoveLast() {
  1957. RepeatedPtrFieldBase::RemoveLast<TypeHandler>();
  1958. }
  1959. template <typename Element>
  1960. inline void RepeatedPtrField<Element>::DeleteSubrange(int start, int num) {
  1961. GOOGLE_DCHECK_GE(start, 0);
  1962. GOOGLE_DCHECK_GE(num, 0);
  1963. GOOGLE_DCHECK_LE(start + num, size());
  1964. for (int i = 0; i < num; ++i) {
  1965. RepeatedPtrFieldBase::Delete<TypeHandler>(start + i);
  1966. }
  1967. UnsafeArenaExtractSubrange(start, num, nullptr);
  1968. }
  1969. template <typename Element>
  1970. inline void RepeatedPtrField<Element>::ExtractSubrange(int start, int num,
  1971. Element** elements) {
  1972. typename internal::TypeImplementsMergeBehavior<
  1973. typename TypeHandler::Type>::type t;
  1974. ExtractSubrangeInternal(start, num, elements, t);
  1975. }
  1976. // ExtractSubrange() implementation for types that implement merge/copy
  1977. // behavior.
  1978. template <typename Element>
  1979. inline void RepeatedPtrField<Element>::ExtractSubrangeInternal(
  1980. int start, int num, Element** elements, std::true_type) {
  1981. GOOGLE_DCHECK_GE(start, 0);
  1982. GOOGLE_DCHECK_GE(num, 0);
  1983. GOOGLE_DCHECK_LE(start + num, size());
  1984. if (num == 0) return;
  1985. GOOGLE_DCHECK_NE(elements, nullptr)
  1986. << "Releasing elements without transferring ownership is an unsafe "
  1987. "operation. Use UnsafeArenaExtractSubrange.";
  1988. if (elements == nullptr) {
  1989. CloseGap(start, num);
  1990. return;
  1991. }
  1992. Arena* arena = GetArena();
  1993. #ifdef PROTOBUF_FORCE_COPY_IN_RELEASE
  1994. // Always copy.
  1995. for (int i = 0; i < num; ++i) {
  1996. elements[i] = copy<TypeHandler>(
  1997. RepeatedPtrFieldBase::Mutable<TypeHandler>(i + start));
  1998. }
  1999. if (arena == nullptr) {
  2000. for (int i = 0; i < num; ++i) {
  2001. delete RepeatedPtrFieldBase::Mutable<TypeHandler>(i + start);
  2002. }
  2003. }
  2004. #else // PROTOBUF_FORCE_COPY_IN_RELEASE
  2005. // If we're on an arena, we perform a copy for each element so that the
  2006. // returned elements are heap-allocated. Otherwise, just forward it.
  2007. if (arena != nullptr) {
  2008. for (int i = 0; i < num; ++i) {
  2009. elements[i] = copy<TypeHandler>(
  2010. RepeatedPtrFieldBase::Mutable<TypeHandler>(i + start));
  2011. }
  2012. } else {
  2013. for (int i = 0; i < num; ++i) {
  2014. elements[i] = RepeatedPtrFieldBase::Mutable<TypeHandler>(i + start);
  2015. }
  2016. }
  2017. #endif // !PROTOBUF_FORCE_COPY_IN_RELEASE
  2018. CloseGap(start, num);
  2019. }
  2020. // ExtractSubrange() implementation for types that do not implement merge/copy
  2021. // behavior.
  2022. template <typename Element>
  2023. inline void RepeatedPtrField<Element>::ExtractSubrangeInternal(
  2024. int start, int num, Element** elements, std::false_type) {
  2025. // This case is identical to UnsafeArenaExtractSubrange(). However, since
  2026. // ExtractSubrange() must return heap-allocated objects by contract, and we
  2027. // cannot fulfill this contract if we are an on arena, we must GOOGLE_DCHECK() that
  2028. // we are not on an arena.
  2029. GOOGLE_DCHECK(GetArena() == NULL)
  2030. << "ExtractSubrange() when arena is non-NULL is only supported when "
  2031. << "the Element type supplies a MergeFrom() operation to make copies.";
  2032. UnsafeArenaExtractSubrange(start, num, elements);
  2033. }
  2034. template <typename Element>
  2035. inline void RepeatedPtrField<Element>::UnsafeArenaExtractSubrange(
  2036. int start, int num, Element** elements) {
  2037. GOOGLE_DCHECK_GE(start, 0);
  2038. GOOGLE_DCHECK_GE(num, 0);
  2039. GOOGLE_DCHECK_LE(start + num, size());
  2040. if (num > 0) {
  2041. // Save the values of the removed elements if requested.
  2042. if (elements != NULL) {
  2043. for (int i = 0; i < num; ++i) {
  2044. elements[i] = RepeatedPtrFieldBase::Mutable<TypeHandler>(i + start);
  2045. }
  2046. }
  2047. CloseGap(start, num);
  2048. }
  2049. }
  2050. template <typename Element>
  2051. inline void RepeatedPtrField<Element>::Clear() {
  2052. RepeatedPtrFieldBase::Clear<TypeHandler>();
  2053. }
  2054. template <typename Element>
  2055. inline void RepeatedPtrField<Element>::MergeFrom(
  2056. const RepeatedPtrField& other) {
  2057. RepeatedPtrFieldBase::MergeFrom<TypeHandler>(other);
  2058. }
  2059. template <typename Element>
  2060. inline void RepeatedPtrField<Element>::CopyFrom(const RepeatedPtrField& other) {
  2061. RepeatedPtrFieldBase::CopyFrom<TypeHandler>(other);
  2062. }
  2063. template <typename Element>
  2064. template <typename Iter>
  2065. inline void RepeatedPtrField<Element>::Assign(Iter begin, Iter end) {
  2066. Clear();
  2067. Add(begin, end);
  2068. }
  2069. template <typename Element>
  2070. inline typename RepeatedPtrField<Element>::iterator
  2071. RepeatedPtrField<Element>::erase(const_iterator position) {
  2072. return erase(position, position + 1);
  2073. }
  2074. template <typename Element>
  2075. inline typename RepeatedPtrField<Element>::iterator
  2076. RepeatedPtrField<Element>::erase(const_iterator first, const_iterator last) {
  2077. size_type pos_offset = std::distance(cbegin(), first);
  2078. size_type last_offset = std::distance(cbegin(), last);
  2079. DeleteSubrange(pos_offset, last_offset - pos_offset);
  2080. return begin() + pos_offset;
  2081. }
  2082. template <typename Element>
  2083. inline Element** RepeatedPtrField<Element>::mutable_data() {
  2084. return RepeatedPtrFieldBase::mutable_data<TypeHandler>();
  2085. }
  2086. template <typename Element>
  2087. inline const Element* const* RepeatedPtrField<Element>::data() const {
  2088. return RepeatedPtrFieldBase::data<TypeHandler>();
  2089. }
  2090. template <typename Element>
  2091. inline void RepeatedPtrField<Element>::Swap(RepeatedPtrField* other) {
  2092. if (this == other) return;
  2093. RepeatedPtrFieldBase::Swap<TypeHandler>(other);
  2094. }
  2095. template <typename Element>
  2096. inline void RepeatedPtrField<Element>::UnsafeArenaSwap(
  2097. RepeatedPtrField* other) {
  2098. if (this == other) return;
  2099. RepeatedPtrFieldBase::InternalSwap(other);
  2100. }
  2101. template <typename Element>
  2102. inline void RepeatedPtrField<Element>::SwapElements(int index1, int index2) {
  2103. RepeatedPtrFieldBase::SwapElements(index1, index2);
  2104. }
  2105. template <typename Element>
  2106. inline Arena* RepeatedPtrField<Element>::GetArena() const {
  2107. return RepeatedPtrFieldBase::GetArena();
  2108. }
  2109. template <typename Element>
  2110. inline size_t RepeatedPtrField<Element>::SpaceUsedExcludingSelfLong() const {
  2111. return RepeatedPtrFieldBase::SpaceUsedExcludingSelfLong<TypeHandler>();
  2112. }
  2113. template <typename Element>
  2114. inline void RepeatedPtrField<Element>::AddAllocated(Element* value) {
  2115. RepeatedPtrFieldBase::AddAllocated<TypeHandler>(value);
  2116. }
  2117. template <typename Element>
  2118. inline void RepeatedPtrField<Element>::UnsafeArenaAddAllocated(Element* value) {
  2119. RepeatedPtrFieldBase::UnsafeArenaAddAllocated<TypeHandler>(value);
  2120. }
  2121. template <typename Element>
  2122. inline Element* RepeatedPtrField<Element>::ReleaseLast() {
  2123. return RepeatedPtrFieldBase::ReleaseLast<TypeHandler>();
  2124. }
  2125. template <typename Element>
  2126. inline Element* RepeatedPtrField<Element>::UnsafeArenaReleaseLast() {
  2127. return RepeatedPtrFieldBase::UnsafeArenaReleaseLast<TypeHandler>();
  2128. }
  2129. template <typename Element>
  2130. inline int RepeatedPtrField<Element>::ClearedCount() const {
  2131. return RepeatedPtrFieldBase::ClearedCount();
  2132. }
  2133. template <typename Element>
  2134. inline void RepeatedPtrField<Element>::AddCleared(Element* value) {
  2135. return RepeatedPtrFieldBase::AddCleared<TypeHandler>(value);
  2136. }
  2137. template <typename Element>
  2138. inline Element* RepeatedPtrField<Element>::ReleaseCleared() {
  2139. return RepeatedPtrFieldBase::ReleaseCleared<TypeHandler>();
  2140. }
  2141. template <typename Element>
  2142. inline void RepeatedPtrField<Element>::Reserve(int new_size) {
  2143. return RepeatedPtrFieldBase::Reserve(new_size);
  2144. }
  2145. template <typename Element>
  2146. inline int RepeatedPtrField<Element>::Capacity() const {
  2147. return RepeatedPtrFieldBase::Capacity();
  2148. }
  2149. // -------------------------------------------------------------------
  2150. namespace internal {
  2151. // STL-like iterator implementation for RepeatedPtrField. You should not
  2152. // refer to this class directly; use RepeatedPtrField<T>::iterator instead.
  2153. //
  2154. // The iterator for RepeatedPtrField<T>, RepeatedPtrIterator<T>, is
  2155. // very similar to iterator_ptr<T**> in util/gtl/iterator_adaptors.h,
  2156. // but adds random-access operators and is modified to wrap a void** base
  2157. // iterator (since RepeatedPtrField stores its array as a void* array and
  2158. // casting void** to T** would violate C++ aliasing rules).
  2159. //
  2160. // This code based on net/proto/proto-array-internal.h by Jeffrey Yasskin
  2161. // (jyasskin@google.com).
  2162. template <typename Element>
  2163. class RepeatedPtrIterator {
  2164. public:
  2165. using iterator = RepeatedPtrIterator<Element>;
  2166. using iterator_category = std::random_access_iterator_tag;
  2167. using value_type = typename std::remove_const<Element>::type;
  2168. using difference_type = std::ptrdiff_t;
  2169. using pointer = Element*;
  2170. using reference = Element&;
  2171. RepeatedPtrIterator() : it_(NULL) {}
  2172. explicit RepeatedPtrIterator(void* const* it) : it_(it) {}
  2173. // Allow "upcasting" from RepeatedPtrIterator<T**> to
  2174. // RepeatedPtrIterator<const T*const*>.
  2175. template <typename OtherElement>
  2176. RepeatedPtrIterator(const RepeatedPtrIterator<OtherElement>& other)
  2177. : it_(other.it_) {
  2178. // Force a compiler error if the other type is not convertible to ours.
  2179. if (false) {
  2180. implicit_cast<Element*>(static_cast<OtherElement*>(nullptr));
  2181. }
  2182. }
  2183. // dereferenceable
  2184. reference operator*() const { return *reinterpret_cast<Element*>(*it_); }
  2185. pointer operator->() const { return &(operator*()); }
  2186. // {inc,dec}rementable
  2187. iterator& operator++() {
  2188. ++it_;
  2189. return *this;
  2190. }
  2191. iterator operator++(int) { return iterator(it_++); }
  2192. iterator& operator--() {
  2193. --it_;
  2194. return *this;
  2195. }
  2196. iterator operator--(int) { return iterator(it_--); }
  2197. // equality_comparable
  2198. bool operator==(const iterator& x) const { return it_ == x.it_; }
  2199. bool operator!=(const iterator& x) const { return it_ != x.it_; }
  2200. // less_than_comparable
  2201. bool operator<(const iterator& x) const { return it_ < x.it_; }
  2202. bool operator<=(const iterator& x) const { return it_ <= x.it_; }
  2203. bool operator>(const iterator& x) const { return it_ > x.it_; }
  2204. bool operator>=(const iterator& x) const { return it_ >= x.it_; }
  2205. // addable, subtractable
  2206. iterator& operator+=(difference_type d) {
  2207. it_ += d;
  2208. return *this;
  2209. }
  2210. friend iterator operator+(iterator it, const difference_type d) {
  2211. it += d;
  2212. return it;
  2213. }
  2214. friend iterator operator+(const difference_type d, iterator it) {
  2215. it += d;
  2216. return it;
  2217. }
  2218. iterator& operator-=(difference_type d) {
  2219. it_ -= d;
  2220. return *this;
  2221. }
  2222. friend iterator operator-(iterator it, difference_type d) {
  2223. it -= d;
  2224. return it;
  2225. }
  2226. // indexable
  2227. reference operator[](difference_type d) const { return *(*this + d); }
  2228. // random access iterator
  2229. difference_type operator-(const iterator& x) const { return it_ - x.it_; }
  2230. private:
  2231. template <typename OtherElement>
  2232. friend class RepeatedPtrIterator;
  2233. // The internal iterator.
  2234. void* const* it_;
  2235. };
  2236. // Provide an iterator that operates on pointers to the underlying objects
  2237. // rather than the objects themselves as RepeatedPtrIterator does.
  2238. // Consider using this when working with stl algorithms that change
  2239. // the array.
  2240. // The VoidPtr template parameter holds the type-agnostic pointer value
  2241. // referenced by the iterator. It should either be "void *" for a mutable
  2242. // iterator, or "const void* const" for a constant iterator.
  2243. template <typename Element, typename VoidPtr>
  2244. class RepeatedPtrOverPtrsIterator {
  2245. public:
  2246. using iterator = RepeatedPtrOverPtrsIterator<Element, VoidPtr>;
  2247. using iterator_category = std::random_access_iterator_tag;
  2248. using value_type = typename std::remove_const<Element>::type;
  2249. using difference_type = std::ptrdiff_t;
  2250. using pointer = Element*;
  2251. using reference = Element&;
  2252. RepeatedPtrOverPtrsIterator() : it_(NULL) {}
  2253. explicit RepeatedPtrOverPtrsIterator(VoidPtr* it) : it_(it) {}
  2254. // dereferenceable
  2255. reference operator*() const { return *reinterpret_cast<Element*>(it_); }
  2256. pointer operator->() const { return &(operator*()); }
  2257. // {inc,dec}rementable
  2258. iterator& operator++() {
  2259. ++it_;
  2260. return *this;
  2261. }
  2262. iterator operator++(int) { return iterator(it_++); }
  2263. iterator& operator--() {
  2264. --it_;
  2265. return *this;
  2266. }
  2267. iterator operator--(int) { return iterator(it_--); }
  2268. // equality_comparable
  2269. bool operator==(const iterator& x) const { return it_ == x.it_; }
  2270. bool operator!=(const iterator& x) const { return it_ != x.it_; }
  2271. // less_than_comparable
  2272. bool operator<(const iterator& x) const { return it_ < x.it_; }
  2273. bool operator<=(const iterator& x) const { return it_ <= x.it_; }
  2274. bool operator>(const iterator& x) const { return it_ > x.it_; }
  2275. bool operator>=(const iterator& x) const { return it_ >= x.it_; }
  2276. // addable, subtractable
  2277. iterator& operator+=(difference_type d) {
  2278. it_ += d;
  2279. return *this;
  2280. }
  2281. friend iterator operator+(iterator it, difference_type d) {
  2282. it += d;
  2283. return it;
  2284. }
  2285. friend iterator operator+(difference_type d, iterator it) {
  2286. it += d;
  2287. return it;
  2288. }
  2289. iterator& operator-=(difference_type d) {
  2290. it_ -= d;
  2291. return *this;
  2292. }
  2293. friend iterator operator-(iterator it, difference_type d) {
  2294. it -= d;
  2295. return it;
  2296. }
  2297. // indexable
  2298. reference operator[](difference_type d) const { return *(*this + d); }
  2299. // random access iterator
  2300. difference_type operator-(const iterator& x) const { return it_ - x.it_; }
  2301. private:
  2302. template <typename OtherElement>
  2303. friend class RepeatedPtrIterator;
  2304. // The internal iterator.
  2305. VoidPtr* it_;
  2306. };
  2307. void RepeatedPtrFieldBase::InternalSwap(RepeatedPtrFieldBase* other) {
  2308. GOOGLE_DCHECK(this != other);
  2309. // Swap all fields at once.
  2310. static_assert(std::is_standard_layout<RepeatedPtrFieldBase>::value,
  2311. "offsetof() requires standard layout before c++17");
  2312. internal::memswap<offsetof(RepeatedPtrFieldBase, rep_) + sizeof(this->rep_) -
  2313. offsetof(RepeatedPtrFieldBase, arena_)>(
  2314. reinterpret_cast<char*>(this) + offsetof(RepeatedPtrFieldBase, arena_),
  2315. reinterpret_cast<char*>(other) + offsetof(RepeatedPtrFieldBase, arena_));
  2316. }
  2317. } // namespace internal
  2318. template <typename Element>
  2319. inline typename RepeatedPtrField<Element>::iterator
  2320. RepeatedPtrField<Element>::begin() {
  2321. return iterator(raw_data());
  2322. }
  2323. template <typename Element>
  2324. inline typename RepeatedPtrField<Element>::const_iterator
  2325. RepeatedPtrField<Element>::begin() const {
  2326. return iterator(raw_data());
  2327. }
  2328. template <typename Element>
  2329. inline typename RepeatedPtrField<Element>::const_iterator
  2330. RepeatedPtrField<Element>::cbegin() const {
  2331. return begin();
  2332. }
  2333. template <typename Element>
  2334. inline typename RepeatedPtrField<Element>::iterator
  2335. RepeatedPtrField<Element>::end() {
  2336. return iterator(raw_data() + size());
  2337. }
  2338. template <typename Element>
  2339. inline typename RepeatedPtrField<Element>::const_iterator
  2340. RepeatedPtrField<Element>::end() const {
  2341. return iterator(raw_data() + size());
  2342. }
  2343. template <typename Element>
  2344. inline typename RepeatedPtrField<Element>::const_iterator
  2345. RepeatedPtrField<Element>::cend() const {
  2346. return end();
  2347. }
  2348. template <typename Element>
  2349. inline typename RepeatedPtrField<Element>::pointer_iterator
  2350. RepeatedPtrField<Element>::pointer_begin() {
  2351. return pointer_iterator(raw_mutable_data());
  2352. }
  2353. template <typename Element>
  2354. inline typename RepeatedPtrField<Element>::const_pointer_iterator
  2355. RepeatedPtrField<Element>::pointer_begin() const {
  2356. return const_pointer_iterator(const_cast<const void* const*>(raw_data()));
  2357. }
  2358. template <typename Element>
  2359. inline typename RepeatedPtrField<Element>::pointer_iterator
  2360. RepeatedPtrField<Element>::pointer_end() {
  2361. return pointer_iterator(raw_mutable_data() + size());
  2362. }
  2363. template <typename Element>
  2364. inline typename RepeatedPtrField<Element>::const_pointer_iterator
  2365. RepeatedPtrField<Element>::pointer_end() const {
  2366. return const_pointer_iterator(
  2367. const_cast<const void* const*>(raw_data() + size()));
  2368. }
  2369. // Iterators and helper functions that follow the spirit of the STL
  2370. // std::back_insert_iterator and std::back_inserter but are tailor-made
  2371. // for RepeatedField and RepeatedPtrField. Typical usage would be:
  2372. //
  2373. // std::copy(some_sequence.begin(), some_sequence.end(),
  2374. // RepeatedFieldBackInserter(proto.mutable_sequence()));
  2375. //
  2376. // Ported by johannes from util/gtl/proto-array-iterators.h
  2377. namespace internal {
  2378. // A back inserter for RepeatedField objects.
  2379. template <typename T>
  2380. class RepeatedFieldBackInsertIterator {
  2381. public:
  2382. using iterator_category = std::output_iterator_tag;
  2383. using value_type = T;
  2384. using pointer = void;
  2385. using reference = void;
  2386. using difference_type = std::ptrdiff_t;
  2387. explicit RepeatedFieldBackInsertIterator(
  2388. RepeatedField<T>* const mutable_field)
  2389. : field_(mutable_field) {}
  2390. RepeatedFieldBackInsertIterator<T>& operator=(const T& value) {
  2391. field_->Add(value);
  2392. return *this;
  2393. }
  2394. RepeatedFieldBackInsertIterator<T>& operator*() { return *this; }
  2395. RepeatedFieldBackInsertIterator<T>& operator++() { return *this; }
  2396. RepeatedFieldBackInsertIterator<T>& operator++(int /* unused */) {
  2397. return *this;
  2398. }
  2399. private:
  2400. RepeatedField<T>* field_;
  2401. };
  2402. // A back inserter for RepeatedPtrField objects.
  2403. template <typename T>
  2404. class RepeatedPtrFieldBackInsertIterator {
  2405. public:
  2406. using iterator_category = std::output_iterator_tag;
  2407. using value_type = T;
  2408. using pointer = void;
  2409. using reference = void;
  2410. using difference_type = std::ptrdiff_t;
  2411. RepeatedPtrFieldBackInsertIterator(RepeatedPtrField<T>* const mutable_field)
  2412. : field_(mutable_field) {}
  2413. RepeatedPtrFieldBackInsertIterator<T>& operator=(const T& value) {
  2414. *field_->Add() = value;
  2415. return *this;
  2416. }
  2417. RepeatedPtrFieldBackInsertIterator<T>& operator=(
  2418. const T* const ptr_to_value) {
  2419. *field_->Add() = *ptr_to_value;
  2420. return *this;
  2421. }
  2422. RepeatedPtrFieldBackInsertIterator<T>& operator=(T&& value) {
  2423. *field_->Add() = std::move(value);
  2424. return *this;
  2425. }
  2426. RepeatedPtrFieldBackInsertIterator<T>& operator*() { return *this; }
  2427. RepeatedPtrFieldBackInsertIterator<T>& operator++() { return *this; }
  2428. RepeatedPtrFieldBackInsertIterator<T>& operator++(int /* unused */) {
  2429. return *this;
  2430. }
  2431. private:
  2432. RepeatedPtrField<T>* field_;
  2433. };
  2434. // A back inserter for RepeatedPtrFields that inserts by transferring ownership
  2435. // of a pointer.
  2436. template <typename T>
  2437. class AllocatedRepeatedPtrFieldBackInsertIterator {
  2438. public:
  2439. using iterator_category = std::output_iterator_tag;
  2440. using value_type = T;
  2441. using pointer = void;
  2442. using reference = void;
  2443. using difference_type = std::ptrdiff_t;
  2444. explicit AllocatedRepeatedPtrFieldBackInsertIterator(
  2445. RepeatedPtrField<T>* const mutable_field)
  2446. : field_(mutable_field) {}
  2447. AllocatedRepeatedPtrFieldBackInsertIterator<T>& operator=(
  2448. T* const ptr_to_value) {
  2449. field_->AddAllocated(ptr_to_value);
  2450. return *this;
  2451. }
  2452. AllocatedRepeatedPtrFieldBackInsertIterator<T>& operator*() { return *this; }
  2453. AllocatedRepeatedPtrFieldBackInsertIterator<T>& operator++() { return *this; }
  2454. AllocatedRepeatedPtrFieldBackInsertIterator<T>& operator++(int /* unused */) {
  2455. return *this;
  2456. }
  2457. private:
  2458. RepeatedPtrField<T>* field_;
  2459. };
  2460. // Almost identical to AllocatedRepeatedPtrFieldBackInsertIterator. This one
  2461. // uses the UnsafeArenaAddAllocated instead.
  2462. template <typename T>
  2463. class UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator {
  2464. public:
  2465. using iterator_category = std::output_iterator_tag;
  2466. using value_type = T;
  2467. using pointer = void;
  2468. using reference = void;
  2469. using difference_type = std::ptrdiff_t;
  2470. explicit UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator(
  2471. RepeatedPtrField<T>* const mutable_field)
  2472. : field_(mutable_field) {}
  2473. UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator<T>& operator=(
  2474. T const* const ptr_to_value) {
  2475. field_->UnsafeArenaAddAllocated(const_cast<T*>(ptr_to_value));
  2476. return *this;
  2477. }
  2478. UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator<T>& operator*() {
  2479. return *this;
  2480. }
  2481. UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator<T>& operator++() {
  2482. return *this;
  2483. }
  2484. UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator<T>& operator++(
  2485. int /* unused */) {
  2486. return *this;
  2487. }
  2488. private:
  2489. RepeatedPtrField<T>* field_;
  2490. };
  2491. } // namespace internal
  2492. // Provides a back insert iterator for RepeatedField instances,
  2493. // similar to std::back_inserter().
  2494. template <typename T>
  2495. internal::RepeatedFieldBackInsertIterator<T> RepeatedFieldBackInserter(
  2496. RepeatedField<T>* const mutable_field) {
  2497. return internal::RepeatedFieldBackInsertIterator<T>(mutable_field);
  2498. }
  2499. // Provides a back insert iterator for RepeatedPtrField instances,
  2500. // similar to std::back_inserter().
  2501. template <typename T>
  2502. internal::RepeatedPtrFieldBackInsertIterator<T> RepeatedPtrFieldBackInserter(
  2503. RepeatedPtrField<T>* const mutable_field) {
  2504. return internal::RepeatedPtrFieldBackInsertIterator<T>(mutable_field);
  2505. }
  2506. // Special back insert iterator for RepeatedPtrField instances, just in
  2507. // case someone wants to write generic template code that can access both
  2508. // RepeatedFields and RepeatedPtrFields using a common name.
  2509. template <typename T>
  2510. internal::RepeatedPtrFieldBackInsertIterator<T> RepeatedFieldBackInserter(
  2511. RepeatedPtrField<T>* const mutable_field) {
  2512. return internal::RepeatedPtrFieldBackInsertIterator<T>(mutable_field);
  2513. }
  2514. // Provides a back insert iterator for RepeatedPtrField instances
  2515. // similar to std::back_inserter() which transfers the ownership while
  2516. // copying elements.
  2517. template <typename T>
  2518. internal::AllocatedRepeatedPtrFieldBackInsertIterator<T>
  2519. AllocatedRepeatedPtrFieldBackInserter(
  2520. RepeatedPtrField<T>* const mutable_field) {
  2521. return internal::AllocatedRepeatedPtrFieldBackInsertIterator<T>(
  2522. mutable_field);
  2523. }
  2524. // Similar to AllocatedRepeatedPtrFieldBackInserter, using
  2525. // UnsafeArenaAddAllocated instead of AddAllocated.
  2526. // This is slightly faster if that matters. It is also useful in legacy code
  2527. // that uses temporary ownership to avoid copies. Example:
  2528. // RepeatedPtrField<T> temp_field;
  2529. // temp_field.UnsafeArenaAddAllocated(new T);
  2530. // ... // Do something with temp_field
  2531. // temp_field.UnsafeArenaExtractSubrange(0, temp_field.size(), nullptr);
  2532. // If you put temp_field on the arena this fails, because the ownership
  2533. // transfers to the arena at the "AddAllocated" call and is not released anymore
  2534. // causing a double delete. Using UnsafeArenaAddAllocated prevents this.
  2535. template <typename T>
  2536. internal::UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator<T>
  2537. UnsafeArenaAllocatedRepeatedPtrFieldBackInserter(
  2538. RepeatedPtrField<T>* const mutable_field) {
  2539. return internal::UnsafeArenaAllocatedRepeatedPtrFieldBackInsertIterator<T>(
  2540. mutable_field);
  2541. }
  2542. // Extern declarations of common instantiations to reduce library bloat.
  2543. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<bool>;
  2544. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<int32_t>;
  2545. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<uint32_t>;
  2546. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<int64_t>;
  2547. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<uint64_t>;
  2548. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<float>;
  2549. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<double>;
  2550. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE
  2551. RepeatedPtrField<std::string>;
  2552. } // namespace protobuf
  2553. } // namespace google
  2554. #include <google/protobuf/port_undef.inc>
  2555. #endif // GOOGLE_PROTOBUF_REPEATED_FIELD_H__