repeated_field.h 83 KB

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