memory 163 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370
  1. // -*- C++ -*-
  2. //===-------------------------- memory ------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_MEMORY
  10. #define _LIBCPP_MEMORY
  11. /*
  12. memory synopsis
  13. namespace std
  14. {
  15. struct allocator_arg_t { };
  16. inline constexpr allocator_arg_t allocator_arg = allocator_arg_t();
  17. template <class T, class Alloc> struct uses_allocator;
  18. template <class Ptr>
  19. struct pointer_traits
  20. {
  21. typedef Ptr pointer;
  22. typedef <details> element_type;
  23. typedef <details> difference_type;
  24. template <class U> using rebind = <details>;
  25. static pointer pointer_to(<details>);
  26. };
  27. template <class T>
  28. struct pointer_traits<T*>
  29. {
  30. typedef T* pointer;
  31. typedef T element_type;
  32. typedef ptrdiff_t difference_type;
  33. template <class U> using rebind = U*;
  34. static pointer pointer_to(<details>) noexcept; // constexpr in C++20
  35. };
  36. template <class T> constexpr T* to_address(T* p) noexcept; // C++20
  37. template <class Ptr> auto to_address(const Ptr& p) noexcept; // C++20
  38. template <class Alloc>
  39. struct allocator_traits
  40. {
  41. typedef Alloc allocator_type;
  42. typedef typename allocator_type::value_type
  43. value_type;
  44. typedef Alloc::pointer | value_type* pointer;
  45. typedef Alloc::const_pointer
  46. | pointer_traits<pointer>::rebind<const value_type>
  47. const_pointer;
  48. typedef Alloc::void_pointer
  49. | pointer_traits<pointer>::rebind<void>
  50. void_pointer;
  51. typedef Alloc::const_void_pointer
  52. | pointer_traits<pointer>::rebind<const void>
  53. const_void_pointer;
  54. typedef Alloc::difference_type
  55. | pointer_traits<pointer>::difference_type
  56. difference_type;
  57. typedef Alloc::size_type
  58. | make_unsigned<difference_type>::type
  59. size_type;
  60. typedef Alloc::propagate_on_container_copy_assignment
  61. | false_type propagate_on_container_copy_assignment;
  62. typedef Alloc::propagate_on_container_move_assignment
  63. | false_type propagate_on_container_move_assignment;
  64. typedef Alloc::propagate_on_container_swap
  65. | false_type propagate_on_container_swap;
  66. typedef Alloc::is_always_equal
  67. | is_empty is_always_equal;
  68. template <class T> using rebind_alloc = Alloc::rebind<U>::other | Alloc<T, Args...>;
  69. template <class T> using rebind_traits = allocator_traits<rebind_alloc<T>>;
  70. static pointer allocate(allocator_type& a, size_type n); // [[nodiscard]] in C++20
  71. static pointer allocate(allocator_type& a, size_type n, const_void_pointer hint); // [[nodiscard]] in C++20
  72. static void deallocate(allocator_type& a, pointer p, size_type n) noexcept;
  73. template <class T, class... Args>
  74. static void construct(allocator_type& a, T* p, Args&&... args);
  75. template <class T>
  76. static void destroy(allocator_type& a, T* p);
  77. static size_type max_size(const allocator_type& a); // noexcept in C++14
  78. static allocator_type
  79. select_on_container_copy_construction(const allocator_type& a);
  80. };
  81. template <>
  82. class allocator<void>
  83. {
  84. public:
  85. typedef void* pointer;
  86. typedef const void* const_pointer;
  87. typedef void value_type;
  88. template <class _Up> struct rebind {typedef allocator<_Up> other;};
  89. };
  90. template <class T>
  91. class allocator
  92. {
  93. public:
  94. typedef size_t size_type;
  95. typedef ptrdiff_t difference_type;
  96. typedef T* pointer;
  97. typedef const T* const_pointer;
  98. typedef typename add_lvalue_reference<T>::type reference;
  99. typedef typename add_lvalue_reference<const T>::type const_reference;
  100. typedef T value_type;
  101. template <class U> struct rebind {typedef allocator<U> other;};
  102. constexpr allocator() noexcept; // constexpr in C++20
  103. constexpr allocator(const allocator&) noexcept; // constexpr in C++20
  104. template <class U>
  105. constexpr allocator(const allocator<U>&) noexcept; // constexpr in C++20
  106. ~allocator();
  107. pointer address(reference x) const noexcept;
  108. const_pointer address(const_reference x) const noexcept;
  109. pointer allocate(size_type, allocator<void>::const_pointer hint = 0);
  110. void deallocate(pointer p, size_type n) noexcept;
  111. size_type max_size() const noexcept;
  112. template<class U, class... Args>
  113. void construct(U* p, Args&&... args);
  114. template <class U>
  115. void destroy(U* p);
  116. };
  117. template <class T, class U>
  118. bool operator==(const allocator<T>&, const allocator<U>&) noexcept;
  119. template <class T, class U>
  120. bool operator!=(const allocator<T>&, const allocator<U>&) noexcept;
  121. template <class OutputIterator, class T>
  122. class raw_storage_iterator
  123. : public iterator<output_iterator_tag,
  124. T, // purposefully not C++03
  125. ptrdiff_t, // purposefully not C++03
  126. T*, // purposefully not C++03
  127. raw_storage_iterator&> // purposefully not C++03
  128. {
  129. public:
  130. explicit raw_storage_iterator(OutputIterator x);
  131. raw_storage_iterator& operator*();
  132. raw_storage_iterator& operator=(const T& element);
  133. raw_storage_iterator& operator++();
  134. raw_storage_iterator operator++(int);
  135. };
  136. template <class T> pair<T*,ptrdiff_t> get_temporary_buffer(ptrdiff_t n) noexcept;
  137. template <class T> void return_temporary_buffer(T* p) noexcept;
  138. template <class T> T* addressof(T& r) noexcept;
  139. template <class T> T* addressof(const T&& r) noexcept = delete;
  140. template <class InputIterator, class ForwardIterator>
  141. ForwardIterator
  142. uninitialized_copy(InputIterator first, InputIterator last, ForwardIterator result);
  143. template <class InputIterator, class Size, class ForwardIterator>
  144. ForwardIterator
  145. uninitialized_copy_n(InputIterator first, Size n, ForwardIterator result);
  146. template <class ForwardIterator, class T>
  147. void uninitialized_fill(ForwardIterator first, ForwardIterator last, const T& x);
  148. template <class ForwardIterator, class Size, class T>
  149. ForwardIterator
  150. uninitialized_fill_n(ForwardIterator first, Size n, const T& x);
  151. template <class T>
  152. void destroy_at(T* location);
  153. template <class ForwardIterator>
  154. void destroy(ForwardIterator first, ForwardIterator last);
  155. template <class ForwardIterator, class Size>
  156. ForwardIterator destroy_n(ForwardIterator first, Size n);
  157. template <class InputIterator, class ForwardIterator>
  158. ForwardIterator uninitialized_move(InputIterator first, InputIterator last, ForwardIterator result);
  159. template <class InputIterator, class Size, class ForwardIterator>
  160. pair<InputIterator,ForwardIterator> uninitialized_move_n(InputIterator first, Size n, ForwardIterator result);
  161. template <class ForwardIterator>
  162. void uninitialized_value_construct(ForwardIterator first, ForwardIterator last);
  163. template <class ForwardIterator, class Size>
  164. ForwardIterator uninitialized_value_construct_n(ForwardIterator first, Size n);
  165. template <class ForwardIterator>
  166. void uninitialized_default_construct(ForwardIterator first, ForwardIterator last);
  167. template <class ForwardIterator, class Size>
  168. ForwardIterator uninitialized_default_construct_n(ForwardIterator first, Size n);
  169. template <class Y> struct auto_ptr_ref {}; // deprecated in C++11, removed in C++17
  170. template<class X>
  171. class auto_ptr // deprecated in C++11, removed in C++17
  172. {
  173. public:
  174. typedef X element_type;
  175. explicit auto_ptr(X* p =0) throw();
  176. auto_ptr(auto_ptr&) throw();
  177. template<class Y> auto_ptr(auto_ptr<Y>&) throw();
  178. auto_ptr& operator=(auto_ptr&) throw();
  179. template<class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
  180. auto_ptr& operator=(auto_ptr_ref<X> r) throw();
  181. ~auto_ptr() throw();
  182. typename add_lvalue_reference<X>::type operator*() const throw();
  183. X* operator->() const throw();
  184. X* get() const throw();
  185. X* release() throw();
  186. void reset(X* p =0) throw();
  187. auto_ptr(auto_ptr_ref<X>) throw();
  188. template<class Y> operator auto_ptr_ref<Y>() throw();
  189. template<class Y> operator auto_ptr<Y>() throw();
  190. };
  191. template <class T>
  192. struct default_delete
  193. {
  194. constexpr default_delete() noexcept = default;
  195. template <class U> default_delete(const default_delete<U>&) noexcept;
  196. void operator()(T*) const noexcept;
  197. };
  198. template <class T>
  199. struct default_delete<T[]>
  200. {
  201. constexpr default_delete() noexcept = default;
  202. void operator()(T*) const noexcept;
  203. template <class U> void operator()(U*) const = delete;
  204. };
  205. template <class T, class D = default_delete<T>>
  206. class unique_ptr
  207. {
  208. public:
  209. typedef see below pointer;
  210. typedef T element_type;
  211. typedef D deleter_type;
  212. // constructors
  213. constexpr unique_ptr() noexcept;
  214. explicit unique_ptr(pointer p) noexcept;
  215. unique_ptr(pointer p, see below d1) noexcept;
  216. unique_ptr(pointer p, see below d2) noexcept;
  217. unique_ptr(unique_ptr&& u) noexcept;
  218. unique_ptr(nullptr_t) noexcept : unique_ptr() { }
  219. template <class U, class E>
  220. unique_ptr(unique_ptr<U, E>&& u) noexcept;
  221. template <class U>
  222. unique_ptr(auto_ptr<U>&& u) noexcept; // removed in C++17
  223. // destructor
  224. ~unique_ptr();
  225. // assignment
  226. unique_ptr& operator=(unique_ptr&& u) noexcept;
  227. template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
  228. unique_ptr& operator=(nullptr_t) noexcept;
  229. // observers
  230. typename add_lvalue_reference<T>::type operator*() const;
  231. pointer operator->() const noexcept;
  232. pointer get() const noexcept;
  233. deleter_type& get_deleter() noexcept;
  234. const deleter_type& get_deleter() const noexcept;
  235. explicit operator bool() const noexcept;
  236. // modifiers
  237. pointer release() noexcept;
  238. void reset(pointer p = pointer()) noexcept;
  239. void swap(unique_ptr& u) noexcept;
  240. };
  241. template <class T, class D>
  242. class unique_ptr<T[], D>
  243. {
  244. public:
  245. typedef implementation-defined pointer;
  246. typedef T element_type;
  247. typedef D deleter_type;
  248. // constructors
  249. constexpr unique_ptr() noexcept;
  250. explicit unique_ptr(pointer p) noexcept;
  251. unique_ptr(pointer p, see below d) noexcept;
  252. unique_ptr(pointer p, see below d) noexcept;
  253. unique_ptr(unique_ptr&& u) noexcept;
  254. unique_ptr(nullptr_t) noexcept : unique_ptr() { }
  255. // destructor
  256. ~unique_ptr();
  257. // assignment
  258. unique_ptr& operator=(unique_ptr&& u) noexcept;
  259. unique_ptr& operator=(nullptr_t) noexcept;
  260. // observers
  261. T& operator[](size_t i) const;
  262. pointer get() const noexcept;
  263. deleter_type& get_deleter() noexcept;
  264. const deleter_type& get_deleter() const noexcept;
  265. explicit operator bool() const noexcept;
  266. // modifiers
  267. pointer release() noexcept;
  268. void reset(pointer p = pointer()) noexcept;
  269. void reset(nullptr_t) noexcept;
  270. template <class U> void reset(U) = delete;
  271. void swap(unique_ptr& u) noexcept;
  272. };
  273. template <class T, class D>
  274. void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept;
  275. template <class T1, class D1, class T2, class D2>
  276. bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
  277. template <class T1, class D1, class T2, class D2>
  278. bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
  279. template <class T1, class D1, class T2, class D2>
  280. bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
  281. template <class T1, class D1, class T2, class D2>
  282. bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
  283. template <class T1, class D1, class T2, class D2>
  284. bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
  285. template <class T1, class D1, class T2, class D2>
  286. bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y);
  287. template <class T, class D>
  288. bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
  289. template <class T, class D>
  290. bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
  291. template <class T, class D>
  292. bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
  293. template <class T, class D>
  294. bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
  295. template <class T, class D>
  296. bool operator<(const unique_ptr<T, D>& x, nullptr_t);
  297. template <class T, class D>
  298. bool operator<(nullptr_t, const unique_ptr<T, D>& y);
  299. template <class T, class D>
  300. bool operator<=(const unique_ptr<T, D>& x, nullptr_t);
  301. template <class T, class D>
  302. bool operator<=(nullptr_t, const unique_ptr<T, D>& y);
  303. template <class T, class D>
  304. bool operator>(const unique_ptr<T, D>& x, nullptr_t);
  305. template <class T, class D>
  306. bool operator>(nullptr_t, const unique_ptr<T, D>& y);
  307. template <class T, class D>
  308. bool operator>=(const unique_ptr<T, D>& x, nullptr_t);
  309. template <class T, class D>
  310. bool operator>=(nullptr_t, const unique_ptr<T, D>& y);
  311. class bad_weak_ptr
  312. : public std::exception
  313. {
  314. bad_weak_ptr() noexcept;
  315. };
  316. template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // C++14
  317. template<class T> unique_ptr<T> make_unique(size_t n); // C++14
  318. template<class T, class... Args> unspecified make_unique(Args&&...) = delete; // C++14, T == U[N]
  319. template<class E, class T, class Y, class D>
  320. basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, unique_ptr<Y, D> const& p);
  321. template<class T>
  322. class shared_ptr
  323. {
  324. public:
  325. typedef T element_type;
  326. typedef weak_ptr<T> weak_type; // C++17
  327. // constructors:
  328. constexpr shared_ptr() noexcept;
  329. template<class Y> explicit shared_ptr(Y* p);
  330. template<class Y, class D> shared_ptr(Y* p, D d);
  331. template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
  332. template <class D> shared_ptr(nullptr_t p, D d);
  333. template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
  334. template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
  335. shared_ptr(const shared_ptr& r) noexcept;
  336. template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
  337. shared_ptr(shared_ptr&& r) noexcept;
  338. template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept;
  339. template<class Y> explicit shared_ptr(const weak_ptr<Y>& r);
  340. template<class Y> shared_ptr(auto_ptr<Y>&& r); // removed in C++17
  341. template <class Y, class D> shared_ptr(unique_ptr<Y, D>&& r);
  342. shared_ptr(nullptr_t) : shared_ptr() { }
  343. // destructor:
  344. ~shared_ptr();
  345. // assignment:
  346. shared_ptr& operator=(const shared_ptr& r) noexcept;
  347. template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
  348. shared_ptr& operator=(shared_ptr&& r) noexcept;
  349. template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r);
  350. template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r); // removed in C++17
  351. template <class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r);
  352. // modifiers:
  353. void swap(shared_ptr& r) noexcept;
  354. void reset() noexcept;
  355. template<class Y> void reset(Y* p);
  356. template<class Y, class D> void reset(Y* p, D d);
  357. template<class Y, class D, class A> void reset(Y* p, D d, A a);
  358. // observers:
  359. T* get() const noexcept;
  360. T& operator*() const noexcept;
  361. T* operator->() const noexcept;
  362. long use_count() const noexcept;
  363. bool unique() const noexcept;
  364. explicit operator bool() const noexcept;
  365. template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
  366. template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
  367. };
  368. // shared_ptr comparisons:
  369. template<class T, class U>
  370. bool operator==(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
  371. template<class T, class U>
  372. bool operator!=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
  373. template<class T, class U>
  374. bool operator<(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
  375. template<class T, class U>
  376. bool operator>(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
  377. template<class T, class U>
  378. bool operator<=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
  379. template<class T, class U>
  380. bool operator>=(shared_ptr<T> const& a, shared_ptr<U> const& b) noexcept;
  381. template <class T>
  382. bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
  383. template <class T>
  384. bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
  385. template <class T>
  386. bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
  387. template <class T>
  388. bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
  389. template <class T>
  390. bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
  391. template <class T>
  392. bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
  393. template <class T>
  394. bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
  395. template <class T>
  396. bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
  397. template <class T>
  398. bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
  399. template <class T>
  400. bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
  401. template <class T>
  402. bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
  403. template <class T>
  404. bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
  405. // shared_ptr specialized algorithms:
  406. template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept;
  407. // shared_ptr casts:
  408. template<class T, class U>
  409. shared_ptr<T> static_pointer_cast(shared_ptr<U> const& r) noexcept;
  410. template<class T, class U>
  411. shared_ptr<T> dynamic_pointer_cast(shared_ptr<U> const& r) noexcept;
  412. template<class T, class U>
  413. shared_ptr<T> const_pointer_cast(shared_ptr<U> const& r) noexcept;
  414. // shared_ptr I/O:
  415. template<class E, class T, class Y>
  416. basic_ostream<E, T>& operator<< (basic_ostream<E, T>& os, shared_ptr<Y> const& p);
  417. // shared_ptr get_deleter:
  418. template<class D, class T> D* get_deleter(shared_ptr<T> const& p) noexcept;
  419. template<class T, class... Args>
  420. shared_ptr<T> make_shared(Args&&... args);
  421. template<class T, class A, class... Args>
  422. shared_ptr<T> allocate_shared(const A& a, Args&&... args);
  423. template<class T>
  424. class weak_ptr
  425. {
  426. public:
  427. typedef T element_type;
  428. // constructors
  429. constexpr weak_ptr() noexcept;
  430. template<class Y> weak_ptr(shared_ptr<Y> const& r) noexcept;
  431. weak_ptr(weak_ptr const& r) noexcept;
  432. template<class Y> weak_ptr(weak_ptr<Y> const& r) noexcept;
  433. weak_ptr(weak_ptr&& r) noexcept; // C++14
  434. template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // C++14
  435. // destructor
  436. ~weak_ptr();
  437. // assignment
  438. weak_ptr& operator=(weak_ptr const& r) noexcept;
  439. template<class Y> weak_ptr& operator=(weak_ptr<Y> const& r) noexcept;
  440. template<class Y> weak_ptr& operator=(shared_ptr<Y> const& r) noexcept;
  441. weak_ptr& operator=(weak_ptr&& r) noexcept; // C++14
  442. template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // C++14
  443. // modifiers
  444. void swap(weak_ptr& r) noexcept;
  445. void reset() noexcept;
  446. // observers
  447. long use_count() const noexcept;
  448. bool expired() const noexcept;
  449. shared_ptr<T> lock() const noexcept;
  450. template<class U> bool owner_before(shared_ptr<U> const& b) const noexcept;
  451. template<class U> bool owner_before(weak_ptr<U> const& b) const noexcept;
  452. };
  453. // weak_ptr specialized algorithms:
  454. template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept;
  455. // class owner_less:
  456. template<class T> struct owner_less;
  457. template<class T>
  458. struct owner_less<shared_ptr<T> >
  459. : binary_function<shared_ptr<T>, shared_ptr<T>, bool>
  460. {
  461. typedef bool result_type;
  462. bool operator()(shared_ptr<T> const&, shared_ptr<T> const&) const noexcept;
  463. bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
  464. bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
  465. };
  466. template<class T>
  467. struct owner_less<weak_ptr<T> >
  468. : binary_function<weak_ptr<T>, weak_ptr<T>, bool>
  469. {
  470. typedef bool result_type;
  471. bool operator()(weak_ptr<T> const&, weak_ptr<T> const&) const noexcept;
  472. bool operator()(shared_ptr<T> const&, weak_ptr<T> const&) const noexcept;
  473. bool operator()(weak_ptr<T> const&, shared_ptr<T> const&) const noexcept;
  474. };
  475. template <> // Added in C++14
  476. struct owner_less<void>
  477. {
  478. template <class _Tp, class _Up>
  479. bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
  480. template <class _Tp, class _Up>
  481. bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
  482. template <class _Tp, class _Up>
  483. bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const noexcept;
  484. template <class _Tp, class _Up>
  485. bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const noexcept;
  486. typedef void is_transparent;
  487. };
  488. template<class T>
  489. class enable_shared_from_this
  490. {
  491. protected:
  492. constexpr enable_shared_from_this() noexcept;
  493. enable_shared_from_this(enable_shared_from_this const&) noexcept;
  494. enable_shared_from_this& operator=(enable_shared_from_this const&) noexcept;
  495. ~enable_shared_from_this();
  496. public:
  497. shared_ptr<T> shared_from_this();
  498. shared_ptr<T const> shared_from_this() const;
  499. };
  500. template<class T>
  501. bool atomic_is_lock_free(const shared_ptr<T>* p);
  502. template<class T>
  503. shared_ptr<T> atomic_load(const shared_ptr<T>* p);
  504. template<class T>
  505. shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo);
  506. template<class T>
  507. void atomic_store(shared_ptr<T>* p, shared_ptr<T> r);
  508. template<class T>
  509. void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
  510. template<class T>
  511. shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
  512. template<class T>
  513. shared_ptr<T>
  514. atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo);
  515. template<class T>
  516. bool
  517. atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
  518. template<class T>
  519. bool
  520. atomic_compare_exchange_strong( shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w);
  521. template<class T>
  522. bool
  523. atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
  524. shared_ptr<T> w, memory_order success,
  525. memory_order failure);
  526. template<class T>
  527. bool
  528. atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v,
  529. shared_ptr<T> w, memory_order success,
  530. memory_order failure);
  531. // Hash support
  532. template <class T> struct hash;
  533. template <class T, class D> struct hash<unique_ptr<T, D> >;
  534. template <class T> struct hash<shared_ptr<T> >;
  535. template <class T, class Alloc>
  536. inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
  537. // Pointer safety
  538. enum class pointer_safety { relaxed, preferred, strict };
  539. void declare_reachable(void *p);
  540. template <class T> T *undeclare_reachable(T *p);
  541. void declare_no_pointers(char *p, size_t n);
  542. void undeclare_no_pointers(char *p, size_t n);
  543. pointer_safety get_pointer_safety() noexcept;
  544. void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
  545. } // std
  546. */
  547. #include <__config>
  548. #include <type_traits>
  549. #include <typeinfo>
  550. #include <cstddef>
  551. #include <cstdint>
  552. #include <new>
  553. #include <utility>
  554. #include <limits>
  555. #include <iterator>
  556. #include <__functional_base>
  557. #include <iosfwd>
  558. #include <tuple>
  559. #include <stdexcept>
  560. #include <cstring>
  561. #if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
  562. # include <atomic>
  563. #endif
  564. #include <version>
  565. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  566. #pragma GCC system_header
  567. #endif
  568. _LIBCPP_PUSH_MACROS
  569. #include <__undef_macros>
  570. _LIBCPP_BEGIN_NAMESPACE_STD
  571. template <class _ValueType>
  572. inline _LIBCPP_INLINE_VISIBILITY
  573. _ValueType __libcpp_relaxed_load(_ValueType const* __value) {
  574. #if !defined(_LIBCPP_HAS_NO_THREADS) && \
  575. defined(__ATOMIC_RELAXED) && \
  576. (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
  577. return __atomic_load_n(__value, __ATOMIC_RELAXED);
  578. #else
  579. return *__value;
  580. #endif
  581. }
  582. template <class _ValueType>
  583. inline _LIBCPP_INLINE_VISIBILITY
  584. _ValueType __libcpp_acquire_load(_ValueType const* __value) {
  585. #if !defined(_LIBCPP_HAS_NO_THREADS) && \
  586. defined(__ATOMIC_ACQUIRE) && \
  587. (__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
  588. return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
  589. #else
  590. return *__value;
  591. #endif
  592. }
  593. // addressof moved to <type_traits>
  594. template <class _Tp> class allocator;
  595. template <>
  596. class _LIBCPP_TEMPLATE_VIS allocator<void>
  597. {
  598. public:
  599. typedef void* pointer;
  600. typedef const void* const_pointer;
  601. typedef void value_type;
  602. template <class _Up> struct rebind {typedef allocator<_Up> other;};
  603. };
  604. template <>
  605. class _LIBCPP_TEMPLATE_VIS allocator<const void>
  606. {
  607. public:
  608. typedef const void* pointer;
  609. typedef const void* const_pointer;
  610. typedef const void value_type;
  611. template <class _Up> struct rebind {typedef allocator<_Up> other;};
  612. };
  613. // pointer_traits
  614. template <class _Tp, class = void>
  615. struct __has_element_type : false_type {};
  616. template <class _Tp>
  617. struct __has_element_type<_Tp,
  618. typename __void_t<typename _Tp::element_type>::type> : true_type {};
  619. template <class _Ptr, bool = __has_element_type<_Ptr>::value>
  620. struct __pointer_traits_element_type;
  621. template <class _Ptr>
  622. struct __pointer_traits_element_type<_Ptr, true>
  623. {
  624. typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::element_type type;
  625. };
  626. #ifndef _LIBCPP_HAS_NO_VARIADICS
  627. template <template <class, class...> class _Sp, class _Tp, class ..._Args>
  628. struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true>
  629. {
  630. typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::element_type type;
  631. };
  632. template <template <class, class...> class _Sp, class _Tp, class ..._Args>
  633. struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false>
  634. {
  635. typedef _LIBCPP_NODEBUG_TYPE _Tp type;
  636. };
  637. #else // _LIBCPP_HAS_NO_VARIADICS
  638. template <template <class> class _Sp, class _Tp>
  639. struct __pointer_traits_element_type<_Sp<_Tp>, true>
  640. {
  641. typedef typename _Sp<_Tp>::element_type type;
  642. };
  643. template <template <class> class _Sp, class _Tp>
  644. struct __pointer_traits_element_type<_Sp<_Tp>, false>
  645. {
  646. typedef _Tp type;
  647. };
  648. template <template <class, class> class _Sp, class _Tp, class _A0>
  649. struct __pointer_traits_element_type<_Sp<_Tp, _A0>, true>
  650. {
  651. typedef typename _Sp<_Tp, _A0>::element_type type;
  652. };
  653. template <template <class, class> class _Sp, class _Tp, class _A0>
  654. struct __pointer_traits_element_type<_Sp<_Tp, _A0>, false>
  655. {
  656. typedef _Tp type;
  657. };
  658. template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
  659. struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, true>
  660. {
  661. typedef typename _Sp<_Tp, _A0, _A1>::element_type type;
  662. };
  663. template <template <class, class, class> class _Sp, class _Tp, class _A0, class _A1>
  664. struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1>, false>
  665. {
  666. typedef _Tp type;
  667. };
  668. template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
  669. class _A1, class _A2>
  670. struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, true>
  671. {
  672. typedef typename _Sp<_Tp, _A0, _A1, _A2>::element_type type;
  673. };
  674. template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
  675. class _A1, class _A2>
  676. struct __pointer_traits_element_type<_Sp<_Tp, _A0, _A1, _A2>, false>
  677. {
  678. typedef _Tp type;
  679. };
  680. #endif // _LIBCPP_HAS_NO_VARIADICS
  681. template <class _Tp, class = void>
  682. struct __has_difference_type : false_type {};
  683. template <class _Tp>
  684. struct __has_difference_type<_Tp,
  685. typename __void_t<typename _Tp::difference_type>::type> : true_type {};
  686. template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
  687. struct __pointer_traits_difference_type
  688. {
  689. typedef _LIBCPP_NODEBUG_TYPE ptrdiff_t type;
  690. };
  691. template <class _Ptr>
  692. struct __pointer_traits_difference_type<_Ptr, true>
  693. {
  694. typedef _LIBCPP_NODEBUG_TYPE typename _Ptr::difference_type type;
  695. };
  696. template <class _Tp, class _Up>
  697. struct __has_rebind
  698. {
  699. private:
  700. struct __two {char __lx; char __lxx;};
  701. template <class _Xp> static __two __test(...);
  702. template <class _Xp> static char __test(typename _Xp::template rebind<_Up>* = 0);
  703. public:
  704. static const bool value = sizeof(__test<_Tp>(0)) == 1;
  705. };
  706. template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
  707. struct __pointer_traits_rebind
  708. {
  709. #ifndef _LIBCPP_CXX03_LANG
  710. typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up> type;
  711. #else
  712. typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
  713. #endif
  714. };
  715. #ifndef _LIBCPP_HAS_NO_VARIADICS
  716. template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
  717. struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true>
  718. {
  719. #ifndef _LIBCPP_CXX03_LANG
  720. typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
  721. #else
  722. typedef _LIBCPP_NODEBUG_TYPE typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
  723. #endif
  724. };
  725. template <template <class, class...> class _Sp, class _Tp, class ..._Args, class _Up>
  726. struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false>
  727. {
  728. typedef _Sp<_Up, _Args...> type;
  729. };
  730. #else // _LIBCPP_HAS_NO_VARIADICS
  731. template <template <class> class _Sp, class _Tp, class _Up>
  732. struct __pointer_traits_rebind<_Sp<_Tp>, _Up, true>
  733. {
  734. #ifndef _LIBCPP_CXX03_LANG
  735. typedef typename _Sp<_Tp>::template rebind<_Up> type;
  736. #else
  737. typedef typename _Sp<_Tp>::template rebind<_Up>::other type;
  738. #endif
  739. };
  740. template <template <class> class _Sp, class _Tp, class _Up>
  741. struct __pointer_traits_rebind<_Sp<_Tp>, _Up, false>
  742. {
  743. typedef _Sp<_Up> type;
  744. };
  745. template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
  746. struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, true>
  747. {
  748. #ifndef _LIBCPP_CXX03_LANG
  749. typedef typename _Sp<_Tp, _A0>::template rebind<_Up> type;
  750. #else
  751. typedef typename _Sp<_Tp, _A0>::template rebind<_Up>::other type;
  752. #endif
  753. };
  754. template <template <class, class> class _Sp, class _Tp, class _A0, class _Up>
  755. struct __pointer_traits_rebind<_Sp<_Tp, _A0>, _Up, false>
  756. {
  757. typedef _Sp<_Up, _A0> type;
  758. };
  759. template <template <class, class, class> class _Sp, class _Tp, class _A0,
  760. class _A1, class _Up>
  761. struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, true>
  762. {
  763. #ifndef _LIBCPP_CXX03_LANG
  764. typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up> type;
  765. #else
  766. typedef typename _Sp<_Tp, _A0, _A1>::template rebind<_Up>::other type;
  767. #endif
  768. };
  769. template <template <class, class, class> class _Sp, class _Tp, class _A0,
  770. class _A1, class _Up>
  771. struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1>, _Up, false>
  772. {
  773. typedef _Sp<_Up, _A0, _A1> type;
  774. };
  775. template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
  776. class _A1, class _A2, class _Up>
  777. struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, true>
  778. {
  779. #ifndef _LIBCPP_CXX03_LANG
  780. typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up> type;
  781. #else
  782. typedef typename _Sp<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
  783. #endif
  784. };
  785. template <template <class, class, class, class> class _Sp, class _Tp, class _A0,
  786. class _A1, class _A2, class _Up>
  787. struct __pointer_traits_rebind<_Sp<_Tp, _A0, _A1, _A2>, _Up, false>
  788. {
  789. typedef _Sp<_Up, _A0, _A1, _A2> type;
  790. };
  791. #endif // _LIBCPP_HAS_NO_VARIADICS
  792. template <class _Ptr>
  793. struct _LIBCPP_TEMPLATE_VIS pointer_traits
  794. {
  795. typedef _Ptr pointer;
  796. typedef typename __pointer_traits_element_type<pointer>::type element_type;
  797. typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
  798. #ifndef _LIBCPP_CXX03_LANG
  799. template <class _Up> using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
  800. #else
  801. template <class _Up> struct rebind
  802. {typedef typename __pointer_traits_rebind<pointer, _Up>::type other;};
  803. #endif // _LIBCPP_CXX03_LANG
  804. private:
  805. struct __nat {};
  806. public:
  807. _LIBCPP_INLINE_VISIBILITY
  808. static pointer pointer_to(typename conditional<is_void<element_type>::value,
  809. __nat, element_type>::type& __r)
  810. {return pointer::pointer_to(__r);}
  811. };
  812. template <class _Tp>
  813. struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*>
  814. {
  815. typedef _Tp* pointer;
  816. typedef _Tp element_type;
  817. typedef ptrdiff_t difference_type;
  818. #ifndef _LIBCPP_CXX03_LANG
  819. template <class _Up> using rebind = _Up*;
  820. #else
  821. template <class _Up> struct rebind {typedef _Up* other;};
  822. #endif
  823. private:
  824. struct __nat {};
  825. public:
  826. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  827. static pointer pointer_to(typename conditional<is_void<element_type>::value,
  828. __nat, element_type>::type& __r) _NOEXCEPT
  829. {return _VSTD::addressof(__r);}
  830. };
  831. template <class _From, class _To>
  832. struct __rebind_pointer {
  833. #ifndef _LIBCPP_CXX03_LANG
  834. typedef typename pointer_traits<_From>::template rebind<_To> type;
  835. #else
  836. typedef typename pointer_traits<_From>::template rebind<_To>::other type;
  837. #endif
  838. };
  839. // allocator_traits
  840. template <class _Tp, class = void>
  841. struct __has_pointer_type : false_type {};
  842. template <class _Tp>
  843. struct __has_pointer_type<_Tp,
  844. typename __void_t<typename _Tp::pointer>::type> : true_type {};
  845. namespace __pointer_type_imp
  846. {
  847. template <class _Tp, class _Dp, bool = __has_pointer_type<_Dp>::value>
  848. struct __pointer_type
  849. {
  850. typedef _LIBCPP_NODEBUG_TYPE typename _Dp::pointer type;
  851. };
  852. template <class _Tp, class _Dp>
  853. struct __pointer_type<_Tp, _Dp, false>
  854. {
  855. typedef _LIBCPP_NODEBUG_TYPE _Tp* type;
  856. };
  857. } // __pointer_type_imp
  858. template <class _Tp, class _Dp>
  859. struct __pointer_type
  860. {
  861. typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type_imp::__pointer_type<_Tp, typename remove_reference<_Dp>::type>::type type;
  862. };
  863. template <class _Tp, class = void>
  864. struct __has_const_pointer : false_type {};
  865. template <class _Tp>
  866. struct __has_const_pointer<_Tp,
  867. typename __void_t<typename _Tp::const_pointer>::type> : true_type {};
  868. template <class _Tp, class _Ptr, class _Alloc, bool = __has_const_pointer<_Alloc>::value>
  869. struct __const_pointer
  870. {
  871. typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_pointer type;
  872. };
  873. template <class _Tp, class _Ptr, class _Alloc>
  874. struct __const_pointer<_Tp, _Ptr, _Alloc, false>
  875. {
  876. #ifndef _LIBCPP_CXX03_LANG
  877. typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const _Tp> type;
  878. #else
  879. typedef typename pointer_traits<_Ptr>::template rebind<const _Tp>::other type;
  880. #endif
  881. };
  882. template <class _Tp, class = void>
  883. struct __has_void_pointer : false_type {};
  884. template <class _Tp>
  885. struct __has_void_pointer<_Tp,
  886. typename __void_t<typename _Tp::void_pointer>::type> : true_type {};
  887. template <class _Ptr, class _Alloc, bool = __has_void_pointer<_Alloc>::value>
  888. struct __void_pointer
  889. {
  890. typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::void_pointer type;
  891. };
  892. template <class _Ptr, class _Alloc>
  893. struct __void_pointer<_Ptr, _Alloc, false>
  894. {
  895. #ifndef _LIBCPP_CXX03_LANG
  896. typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void> type;
  897. #else
  898. typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<void>::other type;
  899. #endif
  900. };
  901. template <class _Tp, class = void>
  902. struct __has_const_void_pointer : false_type {};
  903. template <class _Tp>
  904. struct __has_const_void_pointer<_Tp,
  905. typename __void_t<typename _Tp::const_void_pointer>::type> : true_type {};
  906. template <class _Ptr, class _Alloc, bool = __has_const_void_pointer<_Alloc>::value>
  907. struct __const_void_pointer
  908. {
  909. typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::const_void_pointer type;
  910. };
  911. template <class _Ptr, class _Alloc>
  912. struct __const_void_pointer<_Ptr, _Alloc, false>
  913. {
  914. #ifndef _LIBCPP_CXX03_LANG
  915. typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void> type;
  916. #else
  917. typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::template rebind<const void>::other type;
  918. #endif
  919. };
  920. template <class _Tp>
  921. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  922. _Tp*
  923. __to_raw_pointer(_Tp* __p) _NOEXCEPT
  924. {
  925. return __p;
  926. }
  927. #if _LIBCPP_STD_VER <= 17
  928. template <class _Pointer>
  929. inline _LIBCPP_INLINE_VISIBILITY
  930. typename pointer_traits<_Pointer>::element_type*
  931. __to_raw_pointer(_Pointer __p) _NOEXCEPT
  932. {
  933. return _VSTD::__to_raw_pointer(__p.operator->());
  934. }
  935. #else
  936. template <class _Pointer>
  937. inline _LIBCPP_INLINE_VISIBILITY
  938. auto
  939. __to_raw_pointer(const _Pointer& __p) _NOEXCEPT
  940. -> decltype(pointer_traits<_Pointer>::to_address(__p))
  941. {
  942. return pointer_traits<_Pointer>::to_address(__p);
  943. }
  944. template <class _Pointer, class... _None>
  945. inline _LIBCPP_INLINE_VISIBILITY
  946. auto
  947. __to_raw_pointer(const _Pointer& __p, _None...) _NOEXCEPT
  948. {
  949. return _VSTD::__to_raw_pointer(__p.operator->());
  950. }
  951. template <class _Tp>
  952. inline _LIBCPP_INLINE_VISIBILITY constexpr
  953. _Tp*
  954. to_address(_Tp* __p) _NOEXCEPT
  955. {
  956. static_assert(!is_function_v<_Tp>, "_Tp is a function type");
  957. return __p;
  958. }
  959. template <class _Pointer>
  960. inline _LIBCPP_INLINE_VISIBILITY
  961. auto
  962. to_address(const _Pointer& __p) _NOEXCEPT
  963. {
  964. return _VSTD::__to_raw_pointer(__p);
  965. }
  966. #endif
  967. template <class _Tp, class = void>
  968. struct __has_size_type : false_type {};
  969. template <class _Tp>
  970. struct __has_size_type<_Tp,
  971. typename __void_t<typename _Tp::size_type>::type> : true_type {};
  972. template <class _Alloc, class _DiffType, bool = __has_size_type<_Alloc>::value>
  973. struct __size_type
  974. {
  975. typedef _LIBCPP_NODEBUG_TYPE typename make_unsigned<_DiffType>::type type;
  976. };
  977. template <class _Alloc, class _DiffType>
  978. struct __size_type<_Alloc, _DiffType, true>
  979. {
  980. typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::size_type type;
  981. };
  982. template <class _Tp, class = void>
  983. struct __has_propagate_on_container_copy_assignment : false_type {};
  984. template <class _Tp>
  985. struct __has_propagate_on_container_copy_assignment<_Tp,
  986. typename __void_t<typename _Tp::propagate_on_container_copy_assignment>::type>
  987. : true_type {};
  988. template <class _Alloc, bool = __has_propagate_on_container_copy_assignment<_Alloc>::value>
  989. struct __propagate_on_container_copy_assignment
  990. {
  991. typedef _LIBCPP_NODEBUG_TYPE false_type type;
  992. };
  993. template <class _Alloc>
  994. struct __propagate_on_container_copy_assignment<_Alloc, true>
  995. {
  996. typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_copy_assignment type;
  997. };
  998. template <class _Tp, class = void>
  999. struct __has_propagate_on_container_move_assignment : false_type {};
  1000. template <class _Tp>
  1001. struct __has_propagate_on_container_move_assignment<_Tp,
  1002. typename __void_t<typename _Tp::propagate_on_container_move_assignment>::type>
  1003. : true_type {};
  1004. template <class _Alloc, bool = __has_propagate_on_container_move_assignment<_Alloc>::value>
  1005. struct __propagate_on_container_move_assignment
  1006. {
  1007. typedef false_type type;
  1008. };
  1009. template <class _Alloc>
  1010. struct __propagate_on_container_move_assignment<_Alloc, true>
  1011. {
  1012. typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_move_assignment type;
  1013. };
  1014. template <class _Tp, class = void>
  1015. struct __has_propagate_on_container_swap : false_type {};
  1016. template <class _Tp>
  1017. struct __has_propagate_on_container_swap<_Tp,
  1018. typename __void_t<typename _Tp::propagate_on_container_swap>::type>
  1019. : true_type {};
  1020. template <class _Alloc, bool = __has_propagate_on_container_swap<_Alloc>::value>
  1021. struct __propagate_on_container_swap
  1022. {
  1023. typedef false_type type;
  1024. };
  1025. template <class _Alloc>
  1026. struct __propagate_on_container_swap<_Alloc, true>
  1027. {
  1028. typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::propagate_on_container_swap type;
  1029. };
  1030. template <class _Tp, class = void>
  1031. struct __has_is_always_equal : false_type {};
  1032. template <class _Tp>
  1033. struct __has_is_always_equal<_Tp,
  1034. typename __void_t<typename _Tp::is_always_equal>::type>
  1035. : true_type {};
  1036. template <class _Alloc, bool = __has_is_always_equal<_Alloc>::value>
  1037. struct __is_always_equal
  1038. {
  1039. typedef _LIBCPP_NODEBUG_TYPE typename _VSTD::is_empty<_Alloc>::type type;
  1040. };
  1041. template <class _Alloc>
  1042. struct __is_always_equal<_Alloc, true>
  1043. {
  1044. typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::is_always_equal type;
  1045. };
  1046. template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
  1047. struct __has_rebind_other
  1048. {
  1049. private:
  1050. struct __two {char __lx; char __lxx;};
  1051. template <class _Xp> static __two __test(...);
  1052. template <class _Xp> static char __test(typename _Xp::template rebind<_Up>::other* = 0);
  1053. public:
  1054. static const bool value = sizeof(__test<_Tp>(0)) == 1;
  1055. };
  1056. template <class _Tp, class _Up>
  1057. struct __has_rebind_other<_Tp, _Up, false>
  1058. {
  1059. static const bool value = false;
  1060. };
  1061. template <class _Tp, class _Up, bool = __has_rebind_other<_Tp, _Up>::value>
  1062. struct __allocator_traits_rebind
  1063. {
  1064. typedef _LIBCPP_NODEBUG_TYPE typename _Tp::template rebind<_Up>::other type;
  1065. };
  1066. #ifndef _LIBCPP_HAS_NO_VARIADICS
  1067. template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
  1068. struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, true>
  1069. {
  1070. typedef _LIBCPP_NODEBUG_TYPE typename _Alloc<_Tp, _Args...>::template rebind<_Up>::other type;
  1071. };
  1072. template <template <class, class...> class _Alloc, class _Tp, class ..._Args, class _Up>
  1073. struct __allocator_traits_rebind<_Alloc<_Tp, _Args...>, _Up, false>
  1074. {
  1075. typedef _LIBCPP_NODEBUG_TYPE _Alloc<_Up, _Args...> type;
  1076. };
  1077. #else // _LIBCPP_HAS_NO_VARIADICS
  1078. template <template <class> class _Alloc, class _Tp, class _Up>
  1079. struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, true>
  1080. {
  1081. typedef typename _Alloc<_Tp>::template rebind<_Up>::other type;
  1082. };
  1083. template <template <class> class _Alloc, class _Tp, class _Up>
  1084. struct __allocator_traits_rebind<_Alloc<_Tp>, _Up, false>
  1085. {
  1086. typedef _Alloc<_Up> type;
  1087. };
  1088. template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
  1089. struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, true>
  1090. {
  1091. typedef typename _Alloc<_Tp, _A0>::template rebind<_Up>::other type;
  1092. };
  1093. template <template <class, class> class _Alloc, class _Tp, class _A0, class _Up>
  1094. struct __allocator_traits_rebind<_Alloc<_Tp, _A0>, _Up, false>
  1095. {
  1096. typedef _Alloc<_Up, _A0> type;
  1097. };
  1098. template <template <class, class, class> class _Alloc, class _Tp, class _A0,
  1099. class _A1, class _Up>
  1100. struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, true>
  1101. {
  1102. typedef typename _Alloc<_Tp, _A0, _A1>::template rebind<_Up>::other type;
  1103. };
  1104. template <template <class, class, class> class _Alloc, class _Tp, class _A0,
  1105. class _A1, class _Up>
  1106. struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1>, _Up, false>
  1107. {
  1108. typedef _Alloc<_Up, _A0, _A1> type;
  1109. };
  1110. template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
  1111. class _A1, class _A2, class _Up>
  1112. struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, true>
  1113. {
  1114. typedef typename _Alloc<_Tp, _A0, _A1, _A2>::template rebind<_Up>::other type;
  1115. };
  1116. template <template <class, class, class, class> class _Alloc, class _Tp, class _A0,
  1117. class _A1, class _A2, class _Up>
  1118. struct __allocator_traits_rebind<_Alloc<_Tp, _A0, _A1, _A2>, _Up, false>
  1119. {
  1120. typedef _Alloc<_Up, _A0, _A1, _A2> type;
  1121. };
  1122. #endif // _LIBCPP_HAS_NO_VARIADICS
  1123. #ifndef _LIBCPP_CXX03_LANG
  1124. template <class _Alloc, class _SizeType, class _ConstVoidPtr>
  1125. auto
  1126. __has_allocate_hint_test(_Alloc&& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
  1127. -> decltype((void)__a.allocate(__sz, __p), true_type());
  1128. template <class _Alloc, class _SizeType, class _ConstVoidPtr>
  1129. auto
  1130. __has_allocate_hint_test(const _Alloc& __a, _SizeType&& __sz, _ConstVoidPtr&& __p)
  1131. -> false_type;
  1132. template <class _Alloc, class _SizeType, class _ConstVoidPtr>
  1133. struct __has_allocate_hint
  1134. : integral_constant<bool,
  1135. is_same<
  1136. decltype(_VSTD::__has_allocate_hint_test(declval<_Alloc>(),
  1137. declval<_SizeType>(),
  1138. declval<_ConstVoidPtr>())),
  1139. true_type>::value>
  1140. {
  1141. };
  1142. #else // _LIBCPP_CXX03_LANG
  1143. template <class _Alloc, class _SizeType, class _ConstVoidPtr>
  1144. struct __has_allocate_hint
  1145. : true_type
  1146. {
  1147. };
  1148. #endif // _LIBCPP_CXX03_LANG
  1149. #if !defined(_LIBCPP_CXX03_LANG)
  1150. template <class _Alloc, class _Tp, class ..._Args>
  1151. decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Tp*>(),
  1152. _VSTD::declval<_Args>()...),
  1153. true_type())
  1154. __has_construct_test(_Alloc&& __a, _Tp* __p, _Args&& ...__args);
  1155. template <class _Alloc, class _Pointer, class ..._Args>
  1156. false_type
  1157. __has_construct_test(const _Alloc& __a, _Pointer&& __p, _Args&& ...__args);
  1158. template <class _Alloc, class _Pointer, class ..._Args>
  1159. struct __has_construct
  1160. : integral_constant<bool,
  1161. is_same<
  1162. decltype(_VSTD::__has_construct_test(declval<_Alloc>(),
  1163. declval<_Pointer>(),
  1164. declval<_Args>()...)),
  1165. true_type>::value>
  1166. {
  1167. };
  1168. template <class _Alloc, class _Pointer>
  1169. auto
  1170. __has_destroy_test(_Alloc&& __a, _Pointer&& __p)
  1171. -> decltype(__a.destroy(__p), true_type());
  1172. template <class _Alloc, class _Pointer>
  1173. auto
  1174. __has_destroy_test(const _Alloc& __a, _Pointer&& __p)
  1175. -> false_type;
  1176. template <class _Alloc, class _Pointer>
  1177. struct __has_destroy
  1178. : integral_constant<bool,
  1179. is_same<
  1180. decltype(_VSTD::__has_destroy_test(declval<_Alloc>(),
  1181. declval<_Pointer>())),
  1182. true_type>::value>
  1183. {
  1184. };
  1185. template <class _Alloc>
  1186. auto
  1187. __has_max_size_test(_Alloc&& __a)
  1188. -> decltype(__a.max_size(), true_type());
  1189. template <class _Alloc>
  1190. auto
  1191. __has_max_size_test(const volatile _Alloc& __a)
  1192. -> false_type;
  1193. template <class _Alloc>
  1194. struct __has_max_size
  1195. : integral_constant<bool,
  1196. is_same<
  1197. decltype(_VSTD::__has_max_size_test(declval<_Alloc&>())),
  1198. true_type>::value>
  1199. {
  1200. };
  1201. template <class _Alloc>
  1202. auto
  1203. __has_select_on_container_copy_construction_test(_Alloc&& __a)
  1204. -> decltype(__a.select_on_container_copy_construction(), true_type());
  1205. template <class _Alloc>
  1206. auto
  1207. __has_select_on_container_copy_construction_test(const volatile _Alloc& __a)
  1208. -> false_type;
  1209. template <class _Alloc>
  1210. struct __has_select_on_container_copy_construction
  1211. : integral_constant<bool,
  1212. is_same<
  1213. decltype(_VSTD::__has_select_on_container_copy_construction_test(declval<_Alloc&>())),
  1214. true_type>::value>
  1215. {
  1216. };
  1217. #else // _LIBCPP_CXX03_LANG
  1218. template <class _Alloc, class _Pointer, class _Tp, class = void>
  1219. struct __has_construct : std::false_type {};
  1220. template <class _Alloc, class _Pointer, class _Tp>
  1221. struct __has_construct<_Alloc, _Pointer, _Tp, typename __void_t<
  1222. decltype(_VSTD::declval<_Alloc>().construct(_VSTD::declval<_Pointer>(), _VSTD::declval<_Tp>()))
  1223. >::type> : std::true_type {};
  1224. template <class _Alloc, class _Pointer, class = void>
  1225. struct __has_destroy : false_type {};
  1226. template <class _Alloc, class _Pointer>
  1227. struct __has_destroy<_Alloc, _Pointer, typename __void_t<
  1228. decltype(_VSTD::declval<_Alloc>().destroy(_VSTD::declval<_Pointer>()))
  1229. >::type> : std::true_type {};
  1230. template <class _Alloc>
  1231. struct __has_max_size
  1232. : true_type
  1233. {
  1234. };
  1235. template <class _Alloc>
  1236. struct __has_select_on_container_copy_construction
  1237. : false_type
  1238. {
  1239. };
  1240. #endif // _LIBCPP_CXX03_LANG
  1241. template <class _Alloc, class _Ptr, bool = __has_difference_type<_Alloc>::value>
  1242. struct __alloc_traits_difference_type
  1243. {
  1244. typedef _LIBCPP_NODEBUG_TYPE typename pointer_traits<_Ptr>::difference_type type;
  1245. };
  1246. template <class _Alloc, class _Ptr>
  1247. struct __alloc_traits_difference_type<_Alloc, _Ptr, true>
  1248. {
  1249. typedef _LIBCPP_NODEBUG_TYPE typename _Alloc::difference_type type;
  1250. };
  1251. template <class _Tp>
  1252. struct __is_default_allocator : false_type {};
  1253. template <class _Tp>
  1254. struct __is_default_allocator<_VSTD::allocator<_Tp> > : true_type {};
  1255. template <class _Alloc,
  1256. bool = __has_construct<_Alloc, typename _Alloc::value_type*, typename _Alloc::value_type&&>::value && !__is_default_allocator<_Alloc>::value
  1257. >
  1258. struct __is_cpp17_move_insertable;
  1259. template <class _Alloc>
  1260. struct __is_cpp17_move_insertable<_Alloc, true> : std::true_type {};
  1261. template <class _Alloc>
  1262. struct __is_cpp17_move_insertable<_Alloc, false> : std::is_move_constructible<typename _Alloc::value_type> {};
  1263. template <class _Alloc,
  1264. bool = __has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value && !__is_default_allocator<_Alloc>::value
  1265. >
  1266. struct __is_cpp17_copy_insertable;
  1267. template <class _Alloc>
  1268. struct __is_cpp17_copy_insertable<_Alloc, true> : __is_cpp17_move_insertable<_Alloc> {};
  1269. template <class _Alloc>
  1270. struct __is_cpp17_copy_insertable<_Alloc, false> : integral_constant<bool,
  1271. std::is_copy_constructible<typename _Alloc::value_type>::value &&
  1272. __is_cpp17_move_insertable<_Alloc>::value>
  1273. {};
  1274. template <class _Alloc>
  1275. struct _LIBCPP_TEMPLATE_VIS allocator_traits
  1276. {
  1277. typedef _Alloc allocator_type;
  1278. typedef typename allocator_type::value_type value_type;
  1279. typedef typename __pointer_type<value_type, allocator_type>::type pointer;
  1280. typedef typename __const_pointer<value_type, pointer, allocator_type>::type const_pointer;
  1281. typedef typename __void_pointer<pointer, allocator_type>::type void_pointer;
  1282. typedef typename __const_void_pointer<pointer, allocator_type>::type const_void_pointer;
  1283. typedef typename __alloc_traits_difference_type<allocator_type, pointer>::type difference_type;
  1284. typedef typename __size_type<allocator_type, difference_type>::type size_type;
  1285. typedef typename __propagate_on_container_copy_assignment<allocator_type>::type
  1286. propagate_on_container_copy_assignment;
  1287. typedef typename __propagate_on_container_move_assignment<allocator_type>::type
  1288. propagate_on_container_move_assignment;
  1289. typedef typename __propagate_on_container_swap<allocator_type>::type
  1290. propagate_on_container_swap;
  1291. typedef typename __is_always_equal<allocator_type>::type
  1292. is_always_equal;
  1293. #ifndef _LIBCPP_CXX03_LANG
  1294. template <class _Tp> using rebind_alloc =
  1295. typename __allocator_traits_rebind<allocator_type, _Tp>::type;
  1296. template <class _Tp> using rebind_traits = allocator_traits<rebind_alloc<_Tp> >;
  1297. #else // _LIBCPP_CXX03_LANG
  1298. template <class _Tp> struct rebind_alloc
  1299. {typedef typename __allocator_traits_rebind<allocator_type, _Tp>::type other;};
  1300. template <class _Tp> struct rebind_traits
  1301. {typedef allocator_traits<typename rebind_alloc<_Tp>::other> other;};
  1302. #endif // _LIBCPP_CXX03_LANG
  1303. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  1304. static pointer allocate(allocator_type& __a, size_type __n)
  1305. {return __a.allocate(__n);}
  1306. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  1307. static pointer allocate(allocator_type& __a, size_type __n, const_void_pointer __hint)
  1308. {return __allocate(__a, __n, __hint,
  1309. __has_allocate_hint<allocator_type, size_type, const_void_pointer>());}
  1310. _LIBCPP_INLINE_VISIBILITY
  1311. static void deallocate(allocator_type& __a, pointer __p, size_type __n) _NOEXCEPT
  1312. {__a.deallocate(__p, __n);}
  1313. #ifndef _LIBCPP_HAS_NO_VARIADICS
  1314. template <class _Tp, class... _Args>
  1315. _LIBCPP_INLINE_VISIBILITY
  1316. static void construct(allocator_type& __a, _Tp* __p, _Args&&... __args)
  1317. {__construct(__has_construct<allocator_type, _Tp*, _Args...>(),
  1318. __a, __p, _VSTD::forward<_Args>(__args)...);}
  1319. #else // _LIBCPP_HAS_NO_VARIADICS
  1320. template <class _Tp>
  1321. _LIBCPP_INLINE_VISIBILITY
  1322. static void construct(allocator_type&, _Tp* __p)
  1323. {
  1324. ::new ((void*)__p) _Tp();
  1325. }
  1326. template <class _Tp, class _A0>
  1327. _LIBCPP_INLINE_VISIBILITY
  1328. static void construct(allocator_type& __a, _Tp* __p, const _A0& __a0)
  1329. {
  1330. __construct(__has_construct<allocator_type, _Tp*, const _A0&>(),
  1331. __a, __p, __a0);
  1332. }
  1333. template <class _Tp, class _A0, class _A1>
  1334. _LIBCPP_INLINE_VISIBILITY
  1335. static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
  1336. const _A1& __a1)
  1337. {
  1338. ::new ((void*)__p) _Tp(__a0, __a1);
  1339. }
  1340. template <class _Tp, class _A0, class _A1, class _A2>
  1341. _LIBCPP_INLINE_VISIBILITY
  1342. static void construct(allocator_type&, _Tp* __p, const _A0& __a0,
  1343. const _A1& __a1, const _A2& __a2)
  1344. {
  1345. ::new ((void*)__p) _Tp(__a0, __a1, __a2);
  1346. }
  1347. #endif // _LIBCPP_HAS_NO_VARIADICS
  1348. template <class _Tp>
  1349. _LIBCPP_INLINE_VISIBILITY
  1350. static void destroy(allocator_type& __a, _Tp* __p)
  1351. {__destroy(__has_destroy<allocator_type, _Tp*>(), __a, __p);}
  1352. _LIBCPP_INLINE_VISIBILITY
  1353. static size_type max_size(const allocator_type& __a) _NOEXCEPT
  1354. {return __max_size(__has_max_size<const allocator_type>(), __a);}
  1355. _LIBCPP_INLINE_VISIBILITY
  1356. static allocator_type
  1357. select_on_container_copy_construction(const allocator_type& __a)
  1358. {return __select_on_container_copy_construction(
  1359. __has_select_on_container_copy_construction<const allocator_type>(),
  1360. __a);}
  1361. template <class _Ptr>
  1362. _LIBCPP_INLINE_VISIBILITY
  1363. static
  1364. void
  1365. __construct_forward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __begin2)
  1366. {
  1367. static_assert(__is_cpp17_move_insertable<allocator_type>::value,
  1368. "The specified type does not meet the requirements of Cpp17MoveInsertible");
  1369. for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
  1370. construct(__a, _VSTD::__to_raw_pointer(__begin2),
  1371. #ifdef _LIBCPP_NO_EXCEPTIONS
  1372. _VSTD::move(*__begin1)
  1373. #else
  1374. _VSTD::move_if_noexcept(*__begin1)
  1375. #endif
  1376. );
  1377. }
  1378. template <class _Tp>
  1379. _LIBCPP_INLINE_VISIBILITY
  1380. static
  1381. typename enable_if
  1382. <
  1383. (__is_default_allocator<allocator_type>::value
  1384. || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
  1385. is_trivially_move_constructible<_Tp>::value,
  1386. void
  1387. >::type
  1388. __construct_forward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __begin2)
  1389. {
  1390. ptrdiff_t _Np = __end1 - __begin1;
  1391. if (_Np > 0)
  1392. {
  1393. _VSTD::memcpy(__begin2, __begin1, _Np * sizeof(_Tp));
  1394. __begin2 += _Np;
  1395. }
  1396. }
  1397. template <class _Iter, class _Ptr>
  1398. _LIBCPP_INLINE_VISIBILITY
  1399. static
  1400. void
  1401. __construct_range_forward(allocator_type& __a, _Iter __begin1, _Iter __end1, _Ptr& __begin2)
  1402. {
  1403. for (; __begin1 != __end1; ++__begin1, (void) ++__begin2)
  1404. construct(__a, _VSTD::__to_raw_pointer(__begin2), *__begin1);
  1405. }
  1406. template <class _SourceTp, class _DestTp,
  1407. class _RawSourceTp = typename remove_const<_SourceTp>::type,
  1408. class _RawDestTp = typename remove_const<_DestTp>::type>
  1409. _LIBCPP_INLINE_VISIBILITY
  1410. static
  1411. typename enable_if
  1412. <
  1413. is_trivially_move_constructible<_DestTp>::value &&
  1414. is_same<_RawSourceTp, _RawDestTp>::value &&
  1415. (__is_default_allocator<allocator_type>::value ||
  1416. !__has_construct<allocator_type, _DestTp*, _SourceTp&>::value),
  1417. void
  1418. >::type
  1419. __construct_range_forward(allocator_type&, _SourceTp* __begin1, _SourceTp* __end1, _DestTp*& __begin2)
  1420. {
  1421. ptrdiff_t _Np = __end1 - __begin1;
  1422. if (_Np > 0)
  1423. {
  1424. _VSTD::memcpy(const_cast<_RawDestTp*>(__begin2), __begin1, _Np * sizeof(_DestTp));
  1425. __begin2 += _Np;
  1426. }
  1427. }
  1428. template <class _Ptr>
  1429. _LIBCPP_INLINE_VISIBILITY
  1430. static
  1431. void
  1432. __construct_backward_with_exception_guarantees(allocator_type& __a, _Ptr __begin1, _Ptr __end1, _Ptr& __end2)
  1433. {
  1434. static_assert(__is_cpp17_move_insertable<allocator_type>::value,
  1435. "The specified type does not meet the requirements of Cpp17MoveInsertable");
  1436. while (__end1 != __begin1)
  1437. {
  1438. construct(__a, _VSTD::__to_raw_pointer(__end2 - 1),
  1439. #ifdef _LIBCPP_NO_EXCEPTIONS
  1440. _VSTD::move(*--__end1)
  1441. #else
  1442. _VSTD::move_if_noexcept(*--__end1)
  1443. #endif
  1444. );
  1445. --__end2;
  1446. }
  1447. }
  1448. template <class _Tp>
  1449. _LIBCPP_INLINE_VISIBILITY
  1450. static
  1451. typename enable_if
  1452. <
  1453. (__is_default_allocator<allocator_type>::value
  1454. || !__has_construct<allocator_type, _Tp*, _Tp>::value) &&
  1455. is_trivially_move_constructible<_Tp>::value,
  1456. void
  1457. >::type
  1458. __construct_backward_with_exception_guarantees(allocator_type&, _Tp* __begin1, _Tp* __end1, _Tp*& __end2)
  1459. {
  1460. ptrdiff_t _Np = __end1 - __begin1;
  1461. __end2 -= _Np;
  1462. if (_Np > 0)
  1463. _VSTD::memcpy(__end2, __begin1, _Np * sizeof(_Tp));
  1464. }
  1465. private:
  1466. _LIBCPP_INLINE_VISIBILITY
  1467. static pointer __allocate(allocator_type& __a, size_type __n,
  1468. const_void_pointer __hint, true_type)
  1469. {return __a.allocate(__n, __hint);}
  1470. _LIBCPP_INLINE_VISIBILITY
  1471. static pointer __allocate(allocator_type& __a, size_type __n,
  1472. const_void_pointer, false_type)
  1473. {return __a.allocate(__n);}
  1474. #ifndef _LIBCPP_HAS_NO_VARIADICS
  1475. template <class _Tp, class... _Args>
  1476. _LIBCPP_INLINE_VISIBILITY
  1477. static void __construct(true_type, allocator_type& __a, _Tp* __p, _Args&&... __args)
  1478. {__a.construct(__p, _VSTD::forward<_Args>(__args)...);}
  1479. template <class _Tp, class... _Args>
  1480. _LIBCPP_INLINE_VISIBILITY
  1481. static void __construct(false_type, allocator_type&, _Tp* __p, _Args&&... __args)
  1482. {
  1483. ::new ((void*)__p) _Tp(_VSTD::forward<_Args>(__args)...);
  1484. }
  1485. #else // _LIBCPP_HAS_NO_VARIADICS
  1486. template <class _Tp, class _A0>
  1487. _LIBCPP_INLINE_VISIBILITY
  1488. static void __construct(true_type, allocator_type& __a, _Tp* __p,
  1489. const _A0& __a0)
  1490. {__a.construct(__p, __a0);}
  1491. template <class _Tp, class _A0>
  1492. _LIBCPP_INLINE_VISIBILITY
  1493. static void __construct(false_type, allocator_type&, _Tp* __p,
  1494. const _A0& __a0)
  1495. {
  1496. ::new ((void*)__p) _Tp(__a0);
  1497. }
  1498. #endif // _LIBCPP_HAS_NO_VARIADICS
  1499. template <class _Tp>
  1500. _LIBCPP_INLINE_VISIBILITY
  1501. static void __destroy(true_type, allocator_type& __a, _Tp* __p)
  1502. {__a.destroy(__p);}
  1503. template <class _Tp>
  1504. _LIBCPP_INLINE_VISIBILITY
  1505. static void __destroy(false_type, allocator_type&, _Tp* __p)
  1506. {
  1507. __p->~_Tp();
  1508. }
  1509. _LIBCPP_INLINE_VISIBILITY
  1510. static size_type __max_size(true_type, const allocator_type& __a) _NOEXCEPT
  1511. {return __a.max_size();}
  1512. _LIBCPP_INLINE_VISIBILITY
  1513. static size_type __max_size(false_type, const allocator_type&) _NOEXCEPT
  1514. {return numeric_limits<size_type>::max() / sizeof(value_type);}
  1515. _LIBCPP_INLINE_VISIBILITY
  1516. static allocator_type
  1517. __select_on_container_copy_construction(true_type, const allocator_type& __a)
  1518. {return __a.select_on_container_copy_construction();}
  1519. _LIBCPP_INLINE_VISIBILITY
  1520. static allocator_type
  1521. __select_on_container_copy_construction(false_type, const allocator_type& __a)
  1522. {return __a;}
  1523. };
  1524. template <class _Traits, class _Tp>
  1525. struct __rebind_alloc_helper
  1526. {
  1527. #ifndef _LIBCPP_CXX03_LANG
  1528. typedef _LIBCPP_NODEBUG_TYPE typename _Traits::template rebind_alloc<_Tp> type;
  1529. #else
  1530. typedef typename _Traits::template rebind_alloc<_Tp>::other type;
  1531. #endif
  1532. };
  1533. // allocator
  1534. template <class _Tp>
  1535. class _LIBCPP_TEMPLATE_VIS allocator
  1536. {
  1537. public:
  1538. typedef size_t size_type;
  1539. typedef ptrdiff_t difference_type;
  1540. typedef _Tp* pointer;
  1541. typedef const _Tp* const_pointer;
  1542. typedef _Tp& reference;
  1543. typedef const _Tp& const_reference;
  1544. typedef _Tp value_type;
  1545. typedef true_type propagate_on_container_move_assignment;
  1546. typedef true_type is_always_equal;
  1547. template <class _Up> struct rebind {typedef allocator<_Up> other;};
  1548. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1549. allocator() _NOEXCEPT {}
  1550. template <class _Up>
  1551. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1552. allocator(const allocator<_Up>&) _NOEXCEPT {}
  1553. _LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
  1554. {return _VSTD::addressof(__x);}
  1555. _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
  1556. {return _VSTD::addressof(__x);}
  1557. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  1558. pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
  1559. {
  1560. if (__n > max_size())
  1561. __throw_length_error("allocator<T>::allocate(size_t n)"
  1562. " 'n' exceeds maximum supported size");
  1563. return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
  1564. }
  1565. _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
  1566. {_VSTD::__libcpp_deallocate((void*)__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
  1567. _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
  1568. {return size_type(~0) / sizeof(_Tp);}
  1569. #if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
  1570. template <class _Up, class... _Args>
  1571. _LIBCPP_INLINE_VISIBILITY
  1572. void
  1573. construct(_Up* __p, _Args&&... __args)
  1574. {
  1575. ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
  1576. }
  1577. #else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
  1578. _LIBCPP_INLINE_VISIBILITY
  1579. void
  1580. construct(pointer __p)
  1581. {
  1582. ::new((void*)__p) _Tp();
  1583. }
  1584. # if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
  1585. template <class _A0>
  1586. _LIBCPP_INLINE_VISIBILITY
  1587. void
  1588. construct(pointer __p, _A0& __a0)
  1589. {
  1590. ::new((void*)__p) _Tp(__a0);
  1591. }
  1592. template <class _A0>
  1593. _LIBCPP_INLINE_VISIBILITY
  1594. void
  1595. construct(pointer __p, const _A0& __a0)
  1596. {
  1597. ::new((void*)__p) _Tp(__a0);
  1598. }
  1599. # endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
  1600. template <class _A0, class _A1>
  1601. _LIBCPP_INLINE_VISIBILITY
  1602. void
  1603. construct(pointer __p, _A0& __a0, _A1& __a1)
  1604. {
  1605. ::new((void*)__p) _Tp(__a0, __a1);
  1606. }
  1607. template <class _A0, class _A1>
  1608. _LIBCPP_INLINE_VISIBILITY
  1609. void
  1610. construct(pointer __p, const _A0& __a0, _A1& __a1)
  1611. {
  1612. ::new((void*)__p) _Tp(__a0, __a1);
  1613. }
  1614. template <class _A0, class _A1>
  1615. _LIBCPP_INLINE_VISIBILITY
  1616. void
  1617. construct(pointer __p, _A0& __a0, const _A1& __a1)
  1618. {
  1619. ::new((void*)__p) _Tp(__a0, __a1);
  1620. }
  1621. template <class _A0, class _A1>
  1622. _LIBCPP_INLINE_VISIBILITY
  1623. void
  1624. construct(pointer __p, const _A0& __a0, const _A1& __a1)
  1625. {
  1626. ::new((void*)__p) _Tp(__a0, __a1);
  1627. }
  1628. #endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
  1629. _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
  1630. };
  1631. template <class _Tp>
  1632. class _LIBCPP_TEMPLATE_VIS allocator<const _Tp>
  1633. {
  1634. public:
  1635. typedef size_t size_type;
  1636. typedef ptrdiff_t difference_type;
  1637. typedef const _Tp* pointer;
  1638. typedef const _Tp* const_pointer;
  1639. typedef const _Tp& reference;
  1640. typedef const _Tp& const_reference;
  1641. typedef const _Tp value_type;
  1642. typedef true_type propagate_on_container_move_assignment;
  1643. typedef true_type is_always_equal;
  1644. template <class _Up> struct rebind {typedef allocator<_Up> other;};
  1645. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1646. allocator() _NOEXCEPT {}
  1647. template <class _Up>
  1648. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  1649. allocator(const allocator<_Up>&) _NOEXCEPT {}
  1650. _LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
  1651. {return _VSTD::addressof(__x);}
  1652. _LIBCPP_INLINE_VISIBILITY pointer allocate(size_type __n, allocator<void>::const_pointer = 0)
  1653. {
  1654. if (__n > max_size())
  1655. __throw_length_error("allocator<const T>::allocate(size_t n)"
  1656. " 'n' exceeds maximum supported size");
  1657. return static_cast<pointer>(_VSTD::__libcpp_allocate(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)));
  1658. }
  1659. _LIBCPP_INLINE_VISIBILITY void deallocate(pointer __p, size_type __n) _NOEXCEPT
  1660. {_VSTD::__libcpp_deallocate((void*) const_cast<_Tp *>(__p), __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));}
  1661. _LIBCPP_INLINE_VISIBILITY size_type max_size() const _NOEXCEPT
  1662. {return size_type(~0) / sizeof(_Tp);}
  1663. #if !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
  1664. template <class _Up, class... _Args>
  1665. _LIBCPP_INLINE_VISIBILITY
  1666. void
  1667. construct(_Up* __p, _Args&&... __args)
  1668. {
  1669. ::new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
  1670. }
  1671. #else // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
  1672. _LIBCPP_INLINE_VISIBILITY
  1673. void
  1674. construct(pointer __p)
  1675. {
  1676. ::new((void*) const_cast<_Tp *>(__p)) _Tp();
  1677. }
  1678. # if defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
  1679. template <class _A0>
  1680. _LIBCPP_INLINE_VISIBILITY
  1681. void
  1682. construct(pointer __p, _A0& __a0)
  1683. {
  1684. ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
  1685. }
  1686. template <class _A0>
  1687. _LIBCPP_INLINE_VISIBILITY
  1688. void
  1689. construct(pointer __p, const _A0& __a0)
  1690. {
  1691. ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0);
  1692. }
  1693. # endif // defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES)
  1694. template <class _A0, class _A1>
  1695. _LIBCPP_INLINE_VISIBILITY
  1696. void
  1697. construct(pointer __p, _A0& __a0, _A1& __a1)
  1698. {
  1699. ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
  1700. }
  1701. template <class _A0, class _A1>
  1702. _LIBCPP_INLINE_VISIBILITY
  1703. void
  1704. construct(pointer __p, const _A0& __a0, _A1& __a1)
  1705. {
  1706. ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
  1707. }
  1708. template <class _A0, class _A1>
  1709. _LIBCPP_INLINE_VISIBILITY
  1710. void
  1711. construct(pointer __p, _A0& __a0, const _A1& __a1)
  1712. {
  1713. ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
  1714. }
  1715. template <class _A0, class _A1>
  1716. _LIBCPP_INLINE_VISIBILITY
  1717. void
  1718. construct(pointer __p, const _A0& __a0, const _A1& __a1)
  1719. {
  1720. ::new((void*) const_cast<_Tp *>(__p)) _Tp(__a0, __a1);
  1721. }
  1722. #endif // !defined(_LIBCPP_HAS_NO_RVALUE_REFERENCES) && !defined(_LIBCPP_HAS_NO_VARIADICS)
  1723. _LIBCPP_INLINE_VISIBILITY void destroy(pointer __p) {__p->~_Tp();}
  1724. };
  1725. template <class _Tp, class _Up>
  1726. inline _LIBCPP_INLINE_VISIBILITY
  1727. bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
  1728. template <class _Tp, class _Up>
  1729. inline _LIBCPP_INLINE_VISIBILITY
  1730. bool operator!=(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return false;}
  1731. template <class _OutputIterator, class _Tp>
  1732. class _LIBCPP_TEMPLATE_VIS raw_storage_iterator
  1733. : public iterator<output_iterator_tag,
  1734. _Tp, // purposefully not C++03
  1735. ptrdiff_t, // purposefully not C++03
  1736. _Tp*, // purposefully not C++03
  1737. raw_storage_iterator<_OutputIterator, _Tp>&> // purposefully not C++03
  1738. {
  1739. private:
  1740. _OutputIterator __x_;
  1741. public:
  1742. _LIBCPP_INLINE_VISIBILITY explicit raw_storage_iterator(_OutputIterator __x) : __x_(__x) {}
  1743. _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator*() {return *this;}
  1744. _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(const _Tp& __element)
  1745. {::new(_VSTD::addressof(*__x_)) _Tp(__element); return *this;}
  1746. #if _LIBCPP_STD_VER >= 14
  1747. _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator=(_Tp&& __element)
  1748. {::new(_VSTD::addressof(*__x_)) _Tp(_VSTD::move(__element)); return *this;}
  1749. #endif
  1750. _LIBCPP_INLINE_VISIBILITY raw_storage_iterator& operator++() {++__x_; return *this;}
  1751. _LIBCPP_INLINE_VISIBILITY raw_storage_iterator operator++(int)
  1752. {raw_storage_iterator __t(*this); ++__x_; return __t;}
  1753. #if _LIBCPP_STD_VER >= 14
  1754. _LIBCPP_INLINE_VISIBILITY _OutputIterator base() const { return __x_; }
  1755. #endif
  1756. };
  1757. template <class _Tp>
  1758. _LIBCPP_NODISCARD_EXT _LIBCPP_NO_CFI
  1759. pair<_Tp*, ptrdiff_t>
  1760. get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT
  1761. {
  1762. pair<_Tp*, ptrdiff_t> __r(0, 0);
  1763. const ptrdiff_t __m = (~ptrdiff_t(0) ^
  1764. ptrdiff_t(ptrdiff_t(1) << (sizeof(ptrdiff_t) * __CHAR_BIT__ - 1)))
  1765. / sizeof(_Tp);
  1766. if (__n > __m)
  1767. __n = __m;
  1768. while (__n > 0)
  1769. {
  1770. #if !defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
  1771. if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
  1772. {
  1773. std::align_val_t __al =
  1774. std::align_val_t(std::alignment_of<_Tp>::value);
  1775. __r.first = static_cast<_Tp*>(::operator new(
  1776. __n * sizeof(_Tp), __al, nothrow));
  1777. } else {
  1778. __r.first = static_cast<_Tp*>(::operator new(
  1779. __n * sizeof(_Tp), nothrow));
  1780. }
  1781. #else
  1782. if (__is_overaligned_for_new(_LIBCPP_ALIGNOF(_Tp)))
  1783. {
  1784. // Since aligned operator new is unavailable, return an empty
  1785. // buffer rather than one with invalid alignment.
  1786. return __r;
  1787. }
  1788. __r.first = static_cast<_Tp*>(::operator new(__n * sizeof(_Tp), nothrow));
  1789. #endif
  1790. if (__r.first)
  1791. {
  1792. __r.second = __n;
  1793. break;
  1794. }
  1795. __n /= 2;
  1796. }
  1797. return __r;
  1798. }
  1799. template <class _Tp>
  1800. inline _LIBCPP_INLINE_VISIBILITY
  1801. void return_temporary_buffer(_Tp* __p) _NOEXCEPT
  1802. {
  1803. _VSTD::__libcpp_deallocate_unsized((void*)__p, _LIBCPP_ALIGNOF(_Tp));
  1804. }
  1805. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  1806. template <class _Tp>
  1807. struct _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr_ref
  1808. {
  1809. _Tp* __ptr_;
  1810. };
  1811. template<class _Tp>
  1812. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr
  1813. {
  1814. private:
  1815. _Tp* __ptr_;
  1816. public:
  1817. typedef _Tp element_type;
  1818. _LIBCPP_INLINE_VISIBILITY explicit auto_ptr(_Tp* __p = 0) throw() : __ptr_(__p) {}
  1819. _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr& __p) throw() : __ptr_(__p.release()) {}
  1820. template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr<_Up>& __p) throw()
  1821. : __ptr_(__p.release()) {}
  1822. _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr& __p) throw()
  1823. {reset(__p.release()); return *this;}
  1824. template<class _Up> _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr<_Up>& __p) throw()
  1825. {reset(__p.release()); return *this;}
  1826. _LIBCPP_INLINE_VISIBILITY auto_ptr& operator=(auto_ptr_ref<_Tp> __p) throw()
  1827. {reset(__p.__ptr_); return *this;}
  1828. _LIBCPP_INLINE_VISIBILITY ~auto_ptr() throw() {delete __ptr_;}
  1829. _LIBCPP_INLINE_VISIBILITY _Tp& operator*() const throw()
  1830. {return *__ptr_;}
  1831. _LIBCPP_INLINE_VISIBILITY _Tp* operator->() const throw() {return __ptr_;}
  1832. _LIBCPP_INLINE_VISIBILITY _Tp* get() const throw() {return __ptr_;}
  1833. _LIBCPP_INLINE_VISIBILITY _Tp* release() throw()
  1834. {
  1835. _Tp* __t = __ptr_;
  1836. __ptr_ = 0;
  1837. return __t;
  1838. }
  1839. _LIBCPP_INLINE_VISIBILITY void reset(_Tp* __p = 0) throw()
  1840. {
  1841. if (__ptr_ != __p)
  1842. delete __ptr_;
  1843. __ptr_ = __p;
  1844. }
  1845. _LIBCPP_INLINE_VISIBILITY auto_ptr(auto_ptr_ref<_Tp> __p) throw() : __ptr_(__p.__ptr_) {}
  1846. template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr_ref<_Up>() throw()
  1847. {auto_ptr_ref<_Up> __t; __t.__ptr_ = release(); return __t;}
  1848. template<class _Up> _LIBCPP_INLINE_VISIBILITY operator auto_ptr<_Up>() throw()
  1849. {return auto_ptr<_Up>(release());}
  1850. };
  1851. template <>
  1852. class _LIBCPP_TEMPLATE_VIS _LIBCPP_DEPRECATED_IN_CXX11 auto_ptr<void>
  1853. {
  1854. public:
  1855. typedef void element_type;
  1856. };
  1857. #endif
  1858. template <class _Tp, int _Idx,
  1859. bool _CanBeEmptyBase =
  1860. is_empty<_Tp>::value && !__libcpp_is_final<_Tp>::value>
  1861. struct __compressed_pair_elem {
  1862. typedef _Tp _ParamT;
  1863. typedef _Tp& reference;
  1864. typedef const _Tp& const_reference;
  1865. #ifndef _LIBCPP_CXX03_LANG
  1866. _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() : __value_() {}
  1867. template <class _Up, class = typename enable_if<
  1868. !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
  1869. >::type>
  1870. _LIBCPP_INLINE_VISIBILITY
  1871. constexpr explicit
  1872. __compressed_pair_elem(_Up&& __u)
  1873. : __value_(_VSTD::forward<_Up>(__u))
  1874. {
  1875. }
  1876. template <class... _Args, size_t... _Indexes>
  1877. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  1878. __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
  1879. __tuple_indices<_Indexes...>)
  1880. : __value_(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
  1881. #else
  1882. _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_() {}
  1883. _LIBCPP_INLINE_VISIBILITY
  1884. __compressed_pair_elem(_ParamT __p) : __value_(std::forward<_ParamT>(__p)) {}
  1885. #endif
  1886. _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return __value_; }
  1887. _LIBCPP_INLINE_VISIBILITY
  1888. const_reference __get() const _NOEXCEPT { return __value_; }
  1889. private:
  1890. _Tp __value_;
  1891. };
  1892. template <class _Tp, int _Idx>
  1893. struct __compressed_pair_elem<_Tp, _Idx, true> : private _Tp {
  1894. typedef _Tp _ParamT;
  1895. typedef _Tp& reference;
  1896. typedef const _Tp& const_reference;
  1897. typedef _Tp __value_type;
  1898. #ifndef _LIBCPP_CXX03_LANG
  1899. _LIBCPP_INLINE_VISIBILITY constexpr __compressed_pair_elem() = default;
  1900. template <class _Up, class = typename enable_if<
  1901. !is_same<__compressed_pair_elem, typename decay<_Up>::type>::value
  1902. >::type>
  1903. _LIBCPP_INLINE_VISIBILITY
  1904. constexpr explicit
  1905. __compressed_pair_elem(_Up&& __u)
  1906. : __value_type(_VSTD::forward<_Up>(__u))
  1907. {}
  1908. template <class... _Args, size_t... _Indexes>
  1909. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  1910. __compressed_pair_elem(piecewise_construct_t, tuple<_Args...> __args,
  1911. __tuple_indices<_Indexes...>)
  1912. : __value_type(_VSTD::forward<_Args>(_VSTD::get<_Indexes>(__args))...) {}
  1913. #else
  1914. _LIBCPP_INLINE_VISIBILITY __compressed_pair_elem() : __value_type() {}
  1915. _LIBCPP_INLINE_VISIBILITY
  1916. __compressed_pair_elem(_ParamT __p)
  1917. : __value_type(std::forward<_ParamT>(__p)) {}
  1918. #endif
  1919. _LIBCPP_INLINE_VISIBILITY reference __get() _NOEXCEPT { return *this; }
  1920. _LIBCPP_INLINE_VISIBILITY
  1921. const_reference __get() const _NOEXCEPT { return *this; }
  1922. };
  1923. // Tag used to construct the second element of the compressed pair.
  1924. struct __second_tag {};
  1925. template <class _T1, class _T2>
  1926. class __compressed_pair : private __compressed_pair_elem<_T1, 0>,
  1927. private __compressed_pair_elem<_T2, 1> {
  1928. typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T1, 0> _Base1;
  1929. typedef _LIBCPP_NODEBUG_TYPE __compressed_pair_elem<_T2, 1> _Base2;
  1930. // NOTE: This static assert should never fire because __compressed_pair
  1931. // is *almost never* used in a scenario where it's possible for T1 == T2.
  1932. // (The exception is std::function where it is possible that the function
  1933. // object and the allocator have the same type).
  1934. static_assert((!is_same<_T1, _T2>::value),
  1935. "__compressed_pair cannot be instantated when T1 and T2 are the same type; "
  1936. "The current implementation is NOT ABI-compatible with the previous "
  1937. "implementation for this configuration");
  1938. public:
  1939. #ifndef _LIBCPP_CXX03_LANG
  1940. template <bool _Dummy = true,
  1941. class = typename enable_if<
  1942. __dependent_type<is_default_constructible<_T1>, _Dummy>::value &&
  1943. __dependent_type<is_default_constructible<_T2>, _Dummy>::value
  1944. >::type
  1945. >
  1946. _LIBCPP_INLINE_VISIBILITY
  1947. constexpr __compressed_pair() {}
  1948. template <class _Tp, typename enable_if<!is_same<typename decay<_Tp>::type,
  1949. __compressed_pair>::value,
  1950. bool>::type = true>
  1951. _LIBCPP_INLINE_VISIBILITY constexpr explicit
  1952. __compressed_pair(_Tp&& __t)
  1953. : _Base1(std::forward<_Tp>(__t)), _Base2() {}
  1954. template <class _Tp>
  1955. _LIBCPP_INLINE_VISIBILITY constexpr
  1956. __compressed_pair(__second_tag, _Tp&& __t)
  1957. : _Base1(), _Base2(std::forward<_Tp>(__t)) {}
  1958. template <class _U1, class _U2>
  1959. _LIBCPP_INLINE_VISIBILITY constexpr
  1960. __compressed_pair(_U1&& __t1, _U2&& __t2)
  1961. : _Base1(std::forward<_U1>(__t1)), _Base2(std::forward<_U2>(__t2)) {}
  1962. template <class... _Args1, class... _Args2>
  1963. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  1964. __compressed_pair(piecewise_construct_t __pc, tuple<_Args1...> __first_args,
  1965. tuple<_Args2...> __second_args)
  1966. : _Base1(__pc, _VSTD::move(__first_args),
  1967. typename __make_tuple_indices<sizeof...(_Args1)>::type()),
  1968. _Base2(__pc, _VSTD::move(__second_args),
  1969. typename __make_tuple_indices<sizeof...(_Args2)>::type()) {}
  1970. #else
  1971. _LIBCPP_INLINE_VISIBILITY
  1972. __compressed_pair() {}
  1973. _LIBCPP_INLINE_VISIBILITY explicit
  1974. __compressed_pair(_T1 __t1) : _Base1(_VSTD::forward<_T1>(__t1)) {}
  1975. _LIBCPP_INLINE_VISIBILITY
  1976. __compressed_pair(__second_tag, _T2 __t2)
  1977. : _Base1(), _Base2(_VSTD::forward<_T2>(__t2)) {}
  1978. _LIBCPP_INLINE_VISIBILITY
  1979. __compressed_pair(_T1 __t1, _T2 __t2)
  1980. : _Base1(_VSTD::forward<_T1>(__t1)), _Base2(_VSTD::forward<_T2>(__t2)) {}
  1981. #endif
  1982. _LIBCPP_INLINE_VISIBILITY
  1983. typename _Base1::reference first() _NOEXCEPT {
  1984. return static_cast<_Base1&>(*this).__get();
  1985. }
  1986. _LIBCPP_INLINE_VISIBILITY
  1987. typename _Base1::const_reference first() const _NOEXCEPT {
  1988. return static_cast<_Base1 const&>(*this).__get();
  1989. }
  1990. _LIBCPP_INLINE_VISIBILITY
  1991. typename _Base2::reference second() _NOEXCEPT {
  1992. return static_cast<_Base2&>(*this).__get();
  1993. }
  1994. _LIBCPP_INLINE_VISIBILITY
  1995. typename _Base2::const_reference second() const _NOEXCEPT {
  1996. return static_cast<_Base2 const&>(*this).__get();
  1997. }
  1998. _LIBCPP_INLINE_VISIBILITY
  1999. void swap(__compressed_pair& __x)
  2000. _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
  2001. __is_nothrow_swappable<_T2>::value)
  2002. {
  2003. using std::swap;
  2004. swap(first(), __x.first());
  2005. swap(second(), __x.second());
  2006. }
  2007. };
  2008. template <class _T1, class _T2>
  2009. inline _LIBCPP_INLINE_VISIBILITY
  2010. void swap(__compressed_pair<_T1, _T2>& __x, __compressed_pair<_T1, _T2>& __y)
  2011. _NOEXCEPT_(__is_nothrow_swappable<_T1>::value &&
  2012. __is_nothrow_swappable<_T2>::value) {
  2013. __x.swap(__y);
  2014. }
  2015. // default_delete
  2016. template <class _Tp>
  2017. struct _LIBCPP_TEMPLATE_VIS default_delete {
  2018. static_assert(!is_function<_Tp>::value,
  2019. "default_delete cannot be instantiated for function types");
  2020. #ifndef _LIBCPP_CXX03_LANG
  2021. _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
  2022. #else
  2023. _LIBCPP_INLINE_VISIBILITY default_delete() {}
  2024. #endif
  2025. template <class _Up>
  2026. _LIBCPP_INLINE_VISIBILITY
  2027. default_delete(const default_delete<_Up>&,
  2028. typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
  2029. 0) _NOEXCEPT {}
  2030. _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __ptr) const _NOEXCEPT {
  2031. static_assert(sizeof(_Tp) > 0,
  2032. "default_delete can not delete incomplete type");
  2033. static_assert(!is_void<_Tp>::value,
  2034. "default_delete can not delete incomplete type");
  2035. delete __ptr;
  2036. }
  2037. };
  2038. template <class _Tp>
  2039. struct _LIBCPP_TEMPLATE_VIS default_delete<_Tp[]> {
  2040. private:
  2041. template <class _Up>
  2042. struct _EnableIfConvertible
  2043. : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
  2044. public:
  2045. #ifndef _LIBCPP_CXX03_LANG
  2046. _LIBCPP_INLINE_VISIBILITY constexpr default_delete() _NOEXCEPT = default;
  2047. #else
  2048. _LIBCPP_INLINE_VISIBILITY default_delete() {}
  2049. #endif
  2050. template <class _Up>
  2051. _LIBCPP_INLINE_VISIBILITY
  2052. default_delete(const default_delete<_Up[]>&,
  2053. typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPT {}
  2054. template <class _Up>
  2055. _LIBCPP_INLINE_VISIBILITY
  2056. typename _EnableIfConvertible<_Up>::type
  2057. operator()(_Up* __ptr) const _NOEXCEPT {
  2058. static_assert(sizeof(_Tp) > 0,
  2059. "default_delete can not delete incomplete type");
  2060. static_assert(!is_void<_Tp>::value,
  2061. "default_delete can not delete void type");
  2062. delete[] __ptr;
  2063. }
  2064. };
  2065. template <class _Deleter>
  2066. struct __unique_ptr_deleter_sfinae {
  2067. static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
  2068. typedef const _Deleter& __lval_ref_type;
  2069. typedef _Deleter&& __good_rval_ref_type;
  2070. typedef true_type __enable_rval_overload;
  2071. };
  2072. template <class _Deleter>
  2073. struct __unique_ptr_deleter_sfinae<_Deleter const&> {
  2074. typedef const _Deleter& __lval_ref_type;
  2075. typedef const _Deleter&& __bad_rval_ref_type;
  2076. typedef false_type __enable_rval_overload;
  2077. };
  2078. template <class _Deleter>
  2079. struct __unique_ptr_deleter_sfinae<_Deleter&> {
  2080. typedef _Deleter& __lval_ref_type;
  2081. typedef _Deleter&& __bad_rval_ref_type;
  2082. typedef false_type __enable_rval_overload;
  2083. };
  2084. template <class _Tp, class _Dp = default_delete<_Tp> >
  2085. class _LIBCPP_TEMPLATE_VIS unique_ptr {
  2086. public:
  2087. typedef _Tp element_type;
  2088. typedef _Dp deleter_type;
  2089. typedef _LIBCPP_NODEBUG_TYPE typename __pointer_type<_Tp, deleter_type>::type pointer;
  2090. static_assert(!is_rvalue_reference<deleter_type>::value,
  2091. "the specified deleter type cannot be an rvalue reference");
  2092. private:
  2093. __compressed_pair<pointer, deleter_type> __ptr_;
  2094. struct __nat { int __for_bool_; };
  2095. typedef _LIBCPP_NODEBUG_TYPE __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
  2096. template <bool _Dummy>
  2097. using _LValRefType _LIBCPP_NODEBUG_TYPE =
  2098. typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
  2099. template <bool _Dummy>
  2100. using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
  2101. typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
  2102. template <bool _Dummy>
  2103. using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
  2104. typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
  2105. template <bool _Dummy, class _Deleter = typename __dependent_type<
  2106. __identity<deleter_type>, _Dummy>::type>
  2107. using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
  2108. typename enable_if<is_default_constructible<_Deleter>::value &&
  2109. !is_pointer<_Deleter>::value>::type;
  2110. template <class _ArgType>
  2111. using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
  2112. typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
  2113. template <class _UPtr, class _Up>
  2114. using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
  2115. is_convertible<typename _UPtr::pointer, pointer>::value &&
  2116. !is_array<_Up>::value
  2117. >::type;
  2118. template <class _UDel>
  2119. using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
  2120. (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
  2121. (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
  2122. >::type;
  2123. template <class _UDel>
  2124. using _EnableIfDeleterAssignable = typename enable_if<
  2125. is_assignable<_Dp&, _UDel&&>::value
  2126. >::type;
  2127. public:
  2128. template <bool _Dummy = true,
  2129. class = _EnableIfDeleterDefaultConstructible<_Dummy> >
  2130. _LIBCPP_INLINE_VISIBILITY
  2131. _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer()) {}
  2132. template <bool _Dummy = true,
  2133. class = _EnableIfDeleterDefaultConstructible<_Dummy> >
  2134. _LIBCPP_INLINE_VISIBILITY
  2135. _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer()) {}
  2136. template <bool _Dummy = true,
  2137. class = _EnableIfDeleterDefaultConstructible<_Dummy> >
  2138. _LIBCPP_INLINE_VISIBILITY
  2139. explicit unique_ptr(pointer __p) _NOEXCEPT : __ptr_(__p) {}
  2140. template <bool _Dummy = true,
  2141. class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
  2142. _LIBCPP_INLINE_VISIBILITY
  2143. unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPT
  2144. : __ptr_(__p, __d) {}
  2145. template <bool _Dummy = true,
  2146. class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
  2147. _LIBCPP_INLINE_VISIBILITY
  2148. unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
  2149. : __ptr_(__p, _VSTD::move(__d)) {
  2150. static_assert(!is_reference<deleter_type>::value,
  2151. "rvalue deleter bound to reference");
  2152. }
  2153. template <bool _Dummy = true,
  2154. class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
  2155. _LIBCPP_INLINE_VISIBILITY
  2156. unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
  2157. _LIBCPP_INLINE_VISIBILITY
  2158. unique_ptr(unique_ptr&& __u) _NOEXCEPT
  2159. : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
  2160. }
  2161. template <class _Up, class _Ep,
  2162. class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
  2163. class = _EnableIfDeleterConvertible<_Ep>
  2164. >
  2165. _LIBCPP_INLINE_VISIBILITY
  2166. unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
  2167. : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {}
  2168. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  2169. template <class _Up>
  2170. _LIBCPP_INLINE_VISIBILITY
  2171. unique_ptr(auto_ptr<_Up>&& __p,
  2172. typename enable_if<is_convertible<_Up*, _Tp*>::value &&
  2173. is_same<_Dp, default_delete<_Tp> >::value,
  2174. __nat>::type = __nat()) _NOEXCEPT
  2175. : __ptr_(__p.release()) {}
  2176. #endif
  2177. _LIBCPP_INLINE_VISIBILITY
  2178. unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
  2179. reset(__u.release());
  2180. __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
  2181. return *this;
  2182. }
  2183. template <class _Up, class _Ep,
  2184. class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
  2185. class = _EnableIfDeleterAssignable<_Ep>
  2186. >
  2187. _LIBCPP_INLINE_VISIBILITY
  2188. unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
  2189. reset(__u.release());
  2190. __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
  2191. return *this;
  2192. }
  2193. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  2194. template <class _Up>
  2195. _LIBCPP_INLINE_VISIBILITY
  2196. typename enable_if<is_convertible<_Up*, _Tp*>::value &&
  2197. is_same<_Dp, default_delete<_Tp> >::value,
  2198. unique_ptr&>::type
  2199. operator=(auto_ptr<_Up> __p) {
  2200. reset(__p.release());
  2201. return *this;
  2202. }
  2203. #endif
  2204. #ifdef _LIBCPP_CXX03_LANG
  2205. unique_ptr(unique_ptr const&) = delete;
  2206. unique_ptr& operator=(unique_ptr const&) = delete;
  2207. #endif
  2208. _LIBCPP_INLINE_VISIBILITY
  2209. ~unique_ptr() { reset(); }
  2210. _LIBCPP_INLINE_VISIBILITY
  2211. unique_ptr& operator=(nullptr_t) _NOEXCEPT {
  2212. reset();
  2213. return *this;
  2214. }
  2215. _LIBCPP_INLINE_VISIBILITY
  2216. typename add_lvalue_reference<_Tp>::type
  2217. operator*() const {
  2218. return *__ptr_.first();
  2219. }
  2220. _LIBCPP_INLINE_VISIBILITY
  2221. pointer operator->() const _NOEXCEPT {
  2222. return __ptr_.first();
  2223. }
  2224. _LIBCPP_INLINE_VISIBILITY
  2225. pointer get() const _NOEXCEPT {
  2226. return __ptr_.first();
  2227. }
  2228. _LIBCPP_INLINE_VISIBILITY
  2229. deleter_type& get_deleter() _NOEXCEPT {
  2230. return __ptr_.second();
  2231. }
  2232. _LIBCPP_INLINE_VISIBILITY
  2233. const deleter_type& get_deleter() const _NOEXCEPT {
  2234. return __ptr_.second();
  2235. }
  2236. _LIBCPP_INLINE_VISIBILITY
  2237. _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
  2238. return __ptr_.first() != nullptr;
  2239. }
  2240. _LIBCPP_INLINE_VISIBILITY
  2241. pointer release() _NOEXCEPT {
  2242. pointer __t = __ptr_.first();
  2243. __ptr_.first() = pointer();
  2244. return __t;
  2245. }
  2246. _LIBCPP_INLINE_VISIBILITY
  2247. void reset(pointer __p = pointer()) _NOEXCEPT {
  2248. pointer __tmp = __ptr_.first();
  2249. __ptr_.first() = __p;
  2250. if (__tmp)
  2251. __ptr_.second()(__tmp);
  2252. }
  2253. _LIBCPP_INLINE_VISIBILITY
  2254. void swap(unique_ptr& __u) _NOEXCEPT {
  2255. __ptr_.swap(__u.__ptr_);
  2256. }
  2257. };
  2258. template <class _Tp, class _Dp>
  2259. class _LIBCPP_TEMPLATE_VIS unique_ptr<_Tp[], _Dp> {
  2260. public:
  2261. typedef _Tp element_type;
  2262. typedef _Dp deleter_type;
  2263. typedef typename __pointer_type<_Tp, deleter_type>::type pointer;
  2264. private:
  2265. __compressed_pair<pointer, deleter_type> __ptr_;
  2266. template <class _From>
  2267. struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
  2268. template <class _FromElem>
  2269. struct _CheckArrayPointerConversion<_FromElem*>
  2270. : integral_constant<bool,
  2271. is_same<_FromElem*, pointer>::value ||
  2272. (is_same<pointer, element_type*>::value &&
  2273. is_convertible<_FromElem(*)[], element_type(*)[]>::value)
  2274. >
  2275. {};
  2276. typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
  2277. template <bool _Dummy>
  2278. using _LValRefType _LIBCPP_NODEBUG_TYPE =
  2279. typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
  2280. template <bool _Dummy>
  2281. using _GoodRValRefType _LIBCPP_NODEBUG_TYPE =
  2282. typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
  2283. template <bool _Dummy>
  2284. using _BadRValRefType _LIBCPP_NODEBUG_TYPE =
  2285. typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
  2286. template <bool _Dummy, class _Deleter = typename __dependent_type<
  2287. __identity<deleter_type>, _Dummy>::type>
  2288. using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE =
  2289. typename enable_if<is_default_constructible<_Deleter>::value &&
  2290. !is_pointer<_Deleter>::value>::type;
  2291. template <class _ArgType>
  2292. using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE =
  2293. typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
  2294. template <class _Pp>
  2295. using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
  2296. _CheckArrayPointerConversion<_Pp>::value
  2297. >::type;
  2298. template <class _UPtr, class _Up,
  2299. class _ElemT = typename _UPtr::element_type>
  2300. using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
  2301. is_array<_Up>::value &&
  2302. is_same<pointer, element_type*>::value &&
  2303. is_same<typename _UPtr::pointer, _ElemT*>::value &&
  2304. is_convertible<_ElemT(*)[], element_type(*)[]>::value
  2305. >::type;
  2306. template <class _UDel>
  2307. using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE = typename enable_if<
  2308. (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
  2309. (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
  2310. >::type;
  2311. template <class _UDel>
  2312. using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE = typename enable_if<
  2313. is_assignable<_Dp&, _UDel&&>::value
  2314. >::type;
  2315. public:
  2316. template <bool _Dummy = true,
  2317. class = _EnableIfDeleterDefaultConstructible<_Dummy> >
  2318. _LIBCPP_INLINE_VISIBILITY
  2319. _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT : __ptr_(pointer()) {}
  2320. template <bool _Dummy = true,
  2321. class = _EnableIfDeleterDefaultConstructible<_Dummy> >
  2322. _LIBCPP_INLINE_VISIBILITY
  2323. _LIBCPP_CONSTEXPR unique_ptr(nullptr_t) _NOEXCEPT : __ptr_(pointer()) {}
  2324. template <class _Pp, bool _Dummy = true,
  2325. class = _EnableIfDeleterDefaultConstructible<_Dummy>,
  2326. class = _EnableIfPointerConvertible<_Pp> >
  2327. _LIBCPP_INLINE_VISIBILITY
  2328. explicit unique_ptr(_Pp __p) _NOEXCEPT
  2329. : __ptr_(__p) {}
  2330. template <class _Pp, bool _Dummy = true,
  2331. class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
  2332. class = _EnableIfPointerConvertible<_Pp> >
  2333. _LIBCPP_INLINE_VISIBILITY
  2334. unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPT
  2335. : __ptr_(__p, __d) {}
  2336. template <bool _Dummy = true,
  2337. class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
  2338. _LIBCPP_INLINE_VISIBILITY
  2339. unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPT
  2340. : __ptr_(nullptr, __d) {}
  2341. template <class _Pp, bool _Dummy = true,
  2342. class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
  2343. class = _EnableIfPointerConvertible<_Pp> >
  2344. _LIBCPP_INLINE_VISIBILITY
  2345. unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
  2346. : __ptr_(__p, _VSTD::move(__d)) {
  2347. static_assert(!is_reference<deleter_type>::value,
  2348. "rvalue deleter bound to reference");
  2349. }
  2350. template <bool _Dummy = true,
  2351. class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
  2352. _LIBCPP_INLINE_VISIBILITY
  2353. unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPT
  2354. : __ptr_(nullptr, _VSTD::move(__d)) {
  2355. static_assert(!is_reference<deleter_type>::value,
  2356. "rvalue deleter bound to reference");
  2357. }
  2358. template <class _Pp, bool _Dummy = true,
  2359. class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
  2360. class = _EnableIfPointerConvertible<_Pp> >
  2361. _LIBCPP_INLINE_VISIBILITY
  2362. unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
  2363. _LIBCPP_INLINE_VISIBILITY
  2364. unique_ptr(unique_ptr&& __u) _NOEXCEPT
  2365. : __ptr_(__u.release(), _VSTD::forward<deleter_type>(__u.get_deleter())) {
  2366. }
  2367. _LIBCPP_INLINE_VISIBILITY
  2368. unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPT {
  2369. reset(__u.release());
  2370. __ptr_.second() = _VSTD::forward<deleter_type>(__u.get_deleter());
  2371. return *this;
  2372. }
  2373. template <class _Up, class _Ep,
  2374. class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
  2375. class = _EnableIfDeleterConvertible<_Ep>
  2376. >
  2377. _LIBCPP_INLINE_VISIBILITY
  2378. unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT
  2379. : __ptr_(__u.release(), _VSTD::forward<_Ep>(__u.get_deleter())) {
  2380. }
  2381. template <class _Up, class _Ep,
  2382. class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
  2383. class = _EnableIfDeleterAssignable<_Ep>
  2384. >
  2385. _LIBCPP_INLINE_VISIBILITY
  2386. unique_ptr&
  2387. operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPT {
  2388. reset(__u.release());
  2389. __ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
  2390. return *this;
  2391. }
  2392. #ifdef _LIBCPP_CXX03_LANG
  2393. unique_ptr(unique_ptr const&) = delete;
  2394. unique_ptr& operator=(unique_ptr const&) = delete;
  2395. #endif
  2396. public:
  2397. _LIBCPP_INLINE_VISIBILITY
  2398. ~unique_ptr() { reset(); }
  2399. _LIBCPP_INLINE_VISIBILITY
  2400. unique_ptr& operator=(nullptr_t) _NOEXCEPT {
  2401. reset();
  2402. return *this;
  2403. }
  2404. _LIBCPP_INLINE_VISIBILITY
  2405. typename add_lvalue_reference<_Tp>::type
  2406. operator[](size_t __i) const {
  2407. return __ptr_.first()[__i];
  2408. }
  2409. _LIBCPP_INLINE_VISIBILITY
  2410. pointer get() const _NOEXCEPT {
  2411. return __ptr_.first();
  2412. }
  2413. _LIBCPP_INLINE_VISIBILITY
  2414. deleter_type& get_deleter() _NOEXCEPT {
  2415. return __ptr_.second();
  2416. }
  2417. _LIBCPP_INLINE_VISIBILITY
  2418. const deleter_type& get_deleter() const _NOEXCEPT {
  2419. return __ptr_.second();
  2420. }
  2421. _LIBCPP_INLINE_VISIBILITY
  2422. _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {
  2423. return __ptr_.first() != nullptr;
  2424. }
  2425. _LIBCPP_INLINE_VISIBILITY
  2426. pointer release() _NOEXCEPT {
  2427. pointer __t = __ptr_.first();
  2428. __ptr_.first() = pointer();
  2429. return __t;
  2430. }
  2431. template <class _Pp>
  2432. _LIBCPP_INLINE_VISIBILITY
  2433. typename enable_if<
  2434. _CheckArrayPointerConversion<_Pp>::value
  2435. >::type
  2436. reset(_Pp __p) _NOEXCEPT {
  2437. pointer __tmp = __ptr_.first();
  2438. __ptr_.first() = __p;
  2439. if (__tmp)
  2440. __ptr_.second()(__tmp);
  2441. }
  2442. _LIBCPP_INLINE_VISIBILITY
  2443. void reset(nullptr_t = nullptr) _NOEXCEPT {
  2444. pointer __tmp = __ptr_.first();
  2445. __ptr_.first() = nullptr;
  2446. if (__tmp)
  2447. __ptr_.second()(__tmp);
  2448. }
  2449. _LIBCPP_INLINE_VISIBILITY
  2450. void swap(unique_ptr& __u) _NOEXCEPT {
  2451. __ptr_.swap(__u.__ptr_);
  2452. }
  2453. };
  2454. template <class _Tp, class _Dp>
  2455. inline _LIBCPP_INLINE_VISIBILITY
  2456. typename enable_if<
  2457. __is_swappable<_Dp>::value,
  2458. void
  2459. >::type
  2460. swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPT {__x.swap(__y);}
  2461. template <class _T1, class _D1, class _T2, class _D2>
  2462. inline _LIBCPP_INLINE_VISIBILITY
  2463. bool
  2464. operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
  2465. template <class _T1, class _D1, class _T2, class _D2>
  2466. inline _LIBCPP_INLINE_VISIBILITY
  2467. bool
  2468. operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
  2469. template <class _T1, class _D1, class _T2, class _D2>
  2470. inline _LIBCPP_INLINE_VISIBILITY
  2471. bool
  2472. operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
  2473. {
  2474. typedef typename unique_ptr<_T1, _D1>::pointer _P1;
  2475. typedef typename unique_ptr<_T2, _D2>::pointer _P2;
  2476. typedef typename common_type<_P1, _P2>::type _Vp;
  2477. return less<_Vp>()(__x.get(), __y.get());
  2478. }
  2479. template <class _T1, class _D1, class _T2, class _D2>
  2480. inline _LIBCPP_INLINE_VISIBILITY
  2481. bool
  2482. operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
  2483. template <class _T1, class _D1, class _T2, class _D2>
  2484. inline _LIBCPP_INLINE_VISIBILITY
  2485. bool
  2486. operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
  2487. template <class _T1, class _D1, class _T2, class _D2>
  2488. inline _LIBCPP_INLINE_VISIBILITY
  2489. bool
  2490. operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
  2491. template <class _T1, class _D1>
  2492. inline _LIBCPP_INLINE_VISIBILITY
  2493. bool
  2494. operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
  2495. {
  2496. return !__x;
  2497. }
  2498. template <class _T1, class _D1>
  2499. inline _LIBCPP_INLINE_VISIBILITY
  2500. bool
  2501. operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
  2502. {
  2503. return !__x;
  2504. }
  2505. template <class _T1, class _D1>
  2506. inline _LIBCPP_INLINE_VISIBILITY
  2507. bool
  2508. operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
  2509. {
  2510. return static_cast<bool>(__x);
  2511. }
  2512. template <class _T1, class _D1>
  2513. inline _LIBCPP_INLINE_VISIBILITY
  2514. bool
  2515. operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
  2516. {
  2517. return static_cast<bool>(__x);
  2518. }
  2519. template <class _T1, class _D1>
  2520. inline _LIBCPP_INLINE_VISIBILITY
  2521. bool
  2522. operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
  2523. {
  2524. typedef typename unique_ptr<_T1, _D1>::pointer _P1;
  2525. return less<_P1>()(__x.get(), nullptr);
  2526. }
  2527. template <class _T1, class _D1>
  2528. inline _LIBCPP_INLINE_VISIBILITY
  2529. bool
  2530. operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
  2531. {
  2532. typedef typename unique_ptr<_T1, _D1>::pointer _P1;
  2533. return less<_P1>()(nullptr, __x.get());
  2534. }
  2535. template <class _T1, class _D1>
  2536. inline _LIBCPP_INLINE_VISIBILITY
  2537. bool
  2538. operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
  2539. {
  2540. return nullptr < __x;
  2541. }
  2542. template <class _T1, class _D1>
  2543. inline _LIBCPP_INLINE_VISIBILITY
  2544. bool
  2545. operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
  2546. {
  2547. return __x < nullptr;
  2548. }
  2549. template <class _T1, class _D1>
  2550. inline _LIBCPP_INLINE_VISIBILITY
  2551. bool
  2552. operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
  2553. {
  2554. return !(nullptr < __x);
  2555. }
  2556. template <class _T1, class _D1>
  2557. inline _LIBCPP_INLINE_VISIBILITY
  2558. bool
  2559. operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
  2560. {
  2561. return !(__x < nullptr);
  2562. }
  2563. template <class _T1, class _D1>
  2564. inline _LIBCPP_INLINE_VISIBILITY
  2565. bool
  2566. operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
  2567. {
  2568. return !(__x < nullptr);
  2569. }
  2570. template <class _T1, class _D1>
  2571. inline _LIBCPP_INLINE_VISIBILITY
  2572. bool
  2573. operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
  2574. {
  2575. return !(nullptr < __x);
  2576. }
  2577. #if _LIBCPP_STD_VER > 11
  2578. template<class _Tp>
  2579. struct __unique_if
  2580. {
  2581. typedef unique_ptr<_Tp> __unique_single;
  2582. };
  2583. template<class _Tp>
  2584. struct __unique_if<_Tp[]>
  2585. {
  2586. typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
  2587. };
  2588. template<class _Tp, size_t _Np>
  2589. struct __unique_if<_Tp[_Np]>
  2590. {
  2591. typedef void __unique_array_known_bound;
  2592. };
  2593. template<class _Tp, class... _Args>
  2594. inline _LIBCPP_INLINE_VISIBILITY
  2595. typename __unique_if<_Tp>::__unique_single
  2596. make_unique(_Args&&... __args)
  2597. {
  2598. return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
  2599. }
  2600. template<class _Tp>
  2601. inline _LIBCPP_INLINE_VISIBILITY
  2602. typename __unique_if<_Tp>::__unique_array_unknown_bound
  2603. make_unique(size_t __n)
  2604. {
  2605. typedef typename remove_extent<_Tp>::type _Up;
  2606. return unique_ptr<_Tp>(new _Up[__n]());
  2607. }
  2608. template<class _Tp, class... _Args>
  2609. typename __unique_if<_Tp>::__unique_array_known_bound
  2610. make_unique(_Args&&...) = delete;
  2611. #endif // _LIBCPP_STD_VER > 11
  2612. template <class _Tp, class _Dp>
  2613. #ifdef _LIBCPP_CXX03_LANG
  2614. struct _LIBCPP_TEMPLATE_VIS hash<unique_ptr<_Tp, _Dp> >
  2615. #else
  2616. struct _LIBCPP_TEMPLATE_VIS hash<__enable_hash_helper<
  2617. unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
  2618. #endif
  2619. {
  2620. typedef unique_ptr<_Tp, _Dp> argument_type;
  2621. typedef size_t result_type;
  2622. _LIBCPP_INLINE_VISIBILITY
  2623. result_type operator()(const argument_type& __ptr) const
  2624. {
  2625. typedef typename argument_type::pointer pointer;
  2626. return hash<pointer>()(__ptr.get());
  2627. }
  2628. };
  2629. struct __destruct_n
  2630. {
  2631. private:
  2632. size_t __size_;
  2633. template <class _Tp>
  2634. _LIBCPP_INLINE_VISIBILITY void __process(_Tp* __p, false_type) _NOEXCEPT
  2635. {for (size_t __i = 0; __i < __size_; ++__i, ++__p) __p->~_Tp();}
  2636. template <class _Tp>
  2637. _LIBCPP_INLINE_VISIBILITY void __process(_Tp*, true_type) _NOEXCEPT
  2638. {}
  2639. _LIBCPP_INLINE_VISIBILITY void __incr(false_type) _NOEXCEPT
  2640. {++__size_;}
  2641. _LIBCPP_INLINE_VISIBILITY void __incr(true_type) _NOEXCEPT
  2642. {}
  2643. _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, false_type) _NOEXCEPT
  2644. {__size_ = __s;}
  2645. _LIBCPP_INLINE_VISIBILITY void __set(size_t, true_type) _NOEXCEPT
  2646. {}
  2647. public:
  2648. _LIBCPP_INLINE_VISIBILITY explicit __destruct_n(size_t __s) _NOEXCEPT
  2649. : __size_(__s) {}
  2650. template <class _Tp>
  2651. _LIBCPP_INLINE_VISIBILITY void __incr(_Tp*) _NOEXCEPT
  2652. {__incr(integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
  2653. template <class _Tp>
  2654. _LIBCPP_INLINE_VISIBILITY void __set(size_t __s, _Tp*) _NOEXCEPT
  2655. {__set(__s, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
  2656. template <class _Tp>
  2657. _LIBCPP_INLINE_VISIBILITY void operator()(_Tp* __p) _NOEXCEPT
  2658. {__process(__p, integral_constant<bool, is_trivially_destructible<_Tp>::value>());}
  2659. };
  2660. template <class _Alloc>
  2661. class __allocator_destructor
  2662. {
  2663. typedef _LIBCPP_NODEBUG_TYPE allocator_traits<_Alloc> __alloc_traits;
  2664. public:
  2665. typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::pointer pointer;
  2666. typedef _LIBCPP_NODEBUG_TYPE typename __alloc_traits::size_type size_type;
  2667. private:
  2668. _Alloc& __alloc_;
  2669. size_type __s_;
  2670. public:
  2671. _LIBCPP_INLINE_VISIBILITY __allocator_destructor(_Alloc& __a, size_type __s)
  2672. _NOEXCEPT
  2673. : __alloc_(__a), __s_(__s) {}
  2674. _LIBCPP_INLINE_VISIBILITY
  2675. void operator()(pointer __p) _NOEXCEPT
  2676. {__alloc_traits::deallocate(__alloc_, __p, __s_);}
  2677. };
  2678. template <class _InputIterator, class _ForwardIterator>
  2679. _ForwardIterator
  2680. uninitialized_copy(_InputIterator __f, _InputIterator __l, _ForwardIterator __r)
  2681. {
  2682. typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
  2683. #ifndef _LIBCPP_NO_EXCEPTIONS
  2684. _ForwardIterator __s = __r;
  2685. try
  2686. {
  2687. #endif
  2688. for (; __f != __l; ++__f, (void) ++__r)
  2689. ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
  2690. #ifndef _LIBCPP_NO_EXCEPTIONS
  2691. }
  2692. catch (...)
  2693. {
  2694. for (; __s != __r; ++__s)
  2695. __s->~value_type();
  2696. throw;
  2697. }
  2698. #endif
  2699. return __r;
  2700. }
  2701. template <class _InputIterator, class _Size, class _ForwardIterator>
  2702. _ForwardIterator
  2703. uninitialized_copy_n(_InputIterator __f, _Size __n, _ForwardIterator __r)
  2704. {
  2705. typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
  2706. #ifndef _LIBCPP_NO_EXCEPTIONS
  2707. _ForwardIterator __s = __r;
  2708. try
  2709. {
  2710. #endif
  2711. for (; __n > 0; ++__f, (void) ++__r, (void) --__n)
  2712. ::new (static_cast<void*>(_VSTD::addressof(*__r))) value_type(*__f);
  2713. #ifndef _LIBCPP_NO_EXCEPTIONS
  2714. }
  2715. catch (...)
  2716. {
  2717. for (; __s != __r; ++__s)
  2718. __s->~value_type();
  2719. throw;
  2720. }
  2721. #endif
  2722. return __r;
  2723. }
  2724. template <class _ForwardIterator, class _Tp>
  2725. void
  2726. uninitialized_fill(_ForwardIterator __f, _ForwardIterator __l, const _Tp& __x)
  2727. {
  2728. typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
  2729. #ifndef _LIBCPP_NO_EXCEPTIONS
  2730. _ForwardIterator __s = __f;
  2731. try
  2732. {
  2733. #endif
  2734. for (; __f != __l; ++__f)
  2735. ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
  2736. #ifndef _LIBCPP_NO_EXCEPTIONS
  2737. }
  2738. catch (...)
  2739. {
  2740. for (; __s != __f; ++__s)
  2741. __s->~value_type();
  2742. throw;
  2743. }
  2744. #endif
  2745. }
  2746. template <class _ForwardIterator, class _Size, class _Tp>
  2747. _ForwardIterator
  2748. uninitialized_fill_n(_ForwardIterator __f, _Size __n, const _Tp& __x)
  2749. {
  2750. typedef typename iterator_traits<_ForwardIterator>::value_type value_type;
  2751. #ifndef _LIBCPP_NO_EXCEPTIONS
  2752. _ForwardIterator __s = __f;
  2753. try
  2754. {
  2755. #endif
  2756. for (; __n > 0; ++__f, (void) --__n)
  2757. ::new (static_cast<void*>(_VSTD::addressof(*__f))) value_type(__x);
  2758. #ifndef _LIBCPP_NO_EXCEPTIONS
  2759. }
  2760. catch (...)
  2761. {
  2762. for (; __s != __f; ++__s)
  2763. __s->~value_type();
  2764. throw;
  2765. }
  2766. #endif
  2767. return __f;
  2768. }
  2769. #if _LIBCPP_STD_VER > 14
  2770. template <class _Tp>
  2771. inline _LIBCPP_INLINE_VISIBILITY
  2772. void destroy_at(_Tp* __loc) {
  2773. _LIBCPP_ASSERT(__loc, "null pointer given to destroy_at");
  2774. __loc->~_Tp();
  2775. }
  2776. template <class _ForwardIterator>
  2777. inline _LIBCPP_INLINE_VISIBILITY
  2778. void destroy(_ForwardIterator __first, _ForwardIterator __last) {
  2779. for (; __first != __last; ++__first)
  2780. _VSTD::destroy_at(_VSTD::addressof(*__first));
  2781. }
  2782. template <class _ForwardIterator, class _Size>
  2783. inline _LIBCPP_INLINE_VISIBILITY
  2784. _ForwardIterator destroy_n(_ForwardIterator __first, _Size __n) {
  2785. for (; __n > 0; (void)++__first, --__n)
  2786. _VSTD::destroy_at(_VSTD::addressof(*__first));
  2787. return __first;
  2788. }
  2789. template <class _ForwardIterator>
  2790. inline _LIBCPP_INLINE_VISIBILITY
  2791. void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last) {
  2792. using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
  2793. auto __idx = __first;
  2794. #ifndef _LIBCPP_NO_EXCEPTIONS
  2795. try {
  2796. #endif
  2797. for (; __idx != __last; ++__idx)
  2798. ::new((void*)_VSTD::addressof(*__idx)) _Vt;
  2799. #ifndef _LIBCPP_NO_EXCEPTIONS
  2800. } catch (...) {
  2801. _VSTD::destroy(__first, __idx);
  2802. throw;
  2803. }
  2804. #endif
  2805. }
  2806. template <class _ForwardIterator, class _Size>
  2807. inline _LIBCPP_INLINE_VISIBILITY
  2808. _ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __n) {
  2809. using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
  2810. auto __idx = __first;
  2811. #ifndef _LIBCPP_NO_EXCEPTIONS
  2812. try {
  2813. #endif
  2814. for (; __n > 0; (void)++__idx, --__n)
  2815. ::new((void*)_VSTD::addressof(*__idx)) _Vt;
  2816. return __idx;
  2817. #ifndef _LIBCPP_NO_EXCEPTIONS
  2818. } catch (...) {
  2819. _VSTD::destroy(__first, __idx);
  2820. throw;
  2821. }
  2822. #endif
  2823. }
  2824. template <class _ForwardIterator>
  2825. inline _LIBCPP_INLINE_VISIBILITY
  2826. void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last) {
  2827. using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
  2828. auto __idx = __first;
  2829. #ifndef _LIBCPP_NO_EXCEPTIONS
  2830. try {
  2831. #endif
  2832. for (; __idx != __last; ++__idx)
  2833. ::new((void*)_VSTD::addressof(*__idx)) _Vt();
  2834. #ifndef _LIBCPP_NO_EXCEPTIONS
  2835. } catch (...) {
  2836. _VSTD::destroy(__first, __idx);
  2837. throw;
  2838. }
  2839. #endif
  2840. }
  2841. template <class _ForwardIterator, class _Size>
  2842. inline _LIBCPP_INLINE_VISIBILITY
  2843. _ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __n) {
  2844. using _Vt = typename iterator_traits<_ForwardIterator>::value_type;
  2845. auto __idx = __first;
  2846. #ifndef _LIBCPP_NO_EXCEPTIONS
  2847. try {
  2848. #endif
  2849. for (; __n > 0; (void)++__idx, --__n)
  2850. ::new((void*)_VSTD::addressof(*__idx)) _Vt();
  2851. return __idx;
  2852. #ifndef _LIBCPP_NO_EXCEPTIONS
  2853. } catch (...) {
  2854. _VSTD::destroy(__first, __idx);
  2855. throw;
  2856. }
  2857. #endif
  2858. }
  2859. template <class _InputIt, class _ForwardIt>
  2860. inline _LIBCPP_INLINE_VISIBILITY
  2861. _ForwardIt uninitialized_move(_InputIt __first, _InputIt __last, _ForwardIt __first_res) {
  2862. using _Vt = typename iterator_traits<_ForwardIt>::value_type;
  2863. auto __idx = __first_res;
  2864. #ifndef _LIBCPP_NO_EXCEPTIONS
  2865. try {
  2866. #endif
  2867. for (; __first != __last; (void)++__idx, ++__first)
  2868. ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
  2869. return __idx;
  2870. #ifndef _LIBCPP_NO_EXCEPTIONS
  2871. } catch (...) {
  2872. _VSTD::destroy(__first_res, __idx);
  2873. throw;
  2874. }
  2875. #endif
  2876. }
  2877. template <class _InputIt, class _Size, class _ForwardIt>
  2878. inline _LIBCPP_INLINE_VISIBILITY
  2879. pair<_InputIt, _ForwardIt>
  2880. uninitialized_move_n(_InputIt __first, _Size __n, _ForwardIt __first_res) {
  2881. using _Vt = typename iterator_traits<_ForwardIt>::value_type;
  2882. auto __idx = __first_res;
  2883. #ifndef _LIBCPP_NO_EXCEPTIONS
  2884. try {
  2885. #endif
  2886. for (; __n > 0; ++__idx, (void)++__first, --__n)
  2887. ::new((void*)_VSTD::addressof(*__idx)) _Vt(std::move(*__first));
  2888. return {__first, __idx};
  2889. #ifndef _LIBCPP_NO_EXCEPTIONS
  2890. } catch (...) {
  2891. _VSTD::destroy(__first_res, __idx);
  2892. throw;
  2893. }
  2894. #endif
  2895. }
  2896. #endif // _LIBCPP_STD_VER > 14
  2897. // NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
  2898. // should be sufficient for thread safety.
  2899. // See https://bugs.llvm.org/show_bug.cgi?id=22803
  2900. #if defined(__clang__) && __has_builtin(__atomic_add_fetch) \
  2901. && defined(__ATOMIC_RELAXED) \
  2902. && defined(__ATOMIC_ACQ_REL)
  2903. # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
  2904. #elif defined(_LIBCPP_COMPILER_GCC)
  2905. # define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT
  2906. #endif
  2907. template <class _Tp>
  2908. inline _LIBCPP_INLINE_VISIBILITY _Tp
  2909. __libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT
  2910. {
  2911. #if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
  2912. return __atomic_add_fetch(&__t, 1, __ATOMIC_RELAXED);
  2913. #else
  2914. return __t += 1;
  2915. #endif
  2916. }
  2917. template <class _Tp>
  2918. inline _LIBCPP_INLINE_VISIBILITY _Tp
  2919. __libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT
  2920. {
  2921. #if defined(_LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT) && !defined(_LIBCPP_HAS_NO_THREADS)
  2922. return __atomic_add_fetch(&__t, -1, __ATOMIC_ACQ_REL);
  2923. #else
  2924. return __t -= 1;
  2925. #endif
  2926. }
  2927. class _LIBCPP_EXCEPTION_ABI bad_weak_ptr
  2928. : public std::exception
  2929. {
  2930. public:
  2931. virtual ~bad_weak_ptr() _NOEXCEPT;
  2932. virtual const char* what() const _NOEXCEPT;
  2933. };
  2934. _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
  2935. void __throw_bad_weak_ptr()
  2936. {
  2937. #ifndef _LIBCPP_NO_EXCEPTIONS
  2938. throw bad_weak_ptr();
  2939. #else
  2940. _VSTD::abort();
  2941. #endif
  2942. }
  2943. template<class _Tp> class _LIBCPP_TEMPLATE_VIS weak_ptr;
  2944. class _LIBCPP_TYPE_VIS __shared_count
  2945. {
  2946. __shared_count(const __shared_count&);
  2947. __shared_count& operator=(const __shared_count&);
  2948. protected:
  2949. long __shared_owners_;
  2950. virtual ~__shared_count();
  2951. private:
  2952. virtual void __on_zero_shared() _NOEXCEPT = 0;
  2953. public:
  2954. _LIBCPP_INLINE_VISIBILITY
  2955. explicit __shared_count(long __refs = 0) _NOEXCEPT
  2956. : __shared_owners_(__refs) {}
  2957. #if defined(_LIBCPP_BUILDING_LIBRARY) && \
  2958. defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
  2959. void __add_shared() _NOEXCEPT;
  2960. bool __release_shared() _NOEXCEPT;
  2961. #else
  2962. _LIBCPP_INLINE_VISIBILITY
  2963. void __add_shared() _NOEXCEPT {
  2964. __libcpp_atomic_refcount_increment(__shared_owners_);
  2965. }
  2966. _LIBCPP_INLINE_VISIBILITY
  2967. bool __release_shared() _NOEXCEPT {
  2968. if (__libcpp_atomic_refcount_decrement(__shared_owners_) == -1) {
  2969. __on_zero_shared();
  2970. return true;
  2971. }
  2972. return false;
  2973. }
  2974. #endif
  2975. _LIBCPP_INLINE_VISIBILITY
  2976. long use_count() const _NOEXCEPT {
  2977. return __libcpp_relaxed_load(&__shared_owners_) + 1;
  2978. }
  2979. };
  2980. class _LIBCPP_TYPE_VIS __shared_weak_count
  2981. : private __shared_count
  2982. {
  2983. long __shared_weak_owners_;
  2984. public:
  2985. _LIBCPP_INLINE_VISIBILITY
  2986. explicit __shared_weak_count(long __refs = 0) _NOEXCEPT
  2987. : __shared_count(__refs),
  2988. __shared_weak_owners_(__refs) {}
  2989. protected:
  2990. virtual ~__shared_weak_count();
  2991. public:
  2992. #if defined(_LIBCPP_BUILDING_LIBRARY) && \
  2993. defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
  2994. void __add_shared() _NOEXCEPT;
  2995. void __add_weak() _NOEXCEPT;
  2996. void __release_shared() _NOEXCEPT;
  2997. #else
  2998. _LIBCPP_INLINE_VISIBILITY
  2999. void __add_shared() _NOEXCEPT {
  3000. __shared_count::__add_shared();
  3001. }
  3002. _LIBCPP_INLINE_VISIBILITY
  3003. void __add_weak() _NOEXCEPT {
  3004. __libcpp_atomic_refcount_increment(__shared_weak_owners_);
  3005. }
  3006. _LIBCPP_INLINE_VISIBILITY
  3007. void __release_shared() _NOEXCEPT {
  3008. if (__shared_count::__release_shared())
  3009. __release_weak();
  3010. }
  3011. #endif
  3012. void __release_weak() _NOEXCEPT;
  3013. _LIBCPP_INLINE_VISIBILITY
  3014. long use_count() const _NOEXCEPT {return __shared_count::use_count();}
  3015. __shared_weak_count* lock() _NOEXCEPT;
  3016. // Define the function out only if we build static libc++ without RTTI.
  3017. // Otherwise we may break clients who need to compile their projects with
  3018. // -fno-rtti and yet link against a libc++.dylib compiled
  3019. // without -fno-rtti.
  3020. #if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)
  3021. virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
  3022. #endif
  3023. private:
  3024. virtual void __on_zero_shared_weak() _NOEXCEPT = 0;
  3025. };
  3026. template <class _Tp, class _Dp, class _Alloc>
  3027. class __shared_ptr_pointer
  3028. : public __shared_weak_count
  3029. {
  3030. __compressed_pair<__compressed_pair<_Tp, _Dp>, _Alloc> __data_;
  3031. public:
  3032. _LIBCPP_INLINE_VISIBILITY
  3033. __shared_ptr_pointer(_Tp __p, _Dp __d, _Alloc __a)
  3034. : __data_(__compressed_pair<_Tp, _Dp>(__p, _VSTD::move(__d)), _VSTD::move(__a)) {}
  3035. #ifndef _LIBCPP_NO_RTTI
  3036. virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;
  3037. #endif
  3038. private:
  3039. virtual void __on_zero_shared() _NOEXCEPT;
  3040. virtual void __on_zero_shared_weak() _NOEXCEPT;
  3041. };
  3042. #ifndef _LIBCPP_NO_RTTI
  3043. template <class _Tp, class _Dp, class _Alloc>
  3044. const void*
  3045. __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__get_deleter(const type_info& __t) const _NOEXCEPT
  3046. {
  3047. return __t == typeid(_Dp) ? _VSTD::addressof(__data_.first().second()) : nullptr;
  3048. }
  3049. #endif // _LIBCPP_NO_RTTI
  3050. template <class _Tp, class _Dp, class _Alloc>
  3051. void
  3052. __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared() _NOEXCEPT
  3053. {
  3054. __data_.first().second()(__data_.first().first());
  3055. __data_.first().second().~_Dp();
  3056. }
  3057. template <class _Tp, class _Dp, class _Alloc>
  3058. void
  3059. __shared_ptr_pointer<_Tp, _Dp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
  3060. {
  3061. typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_pointer>::type _Al;
  3062. typedef allocator_traits<_Al> _ATraits;
  3063. typedef pointer_traits<typename _ATraits::pointer> _PTraits;
  3064. _Al __a(__data_.second());
  3065. __data_.second().~_Alloc();
  3066. __a.deallocate(_PTraits::pointer_to(*this), 1);
  3067. }
  3068. template <class _Tp, class _Alloc>
  3069. class __shared_ptr_emplace
  3070. : public __shared_weak_count
  3071. {
  3072. __compressed_pair<_Alloc, _Tp> __data_;
  3073. public:
  3074. #ifndef _LIBCPP_HAS_NO_VARIADICS
  3075. _LIBCPP_INLINE_VISIBILITY
  3076. __shared_ptr_emplace(_Alloc __a)
  3077. : __data_(_VSTD::move(__a)) {}
  3078. template <class ..._Args>
  3079. _LIBCPP_INLINE_VISIBILITY
  3080. __shared_ptr_emplace(_Alloc __a, _Args&& ...__args)
  3081. : __data_(piecewise_construct, _VSTD::forward_as_tuple(__a),
  3082. _VSTD::forward_as_tuple(_VSTD::forward<_Args>(__args)...)) {}
  3083. #else // _LIBCPP_HAS_NO_VARIADICS
  3084. _LIBCPP_INLINE_VISIBILITY
  3085. __shared_ptr_emplace(_Alloc __a)
  3086. : __data_(__a) {}
  3087. template <class _A0>
  3088. _LIBCPP_INLINE_VISIBILITY
  3089. __shared_ptr_emplace(_Alloc __a, _A0& __a0)
  3090. : __data_(__a, _Tp(__a0)) {}
  3091. template <class _A0, class _A1>
  3092. _LIBCPP_INLINE_VISIBILITY
  3093. __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1)
  3094. : __data_(__a, _Tp(__a0, __a1)) {}
  3095. template <class _A0, class _A1, class _A2>
  3096. _LIBCPP_INLINE_VISIBILITY
  3097. __shared_ptr_emplace(_Alloc __a, _A0& __a0, _A1& __a1, _A2& __a2)
  3098. : __data_(__a, _Tp(__a0, __a1, __a2)) {}
  3099. #endif // _LIBCPP_HAS_NO_VARIADICS
  3100. private:
  3101. virtual void __on_zero_shared() _NOEXCEPT;
  3102. virtual void __on_zero_shared_weak() _NOEXCEPT;
  3103. public:
  3104. _LIBCPP_INLINE_VISIBILITY
  3105. _Tp* get() _NOEXCEPT {return _VSTD::addressof(__data_.second());}
  3106. };
  3107. template <class _Tp, class _Alloc>
  3108. void
  3109. __shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared() _NOEXCEPT
  3110. {
  3111. __data_.second().~_Tp();
  3112. }
  3113. template <class _Tp, class _Alloc>
  3114. void
  3115. __shared_ptr_emplace<_Tp, _Alloc>::__on_zero_shared_weak() _NOEXCEPT
  3116. {
  3117. typedef typename __allocator_traits_rebind<_Alloc, __shared_ptr_emplace>::type _Al;
  3118. typedef allocator_traits<_Al> _ATraits;
  3119. typedef pointer_traits<typename _ATraits::pointer> _PTraits;
  3120. _Al __a(__data_.first());
  3121. __data_.first().~_Alloc();
  3122. __a.deallocate(_PTraits::pointer_to(*this), 1);
  3123. }
  3124. struct __shared_ptr_dummy_rebind_allocator_type;
  3125. template <>
  3126. class _LIBCPP_TEMPLATE_VIS allocator<__shared_ptr_dummy_rebind_allocator_type>
  3127. {
  3128. public:
  3129. template <class _Other>
  3130. struct rebind
  3131. {
  3132. typedef allocator<_Other> other;
  3133. };
  3134. };
  3135. template<class _Tp> class _LIBCPP_TEMPLATE_VIS enable_shared_from_this;
  3136. template<class _Tp>
  3137. class _LIBCPP_TEMPLATE_VIS shared_ptr
  3138. {
  3139. public:
  3140. typedef _Tp element_type;
  3141. #if _LIBCPP_STD_VER > 14
  3142. typedef weak_ptr<_Tp> weak_type;
  3143. #endif
  3144. private:
  3145. element_type* __ptr_;
  3146. __shared_weak_count* __cntrl_;
  3147. struct __nat {int __for_bool_;};
  3148. public:
  3149. _LIBCPP_INLINE_VISIBILITY
  3150. _LIBCPP_CONSTEXPR shared_ptr() _NOEXCEPT;
  3151. _LIBCPP_INLINE_VISIBILITY
  3152. _LIBCPP_CONSTEXPR shared_ptr(nullptr_t) _NOEXCEPT;
  3153. template<class _Yp>
  3154. explicit shared_ptr(_Yp* __p,
  3155. typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
  3156. template<class _Yp, class _Dp>
  3157. shared_ptr(_Yp* __p, _Dp __d,
  3158. typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
  3159. template<class _Yp, class _Dp, class _Alloc>
  3160. shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
  3161. typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
  3162. template <class _Dp> shared_ptr(nullptr_t __p, _Dp __d);
  3163. template <class _Dp, class _Alloc> shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a);
  3164. template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(const shared_ptr<_Yp>& __r, element_type* __p) _NOEXCEPT;
  3165. _LIBCPP_INLINE_VISIBILITY
  3166. shared_ptr(const shared_ptr& __r) _NOEXCEPT;
  3167. template<class _Yp>
  3168. _LIBCPP_INLINE_VISIBILITY
  3169. shared_ptr(const shared_ptr<_Yp>& __r,
  3170. typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
  3171. _NOEXCEPT;
  3172. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3173. _LIBCPP_INLINE_VISIBILITY
  3174. shared_ptr(shared_ptr&& __r) _NOEXCEPT;
  3175. template<class _Yp> _LIBCPP_INLINE_VISIBILITY shared_ptr(shared_ptr<_Yp>&& __r,
  3176. typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat())
  3177. _NOEXCEPT;
  3178. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3179. template<class _Yp> explicit shared_ptr(const weak_ptr<_Yp>& __r,
  3180. typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type= __nat());
  3181. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  3182. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3183. template<class _Yp>
  3184. shared_ptr(auto_ptr<_Yp>&& __r,
  3185. typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
  3186. #else
  3187. template<class _Yp>
  3188. shared_ptr(auto_ptr<_Yp> __r,
  3189. typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type = __nat());
  3190. #endif
  3191. #endif
  3192. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3193. template <class _Yp, class _Dp>
  3194. shared_ptr(unique_ptr<_Yp, _Dp>&&,
  3195. typename enable_if
  3196. <
  3197. !is_lvalue_reference<_Dp>::value &&
  3198. !is_array<_Yp>::value &&
  3199. is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
  3200. __nat
  3201. >::type = __nat());
  3202. template <class _Yp, class _Dp>
  3203. shared_ptr(unique_ptr<_Yp, _Dp>&&,
  3204. typename enable_if
  3205. <
  3206. is_lvalue_reference<_Dp>::value &&
  3207. !is_array<_Yp>::value &&
  3208. is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
  3209. __nat
  3210. >::type = __nat());
  3211. #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3212. template <class _Yp, class _Dp>
  3213. shared_ptr(unique_ptr<_Yp, _Dp>,
  3214. typename enable_if
  3215. <
  3216. !is_lvalue_reference<_Dp>::value &&
  3217. !is_array<_Yp>::value &&
  3218. is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
  3219. __nat
  3220. >::type = __nat());
  3221. template <class _Yp, class _Dp>
  3222. shared_ptr(unique_ptr<_Yp, _Dp>,
  3223. typename enable_if
  3224. <
  3225. is_lvalue_reference<_Dp>::value &&
  3226. !is_array<_Yp>::value &&
  3227. is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
  3228. __nat
  3229. >::type = __nat());
  3230. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3231. ~shared_ptr();
  3232. _LIBCPP_INLINE_VISIBILITY
  3233. shared_ptr& operator=(const shared_ptr& __r) _NOEXCEPT;
  3234. template<class _Yp>
  3235. typename enable_if
  3236. <
  3237. is_convertible<_Yp*, element_type*>::value,
  3238. shared_ptr&
  3239. >::type
  3240. _LIBCPP_INLINE_VISIBILITY
  3241. operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT;
  3242. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3243. _LIBCPP_INLINE_VISIBILITY
  3244. shared_ptr& operator=(shared_ptr&& __r) _NOEXCEPT;
  3245. template<class _Yp>
  3246. typename enable_if
  3247. <
  3248. is_convertible<_Yp*, element_type*>::value,
  3249. shared_ptr<_Tp>&
  3250. >::type
  3251. _LIBCPP_INLINE_VISIBILITY
  3252. operator=(shared_ptr<_Yp>&& __r);
  3253. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  3254. template<class _Yp>
  3255. _LIBCPP_INLINE_VISIBILITY
  3256. typename enable_if
  3257. <
  3258. !is_array<_Yp>::value &&
  3259. is_convertible<_Yp*, element_type*>::value,
  3260. shared_ptr
  3261. >::type&
  3262. operator=(auto_ptr<_Yp>&& __r);
  3263. #endif
  3264. #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3265. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  3266. template<class _Yp>
  3267. _LIBCPP_INLINE_VISIBILITY
  3268. typename enable_if
  3269. <
  3270. !is_array<_Yp>::value &&
  3271. is_convertible<_Yp*, element_type*>::value,
  3272. shared_ptr&
  3273. >::type
  3274. operator=(auto_ptr<_Yp> __r);
  3275. #endif
  3276. #endif
  3277. template <class _Yp, class _Dp>
  3278. typename enable_if
  3279. <
  3280. !is_array<_Yp>::value &&
  3281. is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
  3282. shared_ptr&
  3283. >::type
  3284. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3285. _LIBCPP_INLINE_VISIBILITY
  3286. operator=(unique_ptr<_Yp, _Dp>&& __r);
  3287. #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3288. _LIBCPP_INLINE_VISIBILITY
  3289. operator=(unique_ptr<_Yp, _Dp> __r);
  3290. #endif
  3291. _LIBCPP_INLINE_VISIBILITY
  3292. void swap(shared_ptr& __r) _NOEXCEPT;
  3293. _LIBCPP_INLINE_VISIBILITY
  3294. void reset() _NOEXCEPT;
  3295. template<class _Yp>
  3296. typename enable_if
  3297. <
  3298. is_convertible<_Yp*, element_type*>::value,
  3299. void
  3300. >::type
  3301. _LIBCPP_INLINE_VISIBILITY
  3302. reset(_Yp* __p);
  3303. template<class _Yp, class _Dp>
  3304. typename enable_if
  3305. <
  3306. is_convertible<_Yp*, element_type*>::value,
  3307. void
  3308. >::type
  3309. _LIBCPP_INLINE_VISIBILITY
  3310. reset(_Yp* __p, _Dp __d);
  3311. template<class _Yp, class _Dp, class _Alloc>
  3312. typename enable_if
  3313. <
  3314. is_convertible<_Yp*, element_type*>::value,
  3315. void
  3316. >::type
  3317. _LIBCPP_INLINE_VISIBILITY
  3318. reset(_Yp* __p, _Dp __d, _Alloc __a);
  3319. _LIBCPP_INLINE_VISIBILITY
  3320. element_type* get() const _NOEXCEPT {return __ptr_;}
  3321. _LIBCPP_INLINE_VISIBILITY
  3322. typename add_lvalue_reference<element_type>::type operator*() const _NOEXCEPT
  3323. {return *__ptr_;}
  3324. _LIBCPP_INLINE_VISIBILITY
  3325. element_type* operator->() const _NOEXCEPT {return __ptr_;}
  3326. _LIBCPP_INLINE_VISIBILITY
  3327. long use_count() const _NOEXCEPT {return __cntrl_ ? __cntrl_->use_count() : 0;}
  3328. _LIBCPP_INLINE_VISIBILITY
  3329. bool unique() const _NOEXCEPT {return use_count() == 1;}
  3330. _LIBCPP_INLINE_VISIBILITY
  3331. _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT {return get() != 0;}
  3332. template <class _Up>
  3333. _LIBCPP_INLINE_VISIBILITY
  3334. bool owner_before(shared_ptr<_Up> const& __p) const _NOEXCEPT
  3335. {return __cntrl_ < __p.__cntrl_;}
  3336. template <class _Up>
  3337. _LIBCPP_INLINE_VISIBILITY
  3338. bool owner_before(weak_ptr<_Up> const& __p) const _NOEXCEPT
  3339. {return __cntrl_ < __p.__cntrl_;}
  3340. _LIBCPP_INLINE_VISIBILITY
  3341. bool
  3342. __owner_equivalent(const shared_ptr& __p) const
  3343. {return __cntrl_ == __p.__cntrl_;}
  3344. #ifndef _LIBCPP_NO_RTTI
  3345. template <class _Dp>
  3346. _LIBCPP_INLINE_VISIBILITY
  3347. _Dp* __get_deleter() const _NOEXCEPT
  3348. {return static_cast<_Dp*>(__cntrl_
  3349. ? const_cast<void *>(__cntrl_->__get_deleter(typeid(_Dp)))
  3350. : nullptr);}
  3351. #endif // _LIBCPP_NO_RTTI
  3352. template<class _Yp, class _CntrlBlk>
  3353. static shared_ptr<_Tp>
  3354. __create_with_control_block(_Yp* __p, _CntrlBlk* __cntrl)
  3355. {
  3356. shared_ptr<_Tp> __r;
  3357. __r.__ptr_ = __p;
  3358. __r.__cntrl_ = __cntrl;
  3359. __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
  3360. return __r;
  3361. }
  3362. template<class _Alloc, class ..._Args>
  3363. static
  3364. shared_ptr<_Tp>
  3365. allocate_shared(const _Alloc& __a, _Args&& ...__args);
  3366. private:
  3367. template <class _Yp, bool = is_function<_Yp>::value>
  3368. struct __shared_ptr_default_allocator
  3369. {
  3370. typedef allocator<_Yp> type;
  3371. };
  3372. template <class _Yp>
  3373. struct __shared_ptr_default_allocator<_Yp, true>
  3374. {
  3375. typedef allocator<__shared_ptr_dummy_rebind_allocator_type> type;
  3376. };
  3377. template <class _Yp, class _OrigPtr>
  3378. _LIBCPP_INLINE_VISIBILITY
  3379. typename enable_if<is_convertible<_OrigPtr*,
  3380. const enable_shared_from_this<_Yp>*
  3381. >::value,
  3382. void>::type
  3383. __enable_weak_this(const enable_shared_from_this<_Yp>* __e,
  3384. _OrigPtr* __ptr) _NOEXCEPT
  3385. {
  3386. typedef typename remove_cv<_Yp>::type _RawYp;
  3387. if (__e && __e->__weak_this_.expired())
  3388. {
  3389. __e->__weak_this_ = shared_ptr<_RawYp>(*this,
  3390. const_cast<_RawYp*>(static_cast<const _Yp*>(__ptr)));
  3391. }
  3392. }
  3393. _LIBCPP_INLINE_VISIBILITY void __enable_weak_this(...) _NOEXCEPT {}
  3394. template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
  3395. template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
  3396. };
  3397. template<class _Tp>
  3398. inline
  3399. _LIBCPP_CONSTEXPR
  3400. shared_ptr<_Tp>::shared_ptr() _NOEXCEPT
  3401. : __ptr_(0),
  3402. __cntrl_(0)
  3403. {
  3404. }
  3405. template<class _Tp>
  3406. inline
  3407. _LIBCPP_CONSTEXPR
  3408. shared_ptr<_Tp>::shared_ptr(nullptr_t) _NOEXCEPT
  3409. : __ptr_(0),
  3410. __cntrl_(0)
  3411. {
  3412. }
  3413. template<class _Tp>
  3414. template<class _Yp>
  3415. shared_ptr<_Tp>::shared_ptr(_Yp* __p,
  3416. typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
  3417. : __ptr_(__p)
  3418. {
  3419. unique_ptr<_Yp> __hold(__p);
  3420. typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
  3421. typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, _AllocT > _CntrlBlk;
  3422. __cntrl_ = new _CntrlBlk(__p, default_delete<_Yp>(), _AllocT());
  3423. __hold.release();
  3424. __enable_weak_this(__p, __p);
  3425. }
  3426. template<class _Tp>
  3427. template<class _Yp, class _Dp>
  3428. shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d,
  3429. typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
  3430. : __ptr_(__p)
  3431. {
  3432. #ifndef _LIBCPP_NO_EXCEPTIONS
  3433. try
  3434. {
  3435. #endif // _LIBCPP_NO_EXCEPTIONS
  3436. typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
  3437. typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
  3438. __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
  3439. __enable_weak_this(__p, __p);
  3440. #ifndef _LIBCPP_NO_EXCEPTIONS
  3441. }
  3442. catch (...)
  3443. {
  3444. __d(__p);
  3445. throw;
  3446. }
  3447. #endif // _LIBCPP_NO_EXCEPTIONS
  3448. }
  3449. template<class _Tp>
  3450. template<class _Dp>
  3451. shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d)
  3452. : __ptr_(0)
  3453. {
  3454. #ifndef _LIBCPP_NO_EXCEPTIONS
  3455. try
  3456. {
  3457. #endif // _LIBCPP_NO_EXCEPTIONS
  3458. typedef typename __shared_ptr_default_allocator<_Tp>::type _AllocT;
  3459. typedef __shared_ptr_pointer<nullptr_t, _Dp, _AllocT > _CntrlBlk;
  3460. __cntrl_ = new _CntrlBlk(__p, __d, _AllocT());
  3461. #ifndef _LIBCPP_NO_EXCEPTIONS
  3462. }
  3463. catch (...)
  3464. {
  3465. __d(__p);
  3466. throw;
  3467. }
  3468. #endif // _LIBCPP_NO_EXCEPTIONS
  3469. }
  3470. template<class _Tp>
  3471. template<class _Yp, class _Dp, class _Alloc>
  3472. shared_ptr<_Tp>::shared_ptr(_Yp* __p, _Dp __d, _Alloc __a,
  3473. typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
  3474. : __ptr_(__p)
  3475. {
  3476. #ifndef _LIBCPP_NO_EXCEPTIONS
  3477. try
  3478. {
  3479. #endif // _LIBCPP_NO_EXCEPTIONS
  3480. typedef __shared_ptr_pointer<_Yp*, _Dp, _Alloc> _CntrlBlk;
  3481. typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
  3482. typedef __allocator_destructor<_A2> _D2;
  3483. _A2 __a2(__a);
  3484. unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
  3485. ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
  3486. _CntrlBlk(__p, __d, __a);
  3487. __cntrl_ = _VSTD::addressof(*__hold2.release());
  3488. __enable_weak_this(__p, __p);
  3489. #ifndef _LIBCPP_NO_EXCEPTIONS
  3490. }
  3491. catch (...)
  3492. {
  3493. __d(__p);
  3494. throw;
  3495. }
  3496. #endif // _LIBCPP_NO_EXCEPTIONS
  3497. }
  3498. template<class _Tp>
  3499. template<class _Dp, class _Alloc>
  3500. shared_ptr<_Tp>::shared_ptr(nullptr_t __p, _Dp __d, _Alloc __a)
  3501. : __ptr_(0)
  3502. {
  3503. #ifndef _LIBCPP_NO_EXCEPTIONS
  3504. try
  3505. {
  3506. #endif // _LIBCPP_NO_EXCEPTIONS
  3507. typedef __shared_ptr_pointer<nullptr_t, _Dp, _Alloc> _CntrlBlk;
  3508. typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
  3509. typedef __allocator_destructor<_A2> _D2;
  3510. _A2 __a2(__a);
  3511. unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
  3512. ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
  3513. _CntrlBlk(__p, __d, __a);
  3514. __cntrl_ = _VSTD::addressof(*__hold2.release());
  3515. #ifndef _LIBCPP_NO_EXCEPTIONS
  3516. }
  3517. catch (...)
  3518. {
  3519. __d(__p);
  3520. throw;
  3521. }
  3522. #endif // _LIBCPP_NO_EXCEPTIONS
  3523. }
  3524. template<class _Tp>
  3525. template<class _Yp>
  3526. inline
  3527. shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r, element_type *__p) _NOEXCEPT
  3528. : __ptr_(__p),
  3529. __cntrl_(__r.__cntrl_)
  3530. {
  3531. if (__cntrl_)
  3532. __cntrl_->__add_shared();
  3533. }
  3534. template<class _Tp>
  3535. inline
  3536. shared_ptr<_Tp>::shared_ptr(const shared_ptr& __r) _NOEXCEPT
  3537. : __ptr_(__r.__ptr_),
  3538. __cntrl_(__r.__cntrl_)
  3539. {
  3540. if (__cntrl_)
  3541. __cntrl_->__add_shared();
  3542. }
  3543. template<class _Tp>
  3544. template<class _Yp>
  3545. inline
  3546. shared_ptr<_Tp>::shared_ptr(const shared_ptr<_Yp>& __r,
  3547. typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
  3548. _NOEXCEPT
  3549. : __ptr_(__r.__ptr_),
  3550. __cntrl_(__r.__cntrl_)
  3551. {
  3552. if (__cntrl_)
  3553. __cntrl_->__add_shared();
  3554. }
  3555. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3556. template<class _Tp>
  3557. inline
  3558. shared_ptr<_Tp>::shared_ptr(shared_ptr&& __r) _NOEXCEPT
  3559. : __ptr_(__r.__ptr_),
  3560. __cntrl_(__r.__cntrl_)
  3561. {
  3562. __r.__ptr_ = 0;
  3563. __r.__cntrl_ = 0;
  3564. }
  3565. template<class _Tp>
  3566. template<class _Yp>
  3567. inline
  3568. shared_ptr<_Tp>::shared_ptr(shared_ptr<_Yp>&& __r,
  3569. typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
  3570. _NOEXCEPT
  3571. : __ptr_(__r.__ptr_),
  3572. __cntrl_(__r.__cntrl_)
  3573. {
  3574. __r.__ptr_ = 0;
  3575. __r.__cntrl_ = 0;
  3576. }
  3577. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3578. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  3579. template<class _Tp>
  3580. template<class _Yp>
  3581. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3582. shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp>&& __r,
  3583. #else
  3584. shared_ptr<_Tp>::shared_ptr(auto_ptr<_Yp> __r,
  3585. #endif
  3586. typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
  3587. : __ptr_(__r.get())
  3588. {
  3589. typedef __shared_ptr_pointer<_Yp*, default_delete<_Yp>, allocator<_Yp> > _CntrlBlk;
  3590. __cntrl_ = new _CntrlBlk(__r.get(), default_delete<_Yp>(), allocator<_Yp>());
  3591. __enable_weak_this(__r.get(), __r.get());
  3592. __r.release();
  3593. }
  3594. #endif
  3595. template<class _Tp>
  3596. template <class _Yp, class _Dp>
  3597. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3598. shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
  3599. #else
  3600. shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
  3601. #endif
  3602. typename enable_if
  3603. <
  3604. !is_lvalue_reference<_Dp>::value &&
  3605. !is_array<_Yp>::value &&
  3606. is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
  3607. __nat
  3608. >::type)
  3609. : __ptr_(__r.get())
  3610. {
  3611. #if _LIBCPP_STD_VER > 11
  3612. if (__ptr_ == nullptr)
  3613. __cntrl_ = nullptr;
  3614. else
  3615. #endif
  3616. {
  3617. typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
  3618. typedef __shared_ptr_pointer<_Yp*, _Dp, _AllocT > _CntrlBlk;
  3619. __cntrl_ = new _CntrlBlk(__r.get(), __r.get_deleter(), _AllocT());
  3620. __enable_weak_this(__r.get(), __r.get());
  3621. }
  3622. __r.release();
  3623. }
  3624. template<class _Tp>
  3625. template <class _Yp, class _Dp>
  3626. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3627. shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp>&& __r,
  3628. #else
  3629. shared_ptr<_Tp>::shared_ptr(unique_ptr<_Yp, _Dp> __r,
  3630. #endif
  3631. typename enable_if
  3632. <
  3633. is_lvalue_reference<_Dp>::value &&
  3634. !is_array<_Yp>::value &&
  3635. is_convertible<typename unique_ptr<_Yp, _Dp>::pointer, element_type*>::value,
  3636. __nat
  3637. >::type)
  3638. : __ptr_(__r.get())
  3639. {
  3640. #if _LIBCPP_STD_VER > 11
  3641. if (__ptr_ == nullptr)
  3642. __cntrl_ = nullptr;
  3643. else
  3644. #endif
  3645. {
  3646. typedef typename __shared_ptr_default_allocator<_Yp>::type _AllocT;
  3647. typedef __shared_ptr_pointer<_Yp*,
  3648. reference_wrapper<typename remove_reference<_Dp>::type>,
  3649. _AllocT > _CntrlBlk;
  3650. __cntrl_ = new _CntrlBlk(__r.get(), ref(__r.get_deleter()), _AllocT());
  3651. __enable_weak_this(__r.get(), __r.get());
  3652. }
  3653. __r.release();
  3654. }
  3655. template<class _Tp>
  3656. template<class _Alloc, class ..._Args>
  3657. shared_ptr<_Tp>
  3658. shared_ptr<_Tp>::allocate_shared(const _Alloc& __a, _Args&& ...__args)
  3659. {
  3660. static_assert( is_constructible<_Tp, _Args...>::value, "Can't construct object in allocate_shared" );
  3661. typedef __shared_ptr_emplace<_Tp, _Alloc> _CntrlBlk;
  3662. typedef typename __allocator_traits_rebind<_Alloc, _CntrlBlk>::type _A2;
  3663. typedef __allocator_destructor<_A2> _D2;
  3664. _A2 __a2(__a);
  3665. unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
  3666. ::new(static_cast<void*>(_VSTD::addressof(*__hold2.get())))
  3667. _CntrlBlk(__a, _VSTD::forward<_Args>(__args)...);
  3668. shared_ptr<_Tp> __r;
  3669. __r.__ptr_ = __hold2.get()->get();
  3670. __r.__cntrl_ = _VSTD::addressof(*__hold2.release());
  3671. __r.__enable_weak_this(__r.__ptr_, __r.__ptr_);
  3672. return __r;
  3673. }
  3674. template<class _Tp>
  3675. shared_ptr<_Tp>::~shared_ptr()
  3676. {
  3677. if (__cntrl_)
  3678. __cntrl_->__release_shared();
  3679. }
  3680. template<class _Tp>
  3681. inline
  3682. shared_ptr<_Tp>&
  3683. shared_ptr<_Tp>::operator=(const shared_ptr& __r) _NOEXCEPT
  3684. {
  3685. shared_ptr(__r).swap(*this);
  3686. return *this;
  3687. }
  3688. template<class _Tp>
  3689. template<class _Yp>
  3690. inline
  3691. typename enable_if
  3692. <
  3693. is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
  3694. shared_ptr<_Tp>&
  3695. >::type
  3696. shared_ptr<_Tp>::operator=(const shared_ptr<_Yp>& __r) _NOEXCEPT
  3697. {
  3698. shared_ptr(__r).swap(*this);
  3699. return *this;
  3700. }
  3701. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3702. template<class _Tp>
  3703. inline
  3704. shared_ptr<_Tp>&
  3705. shared_ptr<_Tp>::operator=(shared_ptr&& __r) _NOEXCEPT
  3706. {
  3707. shared_ptr(_VSTD::move(__r)).swap(*this);
  3708. return *this;
  3709. }
  3710. template<class _Tp>
  3711. template<class _Yp>
  3712. inline
  3713. typename enable_if
  3714. <
  3715. is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
  3716. shared_ptr<_Tp>&
  3717. >::type
  3718. shared_ptr<_Tp>::operator=(shared_ptr<_Yp>&& __r)
  3719. {
  3720. shared_ptr(_VSTD::move(__r)).swap(*this);
  3721. return *this;
  3722. }
  3723. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  3724. template<class _Tp>
  3725. template<class _Yp>
  3726. inline
  3727. typename enable_if
  3728. <
  3729. !is_array<_Yp>::value &&
  3730. is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
  3731. shared_ptr<_Tp>
  3732. >::type&
  3733. shared_ptr<_Tp>::operator=(auto_ptr<_Yp>&& __r)
  3734. {
  3735. shared_ptr(_VSTD::move(__r)).swap(*this);
  3736. return *this;
  3737. }
  3738. #endif
  3739. template<class _Tp>
  3740. template <class _Yp, class _Dp>
  3741. inline
  3742. typename enable_if
  3743. <
  3744. !is_array<_Yp>::value &&
  3745. is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
  3746. typename shared_ptr<_Tp>::element_type*>::value,
  3747. shared_ptr<_Tp>&
  3748. >::type
  3749. shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp>&& __r)
  3750. {
  3751. shared_ptr(_VSTD::move(__r)).swap(*this);
  3752. return *this;
  3753. }
  3754. #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3755. #if _LIBCPP_STD_VER <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
  3756. template<class _Tp>
  3757. template<class _Yp>
  3758. inline _LIBCPP_INLINE_VISIBILITY
  3759. typename enable_if
  3760. <
  3761. !is_array<_Yp>::value &&
  3762. is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
  3763. shared_ptr<_Tp>&
  3764. >::type
  3765. shared_ptr<_Tp>::operator=(auto_ptr<_Yp> __r)
  3766. {
  3767. shared_ptr(__r).swap(*this);
  3768. return *this;
  3769. }
  3770. #endif
  3771. template<class _Tp>
  3772. template <class _Yp, class _Dp>
  3773. inline _LIBCPP_INLINE_VISIBILITY
  3774. typename enable_if
  3775. <
  3776. !is_array<_Yp>::value &&
  3777. is_convertible<typename unique_ptr<_Yp, _Dp>::pointer,
  3778. typename shared_ptr<_Tp>::element_type*>::value,
  3779. shared_ptr<_Tp>&
  3780. >::type
  3781. shared_ptr<_Tp>::operator=(unique_ptr<_Yp, _Dp> __r)
  3782. {
  3783. shared_ptr(_VSTD::move(__r)).swap(*this);
  3784. return *this;
  3785. }
  3786. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  3787. template<class _Tp>
  3788. inline
  3789. void
  3790. shared_ptr<_Tp>::swap(shared_ptr& __r) _NOEXCEPT
  3791. {
  3792. _VSTD::swap(__ptr_, __r.__ptr_);
  3793. _VSTD::swap(__cntrl_, __r.__cntrl_);
  3794. }
  3795. template<class _Tp>
  3796. inline
  3797. void
  3798. shared_ptr<_Tp>::reset() _NOEXCEPT
  3799. {
  3800. shared_ptr().swap(*this);
  3801. }
  3802. template<class _Tp>
  3803. template<class _Yp>
  3804. inline
  3805. typename enable_if
  3806. <
  3807. is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
  3808. void
  3809. >::type
  3810. shared_ptr<_Tp>::reset(_Yp* __p)
  3811. {
  3812. shared_ptr(__p).swap(*this);
  3813. }
  3814. template<class _Tp>
  3815. template<class _Yp, class _Dp>
  3816. inline
  3817. typename enable_if
  3818. <
  3819. is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
  3820. void
  3821. >::type
  3822. shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d)
  3823. {
  3824. shared_ptr(__p, __d).swap(*this);
  3825. }
  3826. template<class _Tp>
  3827. template<class _Yp, class _Dp, class _Alloc>
  3828. inline
  3829. typename enable_if
  3830. <
  3831. is_convertible<_Yp*, typename shared_ptr<_Tp>::element_type*>::value,
  3832. void
  3833. >::type
  3834. shared_ptr<_Tp>::reset(_Yp* __p, _Dp __d, _Alloc __a)
  3835. {
  3836. shared_ptr(__p, __d, __a).swap(*this);
  3837. }
  3838. template<class _Tp, class ..._Args>
  3839. inline _LIBCPP_INLINE_VISIBILITY
  3840. typename enable_if
  3841. <
  3842. !is_array<_Tp>::value,
  3843. shared_ptr<_Tp>
  3844. >::type
  3845. make_shared(_Args&& ...__args)
  3846. {
  3847. static_assert(is_constructible<_Tp, _Args...>::value, "Can't construct object in make_shared");
  3848. typedef __shared_ptr_emplace<_Tp, allocator<_Tp> > _CntrlBlk;
  3849. typedef allocator<_CntrlBlk> _A2;
  3850. typedef __allocator_destructor<_A2> _D2;
  3851. _A2 __a2;
  3852. unique_ptr<_CntrlBlk, _D2> __hold2(__a2.allocate(1), _D2(__a2, 1));
  3853. ::new(__hold2.get()) _CntrlBlk(__a2, _VSTD::forward<_Args>(__args)...);
  3854. _Tp *__ptr = __hold2.get()->get();
  3855. return shared_ptr<_Tp>::__create_with_control_block(__ptr, __hold2.release());
  3856. }
  3857. template<class _Tp, class _Alloc, class ..._Args>
  3858. inline _LIBCPP_INLINE_VISIBILITY
  3859. typename enable_if
  3860. <
  3861. !is_array<_Tp>::value,
  3862. shared_ptr<_Tp>
  3863. >::type
  3864. allocate_shared(const _Alloc& __a, _Args&& ...__args)
  3865. {
  3866. return shared_ptr<_Tp>::allocate_shared(__a, _VSTD::forward<_Args>(__args)...);
  3867. }
  3868. template<class _Tp, class _Up>
  3869. inline _LIBCPP_INLINE_VISIBILITY
  3870. bool
  3871. operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
  3872. {
  3873. return __x.get() == __y.get();
  3874. }
  3875. template<class _Tp, class _Up>
  3876. inline _LIBCPP_INLINE_VISIBILITY
  3877. bool
  3878. operator!=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
  3879. {
  3880. return !(__x == __y);
  3881. }
  3882. template<class _Tp, class _Up>
  3883. inline _LIBCPP_INLINE_VISIBILITY
  3884. bool
  3885. operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
  3886. {
  3887. #if _LIBCPP_STD_VER <= 11
  3888. typedef typename common_type<_Tp*, _Up*>::type _Vp;
  3889. return less<_Vp>()(__x.get(), __y.get());
  3890. #else
  3891. return less<>()(__x.get(), __y.get());
  3892. #endif
  3893. }
  3894. template<class _Tp, class _Up>
  3895. inline _LIBCPP_INLINE_VISIBILITY
  3896. bool
  3897. operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
  3898. {
  3899. return __y < __x;
  3900. }
  3901. template<class _Tp, class _Up>
  3902. inline _LIBCPP_INLINE_VISIBILITY
  3903. bool
  3904. operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
  3905. {
  3906. return !(__y < __x);
  3907. }
  3908. template<class _Tp, class _Up>
  3909. inline _LIBCPP_INLINE_VISIBILITY
  3910. bool
  3911. operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
  3912. {
  3913. return !(__x < __y);
  3914. }
  3915. template<class _Tp>
  3916. inline _LIBCPP_INLINE_VISIBILITY
  3917. bool
  3918. operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
  3919. {
  3920. return !__x;
  3921. }
  3922. template<class _Tp>
  3923. inline _LIBCPP_INLINE_VISIBILITY
  3924. bool
  3925. operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
  3926. {
  3927. return !__x;
  3928. }
  3929. template<class _Tp>
  3930. inline _LIBCPP_INLINE_VISIBILITY
  3931. bool
  3932. operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
  3933. {
  3934. return static_cast<bool>(__x);
  3935. }
  3936. template<class _Tp>
  3937. inline _LIBCPP_INLINE_VISIBILITY
  3938. bool
  3939. operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
  3940. {
  3941. return static_cast<bool>(__x);
  3942. }
  3943. template<class _Tp>
  3944. inline _LIBCPP_INLINE_VISIBILITY
  3945. bool
  3946. operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
  3947. {
  3948. return less<_Tp*>()(__x.get(), nullptr);
  3949. }
  3950. template<class _Tp>
  3951. inline _LIBCPP_INLINE_VISIBILITY
  3952. bool
  3953. operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
  3954. {
  3955. return less<_Tp*>()(nullptr, __x.get());
  3956. }
  3957. template<class _Tp>
  3958. inline _LIBCPP_INLINE_VISIBILITY
  3959. bool
  3960. operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
  3961. {
  3962. return nullptr < __x;
  3963. }
  3964. template<class _Tp>
  3965. inline _LIBCPP_INLINE_VISIBILITY
  3966. bool
  3967. operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
  3968. {
  3969. return __x < nullptr;
  3970. }
  3971. template<class _Tp>
  3972. inline _LIBCPP_INLINE_VISIBILITY
  3973. bool
  3974. operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
  3975. {
  3976. return !(nullptr < __x);
  3977. }
  3978. template<class _Tp>
  3979. inline _LIBCPP_INLINE_VISIBILITY
  3980. bool
  3981. operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
  3982. {
  3983. return !(__x < nullptr);
  3984. }
  3985. template<class _Tp>
  3986. inline _LIBCPP_INLINE_VISIBILITY
  3987. bool
  3988. operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
  3989. {
  3990. return !(__x < nullptr);
  3991. }
  3992. template<class _Tp>
  3993. inline _LIBCPP_INLINE_VISIBILITY
  3994. bool
  3995. operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
  3996. {
  3997. return !(nullptr < __x);
  3998. }
  3999. template<class _Tp>
  4000. inline _LIBCPP_INLINE_VISIBILITY
  4001. void
  4002. swap(shared_ptr<_Tp>& __x, shared_ptr<_Tp>& __y) _NOEXCEPT
  4003. {
  4004. __x.swap(__y);
  4005. }
  4006. template<class _Tp, class _Up>
  4007. inline _LIBCPP_INLINE_VISIBILITY
  4008. typename enable_if
  4009. <
  4010. !is_array<_Tp>::value && !is_array<_Up>::value,
  4011. shared_ptr<_Tp>
  4012. >::type
  4013. static_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
  4014. {
  4015. return shared_ptr<_Tp>(__r, static_cast<_Tp*>(__r.get()));
  4016. }
  4017. template<class _Tp, class _Up>
  4018. inline _LIBCPP_INLINE_VISIBILITY
  4019. typename enable_if
  4020. <
  4021. !is_array<_Tp>::value && !is_array<_Up>::value,
  4022. shared_ptr<_Tp>
  4023. >::type
  4024. dynamic_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
  4025. {
  4026. _Tp* __p = dynamic_cast<_Tp*>(__r.get());
  4027. return __p ? shared_ptr<_Tp>(__r, __p) : shared_ptr<_Tp>();
  4028. }
  4029. template<class _Tp, class _Up>
  4030. typename enable_if
  4031. <
  4032. is_array<_Tp>::value == is_array<_Up>::value,
  4033. shared_ptr<_Tp>
  4034. >::type
  4035. const_pointer_cast(const shared_ptr<_Up>& __r) _NOEXCEPT
  4036. {
  4037. typedef typename remove_extent<_Tp>::type _RTp;
  4038. return shared_ptr<_Tp>(__r, const_cast<_RTp*>(__r.get()));
  4039. }
  4040. #ifndef _LIBCPP_NO_RTTI
  4041. template<class _Dp, class _Tp>
  4042. inline _LIBCPP_INLINE_VISIBILITY
  4043. _Dp*
  4044. get_deleter(const shared_ptr<_Tp>& __p) _NOEXCEPT
  4045. {
  4046. return __p.template __get_deleter<_Dp>();
  4047. }
  4048. #endif // _LIBCPP_NO_RTTI
  4049. template<class _Tp>
  4050. class _LIBCPP_TEMPLATE_VIS weak_ptr
  4051. {
  4052. public:
  4053. typedef _Tp element_type;
  4054. private:
  4055. element_type* __ptr_;
  4056. __shared_weak_count* __cntrl_;
  4057. public:
  4058. _LIBCPP_INLINE_VISIBILITY
  4059. _LIBCPP_CONSTEXPR weak_ptr() _NOEXCEPT;
  4060. template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(shared_ptr<_Yp> const& __r,
  4061. typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
  4062. _NOEXCEPT;
  4063. _LIBCPP_INLINE_VISIBILITY
  4064. weak_ptr(weak_ptr const& __r) _NOEXCEPT;
  4065. template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp> const& __r,
  4066. typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
  4067. _NOEXCEPT;
  4068. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  4069. _LIBCPP_INLINE_VISIBILITY
  4070. weak_ptr(weak_ptr&& __r) _NOEXCEPT;
  4071. template<class _Yp> _LIBCPP_INLINE_VISIBILITY weak_ptr(weak_ptr<_Yp>&& __r,
  4072. typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type = 0)
  4073. _NOEXCEPT;
  4074. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  4075. ~weak_ptr();
  4076. _LIBCPP_INLINE_VISIBILITY
  4077. weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
  4078. template<class _Yp>
  4079. typename enable_if
  4080. <
  4081. is_convertible<_Yp*, element_type*>::value,
  4082. weak_ptr&
  4083. >::type
  4084. _LIBCPP_INLINE_VISIBILITY
  4085. operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
  4086. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  4087. _LIBCPP_INLINE_VISIBILITY
  4088. weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
  4089. template<class _Yp>
  4090. typename enable_if
  4091. <
  4092. is_convertible<_Yp*, element_type*>::value,
  4093. weak_ptr&
  4094. >::type
  4095. _LIBCPP_INLINE_VISIBILITY
  4096. operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
  4097. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  4098. template<class _Yp>
  4099. typename enable_if
  4100. <
  4101. is_convertible<_Yp*, element_type*>::value,
  4102. weak_ptr&
  4103. >::type
  4104. _LIBCPP_INLINE_VISIBILITY
  4105. operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT;
  4106. _LIBCPP_INLINE_VISIBILITY
  4107. void swap(weak_ptr& __r) _NOEXCEPT;
  4108. _LIBCPP_INLINE_VISIBILITY
  4109. void reset() _NOEXCEPT;
  4110. _LIBCPP_INLINE_VISIBILITY
  4111. long use_count() const _NOEXCEPT
  4112. {return __cntrl_ ? __cntrl_->use_count() : 0;}
  4113. _LIBCPP_INLINE_VISIBILITY
  4114. bool expired() const _NOEXCEPT
  4115. {return __cntrl_ == 0 || __cntrl_->use_count() == 0;}
  4116. shared_ptr<_Tp> lock() const _NOEXCEPT;
  4117. template<class _Up>
  4118. _LIBCPP_INLINE_VISIBILITY
  4119. bool owner_before(const shared_ptr<_Up>& __r) const _NOEXCEPT
  4120. {return __cntrl_ < __r.__cntrl_;}
  4121. template<class _Up>
  4122. _LIBCPP_INLINE_VISIBILITY
  4123. bool owner_before(const weak_ptr<_Up>& __r) const _NOEXCEPT
  4124. {return __cntrl_ < __r.__cntrl_;}
  4125. template <class _Up> friend class _LIBCPP_TEMPLATE_VIS weak_ptr;
  4126. template <class _Up> friend class _LIBCPP_TEMPLATE_VIS shared_ptr;
  4127. };
  4128. template<class _Tp>
  4129. inline
  4130. _LIBCPP_CONSTEXPR
  4131. weak_ptr<_Tp>::weak_ptr() _NOEXCEPT
  4132. : __ptr_(0),
  4133. __cntrl_(0)
  4134. {
  4135. }
  4136. template<class _Tp>
  4137. inline
  4138. weak_ptr<_Tp>::weak_ptr(weak_ptr const& __r) _NOEXCEPT
  4139. : __ptr_(__r.__ptr_),
  4140. __cntrl_(__r.__cntrl_)
  4141. {
  4142. if (__cntrl_)
  4143. __cntrl_->__add_weak();
  4144. }
  4145. template<class _Tp>
  4146. template<class _Yp>
  4147. inline
  4148. weak_ptr<_Tp>::weak_ptr(shared_ptr<_Yp> const& __r,
  4149. typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
  4150. _NOEXCEPT
  4151. : __ptr_(__r.__ptr_),
  4152. __cntrl_(__r.__cntrl_)
  4153. {
  4154. if (__cntrl_)
  4155. __cntrl_->__add_weak();
  4156. }
  4157. template<class _Tp>
  4158. template<class _Yp>
  4159. inline
  4160. weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp> const& __r,
  4161. typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
  4162. _NOEXCEPT
  4163. : __ptr_(__r.__ptr_),
  4164. __cntrl_(__r.__cntrl_)
  4165. {
  4166. if (__cntrl_)
  4167. __cntrl_->__add_weak();
  4168. }
  4169. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  4170. template<class _Tp>
  4171. inline
  4172. weak_ptr<_Tp>::weak_ptr(weak_ptr&& __r) _NOEXCEPT
  4173. : __ptr_(__r.__ptr_),
  4174. __cntrl_(__r.__cntrl_)
  4175. {
  4176. __r.__ptr_ = 0;
  4177. __r.__cntrl_ = 0;
  4178. }
  4179. template<class _Tp>
  4180. template<class _Yp>
  4181. inline
  4182. weak_ptr<_Tp>::weak_ptr(weak_ptr<_Yp>&& __r,
  4183. typename enable_if<is_convertible<_Yp*, _Tp*>::value, __nat*>::type)
  4184. _NOEXCEPT
  4185. : __ptr_(__r.__ptr_),
  4186. __cntrl_(__r.__cntrl_)
  4187. {
  4188. __r.__ptr_ = 0;
  4189. __r.__cntrl_ = 0;
  4190. }
  4191. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  4192. template<class _Tp>
  4193. weak_ptr<_Tp>::~weak_ptr()
  4194. {
  4195. if (__cntrl_)
  4196. __cntrl_->__release_weak();
  4197. }
  4198. template<class _Tp>
  4199. inline
  4200. weak_ptr<_Tp>&
  4201. weak_ptr<_Tp>::operator=(weak_ptr const& __r) _NOEXCEPT
  4202. {
  4203. weak_ptr(__r).swap(*this);
  4204. return *this;
  4205. }
  4206. template<class _Tp>
  4207. template<class _Yp>
  4208. inline
  4209. typename enable_if
  4210. <
  4211. is_convertible<_Yp*, _Tp*>::value,
  4212. weak_ptr<_Tp>&
  4213. >::type
  4214. weak_ptr<_Tp>::operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT
  4215. {
  4216. weak_ptr(__r).swap(*this);
  4217. return *this;
  4218. }
  4219. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  4220. template<class _Tp>
  4221. inline
  4222. weak_ptr<_Tp>&
  4223. weak_ptr<_Tp>::operator=(weak_ptr&& __r) _NOEXCEPT
  4224. {
  4225. weak_ptr(_VSTD::move(__r)).swap(*this);
  4226. return *this;
  4227. }
  4228. template<class _Tp>
  4229. template<class _Yp>
  4230. inline
  4231. typename enable_if
  4232. <
  4233. is_convertible<_Yp*, _Tp*>::value,
  4234. weak_ptr<_Tp>&
  4235. >::type
  4236. weak_ptr<_Tp>::operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT
  4237. {
  4238. weak_ptr(_VSTD::move(__r)).swap(*this);
  4239. return *this;
  4240. }
  4241. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  4242. template<class _Tp>
  4243. template<class _Yp>
  4244. inline
  4245. typename enable_if
  4246. <
  4247. is_convertible<_Yp*, _Tp*>::value,
  4248. weak_ptr<_Tp>&
  4249. >::type
  4250. weak_ptr<_Tp>::operator=(shared_ptr<_Yp> const& __r) _NOEXCEPT
  4251. {
  4252. weak_ptr(__r).swap(*this);
  4253. return *this;
  4254. }
  4255. template<class _Tp>
  4256. inline
  4257. void
  4258. weak_ptr<_Tp>::swap(weak_ptr& __r) _NOEXCEPT
  4259. {
  4260. _VSTD::swap(__ptr_, __r.__ptr_);
  4261. _VSTD::swap(__cntrl_, __r.__cntrl_);
  4262. }
  4263. template<class _Tp>
  4264. inline _LIBCPP_INLINE_VISIBILITY
  4265. void
  4266. swap(weak_ptr<_Tp>& __x, weak_ptr<_Tp>& __y) _NOEXCEPT
  4267. {
  4268. __x.swap(__y);
  4269. }
  4270. template<class _Tp>
  4271. inline
  4272. void
  4273. weak_ptr<_Tp>::reset() _NOEXCEPT
  4274. {
  4275. weak_ptr().swap(*this);
  4276. }
  4277. template<class _Tp>
  4278. template<class _Yp>
  4279. shared_ptr<_Tp>::shared_ptr(const weak_ptr<_Yp>& __r,
  4280. typename enable_if<is_convertible<_Yp*, element_type*>::value, __nat>::type)
  4281. : __ptr_(__r.__ptr_),
  4282. __cntrl_(__r.__cntrl_ ? __r.__cntrl_->lock() : __r.__cntrl_)
  4283. {
  4284. if (__cntrl_ == 0)
  4285. __throw_bad_weak_ptr();
  4286. }
  4287. template<class _Tp>
  4288. shared_ptr<_Tp>
  4289. weak_ptr<_Tp>::lock() const _NOEXCEPT
  4290. {
  4291. shared_ptr<_Tp> __r;
  4292. __r.__cntrl_ = __cntrl_ ? __cntrl_->lock() : __cntrl_;
  4293. if (__r.__cntrl_)
  4294. __r.__ptr_ = __ptr_;
  4295. return __r;
  4296. }
  4297. #if _LIBCPP_STD_VER > 14
  4298. template <class _Tp = void> struct owner_less;
  4299. #else
  4300. template <class _Tp> struct owner_less;
  4301. #endif
  4302. template <class _Tp>
  4303. struct _LIBCPP_TEMPLATE_VIS owner_less<shared_ptr<_Tp> >
  4304. : binary_function<shared_ptr<_Tp>, shared_ptr<_Tp>, bool>
  4305. {
  4306. typedef bool result_type;
  4307. _LIBCPP_INLINE_VISIBILITY
  4308. bool operator()(shared_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
  4309. {return __x.owner_before(__y);}
  4310. _LIBCPP_INLINE_VISIBILITY
  4311. bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
  4312. {return __x.owner_before(__y);}
  4313. _LIBCPP_INLINE_VISIBILITY
  4314. bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
  4315. {return __x.owner_before(__y);}
  4316. };
  4317. template <class _Tp>
  4318. struct _LIBCPP_TEMPLATE_VIS owner_less<weak_ptr<_Tp> >
  4319. : binary_function<weak_ptr<_Tp>, weak_ptr<_Tp>, bool>
  4320. {
  4321. typedef bool result_type;
  4322. _LIBCPP_INLINE_VISIBILITY
  4323. bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
  4324. {return __x.owner_before(__y);}
  4325. _LIBCPP_INLINE_VISIBILITY
  4326. bool operator()(shared_ptr<_Tp> const& __x, weak_ptr<_Tp> const& __y) const _NOEXCEPT
  4327. {return __x.owner_before(__y);}
  4328. _LIBCPP_INLINE_VISIBILITY
  4329. bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Tp> const& __y) const _NOEXCEPT
  4330. {return __x.owner_before(__y);}
  4331. };
  4332. #if _LIBCPP_STD_VER > 14
  4333. template <>
  4334. struct _LIBCPP_TEMPLATE_VIS owner_less<void>
  4335. {
  4336. template <class _Tp, class _Up>
  4337. _LIBCPP_INLINE_VISIBILITY
  4338. bool operator()( shared_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
  4339. {return __x.owner_before(__y);}
  4340. template <class _Tp, class _Up>
  4341. _LIBCPP_INLINE_VISIBILITY
  4342. bool operator()( shared_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
  4343. {return __x.owner_before(__y);}
  4344. template <class _Tp, class _Up>
  4345. _LIBCPP_INLINE_VISIBILITY
  4346. bool operator()( weak_ptr<_Tp> const& __x, shared_ptr<_Up> const& __y) const _NOEXCEPT
  4347. {return __x.owner_before(__y);}
  4348. template <class _Tp, class _Up>
  4349. _LIBCPP_INLINE_VISIBILITY
  4350. bool operator()( weak_ptr<_Tp> const& __x, weak_ptr<_Up> const& __y) const _NOEXCEPT
  4351. {return __x.owner_before(__y);}
  4352. typedef void is_transparent;
  4353. };
  4354. #endif
  4355. template<class _Tp>
  4356. class _LIBCPP_TEMPLATE_VIS enable_shared_from_this
  4357. {
  4358. mutable weak_ptr<_Tp> __weak_this_;
  4359. protected:
  4360. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
  4361. enable_shared_from_this() _NOEXCEPT {}
  4362. _LIBCPP_INLINE_VISIBILITY
  4363. enable_shared_from_this(enable_shared_from_this const&) _NOEXCEPT {}
  4364. _LIBCPP_INLINE_VISIBILITY
  4365. enable_shared_from_this& operator=(enable_shared_from_this const&) _NOEXCEPT
  4366. {return *this;}
  4367. _LIBCPP_INLINE_VISIBILITY
  4368. ~enable_shared_from_this() {}
  4369. public:
  4370. _LIBCPP_INLINE_VISIBILITY
  4371. shared_ptr<_Tp> shared_from_this()
  4372. {return shared_ptr<_Tp>(__weak_this_);}
  4373. _LIBCPP_INLINE_VISIBILITY
  4374. shared_ptr<_Tp const> shared_from_this() const
  4375. {return shared_ptr<const _Tp>(__weak_this_);}
  4376. #if _LIBCPP_STD_VER > 14
  4377. _LIBCPP_INLINE_VISIBILITY
  4378. weak_ptr<_Tp> weak_from_this() _NOEXCEPT
  4379. { return __weak_this_; }
  4380. _LIBCPP_INLINE_VISIBILITY
  4381. weak_ptr<const _Tp> weak_from_this() const _NOEXCEPT
  4382. { return __weak_this_; }
  4383. #endif // _LIBCPP_STD_VER > 14
  4384. template <class _Up> friend class shared_ptr;
  4385. };
  4386. template <class _Tp>
  4387. struct _LIBCPP_TEMPLATE_VIS hash<shared_ptr<_Tp> >
  4388. {
  4389. typedef shared_ptr<_Tp> argument_type;
  4390. typedef size_t result_type;
  4391. _LIBCPP_INLINE_VISIBILITY
  4392. result_type operator()(const argument_type& __ptr) const _NOEXCEPT
  4393. {
  4394. return hash<_Tp*>()(__ptr.get());
  4395. }
  4396. };
  4397. template<class _CharT, class _Traits, class _Yp>
  4398. inline _LIBCPP_INLINE_VISIBILITY
  4399. basic_ostream<_CharT, _Traits>&
  4400. operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p);
  4401. #if !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
  4402. class _LIBCPP_TYPE_VIS __sp_mut
  4403. {
  4404. void* __lx;
  4405. public:
  4406. void lock() _NOEXCEPT;
  4407. void unlock() _NOEXCEPT;
  4408. private:
  4409. _LIBCPP_CONSTEXPR __sp_mut(void*) _NOEXCEPT;
  4410. __sp_mut(const __sp_mut&);
  4411. __sp_mut& operator=(const __sp_mut&);
  4412. friend _LIBCPP_FUNC_VIS __sp_mut& __get_sp_mut(const void*);
  4413. };
  4414. _LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  4415. __sp_mut& __get_sp_mut(const void*);
  4416. template <class _Tp>
  4417. inline _LIBCPP_INLINE_VISIBILITY
  4418. bool
  4419. atomic_is_lock_free(const shared_ptr<_Tp>*)
  4420. {
  4421. return false;
  4422. }
  4423. template <class _Tp>
  4424. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  4425. shared_ptr<_Tp>
  4426. atomic_load(const shared_ptr<_Tp>* __p)
  4427. {
  4428. __sp_mut& __m = __get_sp_mut(__p);
  4429. __m.lock();
  4430. shared_ptr<_Tp> __q = *__p;
  4431. __m.unlock();
  4432. return __q;
  4433. }
  4434. template <class _Tp>
  4435. inline _LIBCPP_INLINE_VISIBILITY
  4436. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  4437. shared_ptr<_Tp>
  4438. atomic_load_explicit(const shared_ptr<_Tp>* __p, memory_order)
  4439. {
  4440. return atomic_load(__p);
  4441. }
  4442. template <class _Tp>
  4443. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  4444. void
  4445. atomic_store(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
  4446. {
  4447. __sp_mut& __m = __get_sp_mut(__p);
  4448. __m.lock();
  4449. __p->swap(__r);
  4450. __m.unlock();
  4451. }
  4452. template <class _Tp>
  4453. inline _LIBCPP_INLINE_VISIBILITY
  4454. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  4455. void
  4456. atomic_store_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
  4457. {
  4458. atomic_store(__p, __r);
  4459. }
  4460. template <class _Tp>
  4461. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  4462. shared_ptr<_Tp>
  4463. atomic_exchange(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r)
  4464. {
  4465. __sp_mut& __m = __get_sp_mut(__p);
  4466. __m.lock();
  4467. __p->swap(__r);
  4468. __m.unlock();
  4469. return __r;
  4470. }
  4471. template <class _Tp>
  4472. inline _LIBCPP_INLINE_VISIBILITY
  4473. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  4474. shared_ptr<_Tp>
  4475. atomic_exchange_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp> __r, memory_order)
  4476. {
  4477. return atomic_exchange(__p, __r);
  4478. }
  4479. template <class _Tp>
  4480. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  4481. bool
  4482. atomic_compare_exchange_strong(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
  4483. {
  4484. shared_ptr<_Tp> __temp;
  4485. __sp_mut& __m = __get_sp_mut(__p);
  4486. __m.lock();
  4487. if (__p->__owner_equivalent(*__v))
  4488. {
  4489. _VSTD::swap(__temp, *__p);
  4490. *__p = __w;
  4491. __m.unlock();
  4492. return true;
  4493. }
  4494. _VSTD::swap(__temp, *__v);
  4495. *__v = *__p;
  4496. __m.unlock();
  4497. return false;
  4498. }
  4499. template <class _Tp>
  4500. inline _LIBCPP_INLINE_VISIBILITY
  4501. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  4502. bool
  4503. atomic_compare_exchange_weak(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v, shared_ptr<_Tp> __w)
  4504. {
  4505. return atomic_compare_exchange_strong(__p, __v, __w);
  4506. }
  4507. template <class _Tp>
  4508. inline _LIBCPP_INLINE_VISIBILITY
  4509. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  4510. bool
  4511. atomic_compare_exchange_strong_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
  4512. shared_ptr<_Tp> __w, memory_order, memory_order)
  4513. {
  4514. return atomic_compare_exchange_strong(__p, __v, __w);
  4515. }
  4516. template <class _Tp>
  4517. inline _LIBCPP_INLINE_VISIBILITY
  4518. _LIBCPP_AVAILABILITY_ATOMIC_SHARED_PTR
  4519. bool
  4520. atomic_compare_exchange_weak_explicit(shared_ptr<_Tp>* __p, shared_ptr<_Tp>* __v,
  4521. shared_ptr<_Tp> __w, memory_order, memory_order)
  4522. {
  4523. return atomic_compare_exchange_weak(__p, __v, __w);
  4524. }
  4525. #endif // !defined(_LIBCPP_HAS_NO_ATOMIC_HEADER)
  4526. //enum class
  4527. #if defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE)
  4528. # ifndef _LIBCPP_CXX03_LANG
  4529. enum class pointer_safety : unsigned char {
  4530. relaxed,
  4531. preferred,
  4532. strict
  4533. };
  4534. # endif
  4535. #else
  4536. struct _LIBCPP_TYPE_VIS pointer_safety
  4537. {
  4538. enum __lx
  4539. {
  4540. relaxed,
  4541. preferred,
  4542. strict
  4543. };
  4544. __lx __v_;
  4545. _LIBCPP_INLINE_VISIBILITY
  4546. pointer_safety() : __v_() {}
  4547. _LIBCPP_INLINE_VISIBILITY
  4548. pointer_safety(__lx __v) : __v_(__v) {}
  4549. _LIBCPP_INLINE_VISIBILITY
  4550. operator int() const {return __v_;}
  4551. };
  4552. #endif
  4553. #if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) && \
  4554. defined(_LIBCPP_BUILDING_LIBRARY)
  4555. _LIBCPP_FUNC_VIS pointer_safety get_pointer_safety() _NOEXCEPT;
  4556. #else
  4557. // This function is only offered in C++03 under ABI v1.
  4558. # if !defined(_LIBCPP_ABI_POINTER_SAFETY_ENUM_TYPE) || !defined(_LIBCPP_CXX03_LANG)
  4559. inline _LIBCPP_INLINE_VISIBILITY
  4560. pointer_safety get_pointer_safety() _NOEXCEPT {
  4561. return pointer_safety::relaxed;
  4562. }
  4563. # endif
  4564. #endif
  4565. _LIBCPP_FUNC_VIS void declare_reachable(void* __p);
  4566. _LIBCPP_FUNC_VIS void declare_no_pointers(char* __p, size_t __n);
  4567. _LIBCPP_FUNC_VIS void undeclare_no_pointers(char* __p, size_t __n);
  4568. _LIBCPP_FUNC_VIS void* __undeclare_reachable(void* __p);
  4569. template <class _Tp>
  4570. inline _LIBCPP_INLINE_VISIBILITY
  4571. _Tp*
  4572. undeclare_reachable(_Tp* __p)
  4573. {
  4574. return static_cast<_Tp*>(__undeclare_reachable(__p));
  4575. }
  4576. _LIBCPP_FUNC_VIS void* align(size_t __align, size_t __sz, void*& __ptr, size_t& __space);
  4577. // --- Helper for container swap --
  4578. template <typename _Alloc>
  4579. inline _LIBCPP_INLINE_VISIBILITY
  4580. void __swap_allocator(_Alloc & __a1, _Alloc & __a2)
  4581. #if _LIBCPP_STD_VER >= 14
  4582. _NOEXCEPT
  4583. #else
  4584. _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
  4585. #endif
  4586. {
  4587. __swap_allocator(__a1, __a2,
  4588. integral_constant<bool, _VSTD::allocator_traits<_Alloc>::propagate_on_container_swap::value>());
  4589. }
  4590. template <typename _Alloc>
  4591. _LIBCPP_INLINE_VISIBILITY
  4592. void __swap_allocator(_Alloc & __a1, _Alloc & __a2, true_type)
  4593. #if _LIBCPP_STD_VER >= 14
  4594. _NOEXCEPT
  4595. #else
  4596. _NOEXCEPT_(__is_nothrow_swappable<_Alloc>::value)
  4597. #endif
  4598. {
  4599. using _VSTD::swap;
  4600. swap(__a1, __a2);
  4601. }
  4602. template <typename _Alloc>
  4603. inline _LIBCPP_INLINE_VISIBILITY
  4604. void __swap_allocator(_Alloc &, _Alloc &, false_type) _NOEXCEPT {}
  4605. template <typename _Alloc, typename _Traits=allocator_traits<_Alloc> >
  4606. struct __noexcept_move_assign_container : public integral_constant<bool,
  4607. _Traits::propagate_on_container_move_assignment::value
  4608. #if _LIBCPP_STD_VER > 14
  4609. || _Traits::is_always_equal::value
  4610. #else
  4611. && is_nothrow_move_assignable<_Alloc>::value
  4612. #endif
  4613. > {};
  4614. #ifndef _LIBCPP_HAS_NO_VARIADICS
  4615. template <class _Tp, class _Alloc>
  4616. struct __temp_value {
  4617. typedef allocator_traits<_Alloc> _Traits;
  4618. typename aligned_storage<sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp)>::type __v;
  4619. _Alloc &__a;
  4620. _Tp *__addr() { return reinterpret_cast<_Tp *>(addressof(__v)); }
  4621. _Tp & get() { return *__addr(); }
  4622. template<class... _Args>
  4623. _LIBCPP_NO_CFI
  4624. __temp_value(_Alloc &__alloc, _Args&& ... __args) : __a(__alloc) {
  4625. _Traits::construct(__a, reinterpret_cast<_Tp*>(addressof(__v)),
  4626. _VSTD::forward<_Args>(__args)...);
  4627. }
  4628. ~__temp_value() { _Traits::destroy(__a, __addr()); }
  4629. };
  4630. #endif
  4631. template<typename _Alloc, typename = void, typename = void>
  4632. struct __is_allocator : false_type {};
  4633. template<typename _Alloc>
  4634. struct __is_allocator<_Alloc,
  4635. typename __void_t<typename _Alloc::value_type>::type,
  4636. typename __void_t<decltype(_VSTD::declval<_Alloc&>().allocate(size_t(0)))>::type
  4637. >
  4638. : true_type {};
  4639. // __builtin_new_allocator -- A non-templated helper for allocating and
  4640. // deallocating memory using __builtin_operator_new and
  4641. // __builtin_operator_delete. It should be used in preference to
  4642. // `std::allocator<T>` to avoid additional instantiations.
  4643. struct __builtin_new_allocator {
  4644. struct __builtin_new_deleter {
  4645. typedef void* pointer_type;
  4646. _LIBCPP_CONSTEXPR explicit __builtin_new_deleter(size_t __size, size_t __align)
  4647. : __size_(__size), __align_(__align) {}
  4648. void operator()(void* p) const _NOEXCEPT {
  4649. std::__libcpp_deallocate(p, __size_, __align_);
  4650. }
  4651. private:
  4652. size_t __size_;
  4653. size_t __align_;
  4654. };
  4655. typedef unique_ptr<void, __builtin_new_deleter> __holder_t;
  4656. static __holder_t __allocate_bytes(size_t __s, size_t __align) {
  4657. return __holder_t(std::__libcpp_allocate(__s, __align),
  4658. __builtin_new_deleter(__s, __align));
  4659. }
  4660. static void __deallocate_bytes(void* __p, size_t __s,
  4661. size_t __align) _NOEXCEPT {
  4662. std::__libcpp_deallocate(__p, __s, __align);
  4663. }
  4664. template <class _Tp>
  4665. _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
  4666. static __holder_t __allocate_type(size_t __n) {
  4667. return __allocate_bytes(__n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
  4668. }
  4669. template <class _Tp>
  4670. _LIBCPP_NODEBUG _LIBCPP_ALWAYS_INLINE
  4671. static void __deallocate_type(void* __p, size_t __n) _NOEXCEPT {
  4672. __deallocate_bytes(__p, __n * sizeof(_Tp), _LIBCPP_ALIGNOF(_Tp));
  4673. }
  4674. };
  4675. _LIBCPP_END_NAMESPACE_STD
  4676. _LIBCPP_POP_MACROS
  4677. #if defined(_LIBCPP_HAS_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
  4678. # include <__pstl_memory>
  4679. #endif
  4680. #endif // _LIBCPP_MEMORY