You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

repeated_field.h 43 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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. // This header covers RepeatedField.
  42. #ifndef GOOGLE_PROTOBUF_REPEATED_FIELD_H__
  43. #define GOOGLE_PROTOBUF_REPEATED_FIELD_H__
  44. #include <algorithm>
  45. #include <iterator>
  46. #include <limits>
  47. #include <string>
  48. #include <type_traits>
  49. #include <utility>
  50. #include <google/protobuf/stubs/logging.h>
  51. #include <google/protobuf/stubs/common.h>
  52. #include <google/protobuf/arena.h>
  53. #include <google/protobuf/port.h>
  54. #include <google/protobuf/message_lite.h>
  55. #include <google/protobuf/repeated_ptr_field.h>
  56. // Must be included last.
  57. #include <google/protobuf/port_def.inc>
  58. #ifdef SWIG
  59. #error "You cannot SWIG proto headers"
  60. #endif
  61. namespace google {
  62. namespace protobuf {
  63. class Message;
  64. namespace internal {
  65. template <typename T, int kRepHeaderSize>
  66. constexpr int RepeatedFieldLowerClampLimit() {
  67. // The header is padded to be at least `sizeof(T)` when it would be smaller
  68. // otherwise.
  69. static_assert(sizeof(T) <= kRepHeaderSize, "");
  70. // We want to pad the minimum size to be a power of two bytes, including the
  71. // header.
  72. // The first allocation is kRepHeaderSize bytes worth of elements for a total
  73. // of 2*kRepHeaderSize bytes.
  74. // For an 8-byte header, we allocate 8 bool, 2 ints, or 1 int64.
  75. return kRepHeaderSize / sizeof(T);
  76. }
  77. // kRepeatedFieldUpperClampLimit is the lowest signed integer value that
  78. // overflows when multiplied by 2 (which is undefined behavior). Sizes above
  79. // this will clamp to the maximum int value instead of following exponential
  80. // growth when growing a repeated field.
  81. constexpr int kRepeatedFieldUpperClampLimit =
  82. (std::numeric_limits<int>::max() / 2) + 1;
  83. template <typename Iter>
  84. inline int CalculateReserve(Iter begin, Iter end, std::forward_iterator_tag) {
  85. return static_cast<int>(std::distance(begin, end));
  86. }
  87. template <typename Iter>
  88. inline int CalculateReserve(Iter /*begin*/, Iter /*end*/,
  89. std::input_iterator_tag /*unused*/) {
  90. return -1;
  91. }
  92. template <typename Iter>
  93. inline int CalculateReserve(Iter begin, Iter end) {
  94. typedef typename std::iterator_traits<Iter>::iterator_category Category;
  95. return CalculateReserve(begin, end, Category());
  96. }
  97. // Swaps two blocks of memory of size sizeof(T).
  98. template <typename T>
  99. inline void SwapBlock(char* p, char* q) {
  100. T tmp;
  101. memcpy(&tmp, p, sizeof(T));
  102. memcpy(p, q, sizeof(T));
  103. memcpy(q, &tmp, sizeof(T));
  104. }
  105. // Swaps two blocks of memory of size kSize:
  106. // template <int kSize> void memswap(char* p, char* q);
  107. template <int kSize>
  108. inline typename std::enable_if<(kSize == 0), void>::type memswap(char*, char*) {
  109. }
  110. #define PROTO_MEMSWAP_DEF_SIZE(reg_type, max_size) \
  111. template <int kSize> \
  112. typename std::enable_if<(kSize >= sizeof(reg_type) && kSize < (max_size)), \
  113. void>::type \
  114. memswap(char* p, char* q) { \
  115. SwapBlock<reg_type>(p, q); \
  116. memswap<kSize - sizeof(reg_type)>(p + sizeof(reg_type), \
  117. q + sizeof(reg_type)); \
  118. }
  119. PROTO_MEMSWAP_DEF_SIZE(uint8_t, 2)
  120. PROTO_MEMSWAP_DEF_SIZE(uint16_t, 4)
  121. PROTO_MEMSWAP_DEF_SIZE(uint32_t, 8)
  122. #ifdef __SIZEOF_INT128__
  123. PROTO_MEMSWAP_DEF_SIZE(uint64_t, 16)
  124. PROTO_MEMSWAP_DEF_SIZE(__uint128_t, (1u << 31))
  125. #else
  126. PROTO_MEMSWAP_DEF_SIZE(uint64_t, (1u << 31))
  127. #endif
  128. #undef PROTO_MEMSWAP_DEF_SIZE
  129. template <typename Element>
  130. class RepeatedIterator;
  131. } // namespace internal
  132. // RepeatedField is used to represent repeated fields of a primitive type (in
  133. // other words, everything except strings and nested Messages). Most users will
  134. // not ever use a RepeatedField directly; they will use the get-by-index,
  135. // set-by-index, and add accessors that are generated for all repeated fields.
  136. template <typename Element>
  137. class RepeatedField final {
  138. static_assert(
  139. alignof(Arena) >= alignof(Element),
  140. "We only support types that have an alignment smaller than Arena");
  141. public:
  142. constexpr RepeatedField();
  143. explicit RepeatedField(Arena* arena);
  144. RepeatedField(const RepeatedField& other);
  145. template <typename Iter,
  146. typename = typename std::enable_if<std::is_constructible<
  147. Element, decltype(*std::declval<Iter>())>::value>::type>
  148. RepeatedField(Iter begin, Iter end);
  149. ~RepeatedField();
  150. RepeatedField& operator=(const RepeatedField& other);
  151. RepeatedField(RepeatedField&& other) noexcept;
  152. RepeatedField& operator=(RepeatedField&& other) noexcept;
  153. bool empty() const;
  154. int size() const;
  155. const Element& Get(int index) const;
  156. Element* Mutable(int index);
  157. const Element& operator[](int index) const { return Get(index); }
  158. Element& operator[](int index) { return *Mutable(index); }
  159. const Element& at(int index) const;
  160. Element& at(int index);
  161. void Set(int index, const Element& value);
  162. void Add(const Element& value);
  163. // Appends a new element and returns a pointer to it.
  164. // The new element is uninitialized if |Element| is a POD type.
  165. Element* Add();
  166. // Appends elements in the range [begin, end) after reserving
  167. // the appropriate number of elements.
  168. template <typename Iter>
  169. void Add(Iter begin, Iter end);
  170. // Removes the last element in the array.
  171. void RemoveLast();
  172. // Extracts elements with indices in "[start .. start+num-1]".
  173. // Copies them into "elements[0 .. num-1]" if "elements" is not nullptr.
  174. // Caution: also moves elements with indices [start+num ..].
  175. // Calling this routine inside a loop can cause quadratic behavior.
  176. void ExtractSubrange(int start, int num, Element* elements);
  177. PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear();
  178. void MergeFrom(const RepeatedField& other);
  179. PROTOBUF_ATTRIBUTE_REINITIALIZES void CopyFrom(const RepeatedField& other);
  180. // Replaces the contents with RepeatedField(begin, end).
  181. template <typename Iter>
  182. PROTOBUF_ATTRIBUTE_REINITIALIZES void Assign(Iter begin, Iter end);
  183. // Reserves space to expand the field to at least the given size. If the
  184. // array is grown, it will always be at least doubled in size.
  185. void Reserve(int new_size);
  186. // Resizes the RepeatedField to a new, smaller size. This is O(1).
  187. void Truncate(int new_size);
  188. void AddAlreadyReserved(const Element& value);
  189. // Appends a new element and return a pointer to it.
  190. // The new element is uninitialized if |Element| is a POD type.
  191. // Should be called only if Capacity() > Size().
  192. Element* AddAlreadyReserved();
  193. Element* AddNAlreadyReserved(int elements);
  194. int Capacity() const;
  195. // Like STL resize. Uses value to fill appended elements.
  196. // Like Truncate() if new_size <= size(), otherwise this is
  197. // O(new_size - size()).
  198. void Resize(int new_size, const Element& value);
  199. // Gets the underlying array. This pointer is possibly invalidated by
  200. // any add or remove operation.
  201. Element* mutable_data();
  202. const Element* data() const;
  203. // Swaps entire contents with "other". If they are separate arenas then,
  204. // copies data between each other.
  205. void Swap(RepeatedField* other);
  206. // Swaps entire contents with "other". Should be called only if the caller can
  207. // guarantee that both repeated fields are on the same arena or are on the
  208. // heap. Swapping between different arenas is disallowed and caught by a
  209. // GOOGLE_DCHECK (see API docs for details).
  210. void UnsafeArenaSwap(RepeatedField* other);
  211. // Swaps two elements.
  212. void SwapElements(int index1, int index2);
  213. // STL-like iterator support
  214. typedef internal::RepeatedIterator<Element> iterator;
  215. typedef internal::RepeatedIterator<const Element> const_iterator;
  216. typedef Element value_type;
  217. typedef value_type& reference;
  218. typedef const value_type& const_reference;
  219. typedef value_type* pointer;
  220. typedef const value_type* const_pointer;
  221. typedef int size_type;
  222. typedef ptrdiff_t difference_type;
  223. iterator begin();
  224. const_iterator begin() const;
  225. const_iterator cbegin() const;
  226. iterator end();
  227. const_iterator end() const;
  228. const_iterator cend() const;
  229. // Reverse iterator support
  230. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  231. typedef std::reverse_iterator<iterator> reverse_iterator;
  232. reverse_iterator rbegin() { return reverse_iterator(end()); }
  233. const_reverse_iterator rbegin() const {
  234. return const_reverse_iterator(end());
  235. }
  236. reverse_iterator rend() { return reverse_iterator(begin()); }
  237. const_reverse_iterator rend() const {
  238. return const_reverse_iterator(begin());
  239. }
  240. // Returns the number of bytes used by the repeated field, excluding
  241. // sizeof(*this)
  242. size_t SpaceUsedExcludingSelfLong() const;
  243. int SpaceUsedExcludingSelf() const {
  244. return internal::ToIntSize(SpaceUsedExcludingSelfLong());
  245. }
  246. // Removes the element referenced by position.
  247. //
  248. // Returns an iterator to the element immediately following the removed
  249. // element.
  250. //
  251. // Invalidates all iterators at or after the removed element, including end().
  252. iterator erase(const_iterator position);
  253. // Removes the elements in the range [first, last).
  254. //
  255. // Returns an iterator to the element immediately following the removed range.
  256. //
  257. // Invalidates all iterators at or after the removed range, including end().
  258. iterator erase(const_iterator first, const_iterator last);
  259. // Gets the Arena on which this RepeatedField stores its elements.
  260. inline Arena* GetArena() const {
  261. return GetOwningArena();
  262. }
  263. // For internal use only.
  264. //
  265. // This is public due to it being called by generated code.
  266. inline void InternalSwap(RepeatedField* other);
  267. private:
  268. template <typename T> friend class Arena::InternalHelper;
  269. // Gets the Arena on which this RepeatedField stores its elements.
  270. inline Arena* GetOwningArena() const {
  271. return (total_size_ == 0) ? static_cast<Arena*>(arena_or_elements_)
  272. : rep()->arena;
  273. }
  274. static constexpr int kInitialSize = 0;
  275. // A note on the representation here (see also comment below for
  276. // RepeatedPtrFieldBase's struct Rep):
  277. //
  278. // We maintain the same sizeof(RepeatedField) as before we added arena support
  279. // so that we do not degrade performance by bloating memory usage. Directly
  280. // adding an arena_ element to RepeatedField is quite costly. By using
  281. // indirection in this way, we keep the same size when the RepeatedField is
  282. // empty (common case), and add only an 8-byte header to the elements array
  283. // when non-empty. We make sure to place the size fields directly in the
  284. // RepeatedField class to avoid costly cache misses due to the indirection.
  285. int current_size_;
  286. int total_size_;
  287. // Pad the Rep after arena allow for power-of-two byte sizes when
  288. // sizeof(Element) > sizeof(Arena*). eg for 16-byte objects.
  289. static PROTOBUF_CONSTEXPR const size_t kRepHeaderSize =
  290. sizeof(Arena*) < sizeof(Element) ? sizeof(Element) : sizeof(Arena*);
  291. struct Rep {
  292. Arena* arena;
  293. Element* elements() {
  294. return reinterpret_cast<Element*>(reinterpret_cast<char*>(this) +
  295. kRepHeaderSize);
  296. }
  297. };
  298. // If total_size_ == 0 this points to an Arena otherwise it points to the
  299. // elements member of a Rep struct. Using this invariant allows the storage of
  300. // the arena pointer without an extra allocation in the constructor.
  301. void* arena_or_elements_;
  302. // Returns a pointer to elements array.
  303. // pre-condition: the array must have been allocated.
  304. Element* elements() const {
  305. GOOGLE_DCHECK_GT(total_size_, 0);
  306. // Because of above pre-condition this cast is safe.
  307. return unsafe_elements();
  308. }
  309. // Returns a pointer to elements array if it exists; otherwise either null or
  310. // an invalid pointer is returned. This only happens for empty repeated
  311. // fields, where you can't dereference this pointer anyway (it's empty).
  312. Element* unsafe_elements() const {
  313. return static_cast<Element*>(arena_or_elements_);
  314. }
  315. // Returns a pointer to the Rep struct.
  316. // pre-condition: the Rep must have been allocated, ie elements() is safe.
  317. Rep* rep() const {
  318. return reinterpret_cast<Rep*>(reinterpret_cast<char*>(elements()) -
  319. kRepHeaderSize);
  320. }
  321. friend class Arena;
  322. typedef void InternalArenaConstructable_;
  323. // Moves the contents of |from| into |to|, possibly clobbering |from| in the
  324. // process. For primitive types this is just a memcpy(), but it could be
  325. // specialized for non-primitive types to, say, swap each element instead.
  326. void MoveArray(Element* to, Element* from, int size);
  327. // Copies the elements of |from| into |to|.
  328. void CopyArray(Element* to, const Element* from, int size);
  329. // Internal helper to delete all elements and deallocate the storage.
  330. void InternalDeallocate(Rep* rep, int size, bool in_destructor) {
  331. if (rep != nullptr) {
  332. Element* e = &rep->elements()[0];
  333. if (!std::is_trivial<Element>::value) {
  334. Element* limit = &rep->elements()[size];
  335. for (; e < limit; e++) {
  336. e->~Element();
  337. }
  338. }
  339. const size_t bytes = size * sizeof(*e) + kRepHeaderSize;
  340. if (rep->arena == nullptr) {
  341. internal::SizedDelete(rep, bytes);
  342. } else if (!in_destructor) {
  343. // If we are in the destructor, we might be being destroyed as part of
  344. // the arena teardown. We can't try and return blocks to the arena then.
  345. rep->arena->ReturnArrayMemory(rep, bytes);
  346. }
  347. }
  348. }
  349. // This class is a performance wrapper around RepeatedField::Add(const T&)
  350. // function. In general unless a RepeatedField is a local stack variable LLVM
  351. // has a hard time optimizing Add. The machine code tends to be
  352. // loop:
  353. // mov %size, dword ptr [%repeated_field] // load
  354. // cmp %size, dword ptr [%repeated_field + 4]
  355. // jae fallback
  356. // mov %buffer, qword ptr [%repeated_field + 8]
  357. // mov dword [%buffer + %size * 4], %value
  358. // inc %size // increment
  359. // mov dword ptr [%repeated_field], %size // store
  360. // jmp loop
  361. //
  362. // This puts a load/store in each iteration of the important loop variable
  363. // size. It's a pretty bad compile that happens even in simple cases, but
  364. // largely the presence of the fallback path disturbs the compilers mem-to-reg
  365. // analysis.
  366. //
  367. // This class takes ownership of a repeated field for the duration of its
  368. // lifetime. The repeated field should not be accessed during this time, ie.
  369. // only access through this class is allowed. This class should always be a
  370. // function local stack variable. Intended use
  371. //
  372. // void AddSequence(const int* begin, const int* end, RepeatedField<int>* out)
  373. // {
  374. // RepeatedFieldAdder<int> adder(out); // Take ownership of out
  375. // for (auto it = begin; it != end; ++it) {
  376. // adder.Add(*it);
  377. // }
  378. // }
  379. //
  380. // Typically, due to the fact that adder is a local stack variable, the
  381. // compiler will be successful in mem-to-reg transformation and the machine
  382. // code will be loop: cmp %size, %capacity jae fallback mov dword ptr [%buffer
  383. // + %size * 4], %val inc %size jmp loop
  384. //
  385. // The first version executes at 7 cycles per iteration while the second
  386. // version executes at only 1 or 2 cycles.
  387. template <int = 0, bool = std::is_trivial<Element>::value>
  388. class FastAdderImpl {
  389. public:
  390. explicit FastAdderImpl(RepeatedField* rf) : repeated_field_(rf) {
  391. index_ = repeated_field_->current_size_;
  392. capacity_ = repeated_field_->total_size_;
  393. buffer_ = repeated_field_->unsafe_elements();
  394. }
  395. ~FastAdderImpl() { repeated_field_->current_size_ = index_; }
  396. void Add(Element val) {
  397. if (index_ == capacity_) {
  398. repeated_field_->current_size_ = index_;
  399. repeated_field_->Reserve(index_ + 1);
  400. capacity_ = repeated_field_->total_size_;
  401. buffer_ = repeated_field_->unsafe_elements();
  402. }
  403. buffer_[index_++] = val;
  404. }
  405. private:
  406. RepeatedField* repeated_field_;
  407. int index_;
  408. int capacity_;
  409. Element* buffer_;
  410. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FastAdderImpl);
  411. };
  412. // FastAdder is a wrapper for adding fields. The specialization above handles
  413. // POD types more efficiently than RepeatedField.
  414. template <int I>
  415. class FastAdderImpl<I, false> {
  416. public:
  417. explicit FastAdderImpl(RepeatedField* rf) : repeated_field_(rf) {}
  418. void Add(const Element& val) { repeated_field_->Add(val); }
  419. private:
  420. RepeatedField* repeated_field_;
  421. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FastAdderImpl);
  422. };
  423. using FastAdder = FastAdderImpl<>;
  424. friend class TestRepeatedFieldHelper;
  425. friend class ::google::protobuf::internal::ParseContext;
  426. };
  427. namespace internal {
  428. // This is a helper template to copy an array of elements efficiently when they
  429. // have a trivial copy constructor, and correctly otherwise. This really
  430. // shouldn't be necessary, but our compiler doesn't optimize std::copy very
  431. // effectively.
  432. template <typename Element,
  433. bool HasTrivialCopy = std::is_trivial<Element>::value>
  434. struct ElementCopier {
  435. void operator()(Element* to, const Element* from, int array_size);
  436. };
  437. } // namespace internal
  438. // implementation ====================================================
  439. template <typename Element>
  440. constexpr RepeatedField<Element>::RepeatedField()
  441. : current_size_(0), total_size_(0), arena_or_elements_(nullptr) {}
  442. template <typename Element>
  443. inline RepeatedField<Element>::RepeatedField(Arena* arena)
  444. : current_size_(0), total_size_(0), arena_or_elements_(arena) {}
  445. template <typename Element>
  446. inline RepeatedField<Element>::RepeatedField(const RepeatedField& other)
  447. : current_size_(0), total_size_(0), arena_or_elements_(nullptr) {
  448. if (other.current_size_ != 0) {
  449. Reserve(other.size());
  450. AddNAlreadyReserved(other.size());
  451. CopyArray(Mutable(0), &other.Get(0), other.size());
  452. }
  453. }
  454. template <typename Element>
  455. template <typename Iter, typename>
  456. RepeatedField<Element>::RepeatedField(Iter begin, Iter end)
  457. : current_size_(0), total_size_(0), arena_or_elements_(nullptr) {
  458. Add(begin, end);
  459. }
  460. template <typename Element>
  461. RepeatedField<Element>::~RepeatedField() {
  462. #ifndef NDEBUG
  463. // Try to trigger segfault / asan failure in non-opt builds if arena_
  464. // lifetime has ended before the destructor.
  465. auto arena = GetOwningArena();
  466. if (arena) (void)arena->SpaceAllocated();
  467. #endif
  468. if (total_size_ > 0) {
  469. InternalDeallocate(rep(), total_size_, true);
  470. }
  471. }
  472. template <typename Element>
  473. inline RepeatedField<Element>& RepeatedField<Element>::operator=(
  474. const RepeatedField& other) {
  475. if (this != &other) CopyFrom(other);
  476. return *this;
  477. }
  478. template <typename Element>
  479. inline RepeatedField<Element>::RepeatedField(RepeatedField&& other) noexcept
  480. : RepeatedField() {
  481. #ifdef PROTOBUF_FORCE_COPY_IN_MOVE
  482. CopyFrom(other);
  483. #else // PROTOBUF_FORCE_COPY_IN_MOVE
  484. // We don't just call Swap(&other) here because it would perform 3 copies if
  485. // other is on an arena. This field can't be on an arena because arena
  486. // construction always uses the Arena* accepting constructor.
  487. if (other.GetOwningArena()) {
  488. CopyFrom(other);
  489. } else {
  490. InternalSwap(&other);
  491. }
  492. #endif // !PROTOBUF_FORCE_COPY_IN_MOVE
  493. }
  494. template <typename Element>
  495. inline RepeatedField<Element>& RepeatedField<Element>::operator=(
  496. RepeatedField&& other) noexcept {
  497. // We don't just call Swap(&other) here because it would perform 3 copies if
  498. // the two fields are on different arenas.
  499. if (this != &other) {
  500. if (GetOwningArena() != other.GetOwningArena()
  501. #ifdef PROTOBUF_FORCE_COPY_IN_MOVE
  502. || GetOwningArena() == nullptr
  503. #endif // !PROTOBUF_FORCE_COPY_IN_MOVE
  504. ) {
  505. CopyFrom(other);
  506. } else {
  507. InternalSwap(&other);
  508. }
  509. }
  510. return *this;
  511. }
  512. template <typename Element>
  513. inline bool RepeatedField<Element>::empty() const {
  514. return current_size_ == 0;
  515. }
  516. template <typename Element>
  517. inline int RepeatedField<Element>::size() const {
  518. return current_size_;
  519. }
  520. template <typename Element>
  521. inline int RepeatedField<Element>::Capacity() const {
  522. return total_size_;
  523. }
  524. template <typename Element>
  525. inline void RepeatedField<Element>::AddAlreadyReserved(const Element& value) {
  526. GOOGLE_DCHECK_LT(current_size_, total_size_);
  527. elements()[current_size_++] = value;
  528. }
  529. template <typename Element>
  530. inline Element* RepeatedField<Element>::AddAlreadyReserved() {
  531. GOOGLE_DCHECK_LT(current_size_, total_size_);
  532. return &elements()[current_size_++];
  533. }
  534. template <typename Element>
  535. inline Element* RepeatedField<Element>::AddNAlreadyReserved(int elements) {
  536. GOOGLE_DCHECK_GE(total_size_ - current_size_, elements)
  537. << total_size_ << ", " << current_size_;
  538. // Warning: sometimes people call this when elements == 0 and
  539. // total_size_ == 0. In this case the return pointer points to a zero size
  540. // array (n == 0). Hence we can just use unsafe_elements(), because the user
  541. // cannot dereference the pointer anyway.
  542. Element* ret = unsafe_elements() + current_size_;
  543. current_size_ += elements;
  544. return ret;
  545. }
  546. template <typename Element>
  547. inline void RepeatedField<Element>::Resize(int new_size, const Element& value) {
  548. GOOGLE_DCHECK_GE(new_size, 0);
  549. if (new_size > current_size_) {
  550. Reserve(new_size);
  551. std::fill(&elements()[current_size_], &elements()[new_size], value);
  552. }
  553. current_size_ = new_size;
  554. }
  555. template <typename Element>
  556. inline const Element& RepeatedField<Element>::Get(int index) const {
  557. GOOGLE_DCHECK_GE(index, 0);
  558. GOOGLE_DCHECK_LT(index, current_size_);
  559. return elements()[index];
  560. }
  561. template <typename Element>
  562. inline const Element& RepeatedField<Element>::at(int index) const {
  563. GOOGLE_CHECK_GE(index, 0);
  564. GOOGLE_CHECK_LT(index, current_size_);
  565. return elements()[index];
  566. }
  567. template <typename Element>
  568. inline Element& RepeatedField<Element>::at(int index) {
  569. GOOGLE_CHECK_GE(index, 0);
  570. GOOGLE_CHECK_LT(index, current_size_);
  571. return elements()[index];
  572. }
  573. template <typename Element>
  574. inline Element* RepeatedField<Element>::Mutable(int index) {
  575. GOOGLE_DCHECK_GE(index, 0);
  576. GOOGLE_DCHECK_LT(index, current_size_);
  577. return &elements()[index];
  578. }
  579. template <typename Element>
  580. inline void RepeatedField<Element>::Set(int index, const Element& value) {
  581. GOOGLE_DCHECK_GE(index, 0);
  582. GOOGLE_DCHECK_LT(index, current_size_);
  583. elements()[index] = value;
  584. }
  585. template <typename Element>
  586. inline void RepeatedField<Element>::Add(const Element& value) {
  587. uint32_t size = current_size_;
  588. if (static_cast<int>(size) == total_size_) {
  589. // value could reference an element of the array. Reserving new space will
  590. // invalidate the reference. So we must make a copy first.
  591. auto tmp = value;
  592. Reserve(total_size_ + 1);
  593. elements()[size] = std::move(tmp);
  594. } else {
  595. elements()[size] = value;
  596. }
  597. current_size_ = size + 1;
  598. }
  599. template <typename Element>
  600. inline Element* RepeatedField<Element>::Add() {
  601. uint32_t size = current_size_;
  602. if (static_cast<int>(size) == total_size_) Reserve(total_size_ + 1);
  603. auto ptr = &elements()[size];
  604. current_size_ = size + 1;
  605. return ptr;
  606. }
  607. template <typename Element>
  608. template <typename Iter>
  609. inline void RepeatedField<Element>::Add(Iter begin, Iter end) {
  610. int reserve = internal::CalculateReserve(begin, end);
  611. if (reserve != -1) {
  612. if (reserve == 0) {
  613. return;
  614. }
  615. Reserve(reserve + size());
  616. // TODO(ckennelly): The compiler loses track of the buffer freshly
  617. // allocated by Reserve() by the time we call elements, so it cannot
  618. // guarantee that elements does not alias [begin(), end()).
  619. //
  620. // If restrict is available, annotating the pointer obtained from elements()
  621. // causes this to lower to memcpy instead of memmove.
  622. std::copy(begin, end, elements() + size());
  623. current_size_ = reserve + size();
  624. } else {
  625. FastAdder fast_adder(this);
  626. for (; begin != end; ++begin) fast_adder.Add(*begin);
  627. }
  628. }
  629. template <typename Element>
  630. inline void RepeatedField<Element>::RemoveLast() {
  631. GOOGLE_DCHECK_GT(current_size_, 0);
  632. current_size_--;
  633. }
  634. template <typename Element>
  635. void RepeatedField<Element>::ExtractSubrange(int start, int num,
  636. Element* elements) {
  637. GOOGLE_DCHECK_GE(start, 0);
  638. GOOGLE_DCHECK_GE(num, 0);
  639. GOOGLE_DCHECK_LE(start + num, this->current_size_);
  640. // Save the values of the removed elements if requested.
  641. if (elements != nullptr) {
  642. for (int i = 0; i < num; ++i) elements[i] = this->Get(i + start);
  643. }
  644. // Slide remaining elements down to fill the gap.
  645. if (num > 0) {
  646. for (int i = start + num; i < this->current_size_; ++i)
  647. this->Set(i - num, this->Get(i));
  648. this->Truncate(this->current_size_ - num);
  649. }
  650. }
  651. template <typename Element>
  652. inline void RepeatedField<Element>::Clear() {
  653. current_size_ = 0;
  654. }
  655. template <typename Element>
  656. inline void RepeatedField<Element>::MergeFrom(const RepeatedField& other) {
  657. GOOGLE_DCHECK_NE(&other, this);
  658. if (other.current_size_ != 0) {
  659. int existing_size = size();
  660. Reserve(existing_size + other.size());
  661. AddNAlreadyReserved(other.size());
  662. CopyArray(Mutable(existing_size), &other.Get(0), other.size());
  663. }
  664. }
  665. template <typename Element>
  666. inline void RepeatedField<Element>::CopyFrom(const RepeatedField& other) {
  667. if (&other == this) return;
  668. Clear();
  669. MergeFrom(other);
  670. }
  671. template <typename Element>
  672. template <typename Iter>
  673. inline void RepeatedField<Element>::Assign(Iter begin, Iter end) {
  674. Clear();
  675. Add(begin, end);
  676. }
  677. template <typename Element>
  678. inline typename RepeatedField<Element>::iterator RepeatedField<Element>::erase(
  679. const_iterator position) {
  680. return erase(position, position + 1);
  681. }
  682. template <typename Element>
  683. inline typename RepeatedField<Element>::iterator RepeatedField<Element>::erase(
  684. const_iterator first, const_iterator last) {
  685. size_type first_offset = first - cbegin();
  686. if (first != last) {
  687. Truncate(std::copy(last, cend(), begin() + first_offset) - cbegin());
  688. }
  689. return begin() + first_offset;
  690. }
  691. template <typename Element>
  692. inline Element* RepeatedField<Element>::mutable_data() {
  693. return unsafe_elements();
  694. }
  695. template <typename Element>
  696. inline const Element* RepeatedField<Element>::data() const {
  697. return unsafe_elements();
  698. }
  699. template <typename Element>
  700. inline void RepeatedField<Element>::InternalSwap(RepeatedField* other) {
  701. GOOGLE_DCHECK(this != other);
  702. // Swap all fields at once.
  703. static_assert(std::is_standard_layout<RepeatedField<Element>>::value,
  704. "offsetof() requires standard layout before c++17");
  705. internal::memswap<offsetof(RepeatedField, arena_or_elements_) +
  706. sizeof(this->arena_or_elements_) -
  707. offsetof(RepeatedField, current_size_)>(
  708. reinterpret_cast<char*>(this) + offsetof(RepeatedField, current_size_),
  709. reinterpret_cast<char*>(other) + offsetof(RepeatedField, current_size_));
  710. }
  711. template <typename Element>
  712. void RepeatedField<Element>::Swap(RepeatedField* other) {
  713. if (this == other) return;
  714. #ifdef PROTOBUF_FORCE_COPY_IN_SWAP
  715. if (GetOwningArena() != nullptr &&
  716. GetOwningArena() == other->GetOwningArena()) {
  717. #else // PROTOBUF_FORCE_COPY_IN_SWAP
  718. if (GetOwningArena() == other->GetOwningArena()) {
  719. #endif // !PROTOBUF_FORCE_COPY_IN_SWAP
  720. InternalSwap(other);
  721. } else {
  722. RepeatedField<Element> temp(other->GetOwningArena());
  723. temp.MergeFrom(*this);
  724. CopyFrom(*other);
  725. other->UnsafeArenaSwap(&temp);
  726. }
  727. }
  728. template <typename Element>
  729. void RepeatedField<Element>::UnsafeArenaSwap(RepeatedField* other) {
  730. if (this == other) return;
  731. GOOGLE_DCHECK_EQ(GetOwningArena(), other->GetOwningArena());
  732. InternalSwap(other);
  733. }
  734. template <typename Element>
  735. void RepeatedField<Element>::SwapElements(int index1, int index2) {
  736. using std::swap; // enable ADL with fallback
  737. swap(elements()[index1], elements()[index2]);
  738. }
  739. template <typename Element>
  740. inline typename RepeatedField<Element>::iterator
  741. RepeatedField<Element>::begin() {
  742. return iterator(unsafe_elements());
  743. }
  744. template <typename Element>
  745. inline typename RepeatedField<Element>::const_iterator
  746. RepeatedField<Element>::begin() const {
  747. return const_iterator(unsafe_elements());
  748. }
  749. template <typename Element>
  750. inline typename RepeatedField<Element>::const_iterator
  751. RepeatedField<Element>::cbegin() const {
  752. return const_iterator(unsafe_elements());
  753. }
  754. template <typename Element>
  755. inline typename RepeatedField<Element>::iterator RepeatedField<Element>::end() {
  756. return iterator(unsafe_elements() + current_size_);
  757. }
  758. template <typename Element>
  759. inline typename RepeatedField<Element>::const_iterator
  760. RepeatedField<Element>::end() const {
  761. return const_iterator(unsafe_elements() + current_size_);
  762. }
  763. template <typename Element>
  764. inline typename RepeatedField<Element>::const_iterator
  765. RepeatedField<Element>::cend() const {
  766. return const_iterator(unsafe_elements() + current_size_);
  767. }
  768. template <typename Element>
  769. inline size_t RepeatedField<Element>::SpaceUsedExcludingSelfLong() const {
  770. return total_size_ > 0 ? (total_size_ * sizeof(Element) + kRepHeaderSize) : 0;
  771. }
  772. namespace internal {
  773. // Returns the new size for a reserved field based on its 'total_size' and the
  774. // requested 'new_size'. The result is clamped to the closed interval:
  775. // [internal::kMinRepeatedFieldAllocationSize,
  776. // std::numeric_limits<int>::max()]
  777. // Requires:
  778. // new_size > total_size &&
  779. // (total_size == 0 ||
  780. // total_size >= kRepeatedFieldLowerClampLimit)
  781. template <typename T, int kRepHeaderSize>
  782. inline int CalculateReserveSize(int total_size, int new_size) {
  783. constexpr int lower_limit = RepeatedFieldLowerClampLimit<T, kRepHeaderSize>();
  784. if (new_size < lower_limit) {
  785. // Clamp to smallest allowed size.
  786. return lower_limit;
  787. }
  788. constexpr int kMaxSizeBeforeClamp =
  789. (std::numeric_limits<int>::max() - kRepHeaderSize) / 2;
  790. if (PROTOBUF_PREDICT_FALSE(total_size > kMaxSizeBeforeClamp)) {
  791. return std::numeric_limits<int>::max();
  792. }
  793. // We want to double the number of bytes, not the number of elements, to try
  794. // to stay within power-of-two allocations.
  795. // The allocation has kRepHeaderSize + sizeof(T) * capacity.
  796. int doubled_size = 2 * total_size + kRepHeaderSize / sizeof(T);
  797. return std::max(doubled_size, new_size);
  798. }
  799. } // namespace internal
  800. // Avoid inlining of Reserve(): new, copy, and delete[] lead to a significant
  801. // amount of code bloat.
  802. template <typename Element>
  803. void RepeatedField<Element>::Reserve(int new_size) {
  804. if (total_size_ >= new_size) return;
  805. Rep* old_rep = total_size_ > 0 ? rep() : nullptr;
  806. Rep* new_rep;
  807. Arena* arena = GetOwningArena();
  808. new_size = internal::CalculateReserveSize<Element, kRepHeaderSize>(
  809. total_size_, new_size);
  810. GOOGLE_DCHECK_LE(
  811. static_cast<size_t>(new_size),
  812. (std::numeric_limits<size_t>::max() - kRepHeaderSize) / sizeof(Element))
  813. << "Requested size is too large to fit into size_t.";
  814. size_t bytes =
  815. kRepHeaderSize + sizeof(Element) * static_cast<size_t>(new_size);
  816. if (arena == nullptr) {
  817. new_rep = static_cast<Rep*>(::operator new(bytes));
  818. } else {
  819. new_rep = reinterpret_cast<Rep*>(Arena::CreateArray<char>(arena, bytes));
  820. }
  821. new_rep->arena = arena;
  822. int old_total_size = total_size_;
  823. // Already known: new_size >= internal::kMinRepeatedFieldAllocationSize
  824. // Maintain invariant:
  825. // total_size_ == 0 ||
  826. // total_size_ >= internal::kMinRepeatedFieldAllocationSize
  827. total_size_ = new_size;
  828. arena_or_elements_ = new_rep->elements();
  829. // Invoke placement-new on newly allocated elements. We shouldn't have to do
  830. // this, since Element is supposed to be POD, but a previous version of this
  831. // code allocated storage with "new Element[size]" and some code uses
  832. // RepeatedField with non-POD types, relying on constructor invocation. If
  833. // Element has a trivial constructor (e.g., int32_t), gcc (tested with -O2)
  834. // completely removes this loop because the loop body is empty, so this has no
  835. // effect unless its side-effects are required for correctness.
  836. // Note that we do this before MoveArray() below because Element's copy
  837. // assignment implementation will want an initialized instance first.
  838. Element* e = &elements()[0];
  839. Element* limit = e + total_size_;
  840. for (; e < limit; e++) {
  841. new (e) Element;
  842. }
  843. if (current_size_ > 0) {
  844. MoveArray(&elements()[0], old_rep->elements(), current_size_);
  845. }
  846. // Likewise, we need to invoke destructors on the old array.
  847. InternalDeallocate(old_rep, old_total_size, false);
  848. }
  849. template <typename Element>
  850. inline void RepeatedField<Element>::Truncate(int new_size) {
  851. GOOGLE_DCHECK_LE(new_size, current_size_);
  852. if (current_size_ > 0) {
  853. current_size_ = new_size;
  854. }
  855. }
  856. template <typename Element>
  857. inline void RepeatedField<Element>::MoveArray(Element* to, Element* from,
  858. int array_size) {
  859. CopyArray(to, from, array_size);
  860. }
  861. template <typename Element>
  862. inline void RepeatedField<Element>::CopyArray(Element* to, const Element* from,
  863. int array_size) {
  864. internal::ElementCopier<Element>()(to, from, array_size);
  865. }
  866. namespace internal {
  867. template <typename Element, bool HasTrivialCopy>
  868. void ElementCopier<Element, HasTrivialCopy>::operator()(Element* to,
  869. const Element* from,
  870. int array_size) {
  871. std::copy(from, from + array_size, to);
  872. }
  873. template <typename Element>
  874. struct ElementCopier<Element, true> {
  875. void operator()(Element* to, const Element* from, int array_size) {
  876. memcpy(to, from, static_cast<size_t>(array_size) * sizeof(Element));
  877. }
  878. };
  879. } // namespace internal
  880. // -------------------------------------------------------------------
  881. // Iterators and helper functions that follow the spirit of the STL
  882. // std::back_insert_iterator and std::back_inserter but are tailor-made
  883. // for RepeatedField and RepeatedPtrField. Typical usage would be:
  884. //
  885. // std::copy(some_sequence.begin(), some_sequence.end(),
  886. // RepeatedFieldBackInserter(proto.mutable_sequence()));
  887. //
  888. // Ported by johannes from util/gtl/proto-array-iterators.h
  889. namespace internal {
  890. // STL-like iterator implementation for RepeatedField. You should not
  891. // refer to this class directly; use RepeatedField<T>::iterator instead.
  892. //
  893. // Note: All of the iterator operators *must* be inlined to avoid performance
  894. // regressions. This is caused by the extern template declarations below (which
  895. // are required because of the RepeatedField extern template declarations). If
  896. // any of these functions aren't explicitly inlined (e.g. defined in the class),
  897. // the compiler isn't allowed to inline them.
  898. template <typename Element>
  899. class RepeatedIterator {
  900. public:
  901. using iterator_category = std::random_access_iterator_tag;
  902. // Note: remove_const is necessary for std::partial_sum, which uses value_type
  903. // to determine the summation variable type.
  904. using value_type = typename std::remove_const<Element>::type;
  905. using difference_type = std::ptrdiff_t;
  906. using pointer = Element*;
  907. using reference = Element&;
  908. constexpr RepeatedIterator() noexcept : it_(nullptr) {}
  909. // Allows "upcasting" from RepeatedIterator<T**> to
  910. // RepeatedIterator<const T*const*>.
  911. template <typename OtherElement,
  912. typename std::enable_if<std::is_convertible<
  913. OtherElement*, pointer>::value>::type* = nullptr>
  914. constexpr RepeatedIterator(
  915. const RepeatedIterator<OtherElement>& other) noexcept
  916. : it_(other.it_) {}
  917. // dereferenceable
  918. constexpr reference operator*() const noexcept { return *it_; }
  919. constexpr pointer operator->() const noexcept { return it_; }
  920. private:
  921. // Helper alias to hide the internal type.
  922. using iterator = RepeatedIterator<Element>;
  923. public:
  924. // {inc,dec}rementable
  925. iterator& operator++() noexcept {
  926. ++it_;
  927. return *this;
  928. }
  929. iterator operator++(int) noexcept { return iterator(it_++); }
  930. iterator& operator--() noexcept {
  931. --it_;
  932. return *this;
  933. }
  934. iterator operator--(int) noexcept { return iterator(it_--); }
  935. // equality_comparable
  936. friend constexpr bool operator==(const iterator& x,
  937. const iterator& y) noexcept {
  938. return x.it_ == y.it_;
  939. }
  940. friend constexpr bool operator!=(const iterator& x,
  941. const iterator& y) noexcept {
  942. return x.it_ != y.it_;
  943. }
  944. // less_than_comparable
  945. friend constexpr bool operator<(const iterator& x,
  946. const iterator& y) noexcept {
  947. return x.it_ < y.it_;
  948. }
  949. friend constexpr bool operator<=(const iterator& x,
  950. const iterator& y) noexcept {
  951. return x.it_ <= y.it_;
  952. }
  953. friend constexpr bool operator>(const iterator& x,
  954. const iterator& y) noexcept {
  955. return x.it_ > y.it_;
  956. }
  957. friend constexpr bool operator>=(const iterator& x,
  958. const iterator& y) noexcept {
  959. return x.it_ >= y.it_;
  960. }
  961. // addable, subtractable
  962. iterator& operator+=(difference_type d) noexcept {
  963. it_ += d;
  964. return *this;
  965. }
  966. constexpr iterator operator+(difference_type d) const noexcept {
  967. return iterator(it_ + d);
  968. }
  969. friend constexpr iterator operator+(const difference_type d,
  970. iterator it) noexcept {
  971. return it + d;
  972. }
  973. iterator& operator-=(difference_type d) noexcept {
  974. it_ -= d;
  975. return *this;
  976. }
  977. iterator constexpr operator-(difference_type d) const noexcept {
  978. return iterator(it_ - d);
  979. }
  980. // indexable
  981. constexpr reference operator[](difference_type d) const noexcept {
  982. return it_[d];
  983. }
  984. // random access iterator
  985. friend constexpr difference_type operator-(iterator it1,
  986. iterator it2) noexcept {
  987. return it1.it_ - it2.it_;
  988. }
  989. private:
  990. template <typename OtherElement>
  991. friend class RepeatedIterator;
  992. // Allow construction from RepeatedField.
  993. friend class RepeatedField<value_type>;
  994. explicit RepeatedIterator(Element* it) noexcept : it_(it) {}
  995. // The internal iterator.
  996. Element* it_;
  997. };
  998. // A back inserter for RepeatedField objects.
  999. template <typename T>
  1000. class RepeatedFieldBackInsertIterator {
  1001. public:
  1002. using iterator_category = std::output_iterator_tag;
  1003. using value_type = T;
  1004. using pointer = void;
  1005. using reference = void;
  1006. using difference_type = std::ptrdiff_t;
  1007. explicit RepeatedFieldBackInsertIterator(
  1008. RepeatedField<T>* const mutable_field)
  1009. : field_(mutable_field) {}
  1010. RepeatedFieldBackInsertIterator<T>& operator=(const T& value) {
  1011. field_->Add(value);
  1012. return *this;
  1013. }
  1014. RepeatedFieldBackInsertIterator<T>& operator*() { return *this; }
  1015. RepeatedFieldBackInsertIterator<T>& operator++() { return *this; }
  1016. RepeatedFieldBackInsertIterator<T>& operator++(int /* unused */) {
  1017. return *this;
  1018. }
  1019. private:
  1020. RepeatedField<T>* field_;
  1021. };
  1022. } // namespace internal
  1023. // Provides a back insert iterator for RepeatedField instances,
  1024. // similar to std::back_inserter().
  1025. template <typename T>
  1026. internal::RepeatedFieldBackInsertIterator<T> RepeatedFieldBackInserter(
  1027. RepeatedField<T>* const mutable_field) {
  1028. return internal::RepeatedFieldBackInsertIterator<T>(mutable_field);
  1029. }
  1030. // Extern declarations of common instantiations to reduce library bloat.
  1031. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<bool>;
  1032. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<int32_t>;
  1033. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<uint32_t>;
  1034. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<int64_t>;
  1035. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<uint64_t>;
  1036. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<float>;
  1037. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<double>;
  1038. namespace internal {
  1039. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedIterator<bool>;
  1040. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE
  1041. RepeatedIterator<int32_t>;
  1042. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE
  1043. RepeatedIterator<uint32_t>;
  1044. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE
  1045. RepeatedIterator<int64_t>;
  1046. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE
  1047. RepeatedIterator<uint64_t>;
  1048. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedIterator<float>;
  1049. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedIterator<double>;
  1050. } // namespace internal
  1051. } // namespace protobuf
  1052. } // namespace google
  1053. #include <google/protobuf/port_undef.inc>
  1054. #endif // GOOGLE_PROTOBUF_REPEATED_FIELD_H__