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_ptr_field.h 96 kB

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