repeated_field.h 90 KB

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