entt.hpp 1.5 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625
  1. // #include "config/version.h"
  2. #ifndef ENTT_CONFIG_VERSION_H
  3. #define ENTT_CONFIG_VERSION_H
  4. #define ENTT_VERSION_MAJOR 3
  5. #define ENTT_VERSION_MINOR 8
  6. #define ENTT_VERSION_PATCH 0
  7. #endif
  8. // #include "core/algorithm.hpp"
  9. #ifndef ENTT_CORE_ALGORITHM_HPP
  10. #define ENTT_CORE_ALGORITHM_HPP
  11. #include <vector>
  12. #include <utility>
  13. #include <iterator>
  14. #include <algorithm>
  15. #include <functional>
  16. // #include "utility.hpp"
  17. #ifndef ENTT_CORE_UTILITY_HPP
  18. #define ENTT_CORE_UTILITY_HPP
  19. #include <utility>
  20. // #include "../config/config.h"
  21. #ifndef ENTT_CONFIG_CONFIG_H
  22. #define ENTT_CONFIG_CONFIG_H
  23. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  24. # define ENTT_NOEXCEPT noexcept
  25. # define ENTT_THROW throw
  26. # define ENTT_TRY try
  27. # define ENTT_CATCH catch(...)
  28. #else
  29. # define ENTT_NOEXCEPT
  30. # define ENTT_THROW
  31. # define ENTT_TRY if(true)
  32. # define ENTT_CATCH if(false)
  33. #endif
  34. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  35. # include <new>
  36. # define ENTT_LAUNDER(expr) std::launder(expr)
  37. #else
  38. # define ENTT_LAUNDER(expr) expr
  39. #endif
  40. #ifndef ENTT_USE_ATOMIC
  41. # define ENTT_MAYBE_ATOMIC(Type) Type
  42. #else
  43. # include <atomic>
  44. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  45. #endif
  46. #ifndef ENTT_ID_TYPE
  47. # include <cstdint>
  48. # define ENTT_ID_TYPE std::uint32_t
  49. #endif
  50. #ifdef ENTT_SPARSE_PAGE
  51. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  52. #else
  53. # define ENTT_SPARSE_PAGE 4096
  54. #endif
  55. #ifdef ENTT_PACKED_PAGE
  56. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  57. #else
  58. # define ENTT_PACKED_PAGE 1024
  59. #endif
  60. #ifdef ENTT_DISABLE_ASSERT
  61. # undef ENTT_ASSERT
  62. # define ENTT_ASSERT(...) (void(0))
  63. #elif !defined ENTT_ASSERT
  64. # include <cassert>
  65. # define ENTT_ASSERT(condition, ...) assert(condition)
  66. #endif
  67. #ifdef ENTT_NO_ETO
  68. # include <type_traits>
  69. # define ENTT_IGNORE_IF_EMPTY std::false_type
  70. #else
  71. # include <type_traits>
  72. # define ENTT_IGNORE_IF_EMPTY std::true_type
  73. #endif
  74. #ifndef ENTT_STANDARD_CPP
  75. # if defined __clang__ || defined __GNUC__
  76. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  77. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  78. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  79. # elif defined _MSC_VER
  80. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  81. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  82. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  83. # endif
  84. #endif
  85. #endif
  86. namespace entt {
  87. /*! @brief Identity function object (waiting for C++20). */
  88. struct identity {
  89. /**
  90. * @brief Returns its argument unchanged.
  91. * @tparam Type Type of the argument.
  92. * @param value The actual argument.
  93. * @return The submitted value as-is.
  94. */
  95. template<class Type>
  96. [[nodiscard]] constexpr Type && operator()(Type &&value) const ENTT_NOEXCEPT {
  97. return std::forward<Type>(value);
  98. }
  99. };
  100. /**
  101. * @brief Constant utility to disambiguate overloaded members of a class.
  102. * @tparam Type Type of the desired overload.
  103. * @tparam Class Type of class to which the member belongs.
  104. * @param member A valid pointer to a member.
  105. * @return Pointer to the member.
  106. */
  107. template<typename Type, typename Class>
  108. [[nodiscard]] constexpr auto overload(Type Class:: *member) ENTT_NOEXCEPT { return member; }
  109. /**
  110. * @brief Constant utility to disambiguate overloaded functions.
  111. * @tparam Func Function type of the desired overload.
  112. * @param func A valid pointer to a function.
  113. * @return Pointer to the function.
  114. */
  115. template<typename Func>
  116. [[nodiscard]] constexpr auto overload(Func *func) ENTT_NOEXCEPT { return func; }
  117. /**
  118. * @brief Helper type for visitors.
  119. * @tparam Func Types of function objects.
  120. */
  121. template<class... Func>
  122. struct overloaded: Func... {
  123. using Func::operator()...;
  124. };
  125. /**
  126. * @brief Deduction guide.
  127. * @tparam Func Types of function objects.
  128. */
  129. template<class... Func>
  130. overloaded(Func...)
  131. -> overloaded<Func...>;
  132. /**
  133. * @brief Basic implementation of a y-combinator.
  134. * @tparam Func Type of a potentially recursive function.
  135. */
  136. template<class Func>
  137. struct y_combinator {
  138. /**
  139. * @brief Constructs a y-combinator from a given function.
  140. * @param recursive A potentially recursive function.
  141. */
  142. y_combinator(Func recursive):
  143. func{std::move(recursive)}
  144. {}
  145. /**
  146. * @brief Invokes a y-combinator and therefore its underlying function.
  147. * @tparam Args Types of arguments to use to invoke the underlying function.
  148. * @param args Parameters to use to invoke the underlying function.
  149. * @return Return value of the underlying function, if any.
  150. */
  151. template <class... Args>
  152. decltype(auto) operator()(Args &&... args) const {
  153. return func(*this, std::forward<Args>(args)...);
  154. }
  155. /*! @copydoc operator()() */
  156. template <class... Args>
  157. decltype(auto) operator()(Args &&... args) {
  158. return func(*this, std::forward<Args>(args)...);
  159. }
  160. private:
  161. Func func;
  162. };
  163. }
  164. #endif
  165. namespace entt {
  166. /**
  167. * @brief Function object to wrap `std::sort` in a class type.
  168. *
  169. * Unfortunately, `std::sort` cannot be passed as template argument to a class
  170. * template or a function template.<br/>
  171. * This class fills the gap by wrapping some flavors of `std::sort` in a
  172. * function object.
  173. */
  174. struct std_sort {
  175. /**
  176. * @brief Sorts the elements in a range.
  177. *
  178. * Sorts the elements in a range using the given binary comparison function.
  179. *
  180. * @tparam It Type of random access iterator.
  181. * @tparam Compare Type of comparison function object.
  182. * @tparam Args Types of arguments to forward to the sort function.
  183. * @param first An iterator to the first element of the range to sort.
  184. * @param last An iterator past the last element of the range to sort.
  185. * @param compare A valid comparison function object.
  186. * @param args Arguments to forward to the sort function, if any.
  187. */
  188. template<typename It, typename Compare = std::less<>, typename... Args>
  189. void operator()(It first, It last, Compare compare = Compare{}, Args &&... args) const {
  190. std::sort(std::forward<Args>(args)..., std::move(first), std::move(last), std::move(compare));
  191. }
  192. };
  193. /*! @brief Function object for performing insertion sort. */
  194. struct insertion_sort {
  195. /**
  196. * @brief Sorts the elements in a range.
  197. *
  198. * Sorts the elements in a range using the given binary comparison function.
  199. *
  200. * @tparam It Type of random access iterator.
  201. * @tparam Compare Type of comparison function object.
  202. * @param first An iterator to the first element of the range to sort.
  203. * @param last An iterator past the last element of the range to sort.
  204. * @param compare A valid comparison function object.
  205. */
  206. template<typename It, typename Compare = std::less<>>
  207. void operator()(It first, It last, Compare compare = Compare{}) const {
  208. if(first < last) {
  209. for(auto it = first + 1; it < last; ++it) {
  210. auto value = std::move(*it);
  211. auto pre = it;
  212. for(; pre > first && compare(value, *(pre-1)); --pre) {
  213. *pre = std::move(*(pre-1));
  214. }
  215. *pre = std::move(value);
  216. }
  217. }
  218. }
  219. };
  220. /**
  221. * @brief Function object for performing LSD radix sort.
  222. * @tparam Bit Number of bits processed per pass.
  223. * @tparam N Maximum number of bits to sort.
  224. */
  225. template<std::size_t Bit, std::size_t N>
  226. struct radix_sort {
  227. static_assert((N % Bit) == 0, "The maximum number of bits to sort must be a multiple of the number of bits processed per pass");
  228. /**
  229. * @brief Sorts the elements in a range.
  230. *
  231. * Sorts the elements in a range using the given _getter_ to access the
  232. * actual data to be sorted.
  233. *
  234. * This implementation is inspired by the online book
  235. * [Physically Based Rendering](http://www.pbr-book.org/3ed-2018/Primitives_and_Intersection_Acceleration/Bounding_Volume_Hierarchies.html#RadixSort).
  236. *
  237. * @tparam It Type of random access iterator.
  238. * @tparam Getter Type of _getter_ function object.
  239. * @param first An iterator to the first element of the range to sort.
  240. * @param last An iterator past the last element of the range to sort.
  241. * @param getter A valid _getter_ function object.
  242. */
  243. template<typename It, typename Getter = identity>
  244. void operator()(It first, It last, Getter getter = Getter{}) const {
  245. if(first < last) {
  246. static constexpr auto mask = (1 << Bit) - 1;
  247. static constexpr auto buckets = 1 << Bit;
  248. static constexpr auto passes = N / Bit;
  249. using value_type = typename std::iterator_traits<It>::value_type;
  250. std::vector<value_type> aux(std::distance(first, last));
  251. auto part = [getter = std::move(getter)](auto from, auto to, auto out, auto start) {
  252. std::size_t index[buckets]{};
  253. std::size_t count[buckets]{};
  254. for(auto it = from; it != to; ++it) {
  255. ++count[(getter(*it) >> start) & mask];
  256. }
  257. for(std::size_t pos{}, end = buckets - 1u; pos < end; ++pos) {
  258. index[pos + 1u] = index[pos] + count[pos];
  259. }
  260. for(auto it = from; it != to; ++it) {
  261. out[index[(getter(*it) >> start) & mask]++] = std::move(*it);
  262. }
  263. };
  264. for(std::size_t pass = 0; pass < (passes & ~1); pass += 2) {
  265. part(first, last, aux.begin(), pass * Bit);
  266. part(aux.begin(), aux.end(), first, (pass + 1) * Bit);
  267. }
  268. if constexpr(passes & 1) {
  269. part(first, last, aux.begin(), (passes - 1) * Bit);
  270. std::move(aux.begin(), aux.end(), first);
  271. }
  272. }
  273. }
  274. };
  275. }
  276. #endif
  277. // #include "core/any.hpp"
  278. #ifndef ENTT_CORE_ANY_HPP
  279. #define ENTT_CORE_ANY_HPP
  280. #include <cstddef>
  281. #include <functional>
  282. #include <memory>
  283. #include <type_traits>
  284. #include <utility>
  285. // #include "../config/config.h"
  286. // #include "../core/utility.hpp"
  287. #ifndef ENTT_CORE_UTILITY_HPP
  288. #define ENTT_CORE_UTILITY_HPP
  289. #include <utility>
  290. // #include "../config/config.h"
  291. #ifndef ENTT_CONFIG_CONFIG_H
  292. #define ENTT_CONFIG_CONFIG_H
  293. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  294. # define ENTT_NOEXCEPT noexcept
  295. # define ENTT_THROW throw
  296. # define ENTT_TRY try
  297. # define ENTT_CATCH catch(...)
  298. #else
  299. # define ENTT_NOEXCEPT
  300. # define ENTT_THROW
  301. # define ENTT_TRY if(true)
  302. # define ENTT_CATCH if(false)
  303. #endif
  304. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  305. # include <new>
  306. # define ENTT_LAUNDER(expr) std::launder(expr)
  307. #else
  308. # define ENTT_LAUNDER(expr) expr
  309. #endif
  310. #ifndef ENTT_USE_ATOMIC
  311. # define ENTT_MAYBE_ATOMIC(Type) Type
  312. #else
  313. # include <atomic>
  314. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  315. #endif
  316. #ifndef ENTT_ID_TYPE
  317. # include <cstdint>
  318. # define ENTT_ID_TYPE std::uint32_t
  319. #endif
  320. #ifdef ENTT_SPARSE_PAGE
  321. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  322. #else
  323. # define ENTT_SPARSE_PAGE 4096
  324. #endif
  325. #ifdef ENTT_PACKED_PAGE
  326. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  327. #else
  328. # define ENTT_PACKED_PAGE 1024
  329. #endif
  330. #ifdef ENTT_DISABLE_ASSERT
  331. # undef ENTT_ASSERT
  332. # define ENTT_ASSERT(...) (void(0))
  333. #elif !defined ENTT_ASSERT
  334. # include <cassert>
  335. # define ENTT_ASSERT(condition, ...) assert(condition)
  336. #endif
  337. #ifdef ENTT_NO_ETO
  338. # include <type_traits>
  339. # define ENTT_IGNORE_IF_EMPTY std::false_type
  340. #else
  341. # include <type_traits>
  342. # define ENTT_IGNORE_IF_EMPTY std::true_type
  343. #endif
  344. #ifndef ENTT_STANDARD_CPP
  345. # if defined __clang__ || defined __GNUC__
  346. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  347. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  348. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  349. # elif defined _MSC_VER
  350. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  351. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  352. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  353. # endif
  354. #endif
  355. #endif
  356. namespace entt {
  357. /*! @brief Identity function object (waiting for C++20). */
  358. struct identity {
  359. /**
  360. * @brief Returns its argument unchanged.
  361. * @tparam Type Type of the argument.
  362. * @param value The actual argument.
  363. * @return The submitted value as-is.
  364. */
  365. template<class Type>
  366. [[nodiscard]] constexpr Type && operator()(Type &&value) const ENTT_NOEXCEPT {
  367. return std::forward<Type>(value);
  368. }
  369. };
  370. /**
  371. * @brief Constant utility to disambiguate overloaded members of a class.
  372. * @tparam Type Type of the desired overload.
  373. * @tparam Class Type of class to which the member belongs.
  374. * @param member A valid pointer to a member.
  375. * @return Pointer to the member.
  376. */
  377. template<typename Type, typename Class>
  378. [[nodiscard]] constexpr auto overload(Type Class:: *member) ENTT_NOEXCEPT { return member; }
  379. /**
  380. * @brief Constant utility to disambiguate overloaded functions.
  381. * @tparam Func Function type of the desired overload.
  382. * @param func A valid pointer to a function.
  383. * @return Pointer to the function.
  384. */
  385. template<typename Func>
  386. [[nodiscard]] constexpr auto overload(Func *func) ENTT_NOEXCEPT { return func; }
  387. /**
  388. * @brief Helper type for visitors.
  389. * @tparam Func Types of function objects.
  390. */
  391. template<class... Func>
  392. struct overloaded: Func... {
  393. using Func::operator()...;
  394. };
  395. /**
  396. * @brief Deduction guide.
  397. * @tparam Func Types of function objects.
  398. */
  399. template<class... Func>
  400. overloaded(Func...)
  401. -> overloaded<Func...>;
  402. /**
  403. * @brief Basic implementation of a y-combinator.
  404. * @tparam Func Type of a potentially recursive function.
  405. */
  406. template<class Func>
  407. struct y_combinator {
  408. /**
  409. * @brief Constructs a y-combinator from a given function.
  410. * @param recursive A potentially recursive function.
  411. */
  412. y_combinator(Func recursive):
  413. func{std::move(recursive)}
  414. {}
  415. /**
  416. * @brief Invokes a y-combinator and therefore its underlying function.
  417. * @tparam Args Types of arguments to use to invoke the underlying function.
  418. * @param args Parameters to use to invoke the underlying function.
  419. * @return Return value of the underlying function, if any.
  420. */
  421. template <class... Args>
  422. decltype(auto) operator()(Args &&... args) const {
  423. return func(*this, std::forward<Args>(args)...);
  424. }
  425. /*! @copydoc operator()() */
  426. template <class... Args>
  427. decltype(auto) operator()(Args &&... args) {
  428. return func(*this, std::forward<Args>(args)...);
  429. }
  430. private:
  431. Func func;
  432. };
  433. }
  434. #endif
  435. // #include "fwd.hpp"
  436. #ifndef ENTT_CORE_FWD_HPP
  437. #define ENTT_CORE_FWD_HPP
  438. #include <type_traits>
  439. // #include "../config/config.h"
  440. namespace entt {
  441. template<std::size_t Len = sizeof(double[2]), std::size_t = alignof(typename std::aligned_storage_t<Len + !Len>)>
  442. class basic_any;
  443. /*! @brief Alias declaration for type identifiers. */
  444. using id_type = ENTT_ID_TYPE;
  445. /*! @brief Alias declaration for the most common use case. */
  446. using any = basic_any<>;
  447. }
  448. #endif
  449. // #include "type_info.hpp"
  450. #ifndef ENTT_CORE_TYPE_INFO_HPP
  451. #define ENTT_CORE_TYPE_INFO_HPP
  452. #include <string_view>
  453. #include <type_traits>
  454. // #include "../config/config.h"
  455. // #include "../core/attribute.h"
  456. #ifndef ENTT_CORE_ATTRIBUTE_H
  457. #define ENTT_CORE_ATTRIBUTE_H
  458. #ifndef ENTT_EXPORT
  459. # if defined _WIN32 || defined __CYGWIN__ || defined _MSC_VER
  460. # define ENTT_EXPORT __declspec(dllexport)
  461. # define ENTT_IMPORT __declspec(dllimport)
  462. # define ENTT_HIDDEN
  463. # elif defined __GNUC__ && __GNUC__ >= 4
  464. # define ENTT_EXPORT __attribute__((visibility("default")))
  465. # define ENTT_IMPORT __attribute__((visibility("default")))
  466. # define ENTT_HIDDEN __attribute__((visibility("hidden")))
  467. # else /* Unsupported compiler */
  468. # define ENTT_EXPORT
  469. # define ENTT_IMPORT
  470. # define ENTT_HIDDEN
  471. # endif
  472. #endif
  473. #ifndef ENTT_API
  474. # if defined ENTT_API_EXPORT
  475. # define ENTT_API ENTT_EXPORT
  476. # elif defined ENTT_API_IMPORT
  477. # define ENTT_API ENTT_IMPORT
  478. # else /* No API */
  479. # define ENTT_API
  480. # endif
  481. #endif
  482. #endif
  483. // #include "hashed_string.hpp"
  484. #ifndef ENTT_CORE_HASHED_STRING_HPP
  485. #define ENTT_CORE_HASHED_STRING_HPP
  486. #include <cstddef>
  487. #include <cstdint>
  488. // #include "../config/config.h"
  489. // #include "fwd.hpp"
  490. namespace entt {
  491. /**
  492. * @cond TURN_OFF_DOXYGEN
  493. * Internal details not to be documented.
  494. */
  495. namespace internal {
  496. template<typename>
  497. struct fnv1a_traits;
  498. template<>
  499. struct fnv1a_traits<std::uint32_t> {
  500. using type = std::uint32_t;
  501. static constexpr std::uint32_t offset = 2166136261;
  502. static constexpr std::uint32_t prime = 16777619;
  503. };
  504. template<>
  505. struct fnv1a_traits<std::uint64_t> {
  506. using type = std::uint64_t;
  507. static constexpr std::uint64_t offset = 14695981039346656037ull;
  508. static constexpr std::uint64_t prime = 1099511628211ull;
  509. };
  510. }
  511. /**
  512. * Internal details not to be documented.
  513. * @endcond
  514. */
  515. /**
  516. * @brief Zero overhead unique identifier.
  517. *
  518. * A hashed string is a compile-time tool that allows users to use
  519. * human-readable identifers in the codebase while using their numeric
  520. * counterparts at runtime.<br/>
  521. * Because of that, a hashed string can also be used in constant expressions if
  522. * required.
  523. *
  524. * @tparam Char Character type.
  525. */
  526. template<typename Char>
  527. class basic_hashed_string {
  528. using traits_type = internal::fnv1a_traits<id_type>;
  529. struct const_wrapper {
  530. // non-explicit constructor on purpose
  531. constexpr const_wrapper(const Char *curr) ENTT_NOEXCEPT: str{curr} {}
  532. const Char *str;
  533. };
  534. // Fowler–Noll–Vo hash function v. 1a - the good
  535. [[nodiscard]] static constexpr id_type helper(const Char *curr) ENTT_NOEXCEPT {
  536. auto value = traits_type::offset;
  537. while(*curr != 0) {
  538. value = (value ^ static_cast<traits_type::type>(*(curr++))) * traits_type::prime;
  539. }
  540. return value;
  541. }
  542. public:
  543. /*! @brief Character type. */
  544. using value_type = Char;
  545. /*! @brief Unsigned integer type. */
  546. using hash_type = id_type;
  547. /**
  548. * @brief Returns directly the numeric representation of a string view.
  549. * @param str Human-readable identifer.
  550. * @param size Length of the string to hash.
  551. * @return The numeric representation of the string.
  552. */
  553. [[nodiscard]] static constexpr hash_type value(const value_type *str, std::size_t size) ENTT_NOEXCEPT {
  554. id_type partial{traits_type::offset};
  555. while(size--) { partial = (partial^(str++)[0])*traits_type::prime; }
  556. return partial;
  557. }
  558. /**
  559. * @brief Returns directly the numeric representation of a string.
  560. *
  561. * Forcing template resolution avoids implicit conversions. An
  562. * human-readable identifier can be anything but a plain, old bunch of
  563. * characters.<br/>
  564. * Example of use:
  565. * @code{.cpp}
  566. * const auto value = basic_hashed_string<char>::to_value("my.png");
  567. * @endcode
  568. *
  569. * @tparam N Number of characters of the identifier.
  570. * @param str Human-readable identifer.
  571. * @return The numeric representation of the string.
  572. */
  573. template<std::size_t N>
  574. [[nodiscard]] static constexpr hash_type value(const value_type (&str)[N]) ENTT_NOEXCEPT {
  575. return helper(str);
  576. }
  577. /**
  578. * @brief Returns directly the numeric representation of a string.
  579. * @param wrapper Helps achieving the purpose by relying on overloading.
  580. * @return The numeric representation of the string.
  581. */
  582. [[nodiscard]] static hash_type value(const_wrapper wrapper) ENTT_NOEXCEPT {
  583. return helper(wrapper.str);
  584. }
  585. /*! @brief Constructs an empty hashed string. */
  586. constexpr basic_hashed_string() ENTT_NOEXCEPT
  587. : str{nullptr}, hash{}
  588. {}
  589. /**
  590. * @brief Constructs a hashed string from an array of const characters.
  591. *
  592. * Forcing template resolution avoids implicit conversions. An
  593. * human-readable identifier can be anything but a plain, old bunch of
  594. * characters.<br/>
  595. * Example of use:
  596. * @code{.cpp}
  597. * basic_hashed_string<char> hs{"my.png"};
  598. * @endcode
  599. *
  600. * @tparam N Number of characters of the identifier.
  601. * @param curr Human-readable identifer.
  602. */
  603. template<std::size_t N>
  604. constexpr basic_hashed_string(const value_type (&curr)[N]) ENTT_NOEXCEPT
  605. : str{curr}, hash{helper(curr)}
  606. {}
  607. /**
  608. * @brief Explicit constructor on purpose to avoid constructing a hashed
  609. * string directly from a `const value_type *`.
  610. * @param wrapper Helps achieving the purpose by relying on overloading.
  611. */
  612. explicit constexpr basic_hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
  613. : str{wrapper.str}, hash{helper(wrapper.str)}
  614. {}
  615. /**
  616. * @brief Returns the human-readable representation of a hashed string.
  617. * @return The string used to initialize the instance.
  618. */
  619. [[nodiscard]] constexpr const value_type * data() const ENTT_NOEXCEPT {
  620. return str;
  621. }
  622. /**
  623. * @brief Returns the numeric representation of a hashed string.
  624. * @return The numeric representation of the instance.
  625. */
  626. [[nodiscard]] constexpr hash_type value() const ENTT_NOEXCEPT {
  627. return hash;
  628. }
  629. /*! @copydoc data */
  630. [[nodiscard]] constexpr operator const value_type *() const ENTT_NOEXCEPT { return data(); }
  631. /**
  632. * @brief Returns the numeric representation of a hashed string.
  633. * @return The numeric representation of the instance.
  634. */
  635. [[nodiscard]] constexpr operator hash_type() const ENTT_NOEXCEPT { return value(); }
  636. /**
  637. * @brief Compares two hashed strings.
  638. * @param other Hashed string with which to compare.
  639. * @return True if the two hashed strings are identical, false otherwise.
  640. */
  641. [[nodiscard]] constexpr bool operator==(const basic_hashed_string &other) const ENTT_NOEXCEPT {
  642. return hash == other.hash;
  643. }
  644. private:
  645. const value_type *str;
  646. hash_type hash;
  647. };
  648. /**
  649. * @brief Deduction guide.
  650. *
  651. * It allows to deduce the character type of the hashed string directly from a
  652. * human-readable identifer provided to the constructor.
  653. *
  654. * @tparam Char Character type.
  655. * @tparam N Number of characters of the identifier.
  656. * @param str Human-readable identifer.
  657. */
  658. template<typename Char, std::size_t N>
  659. basic_hashed_string(const Char (&str)[N])
  660. -> basic_hashed_string<Char>;
  661. /**
  662. * @brief Compares two hashed strings.
  663. * @tparam Char Character type.
  664. * @param lhs A valid hashed string.
  665. * @param rhs A valid hashed string.
  666. * @return True if the two hashed strings are identical, false otherwise.
  667. */
  668. template<typename Char>
  669. [[nodiscard]] constexpr bool operator!=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) ENTT_NOEXCEPT {
  670. return !(lhs == rhs);
  671. }
  672. /*! @brief Aliases for common character types. */
  673. using hashed_string = basic_hashed_string<char>;
  674. /*! @brief Aliases for common character types. */
  675. using hashed_wstring = basic_hashed_string<wchar_t>;
  676. inline namespace literals {
  677. /**
  678. * @brief User defined literal for hashed strings.
  679. * @param str The literal without its suffix.
  680. * @return A properly initialized hashed string.
  681. */
  682. [[nodiscard]] constexpr entt::hashed_string operator"" _hs(const char *str, std::size_t) ENTT_NOEXCEPT {
  683. return entt::hashed_string{str};
  684. }
  685. /**
  686. * @brief User defined literal for hashed wstrings.
  687. * @param str The literal without its suffix.
  688. * @return A properly initialized hashed wstring.
  689. */
  690. [[nodiscard]] constexpr entt::hashed_wstring operator"" _hws(const wchar_t *str, std::size_t) ENTT_NOEXCEPT {
  691. return entt::hashed_wstring{str};
  692. }
  693. }
  694. }
  695. #endif
  696. // #include "fwd.hpp"
  697. namespace entt {
  698. /**
  699. * @cond TURN_OFF_DOXYGEN
  700. * Internal details not to be documented.
  701. */
  702. namespace internal {
  703. struct ENTT_API type_seq final {
  704. [[nodiscard]] static id_type next() ENTT_NOEXCEPT {
  705. static ENTT_MAYBE_ATOMIC(id_type) value{};
  706. return value++;
  707. }
  708. };
  709. template<typename Type>
  710. [[nodiscard]] constexpr auto stripped_type_name() ENTT_NOEXCEPT {
  711. #if defined ENTT_PRETTY_FUNCTION
  712. std::string_view pretty_function{ENTT_PRETTY_FUNCTION};
  713. auto first = pretty_function.find_first_not_of(' ', pretty_function.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX)+1);
  714. auto value = pretty_function.substr(first, pretty_function.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
  715. return value;
  716. #else
  717. return std::string_view{""};
  718. #endif
  719. }
  720. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  721. [[nodiscard]] static constexpr std::string_view type_name(int) ENTT_NOEXCEPT {
  722. constexpr auto value = stripped_type_name<Type>();
  723. return value;
  724. }
  725. template<typename Type>
  726. [[nodiscard]] static std::string_view type_name(char) ENTT_NOEXCEPT {
  727. static const auto value = stripped_type_name<Type>();
  728. return value;
  729. }
  730. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  731. [[nodiscard]] static constexpr id_type type_hash(int) ENTT_NOEXCEPT {
  732. constexpr auto stripped = stripped_type_name<Type>();
  733. constexpr auto value = hashed_string::value(stripped.data(), stripped.size());
  734. return value;
  735. }
  736. template<typename Type>
  737. [[nodiscard]] static id_type type_hash(char) ENTT_NOEXCEPT {
  738. static const auto value = [](const auto stripped) {
  739. return hashed_string::value(stripped.data(), stripped.size());
  740. }(stripped_type_name<Type>());
  741. return value;
  742. }
  743. }
  744. /**
  745. * Internal details not to be documented.
  746. * @endcond
  747. */
  748. /**
  749. * @brief Type sequential identifier.
  750. * @tparam Type Type for which to generate a sequential identifier.
  751. */
  752. template<typename Type, typename = void>
  753. struct ENTT_API type_seq final {
  754. /**
  755. * @brief Returns the sequential identifier of a given type.
  756. * @return The sequential identifier of a given type.
  757. */
  758. [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
  759. static const id_type value = internal::type_seq::next();
  760. return value;
  761. }
  762. /*! @copydoc value */
  763. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  764. };
  765. /**
  766. * @brief Type hash.
  767. * @tparam Type Type for which to generate a hash value.
  768. */
  769. template<typename Type, typename = void>
  770. struct type_hash final {
  771. /**
  772. * @brief Returns the numeric representation of a given type.
  773. * @return The numeric representation of the given type.
  774. */
  775. #if defined ENTT_PRETTY_FUNCTION
  776. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  777. return internal::type_hash<Type>(0);
  778. #else
  779. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  780. return type_seq<Type>::value();
  781. #endif
  782. }
  783. /*! @copydoc value */
  784. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  785. };
  786. /**
  787. * @brief Type name.
  788. * @tparam Type Type for which to generate a name.
  789. */
  790. template<typename Type, typename = void>
  791. struct type_name final {
  792. /**
  793. * @brief Returns the name of a given type.
  794. * @return The name of the given type.
  795. */
  796. [[nodiscard]] static constexpr std::string_view value() ENTT_NOEXCEPT {
  797. return internal::type_name<Type>(0);
  798. }
  799. /*! @copydoc value */
  800. [[nodiscard]] constexpr operator std::string_view() const ENTT_NOEXCEPT { return value(); }
  801. };
  802. /*! @brief Implementation specific information about a type. */
  803. class type_info final {
  804. template<typename>
  805. friend type_info type_id() ENTT_NOEXCEPT;
  806. type_info(id_type seq_v, id_type hash_v, std::string_view name_v) ENTT_NOEXCEPT
  807. : seq_value{seq_v},
  808. hash_value{hash_v},
  809. name_value{name_v}
  810. {}
  811. public:
  812. /*! @brief Default constructor. */
  813. type_info() ENTT_NOEXCEPT
  814. : type_info({}, {}, {})
  815. {}
  816. /*! @brief Default copy constructor. */
  817. type_info(const type_info &) ENTT_NOEXCEPT = default;
  818. /*! @brief Default move constructor. */
  819. type_info(type_info &&) ENTT_NOEXCEPT = default;
  820. /**
  821. * @brief Default copy assignment operator.
  822. * @return This type info object.
  823. */
  824. type_info & operator=(const type_info &) ENTT_NOEXCEPT = default;
  825. /**
  826. * @brief Default move assignment operator.
  827. * @return This type info object.
  828. */
  829. type_info & operator=(type_info &&) ENTT_NOEXCEPT = default;
  830. /**
  831. * @brief Checks if a type info object is properly initialized.
  832. * @return True if the object is properly initialized, false otherwise.
  833. */
  834. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  835. return name_value.data() != nullptr;
  836. }
  837. /**
  838. * @brief Type sequential identifier.
  839. * @return Type sequential identifier.
  840. */
  841. [[nodiscard]] id_type seq() const ENTT_NOEXCEPT {
  842. return seq_value;
  843. }
  844. /**
  845. * @brief Type hash.
  846. * @return Type hash.
  847. */
  848. [[nodiscard]] id_type hash() const ENTT_NOEXCEPT {
  849. return hash_value;
  850. }
  851. /**
  852. * @brief Type name.
  853. * @return Type name.
  854. */
  855. [[nodiscard]] std::string_view name() const ENTT_NOEXCEPT {
  856. return name_value;
  857. }
  858. /**
  859. * @brief Compares the contents of two type info objects.
  860. * @param other Object with which to compare.
  861. * @return False if the two contents differ, true otherwise.
  862. */
  863. [[nodiscard]] bool operator==(const type_info &other) const ENTT_NOEXCEPT {
  864. return hash_value == other.hash_value;
  865. }
  866. private:
  867. id_type seq_value;
  868. id_type hash_value;
  869. std::string_view name_value;
  870. };
  871. /**
  872. * @brief Compares the contents of two type info objects.
  873. * @param lhs A type info object.
  874. * @param rhs A type info object.
  875. * @return True if the two contents differ, false otherwise.
  876. */
  877. [[nodiscard]] inline bool operator!=(const type_info &lhs, const type_info &rhs) ENTT_NOEXCEPT {
  878. return !(lhs == rhs);
  879. }
  880. /**
  881. * @brief Returns the type info object for a given type.
  882. * @tparam Type Type for which to generate a type info object.
  883. * @return The type info object for the given type.
  884. */
  885. template<typename Type>
  886. [[nodiscard]] type_info type_id() ENTT_NOEXCEPT {
  887. return type_info{
  888. type_seq<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  889. type_hash<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  890. type_name<std::remove_cv_t<std::remove_reference_t<Type>>>::value()
  891. };
  892. }
  893. }
  894. #endif
  895. // #include "type_traits.hpp"
  896. #ifndef ENTT_CORE_TYPE_TRAITS_HPP
  897. #define ENTT_CORE_TYPE_TRAITS_HPP
  898. #include <cstddef>
  899. #include <iterator>
  900. #include <type_traits>
  901. #include <utility>
  902. // #include "../config/config.h"
  903. // #include "fwd.hpp"
  904. namespace entt {
  905. /**
  906. * @brief Utility class to disambiguate overloaded functions.
  907. * @tparam N Number of choices available.
  908. */
  909. template<std::size_t N>
  910. struct choice_t
  911. // Unfortunately, doxygen cannot parse such a construct.
  912. /*! @cond TURN_OFF_DOXYGEN */
  913. : choice_t<N-1>
  914. /*! @endcond */
  915. {};
  916. /*! @copybrief choice_t */
  917. template<>
  918. struct choice_t<0> {};
  919. /**
  920. * @brief Variable template for the choice trick.
  921. * @tparam N Number of choices available.
  922. */
  923. template<std::size_t N>
  924. inline constexpr choice_t<N> choice{};
  925. /**
  926. * @brief Identity type trait.
  927. *
  928. * Useful to establish non-deduced contexts in template argument deduction
  929. * (waiting for C++20) or to provide types through function arguments.
  930. *
  931. * @tparam Type A type.
  932. */
  933. template<typename Type>
  934. struct type_identity {
  935. /*! @brief Identity type. */
  936. using type = Type;
  937. };
  938. /**
  939. * @brief Helper type.
  940. * @tparam Type A type.
  941. */
  942. template<typename Type>
  943. using type_identity_t = typename type_identity<Type>::type;
  944. /**
  945. * @brief A type-only `sizeof` wrapper that returns 0 where `sizeof` complains.
  946. * @tparam Type The type of which to return the size.
  947. * @tparam The size of the type if `sizeof` accepts it, 0 otherwise.
  948. */
  949. template<typename Type, typename = void>
  950. struct size_of: std::integral_constant<std::size_t, 0u> {};
  951. /*! @copydoc size_of */
  952. template<typename Type>
  953. struct size_of<Type, std::void_t<decltype(sizeof(Type))>>
  954. : std::integral_constant<std::size_t, sizeof(Type)>
  955. {};
  956. /**
  957. * @brief Helper variable template.
  958. * @tparam Type The type of which to return the size.
  959. */
  960. template<class Type>
  961. inline constexpr std::size_t size_of_v = size_of<Type>::value;
  962. /**
  963. * @brief Using declaration to be used to _repeat_ the same type a number of
  964. * times equal to the size of a given parameter pack.
  965. * @tparam Type A type to repeat.
  966. */
  967. template<typename Type, typename>
  968. using unpack_as_t = Type;
  969. /**
  970. * @brief Helper variable template to be used to _repeat_ the same value a
  971. * number of times equal to the size of a given parameter pack.
  972. * @tparam Value A value to repeat.
  973. */
  974. template<auto Value, typename>
  975. inline constexpr auto unpack_as_v = Value;
  976. /**
  977. * @brief Wraps a static constant.
  978. * @tparam Value A static constant.
  979. */
  980. template<auto Value>
  981. using integral_constant = std::integral_constant<decltype(Value), Value>;
  982. /**
  983. * @brief Alias template to facilitate the creation of named values.
  984. * @tparam Value A constant value at least convertible to `id_type`.
  985. */
  986. template<id_type Value>
  987. using tag = integral_constant<Value>;
  988. /**
  989. * @brief A class to use to push around lists of types, nothing more.
  990. * @tparam Type Types provided by the type list.
  991. */
  992. template<typename... Type>
  993. struct type_list {
  994. /*! @brief Type list type. */
  995. using type = type_list;
  996. /*! @brief Compile-time number of elements in the type list. */
  997. static constexpr auto size = sizeof...(Type);
  998. };
  999. /*! @brief Primary template isn't defined on purpose. */
  1000. template<std::size_t, typename>
  1001. struct type_list_element;
  1002. /**
  1003. * @brief Provides compile-time indexed access to the types of a type list.
  1004. * @tparam Index Index of the type to return.
  1005. * @tparam Type First type provided by the type list.
  1006. * @tparam Other Other types provided by the type list.
  1007. */
  1008. template<std::size_t Index, typename Type, typename... Other>
  1009. struct type_list_element<Index, type_list<Type, Other...>>
  1010. : type_list_element<Index - 1u, type_list<Other...>>
  1011. {};
  1012. /**
  1013. * @brief Provides compile-time indexed access to the types of a type list.
  1014. * @tparam Type First type provided by the type list.
  1015. * @tparam Other Other types provided by the type list.
  1016. */
  1017. template<typename Type, typename... Other>
  1018. struct type_list_element<0u, type_list<Type, Other...>> {
  1019. /*! @brief Searched type. */
  1020. using type = Type;
  1021. };
  1022. /**
  1023. * @brief Helper type.
  1024. * @tparam Index Index of the type to return.
  1025. * @tparam List Type list to search into.
  1026. */
  1027. template<std::size_t Index, typename List>
  1028. using type_list_element_t = typename type_list_element<Index, List>::type;
  1029. /**
  1030. * @brief Concatenates multiple type lists.
  1031. * @tparam Type Types provided by the first type list.
  1032. * @tparam Other Types provided by the second type list.
  1033. * @return A type list composed by the types of both the type lists.
  1034. */
  1035. template<typename... Type, typename... Other>
  1036. constexpr type_list<Type..., Other...> operator+(type_list<Type...>, type_list<Other...>) { return {}; }
  1037. /*! @brief Primary template isn't defined on purpose. */
  1038. template<typename...>
  1039. struct type_list_cat;
  1040. /*! @brief Concatenates multiple type lists. */
  1041. template<>
  1042. struct type_list_cat<> {
  1043. /*! @brief A type list composed by the types of all the type lists. */
  1044. using type = type_list<>;
  1045. };
  1046. /**
  1047. * @brief Concatenates multiple type lists.
  1048. * @tparam Type Types provided by the first type list.
  1049. * @tparam Other Types provided by the second type list.
  1050. * @tparam List Other type lists, if any.
  1051. */
  1052. template<typename... Type, typename... Other, typename... List>
  1053. struct type_list_cat<type_list<Type...>, type_list<Other...>, List...> {
  1054. /*! @brief A type list composed by the types of all the type lists. */
  1055. using type = typename type_list_cat<type_list<Type..., Other...>, List...>::type;
  1056. };
  1057. /**
  1058. * @brief Concatenates multiple type lists.
  1059. * @tparam Type Types provided by the type list.
  1060. */
  1061. template<typename... Type>
  1062. struct type_list_cat<type_list<Type...>> {
  1063. /*! @brief A type list composed by the types of all the type lists. */
  1064. using type = type_list<Type...>;
  1065. };
  1066. /**
  1067. * @brief Helper type.
  1068. * @tparam List Type lists to concatenate.
  1069. */
  1070. template<typename... List>
  1071. using type_list_cat_t = typename type_list_cat<List...>::type;
  1072. /*! @brief Primary template isn't defined on purpose. */
  1073. template<typename>
  1074. struct type_list_unique;
  1075. /**
  1076. * @brief Removes duplicates types from a type list.
  1077. * @tparam Type One of the types provided by the given type list.
  1078. * @tparam Other The other types provided by the given type list.
  1079. */
  1080. template<typename Type, typename... Other>
  1081. struct type_list_unique<type_list<Type, Other...>> {
  1082. /*! @brief A type list without duplicate types. */
  1083. using type = std::conditional_t<
  1084. std::disjunction_v<std::is_same<Type, Other>...>,
  1085. typename type_list_unique<type_list<Other...>>::type,
  1086. type_list_cat_t<type_list<Type>, typename type_list_unique<type_list<Other...>>::type>
  1087. >;
  1088. };
  1089. /*! @brief Removes duplicates types from a type list. */
  1090. template<>
  1091. struct type_list_unique<type_list<>> {
  1092. /*! @brief A type list without duplicate types. */
  1093. using type = type_list<>;
  1094. };
  1095. /**
  1096. * @brief Helper type.
  1097. * @tparam Type A type list.
  1098. */
  1099. template<typename Type>
  1100. using type_list_unique_t = typename type_list_unique<Type>::type;
  1101. /**
  1102. * @brief Provides the member constant `value` to true if a type list contains a
  1103. * given type, false otherwise.
  1104. * @tparam List Type list.
  1105. * @tparam Type Type to look for.
  1106. */
  1107. template<typename List, typename Type>
  1108. struct type_list_contains;
  1109. /**
  1110. * @copybrief type_list_contains
  1111. * @tparam Type Types provided by the type list.
  1112. * @tparam Other Type to look for.
  1113. */
  1114. template<typename... Type, typename Other>
  1115. struct type_list_contains<type_list<Type...>, Other>: std::disjunction<std::is_same<Type, Other>...> {};
  1116. /**
  1117. * @brief Helper variable template.
  1118. * @tparam List Type list.
  1119. * @tparam Type Type to look for.
  1120. */
  1121. template<class List, typename Type>
  1122. inline constexpr bool type_list_contains_v = type_list_contains<List, Type>::value;
  1123. /*! @brief Primary template isn't defined on purpose. */
  1124. template<typename...>
  1125. struct type_list_diff;
  1126. /**
  1127. * @brief Computes the difference between two type lists.
  1128. * @tparam Type Types provided by the first type list.
  1129. * @tparam Other Types provided by the second type list.
  1130. */
  1131. template<typename... Type, typename... Other>
  1132. struct type_list_diff<type_list<Type...>, type_list<Other...>> {
  1133. /*! @brief A type list that is the difference between the two type lists. */
  1134. using type = type_list_cat_t<std::conditional_t<type_list_contains_v<type_list<Other...>, Type>, type_list<>, type_list<Type>>...>;
  1135. };
  1136. /**
  1137. * @brief Helper type.
  1138. * @tparam List Type lists between which to compute the difference.
  1139. */
  1140. template<typename... List>
  1141. using type_list_diff_t = typename type_list_diff<List...>::type;
  1142. /**
  1143. * @brief A class to use to push around lists of constant values, nothing more.
  1144. * @tparam Value Values provided by the value list.
  1145. */
  1146. template<auto... Value>
  1147. struct value_list {
  1148. /*! @brief Value list type. */
  1149. using type = value_list;
  1150. /*! @brief Compile-time number of elements in the value list. */
  1151. static constexpr auto size = sizeof...(Value);
  1152. };
  1153. /*! @brief Primary template isn't defined on purpose. */
  1154. template<std::size_t, typename>
  1155. struct value_list_element;
  1156. /**
  1157. * @brief Provides compile-time indexed access to the values of a value list.
  1158. * @tparam Index Index of the value to return.
  1159. * @tparam Value First value provided by the value list.
  1160. * @tparam Other Other values provided by the value list.
  1161. */
  1162. template<std::size_t Index, auto Value, auto... Other>
  1163. struct value_list_element<Index, value_list<Value, Other...>>
  1164. : value_list_element<Index - 1u, value_list<Other...>>
  1165. {};
  1166. /**
  1167. * @brief Provides compile-time indexed access to the types of a type list.
  1168. * @tparam Value First value provided by the value list.
  1169. * @tparam Other Other values provided by the value list.
  1170. */
  1171. template<auto Value, auto... Other>
  1172. struct value_list_element<0u, value_list<Value, Other...>> {
  1173. /*! @brief Searched value. */
  1174. static constexpr auto value = Value;
  1175. };
  1176. /**
  1177. * @brief Helper type.
  1178. * @tparam Index Index of the value to return.
  1179. * @tparam List Value list to search into.
  1180. */
  1181. template<std::size_t Index, typename List>
  1182. inline constexpr auto value_list_element_v = value_list_element<Index, List>::value;
  1183. /**
  1184. * @brief Concatenates multiple value lists.
  1185. * @tparam Value Values provided by the first value list.
  1186. * @tparam Other Values provided by the second value list.
  1187. * @return A value list composed by the values of both the value lists.
  1188. */
  1189. template<auto... Value, auto... Other>
  1190. constexpr value_list<Value..., Other...> operator+(value_list<Value...>, value_list<Other...>) { return {}; }
  1191. /*! @brief Primary template isn't defined on purpose. */
  1192. template<typename...>
  1193. struct value_list_cat;
  1194. /*! @brief Concatenates multiple value lists. */
  1195. template<>
  1196. struct value_list_cat<> {
  1197. /*! @brief A value list composed by the values of all the value lists. */
  1198. using type = value_list<>;
  1199. };
  1200. /**
  1201. * @brief Concatenates multiple value lists.
  1202. * @tparam Value Values provided by the first value list.
  1203. * @tparam Other Values provided by the second value list.
  1204. * @tparam List Other value lists, if any.
  1205. */
  1206. template<auto... Value, auto... Other, typename... List>
  1207. struct value_list_cat<value_list<Value...>, value_list<Other...>, List...> {
  1208. /*! @brief A value list composed by the values of all the value lists. */
  1209. using type = typename value_list_cat<value_list<Value..., Other...>, List...>::type;
  1210. };
  1211. /**
  1212. * @brief Concatenates multiple value lists.
  1213. * @tparam Value Values provided by the value list.
  1214. */
  1215. template<auto... Value>
  1216. struct value_list_cat<value_list<Value...>> {
  1217. /*! @brief A value list composed by the values of all the value lists. */
  1218. using type = value_list<Value...>;
  1219. };
  1220. /**
  1221. * @brief Helper type.
  1222. * @tparam List Value lists to concatenate.
  1223. */
  1224. template<typename... List>
  1225. using value_list_cat_t = typename value_list_cat<List...>::type;
  1226. /**
  1227. * @cond TURN_OFF_DOXYGEN
  1228. * Internal details not to be documented.
  1229. */
  1230. namespace internal {
  1231. template<typename>
  1232. [[nodiscard]] constexpr bool is_equality_comparable(...) { return false; }
  1233. template<typename Type>
  1234. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<0>)
  1235. -> decltype(std::declval<Type>() == std::declval<Type>()) { return true; }
  1236. template<typename Type>
  1237. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<1>)
  1238. -> decltype(std::declval<typename Type::value_type>(), std::declval<Type>() == std::declval<Type>()) {
  1239. if constexpr(std::is_same_v<typename Type::value_type, Type>) {
  1240. return is_equality_comparable<Type>(choice<0>);
  1241. } else {
  1242. return is_equality_comparable<typename Type::value_type>(choice<2>);
  1243. }
  1244. }
  1245. template<typename Type>
  1246. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<2>)
  1247. -> decltype(std::declval<typename Type::mapped_type>(), std::declval<Type>() == std::declval<Type>()) {
  1248. return is_equality_comparable<typename Type::key_type>(choice<2>) && is_equality_comparable<typename Type::mapped_type>(choice<2>);
  1249. }
  1250. }
  1251. /**
  1252. * Internal details not to be documented.
  1253. * @endcond
  1254. */
  1255. /**
  1256. * @brief Provides the member constant `value` to true if a given type is
  1257. * equality comparable, false otherwise.
  1258. * @tparam Type The type to test.
  1259. */
  1260. template<typename Type, typename = void>
  1261. struct is_equality_comparable: std::bool_constant<internal::is_equality_comparable<Type>(choice<2>)> {};
  1262. /**
  1263. * @brief Helper variable template.
  1264. * @tparam Type The type to test.
  1265. */
  1266. template<class Type>
  1267. inline constexpr bool is_equality_comparable_v = is_equality_comparable<Type>::value;
  1268. /*! @brief Same as std::is_invocable, but with tuples. */
  1269. template<typename, typename>
  1270. struct is_applicable: std::false_type {};
  1271. /**
  1272. * @copybrief is_applicable
  1273. * @tparam Func A valid function type.
  1274. * @tparam Tuple Tuple-like type.
  1275. * @tparam Args The list of arguments to use to probe the function type.
  1276. */
  1277. template<typename Func, template<typename...> class Tuple, typename... Args>
  1278. struct is_applicable<Func, Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  1279. /**
  1280. * @copybrief is_applicable
  1281. * @tparam Func A valid function type.
  1282. * @tparam Tuple Tuple-like type.
  1283. * @tparam Args The list of arguments to use to probe the function type.
  1284. */
  1285. template<typename Func, template<typename...> class Tuple, typename... Args>
  1286. struct is_applicable<Func, const Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  1287. /**
  1288. * @brief Helper variable template.
  1289. * @tparam Func A valid function type.
  1290. * @tparam Args The list of arguments to use to probe the function type.
  1291. */
  1292. template<typename Func, typename Args>
  1293. inline constexpr bool is_applicable_v = is_applicable<Func, Args>::value;
  1294. /*! @brief Same as std::is_invocable_r, but with tuples for arguments. */
  1295. template<typename, typename, typename>
  1296. struct is_applicable_r: std::false_type {};
  1297. /**
  1298. * @copybrief is_applicable_r
  1299. * @tparam Ret The type to which the return type of the function should be
  1300. * convertible.
  1301. * @tparam Func A valid function type.
  1302. * @tparam Args The list of arguments to use to probe the function type.
  1303. */
  1304. template<typename Ret, typename Func, typename... Args>
  1305. struct is_applicable_r<Ret, Func, std::tuple<Args...>>: std::is_invocable_r<Ret, Func, Args...> {};
  1306. /**
  1307. * @brief Helper variable template.
  1308. * @tparam Ret The type to which the return type of the function should be
  1309. * convertible.
  1310. * @tparam Func A valid function type.
  1311. * @tparam Args The list of arguments to use to probe the function type.
  1312. */
  1313. template<typename Ret, typename Func, typename Args>
  1314. inline constexpr bool is_applicable_r_v = is_applicable_r<Ret, Func, Args>::value;
  1315. /**
  1316. * @brief Provides the member constant `value` to true if a given type is
  1317. * complete, false otherwise.
  1318. * @tparam Type The type to test.
  1319. */
  1320. template<typename Type, typename = void>
  1321. struct is_complete: std::false_type {};
  1322. /*! @copydoc is_complete */
  1323. template<typename Type>
  1324. struct is_complete<Type, std::void_t<decltype(sizeof(Type))>>: std::true_type {};
  1325. /**
  1326. * @brief Helper variable template.
  1327. * @tparam Type The type to test.
  1328. */
  1329. template<typename Type>
  1330. inline constexpr bool is_complete_v = is_complete<Type>::value;
  1331. /**
  1332. * @brief Provides the member constant `value` to true if a given type is an
  1333. * iterator, false otherwise.
  1334. * @tparam Type The type to test.
  1335. */
  1336. template<typename Type, typename = void>
  1337. struct is_iterator: std::false_type {};
  1338. /*! @copydoc is_iterator */
  1339. template<typename Type>
  1340. struct is_iterator<Type, std::void_t<typename std::iterator_traits<Type>::iterator_category>>
  1341. : std::true_type
  1342. {};
  1343. /**
  1344. * @brief Helper variable template.
  1345. * @tparam Type The type to test.
  1346. */
  1347. template<typename Type>
  1348. inline constexpr bool is_iterator_v = is_iterator<Type>::value;
  1349. /**
  1350. * @brief Provides the member constant `value` to true if a given type is of the
  1351. * required iterator type, false otherwise.
  1352. * @tparam Type The type to test.
  1353. * @tparam It Required iterator type.
  1354. */
  1355. template<typename Type, typename It, typename = void>
  1356. struct is_iterator_type: std::false_type {};
  1357. /*! @copydoc is_iterator_type */
  1358. template<typename Type, typename It>
  1359. struct is_iterator_type<Type, It, std::enable_if_t<is_iterator_v<Type> && std::is_same_v<Type, It>>>
  1360. : std::true_type
  1361. {};
  1362. /*! @copydoc is_iterator_type */
  1363. template<typename Type, typename It>
  1364. struct is_iterator_type<Type, It, std::enable_if_t<!std::is_same_v<Type, It>, std::void_t<typename It::iterator_type>>>
  1365. : is_iterator_type<Type, typename It::iterator_type>
  1366. {};
  1367. /**
  1368. * @brief Helper variable template.
  1369. * @tparam Type The type to test.
  1370. * @tparam It Required iterator type.
  1371. */
  1372. template<typename Type, typename It>
  1373. inline constexpr bool is_iterator_type_v = is_iterator_type<Type, It>::value;
  1374. /**
  1375. * @brief Transcribes the constness of a type to another type.
  1376. * @tparam To The type to which to transcribe the constness.
  1377. * @tparam From The type from which to transcribe the constness.
  1378. */
  1379. template<typename To, typename From>
  1380. struct constness_as {
  1381. /*! @brief The type resulting from the transcription of the constness. */
  1382. using type = std::remove_const_t<To>;
  1383. };
  1384. /*! @copydoc constness_as */
  1385. template<typename To, typename From>
  1386. struct constness_as<To, const From> {
  1387. /*! @brief The type resulting from the transcription of the constness. */
  1388. using type = std::add_const_t<To>;
  1389. };
  1390. /**
  1391. * @brief Alias template to facilitate the transcription of the constness.
  1392. * @tparam To The type to which to transcribe the constness.
  1393. * @tparam From The type from which to transcribe the constness.
  1394. */
  1395. template<typename To, typename From>
  1396. using constness_as_t = typename constness_as<To, From>::type;
  1397. /**
  1398. * @brief Extracts the class of a non-static member object or function.
  1399. * @tparam Member A pointer to a non-static member object or function.
  1400. */
  1401. template<typename Member>
  1402. class member_class {
  1403. static_assert(std::is_member_pointer_v<Member>, "Invalid pointer type to non-static member object or function");
  1404. template<typename Class, typename Ret, typename... Args>
  1405. static Class * clazz(Ret(Class:: *)(Args...));
  1406. template<typename Class, typename Ret, typename... Args>
  1407. static Class * clazz(Ret(Class:: *)(Args...) const);
  1408. template<typename Class, typename Type>
  1409. static Class * clazz(Type Class:: *);
  1410. public:
  1411. /*! @brief The class of the given non-static member object or function. */
  1412. using type = std::remove_pointer_t<decltype(clazz(std::declval<Member>()))>;
  1413. };
  1414. /**
  1415. * @brief Helper type.
  1416. * @tparam Member A pointer to a non-static member object or function.
  1417. */
  1418. template<typename Member>
  1419. using member_class_t = typename member_class<Member>::type;
  1420. }
  1421. #endif
  1422. namespace entt {
  1423. /**
  1424. * @brief A SBO friendly, type-safe container for single values of any type.
  1425. * @tparam Len Size of the storage reserved for the small buffer optimization.
  1426. * @tparam Align Optional alignment requirement.
  1427. */
  1428. template<std::size_t Len, std::size_t Align>
  1429. class basic_any {
  1430. enum class operation: std::uint8_t { COPY, MOVE, DTOR, COMP, ADDR, CADDR, TYPE };
  1431. enum class policy: std::uint8_t { OWNER, REF, CREF };
  1432. using storage_type = std::aligned_storage_t<Len + !Len, Align>;
  1433. using vtable_type = const void *(const operation, const basic_any &, void *);
  1434. template<typename Type>
  1435. static constexpr bool in_situ = Len && alignof(Type) <= alignof(storage_type) && sizeof(Type) <= sizeof(storage_type) && std::is_nothrow_move_constructible_v<Type>;
  1436. template<typename Type>
  1437. [[nodiscard]] static constexpr policy type_to_policy() {
  1438. if constexpr(std::is_lvalue_reference_v<Type>) {
  1439. if constexpr(std::is_const_v<std::remove_reference_t<Type>>) {
  1440. return policy::CREF;
  1441. } else {
  1442. return policy::REF;
  1443. }
  1444. } else {
  1445. return policy::OWNER;
  1446. }
  1447. }
  1448. template<typename Type>
  1449. [[nodiscard]] static bool compare(const void *lhs, const void *rhs) {
  1450. if constexpr(!std::is_function_v<Type> && is_equality_comparable_v<Type>) {
  1451. return *static_cast<const Type *>(lhs) == *static_cast<const Type *>(rhs);
  1452. } else {
  1453. return lhs == rhs;
  1454. }
  1455. }
  1456. template<typename Type>
  1457. static const void * basic_vtable([[maybe_unused]] const operation op, [[maybe_unused]] const basic_any &from, [[maybe_unused]] void *to) {
  1458. static_assert(std::is_same_v<std::remove_reference_t<std::remove_const_t<Type>>, Type>, "Invalid type");
  1459. if constexpr(!std::is_void_v<Type>) {
  1460. const Type *instance = (in_situ<Type> && from.mode == policy::OWNER)
  1461. ? ENTT_LAUNDER(reinterpret_cast<const Type *>(&from.storage))
  1462. : static_cast<const Type *>(from.instance);
  1463. switch(op) {
  1464. case operation::COPY:
  1465. if constexpr(std::is_copy_constructible_v<Type>) {
  1466. static_cast<basic_any *>(to)->emplace<Type>(*instance);
  1467. }
  1468. break;
  1469. case operation::MOVE:
  1470. if constexpr(in_situ<Type>) {
  1471. if(from.mode == policy::OWNER) {
  1472. return new (&static_cast<basic_any *>(to)->storage) Type{std::move(*const_cast<Type *>(instance))};
  1473. }
  1474. }
  1475. return (static_cast<basic_any *>(to)->instance = std::exchange(const_cast<basic_any &>(from).instance, nullptr));
  1476. case operation::DTOR:
  1477. if(from.mode == policy::OWNER) {
  1478. if constexpr(in_situ<Type>) {
  1479. instance->~Type();
  1480. } else if constexpr(std::is_array_v<Type>) {
  1481. delete[] instance;
  1482. } else {
  1483. delete instance;
  1484. }
  1485. }
  1486. break;
  1487. case operation::COMP:
  1488. return compare<Type>(instance, (*static_cast<const basic_any **>(to))->data()) ? to : nullptr;
  1489. case operation::ADDR:
  1490. if(from.mode == policy::CREF) {
  1491. return nullptr;
  1492. }
  1493. [[fallthrough]];
  1494. case operation::CADDR:
  1495. return instance;
  1496. case operation::TYPE:
  1497. *static_cast<type_info *>(to) = type_id<Type>();
  1498. break;
  1499. }
  1500. }
  1501. return nullptr;
  1502. }
  1503. template<typename Type, typename... Args>
  1504. void initialize([[maybe_unused]] Args &&... args) {
  1505. if constexpr(!std::is_void_v<Type>) {
  1506. if constexpr(std::is_lvalue_reference_v<Type>) {
  1507. static_assert(sizeof...(Args) == 1u && (std::is_lvalue_reference_v<Args> && ...), "Invalid arguments");
  1508. instance = (std::addressof(args), ...);
  1509. } else if constexpr(in_situ<Type>) {
  1510. if constexpr(sizeof...(Args) != 0u && std::is_aggregate_v<Type>) {
  1511. new (&storage) Type{std::forward<Args>(args)...};
  1512. } else {
  1513. new (&storage) Type(std::forward<Args>(args)...);
  1514. }
  1515. } else {
  1516. if constexpr(sizeof...(Args) != 0u && std::is_aggregate_v<Type>) {
  1517. instance = new Type{std::forward<Args>(args)...};
  1518. } else {
  1519. instance = new Type(std::forward<Args>(args)...);
  1520. }
  1521. }
  1522. }
  1523. }
  1524. basic_any(const basic_any &other, const policy pol) ENTT_NOEXCEPT
  1525. : instance{other.data()},
  1526. vtable{other.vtable},
  1527. mode{pol}
  1528. {}
  1529. public:
  1530. /*! @brief Size of the internal storage. */
  1531. static constexpr auto length = Len;
  1532. /*! @brief Alignment requirement. */
  1533. static constexpr auto alignment = Align;
  1534. /*! @brief Default constructor. */
  1535. basic_any() ENTT_NOEXCEPT
  1536. : instance{},
  1537. vtable{&basic_vtable<void>},
  1538. mode{policy::OWNER}
  1539. {}
  1540. /**
  1541. * @brief Constructs a wrapper by directly initializing the new object.
  1542. * @tparam Type Type of object to use to initialize the wrapper.
  1543. * @tparam Args Types of arguments to use to construct the new instance.
  1544. * @param args Parameters to use to construct the instance.
  1545. */
  1546. template<typename Type, typename... Args>
  1547. explicit basic_any(std::in_place_type_t<Type>, Args &&... args)
  1548. : instance{},
  1549. vtable{&basic_vtable<std::remove_const_t<std::remove_reference_t<Type>>>},
  1550. mode{type_to_policy<Type>()}
  1551. {
  1552. initialize<Type>(std::forward<Args>(args)...);
  1553. }
  1554. /**
  1555. * @brief Constructs a wrapper that holds an unmanaged object.
  1556. * @tparam Type Type of object to use to initialize the wrapper.
  1557. * @param value An instance of an object to use to initialize the wrapper.
  1558. */
  1559. template<typename Type>
  1560. basic_any(std::reference_wrapper<Type> value) ENTT_NOEXCEPT
  1561. : basic_any{}
  1562. {
  1563. // invokes deprecated assignment operator (and avoids issues with vs2017)
  1564. *this = value;
  1565. }
  1566. /**
  1567. * @brief Constructs a wrapper from a given value.
  1568. * @tparam Type Type of object to use to initialize the wrapper.
  1569. * @param value An instance of an object to use to initialize the wrapper.
  1570. */
  1571. template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::decay_t<Type>, basic_any>>>
  1572. basic_any(Type &&value)
  1573. : instance{},
  1574. vtable{&basic_vtable<std::decay_t<Type>>},
  1575. mode{policy::OWNER}
  1576. {
  1577. initialize<std::decay_t<Type>>(std::forward<Type>(value));
  1578. }
  1579. /**
  1580. * @brief Copy constructor.
  1581. * @param other The instance to copy from.
  1582. */
  1583. basic_any(const basic_any &other)
  1584. : instance{},
  1585. vtable{&basic_vtable<void>},
  1586. mode{policy::OWNER}
  1587. {
  1588. other.vtable(operation::COPY, other, this);
  1589. }
  1590. /**
  1591. * @brief Move constructor.
  1592. * @param other The instance to move from.
  1593. */
  1594. basic_any(basic_any &&other) ENTT_NOEXCEPT
  1595. : instance{},
  1596. vtable{other.vtable},
  1597. mode{other.mode}
  1598. {
  1599. vtable(operation::MOVE, other, this);
  1600. }
  1601. /*! @brief Frees the internal storage, whatever it means. */
  1602. ~basic_any() {
  1603. vtable(operation::DTOR, *this, nullptr);
  1604. }
  1605. /**
  1606. * @brief Copy assignment operator.
  1607. * @param other The instance to copy from.
  1608. * @return This any object.
  1609. */
  1610. basic_any & operator=(const basic_any &other) {
  1611. reset();
  1612. other.vtable(operation::COPY, other, this);
  1613. return *this;
  1614. }
  1615. /**
  1616. * @brief Move assignment operator.
  1617. * @param other The instance to move from.
  1618. * @return This any object.
  1619. */
  1620. basic_any & operator=(basic_any &&other) ENTT_NOEXCEPT {
  1621. std::exchange(vtable, other.vtable)(operation::DTOR, *this, nullptr);
  1622. other.vtable(operation::MOVE, other, this);
  1623. mode = other.mode;
  1624. return *this;
  1625. }
  1626. /**
  1627. * @brief Value assignment operator.
  1628. * @tparam Type Type of object to use to initialize the wrapper.
  1629. * @param value An instance of an object to use to initialize the wrapper.
  1630. * @return This any object.
  1631. */
  1632. template<typename Type>
  1633. [[deprecated("Use std::in_place_type<T &>, entt::make_any<T &>, emplace<Type &> or forward_as_any instead")]]
  1634. basic_any & operator=(std::reference_wrapper<Type> value) ENTT_NOEXCEPT {
  1635. emplace<Type &>(value.get());
  1636. return *this;
  1637. }
  1638. /**
  1639. * @brief Value assignment operator.
  1640. * @tparam Type Type of object to use to initialize the wrapper.
  1641. * @param value An instance of an object to use to initialize the wrapper.
  1642. * @return This any object.
  1643. */
  1644. template<typename Type>
  1645. std::enable_if_t<!std::is_same_v<std::decay_t<Type>, basic_any>, basic_any &>
  1646. operator=(Type &&value) {
  1647. emplace<std::decay_t<Type>>(std::forward<Type>(value));
  1648. return *this;
  1649. }
  1650. /**
  1651. * @brief Returns the type of the contained object.
  1652. * @return The type of the contained object, if any.
  1653. */
  1654. [[nodiscard]] type_info type() const ENTT_NOEXCEPT {
  1655. type_info info{};
  1656. vtable(operation::TYPE, *this, &info);
  1657. return info;
  1658. }
  1659. /**
  1660. * @brief Returns an opaque pointer to the contained instance.
  1661. * @return An opaque pointer the contained instance, if any.
  1662. */
  1663. [[nodiscard]] const void * data() const ENTT_NOEXCEPT {
  1664. return vtable(operation::CADDR, *this, nullptr);
  1665. }
  1666. /*! @copydoc data */
  1667. [[nodiscard]] void * data() ENTT_NOEXCEPT {
  1668. return const_cast<void *>(vtable(operation::ADDR, *this, nullptr));
  1669. }
  1670. /**
  1671. * @brief Replaces the contained object by creating a new instance directly.
  1672. * @tparam Type Type of object to use to initialize the wrapper.
  1673. * @tparam Args Types of arguments to use to construct the new instance.
  1674. * @param args Parameters to use to construct the instance.
  1675. */
  1676. template<typename Type, typename... Args>
  1677. void emplace(Args &&... args) {
  1678. std::exchange(vtable, &basic_vtable<std::remove_const_t<std::remove_reference_t<Type>>>)(operation::DTOR, *this, nullptr);
  1679. mode = type_to_policy<Type>();
  1680. initialize<Type>(std::forward<Args>(args)...);
  1681. }
  1682. /*! @brief Destroys contained object */
  1683. void reset() {
  1684. std::exchange(vtable, &basic_vtable<void>)(operation::DTOR, *this, nullptr);
  1685. mode = policy::OWNER;
  1686. }
  1687. /**
  1688. * @brief Returns false if a wrapper is empty, true otherwise.
  1689. * @return False if the wrapper is empty, true otherwise.
  1690. */
  1691. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  1692. return !(vtable(operation::CADDR, *this, nullptr) == nullptr);
  1693. }
  1694. /**
  1695. * @brief Checks if two wrappers differ in their content.
  1696. * @param other Wrapper with which to compare.
  1697. * @return False if the two objects differ in their content, true otherwise.
  1698. */
  1699. bool operator==(const basic_any &other) const ENTT_NOEXCEPT {
  1700. const basic_any *trampoline = &other;
  1701. return type() == other.type() && (vtable(operation::COMP, *this, &trampoline) || !other.data());
  1702. }
  1703. /**
  1704. * @brief Aliasing constructor.
  1705. * @return A wrapper that shares a reference to an unmanaged object.
  1706. */
  1707. [[nodiscard]] basic_any as_ref() ENTT_NOEXCEPT {
  1708. return basic_any{*this, (mode == policy::CREF ? policy::CREF : policy::REF)};
  1709. }
  1710. /*! @copydoc as_ref */
  1711. [[nodiscard]] basic_any as_ref() const ENTT_NOEXCEPT {
  1712. return basic_any{*this, policy::CREF};
  1713. }
  1714. /**
  1715. * @brief Returns true if a wrapper owns its object, false otherwise.
  1716. * @return True if the wrapper owns its object, false otherwise.
  1717. */
  1718. [[nodiscard]] bool owner() const ENTT_NOEXCEPT {
  1719. return (mode == policy::OWNER);
  1720. }
  1721. private:
  1722. union { const void *instance; storage_type storage; };
  1723. vtable_type *vtable;
  1724. policy mode;
  1725. };
  1726. /**
  1727. * @brief Checks if two wrappers differ in their content.
  1728. * @tparam Len Size of the storage reserved for the small buffer optimization.
  1729. * @tparam Align Alignment requirement.
  1730. * @param lhs A wrapper, either empty or not.
  1731. * @param rhs A wrapper, either empty or not.
  1732. * @return True if the two wrappers differ in their content, false otherwise.
  1733. */
  1734. template<std::size_t Len, std::size_t Align>
  1735. [[nodiscard]] inline bool operator!=(const basic_any<Len, Align> &lhs, const basic_any<Len, Align> &rhs) ENTT_NOEXCEPT {
  1736. return !(lhs == rhs);
  1737. }
  1738. /**
  1739. * @brief Performs type-safe access to the contained object.
  1740. * @tparam Type Type to which conversion is required.
  1741. * @tparam Len Size of the storage reserved for the small buffer optimization.
  1742. * @tparam Align Alignment requirement.
  1743. * @param data Target any object.
  1744. * @return The element converted to the requested type.
  1745. */
  1746. template<typename Type, std::size_t Len, std::size_t Align>
  1747. Type any_cast(const basic_any<Len, Align> &data) ENTT_NOEXCEPT {
  1748. const auto * const instance = any_cast<std::remove_reference_t<Type>>(&data);
  1749. ENTT_ASSERT(instance, "Invalid instance");
  1750. return static_cast<Type>(*instance);
  1751. }
  1752. /*! @copydoc any_cast */
  1753. template<typename Type, std::size_t Len, std::size_t Align>
  1754. Type any_cast(basic_any<Len, Align> &data) ENTT_NOEXCEPT {
  1755. // forces const on non-reference types to make them work also with wrappers for const references
  1756. auto * const instance = any_cast<std::remove_reference_t<const Type>>(&data);
  1757. ENTT_ASSERT(instance, "Invalid instance");
  1758. return static_cast<Type>(*instance);
  1759. }
  1760. /*! @copydoc any_cast */
  1761. template<typename Type, std::size_t Len, std::size_t Align>
  1762. Type any_cast(basic_any<Len, Align> &&data) ENTT_NOEXCEPT {
  1763. // forces const on non-reference types to make them work also with wrappers for const references
  1764. auto * const instance = any_cast<std::remove_reference_t<const Type>>(&data);
  1765. ENTT_ASSERT(instance, "Invalid instance");
  1766. return static_cast<Type>(std::move(*instance));
  1767. }
  1768. /*! @copydoc any_cast */
  1769. template<typename Type, std::size_t Len, std::size_t Align>
  1770. const Type * any_cast(const basic_any<Len, Align> *data) ENTT_NOEXCEPT {
  1771. return (data->type() == type_id<Type>() ? static_cast<const Type *>(data->data()) : nullptr);
  1772. }
  1773. /*! @copydoc any_cast */
  1774. template<typename Type, std::size_t Len, std::size_t Align>
  1775. Type * any_cast(basic_any<Len, Align> *data) ENTT_NOEXCEPT {
  1776. // last attempt to make wrappers for const references return their values
  1777. return (data->type() == type_id<Type>() ? static_cast<Type *>(static_cast<constness_as_t<basic_any<Len, Align>, Type> *>(data)->data()) : nullptr);
  1778. }
  1779. /**
  1780. * @brief Constructs a wrapper from a given type, passing it all arguments.
  1781. * @tparam Type Type of object to use to initialize the wrapper.
  1782. * @tparam Len Size of the storage reserved for the small buffer optimization.
  1783. * @tparam Align Optional alignment requirement.
  1784. * @tparam Args Types of arguments to use to construct the new instance.
  1785. * @param args Parameters to use to construct the instance.
  1786. * @return A properly initialized wrapper for an object of the given type.
  1787. */
  1788. template<typename Type, std::size_t Len = basic_any<>::length, std::size_t Align = basic_any<Len>::alignment, typename... Args>
  1789. basic_any<Len, Align> make_any(Args &&... args) {
  1790. return basic_any<Len, Align>{std::in_place_type<Type>, std::forward<Args>(args)...};
  1791. }
  1792. /**
  1793. * @brief Forwards its argument and avoids copies for lvalue references.
  1794. * @tparam Len Size of the storage reserved for the small buffer optimization.
  1795. * @tparam Align Optional alignment requirement.
  1796. * @tparam Type Type of argument to use to construct the new instance.
  1797. * @param value Parameter to use to construct the instance.
  1798. * @return A properly initialized and not necessarily owning wrapper.
  1799. */
  1800. template<std::size_t Len = basic_any<>::length, std::size_t Align = basic_any<Len>::alignment, typename Type>
  1801. basic_any<Len, Align> forward_as_any(Type &&value) {
  1802. return basic_any<Len, Align>{std::in_place_type<std::conditional_t<std::is_rvalue_reference_v<Type>, std::decay_t<Type>, Type>>, std::forward<Type>(value)};
  1803. }
  1804. }
  1805. #endif
  1806. // #include "core/attribute.h"
  1807. #ifndef ENTT_CORE_ATTRIBUTE_H
  1808. #define ENTT_CORE_ATTRIBUTE_H
  1809. #ifndef ENTT_EXPORT
  1810. # if defined _WIN32 || defined __CYGWIN__ || defined _MSC_VER
  1811. # define ENTT_EXPORT __declspec(dllexport)
  1812. # define ENTT_IMPORT __declspec(dllimport)
  1813. # define ENTT_HIDDEN
  1814. # elif defined __GNUC__ && __GNUC__ >= 4
  1815. # define ENTT_EXPORT __attribute__((visibility("default")))
  1816. # define ENTT_IMPORT __attribute__((visibility("default")))
  1817. # define ENTT_HIDDEN __attribute__((visibility("hidden")))
  1818. # else /* Unsupported compiler */
  1819. # define ENTT_EXPORT
  1820. # define ENTT_IMPORT
  1821. # define ENTT_HIDDEN
  1822. # endif
  1823. #endif
  1824. #ifndef ENTT_API
  1825. # if defined ENTT_API_EXPORT
  1826. # define ENTT_API ENTT_EXPORT
  1827. # elif defined ENTT_API_IMPORT
  1828. # define ENTT_API ENTT_IMPORT
  1829. # else /* No API */
  1830. # define ENTT_API
  1831. # endif
  1832. #endif
  1833. #endif
  1834. // #include "core/family.hpp"
  1835. #ifndef ENTT_CORE_FAMILY_HPP
  1836. #define ENTT_CORE_FAMILY_HPP
  1837. // #include "../config/config.h"
  1838. // #include "fwd.hpp"
  1839. namespace entt {
  1840. /**
  1841. * @brief Dynamic identifier generator.
  1842. *
  1843. * Utility class template that can be used to assign unique identifiers to types
  1844. * at runtime. Use different specializations to create separate sets of
  1845. * identifiers.
  1846. */
  1847. template<typename...>
  1848. class family {
  1849. inline static ENTT_MAYBE_ATOMIC(id_type) identifier{};
  1850. public:
  1851. /*! @brief Unsigned integer type. */
  1852. using family_type = id_type;
  1853. /*! @brief Statically generated unique identifier for the given type. */
  1854. template<typename... Type>
  1855. // at the time I'm writing, clang crashes during compilation if auto is used instead of family_type
  1856. inline static const family_type type = identifier++;
  1857. };
  1858. }
  1859. #endif
  1860. // #include "core/hashed_string.hpp"
  1861. #ifndef ENTT_CORE_HASHED_STRING_HPP
  1862. #define ENTT_CORE_HASHED_STRING_HPP
  1863. #include <cstddef>
  1864. #include <cstdint>
  1865. // #include "../config/config.h"
  1866. // #include "fwd.hpp"
  1867. namespace entt {
  1868. /**
  1869. * @cond TURN_OFF_DOXYGEN
  1870. * Internal details not to be documented.
  1871. */
  1872. namespace internal {
  1873. template<typename>
  1874. struct fnv1a_traits;
  1875. template<>
  1876. struct fnv1a_traits<std::uint32_t> {
  1877. using type = std::uint32_t;
  1878. static constexpr std::uint32_t offset = 2166136261;
  1879. static constexpr std::uint32_t prime = 16777619;
  1880. };
  1881. template<>
  1882. struct fnv1a_traits<std::uint64_t> {
  1883. using type = std::uint64_t;
  1884. static constexpr std::uint64_t offset = 14695981039346656037ull;
  1885. static constexpr std::uint64_t prime = 1099511628211ull;
  1886. };
  1887. }
  1888. /**
  1889. * Internal details not to be documented.
  1890. * @endcond
  1891. */
  1892. /**
  1893. * @brief Zero overhead unique identifier.
  1894. *
  1895. * A hashed string is a compile-time tool that allows users to use
  1896. * human-readable identifers in the codebase while using their numeric
  1897. * counterparts at runtime.<br/>
  1898. * Because of that, a hashed string can also be used in constant expressions if
  1899. * required.
  1900. *
  1901. * @tparam Char Character type.
  1902. */
  1903. template<typename Char>
  1904. class basic_hashed_string {
  1905. using traits_type = internal::fnv1a_traits<id_type>;
  1906. struct const_wrapper {
  1907. // non-explicit constructor on purpose
  1908. constexpr const_wrapper(const Char *curr) ENTT_NOEXCEPT: str{curr} {}
  1909. const Char *str;
  1910. };
  1911. // Fowler–Noll–Vo hash function v. 1a - the good
  1912. [[nodiscard]] static constexpr id_type helper(const Char *curr) ENTT_NOEXCEPT {
  1913. auto value = traits_type::offset;
  1914. while(*curr != 0) {
  1915. value = (value ^ static_cast<traits_type::type>(*(curr++))) * traits_type::prime;
  1916. }
  1917. return value;
  1918. }
  1919. public:
  1920. /*! @brief Character type. */
  1921. using value_type = Char;
  1922. /*! @brief Unsigned integer type. */
  1923. using hash_type = id_type;
  1924. /**
  1925. * @brief Returns directly the numeric representation of a string view.
  1926. * @param str Human-readable identifer.
  1927. * @param size Length of the string to hash.
  1928. * @return The numeric representation of the string.
  1929. */
  1930. [[nodiscard]] static constexpr hash_type value(const value_type *str, std::size_t size) ENTT_NOEXCEPT {
  1931. id_type partial{traits_type::offset};
  1932. while(size--) { partial = (partial^(str++)[0])*traits_type::prime; }
  1933. return partial;
  1934. }
  1935. /**
  1936. * @brief Returns directly the numeric representation of a string.
  1937. *
  1938. * Forcing template resolution avoids implicit conversions. An
  1939. * human-readable identifier can be anything but a plain, old bunch of
  1940. * characters.<br/>
  1941. * Example of use:
  1942. * @code{.cpp}
  1943. * const auto value = basic_hashed_string<char>::to_value("my.png");
  1944. * @endcode
  1945. *
  1946. * @tparam N Number of characters of the identifier.
  1947. * @param str Human-readable identifer.
  1948. * @return The numeric representation of the string.
  1949. */
  1950. template<std::size_t N>
  1951. [[nodiscard]] static constexpr hash_type value(const value_type (&str)[N]) ENTT_NOEXCEPT {
  1952. return helper(str);
  1953. }
  1954. /**
  1955. * @brief Returns directly the numeric representation of a string.
  1956. * @param wrapper Helps achieving the purpose by relying on overloading.
  1957. * @return The numeric representation of the string.
  1958. */
  1959. [[nodiscard]] static hash_type value(const_wrapper wrapper) ENTT_NOEXCEPT {
  1960. return helper(wrapper.str);
  1961. }
  1962. /*! @brief Constructs an empty hashed string. */
  1963. constexpr basic_hashed_string() ENTT_NOEXCEPT
  1964. : str{nullptr}, hash{}
  1965. {}
  1966. /**
  1967. * @brief Constructs a hashed string from an array of const characters.
  1968. *
  1969. * Forcing template resolution avoids implicit conversions. An
  1970. * human-readable identifier can be anything but a plain, old bunch of
  1971. * characters.<br/>
  1972. * Example of use:
  1973. * @code{.cpp}
  1974. * basic_hashed_string<char> hs{"my.png"};
  1975. * @endcode
  1976. *
  1977. * @tparam N Number of characters of the identifier.
  1978. * @param curr Human-readable identifer.
  1979. */
  1980. template<std::size_t N>
  1981. constexpr basic_hashed_string(const value_type (&curr)[N]) ENTT_NOEXCEPT
  1982. : str{curr}, hash{helper(curr)}
  1983. {}
  1984. /**
  1985. * @brief Explicit constructor on purpose to avoid constructing a hashed
  1986. * string directly from a `const value_type *`.
  1987. * @param wrapper Helps achieving the purpose by relying on overloading.
  1988. */
  1989. explicit constexpr basic_hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
  1990. : str{wrapper.str}, hash{helper(wrapper.str)}
  1991. {}
  1992. /**
  1993. * @brief Returns the human-readable representation of a hashed string.
  1994. * @return The string used to initialize the instance.
  1995. */
  1996. [[nodiscard]] constexpr const value_type * data() const ENTT_NOEXCEPT {
  1997. return str;
  1998. }
  1999. /**
  2000. * @brief Returns the numeric representation of a hashed string.
  2001. * @return The numeric representation of the instance.
  2002. */
  2003. [[nodiscard]] constexpr hash_type value() const ENTT_NOEXCEPT {
  2004. return hash;
  2005. }
  2006. /*! @copydoc data */
  2007. [[nodiscard]] constexpr operator const value_type *() const ENTT_NOEXCEPT { return data(); }
  2008. /**
  2009. * @brief Returns the numeric representation of a hashed string.
  2010. * @return The numeric representation of the instance.
  2011. */
  2012. [[nodiscard]] constexpr operator hash_type() const ENTT_NOEXCEPT { return value(); }
  2013. /**
  2014. * @brief Compares two hashed strings.
  2015. * @param other Hashed string with which to compare.
  2016. * @return True if the two hashed strings are identical, false otherwise.
  2017. */
  2018. [[nodiscard]] constexpr bool operator==(const basic_hashed_string &other) const ENTT_NOEXCEPT {
  2019. return hash == other.hash;
  2020. }
  2021. private:
  2022. const value_type *str;
  2023. hash_type hash;
  2024. };
  2025. /**
  2026. * @brief Deduction guide.
  2027. *
  2028. * It allows to deduce the character type of the hashed string directly from a
  2029. * human-readable identifer provided to the constructor.
  2030. *
  2031. * @tparam Char Character type.
  2032. * @tparam N Number of characters of the identifier.
  2033. * @param str Human-readable identifer.
  2034. */
  2035. template<typename Char, std::size_t N>
  2036. basic_hashed_string(const Char (&str)[N])
  2037. -> basic_hashed_string<Char>;
  2038. /**
  2039. * @brief Compares two hashed strings.
  2040. * @tparam Char Character type.
  2041. * @param lhs A valid hashed string.
  2042. * @param rhs A valid hashed string.
  2043. * @return True if the two hashed strings are identical, false otherwise.
  2044. */
  2045. template<typename Char>
  2046. [[nodiscard]] constexpr bool operator!=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) ENTT_NOEXCEPT {
  2047. return !(lhs == rhs);
  2048. }
  2049. /*! @brief Aliases for common character types. */
  2050. using hashed_string = basic_hashed_string<char>;
  2051. /*! @brief Aliases for common character types. */
  2052. using hashed_wstring = basic_hashed_string<wchar_t>;
  2053. inline namespace literals {
  2054. /**
  2055. * @brief User defined literal for hashed strings.
  2056. * @param str The literal without its suffix.
  2057. * @return A properly initialized hashed string.
  2058. */
  2059. [[nodiscard]] constexpr entt::hashed_string operator"" _hs(const char *str, std::size_t) ENTT_NOEXCEPT {
  2060. return entt::hashed_string{str};
  2061. }
  2062. /**
  2063. * @brief User defined literal for hashed wstrings.
  2064. * @param str The literal without its suffix.
  2065. * @return A properly initialized hashed wstring.
  2066. */
  2067. [[nodiscard]] constexpr entt::hashed_wstring operator"" _hws(const wchar_t *str, std::size_t) ENTT_NOEXCEPT {
  2068. return entt::hashed_wstring{str};
  2069. }
  2070. }
  2071. }
  2072. #endif
  2073. // #include "core/ident.hpp"
  2074. #ifndef ENTT_CORE_IDENT_HPP
  2075. #define ENTT_CORE_IDENT_HPP
  2076. #include <cstddef>
  2077. #include <utility>
  2078. #include <type_traits>
  2079. // #include "../config/config.h"
  2080. // #include "fwd.hpp"
  2081. // #include "type_traits.hpp"
  2082. namespace entt {
  2083. /**
  2084. * @brief Types identifiers.
  2085. *
  2086. * Variable template used to generate identifiers at compile-time for the given
  2087. * types. Use the `get` member function to know what's the identifier associated
  2088. * to the specific type.
  2089. *
  2090. * @note
  2091. * Identifiers are constant expression and can be used in any context where such
  2092. * an expression is required. As an example:
  2093. * @code{.cpp}
  2094. * using id = entt::identifier<a_type, another_type>;
  2095. *
  2096. * switch(a_type_identifier) {
  2097. * case id::type<a_type>:
  2098. * // ...
  2099. * break;
  2100. * case id::type<another_type>:
  2101. * // ...
  2102. * break;
  2103. * default:
  2104. * // ...
  2105. * }
  2106. * @endcode
  2107. *
  2108. * @tparam Types List of types for which to generate identifiers.
  2109. */
  2110. template<typename... Types>
  2111. class identifier {
  2112. template<typename Type, std::size_t... Index>
  2113. [[nodiscard]] static constexpr id_type get(std::index_sequence<Index...>) {
  2114. static_assert(std::disjunction_v<std::is_same<Type, Types>...>, "Invalid type");
  2115. return (0 + ... + (std::is_same_v<Type, type_list_element_t<Index, type_list<std::decay_t<Types>...>>> ? id_type{Index} : id_type{}));
  2116. }
  2117. public:
  2118. /*! @brief Unsigned integer type. */
  2119. using identifier_type = id_type;
  2120. /*! @brief Statically generated unique identifier for the given type. */
  2121. template<typename Type>
  2122. static constexpr identifier_type type = get<std::decay_t<Type>>(std::index_sequence_for<Types...>{});
  2123. };
  2124. }
  2125. #endif
  2126. // #include "core/monostate.hpp"
  2127. #ifndef ENTT_CORE_MONOSTATE_HPP
  2128. #define ENTT_CORE_MONOSTATE_HPP
  2129. // #include "../config/config.h"
  2130. // #include "fwd.hpp"
  2131. namespace entt {
  2132. /**
  2133. * @brief Minimal implementation of the monostate pattern.
  2134. *
  2135. * A minimal, yet complete configuration system built on top of the monostate
  2136. * pattern. Thread safe by design, it works only with basic types like `int`s or
  2137. * `bool`s.<br/>
  2138. * Multiple types and therefore more than one value can be associated with a
  2139. * single key. Because of this, users must pay attention to use the same type
  2140. * both during an assignment and when they try to read back their data.
  2141. * Otherwise, they can incur in unexpected results.
  2142. */
  2143. template<id_type>
  2144. struct monostate {
  2145. /**
  2146. * @brief Assigns a value of a specific type to a given key.
  2147. * @tparam Type Type of the value to assign.
  2148. * @param val User data to assign to the given key.
  2149. */
  2150. template<typename Type>
  2151. void operator=(Type val) const ENTT_NOEXCEPT {
  2152. value<Type> = val;
  2153. }
  2154. /**
  2155. * @brief Gets a value of a specific type for a given key.
  2156. * @tparam Type Type of the value to get.
  2157. * @return Stored value, if any.
  2158. */
  2159. template<typename Type>
  2160. operator Type() const ENTT_NOEXCEPT {
  2161. return value<Type>;
  2162. }
  2163. private:
  2164. template<typename Type>
  2165. inline static ENTT_MAYBE_ATOMIC(Type) value{};
  2166. };
  2167. /**
  2168. * @brief Helper variable template.
  2169. * @tparam Value Value used to differentiate between different variables.
  2170. */
  2171. template<id_type Value>
  2172. inline monostate<Value> monostate_v = {};
  2173. }
  2174. #endif
  2175. // #include "core/type_info.hpp"
  2176. #ifndef ENTT_CORE_TYPE_INFO_HPP
  2177. #define ENTT_CORE_TYPE_INFO_HPP
  2178. #include <string_view>
  2179. #include <type_traits>
  2180. // #include "../config/config.h"
  2181. // #include "../core/attribute.h"
  2182. // #include "hashed_string.hpp"
  2183. // #include "fwd.hpp"
  2184. namespace entt {
  2185. /**
  2186. * @cond TURN_OFF_DOXYGEN
  2187. * Internal details not to be documented.
  2188. */
  2189. namespace internal {
  2190. struct ENTT_API type_seq final {
  2191. [[nodiscard]] static id_type next() ENTT_NOEXCEPT {
  2192. static ENTT_MAYBE_ATOMIC(id_type) value{};
  2193. return value++;
  2194. }
  2195. };
  2196. template<typename Type>
  2197. [[nodiscard]] constexpr auto stripped_type_name() ENTT_NOEXCEPT {
  2198. #if defined ENTT_PRETTY_FUNCTION
  2199. std::string_view pretty_function{ENTT_PRETTY_FUNCTION};
  2200. auto first = pretty_function.find_first_not_of(' ', pretty_function.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX)+1);
  2201. auto value = pretty_function.substr(first, pretty_function.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
  2202. return value;
  2203. #else
  2204. return std::string_view{""};
  2205. #endif
  2206. }
  2207. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  2208. [[nodiscard]] static constexpr std::string_view type_name(int) ENTT_NOEXCEPT {
  2209. constexpr auto value = stripped_type_name<Type>();
  2210. return value;
  2211. }
  2212. template<typename Type>
  2213. [[nodiscard]] static std::string_view type_name(char) ENTT_NOEXCEPT {
  2214. static const auto value = stripped_type_name<Type>();
  2215. return value;
  2216. }
  2217. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  2218. [[nodiscard]] static constexpr id_type type_hash(int) ENTT_NOEXCEPT {
  2219. constexpr auto stripped = stripped_type_name<Type>();
  2220. constexpr auto value = hashed_string::value(stripped.data(), stripped.size());
  2221. return value;
  2222. }
  2223. template<typename Type>
  2224. [[nodiscard]] static id_type type_hash(char) ENTT_NOEXCEPT {
  2225. static const auto value = [](const auto stripped) {
  2226. return hashed_string::value(stripped.data(), stripped.size());
  2227. }(stripped_type_name<Type>());
  2228. return value;
  2229. }
  2230. }
  2231. /**
  2232. * Internal details not to be documented.
  2233. * @endcond
  2234. */
  2235. /**
  2236. * @brief Type sequential identifier.
  2237. * @tparam Type Type for which to generate a sequential identifier.
  2238. */
  2239. template<typename Type, typename = void>
  2240. struct ENTT_API type_seq final {
  2241. /**
  2242. * @brief Returns the sequential identifier of a given type.
  2243. * @return The sequential identifier of a given type.
  2244. */
  2245. [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
  2246. static const id_type value = internal::type_seq::next();
  2247. return value;
  2248. }
  2249. /*! @copydoc value */
  2250. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  2251. };
  2252. /**
  2253. * @brief Type hash.
  2254. * @tparam Type Type for which to generate a hash value.
  2255. */
  2256. template<typename Type, typename = void>
  2257. struct type_hash final {
  2258. /**
  2259. * @brief Returns the numeric representation of a given type.
  2260. * @return The numeric representation of the given type.
  2261. */
  2262. #if defined ENTT_PRETTY_FUNCTION
  2263. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  2264. return internal::type_hash<Type>(0);
  2265. #else
  2266. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  2267. return type_seq<Type>::value();
  2268. #endif
  2269. }
  2270. /*! @copydoc value */
  2271. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  2272. };
  2273. /**
  2274. * @brief Type name.
  2275. * @tparam Type Type for which to generate a name.
  2276. */
  2277. template<typename Type, typename = void>
  2278. struct type_name final {
  2279. /**
  2280. * @brief Returns the name of a given type.
  2281. * @return The name of the given type.
  2282. */
  2283. [[nodiscard]] static constexpr std::string_view value() ENTT_NOEXCEPT {
  2284. return internal::type_name<Type>(0);
  2285. }
  2286. /*! @copydoc value */
  2287. [[nodiscard]] constexpr operator std::string_view() const ENTT_NOEXCEPT { return value(); }
  2288. };
  2289. /*! @brief Implementation specific information about a type. */
  2290. class type_info final {
  2291. template<typename>
  2292. friend type_info type_id() ENTT_NOEXCEPT;
  2293. type_info(id_type seq_v, id_type hash_v, std::string_view name_v) ENTT_NOEXCEPT
  2294. : seq_value{seq_v},
  2295. hash_value{hash_v},
  2296. name_value{name_v}
  2297. {}
  2298. public:
  2299. /*! @brief Default constructor. */
  2300. type_info() ENTT_NOEXCEPT
  2301. : type_info({}, {}, {})
  2302. {}
  2303. /*! @brief Default copy constructor. */
  2304. type_info(const type_info &) ENTT_NOEXCEPT = default;
  2305. /*! @brief Default move constructor. */
  2306. type_info(type_info &&) ENTT_NOEXCEPT = default;
  2307. /**
  2308. * @brief Default copy assignment operator.
  2309. * @return This type info object.
  2310. */
  2311. type_info & operator=(const type_info &) ENTT_NOEXCEPT = default;
  2312. /**
  2313. * @brief Default move assignment operator.
  2314. * @return This type info object.
  2315. */
  2316. type_info & operator=(type_info &&) ENTT_NOEXCEPT = default;
  2317. /**
  2318. * @brief Checks if a type info object is properly initialized.
  2319. * @return True if the object is properly initialized, false otherwise.
  2320. */
  2321. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  2322. return name_value.data() != nullptr;
  2323. }
  2324. /**
  2325. * @brief Type sequential identifier.
  2326. * @return Type sequential identifier.
  2327. */
  2328. [[nodiscard]] id_type seq() const ENTT_NOEXCEPT {
  2329. return seq_value;
  2330. }
  2331. /**
  2332. * @brief Type hash.
  2333. * @return Type hash.
  2334. */
  2335. [[nodiscard]] id_type hash() const ENTT_NOEXCEPT {
  2336. return hash_value;
  2337. }
  2338. /**
  2339. * @brief Type name.
  2340. * @return Type name.
  2341. */
  2342. [[nodiscard]] std::string_view name() const ENTT_NOEXCEPT {
  2343. return name_value;
  2344. }
  2345. /**
  2346. * @brief Compares the contents of two type info objects.
  2347. * @param other Object with which to compare.
  2348. * @return False if the two contents differ, true otherwise.
  2349. */
  2350. [[nodiscard]] bool operator==(const type_info &other) const ENTT_NOEXCEPT {
  2351. return hash_value == other.hash_value;
  2352. }
  2353. private:
  2354. id_type seq_value;
  2355. id_type hash_value;
  2356. std::string_view name_value;
  2357. };
  2358. /**
  2359. * @brief Compares the contents of two type info objects.
  2360. * @param lhs A type info object.
  2361. * @param rhs A type info object.
  2362. * @return True if the two contents differ, false otherwise.
  2363. */
  2364. [[nodiscard]] inline bool operator!=(const type_info &lhs, const type_info &rhs) ENTT_NOEXCEPT {
  2365. return !(lhs == rhs);
  2366. }
  2367. /**
  2368. * @brief Returns the type info object for a given type.
  2369. * @tparam Type Type for which to generate a type info object.
  2370. * @return The type info object for the given type.
  2371. */
  2372. template<typename Type>
  2373. [[nodiscard]] type_info type_id() ENTT_NOEXCEPT {
  2374. return type_info{
  2375. type_seq<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  2376. type_hash<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  2377. type_name<std::remove_cv_t<std::remove_reference_t<Type>>>::value()
  2378. };
  2379. }
  2380. }
  2381. #endif
  2382. // #include "core/type_traits.hpp"
  2383. #ifndef ENTT_CORE_TYPE_TRAITS_HPP
  2384. #define ENTT_CORE_TYPE_TRAITS_HPP
  2385. #include <cstddef>
  2386. #include <iterator>
  2387. #include <type_traits>
  2388. #include <utility>
  2389. // #include "../config/config.h"
  2390. // #include "fwd.hpp"
  2391. namespace entt {
  2392. /**
  2393. * @brief Utility class to disambiguate overloaded functions.
  2394. * @tparam N Number of choices available.
  2395. */
  2396. template<std::size_t N>
  2397. struct choice_t
  2398. // Unfortunately, doxygen cannot parse such a construct.
  2399. /*! @cond TURN_OFF_DOXYGEN */
  2400. : choice_t<N-1>
  2401. /*! @endcond */
  2402. {};
  2403. /*! @copybrief choice_t */
  2404. template<>
  2405. struct choice_t<0> {};
  2406. /**
  2407. * @brief Variable template for the choice trick.
  2408. * @tparam N Number of choices available.
  2409. */
  2410. template<std::size_t N>
  2411. inline constexpr choice_t<N> choice{};
  2412. /**
  2413. * @brief Identity type trait.
  2414. *
  2415. * Useful to establish non-deduced contexts in template argument deduction
  2416. * (waiting for C++20) or to provide types through function arguments.
  2417. *
  2418. * @tparam Type A type.
  2419. */
  2420. template<typename Type>
  2421. struct type_identity {
  2422. /*! @brief Identity type. */
  2423. using type = Type;
  2424. };
  2425. /**
  2426. * @brief Helper type.
  2427. * @tparam Type A type.
  2428. */
  2429. template<typename Type>
  2430. using type_identity_t = typename type_identity<Type>::type;
  2431. /**
  2432. * @brief A type-only `sizeof` wrapper that returns 0 where `sizeof` complains.
  2433. * @tparam Type The type of which to return the size.
  2434. * @tparam The size of the type if `sizeof` accepts it, 0 otherwise.
  2435. */
  2436. template<typename Type, typename = void>
  2437. struct size_of: std::integral_constant<std::size_t, 0u> {};
  2438. /*! @copydoc size_of */
  2439. template<typename Type>
  2440. struct size_of<Type, std::void_t<decltype(sizeof(Type))>>
  2441. : std::integral_constant<std::size_t, sizeof(Type)>
  2442. {};
  2443. /**
  2444. * @brief Helper variable template.
  2445. * @tparam Type The type of which to return the size.
  2446. */
  2447. template<class Type>
  2448. inline constexpr std::size_t size_of_v = size_of<Type>::value;
  2449. /**
  2450. * @brief Using declaration to be used to _repeat_ the same type a number of
  2451. * times equal to the size of a given parameter pack.
  2452. * @tparam Type A type to repeat.
  2453. */
  2454. template<typename Type, typename>
  2455. using unpack_as_t = Type;
  2456. /**
  2457. * @brief Helper variable template to be used to _repeat_ the same value a
  2458. * number of times equal to the size of a given parameter pack.
  2459. * @tparam Value A value to repeat.
  2460. */
  2461. template<auto Value, typename>
  2462. inline constexpr auto unpack_as_v = Value;
  2463. /**
  2464. * @brief Wraps a static constant.
  2465. * @tparam Value A static constant.
  2466. */
  2467. template<auto Value>
  2468. using integral_constant = std::integral_constant<decltype(Value), Value>;
  2469. /**
  2470. * @brief Alias template to facilitate the creation of named values.
  2471. * @tparam Value A constant value at least convertible to `id_type`.
  2472. */
  2473. template<id_type Value>
  2474. using tag = integral_constant<Value>;
  2475. /**
  2476. * @brief A class to use to push around lists of types, nothing more.
  2477. * @tparam Type Types provided by the type list.
  2478. */
  2479. template<typename... Type>
  2480. struct type_list {
  2481. /*! @brief Type list type. */
  2482. using type = type_list;
  2483. /*! @brief Compile-time number of elements in the type list. */
  2484. static constexpr auto size = sizeof...(Type);
  2485. };
  2486. /*! @brief Primary template isn't defined on purpose. */
  2487. template<std::size_t, typename>
  2488. struct type_list_element;
  2489. /**
  2490. * @brief Provides compile-time indexed access to the types of a type list.
  2491. * @tparam Index Index of the type to return.
  2492. * @tparam Type First type provided by the type list.
  2493. * @tparam Other Other types provided by the type list.
  2494. */
  2495. template<std::size_t Index, typename Type, typename... Other>
  2496. struct type_list_element<Index, type_list<Type, Other...>>
  2497. : type_list_element<Index - 1u, type_list<Other...>>
  2498. {};
  2499. /**
  2500. * @brief Provides compile-time indexed access to the types of a type list.
  2501. * @tparam Type First type provided by the type list.
  2502. * @tparam Other Other types provided by the type list.
  2503. */
  2504. template<typename Type, typename... Other>
  2505. struct type_list_element<0u, type_list<Type, Other...>> {
  2506. /*! @brief Searched type. */
  2507. using type = Type;
  2508. };
  2509. /**
  2510. * @brief Helper type.
  2511. * @tparam Index Index of the type to return.
  2512. * @tparam List Type list to search into.
  2513. */
  2514. template<std::size_t Index, typename List>
  2515. using type_list_element_t = typename type_list_element<Index, List>::type;
  2516. /**
  2517. * @brief Concatenates multiple type lists.
  2518. * @tparam Type Types provided by the first type list.
  2519. * @tparam Other Types provided by the second type list.
  2520. * @return A type list composed by the types of both the type lists.
  2521. */
  2522. template<typename... Type, typename... Other>
  2523. constexpr type_list<Type..., Other...> operator+(type_list<Type...>, type_list<Other...>) { return {}; }
  2524. /*! @brief Primary template isn't defined on purpose. */
  2525. template<typename...>
  2526. struct type_list_cat;
  2527. /*! @brief Concatenates multiple type lists. */
  2528. template<>
  2529. struct type_list_cat<> {
  2530. /*! @brief A type list composed by the types of all the type lists. */
  2531. using type = type_list<>;
  2532. };
  2533. /**
  2534. * @brief Concatenates multiple type lists.
  2535. * @tparam Type Types provided by the first type list.
  2536. * @tparam Other Types provided by the second type list.
  2537. * @tparam List Other type lists, if any.
  2538. */
  2539. template<typename... Type, typename... Other, typename... List>
  2540. struct type_list_cat<type_list<Type...>, type_list<Other...>, List...> {
  2541. /*! @brief A type list composed by the types of all the type lists. */
  2542. using type = typename type_list_cat<type_list<Type..., Other...>, List...>::type;
  2543. };
  2544. /**
  2545. * @brief Concatenates multiple type lists.
  2546. * @tparam Type Types provided by the type list.
  2547. */
  2548. template<typename... Type>
  2549. struct type_list_cat<type_list<Type...>> {
  2550. /*! @brief A type list composed by the types of all the type lists. */
  2551. using type = type_list<Type...>;
  2552. };
  2553. /**
  2554. * @brief Helper type.
  2555. * @tparam List Type lists to concatenate.
  2556. */
  2557. template<typename... List>
  2558. using type_list_cat_t = typename type_list_cat<List...>::type;
  2559. /*! @brief Primary template isn't defined on purpose. */
  2560. template<typename>
  2561. struct type_list_unique;
  2562. /**
  2563. * @brief Removes duplicates types from a type list.
  2564. * @tparam Type One of the types provided by the given type list.
  2565. * @tparam Other The other types provided by the given type list.
  2566. */
  2567. template<typename Type, typename... Other>
  2568. struct type_list_unique<type_list<Type, Other...>> {
  2569. /*! @brief A type list without duplicate types. */
  2570. using type = std::conditional_t<
  2571. std::disjunction_v<std::is_same<Type, Other>...>,
  2572. typename type_list_unique<type_list<Other...>>::type,
  2573. type_list_cat_t<type_list<Type>, typename type_list_unique<type_list<Other...>>::type>
  2574. >;
  2575. };
  2576. /*! @brief Removes duplicates types from a type list. */
  2577. template<>
  2578. struct type_list_unique<type_list<>> {
  2579. /*! @brief A type list without duplicate types. */
  2580. using type = type_list<>;
  2581. };
  2582. /**
  2583. * @brief Helper type.
  2584. * @tparam Type A type list.
  2585. */
  2586. template<typename Type>
  2587. using type_list_unique_t = typename type_list_unique<Type>::type;
  2588. /**
  2589. * @brief Provides the member constant `value` to true if a type list contains a
  2590. * given type, false otherwise.
  2591. * @tparam List Type list.
  2592. * @tparam Type Type to look for.
  2593. */
  2594. template<typename List, typename Type>
  2595. struct type_list_contains;
  2596. /**
  2597. * @copybrief type_list_contains
  2598. * @tparam Type Types provided by the type list.
  2599. * @tparam Other Type to look for.
  2600. */
  2601. template<typename... Type, typename Other>
  2602. struct type_list_contains<type_list<Type...>, Other>: std::disjunction<std::is_same<Type, Other>...> {};
  2603. /**
  2604. * @brief Helper variable template.
  2605. * @tparam List Type list.
  2606. * @tparam Type Type to look for.
  2607. */
  2608. template<class List, typename Type>
  2609. inline constexpr bool type_list_contains_v = type_list_contains<List, Type>::value;
  2610. /*! @brief Primary template isn't defined on purpose. */
  2611. template<typename...>
  2612. struct type_list_diff;
  2613. /**
  2614. * @brief Computes the difference between two type lists.
  2615. * @tparam Type Types provided by the first type list.
  2616. * @tparam Other Types provided by the second type list.
  2617. */
  2618. template<typename... Type, typename... Other>
  2619. struct type_list_diff<type_list<Type...>, type_list<Other...>> {
  2620. /*! @brief A type list that is the difference between the two type lists. */
  2621. using type = type_list_cat_t<std::conditional_t<type_list_contains_v<type_list<Other...>, Type>, type_list<>, type_list<Type>>...>;
  2622. };
  2623. /**
  2624. * @brief Helper type.
  2625. * @tparam List Type lists between which to compute the difference.
  2626. */
  2627. template<typename... List>
  2628. using type_list_diff_t = typename type_list_diff<List...>::type;
  2629. /**
  2630. * @brief A class to use to push around lists of constant values, nothing more.
  2631. * @tparam Value Values provided by the value list.
  2632. */
  2633. template<auto... Value>
  2634. struct value_list {
  2635. /*! @brief Value list type. */
  2636. using type = value_list;
  2637. /*! @brief Compile-time number of elements in the value list. */
  2638. static constexpr auto size = sizeof...(Value);
  2639. };
  2640. /*! @brief Primary template isn't defined on purpose. */
  2641. template<std::size_t, typename>
  2642. struct value_list_element;
  2643. /**
  2644. * @brief Provides compile-time indexed access to the values of a value list.
  2645. * @tparam Index Index of the value to return.
  2646. * @tparam Value First value provided by the value list.
  2647. * @tparam Other Other values provided by the value list.
  2648. */
  2649. template<std::size_t Index, auto Value, auto... Other>
  2650. struct value_list_element<Index, value_list<Value, Other...>>
  2651. : value_list_element<Index - 1u, value_list<Other...>>
  2652. {};
  2653. /**
  2654. * @brief Provides compile-time indexed access to the types of a type list.
  2655. * @tparam Value First value provided by the value list.
  2656. * @tparam Other Other values provided by the value list.
  2657. */
  2658. template<auto Value, auto... Other>
  2659. struct value_list_element<0u, value_list<Value, Other...>> {
  2660. /*! @brief Searched value. */
  2661. static constexpr auto value = Value;
  2662. };
  2663. /**
  2664. * @brief Helper type.
  2665. * @tparam Index Index of the value to return.
  2666. * @tparam List Value list to search into.
  2667. */
  2668. template<std::size_t Index, typename List>
  2669. inline constexpr auto value_list_element_v = value_list_element<Index, List>::value;
  2670. /**
  2671. * @brief Concatenates multiple value lists.
  2672. * @tparam Value Values provided by the first value list.
  2673. * @tparam Other Values provided by the second value list.
  2674. * @return A value list composed by the values of both the value lists.
  2675. */
  2676. template<auto... Value, auto... Other>
  2677. constexpr value_list<Value..., Other...> operator+(value_list<Value...>, value_list<Other...>) { return {}; }
  2678. /*! @brief Primary template isn't defined on purpose. */
  2679. template<typename...>
  2680. struct value_list_cat;
  2681. /*! @brief Concatenates multiple value lists. */
  2682. template<>
  2683. struct value_list_cat<> {
  2684. /*! @brief A value list composed by the values of all the value lists. */
  2685. using type = value_list<>;
  2686. };
  2687. /**
  2688. * @brief Concatenates multiple value lists.
  2689. * @tparam Value Values provided by the first value list.
  2690. * @tparam Other Values provided by the second value list.
  2691. * @tparam List Other value lists, if any.
  2692. */
  2693. template<auto... Value, auto... Other, typename... List>
  2694. struct value_list_cat<value_list<Value...>, value_list<Other...>, List...> {
  2695. /*! @brief A value list composed by the values of all the value lists. */
  2696. using type = typename value_list_cat<value_list<Value..., Other...>, List...>::type;
  2697. };
  2698. /**
  2699. * @brief Concatenates multiple value lists.
  2700. * @tparam Value Values provided by the value list.
  2701. */
  2702. template<auto... Value>
  2703. struct value_list_cat<value_list<Value...>> {
  2704. /*! @brief A value list composed by the values of all the value lists. */
  2705. using type = value_list<Value...>;
  2706. };
  2707. /**
  2708. * @brief Helper type.
  2709. * @tparam List Value lists to concatenate.
  2710. */
  2711. template<typename... List>
  2712. using value_list_cat_t = typename value_list_cat<List...>::type;
  2713. /**
  2714. * @cond TURN_OFF_DOXYGEN
  2715. * Internal details not to be documented.
  2716. */
  2717. namespace internal {
  2718. template<typename>
  2719. [[nodiscard]] constexpr bool is_equality_comparable(...) { return false; }
  2720. template<typename Type>
  2721. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<0>)
  2722. -> decltype(std::declval<Type>() == std::declval<Type>()) { return true; }
  2723. template<typename Type>
  2724. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<1>)
  2725. -> decltype(std::declval<typename Type::value_type>(), std::declval<Type>() == std::declval<Type>()) {
  2726. if constexpr(std::is_same_v<typename Type::value_type, Type>) {
  2727. return is_equality_comparable<Type>(choice<0>);
  2728. } else {
  2729. return is_equality_comparable<typename Type::value_type>(choice<2>);
  2730. }
  2731. }
  2732. template<typename Type>
  2733. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<2>)
  2734. -> decltype(std::declval<typename Type::mapped_type>(), std::declval<Type>() == std::declval<Type>()) {
  2735. return is_equality_comparable<typename Type::key_type>(choice<2>) && is_equality_comparable<typename Type::mapped_type>(choice<2>);
  2736. }
  2737. }
  2738. /**
  2739. * Internal details not to be documented.
  2740. * @endcond
  2741. */
  2742. /**
  2743. * @brief Provides the member constant `value` to true if a given type is
  2744. * equality comparable, false otherwise.
  2745. * @tparam Type The type to test.
  2746. */
  2747. template<typename Type, typename = void>
  2748. struct is_equality_comparable: std::bool_constant<internal::is_equality_comparable<Type>(choice<2>)> {};
  2749. /**
  2750. * @brief Helper variable template.
  2751. * @tparam Type The type to test.
  2752. */
  2753. template<class Type>
  2754. inline constexpr bool is_equality_comparable_v = is_equality_comparable<Type>::value;
  2755. /*! @brief Same as std::is_invocable, but with tuples. */
  2756. template<typename, typename>
  2757. struct is_applicable: std::false_type {};
  2758. /**
  2759. * @copybrief is_applicable
  2760. * @tparam Func A valid function type.
  2761. * @tparam Tuple Tuple-like type.
  2762. * @tparam Args The list of arguments to use to probe the function type.
  2763. */
  2764. template<typename Func, template<typename...> class Tuple, typename... Args>
  2765. struct is_applicable<Func, Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  2766. /**
  2767. * @copybrief is_applicable
  2768. * @tparam Func A valid function type.
  2769. * @tparam Tuple Tuple-like type.
  2770. * @tparam Args The list of arguments to use to probe the function type.
  2771. */
  2772. template<typename Func, template<typename...> class Tuple, typename... Args>
  2773. struct is_applicable<Func, const Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  2774. /**
  2775. * @brief Helper variable template.
  2776. * @tparam Func A valid function type.
  2777. * @tparam Args The list of arguments to use to probe the function type.
  2778. */
  2779. template<typename Func, typename Args>
  2780. inline constexpr bool is_applicable_v = is_applicable<Func, Args>::value;
  2781. /*! @brief Same as std::is_invocable_r, but with tuples for arguments. */
  2782. template<typename, typename, typename>
  2783. struct is_applicable_r: std::false_type {};
  2784. /**
  2785. * @copybrief is_applicable_r
  2786. * @tparam Ret The type to which the return type of the function should be
  2787. * convertible.
  2788. * @tparam Func A valid function type.
  2789. * @tparam Args The list of arguments to use to probe the function type.
  2790. */
  2791. template<typename Ret, typename Func, typename... Args>
  2792. struct is_applicable_r<Ret, Func, std::tuple<Args...>>: std::is_invocable_r<Ret, Func, Args...> {};
  2793. /**
  2794. * @brief Helper variable template.
  2795. * @tparam Ret The type to which the return type of the function should be
  2796. * convertible.
  2797. * @tparam Func A valid function type.
  2798. * @tparam Args The list of arguments to use to probe the function type.
  2799. */
  2800. template<typename Ret, typename Func, typename Args>
  2801. inline constexpr bool is_applicable_r_v = is_applicable_r<Ret, Func, Args>::value;
  2802. /**
  2803. * @brief Provides the member constant `value` to true if a given type is
  2804. * complete, false otherwise.
  2805. * @tparam Type The type to test.
  2806. */
  2807. template<typename Type, typename = void>
  2808. struct is_complete: std::false_type {};
  2809. /*! @copydoc is_complete */
  2810. template<typename Type>
  2811. struct is_complete<Type, std::void_t<decltype(sizeof(Type))>>: std::true_type {};
  2812. /**
  2813. * @brief Helper variable template.
  2814. * @tparam Type The type to test.
  2815. */
  2816. template<typename Type>
  2817. inline constexpr bool is_complete_v = is_complete<Type>::value;
  2818. /**
  2819. * @brief Provides the member constant `value` to true if a given type is an
  2820. * iterator, false otherwise.
  2821. * @tparam Type The type to test.
  2822. */
  2823. template<typename Type, typename = void>
  2824. struct is_iterator: std::false_type {};
  2825. /*! @copydoc is_iterator */
  2826. template<typename Type>
  2827. struct is_iterator<Type, std::void_t<typename std::iterator_traits<Type>::iterator_category>>
  2828. : std::true_type
  2829. {};
  2830. /**
  2831. * @brief Helper variable template.
  2832. * @tparam Type The type to test.
  2833. */
  2834. template<typename Type>
  2835. inline constexpr bool is_iterator_v = is_iterator<Type>::value;
  2836. /**
  2837. * @brief Provides the member constant `value` to true if a given type is of the
  2838. * required iterator type, false otherwise.
  2839. * @tparam Type The type to test.
  2840. * @tparam It Required iterator type.
  2841. */
  2842. template<typename Type, typename It, typename = void>
  2843. struct is_iterator_type: std::false_type {};
  2844. /*! @copydoc is_iterator_type */
  2845. template<typename Type, typename It>
  2846. struct is_iterator_type<Type, It, std::enable_if_t<is_iterator_v<Type> && std::is_same_v<Type, It>>>
  2847. : std::true_type
  2848. {};
  2849. /*! @copydoc is_iterator_type */
  2850. template<typename Type, typename It>
  2851. struct is_iterator_type<Type, It, std::enable_if_t<!std::is_same_v<Type, It>, std::void_t<typename It::iterator_type>>>
  2852. : is_iterator_type<Type, typename It::iterator_type>
  2853. {};
  2854. /**
  2855. * @brief Helper variable template.
  2856. * @tparam Type The type to test.
  2857. * @tparam It Required iterator type.
  2858. */
  2859. template<typename Type, typename It>
  2860. inline constexpr bool is_iterator_type_v = is_iterator_type<Type, It>::value;
  2861. /**
  2862. * @brief Transcribes the constness of a type to another type.
  2863. * @tparam To The type to which to transcribe the constness.
  2864. * @tparam From The type from which to transcribe the constness.
  2865. */
  2866. template<typename To, typename From>
  2867. struct constness_as {
  2868. /*! @brief The type resulting from the transcription of the constness. */
  2869. using type = std::remove_const_t<To>;
  2870. };
  2871. /*! @copydoc constness_as */
  2872. template<typename To, typename From>
  2873. struct constness_as<To, const From> {
  2874. /*! @brief The type resulting from the transcription of the constness. */
  2875. using type = std::add_const_t<To>;
  2876. };
  2877. /**
  2878. * @brief Alias template to facilitate the transcription of the constness.
  2879. * @tparam To The type to which to transcribe the constness.
  2880. * @tparam From The type from which to transcribe the constness.
  2881. */
  2882. template<typename To, typename From>
  2883. using constness_as_t = typename constness_as<To, From>::type;
  2884. /**
  2885. * @brief Extracts the class of a non-static member object or function.
  2886. * @tparam Member A pointer to a non-static member object or function.
  2887. */
  2888. template<typename Member>
  2889. class member_class {
  2890. static_assert(std::is_member_pointer_v<Member>, "Invalid pointer type to non-static member object or function");
  2891. template<typename Class, typename Ret, typename... Args>
  2892. static Class * clazz(Ret(Class:: *)(Args...));
  2893. template<typename Class, typename Ret, typename... Args>
  2894. static Class * clazz(Ret(Class:: *)(Args...) const);
  2895. template<typename Class, typename Type>
  2896. static Class * clazz(Type Class:: *);
  2897. public:
  2898. /*! @brief The class of the given non-static member object or function. */
  2899. using type = std::remove_pointer_t<decltype(clazz(std::declval<Member>()))>;
  2900. };
  2901. /**
  2902. * @brief Helper type.
  2903. * @tparam Member A pointer to a non-static member object or function.
  2904. */
  2905. template<typename Member>
  2906. using member_class_t = typename member_class<Member>::type;
  2907. }
  2908. #endif
  2909. // #include "core/utility.hpp"
  2910. #ifndef ENTT_CORE_UTILITY_HPP
  2911. #define ENTT_CORE_UTILITY_HPP
  2912. #include <utility>
  2913. // #include "../config/config.h"
  2914. namespace entt {
  2915. /*! @brief Identity function object (waiting for C++20). */
  2916. struct identity {
  2917. /**
  2918. * @brief Returns its argument unchanged.
  2919. * @tparam Type Type of the argument.
  2920. * @param value The actual argument.
  2921. * @return The submitted value as-is.
  2922. */
  2923. template<class Type>
  2924. [[nodiscard]] constexpr Type && operator()(Type &&value) const ENTT_NOEXCEPT {
  2925. return std::forward<Type>(value);
  2926. }
  2927. };
  2928. /**
  2929. * @brief Constant utility to disambiguate overloaded members of a class.
  2930. * @tparam Type Type of the desired overload.
  2931. * @tparam Class Type of class to which the member belongs.
  2932. * @param member A valid pointer to a member.
  2933. * @return Pointer to the member.
  2934. */
  2935. template<typename Type, typename Class>
  2936. [[nodiscard]] constexpr auto overload(Type Class:: *member) ENTT_NOEXCEPT { return member; }
  2937. /**
  2938. * @brief Constant utility to disambiguate overloaded functions.
  2939. * @tparam Func Function type of the desired overload.
  2940. * @param func A valid pointer to a function.
  2941. * @return Pointer to the function.
  2942. */
  2943. template<typename Func>
  2944. [[nodiscard]] constexpr auto overload(Func *func) ENTT_NOEXCEPT { return func; }
  2945. /**
  2946. * @brief Helper type for visitors.
  2947. * @tparam Func Types of function objects.
  2948. */
  2949. template<class... Func>
  2950. struct overloaded: Func... {
  2951. using Func::operator()...;
  2952. };
  2953. /**
  2954. * @brief Deduction guide.
  2955. * @tparam Func Types of function objects.
  2956. */
  2957. template<class... Func>
  2958. overloaded(Func...)
  2959. -> overloaded<Func...>;
  2960. /**
  2961. * @brief Basic implementation of a y-combinator.
  2962. * @tparam Func Type of a potentially recursive function.
  2963. */
  2964. template<class Func>
  2965. struct y_combinator {
  2966. /**
  2967. * @brief Constructs a y-combinator from a given function.
  2968. * @param recursive A potentially recursive function.
  2969. */
  2970. y_combinator(Func recursive):
  2971. func{std::move(recursive)}
  2972. {}
  2973. /**
  2974. * @brief Invokes a y-combinator and therefore its underlying function.
  2975. * @tparam Args Types of arguments to use to invoke the underlying function.
  2976. * @param args Parameters to use to invoke the underlying function.
  2977. * @return Return value of the underlying function, if any.
  2978. */
  2979. template <class... Args>
  2980. decltype(auto) operator()(Args &&... args) const {
  2981. return func(*this, std::forward<Args>(args)...);
  2982. }
  2983. /*! @copydoc operator()() */
  2984. template <class... Args>
  2985. decltype(auto) operator()(Args &&... args) {
  2986. return func(*this, std::forward<Args>(args)...);
  2987. }
  2988. private:
  2989. Func func;
  2990. };
  2991. }
  2992. #endif
  2993. // #include "entity/component.hpp"
  2994. #ifndef ENTT_ENTITY_COMPONENT_HPP
  2995. #define ENTT_ENTITY_COMPONENT_HPP
  2996. #include <type_traits>
  2997. // #include "../config/config.h"
  2998. #ifndef ENTT_CONFIG_CONFIG_H
  2999. #define ENTT_CONFIG_CONFIG_H
  3000. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  3001. # define ENTT_NOEXCEPT noexcept
  3002. # define ENTT_THROW throw
  3003. # define ENTT_TRY try
  3004. # define ENTT_CATCH catch(...)
  3005. #else
  3006. # define ENTT_NOEXCEPT
  3007. # define ENTT_THROW
  3008. # define ENTT_TRY if(true)
  3009. # define ENTT_CATCH if(false)
  3010. #endif
  3011. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  3012. # include <new>
  3013. # define ENTT_LAUNDER(expr) std::launder(expr)
  3014. #else
  3015. # define ENTT_LAUNDER(expr) expr
  3016. #endif
  3017. #ifndef ENTT_USE_ATOMIC
  3018. # define ENTT_MAYBE_ATOMIC(Type) Type
  3019. #else
  3020. # include <atomic>
  3021. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  3022. #endif
  3023. #ifndef ENTT_ID_TYPE
  3024. # include <cstdint>
  3025. # define ENTT_ID_TYPE std::uint32_t
  3026. #endif
  3027. #ifdef ENTT_SPARSE_PAGE
  3028. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  3029. #else
  3030. # define ENTT_SPARSE_PAGE 4096
  3031. #endif
  3032. #ifdef ENTT_PACKED_PAGE
  3033. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  3034. #else
  3035. # define ENTT_PACKED_PAGE 1024
  3036. #endif
  3037. #ifdef ENTT_DISABLE_ASSERT
  3038. # undef ENTT_ASSERT
  3039. # define ENTT_ASSERT(...) (void(0))
  3040. #elif !defined ENTT_ASSERT
  3041. # include <cassert>
  3042. # define ENTT_ASSERT(condition, ...) assert(condition)
  3043. #endif
  3044. #ifdef ENTT_NO_ETO
  3045. # include <type_traits>
  3046. # define ENTT_IGNORE_IF_EMPTY std::false_type
  3047. #else
  3048. # include <type_traits>
  3049. # define ENTT_IGNORE_IF_EMPTY std::true_type
  3050. #endif
  3051. #ifndef ENTT_STANDARD_CPP
  3052. # if defined __clang__ || defined __GNUC__
  3053. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  3054. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  3055. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  3056. # elif defined _MSC_VER
  3057. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  3058. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  3059. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  3060. # endif
  3061. #endif
  3062. #endif
  3063. namespace entt {
  3064. /*! @brief Commonly used default traits for all types. */
  3065. struct basic_component_traits {
  3066. /*! @brief Pointer stability, default is `std::false_type`. */
  3067. using in_place_delete = std::false_type;
  3068. /*! @brief Empty type optimization, default is `ENTT_IGNORE_IF_EMPTY`. */
  3069. using ignore_if_empty = ENTT_IGNORE_IF_EMPTY;
  3070. };
  3071. /**
  3072. * @brief Common way to access various properties of components.
  3073. * @tparam Type Type of component.
  3074. */
  3075. template<typename Type, typename = void>
  3076. struct component_traits: basic_component_traits {
  3077. static_assert(std::is_same_v<std::decay_t<Type>, Type>, "Unsupported type");
  3078. };
  3079. }
  3080. #endif
  3081. // #include "entity/entity.hpp"
  3082. #ifndef ENTT_ENTITY_ENTITY_HPP
  3083. #define ENTT_ENTITY_ENTITY_HPP
  3084. #include <cstddef>
  3085. #include <cstdint>
  3086. #include <type_traits>
  3087. // #include "../config/config.h"
  3088. namespace entt {
  3089. /**
  3090. * @cond TURN_OFF_DOXYGEN
  3091. * Internal details not to be documented.
  3092. */
  3093. namespace internal {
  3094. template<typename, typename = void>
  3095. struct entt_traits;
  3096. template<typename Type>
  3097. struct entt_traits<Type, std::enable_if_t<std::is_enum_v<Type>>>
  3098. : entt_traits<std::underlying_type_t<Type>>
  3099. {};
  3100. template<typename Type>
  3101. struct entt_traits<Type, std::enable_if_t<std::is_class_v<Type>>>
  3102. : entt_traits<typename Type::entity_type>
  3103. {};
  3104. template<>
  3105. struct entt_traits<std::uint32_t> {
  3106. using entity_type = std::uint32_t;
  3107. using version_type = std::uint16_t;
  3108. using difference_type = std::int64_t;
  3109. static constexpr entity_type entity_mask = 0xFFFFF;
  3110. static constexpr entity_type version_mask = 0xFFF;
  3111. static constexpr std::size_t entity_shift = 20u;
  3112. };
  3113. template<>
  3114. struct entt_traits<std::uint64_t> {
  3115. using entity_type = std::uint64_t;
  3116. using version_type = std::uint32_t;
  3117. using difference_type = std::int64_t;
  3118. static constexpr entity_type entity_mask = 0xFFFFFFFF;
  3119. static constexpr entity_type version_mask = 0xFFFFFFFF;
  3120. static constexpr std::size_t entity_shift = 32u;
  3121. };
  3122. }
  3123. /**
  3124. * Internal details not to be documented.
  3125. * @endcond
  3126. */
  3127. /**
  3128. * @brief Entity traits.
  3129. * @tparam Type Type of identifier.
  3130. */
  3131. template<typename Type>
  3132. class entt_traits: private internal::entt_traits<Type> {
  3133. using traits_type = internal::entt_traits<Type>;
  3134. public:
  3135. /*! @brief Value type. */
  3136. using value_type = Type;
  3137. /*! @brief Underlying entity type. */
  3138. using entity_type = typename traits_type::entity_type;
  3139. /*! @brief Underlying version type. */
  3140. using version_type = typename traits_type::version_type;
  3141. /*! @brief Difference type. */
  3142. using difference_type = typename traits_type::difference_type;
  3143. /**
  3144. * @brief Converts an entity to its underlying type.
  3145. * @param value The value to convert.
  3146. * @return The integral representation of the given value.
  3147. */
  3148. [[nodiscard]] static constexpr entity_type to_integral(const value_type value) ENTT_NOEXCEPT {
  3149. return static_cast<entity_type>(value);
  3150. }
  3151. /**
  3152. * @brief Returns the entity part once converted to the underlying type.
  3153. * @param value The value to convert.
  3154. * @return The integral representation of the entity part.
  3155. */
  3156. [[nodiscard]] static constexpr entity_type to_entity(const value_type value) ENTT_NOEXCEPT {
  3157. return (to_integral(value) & traits_type::entity_mask);
  3158. }
  3159. /**
  3160. * @brief Returns the version part once converted to the underlying type.
  3161. * @param value The value to convert.
  3162. * @return The integral representation of the version part.
  3163. */
  3164. [[nodiscard]] static constexpr version_type to_version(const value_type value) ENTT_NOEXCEPT {
  3165. constexpr auto mask = (traits_type::version_mask << traits_type::entity_shift);
  3166. return ((to_integral(value) & mask) >> traits_type::entity_shift);
  3167. }
  3168. /**
  3169. * @brief Constructs an identifier from its parts.
  3170. *
  3171. * If the version part is not provided, a tombstone is returned.<br/>
  3172. * If the entity part is not provided, a null identifier is returned.
  3173. *
  3174. * @param entity The entity part of the identifier.
  3175. * @param version The version part of the identifier.
  3176. * @return A properly constructed identifier.
  3177. */
  3178. [[nodiscard]] static constexpr value_type construct(const entity_type entity = traits_type::entity_mask, const version_type version = traits_type::version_mask) ENTT_NOEXCEPT {
  3179. return value_type{(entity & traits_type::entity_mask) | (version << traits_type::entity_shift)};
  3180. }
  3181. };
  3182. /**
  3183. * @brief Converts an entity to its underlying type.
  3184. * @tparam Entity The value type.
  3185. * @param entity The value to convert.
  3186. * @return The integral representation of the given value.
  3187. */
  3188. template<typename Entity>
  3189. [[nodiscard]] constexpr auto to_integral(const Entity entity) ENTT_NOEXCEPT {
  3190. return entt_traits<Entity>::to_integral(entity);
  3191. }
  3192. /*! @brief Null object for all entity identifiers. */
  3193. struct null_t {
  3194. /**
  3195. * @brief Converts the null object to identifiers of any type.
  3196. * @tparam Entity Type of entity identifier.
  3197. * @return The null representation for the given type.
  3198. */
  3199. template<typename Entity>
  3200. [[nodiscard]] constexpr operator Entity() const ENTT_NOEXCEPT {
  3201. return entt_traits<Entity>::construct();
  3202. }
  3203. /**
  3204. * @brief Compares two null objects.
  3205. * @param other A null object.
  3206. * @return True in all cases.
  3207. */
  3208. [[nodiscard]] constexpr bool operator==([[maybe_unused]] const null_t other) const ENTT_NOEXCEPT {
  3209. return true;
  3210. }
  3211. /**
  3212. * @brief Compares two null objects.
  3213. * @param other A null object.
  3214. * @return False in all cases.
  3215. */
  3216. [[nodiscard]] constexpr bool operator!=([[maybe_unused]] const null_t other) const ENTT_NOEXCEPT {
  3217. return false;
  3218. }
  3219. /**
  3220. * @brief Compares a null object and an entity identifier of any type.
  3221. * @tparam Entity Type of entity identifier.
  3222. * @param entity Entity identifier with which to compare.
  3223. * @return False if the two elements differ, true otherwise.
  3224. */
  3225. template<typename Entity>
  3226. [[nodiscard]] constexpr bool operator==(const Entity entity) const ENTT_NOEXCEPT {
  3227. return entt_traits<Entity>::to_entity(entity) == entt_traits<Entity>::to_entity(*this);
  3228. }
  3229. /**
  3230. * @brief Compares a null object and an entity identifier of any type.
  3231. * @tparam Entity Type of entity identifier.
  3232. * @param entity Entity identifier with which to compare.
  3233. * @return True if the two elements differ, false otherwise.
  3234. */
  3235. template<typename Entity>
  3236. [[nodiscard]] constexpr bool operator!=(const Entity entity) const ENTT_NOEXCEPT {
  3237. return !(entity == *this);
  3238. }
  3239. /**
  3240. * @brief Creates a null object from an entity identifier of any type.
  3241. * @tparam Entity Type of entity identifier.
  3242. * @param entity Entity identifier to turn into a null object.
  3243. * @return The null representation for the given identifier.
  3244. */
  3245. template<typename Entity>
  3246. [[nodiscard]] constexpr Entity operator|(const Entity entity) const ENTT_NOEXCEPT {
  3247. return entt_traits<Entity>::construct(entt_traits<Entity>::to_entity(*this), entt_traits<Entity>::to_version(entity));
  3248. }
  3249. };
  3250. /**
  3251. * @brief Compares a null object and an entity identifier of any type.
  3252. * @tparam Entity Type of entity identifier.
  3253. * @param entity Entity identifier with which to compare.
  3254. * @param other A null object yet to be converted.
  3255. * @return False if the two elements differ, true otherwise.
  3256. */
  3257. template<typename Entity>
  3258. [[nodiscard]] constexpr bool operator==(const Entity entity, const null_t other) ENTT_NOEXCEPT {
  3259. return other.operator==(entity);
  3260. }
  3261. /**
  3262. * @brief Compares a null object and an entity identifier of any type.
  3263. * @tparam Entity Type of entity identifier.
  3264. * @param entity Entity identifier with which to compare.
  3265. * @param other A null object yet to be converted.
  3266. * @return True if the two elements differ, false otherwise.
  3267. */
  3268. template<typename Entity>
  3269. [[nodiscard]] constexpr bool operator!=(const Entity entity, const null_t other) ENTT_NOEXCEPT {
  3270. return !(other == entity);
  3271. }
  3272. /*! @brief Tombstone object for all entity identifiers. */
  3273. struct tombstone_t {
  3274. /**
  3275. * @brief Converts the tombstone object to identifiers of any type.
  3276. * @tparam Entity Type of entity identifier.
  3277. * @return The tombstone representation for the given type.
  3278. */
  3279. template<typename Entity>
  3280. [[nodiscard]] constexpr operator Entity() const ENTT_NOEXCEPT {
  3281. return entt_traits<Entity>::construct();
  3282. }
  3283. /**
  3284. * @brief Compares two tombstone objects.
  3285. * @param other A tombstone object.
  3286. * @return True in all cases.
  3287. */
  3288. [[nodiscard]] constexpr bool operator==([[maybe_unused]] const tombstone_t other) const ENTT_NOEXCEPT {
  3289. return true;
  3290. }
  3291. /**
  3292. * @brief Compares two tombstone objects.
  3293. * @param other A tombstone object.
  3294. * @return False in all cases.
  3295. */
  3296. [[nodiscard]] constexpr bool operator!=([[maybe_unused]] const tombstone_t other) const ENTT_NOEXCEPT {
  3297. return false;
  3298. }
  3299. /**
  3300. * @brief Compares a tombstone object and an entity identifier of any type.
  3301. * @tparam Entity Type of entity identifier.
  3302. * @param entity Entity identifier with which to compare.
  3303. * @return False if the two elements differ, true otherwise.
  3304. */
  3305. template<typename Entity>
  3306. [[nodiscard]] constexpr bool operator==(const Entity entity) const ENTT_NOEXCEPT {
  3307. return entt_traits<Entity>::to_version(entity) == entt_traits<Entity>::to_version(*this);
  3308. }
  3309. /**
  3310. * @brief Compares a tombstone object and an entity identifier of any type.
  3311. * @tparam Entity Type of entity identifier.
  3312. * @param entity Entity identifier with which to compare.
  3313. * @return True if the two elements differ, false otherwise.
  3314. */
  3315. template<typename Entity>
  3316. [[nodiscard]] constexpr bool operator!=(const Entity entity) const ENTT_NOEXCEPT {
  3317. return !(entity == *this);
  3318. }
  3319. /**
  3320. * @brief Creates a tombstone object from an entity identifier of any type.
  3321. * @tparam Entity Type of entity identifier.
  3322. * @param entity Entity identifier to turn into a tombstone object.
  3323. * @return The tombstone representation for the given identifier.
  3324. */
  3325. template<typename Entity>
  3326. [[nodiscard]] constexpr Entity operator|(const Entity entity) const ENTT_NOEXCEPT {
  3327. return entt_traits<Entity>::construct(entt_traits<Entity>::to_entity(entity));
  3328. }
  3329. };
  3330. /**
  3331. * @brief Compares a tombstone object and an entity identifier of any type.
  3332. * @tparam Entity Type of entity identifier.
  3333. * @param entity Entity identifier with which to compare.
  3334. * @param other A tombstone object yet to be converted.
  3335. * @return False if the two elements differ, true otherwise.
  3336. */
  3337. template<typename Entity>
  3338. [[nodiscard]] constexpr bool operator==(const Entity entity, const tombstone_t other) ENTT_NOEXCEPT {
  3339. return other.operator==(entity);
  3340. }
  3341. /**
  3342. * @brief Compares a tombstone object and an entity identifier of any type.
  3343. * @tparam Entity Type of entity identifier.
  3344. * @param entity Entity identifier with which to compare.
  3345. * @param other A tombstone object yet to be converted.
  3346. * @return True if the two elements differ, false otherwise.
  3347. */
  3348. template<typename Entity>
  3349. [[nodiscard]] constexpr bool operator!=(const Entity entity, const tombstone_t other) ENTT_NOEXCEPT {
  3350. return !(other == entity);
  3351. }
  3352. /**
  3353. * @brief Compile-time constant for null entities.
  3354. *
  3355. * There exist implicit conversions from this variable to entity identifiers of
  3356. * any allowed type. Similarly, there exist comparision operators between the
  3357. * null entity and any other entity identifier.
  3358. */
  3359. inline constexpr null_t null{};
  3360. /**
  3361. * @brief Compile-time constant for tombstone entities.
  3362. *
  3363. * There exist implicit conversions from this variable to entity identifiers of
  3364. * any allowed type. Similarly, there exist comparision operators between the
  3365. * tombstone entity and any other entity identifier.
  3366. */
  3367. inline constexpr tombstone_t tombstone{};
  3368. }
  3369. #endif
  3370. // #include "entity/group.hpp"
  3371. #ifndef ENTT_ENTITY_GROUP_HPP
  3372. #define ENTT_ENTITY_GROUP_HPP
  3373. #include <tuple>
  3374. #include <utility>
  3375. #include <type_traits>
  3376. // #include "../config/config.h"
  3377. // #include "../core/type_traits.hpp"
  3378. #ifndef ENTT_CORE_TYPE_TRAITS_HPP
  3379. #define ENTT_CORE_TYPE_TRAITS_HPP
  3380. #include <cstddef>
  3381. #include <iterator>
  3382. #include <type_traits>
  3383. #include <utility>
  3384. // #include "../config/config.h"
  3385. #ifndef ENTT_CONFIG_CONFIG_H
  3386. #define ENTT_CONFIG_CONFIG_H
  3387. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  3388. # define ENTT_NOEXCEPT noexcept
  3389. # define ENTT_THROW throw
  3390. # define ENTT_TRY try
  3391. # define ENTT_CATCH catch(...)
  3392. #else
  3393. # define ENTT_NOEXCEPT
  3394. # define ENTT_THROW
  3395. # define ENTT_TRY if(true)
  3396. # define ENTT_CATCH if(false)
  3397. #endif
  3398. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  3399. # include <new>
  3400. # define ENTT_LAUNDER(expr) std::launder(expr)
  3401. #else
  3402. # define ENTT_LAUNDER(expr) expr
  3403. #endif
  3404. #ifndef ENTT_USE_ATOMIC
  3405. # define ENTT_MAYBE_ATOMIC(Type) Type
  3406. #else
  3407. # include <atomic>
  3408. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  3409. #endif
  3410. #ifndef ENTT_ID_TYPE
  3411. # include <cstdint>
  3412. # define ENTT_ID_TYPE std::uint32_t
  3413. #endif
  3414. #ifdef ENTT_SPARSE_PAGE
  3415. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  3416. #else
  3417. # define ENTT_SPARSE_PAGE 4096
  3418. #endif
  3419. #ifdef ENTT_PACKED_PAGE
  3420. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  3421. #else
  3422. # define ENTT_PACKED_PAGE 1024
  3423. #endif
  3424. #ifdef ENTT_DISABLE_ASSERT
  3425. # undef ENTT_ASSERT
  3426. # define ENTT_ASSERT(...) (void(0))
  3427. #elif !defined ENTT_ASSERT
  3428. # include <cassert>
  3429. # define ENTT_ASSERT(condition, ...) assert(condition)
  3430. #endif
  3431. #ifdef ENTT_NO_ETO
  3432. # include <type_traits>
  3433. # define ENTT_IGNORE_IF_EMPTY std::false_type
  3434. #else
  3435. # include <type_traits>
  3436. # define ENTT_IGNORE_IF_EMPTY std::true_type
  3437. #endif
  3438. #ifndef ENTT_STANDARD_CPP
  3439. # if defined __clang__ || defined __GNUC__
  3440. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  3441. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  3442. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  3443. # elif defined _MSC_VER
  3444. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  3445. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  3446. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  3447. # endif
  3448. #endif
  3449. #endif
  3450. // #include "fwd.hpp"
  3451. #ifndef ENTT_CORE_FWD_HPP
  3452. #define ENTT_CORE_FWD_HPP
  3453. #include <type_traits>
  3454. // #include "../config/config.h"
  3455. namespace entt {
  3456. template<std::size_t Len = sizeof(double[2]), std::size_t = alignof(typename std::aligned_storage_t<Len + !Len>)>
  3457. class basic_any;
  3458. /*! @brief Alias declaration for type identifiers. */
  3459. using id_type = ENTT_ID_TYPE;
  3460. /*! @brief Alias declaration for the most common use case. */
  3461. using any = basic_any<>;
  3462. }
  3463. #endif
  3464. namespace entt {
  3465. /**
  3466. * @brief Utility class to disambiguate overloaded functions.
  3467. * @tparam N Number of choices available.
  3468. */
  3469. template<std::size_t N>
  3470. struct choice_t
  3471. // Unfortunately, doxygen cannot parse such a construct.
  3472. /*! @cond TURN_OFF_DOXYGEN */
  3473. : choice_t<N-1>
  3474. /*! @endcond */
  3475. {};
  3476. /*! @copybrief choice_t */
  3477. template<>
  3478. struct choice_t<0> {};
  3479. /**
  3480. * @brief Variable template for the choice trick.
  3481. * @tparam N Number of choices available.
  3482. */
  3483. template<std::size_t N>
  3484. inline constexpr choice_t<N> choice{};
  3485. /**
  3486. * @brief Identity type trait.
  3487. *
  3488. * Useful to establish non-deduced contexts in template argument deduction
  3489. * (waiting for C++20) or to provide types through function arguments.
  3490. *
  3491. * @tparam Type A type.
  3492. */
  3493. template<typename Type>
  3494. struct type_identity {
  3495. /*! @brief Identity type. */
  3496. using type = Type;
  3497. };
  3498. /**
  3499. * @brief Helper type.
  3500. * @tparam Type A type.
  3501. */
  3502. template<typename Type>
  3503. using type_identity_t = typename type_identity<Type>::type;
  3504. /**
  3505. * @brief A type-only `sizeof` wrapper that returns 0 where `sizeof` complains.
  3506. * @tparam Type The type of which to return the size.
  3507. * @tparam The size of the type if `sizeof` accepts it, 0 otherwise.
  3508. */
  3509. template<typename Type, typename = void>
  3510. struct size_of: std::integral_constant<std::size_t, 0u> {};
  3511. /*! @copydoc size_of */
  3512. template<typename Type>
  3513. struct size_of<Type, std::void_t<decltype(sizeof(Type))>>
  3514. : std::integral_constant<std::size_t, sizeof(Type)>
  3515. {};
  3516. /**
  3517. * @brief Helper variable template.
  3518. * @tparam Type The type of which to return the size.
  3519. */
  3520. template<class Type>
  3521. inline constexpr std::size_t size_of_v = size_of<Type>::value;
  3522. /**
  3523. * @brief Using declaration to be used to _repeat_ the same type a number of
  3524. * times equal to the size of a given parameter pack.
  3525. * @tparam Type A type to repeat.
  3526. */
  3527. template<typename Type, typename>
  3528. using unpack_as_t = Type;
  3529. /**
  3530. * @brief Helper variable template to be used to _repeat_ the same value a
  3531. * number of times equal to the size of a given parameter pack.
  3532. * @tparam Value A value to repeat.
  3533. */
  3534. template<auto Value, typename>
  3535. inline constexpr auto unpack_as_v = Value;
  3536. /**
  3537. * @brief Wraps a static constant.
  3538. * @tparam Value A static constant.
  3539. */
  3540. template<auto Value>
  3541. using integral_constant = std::integral_constant<decltype(Value), Value>;
  3542. /**
  3543. * @brief Alias template to facilitate the creation of named values.
  3544. * @tparam Value A constant value at least convertible to `id_type`.
  3545. */
  3546. template<id_type Value>
  3547. using tag = integral_constant<Value>;
  3548. /**
  3549. * @brief A class to use to push around lists of types, nothing more.
  3550. * @tparam Type Types provided by the type list.
  3551. */
  3552. template<typename... Type>
  3553. struct type_list {
  3554. /*! @brief Type list type. */
  3555. using type = type_list;
  3556. /*! @brief Compile-time number of elements in the type list. */
  3557. static constexpr auto size = sizeof...(Type);
  3558. };
  3559. /*! @brief Primary template isn't defined on purpose. */
  3560. template<std::size_t, typename>
  3561. struct type_list_element;
  3562. /**
  3563. * @brief Provides compile-time indexed access to the types of a type list.
  3564. * @tparam Index Index of the type to return.
  3565. * @tparam Type First type provided by the type list.
  3566. * @tparam Other Other types provided by the type list.
  3567. */
  3568. template<std::size_t Index, typename Type, typename... Other>
  3569. struct type_list_element<Index, type_list<Type, Other...>>
  3570. : type_list_element<Index - 1u, type_list<Other...>>
  3571. {};
  3572. /**
  3573. * @brief Provides compile-time indexed access to the types of a type list.
  3574. * @tparam Type First type provided by the type list.
  3575. * @tparam Other Other types provided by the type list.
  3576. */
  3577. template<typename Type, typename... Other>
  3578. struct type_list_element<0u, type_list<Type, Other...>> {
  3579. /*! @brief Searched type. */
  3580. using type = Type;
  3581. };
  3582. /**
  3583. * @brief Helper type.
  3584. * @tparam Index Index of the type to return.
  3585. * @tparam List Type list to search into.
  3586. */
  3587. template<std::size_t Index, typename List>
  3588. using type_list_element_t = typename type_list_element<Index, List>::type;
  3589. /**
  3590. * @brief Concatenates multiple type lists.
  3591. * @tparam Type Types provided by the first type list.
  3592. * @tparam Other Types provided by the second type list.
  3593. * @return A type list composed by the types of both the type lists.
  3594. */
  3595. template<typename... Type, typename... Other>
  3596. constexpr type_list<Type..., Other...> operator+(type_list<Type...>, type_list<Other...>) { return {}; }
  3597. /*! @brief Primary template isn't defined on purpose. */
  3598. template<typename...>
  3599. struct type_list_cat;
  3600. /*! @brief Concatenates multiple type lists. */
  3601. template<>
  3602. struct type_list_cat<> {
  3603. /*! @brief A type list composed by the types of all the type lists. */
  3604. using type = type_list<>;
  3605. };
  3606. /**
  3607. * @brief Concatenates multiple type lists.
  3608. * @tparam Type Types provided by the first type list.
  3609. * @tparam Other Types provided by the second type list.
  3610. * @tparam List Other type lists, if any.
  3611. */
  3612. template<typename... Type, typename... Other, typename... List>
  3613. struct type_list_cat<type_list<Type...>, type_list<Other...>, List...> {
  3614. /*! @brief A type list composed by the types of all the type lists. */
  3615. using type = typename type_list_cat<type_list<Type..., Other...>, List...>::type;
  3616. };
  3617. /**
  3618. * @brief Concatenates multiple type lists.
  3619. * @tparam Type Types provided by the type list.
  3620. */
  3621. template<typename... Type>
  3622. struct type_list_cat<type_list<Type...>> {
  3623. /*! @brief A type list composed by the types of all the type lists. */
  3624. using type = type_list<Type...>;
  3625. };
  3626. /**
  3627. * @brief Helper type.
  3628. * @tparam List Type lists to concatenate.
  3629. */
  3630. template<typename... List>
  3631. using type_list_cat_t = typename type_list_cat<List...>::type;
  3632. /*! @brief Primary template isn't defined on purpose. */
  3633. template<typename>
  3634. struct type_list_unique;
  3635. /**
  3636. * @brief Removes duplicates types from a type list.
  3637. * @tparam Type One of the types provided by the given type list.
  3638. * @tparam Other The other types provided by the given type list.
  3639. */
  3640. template<typename Type, typename... Other>
  3641. struct type_list_unique<type_list<Type, Other...>> {
  3642. /*! @brief A type list without duplicate types. */
  3643. using type = std::conditional_t<
  3644. std::disjunction_v<std::is_same<Type, Other>...>,
  3645. typename type_list_unique<type_list<Other...>>::type,
  3646. type_list_cat_t<type_list<Type>, typename type_list_unique<type_list<Other...>>::type>
  3647. >;
  3648. };
  3649. /*! @brief Removes duplicates types from a type list. */
  3650. template<>
  3651. struct type_list_unique<type_list<>> {
  3652. /*! @brief A type list without duplicate types. */
  3653. using type = type_list<>;
  3654. };
  3655. /**
  3656. * @brief Helper type.
  3657. * @tparam Type A type list.
  3658. */
  3659. template<typename Type>
  3660. using type_list_unique_t = typename type_list_unique<Type>::type;
  3661. /**
  3662. * @brief Provides the member constant `value` to true if a type list contains a
  3663. * given type, false otherwise.
  3664. * @tparam List Type list.
  3665. * @tparam Type Type to look for.
  3666. */
  3667. template<typename List, typename Type>
  3668. struct type_list_contains;
  3669. /**
  3670. * @copybrief type_list_contains
  3671. * @tparam Type Types provided by the type list.
  3672. * @tparam Other Type to look for.
  3673. */
  3674. template<typename... Type, typename Other>
  3675. struct type_list_contains<type_list<Type...>, Other>: std::disjunction<std::is_same<Type, Other>...> {};
  3676. /**
  3677. * @brief Helper variable template.
  3678. * @tparam List Type list.
  3679. * @tparam Type Type to look for.
  3680. */
  3681. template<class List, typename Type>
  3682. inline constexpr bool type_list_contains_v = type_list_contains<List, Type>::value;
  3683. /*! @brief Primary template isn't defined on purpose. */
  3684. template<typename...>
  3685. struct type_list_diff;
  3686. /**
  3687. * @brief Computes the difference between two type lists.
  3688. * @tparam Type Types provided by the first type list.
  3689. * @tparam Other Types provided by the second type list.
  3690. */
  3691. template<typename... Type, typename... Other>
  3692. struct type_list_diff<type_list<Type...>, type_list<Other...>> {
  3693. /*! @brief A type list that is the difference between the two type lists. */
  3694. using type = type_list_cat_t<std::conditional_t<type_list_contains_v<type_list<Other...>, Type>, type_list<>, type_list<Type>>...>;
  3695. };
  3696. /**
  3697. * @brief Helper type.
  3698. * @tparam List Type lists between which to compute the difference.
  3699. */
  3700. template<typename... List>
  3701. using type_list_diff_t = typename type_list_diff<List...>::type;
  3702. /**
  3703. * @brief A class to use to push around lists of constant values, nothing more.
  3704. * @tparam Value Values provided by the value list.
  3705. */
  3706. template<auto... Value>
  3707. struct value_list {
  3708. /*! @brief Value list type. */
  3709. using type = value_list;
  3710. /*! @brief Compile-time number of elements in the value list. */
  3711. static constexpr auto size = sizeof...(Value);
  3712. };
  3713. /*! @brief Primary template isn't defined on purpose. */
  3714. template<std::size_t, typename>
  3715. struct value_list_element;
  3716. /**
  3717. * @brief Provides compile-time indexed access to the values of a value list.
  3718. * @tparam Index Index of the value to return.
  3719. * @tparam Value First value provided by the value list.
  3720. * @tparam Other Other values provided by the value list.
  3721. */
  3722. template<std::size_t Index, auto Value, auto... Other>
  3723. struct value_list_element<Index, value_list<Value, Other...>>
  3724. : value_list_element<Index - 1u, value_list<Other...>>
  3725. {};
  3726. /**
  3727. * @brief Provides compile-time indexed access to the types of a type list.
  3728. * @tparam Value First value provided by the value list.
  3729. * @tparam Other Other values provided by the value list.
  3730. */
  3731. template<auto Value, auto... Other>
  3732. struct value_list_element<0u, value_list<Value, Other...>> {
  3733. /*! @brief Searched value. */
  3734. static constexpr auto value = Value;
  3735. };
  3736. /**
  3737. * @brief Helper type.
  3738. * @tparam Index Index of the value to return.
  3739. * @tparam List Value list to search into.
  3740. */
  3741. template<std::size_t Index, typename List>
  3742. inline constexpr auto value_list_element_v = value_list_element<Index, List>::value;
  3743. /**
  3744. * @brief Concatenates multiple value lists.
  3745. * @tparam Value Values provided by the first value list.
  3746. * @tparam Other Values provided by the second value list.
  3747. * @return A value list composed by the values of both the value lists.
  3748. */
  3749. template<auto... Value, auto... Other>
  3750. constexpr value_list<Value..., Other...> operator+(value_list<Value...>, value_list<Other...>) { return {}; }
  3751. /*! @brief Primary template isn't defined on purpose. */
  3752. template<typename...>
  3753. struct value_list_cat;
  3754. /*! @brief Concatenates multiple value lists. */
  3755. template<>
  3756. struct value_list_cat<> {
  3757. /*! @brief A value list composed by the values of all the value lists. */
  3758. using type = value_list<>;
  3759. };
  3760. /**
  3761. * @brief Concatenates multiple value lists.
  3762. * @tparam Value Values provided by the first value list.
  3763. * @tparam Other Values provided by the second value list.
  3764. * @tparam List Other value lists, if any.
  3765. */
  3766. template<auto... Value, auto... Other, typename... List>
  3767. struct value_list_cat<value_list<Value...>, value_list<Other...>, List...> {
  3768. /*! @brief A value list composed by the values of all the value lists. */
  3769. using type = typename value_list_cat<value_list<Value..., Other...>, List...>::type;
  3770. };
  3771. /**
  3772. * @brief Concatenates multiple value lists.
  3773. * @tparam Value Values provided by the value list.
  3774. */
  3775. template<auto... Value>
  3776. struct value_list_cat<value_list<Value...>> {
  3777. /*! @brief A value list composed by the values of all the value lists. */
  3778. using type = value_list<Value...>;
  3779. };
  3780. /**
  3781. * @brief Helper type.
  3782. * @tparam List Value lists to concatenate.
  3783. */
  3784. template<typename... List>
  3785. using value_list_cat_t = typename value_list_cat<List...>::type;
  3786. /**
  3787. * @cond TURN_OFF_DOXYGEN
  3788. * Internal details not to be documented.
  3789. */
  3790. namespace internal {
  3791. template<typename>
  3792. [[nodiscard]] constexpr bool is_equality_comparable(...) { return false; }
  3793. template<typename Type>
  3794. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<0>)
  3795. -> decltype(std::declval<Type>() == std::declval<Type>()) { return true; }
  3796. template<typename Type>
  3797. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<1>)
  3798. -> decltype(std::declval<typename Type::value_type>(), std::declval<Type>() == std::declval<Type>()) {
  3799. if constexpr(std::is_same_v<typename Type::value_type, Type>) {
  3800. return is_equality_comparable<Type>(choice<0>);
  3801. } else {
  3802. return is_equality_comparable<typename Type::value_type>(choice<2>);
  3803. }
  3804. }
  3805. template<typename Type>
  3806. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<2>)
  3807. -> decltype(std::declval<typename Type::mapped_type>(), std::declval<Type>() == std::declval<Type>()) {
  3808. return is_equality_comparable<typename Type::key_type>(choice<2>) && is_equality_comparable<typename Type::mapped_type>(choice<2>);
  3809. }
  3810. }
  3811. /**
  3812. * Internal details not to be documented.
  3813. * @endcond
  3814. */
  3815. /**
  3816. * @brief Provides the member constant `value` to true if a given type is
  3817. * equality comparable, false otherwise.
  3818. * @tparam Type The type to test.
  3819. */
  3820. template<typename Type, typename = void>
  3821. struct is_equality_comparable: std::bool_constant<internal::is_equality_comparable<Type>(choice<2>)> {};
  3822. /**
  3823. * @brief Helper variable template.
  3824. * @tparam Type The type to test.
  3825. */
  3826. template<class Type>
  3827. inline constexpr bool is_equality_comparable_v = is_equality_comparable<Type>::value;
  3828. /*! @brief Same as std::is_invocable, but with tuples. */
  3829. template<typename, typename>
  3830. struct is_applicable: std::false_type {};
  3831. /**
  3832. * @copybrief is_applicable
  3833. * @tparam Func A valid function type.
  3834. * @tparam Tuple Tuple-like type.
  3835. * @tparam Args The list of arguments to use to probe the function type.
  3836. */
  3837. template<typename Func, template<typename...> class Tuple, typename... Args>
  3838. struct is_applicable<Func, Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  3839. /**
  3840. * @copybrief is_applicable
  3841. * @tparam Func A valid function type.
  3842. * @tparam Tuple Tuple-like type.
  3843. * @tparam Args The list of arguments to use to probe the function type.
  3844. */
  3845. template<typename Func, template<typename...> class Tuple, typename... Args>
  3846. struct is_applicable<Func, const Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  3847. /**
  3848. * @brief Helper variable template.
  3849. * @tparam Func A valid function type.
  3850. * @tparam Args The list of arguments to use to probe the function type.
  3851. */
  3852. template<typename Func, typename Args>
  3853. inline constexpr bool is_applicable_v = is_applicable<Func, Args>::value;
  3854. /*! @brief Same as std::is_invocable_r, but with tuples for arguments. */
  3855. template<typename, typename, typename>
  3856. struct is_applicable_r: std::false_type {};
  3857. /**
  3858. * @copybrief is_applicable_r
  3859. * @tparam Ret The type to which the return type of the function should be
  3860. * convertible.
  3861. * @tparam Func A valid function type.
  3862. * @tparam Args The list of arguments to use to probe the function type.
  3863. */
  3864. template<typename Ret, typename Func, typename... Args>
  3865. struct is_applicable_r<Ret, Func, std::tuple<Args...>>: std::is_invocable_r<Ret, Func, Args...> {};
  3866. /**
  3867. * @brief Helper variable template.
  3868. * @tparam Ret The type to which the return type of the function should be
  3869. * convertible.
  3870. * @tparam Func A valid function type.
  3871. * @tparam Args The list of arguments to use to probe the function type.
  3872. */
  3873. template<typename Ret, typename Func, typename Args>
  3874. inline constexpr bool is_applicable_r_v = is_applicable_r<Ret, Func, Args>::value;
  3875. /**
  3876. * @brief Provides the member constant `value` to true if a given type is
  3877. * complete, false otherwise.
  3878. * @tparam Type The type to test.
  3879. */
  3880. template<typename Type, typename = void>
  3881. struct is_complete: std::false_type {};
  3882. /*! @copydoc is_complete */
  3883. template<typename Type>
  3884. struct is_complete<Type, std::void_t<decltype(sizeof(Type))>>: std::true_type {};
  3885. /**
  3886. * @brief Helper variable template.
  3887. * @tparam Type The type to test.
  3888. */
  3889. template<typename Type>
  3890. inline constexpr bool is_complete_v = is_complete<Type>::value;
  3891. /**
  3892. * @brief Provides the member constant `value` to true if a given type is an
  3893. * iterator, false otherwise.
  3894. * @tparam Type The type to test.
  3895. */
  3896. template<typename Type, typename = void>
  3897. struct is_iterator: std::false_type {};
  3898. /*! @copydoc is_iterator */
  3899. template<typename Type>
  3900. struct is_iterator<Type, std::void_t<typename std::iterator_traits<Type>::iterator_category>>
  3901. : std::true_type
  3902. {};
  3903. /**
  3904. * @brief Helper variable template.
  3905. * @tparam Type The type to test.
  3906. */
  3907. template<typename Type>
  3908. inline constexpr bool is_iterator_v = is_iterator<Type>::value;
  3909. /**
  3910. * @brief Provides the member constant `value` to true if a given type is of the
  3911. * required iterator type, false otherwise.
  3912. * @tparam Type The type to test.
  3913. * @tparam It Required iterator type.
  3914. */
  3915. template<typename Type, typename It, typename = void>
  3916. struct is_iterator_type: std::false_type {};
  3917. /*! @copydoc is_iterator_type */
  3918. template<typename Type, typename It>
  3919. struct is_iterator_type<Type, It, std::enable_if_t<is_iterator_v<Type> && std::is_same_v<Type, It>>>
  3920. : std::true_type
  3921. {};
  3922. /*! @copydoc is_iterator_type */
  3923. template<typename Type, typename It>
  3924. struct is_iterator_type<Type, It, std::enable_if_t<!std::is_same_v<Type, It>, std::void_t<typename It::iterator_type>>>
  3925. : is_iterator_type<Type, typename It::iterator_type>
  3926. {};
  3927. /**
  3928. * @brief Helper variable template.
  3929. * @tparam Type The type to test.
  3930. * @tparam It Required iterator type.
  3931. */
  3932. template<typename Type, typename It>
  3933. inline constexpr bool is_iterator_type_v = is_iterator_type<Type, It>::value;
  3934. /**
  3935. * @brief Transcribes the constness of a type to another type.
  3936. * @tparam To The type to which to transcribe the constness.
  3937. * @tparam From The type from which to transcribe the constness.
  3938. */
  3939. template<typename To, typename From>
  3940. struct constness_as {
  3941. /*! @brief The type resulting from the transcription of the constness. */
  3942. using type = std::remove_const_t<To>;
  3943. };
  3944. /*! @copydoc constness_as */
  3945. template<typename To, typename From>
  3946. struct constness_as<To, const From> {
  3947. /*! @brief The type resulting from the transcription of the constness. */
  3948. using type = std::add_const_t<To>;
  3949. };
  3950. /**
  3951. * @brief Alias template to facilitate the transcription of the constness.
  3952. * @tparam To The type to which to transcribe the constness.
  3953. * @tparam From The type from which to transcribe the constness.
  3954. */
  3955. template<typename To, typename From>
  3956. using constness_as_t = typename constness_as<To, From>::type;
  3957. /**
  3958. * @brief Extracts the class of a non-static member object or function.
  3959. * @tparam Member A pointer to a non-static member object or function.
  3960. */
  3961. template<typename Member>
  3962. class member_class {
  3963. static_assert(std::is_member_pointer_v<Member>, "Invalid pointer type to non-static member object or function");
  3964. template<typename Class, typename Ret, typename... Args>
  3965. static Class * clazz(Ret(Class:: *)(Args...));
  3966. template<typename Class, typename Ret, typename... Args>
  3967. static Class * clazz(Ret(Class:: *)(Args...) const);
  3968. template<typename Class, typename Type>
  3969. static Class * clazz(Type Class:: *);
  3970. public:
  3971. /*! @brief The class of the given non-static member object or function. */
  3972. using type = std::remove_pointer_t<decltype(clazz(std::declval<Member>()))>;
  3973. };
  3974. /**
  3975. * @brief Helper type.
  3976. * @tparam Member A pointer to a non-static member object or function.
  3977. */
  3978. template<typename Member>
  3979. using member_class_t = typename member_class<Member>::type;
  3980. }
  3981. #endif
  3982. // #include "entity.hpp"
  3983. #ifndef ENTT_ENTITY_ENTITY_HPP
  3984. #define ENTT_ENTITY_ENTITY_HPP
  3985. #include <cstddef>
  3986. #include <cstdint>
  3987. #include <type_traits>
  3988. // #include "../config/config.h"
  3989. namespace entt {
  3990. /**
  3991. * @cond TURN_OFF_DOXYGEN
  3992. * Internal details not to be documented.
  3993. */
  3994. namespace internal {
  3995. template<typename, typename = void>
  3996. struct entt_traits;
  3997. template<typename Type>
  3998. struct entt_traits<Type, std::enable_if_t<std::is_enum_v<Type>>>
  3999. : entt_traits<std::underlying_type_t<Type>>
  4000. {};
  4001. template<typename Type>
  4002. struct entt_traits<Type, std::enable_if_t<std::is_class_v<Type>>>
  4003. : entt_traits<typename Type::entity_type>
  4004. {};
  4005. template<>
  4006. struct entt_traits<std::uint32_t> {
  4007. using entity_type = std::uint32_t;
  4008. using version_type = std::uint16_t;
  4009. using difference_type = std::int64_t;
  4010. static constexpr entity_type entity_mask = 0xFFFFF;
  4011. static constexpr entity_type version_mask = 0xFFF;
  4012. static constexpr std::size_t entity_shift = 20u;
  4013. };
  4014. template<>
  4015. struct entt_traits<std::uint64_t> {
  4016. using entity_type = std::uint64_t;
  4017. using version_type = std::uint32_t;
  4018. using difference_type = std::int64_t;
  4019. static constexpr entity_type entity_mask = 0xFFFFFFFF;
  4020. static constexpr entity_type version_mask = 0xFFFFFFFF;
  4021. static constexpr std::size_t entity_shift = 32u;
  4022. };
  4023. }
  4024. /**
  4025. * Internal details not to be documented.
  4026. * @endcond
  4027. */
  4028. /**
  4029. * @brief Entity traits.
  4030. * @tparam Type Type of identifier.
  4031. */
  4032. template<typename Type>
  4033. class entt_traits: private internal::entt_traits<Type> {
  4034. using traits_type = internal::entt_traits<Type>;
  4035. public:
  4036. /*! @brief Value type. */
  4037. using value_type = Type;
  4038. /*! @brief Underlying entity type. */
  4039. using entity_type = typename traits_type::entity_type;
  4040. /*! @brief Underlying version type. */
  4041. using version_type = typename traits_type::version_type;
  4042. /*! @brief Difference type. */
  4043. using difference_type = typename traits_type::difference_type;
  4044. /**
  4045. * @brief Converts an entity to its underlying type.
  4046. * @param value The value to convert.
  4047. * @return The integral representation of the given value.
  4048. */
  4049. [[nodiscard]] static constexpr entity_type to_integral(const value_type value) ENTT_NOEXCEPT {
  4050. return static_cast<entity_type>(value);
  4051. }
  4052. /**
  4053. * @brief Returns the entity part once converted to the underlying type.
  4054. * @param value The value to convert.
  4055. * @return The integral representation of the entity part.
  4056. */
  4057. [[nodiscard]] static constexpr entity_type to_entity(const value_type value) ENTT_NOEXCEPT {
  4058. return (to_integral(value) & traits_type::entity_mask);
  4059. }
  4060. /**
  4061. * @brief Returns the version part once converted to the underlying type.
  4062. * @param value The value to convert.
  4063. * @return The integral representation of the version part.
  4064. */
  4065. [[nodiscard]] static constexpr version_type to_version(const value_type value) ENTT_NOEXCEPT {
  4066. constexpr auto mask = (traits_type::version_mask << traits_type::entity_shift);
  4067. return ((to_integral(value) & mask) >> traits_type::entity_shift);
  4068. }
  4069. /**
  4070. * @brief Constructs an identifier from its parts.
  4071. *
  4072. * If the version part is not provided, a tombstone is returned.<br/>
  4073. * If the entity part is not provided, a null identifier is returned.
  4074. *
  4075. * @param entity The entity part of the identifier.
  4076. * @param version The version part of the identifier.
  4077. * @return A properly constructed identifier.
  4078. */
  4079. [[nodiscard]] static constexpr value_type construct(const entity_type entity = traits_type::entity_mask, const version_type version = traits_type::version_mask) ENTT_NOEXCEPT {
  4080. return value_type{(entity & traits_type::entity_mask) | (version << traits_type::entity_shift)};
  4081. }
  4082. };
  4083. /**
  4084. * @brief Converts an entity to its underlying type.
  4085. * @tparam Entity The value type.
  4086. * @param entity The value to convert.
  4087. * @return The integral representation of the given value.
  4088. */
  4089. template<typename Entity>
  4090. [[nodiscard]] constexpr auto to_integral(const Entity entity) ENTT_NOEXCEPT {
  4091. return entt_traits<Entity>::to_integral(entity);
  4092. }
  4093. /*! @brief Null object for all entity identifiers. */
  4094. struct null_t {
  4095. /**
  4096. * @brief Converts the null object to identifiers of any type.
  4097. * @tparam Entity Type of entity identifier.
  4098. * @return The null representation for the given type.
  4099. */
  4100. template<typename Entity>
  4101. [[nodiscard]] constexpr operator Entity() const ENTT_NOEXCEPT {
  4102. return entt_traits<Entity>::construct();
  4103. }
  4104. /**
  4105. * @brief Compares two null objects.
  4106. * @param other A null object.
  4107. * @return True in all cases.
  4108. */
  4109. [[nodiscard]] constexpr bool operator==([[maybe_unused]] const null_t other) const ENTT_NOEXCEPT {
  4110. return true;
  4111. }
  4112. /**
  4113. * @brief Compares two null objects.
  4114. * @param other A null object.
  4115. * @return False in all cases.
  4116. */
  4117. [[nodiscard]] constexpr bool operator!=([[maybe_unused]] const null_t other) const ENTT_NOEXCEPT {
  4118. return false;
  4119. }
  4120. /**
  4121. * @brief Compares a null object and an entity identifier of any type.
  4122. * @tparam Entity Type of entity identifier.
  4123. * @param entity Entity identifier with which to compare.
  4124. * @return False if the two elements differ, true otherwise.
  4125. */
  4126. template<typename Entity>
  4127. [[nodiscard]] constexpr bool operator==(const Entity entity) const ENTT_NOEXCEPT {
  4128. return entt_traits<Entity>::to_entity(entity) == entt_traits<Entity>::to_entity(*this);
  4129. }
  4130. /**
  4131. * @brief Compares a null object and an entity identifier of any type.
  4132. * @tparam Entity Type of entity identifier.
  4133. * @param entity Entity identifier with which to compare.
  4134. * @return True if the two elements differ, false otherwise.
  4135. */
  4136. template<typename Entity>
  4137. [[nodiscard]] constexpr bool operator!=(const Entity entity) const ENTT_NOEXCEPT {
  4138. return !(entity == *this);
  4139. }
  4140. /**
  4141. * @brief Creates a null object from an entity identifier of any type.
  4142. * @tparam Entity Type of entity identifier.
  4143. * @param entity Entity identifier to turn into a null object.
  4144. * @return The null representation for the given identifier.
  4145. */
  4146. template<typename Entity>
  4147. [[nodiscard]] constexpr Entity operator|(const Entity entity) const ENTT_NOEXCEPT {
  4148. return entt_traits<Entity>::construct(entt_traits<Entity>::to_entity(*this), entt_traits<Entity>::to_version(entity));
  4149. }
  4150. };
  4151. /**
  4152. * @brief Compares a null object and an entity identifier of any type.
  4153. * @tparam Entity Type of entity identifier.
  4154. * @param entity Entity identifier with which to compare.
  4155. * @param other A null object yet to be converted.
  4156. * @return False if the two elements differ, true otherwise.
  4157. */
  4158. template<typename Entity>
  4159. [[nodiscard]] constexpr bool operator==(const Entity entity, const null_t other) ENTT_NOEXCEPT {
  4160. return other.operator==(entity);
  4161. }
  4162. /**
  4163. * @brief Compares a null object and an entity identifier of any type.
  4164. * @tparam Entity Type of entity identifier.
  4165. * @param entity Entity identifier with which to compare.
  4166. * @param other A null object yet to be converted.
  4167. * @return True if the two elements differ, false otherwise.
  4168. */
  4169. template<typename Entity>
  4170. [[nodiscard]] constexpr bool operator!=(const Entity entity, const null_t other) ENTT_NOEXCEPT {
  4171. return !(other == entity);
  4172. }
  4173. /*! @brief Tombstone object for all entity identifiers. */
  4174. struct tombstone_t {
  4175. /**
  4176. * @brief Converts the tombstone object to identifiers of any type.
  4177. * @tparam Entity Type of entity identifier.
  4178. * @return The tombstone representation for the given type.
  4179. */
  4180. template<typename Entity>
  4181. [[nodiscard]] constexpr operator Entity() const ENTT_NOEXCEPT {
  4182. return entt_traits<Entity>::construct();
  4183. }
  4184. /**
  4185. * @brief Compares two tombstone objects.
  4186. * @param other A tombstone object.
  4187. * @return True in all cases.
  4188. */
  4189. [[nodiscard]] constexpr bool operator==([[maybe_unused]] const tombstone_t other) const ENTT_NOEXCEPT {
  4190. return true;
  4191. }
  4192. /**
  4193. * @brief Compares two tombstone objects.
  4194. * @param other A tombstone object.
  4195. * @return False in all cases.
  4196. */
  4197. [[nodiscard]] constexpr bool operator!=([[maybe_unused]] const tombstone_t other) const ENTT_NOEXCEPT {
  4198. return false;
  4199. }
  4200. /**
  4201. * @brief Compares a tombstone object and an entity identifier of any type.
  4202. * @tparam Entity Type of entity identifier.
  4203. * @param entity Entity identifier with which to compare.
  4204. * @return False if the two elements differ, true otherwise.
  4205. */
  4206. template<typename Entity>
  4207. [[nodiscard]] constexpr bool operator==(const Entity entity) const ENTT_NOEXCEPT {
  4208. return entt_traits<Entity>::to_version(entity) == entt_traits<Entity>::to_version(*this);
  4209. }
  4210. /**
  4211. * @brief Compares a tombstone object and an entity identifier of any type.
  4212. * @tparam Entity Type of entity identifier.
  4213. * @param entity Entity identifier with which to compare.
  4214. * @return True if the two elements differ, false otherwise.
  4215. */
  4216. template<typename Entity>
  4217. [[nodiscard]] constexpr bool operator!=(const Entity entity) const ENTT_NOEXCEPT {
  4218. return !(entity == *this);
  4219. }
  4220. /**
  4221. * @brief Creates a tombstone object from an entity identifier of any type.
  4222. * @tparam Entity Type of entity identifier.
  4223. * @param entity Entity identifier to turn into a tombstone object.
  4224. * @return The tombstone representation for the given identifier.
  4225. */
  4226. template<typename Entity>
  4227. [[nodiscard]] constexpr Entity operator|(const Entity entity) const ENTT_NOEXCEPT {
  4228. return entt_traits<Entity>::construct(entt_traits<Entity>::to_entity(entity));
  4229. }
  4230. };
  4231. /**
  4232. * @brief Compares a tombstone object and an entity identifier of any type.
  4233. * @tparam Entity Type of entity identifier.
  4234. * @param entity Entity identifier with which to compare.
  4235. * @param other A tombstone object yet to be converted.
  4236. * @return False if the two elements differ, true otherwise.
  4237. */
  4238. template<typename Entity>
  4239. [[nodiscard]] constexpr bool operator==(const Entity entity, const tombstone_t other) ENTT_NOEXCEPT {
  4240. return other.operator==(entity);
  4241. }
  4242. /**
  4243. * @brief Compares a tombstone object and an entity identifier of any type.
  4244. * @tparam Entity Type of entity identifier.
  4245. * @param entity Entity identifier with which to compare.
  4246. * @param other A tombstone object yet to be converted.
  4247. * @return True if the two elements differ, false otherwise.
  4248. */
  4249. template<typename Entity>
  4250. [[nodiscard]] constexpr bool operator!=(const Entity entity, const tombstone_t other) ENTT_NOEXCEPT {
  4251. return !(other == entity);
  4252. }
  4253. /**
  4254. * @brief Compile-time constant for null entities.
  4255. *
  4256. * There exist implicit conversions from this variable to entity identifiers of
  4257. * any allowed type. Similarly, there exist comparision operators between the
  4258. * null entity and any other entity identifier.
  4259. */
  4260. inline constexpr null_t null{};
  4261. /**
  4262. * @brief Compile-time constant for tombstone entities.
  4263. *
  4264. * There exist implicit conversions from this variable to entity identifiers of
  4265. * any allowed type. Similarly, there exist comparision operators between the
  4266. * tombstone entity and any other entity identifier.
  4267. */
  4268. inline constexpr tombstone_t tombstone{};
  4269. }
  4270. #endif
  4271. // #include "fwd.hpp"
  4272. #ifndef ENTT_ENTITY_FWD_HPP
  4273. #define ENTT_ENTITY_FWD_HPP
  4274. #include <memory>
  4275. // #include "../core/fwd.hpp"
  4276. #ifndef ENTT_CORE_FWD_HPP
  4277. #define ENTT_CORE_FWD_HPP
  4278. #include <type_traits>
  4279. // #include "../config/config.h"
  4280. namespace entt {
  4281. template<std::size_t Len = sizeof(double[2]), std::size_t = alignof(typename std::aligned_storage_t<Len + !Len>)>
  4282. class basic_any;
  4283. /*! @brief Alias declaration for type identifiers. */
  4284. using id_type = ENTT_ID_TYPE;
  4285. /*! @brief Alias declaration for the most common use case. */
  4286. using any = basic_any<>;
  4287. }
  4288. #endif
  4289. namespace entt {
  4290. template<typename Entity, typename = std::allocator<Entity>>
  4291. class basic_sparse_set;
  4292. template<typename, typename Type, typename = std::allocator<Type>>
  4293. struct basic_storage;
  4294. template<typename>
  4295. class basic_registry;
  4296. template<typename...>
  4297. struct basic_view;
  4298. template<typename>
  4299. class basic_runtime_view;
  4300. template<typename...>
  4301. class basic_group;
  4302. template<typename>
  4303. class basic_observer;
  4304. template<typename>
  4305. class basic_organizer;
  4306. template<typename, typename...>
  4307. struct basic_handle;
  4308. template<typename>
  4309. class basic_snapshot;
  4310. template<typename>
  4311. class basic_snapshot_loader;
  4312. template<typename>
  4313. class basic_continuous_loader;
  4314. /*! @brief Default entity identifier. */
  4315. enum class entity: id_type {};
  4316. /*! @brief Alias declaration for the most common use case. */
  4317. using sparse_set = basic_sparse_set<entity>;
  4318. /**
  4319. * @brief Alias declaration for the most common use case.
  4320. * @tparam Args Other template parameters.
  4321. */
  4322. template<typename... Args>
  4323. using storage = basic_storage<entity, Args...>;
  4324. /*! @brief Alias declaration for the most common use case. */
  4325. using registry = basic_registry<entity>;
  4326. /*! @brief Alias declaration for the most common use case. */
  4327. using observer = basic_observer<entity>;
  4328. /*! @brief Alias declaration for the most common use case. */
  4329. using organizer = basic_organizer<entity>;
  4330. /*! @brief Alias declaration for the most common use case. */
  4331. using handle = basic_handle<entity>;
  4332. /*! @brief Alias declaration for the most common use case. */
  4333. using const_handle = basic_handle<const entity>;
  4334. /**
  4335. * @brief Alias declaration for the most common use case.
  4336. * @tparam Args Other template parameters.
  4337. */
  4338. template<typename... Args>
  4339. using handle_view = basic_handle<entity, Args...>;
  4340. /**
  4341. * @brief Alias declaration for the most common use case.
  4342. * @tparam Args Other template parameters.
  4343. */
  4344. template<typename... Args>
  4345. using const_handle_view = basic_handle<const entity, Args...>;
  4346. /*! @brief Alias declaration for the most common use case. */
  4347. using snapshot = basic_snapshot<entity>;
  4348. /*! @brief Alias declaration for the most common use case. */
  4349. using snapshot_loader = basic_snapshot_loader<entity>;
  4350. /*! @brief Alias declaration for the most common use case. */
  4351. using continuous_loader = basic_continuous_loader<entity>;
  4352. /**
  4353. * @brief Alias declaration for the most common use case.
  4354. * @tparam Args Other template parameters.
  4355. */
  4356. template<typename... Args>
  4357. using view = basic_view<entity, Args...>;
  4358. /*! @brief Alias declaration for the most common use case. */
  4359. using runtime_view = basic_runtime_view<entity>;
  4360. /**
  4361. * @brief Alias declaration for the most common use case.
  4362. * @tparam Args Other template parameters.
  4363. */
  4364. template<typename... Args>
  4365. using group = basic_group<entity, Args...>;
  4366. }
  4367. #endif
  4368. // #include "sparse_set.hpp"
  4369. #ifndef ENTT_ENTITY_SPARSE_SET_HPP
  4370. #define ENTT_ENTITY_SPARSE_SET_HPP
  4371. #include <cstddef>
  4372. #include <iterator>
  4373. #include <memory>
  4374. #include <type_traits>
  4375. #include <utility>
  4376. // #include "../config/config.h"
  4377. // #include "../core/algorithm.hpp"
  4378. #ifndef ENTT_CORE_ALGORITHM_HPP
  4379. #define ENTT_CORE_ALGORITHM_HPP
  4380. #include <vector>
  4381. #include <utility>
  4382. #include <iterator>
  4383. #include <algorithm>
  4384. #include <functional>
  4385. // #include "utility.hpp"
  4386. #ifndef ENTT_CORE_UTILITY_HPP
  4387. #define ENTT_CORE_UTILITY_HPP
  4388. #include <utility>
  4389. // #include "../config/config.h"
  4390. namespace entt {
  4391. /*! @brief Identity function object (waiting for C++20). */
  4392. struct identity {
  4393. /**
  4394. * @brief Returns its argument unchanged.
  4395. * @tparam Type Type of the argument.
  4396. * @param value The actual argument.
  4397. * @return The submitted value as-is.
  4398. */
  4399. template<class Type>
  4400. [[nodiscard]] constexpr Type && operator()(Type &&value) const ENTT_NOEXCEPT {
  4401. return std::forward<Type>(value);
  4402. }
  4403. };
  4404. /**
  4405. * @brief Constant utility to disambiguate overloaded members of a class.
  4406. * @tparam Type Type of the desired overload.
  4407. * @tparam Class Type of class to which the member belongs.
  4408. * @param member A valid pointer to a member.
  4409. * @return Pointer to the member.
  4410. */
  4411. template<typename Type, typename Class>
  4412. [[nodiscard]] constexpr auto overload(Type Class:: *member) ENTT_NOEXCEPT { return member; }
  4413. /**
  4414. * @brief Constant utility to disambiguate overloaded functions.
  4415. * @tparam Func Function type of the desired overload.
  4416. * @param func A valid pointer to a function.
  4417. * @return Pointer to the function.
  4418. */
  4419. template<typename Func>
  4420. [[nodiscard]] constexpr auto overload(Func *func) ENTT_NOEXCEPT { return func; }
  4421. /**
  4422. * @brief Helper type for visitors.
  4423. * @tparam Func Types of function objects.
  4424. */
  4425. template<class... Func>
  4426. struct overloaded: Func... {
  4427. using Func::operator()...;
  4428. };
  4429. /**
  4430. * @brief Deduction guide.
  4431. * @tparam Func Types of function objects.
  4432. */
  4433. template<class... Func>
  4434. overloaded(Func...)
  4435. -> overloaded<Func...>;
  4436. /**
  4437. * @brief Basic implementation of a y-combinator.
  4438. * @tparam Func Type of a potentially recursive function.
  4439. */
  4440. template<class Func>
  4441. struct y_combinator {
  4442. /**
  4443. * @brief Constructs a y-combinator from a given function.
  4444. * @param recursive A potentially recursive function.
  4445. */
  4446. y_combinator(Func recursive):
  4447. func{std::move(recursive)}
  4448. {}
  4449. /**
  4450. * @brief Invokes a y-combinator and therefore its underlying function.
  4451. * @tparam Args Types of arguments to use to invoke the underlying function.
  4452. * @param args Parameters to use to invoke the underlying function.
  4453. * @return Return value of the underlying function, if any.
  4454. */
  4455. template <class... Args>
  4456. decltype(auto) operator()(Args &&... args) const {
  4457. return func(*this, std::forward<Args>(args)...);
  4458. }
  4459. /*! @copydoc operator()() */
  4460. template <class... Args>
  4461. decltype(auto) operator()(Args &&... args) {
  4462. return func(*this, std::forward<Args>(args)...);
  4463. }
  4464. private:
  4465. Func func;
  4466. };
  4467. }
  4468. #endif
  4469. namespace entt {
  4470. /**
  4471. * @brief Function object to wrap `std::sort` in a class type.
  4472. *
  4473. * Unfortunately, `std::sort` cannot be passed as template argument to a class
  4474. * template or a function template.<br/>
  4475. * This class fills the gap by wrapping some flavors of `std::sort` in a
  4476. * function object.
  4477. */
  4478. struct std_sort {
  4479. /**
  4480. * @brief Sorts the elements in a range.
  4481. *
  4482. * Sorts the elements in a range using the given binary comparison function.
  4483. *
  4484. * @tparam It Type of random access iterator.
  4485. * @tparam Compare Type of comparison function object.
  4486. * @tparam Args Types of arguments to forward to the sort function.
  4487. * @param first An iterator to the first element of the range to sort.
  4488. * @param last An iterator past the last element of the range to sort.
  4489. * @param compare A valid comparison function object.
  4490. * @param args Arguments to forward to the sort function, if any.
  4491. */
  4492. template<typename It, typename Compare = std::less<>, typename... Args>
  4493. void operator()(It first, It last, Compare compare = Compare{}, Args &&... args) const {
  4494. std::sort(std::forward<Args>(args)..., std::move(first), std::move(last), std::move(compare));
  4495. }
  4496. };
  4497. /*! @brief Function object for performing insertion sort. */
  4498. struct insertion_sort {
  4499. /**
  4500. * @brief Sorts the elements in a range.
  4501. *
  4502. * Sorts the elements in a range using the given binary comparison function.
  4503. *
  4504. * @tparam It Type of random access iterator.
  4505. * @tparam Compare Type of comparison function object.
  4506. * @param first An iterator to the first element of the range to sort.
  4507. * @param last An iterator past the last element of the range to sort.
  4508. * @param compare A valid comparison function object.
  4509. */
  4510. template<typename It, typename Compare = std::less<>>
  4511. void operator()(It first, It last, Compare compare = Compare{}) const {
  4512. if(first < last) {
  4513. for(auto it = first + 1; it < last; ++it) {
  4514. auto value = std::move(*it);
  4515. auto pre = it;
  4516. for(; pre > first && compare(value, *(pre-1)); --pre) {
  4517. *pre = std::move(*(pre-1));
  4518. }
  4519. *pre = std::move(value);
  4520. }
  4521. }
  4522. }
  4523. };
  4524. /**
  4525. * @brief Function object for performing LSD radix sort.
  4526. * @tparam Bit Number of bits processed per pass.
  4527. * @tparam N Maximum number of bits to sort.
  4528. */
  4529. template<std::size_t Bit, std::size_t N>
  4530. struct radix_sort {
  4531. static_assert((N % Bit) == 0, "The maximum number of bits to sort must be a multiple of the number of bits processed per pass");
  4532. /**
  4533. * @brief Sorts the elements in a range.
  4534. *
  4535. * Sorts the elements in a range using the given _getter_ to access the
  4536. * actual data to be sorted.
  4537. *
  4538. * This implementation is inspired by the online book
  4539. * [Physically Based Rendering](http://www.pbr-book.org/3ed-2018/Primitives_and_Intersection_Acceleration/Bounding_Volume_Hierarchies.html#RadixSort).
  4540. *
  4541. * @tparam It Type of random access iterator.
  4542. * @tparam Getter Type of _getter_ function object.
  4543. * @param first An iterator to the first element of the range to sort.
  4544. * @param last An iterator past the last element of the range to sort.
  4545. * @param getter A valid _getter_ function object.
  4546. */
  4547. template<typename It, typename Getter = identity>
  4548. void operator()(It first, It last, Getter getter = Getter{}) const {
  4549. if(first < last) {
  4550. static constexpr auto mask = (1 << Bit) - 1;
  4551. static constexpr auto buckets = 1 << Bit;
  4552. static constexpr auto passes = N / Bit;
  4553. using value_type = typename std::iterator_traits<It>::value_type;
  4554. std::vector<value_type> aux(std::distance(first, last));
  4555. auto part = [getter = std::move(getter)](auto from, auto to, auto out, auto start) {
  4556. std::size_t index[buckets]{};
  4557. std::size_t count[buckets]{};
  4558. for(auto it = from; it != to; ++it) {
  4559. ++count[(getter(*it) >> start) & mask];
  4560. }
  4561. for(std::size_t pos{}, end = buckets - 1u; pos < end; ++pos) {
  4562. index[pos + 1u] = index[pos] + count[pos];
  4563. }
  4564. for(auto it = from; it != to; ++it) {
  4565. out[index[(getter(*it) >> start) & mask]++] = std::move(*it);
  4566. }
  4567. };
  4568. for(std::size_t pass = 0; pass < (passes & ~1); pass += 2) {
  4569. part(first, last, aux.begin(), pass * Bit);
  4570. part(aux.begin(), aux.end(), first, (pass + 1) * Bit);
  4571. }
  4572. if constexpr(passes & 1) {
  4573. part(first, last, aux.begin(), (passes - 1) * Bit);
  4574. std::move(aux.begin(), aux.end(), first);
  4575. }
  4576. }
  4577. }
  4578. };
  4579. }
  4580. #endif
  4581. // #include "../core/fwd.hpp"
  4582. // #include "entity.hpp"
  4583. // #include "fwd.hpp"
  4584. namespace entt {
  4585. /*! @brief Sparse set deletion policy. */
  4586. enum class deletion_policy: std::uint8_t {
  4587. /*! @brief Swap-and-pop deletion policy. */
  4588. swap_and_pop = 0u,
  4589. /*! @brief In-place deletion policy. */
  4590. in_place = 1u
  4591. };
  4592. /**
  4593. * @brief Basic sparse set implementation.
  4594. *
  4595. * Sparse set or packed array or whatever is the name users give it.<br/>
  4596. * Two arrays: an _external_ one and an _internal_ one; a _sparse_ one and a
  4597. * _packed_ one; one used for direct access through contiguous memory, the other
  4598. * one used to get the data through an extra level of indirection.<br/>
  4599. * This is largely used by the registry to offer users the fastest access ever
  4600. * to the components. Views and groups in general are almost entirely designed
  4601. * around sparse sets.
  4602. *
  4603. * This type of data structure is widely documented in the literature and on the
  4604. * web. This is nothing more than a customized implementation suitable for the
  4605. * purpose of the framework.
  4606. *
  4607. * @note
  4608. * Internal data structures arrange elements to maximize performance. There are
  4609. * no guarantees that entities are returned in the insertion order when iterate
  4610. * a sparse set. Do not make assumption on the order in any case.
  4611. *
  4612. * @tparam Entity A valid entity type (see entt_traits for more details).
  4613. * @tparam Allocator Type of allocator used to manage memory and elements.
  4614. */
  4615. template<typename Entity, typename Allocator>
  4616. class basic_sparse_set {
  4617. static constexpr auto growth_factor = 1.5;
  4618. static constexpr auto sparse_page = ENTT_SPARSE_PAGE;
  4619. using traits_type = entt_traits<Entity>;
  4620. using alloc_traits = typename std::allocator_traits<Allocator>::template rebind_traits<Entity>;
  4621. using alloc_pointer = typename alloc_traits::pointer;
  4622. using alloc_const_pointer = typename alloc_traits::const_pointer;
  4623. using bucket_alloc_traits = typename std::allocator_traits<Allocator>::template rebind_traits<alloc_pointer>;
  4624. using bucket_alloc_pointer = typename bucket_alloc_traits::pointer;
  4625. static_assert(alloc_traits::propagate_on_container_move_assignment::value);
  4626. static_assert(bucket_alloc_traits::propagate_on_container_move_assignment::value);
  4627. struct sparse_set_iterator final {
  4628. using difference_type = typename traits_type::difference_type;
  4629. using value_type = Entity;
  4630. using pointer = const value_type *;
  4631. using reference = const value_type &;
  4632. using iterator_category = std::random_access_iterator_tag;
  4633. sparse_set_iterator() ENTT_NOEXCEPT = default;
  4634. sparse_set_iterator(const alloc_const_pointer *ref, const difference_type idx) ENTT_NOEXCEPT
  4635. : packed{ref},
  4636. index{idx}
  4637. {}
  4638. sparse_set_iterator & operator++() ENTT_NOEXCEPT {
  4639. return --index, *this;
  4640. }
  4641. sparse_set_iterator operator++(int) ENTT_NOEXCEPT {
  4642. iterator orig = *this;
  4643. return ++(*this), orig;
  4644. }
  4645. sparse_set_iterator & operator--() ENTT_NOEXCEPT {
  4646. return ++index, *this;
  4647. }
  4648. sparse_set_iterator operator--(int) ENTT_NOEXCEPT {
  4649. sparse_set_iterator orig = *this;
  4650. return operator--(), orig;
  4651. }
  4652. sparse_set_iterator & operator+=(const difference_type value) ENTT_NOEXCEPT {
  4653. index -= value;
  4654. return *this;
  4655. }
  4656. sparse_set_iterator operator+(const difference_type value) const ENTT_NOEXCEPT {
  4657. sparse_set_iterator copy = *this;
  4658. return (copy += value);
  4659. }
  4660. sparse_set_iterator & operator-=(const difference_type value) ENTT_NOEXCEPT {
  4661. return (*this += -value);
  4662. }
  4663. sparse_set_iterator operator-(const difference_type value) const ENTT_NOEXCEPT {
  4664. return (*this + -value);
  4665. }
  4666. difference_type operator-(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  4667. return other.index - index;
  4668. }
  4669. [[nodiscard]] reference operator[](const difference_type value) const {
  4670. const auto pos = size_type(index-value-1u);
  4671. return (*packed)[pos];
  4672. }
  4673. [[nodiscard]] bool operator==(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  4674. return other.index == index;
  4675. }
  4676. [[nodiscard]] bool operator!=(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  4677. return !(*this == other);
  4678. }
  4679. [[nodiscard]] bool operator<(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  4680. return index > other.index;
  4681. }
  4682. [[nodiscard]] bool operator>(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  4683. return index < other.index;
  4684. }
  4685. [[nodiscard]] bool operator<=(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  4686. return !(*this > other);
  4687. }
  4688. [[nodiscard]] bool operator>=(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  4689. return !(*this < other);
  4690. }
  4691. [[nodiscard]] pointer operator->() const {
  4692. const auto pos = size_type(index-1u);
  4693. return std::addressof((*packed)[pos]);
  4694. }
  4695. [[nodiscard]] reference operator*() const {
  4696. return *operator->();
  4697. }
  4698. private:
  4699. const alloc_const_pointer *packed;
  4700. difference_type index;
  4701. };
  4702. [[nodiscard]] static auto page(const Entity entt) ENTT_NOEXCEPT {
  4703. return size_type{traits_type::to_entity(entt) / sparse_page};
  4704. }
  4705. [[nodiscard]] static auto offset(const Entity entt) ENTT_NOEXCEPT {
  4706. return size_type{traits_type::to_entity(entt) & (sparse_page - 1)};
  4707. }
  4708. [[nodiscard]] auto assure_page(const std::size_t idx) {
  4709. if(!(idx < bucket)) {
  4710. const size_type sz = idx + 1u;
  4711. const auto mem = bucket_alloc_traits::allocate(bucket_allocator, sz);
  4712. std::uninitialized_value_construct(mem + bucket, mem + sz);
  4713. std::uninitialized_copy(sparse, sparse + bucket, mem);
  4714. std::destroy(sparse, sparse + bucket);
  4715. bucket_alloc_traits::deallocate(bucket_allocator, sparse, bucket);
  4716. sparse = mem;
  4717. bucket = sz;
  4718. }
  4719. if(!sparse[idx]) {
  4720. sparse[idx] = alloc_traits::allocate(allocator, sparse_page);
  4721. std::uninitialized_fill(sparse[idx], sparse[idx] + sparse_page, null);
  4722. }
  4723. return sparse[idx];
  4724. }
  4725. void resize_packed(const std::size_t req) {
  4726. ENTT_ASSERT((req != reserved) && !(req < count), "Invalid request");
  4727. const auto mem = alloc_traits::allocate(allocator, req);
  4728. std::uninitialized_copy(packed, packed + count, mem);
  4729. std::uninitialized_fill(mem + count, mem + req, tombstone);
  4730. std::destroy(packed, packed + reserved);
  4731. alloc_traits::deallocate(allocator, packed, reserved);
  4732. packed = mem;
  4733. reserved = req;
  4734. }
  4735. void release_memory() {
  4736. if(packed) {
  4737. for(size_type pos{}; pos < bucket; ++pos) {
  4738. if(sparse[pos]) {
  4739. std::destroy(sparse[pos], sparse[pos] + sparse_page);
  4740. alloc_traits::deallocate(allocator, sparse[pos], sparse_page);
  4741. }
  4742. }
  4743. std::destroy(packed, packed + reserved);
  4744. std::destroy(sparse, sparse + bucket);
  4745. alloc_traits::deallocate(allocator, packed, reserved);
  4746. bucket_alloc_traits::deallocate(bucket_allocator, sparse, bucket);
  4747. }
  4748. }
  4749. protected:
  4750. /**
  4751. * @brief Swaps two entities in the internal packed array.
  4752. * @param lhs A valid position of an entity within storage.
  4753. * @param rhs A valid position of an entity within storage.
  4754. */
  4755. virtual void swap_at([[maybe_unused]] const std::size_t lhs, [[maybe_unused]] const std::size_t rhs) {}
  4756. /**
  4757. * @brief Moves an entity in the internal packed array.
  4758. * @param from A valid position of an entity within storage.
  4759. * @param to A valid position of an entity within storage.
  4760. */
  4761. virtual void move_and_pop([[maybe_unused]] const std::size_t from, [[maybe_unused]] const std::size_t to) {}
  4762. /**
  4763. * @brief Attempts to erase an entity from the internal packed array.
  4764. * @param entt A valid entity identifier.
  4765. * @param ud Optional user data that are forwarded as-is to derived classes.
  4766. */
  4767. virtual void swap_and_pop(const Entity entt, [[maybe_unused]] void *ud) {
  4768. auto &ref = sparse[page(entt)][offset(entt)];
  4769. const auto pos = size_type{traits_type::to_entity(ref)};
  4770. ENTT_ASSERT(packed[pos] == entt, "Invalid entity identifier");
  4771. auto &last = packed[--count];
  4772. packed[pos] = last;
  4773. sparse[page(last)][offset(last)] = ref;
  4774. // lazy self-assignment guard
  4775. ref = null;
  4776. // unnecessary but it helps to detect nasty bugs
  4777. ENTT_ASSERT((last = tombstone, true), "");
  4778. }
  4779. /**
  4780. * @brief Attempts to erase an entity from the internal packed array.
  4781. * @param entt A valid entity identifier.
  4782. * @param ud Optional user data that are forwarded as-is to derived classes.
  4783. */
  4784. virtual void in_place_pop(const Entity entt, [[maybe_unused]] void *ud) {
  4785. auto &ref = sparse[page(entt)][offset(entt)];
  4786. const auto pos = size_type{traits_type::to_entity(ref)};
  4787. ENTT_ASSERT(packed[pos] == entt, "Invalid entity identifier");
  4788. packed[pos] = std::exchange(free_list, traits_type::construct(static_cast<typename traits_type::entity_type>(pos)));
  4789. // lazy self-assignment guard
  4790. ref = null;
  4791. }
  4792. public:
  4793. /*! @brief Allocator type. */
  4794. using allocator_type = typename alloc_traits::allocator_type;
  4795. /*! @brief Underlying entity identifier. */
  4796. using entity_type = Entity;
  4797. /*! @brief Unsigned integer type. */
  4798. using size_type = std::size_t;
  4799. /*! @brief Pointer type to contained entities. */
  4800. using pointer = alloc_const_pointer;
  4801. /*! @brief Random access iterator type. */
  4802. using iterator = sparse_set_iterator;
  4803. /*! @brief Reverse iterator type. */
  4804. using reverse_iterator = std::reverse_iterator<iterator>;
  4805. /**
  4806. * @brief Constructs an empty container with the given policy and allocator.
  4807. * @param pol Type of deletion policy.
  4808. * @param alloc Allocator to use (possibly default-constructed).
  4809. */
  4810. explicit basic_sparse_set(deletion_policy pol, const allocator_type &alloc = {})
  4811. : allocator{alloc},
  4812. bucket_allocator{alloc},
  4813. sparse{bucket_alloc_traits::allocate(bucket_allocator, 0u)},
  4814. packed{alloc_traits::allocate(allocator, 0u)},
  4815. bucket{0u},
  4816. count{0u},
  4817. reserved{0u},
  4818. free_list{tombstone},
  4819. mode{pol}
  4820. {}
  4821. /**
  4822. * @brief Constructs an empty container with the given allocator.
  4823. * @param alloc Allocator to use (possibly default-constructed).
  4824. */
  4825. explicit basic_sparse_set(const allocator_type &alloc = {})
  4826. : basic_sparse_set{deletion_policy::swap_and_pop, alloc}
  4827. {}
  4828. /**
  4829. * @brief Move constructor.
  4830. * @param other The instance to move from.
  4831. */
  4832. basic_sparse_set(basic_sparse_set &&other) ENTT_NOEXCEPT
  4833. : allocator{std::move(other.allocator)},
  4834. bucket_allocator{std::move(other.bucket_allocator)},
  4835. sparse{std::exchange(other.sparse, bucket_alloc_pointer{})},
  4836. packed{std::exchange(other.packed, alloc_pointer{})},
  4837. bucket{std::exchange(other.bucket, 0u)},
  4838. count{std::exchange(other.count, 0u)},
  4839. reserved{std::exchange(other.reserved, 0u)},
  4840. free_list{std::exchange(other.free_list, tombstone)},
  4841. mode{other.mode}
  4842. {}
  4843. /*! @brief Default destructor. */
  4844. virtual ~basic_sparse_set() {
  4845. release_memory();
  4846. }
  4847. /**
  4848. * @brief Move assignment operator.
  4849. * @param other The instance to move from.
  4850. * @return This sparse set.
  4851. */
  4852. basic_sparse_set & operator=(basic_sparse_set &&other) ENTT_NOEXCEPT {
  4853. release_memory();
  4854. allocator = std::move(other.allocator);
  4855. bucket_allocator = std::move(other.bucket_allocator);
  4856. sparse = std::exchange(other.sparse, bucket_alloc_pointer{});
  4857. packed = std::exchange(other.packed, alloc_pointer{});
  4858. bucket = std::exchange(other.bucket, 0u);
  4859. count = std::exchange(other.count, 0u);
  4860. reserved = std::exchange(other.reserved, 0u);
  4861. free_list = std::exchange(other.free_list, tombstone);
  4862. mode = other.mode;
  4863. return *this;
  4864. }
  4865. /**
  4866. * @brief Returns the deletion policy of a sparse set.
  4867. * @return The deletion policy of the sparse set.
  4868. */
  4869. [[nodiscard]] deletion_policy policy() const ENTT_NOEXCEPT {
  4870. return mode;
  4871. }
  4872. /**
  4873. * @brief Returns the next slot available for insertion.
  4874. * @return The next slot available for insertion.
  4875. */
  4876. [[nodiscard]] size_type slot() const ENTT_NOEXCEPT {
  4877. return free_list == null ? count : size_type{traits_type::to_entity(free_list)};
  4878. }
  4879. /**
  4880. * @brief Increases the capacity of a sparse set.
  4881. *
  4882. * If the new capacity is greater than the current capacity, new storage is
  4883. * allocated, otherwise the method does nothing.
  4884. *
  4885. * @param cap Desired capacity.
  4886. */
  4887. void reserve(const size_type cap) {
  4888. if(cap > reserved) {
  4889. resize_packed(cap);
  4890. }
  4891. }
  4892. /**
  4893. * @brief Returns the number of elements that a sparse set has currently
  4894. * allocated space for.
  4895. * @return Capacity of the sparse set.
  4896. */
  4897. [[nodiscard]] size_type capacity() const ENTT_NOEXCEPT {
  4898. return reserved;
  4899. }
  4900. /*! @brief Requests the removal of unused capacity. */
  4901. void shrink_to_fit() {
  4902. if(count < reserved) {
  4903. resize_packed(count);
  4904. }
  4905. }
  4906. /**
  4907. * @brief Returns the extent of a sparse set.
  4908. *
  4909. * The extent of a sparse set is also the size of the internal sparse array.
  4910. * There is no guarantee that the internal packed array has the same size.
  4911. * Usually the size of the internal sparse array is equal or greater than
  4912. * the one of the internal packed array.
  4913. *
  4914. * @return Extent of the sparse set.
  4915. */
  4916. [[nodiscard]] size_type extent() const ENTT_NOEXCEPT {
  4917. return bucket * sparse_page;
  4918. }
  4919. /**
  4920. * @brief Returns the number of elements in a sparse set.
  4921. *
  4922. * The number of elements is also the size of the internal packed array.
  4923. * There is no guarantee that the internal sparse array has the same size.
  4924. * Usually the size of the internal sparse array is equal or greater than
  4925. * the one of the internal packed array.
  4926. *
  4927. * @return Number of elements.
  4928. */
  4929. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  4930. return count;
  4931. }
  4932. /**
  4933. * @brief Checks whether a sparse set is empty.
  4934. * @return True if the sparse set is empty, false otherwise.
  4935. */
  4936. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  4937. return (count == size_type{});
  4938. }
  4939. /**
  4940. * @brief Direct access to the internal packed array.
  4941. * @return A pointer to the internal packed array.
  4942. */
  4943. [[nodiscard]] pointer data() const ENTT_NOEXCEPT {
  4944. return packed;
  4945. }
  4946. /**
  4947. * @brief Returns an iterator to the beginning.
  4948. *
  4949. * The returned iterator points to the first entity of the internal packed
  4950. * array. If the sparse set is empty, the returned iterator will be equal to
  4951. * `end()`.
  4952. *
  4953. * @return An iterator to the first entity of the internal packed array.
  4954. */
  4955. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  4956. return iterator{std::addressof(packed), static_cast<typename traits_type::difference_type>(count)};
  4957. }
  4958. /**
  4959. * @brief Returns an iterator to the end.
  4960. *
  4961. * The returned iterator points to the element following the last entity in
  4962. * the internal packed array. Attempting to dereference the returned
  4963. * iterator results in undefined behavior.
  4964. *
  4965. * @return An iterator to the element following the last entity of the
  4966. * internal packed array.
  4967. */
  4968. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  4969. return iterator{std::addressof(packed), {}};
  4970. }
  4971. /**
  4972. * @brief Returns a reverse iterator to the beginning.
  4973. *
  4974. * The returned iterator points to the first entity of the reversed internal
  4975. * packed array. If the sparse set is empty, the returned iterator will be
  4976. * equal to `rend()`.
  4977. *
  4978. * @return An iterator to the first entity of the reversed internal packed
  4979. * array.
  4980. */
  4981. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  4982. return std::make_reverse_iterator(end());
  4983. }
  4984. /**
  4985. * @brief Returns a reverse iterator to the end.
  4986. *
  4987. * The returned iterator points to the element following the last entity in
  4988. * the reversed internal packed array. Attempting to dereference the
  4989. * returned iterator results in undefined behavior.
  4990. *
  4991. * @return An iterator to the element following the last entity of the
  4992. * reversed internal packed array.
  4993. */
  4994. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  4995. return std::make_reverse_iterator(begin());
  4996. }
  4997. /**
  4998. * @brief Finds an entity.
  4999. * @param entt A valid entity identifier.
  5000. * @return An iterator to the given entity if it's found, past the end
  5001. * iterator otherwise.
  5002. */
  5003. [[nodiscard]] iterator find(const entity_type entt) const ENTT_NOEXCEPT {
  5004. return contains(entt) ? --(end() - index(entt)) : end();
  5005. }
  5006. /**
  5007. * @brief Checks if a sparse set contains an entity.
  5008. * @param entt A valid entity identifier.
  5009. * @return True if the sparse set contains the entity, false otherwise.
  5010. */
  5011. [[nodiscard]] bool contains(const entity_type entt) const ENTT_NOEXCEPT {
  5012. ENTT_ASSERT(entt != tombstone && entt != null, "Invalid entity");
  5013. const auto curr = page(entt);
  5014. // testing versions permits to avoid accessing the packed array
  5015. return (curr < bucket && sparse[curr] && sparse[curr][offset(entt)] != null);
  5016. }
  5017. /**
  5018. * @brief Returns the position of an entity in a sparse set.
  5019. *
  5020. * @warning
  5021. * Attempting to get the position of an entity that doesn't belong to the
  5022. * sparse set results in undefined behavior.
  5023. *
  5024. * @param entt A valid entity identifier.
  5025. * @return The position of the entity in the sparse set.
  5026. */
  5027. [[nodiscard]] size_type index(const entity_type entt) const ENTT_NOEXCEPT {
  5028. ENTT_ASSERT(contains(entt), "Set does not contain entity");
  5029. return size_type{traits_type::to_entity(sparse[page(entt)][offset(entt)])};
  5030. }
  5031. /**
  5032. * @brief Returns the entity at specified location, with bounds checking.
  5033. * @param pos The position for which to return the entity.
  5034. * @return The entity at specified location if any, a null entity otherwise.
  5035. */
  5036. [[nodiscard]] entity_type at(const size_type pos) const ENTT_NOEXCEPT {
  5037. return pos < count ? packed[pos] : null;
  5038. }
  5039. /**
  5040. * @brief Returns the entity at specified location, without bounds checking.
  5041. * @param pos The position for which to return the entity.
  5042. * @return The entity at specified location.
  5043. */
  5044. [[nodiscard]] entity_type operator[](const size_type pos) const ENTT_NOEXCEPT {
  5045. ENTT_ASSERT(pos < count, "Position is out of bounds");
  5046. return packed[pos];
  5047. }
  5048. /**
  5049. * @brief Appends an entity to a sparse set.
  5050. *
  5051. * @warning
  5052. * Attempting to assign an entity that already belongs to the sparse set
  5053. * results in undefined behavior.
  5054. *
  5055. * @param entt A valid entity identifier.
  5056. * @return The slot used for insertion.
  5057. */
  5058. size_type emplace_back(const entity_type entt) {
  5059. ENTT_ASSERT(!contains(entt), "Set already contains entity");
  5060. if(count == reserved) {
  5061. const size_type sz = static_cast<size_type>(reserved * growth_factor);
  5062. resize_packed(sz + !(sz > reserved));
  5063. }
  5064. assure_page(page(entt))[offset(entt)] = traits_type::construct(static_cast<typename traits_type::entity_type>(count));
  5065. packed[count] = entt;
  5066. return count++;
  5067. }
  5068. /**
  5069. * @brief Assigns an entity to a sparse set.
  5070. *
  5071. * @warning
  5072. * Attempting to assign an entity that already belongs to the sparse set
  5073. * results in undefined behavior.
  5074. *
  5075. * @param entt A valid entity identifier.
  5076. * @return The slot used for insertion.
  5077. */
  5078. size_type emplace(const entity_type entt) {
  5079. if(free_list == null) {
  5080. return emplace_back(entt);
  5081. } else {
  5082. ENTT_ASSERT(!contains(entt), "Set already contains entity");
  5083. const auto pos = size_type{traits_type::to_entity(free_list)};
  5084. sparse[page(entt)][offset(entt)] = traits_type::construct(static_cast<typename traits_type::entity_type>(pos));
  5085. free_list = std::exchange(packed[pos], entt);
  5086. return pos;
  5087. }
  5088. }
  5089. /**
  5090. * @brief Assigns one or more entities to a sparse set.
  5091. *
  5092. * @warning
  5093. * Attempting to assign an entity that already belongs to the sparse set
  5094. * results in undefined behavior.
  5095. *
  5096. * @tparam It Type of input iterator.
  5097. * @param first An iterator to the first element of the range of entities.
  5098. * @param last An iterator past the last element of the range of entities.
  5099. */
  5100. template<typename It>
  5101. void insert(It first, It last) {
  5102. reserve(count + std::distance(first, last));
  5103. for(; first != last; ++first) {
  5104. const auto entt = *first;
  5105. ENTT_ASSERT(!contains(entt), "Set already contains entity");
  5106. assure_page(page(entt))[offset(entt)] = traits_type::construct(static_cast<typename traits_type::entity_type>(count));
  5107. packed[count++] = entt;
  5108. }
  5109. }
  5110. /**
  5111. * @brief Erases an entity from a sparse set.
  5112. *
  5113. * @warning
  5114. * Attempting to erase an entity that doesn't belong to the sparse set
  5115. * results in undefined behavior.
  5116. *
  5117. * @param entt A valid entity identifier.
  5118. * @param ud Optional user data that are forwarded as-is to derived classes.
  5119. */
  5120. void erase(const entity_type entt, void *ud = nullptr) {
  5121. ENTT_ASSERT(contains(entt), "Set does not contain entity");
  5122. (mode == deletion_policy::in_place) ? in_place_pop(entt, ud) : swap_and_pop(entt, ud);
  5123. }
  5124. /**
  5125. * @brief Erases entities from a set.
  5126. *
  5127. * @sa erase
  5128. *
  5129. * @tparam It Type of input iterator.
  5130. * @param first An iterator to the first element of the range of entities.
  5131. * @param last An iterator past the last element of the range of entities.
  5132. * @param ud Optional user data that are forwarded as-is to derived classes.
  5133. */
  5134. template<typename It>
  5135. void erase(It first, It last, void *ud = nullptr) {
  5136. for(; first != last; ++first) {
  5137. erase(*first, ud);
  5138. }
  5139. }
  5140. /**
  5141. * @brief Removes an entity from a sparse set if it exists.
  5142. * @param entt A valid entity identifier.
  5143. * @param ud Optional user data that are forwarded as-is to derived classes.
  5144. * @return True if the entity is actually removed, false otherwise.
  5145. */
  5146. bool remove(const entity_type entt, void *ud = nullptr) {
  5147. return contains(entt) && (erase(entt, ud), true);
  5148. }
  5149. /**
  5150. * @brief Removes entities from a sparse set if they exist.
  5151. * @tparam It Type of input iterator.
  5152. * @param first An iterator to the first element of the range of entities.
  5153. * @param last An iterator past the last element of the range of entities.
  5154. * @param ud Optional user data that are forwarded as-is to derived classes.
  5155. * @return The number of entities actually removed.
  5156. */
  5157. template<typename It>
  5158. size_type remove(It first, It last, void *ud = nullptr) {
  5159. size_type found{};
  5160. for(; first != last; ++first) {
  5161. found += remove(*first, ud);
  5162. }
  5163. return found;
  5164. }
  5165. /*! @brief Removes all tombstones from the packed array of a sparse set. */
  5166. void compact() {
  5167. size_type next = count;
  5168. for(; next && packed[next - 1u] == tombstone; --next);
  5169. for(auto *it = &free_list; *it != null && next; it = std::addressof(packed[traits_type::to_entity(*it)])) {
  5170. if(const size_type pos = traits_type::to_entity(*it); pos < next) {
  5171. --next;
  5172. move_and_pop(next, pos);
  5173. std::swap(packed[next], packed[pos]);
  5174. sparse[page(packed[pos])][offset(packed[pos])] = traits_type::construct(static_cast<const typename traits_type::entity_type>(pos));
  5175. *it = traits_type::construct(static_cast<typename traits_type::entity_type>(next));
  5176. for(; next && packed[next - 1u] == tombstone; --next);
  5177. }
  5178. }
  5179. free_list = tombstone;
  5180. count = next;
  5181. }
  5182. /**
  5183. * @copybrief swap_at
  5184. *
  5185. * For what it's worth, this function affects both the internal sparse array
  5186. * and the internal packed array. Users should not care of that anyway.
  5187. *
  5188. * @warning
  5189. * Attempting to swap entities that don't belong to the sparse set results
  5190. * in undefined behavior.
  5191. *
  5192. * @param lhs A valid entity identifier.
  5193. * @param rhs A valid entity identifier.
  5194. */
  5195. void swap(const entity_type lhs, const entity_type rhs) {
  5196. ENTT_ASSERT(contains(lhs), "Set does not contain entity");
  5197. ENTT_ASSERT(contains(rhs), "Set does not contain entity");
  5198. auto &entt = sparse[page(lhs)][offset(lhs)];
  5199. auto &other = sparse[page(rhs)][offset(rhs)];
  5200. const auto from = size_type{traits_type::to_entity(entt)};
  5201. const auto to = size_type{traits_type::to_entity(other)};
  5202. // basic no-leak guarantee (with invalid state) if swapping throws
  5203. swap_at(from, to);
  5204. std::swap(entt, other);
  5205. std::swap(packed[from], packed[to]);
  5206. }
  5207. /**
  5208. * @brief Sort the first count elements according to the given comparison
  5209. * function.
  5210. *
  5211. * The comparison function object must return `true` if the first element
  5212. * is _less_ than the second one, `false` otherwise. The signature of the
  5213. * comparison function should be equivalent to the following:
  5214. *
  5215. * @code{.cpp}
  5216. * bool(const Entity, const Entity);
  5217. * @endcode
  5218. *
  5219. * Moreover, the comparison function object shall induce a
  5220. * _strict weak ordering_ on the values.
  5221. *
  5222. * The sort function object must offer a member function template
  5223. * `operator()` that accepts three arguments:
  5224. *
  5225. * * An iterator to the first element of the range to sort.
  5226. * * An iterator past the last element of the range to sort.
  5227. * * A comparison function to use to compare the elements.
  5228. *
  5229. * @tparam Compare Type of comparison function object.
  5230. * @tparam Sort Type of sort function object.
  5231. * @tparam Args Types of arguments to forward to the sort function object.
  5232. * @param length Number of elements to sort.
  5233. * @param compare A valid comparison function object.
  5234. * @param algo A valid sort function object.
  5235. * @param args Arguments to forward to the sort function object, if any.
  5236. */
  5237. template<typename Compare, typename Sort = std_sort, typename... Args>
  5238. void sort_n(const size_type length, Compare compare, Sort algo = Sort{}, Args &&... args) {
  5239. // basic no-leak guarantee (with invalid state) if sorting throws
  5240. ENTT_ASSERT(!(length > count), "Length exceeds the number of elements");
  5241. compact();
  5242. algo(std::make_reverse_iterator(packed + length), std::make_reverse_iterator(packed), std::move(compare), std::forward<Args>(args)...);
  5243. for(size_type pos{}; pos < length; ++pos) {
  5244. auto curr = pos;
  5245. auto next = index(packed[curr]);
  5246. while(curr != next) {
  5247. const auto idx = index(packed[next]);
  5248. const auto entt = packed[curr];
  5249. swap_at(next, idx);
  5250. sparse[page(entt)][offset(entt)] = traits_type::construct(static_cast<typename traits_type::entity_type>(curr));
  5251. curr = std::exchange(next, idx);
  5252. }
  5253. }
  5254. }
  5255. /**
  5256. * @brief Sort all elements according to the given comparison function.
  5257. *
  5258. * @sa sort_n
  5259. *
  5260. * @tparam Compare Type of comparison function object.
  5261. * @tparam Sort Type of sort function object.
  5262. * @tparam Args Types of arguments to forward to the sort function object.
  5263. * @param compare A valid comparison function object.
  5264. * @param algo A valid sort function object.
  5265. * @param args Arguments to forward to the sort function object, if any.
  5266. */
  5267. template<typename Compare, typename Sort = std_sort, typename... Args>
  5268. void sort(Compare compare, Sort algo = Sort{}, Args &&... args) {
  5269. sort_n(count, std::move(compare), std::move(algo), std::forward<Args>(args)...);
  5270. }
  5271. /**
  5272. * @brief Sort entities according to their order in another sparse set.
  5273. *
  5274. * Entities that are part of both the sparse sets are ordered internally
  5275. * according to the order they have in `other`. All the other entities goes
  5276. * to the end of the list and there are no guarantees on their order.<br/>
  5277. * In other terms, this function can be used to impose the same order on two
  5278. * sets by using one of them as a master and the other one as a slave.
  5279. *
  5280. * Iterating the sparse set with a couple of iterators returns elements in
  5281. * the expected order after a call to `respect`. See `begin` and `end` for
  5282. * more details.
  5283. *
  5284. * @param other The sparse sets that imposes the order of the entities.
  5285. */
  5286. void respect(const basic_sparse_set &other) {
  5287. compact();
  5288. const auto to = other.end();
  5289. auto from = other.begin();
  5290. for(size_type pos = count - 1; pos && from != to; ++from) {
  5291. if(contains(*from)) {
  5292. if(*from != packed[pos]) {
  5293. // basic no-leak guarantee (with invalid state) if swapping throws
  5294. swap(packed[pos], *from);
  5295. }
  5296. --pos;
  5297. }
  5298. }
  5299. }
  5300. /**
  5301. * @brief Clears a sparse set.
  5302. * @param ud Optional user data that are forwarded as-is to derived classes.
  5303. */
  5304. void clear(void *ud = nullptr) {
  5305. for(auto &&entity: *this) {
  5306. if(entity != tombstone) {
  5307. in_place_pop(entity, ud);
  5308. }
  5309. }
  5310. free_list = tombstone;
  5311. count = 0u;
  5312. }
  5313. private:
  5314. typename alloc_traits::allocator_type allocator;
  5315. typename bucket_alloc_traits::allocator_type bucket_allocator;
  5316. bucket_alloc_pointer sparse;
  5317. alloc_pointer packed;
  5318. std::size_t bucket;
  5319. std::size_t count;
  5320. std::size_t reserved;
  5321. entity_type free_list;
  5322. deletion_policy mode;
  5323. };
  5324. }
  5325. #endif
  5326. // #include "storage.hpp"
  5327. #ifndef ENTT_ENTITY_STORAGE_HPP
  5328. #define ENTT_ENTITY_STORAGE_HPP
  5329. #include <cstddef>
  5330. #include <iterator>
  5331. #include <memory>
  5332. #include <tuple>
  5333. #include <type_traits>
  5334. #include <utility>
  5335. // #include "../config/config.h"
  5336. // #include "../core/algorithm.hpp"
  5337. // #include "../core/fwd.hpp"
  5338. // #include "../core/type_traits.hpp"
  5339. // #include "../signal/sigh.hpp"
  5340. #ifndef ENTT_SIGNAL_SIGH_HPP
  5341. #define ENTT_SIGNAL_SIGH_HPP
  5342. #include <vector>
  5343. #include <utility>
  5344. #include <iterator>
  5345. #include <algorithm>
  5346. #include <functional>
  5347. #include <type_traits>
  5348. // #include "../config/config.h"
  5349. #ifndef ENTT_CONFIG_CONFIG_H
  5350. #define ENTT_CONFIG_CONFIG_H
  5351. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  5352. # define ENTT_NOEXCEPT noexcept
  5353. # define ENTT_THROW throw
  5354. # define ENTT_TRY try
  5355. # define ENTT_CATCH catch(...)
  5356. #else
  5357. # define ENTT_NOEXCEPT
  5358. # define ENTT_THROW
  5359. # define ENTT_TRY if(true)
  5360. # define ENTT_CATCH if(false)
  5361. #endif
  5362. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  5363. # include <new>
  5364. # define ENTT_LAUNDER(expr) std::launder(expr)
  5365. #else
  5366. # define ENTT_LAUNDER(expr) expr
  5367. #endif
  5368. #ifndef ENTT_USE_ATOMIC
  5369. # define ENTT_MAYBE_ATOMIC(Type) Type
  5370. #else
  5371. # include <atomic>
  5372. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  5373. #endif
  5374. #ifndef ENTT_ID_TYPE
  5375. # include <cstdint>
  5376. # define ENTT_ID_TYPE std::uint32_t
  5377. #endif
  5378. #ifdef ENTT_SPARSE_PAGE
  5379. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  5380. #else
  5381. # define ENTT_SPARSE_PAGE 4096
  5382. #endif
  5383. #ifdef ENTT_PACKED_PAGE
  5384. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  5385. #else
  5386. # define ENTT_PACKED_PAGE 1024
  5387. #endif
  5388. #ifdef ENTT_DISABLE_ASSERT
  5389. # undef ENTT_ASSERT
  5390. # define ENTT_ASSERT(...) (void(0))
  5391. #elif !defined ENTT_ASSERT
  5392. # include <cassert>
  5393. # define ENTT_ASSERT(condition, ...) assert(condition)
  5394. #endif
  5395. #ifdef ENTT_NO_ETO
  5396. # include <type_traits>
  5397. # define ENTT_IGNORE_IF_EMPTY std::false_type
  5398. #else
  5399. # include <type_traits>
  5400. # define ENTT_IGNORE_IF_EMPTY std::true_type
  5401. #endif
  5402. #ifndef ENTT_STANDARD_CPP
  5403. # if defined __clang__ || defined __GNUC__
  5404. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  5405. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  5406. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  5407. # elif defined _MSC_VER
  5408. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  5409. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  5410. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  5411. # endif
  5412. #endif
  5413. #endif
  5414. // #include "delegate.hpp"
  5415. #ifndef ENTT_SIGNAL_DELEGATE_HPP
  5416. #define ENTT_SIGNAL_DELEGATE_HPP
  5417. #include <tuple>
  5418. #include <cstddef>
  5419. #include <utility>
  5420. #include <functional>
  5421. #include <type_traits>
  5422. // #include "../core/type_traits.hpp"
  5423. #ifndef ENTT_CORE_TYPE_TRAITS_HPP
  5424. #define ENTT_CORE_TYPE_TRAITS_HPP
  5425. #include <cstddef>
  5426. #include <iterator>
  5427. #include <type_traits>
  5428. #include <utility>
  5429. // #include "../config/config.h"
  5430. #ifndef ENTT_CONFIG_CONFIG_H
  5431. #define ENTT_CONFIG_CONFIG_H
  5432. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  5433. # define ENTT_NOEXCEPT noexcept
  5434. # define ENTT_THROW throw
  5435. # define ENTT_TRY try
  5436. # define ENTT_CATCH catch(...)
  5437. #else
  5438. # define ENTT_NOEXCEPT
  5439. # define ENTT_THROW
  5440. # define ENTT_TRY if(true)
  5441. # define ENTT_CATCH if(false)
  5442. #endif
  5443. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  5444. # include <new>
  5445. # define ENTT_LAUNDER(expr) std::launder(expr)
  5446. #else
  5447. # define ENTT_LAUNDER(expr) expr
  5448. #endif
  5449. #ifndef ENTT_USE_ATOMIC
  5450. # define ENTT_MAYBE_ATOMIC(Type) Type
  5451. #else
  5452. # include <atomic>
  5453. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  5454. #endif
  5455. #ifndef ENTT_ID_TYPE
  5456. # include <cstdint>
  5457. # define ENTT_ID_TYPE std::uint32_t
  5458. #endif
  5459. #ifdef ENTT_SPARSE_PAGE
  5460. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  5461. #else
  5462. # define ENTT_SPARSE_PAGE 4096
  5463. #endif
  5464. #ifdef ENTT_PACKED_PAGE
  5465. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  5466. #else
  5467. # define ENTT_PACKED_PAGE 1024
  5468. #endif
  5469. #ifdef ENTT_DISABLE_ASSERT
  5470. # undef ENTT_ASSERT
  5471. # define ENTT_ASSERT(...) (void(0))
  5472. #elif !defined ENTT_ASSERT
  5473. # include <cassert>
  5474. # define ENTT_ASSERT(condition, ...) assert(condition)
  5475. #endif
  5476. #ifdef ENTT_NO_ETO
  5477. # include <type_traits>
  5478. # define ENTT_IGNORE_IF_EMPTY std::false_type
  5479. #else
  5480. # include <type_traits>
  5481. # define ENTT_IGNORE_IF_EMPTY std::true_type
  5482. #endif
  5483. #ifndef ENTT_STANDARD_CPP
  5484. # if defined __clang__ || defined __GNUC__
  5485. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  5486. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  5487. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  5488. # elif defined _MSC_VER
  5489. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  5490. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  5491. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  5492. # endif
  5493. #endif
  5494. #endif
  5495. // #include "fwd.hpp"
  5496. #ifndef ENTT_CORE_FWD_HPP
  5497. #define ENTT_CORE_FWD_HPP
  5498. #include <type_traits>
  5499. // #include "../config/config.h"
  5500. namespace entt {
  5501. template<std::size_t Len = sizeof(double[2]), std::size_t = alignof(typename std::aligned_storage_t<Len + !Len>)>
  5502. class basic_any;
  5503. /*! @brief Alias declaration for type identifiers. */
  5504. using id_type = ENTT_ID_TYPE;
  5505. /*! @brief Alias declaration for the most common use case. */
  5506. using any = basic_any<>;
  5507. }
  5508. #endif
  5509. namespace entt {
  5510. /**
  5511. * @brief Utility class to disambiguate overloaded functions.
  5512. * @tparam N Number of choices available.
  5513. */
  5514. template<std::size_t N>
  5515. struct choice_t
  5516. // Unfortunately, doxygen cannot parse such a construct.
  5517. /*! @cond TURN_OFF_DOXYGEN */
  5518. : choice_t<N-1>
  5519. /*! @endcond */
  5520. {};
  5521. /*! @copybrief choice_t */
  5522. template<>
  5523. struct choice_t<0> {};
  5524. /**
  5525. * @brief Variable template for the choice trick.
  5526. * @tparam N Number of choices available.
  5527. */
  5528. template<std::size_t N>
  5529. inline constexpr choice_t<N> choice{};
  5530. /**
  5531. * @brief Identity type trait.
  5532. *
  5533. * Useful to establish non-deduced contexts in template argument deduction
  5534. * (waiting for C++20) or to provide types through function arguments.
  5535. *
  5536. * @tparam Type A type.
  5537. */
  5538. template<typename Type>
  5539. struct type_identity {
  5540. /*! @brief Identity type. */
  5541. using type = Type;
  5542. };
  5543. /**
  5544. * @brief Helper type.
  5545. * @tparam Type A type.
  5546. */
  5547. template<typename Type>
  5548. using type_identity_t = typename type_identity<Type>::type;
  5549. /**
  5550. * @brief A type-only `sizeof` wrapper that returns 0 where `sizeof` complains.
  5551. * @tparam Type The type of which to return the size.
  5552. * @tparam The size of the type if `sizeof` accepts it, 0 otherwise.
  5553. */
  5554. template<typename Type, typename = void>
  5555. struct size_of: std::integral_constant<std::size_t, 0u> {};
  5556. /*! @copydoc size_of */
  5557. template<typename Type>
  5558. struct size_of<Type, std::void_t<decltype(sizeof(Type))>>
  5559. : std::integral_constant<std::size_t, sizeof(Type)>
  5560. {};
  5561. /**
  5562. * @brief Helper variable template.
  5563. * @tparam Type The type of which to return the size.
  5564. */
  5565. template<class Type>
  5566. inline constexpr std::size_t size_of_v = size_of<Type>::value;
  5567. /**
  5568. * @brief Using declaration to be used to _repeat_ the same type a number of
  5569. * times equal to the size of a given parameter pack.
  5570. * @tparam Type A type to repeat.
  5571. */
  5572. template<typename Type, typename>
  5573. using unpack_as_t = Type;
  5574. /**
  5575. * @brief Helper variable template to be used to _repeat_ the same value a
  5576. * number of times equal to the size of a given parameter pack.
  5577. * @tparam Value A value to repeat.
  5578. */
  5579. template<auto Value, typename>
  5580. inline constexpr auto unpack_as_v = Value;
  5581. /**
  5582. * @brief Wraps a static constant.
  5583. * @tparam Value A static constant.
  5584. */
  5585. template<auto Value>
  5586. using integral_constant = std::integral_constant<decltype(Value), Value>;
  5587. /**
  5588. * @brief Alias template to facilitate the creation of named values.
  5589. * @tparam Value A constant value at least convertible to `id_type`.
  5590. */
  5591. template<id_type Value>
  5592. using tag = integral_constant<Value>;
  5593. /**
  5594. * @brief A class to use to push around lists of types, nothing more.
  5595. * @tparam Type Types provided by the type list.
  5596. */
  5597. template<typename... Type>
  5598. struct type_list {
  5599. /*! @brief Type list type. */
  5600. using type = type_list;
  5601. /*! @brief Compile-time number of elements in the type list. */
  5602. static constexpr auto size = sizeof...(Type);
  5603. };
  5604. /*! @brief Primary template isn't defined on purpose. */
  5605. template<std::size_t, typename>
  5606. struct type_list_element;
  5607. /**
  5608. * @brief Provides compile-time indexed access to the types of a type list.
  5609. * @tparam Index Index of the type to return.
  5610. * @tparam Type First type provided by the type list.
  5611. * @tparam Other Other types provided by the type list.
  5612. */
  5613. template<std::size_t Index, typename Type, typename... Other>
  5614. struct type_list_element<Index, type_list<Type, Other...>>
  5615. : type_list_element<Index - 1u, type_list<Other...>>
  5616. {};
  5617. /**
  5618. * @brief Provides compile-time indexed access to the types of a type list.
  5619. * @tparam Type First type provided by the type list.
  5620. * @tparam Other Other types provided by the type list.
  5621. */
  5622. template<typename Type, typename... Other>
  5623. struct type_list_element<0u, type_list<Type, Other...>> {
  5624. /*! @brief Searched type. */
  5625. using type = Type;
  5626. };
  5627. /**
  5628. * @brief Helper type.
  5629. * @tparam Index Index of the type to return.
  5630. * @tparam List Type list to search into.
  5631. */
  5632. template<std::size_t Index, typename List>
  5633. using type_list_element_t = typename type_list_element<Index, List>::type;
  5634. /**
  5635. * @brief Concatenates multiple type lists.
  5636. * @tparam Type Types provided by the first type list.
  5637. * @tparam Other Types provided by the second type list.
  5638. * @return A type list composed by the types of both the type lists.
  5639. */
  5640. template<typename... Type, typename... Other>
  5641. constexpr type_list<Type..., Other...> operator+(type_list<Type...>, type_list<Other...>) { return {}; }
  5642. /*! @brief Primary template isn't defined on purpose. */
  5643. template<typename...>
  5644. struct type_list_cat;
  5645. /*! @brief Concatenates multiple type lists. */
  5646. template<>
  5647. struct type_list_cat<> {
  5648. /*! @brief A type list composed by the types of all the type lists. */
  5649. using type = type_list<>;
  5650. };
  5651. /**
  5652. * @brief Concatenates multiple type lists.
  5653. * @tparam Type Types provided by the first type list.
  5654. * @tparam Other Types provided by the second type list.
  5655. * @tparam List Other type lists, if any.
  5656. */
  5657. template<typename... Type, typename... Other, typename... List>
  5658. struct type_list_cat<type_list<Type...>, type_list<Other...>, List...> {
  5659. /*! @brief A type list composed by the types of all the type lists. */
  5660. using type = typename type_list_cat<type_list<Type..., Other...>, List...>::type;
  5661. };
  5662. /**
  5663. * @brief Concatenates multiple type lists.
  5664. * @tparam Type Types provided by the type list.
  5665. */
  5666. template<typename... Type>
  5667. struct type_list_cat<type_list<Type...>> {
  5668. /*! @brief A type list composed by the types of all the type lists. */
  5669. using type = type_list<Type...>;
  5670. };
  5671. /**
  5672. * @brief Helper type.
  5673. * @tparam List Type lists to concatenate.
  5674. */
  5675. template<typename... List>
  5676. using type_list_cat_t = typename type_list_cat<List...>::type;
  5677. /*! @brief Primary template isn't defined on purpose. */
  5678. template<typename>
  5679. struct type_list_unique;
  5680. /**
  5681. * @brief Removes duplicates types from a type list.
  5682. * @tparam Type One of the types provided by the given type list.
  5683. * @tparam Other The other types provided by the given type list.
  5684. */
  5685. template<typename Type, typename... Other>
  5686. struct type_list_unique<type_list<Type, Other...>> {
  5687. /*! @brief A type list without duplicate types. */
  5688. using type = std::conditional_t<
  5689. std::disjunction_v<std::is_same<Type, Other>...>,
  5690. typename type_list_unique<type_list<Other...>>::type,
  5691. type_list_cat_t<type_list<Type>, typename type_list_unique<type_list<Other...>>::type>
  5692. >;
  5693. };
  5694. /*! @brief Removes duplicates types from a type list. */
  5695. template<>
  5696. struct type_list_unique<type_list<>> {
  5697. /*! @brief A type list without duplicate types. */
  5698. using type = type_list<>;
  5699. };
  5700. /**
  5701. * @brief Helper type.
  5702. * @tparam Type A type list.
  5703. */
  5704. template<typename Type>
  5705. using type_list_unique_t = typename type_list_unique<Type>::type;
  5706. /**
  5707. * @brief Provides the member constant `value` to true if a type list contains a
  5708. * given type, false otherwise.
  5709. * @tparam List Type list.
  5710. * @tparam Type Type to look for.
  5711. */
  5712. template<typename List, typename Type>
  5713. struct type_list_contains;
  5714. /**
  5715. * @copybrief type_list_contains
  5716. * @tparam Type Types provided by the type list.
  5717. * @tparam Other Type to look for.
  5718. */
  5719. template<typename... Type, typename Other>
  5720. struct type_list_contains<type_list<Type...>, Other>: std::disjunction<std::is_same<Type, Other>...> {};
  5721. /**
  5722. * @brief Helper variable template.
  5723. * @tparam List Type list.
  5724. * @tparam Type Type to look for.
  5725. */
  5726. template<class List, typename Type>
  5727. inline constexpr bool type_list_contains_v = type_list_contains<List, Type>::value;
  5728. /*! @brief Primary template isn't defined on purpose. */
  5729. template<typename...>
  5730. struct type_list_diff;
  5731. /**
  5732. * @brief Computes the difference between two type lists.
  5733. * @tparam Type Types provided by the first type list.
  5734. * @tparam Other Types provided by the second type list.
  5735. */
  5736. template<typename... Type, typename... Other>
  5737. struct type_list_diff<type_list<Type...>, type_list<Other...>> {
  5738. /*! @brief A type list that is the difference between the two type lists. */
  5739. using type = type_list_cat_t<std::conditional_t<type_list_contains_v<type_list<Other...>, Type>, type_list<>, type_list<Type>>...>;
  5740. };
  5741. /**
  5742. * @brief Helper type.
  5743. * @tparam List Type lists between which to compute the difference.
  5744. */
  5745. template<typename... List>
  5746. using type_list_diff_t = typename type_list_diff<List...>::type;
  5747. /**
  5748. * @brief A class to use to push around lists of constant values, nothing more.
  5749. * @tparam Value Values provided by the value list.
  5750. */
  5751. template<auto... Value>
  5752. struct value_list {
  5753. /*! @brief Value list type. */
  5754. using type = value_list;
  5755. /*! @brief Compile-time number of elements in the value list. */
  5756. static constexpr auto size = sizeof...(Value);
  5757. };
  5758. /*! @brief Primary template isn't defined on purpose. */
  5759. template<std::size_t, typename>
  5760. struct value_list_element;
  5761. /**
  5762. * @brief Provides compile-time indexed access to the values of a value list.
  5763. * @tparam Index Index of the value to return.
  5764. * @tparam Value First value provided by the value list.
  5765. * @tparam Other Other values provided by the value list.
  5766. */
  5767. template<std::size_t Index, auto Value, auto... Other>
  5768. struct value_list_element<Index, value_list<Value, Other...>>
  5769. : value_list_element<Index - 1u, value_list<Other...>>
  5770. {};
  5771. /**
  5772. * @brief Provides compile-time indexed access to the types of a type list.
  5773. * @tparam Value First value provided by the value list.
  5774. * @tparam Other Other values provided by the value list.
  5775. */
  5776. template<auto Value, auto... Other>
  5777. struct value_list_element<0u, value_list<Value, Other...>> {
  5778. /*! @brief Searched value. */
  5779. static constexpr auto value = Value;
  5780. };
  5781. /**
  5782. * @brief Helper type.
  5783. * @tparam Index Index of the value to return.
  5784. * @tparam List Value list to search into.
  5785. */
  5786. template<std::size_t Index, typename List>
  5787. inline constexpr auto value_list_element_v = value_list_element<Index, List>::value;
  5788. /**
  5789. * @brief Concatenates multiple value lists.
  5790. * @tparam Value Values provided by the first value list.
  5791. * @tparam Other Values provided by the second value list.
  5792. * @return A value list composed by the values of both the value lists.
  5793. */
  5794. template<auto... Value, auto... Other>
  5795. constexpr value_list<Value..., Other...> operator+(value_list<Value...>, value_list<Other...>) { return {}; }
  5796. /*! @brief Primary template isn't defined on purpose. */
  5797. template<typename...>
  5798. struct value_list_cat;
  5799. /*! @brief Concatenates multiple value lists. */
  5800. template<>
  5801. struct value_list_cat<> {
  5802. /*! @brief A value list composed by the values of all the value lists. */
  5803. using type = value_list<>;
  5804. };
  5805. /**
  5806. * @brief Concatenates multiple value lists.
  5807. * @tparam Value Values provided by the first value list.
  5808. * @tparam Other Values provided by the second value list.
  5809. * @tparam List Other value lists, if any.
  5810. */
  5811. template<auto... Value, auto... Other, typename... List>
  5812. struct value_list_cat<value_list<Value...>, value_list<Other...>, List...> {
  5813. /*! @brief A value list composed by the values of all the value lists. */
  5814. using type = typename value_list_cat<value_list<Value..., Other...>, List...>::type;
  5815. };
  5816. /**
  5817. * @brief Concatenates multiple value lists.
  5818. * @tparam Value Values provided by the value list.
  5819. */
  5820. template<auto... Value>
  5821. struct value_list_cat<value_list<Value...>> {
  5822. /*! @brief A value list composed by the values of all the value lists. */
  5823. using type = value_list<Value...>;
  5824. };
  5825. /**
  5826. * @brief Helper type.
  5827. * @tparam List Value lists to concatenate.
  5828. */
  5829. template<typename... List>
  5830. using value_list_cat_t = typename value_list_cat<List...>::type;
  5831. /**
  5832. * @cond TURN_OFF_DOXYGEN
  5833. * Internal details not to be documented.
  5834. */
  5835. namespace internal {
  5836. template<typename>
  5837. [[nodiscard]] constexpr bool is_equality_comparable(...) { return false; }
  5838. template<typename Type>
  5839. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<0>)
  5840. -> decltype(std::declval<Type>() == std::declval<Type>()) { return true; }
  5841. template<typename Type>
  5842. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<1>)
  5843. -> decltype(std::declval<typename Type::value_type>(), std::declval<Type>() == std::declval<Type>()) {
  5844. if constexpr(std::is_same_v<typename Type::value_type, Type>) {
  5845. return is_equality_comparable<Type>(choice<0>);
  5846. } else {
  5847. return is_equality_comparable<typename Type::value_type>(choice<2>);
  5848. }
  5849. }
  5850. template<typename Type>
  5851. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<2>)
  5852. -> decltype(std::declval<typename Type::mapped_type>(), std::declval<Type>() == std::declval<Type>()) {
  5853. return is_equality_comparable<typename Type::key_type>(choice<2>) && is_equality_comparable<typename Type::mapped_type>(choice<2>);
  5854. }
  5855. }
  5856. /**
  5857. * Internal details not to be documented.
  5858. * @endcond
  5859. */
  5860. /**
  5861. * @brief Provides the member constant `value` to true if a given type is
  5862. * equality comparable, false otherwise.
  5863. * @tparam Type The type to test.
  5864. */
  5865. template<typename Type, typename = void>
  5866. struct is_equality_comparable: std::bool_constant<internal::is_equality_comparable<Type>(choice<2>)> {};
  5867. /**
  5868. * @brief Helper variable template.
  5869. * @tparam Type The type to test.
  5870. */
  5871. template<class Type>
  5872. inline constexpr bool is_equality_comparable_v = is_equality_comparable<Type>::value;
  5873. /*! @brief Same as std::is_invocable, but with tuples. */
  5874. template<typename, typename>
  5875. struct is_applicable: std::false_type {};
  5876. /**
  5877. * @copybrief is_applicable
  5878. * @tparam Func A valid function type.
  5879. * @tparam Tuple Tuple-like type.
  5880. * @tparam Args The list of arguments to use to probe the function type.
  5881. */
  5882. template<typename Func, template<typename...> class Tuple, typename... Args>
  5883. struct is_applicable<Func, Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  5884. /**
  5885. * @copybrief is_applicable
  5886. * @tparam Func A valid function type.
  5887. * @tparam Tuple Tuple-like type.
  5888. * @tparam Args The list of arguments to use to probe the function type.
  5889. */
  5890. template<typename Func, template<typename...> class Tuple, typename... Args>
  5891. struct is_applicable<Func, const Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  5892. /**
  5893. * @brief Helper variable template.
  5894. * @tparam Func A valid function type.
  5895. * @tparam Args The list of arguments to use to probe the function type.
  5896. */
  5897. template<typename Func, typename Args>
  5898. inline constexpr bool is_applicable_v = is_applicable<Func, Args>::value;
  5899. /*! @brief Same as std::is_invocable_r, but with tuples for arguments. */
  5900. template<typename, typename, typename>
  5901. struct is_applicable_r: std::false_type {};
  5902. /**
  5903. * @copybrief is_applicable_r
  5904. * @tparam Ret The type to which the return type of the function should be
  5905. * convertible.
  5906. * @tparam Func A valid function type.
  5907. * @tparam Args The list of arguments to use to probe the function type.
  5908. */
  5909. template<typename Ret, typename Func, typename... Args>
  5910. struct is_applicable_r<Ret, Func, std::tuple<Args...>>: std::is_invocable_r<Ret, Func, Args...> {};
  5911. /**
  5912. * @brief Helper variable template.
  5913. * @tparam Ret The type to which the return type of the function should be
  5914. * convertible.
  5915. * @tparam Func A valid function type.
  5916. * @tparam Args The list of arguments to use to probe the function type.
  5917. */
  5918. template<typename Ret, typename Func, typename Args>
  5919. inline constexpr bool is_applicable_r_v = is_applicable_r<Ret, Func, Args>::value;
  5920. /**
  5921. * @brief Provides the member constant `value` to true if a given type is
  5922. * complete, false otherwise.
  5923. * @tparam Type The type to test.
  5924. */
  5925. template<typename Type, typename = void>
  5926. struct is_complete: std::false_type {};
  5927. /*! @copydoc is_complete */
  5928. template<typename Type>
  5929. struct is_complete<Type, std::void_t<decltype(sizeof(Type))>>: std::true_type {};
  5930. /**
  5931. * @brief Helper variable template.
  5932. * @tparam Type The type to test.
  5933. */
  5934. template<typename Type>
  5935. inline constexpr bool is_complete_v = is_complete<Type>::value;
  5936. /**
  5937. * @brief Provides the member constant `value` to true if a given type is an
  5938. * iterator, false otherwise.
  5939. * @tparam Type The type to test.
  5940. */
  5941. template<typename Type, typename = void>
  5942. struct is_iterator: std::false_type {};
  5943. /*! @copydoc is_iterator */
  5944. template<typename Type>
  5945. struct is_iterator<Type, std::void_t<typename std::iterator_traits<Type>::iterator_category>>
  5946. : std::true_type
  5947. {};
  5948. /**
  5949. * @brief Helper variable template.
  5950. * @tparam Type The type to test.
  5951. */
  5952. template<typename Type>
  5953. inline constexpr bool is_iterator_v = is_iterator<Type>::value;
  5954. /**
  5955. * @brief Provides the member constant `value` to true if a given type is of the
  5956. * required iterator type, false otherwise.
  5957. * @tparam Type The type to test.
  5958. * @tparam It Required iterator type.
  5959. */
  5960. template<typename Type, typename It, typename = void>
  5961. struct is_iterator_type: std::false_type {};
  5962. /*! @copydoc is_iterator_type */
  5963. template<typename Type, typename It>
  5964. struct is_iterator_type<Type, It, std::enable_if_t<is_iterator_v<Type> && std::is_same_v<Type, It>>>
  5965. : std::true_type
  5966. {};
  5967. /*! @copydoc is_iterator_type */
  5968. template<typename Type, typename It>
  5969. struct is_iterator_type<Type, It, std::enable_if_t<!std::is_same_v<Type, It>, std::void_t<typename It::iterator_type>>>
  5970. : is_iterator_type<Type, typename It::iterator_type>
  5971. {};
  5972. /**
  5973. * @brief Helper variable template.
  5974. * @tparam Type The type to test.
  5975. * @tparam It Required iterator type.
  5976. */
  5977. template<typename Type, typename It>
  5978. inline constexpr bool is_iterator_type_v = is_iterator_type<Type, It>::value;
  5979. /**
  5980. * @brief Transcribes the constness of a type to another type.
  5981. * @tparam To The type to which to transcribe the constness.
  5982. * @tparam From The type from which to transcribe the constness.
  5983. */
  5984. template<typename To, typename From>
  5985. struct constness_as {
  5986. /*! @brief The type resulting from the transcription of the constness. */
  5987. using type = std::remove_const_t<To>;
  5988. };
  5989. /*! @copydoc constness_as */
  5990. template<typename To, typename From>
  5991. struct constness_as<To, const From> {
  5992. /*! @brief The type resulting from the transcription of the constness. */
  5993. using type = std::add_const_t<To>;
  5994. };
  5995. /**
  5996. * @brief Alias template to facilitate the transcription of the constness.
  5997. * @tparam To The type to which to transcribe the constness.
  5998. * @tparam From The type from which to transcribe the constness.
  5999. */
  6000. template<typename To, typename From>
  6001. using constness_as_t = typename constness_as<To, From>::type;
  6002. /**
  6003. * @brief Extracts the class of a non-static member object or function.
  6004. * @tparam Member A pointer to a non-static member object or function.
  6005. */
  6006. template<typename Member>
  6007. class member_class {
  6008. static_assert(std::is_member_pointer_v<Member>, "Invalid pointer type to non-static member object or function");
  6009. template<typename Class, typename Ret, typename... Args>
  6010. static Class * clazz(Ret(Class:: *)(Args...));
  6011. template<typename Class, typename Ret, typename... Args>
  6012. static Class * clazz(Ret(Class:: *)(Args...) const);
  6013. template<typename Class, typename Type>
  6014. static Class * clazz(Type Class:: *);
  6015. public:
  6016. /*! @brief The class of the given non-static member object or function. */
  6017. using type = std::remove_pointer_t<decltype(clazz(std::declval<Member>()))>;
  6018. };
  6019. /**
  6020. * @brief Helper type.
  6021. * @tparam Member A pointer to a non-static member object or function.
  6022. */
  6023. template<typename Member>
  6024. using member_class_t = typename member_class<Member>::type;
  6025. }
  6026. #endif
  6027. // #include "../config/config.h"
  6028. namespace entt {
  6029. /**
  6030. * @cond TURN_OFF_DOXYGEN
  6031. * Internal details not to be documented.
  6032. */
  6033. namespace internal {
  6034. template<typename Ret, typename... Args>
  6035. auto function_pointer(Ret(*)(Args...)) -> Ret(*)(Args...);
  6036. template<typename Ret, typename Type, typename... Args, typename Other>
  6037. auto function_pointer(Ret(*)(Type, Args...), Other &&) -> Ret(*)(Args...);
  6038. template<typename Class, typename Ret, typename... Args, typename... Other>
  6039. auto function_pointer(Ret(Class:: *)(Args...), Other &&...) -> Ret(*)(Args...);
  6040. template<typename Class, typename Ret, typename... Args, typename... Other>
  6041. auto function_pointer(Ret(Class:: *)(Args...) const, Other &&...) -> Ret(*)(Args...);
  6042. template<typename Class, typename Type, typename... Other>
  6043. auto function_pointer(Type Class:: *, Other &&...) -> Type(*)();
  6044. template<typename... Type>
  6045. using function_pointer_t = decltype(internal::function_pointer(std::declval<Type>()...));
  6046. template<typename... Class, typename Ret, typename... Args>
  6047. [[nodiscard]] constexpr auto index_sequence_for(Ret(*)(Args...)) {
  6048. return std::index_sequence_for<Class..., Args...>{};
  6049. }
  6050. }
  6051. /**
  6052. * Internal details not to be documented.
  6053. * @endcond
  6054. */
  6055. /*! @brief Used to wrap a function or a member of a specified type. */
  6056. template<auto>
  6057. struct connect_arg_t {};
  6058. /*! @brief Constant of type connect_arg_t used to disambiguate calls. */
  6059. template<auto Func>
  6060. inline constexpr connect_arg_t<Func> connect_arg{};
  6061. /**
  6062. * @brief Basic delegate implementation.
  6063. *
  6064. * Primary template isn't defined on purpose. All the specializations give a
  6065. * compile-time error unless the template parameter is a function type.
  6066. */
  6067. template<typename>
  6068. class delegate;
  6069. /**
  6070. * @brief Utility class to use to send around functions and members.
  6071. *
  6072. * Unmanaged delegate for function pointers and members. Users of this class are
  6073. * in charge of disconnecting instances before deleting them.
  6074. *
  6075. * A delegate can be used as a general purpose invoker without memory overhead
  6076. * for free functions possibly with payloads and bound or unbound members.
  6077. *
  6078. * @tparam Ret Return type of a function type.
  6079. * @tparam Args Types of arguments of a function type.
  6080. */
  6081. template<typename Ret, typename... Args>
  6082. class delegate<Ret(Args...)> {
  6083. template<auto Candidate, std::size_t... Index>
  6084. [[nodiscard]] auto wrap(std::index_sequence<Index...>) ENTT_NOEXCEPT {
  6085. return [](const void *, Args... args) -> Ret {
  6086. [[maybe_unused]] const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
  6087. return static_cast<Ret>(std::invoke(Candidate, std::forward<type_list_element_t<Index, type_list<Args...>>>(std::get<Index>(arguments))...));
  6088. };
  6089. }
  6090. template<auto Candidate, typename Type, std::size_t... Index>
  6091. [[nodiscard]] auto wrap(Type &, std::index_sequence<Index...>) ENTT_NOEXCEPT {
  6092. return [](const void *payload, Args... args) -> Ret {
  6093. [[maybe_unused]] const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
  6094. Type *curr = static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(payload));
  6095. return static_cast<Ret>(std::invoke(Candidate, *curr, std::forward<type_list_element_t<Index, type_list<Args...>>>(std::get<Index>(arguments))...));
  6096. };
  6097. }
  6098. template<auto Candidate, typename Type, std::size_t... Index>
  6099. [[nodiscard]] auto wrap(Type *, std::index_sequence<Index...>) ENTT_NOEXCEPT {
  6100. return [](const void *payload, Args... args) -> Ret {
  6101. [[maybe_unused]] const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
  6102. Type *curr = static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(payload));
  6103. return static_cast<Ret>(std::invoke(Candidate, curr, std::forward<type_list_element_t<Index, type_list<Args...>>>(std::get<Index>(arguments))...));
  6104. };
  6105. }
  6106. public:
  6107. /*! @brief Function type of the contained target. */
  6108. using function_type = Ret(const void *, Args...);
  6109. /*! @brief Function type of the delegate. */
  6110. using type = Ret(Args...);
  6111. /*! @brief Return type of the delegate. */
  6112. using result_type = Ret;
  6113. /*! @brief Default constructor. */
  6114. delegate() ENTT_NOEXCEPT
  6115. : fn{nullptr}, data{nullptr}
  6116. {}
  6117. /**
  6118. * @brief Constructs a delegate and connects a free function or an unbound
  6119. * member.
  6120. * @tparam Candidate Function or member to connect to the delegate.
  6121. */
  6122. template<auto Candidate>
  6123. delegate(connect_arg_t<Candidate>) ENTT_NOEXCEPT {
  6124. connect<Candidate>();
  6125. }
  6126. /**
  6127. * @brief Constructs a delegate and connects a free function with payload or
  6128. * a bound member.
  6129. * @tparam Candidate Function or member to connect to the delegate.
  6130. * @tparam Type Type of class or type of payload.
  6131. * @param value_or_instance A valid object that fits the purpose.
  6132. */
  6133. template<auto Candidate, typename Type>
  6134. delegate(connect_arg_t<Candidate>, Type &&value_or_instance) ENTT_NOEXCEPT {
  6135. connect<Candidate>(std::forward<Type>(value_or_instance));
  6136. }
  6137. /**
  6138. * @brief Constructs a delegate and connects an user defined function with
  6139. * optional payload.
  6140. * @param function Function to connect to the delegate.
  6141. * @param payload User defined arbitrary data.
  6142. */
  6143. delegate(function_type *function, const void *payload = nullptr) ENTT_NOEXCEPT {
  6144. connect(function, payload);
  6145. }
  6146. /**
  6147. * @brief Connects a free function or an unbound member to a delegate.
  6148. * @tparam Candidate Function or member to connect to the delegate.
  6149. */
  6150. template<auto Candidate>
  6151. void connect() ENTT_NOEXCEPT {
  6152. data = nullptr;
  6153. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Args...>) {
  6154. fn = [](const void *, Args... args) -> Ret {
  6155. return Ret(std::invoke(Candidate, std::forward<Args>(args)...));
  6156. };
  6157. } else if constexpr(std::is_member_pointer_v<decltype(Candidate)>) {
  6158. fn = wrap<Candidate>(internal::index_sequence_for<type_list_element_t<0, type_list<Args...>>>(internal::function_pointer_t<decltype(Candidate)>{}));
  6159. } else {
  6160. fn = wrap<Candidate>(internal::index_sequence_for(internal::function_pointer_t<decltype(Candidate)>{}));
  6161. }
  6162. }
  6163. /**
  6164. * @brief Connects a free function with payload or a bound member to a
  6165. * delegate.
  6166. *
  6167. * The delegate isn't responsible for the connected object or the payload.
  6168. * Users must always guarantee that the lifetime of the instance overcomes
  6169. * the one of the delegate.<br/>
  6170. * When used to connect a free function with payload, its signature must be
  6171. * such that the instance is the first argument before the ones used to
  6172. * define the delegate itself.
  6173. *
  6174. * @tparam Candidate Function or member to connect to the delegate.
  6175. * @tparam Type Type of class or type of payload.
  6176. * @param value_or_instance A valid reference that fits the purpose.
  6177. */
  6178. template<auto Candidate, typename Type>
  6179. void connect(Type &value_or_instance) ENTT_NOEXCEPT {
  6180. data = &value_or_instance;
  6181. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Type &, Args...>) {
  6182. fn = [](const void *payload, Args... args) -> Ret {
  6183. Type *curr = static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(payload));
  6184. return Ret(std::invoke(Candidate, *curr, std::forward<Args>(args)...));
  6185. };
  6186. } else {
  6187. fn = wrap<Candidate>(value_or_instance, internal::index_sequence_for(internal::function_pointer_t<decltype(Candidate), Type>{}));
  6188. }
  6189. }
  6190. /**
  6191. * @brief Connects a free function with payload or a bound member to a
  6192. * delegate.
  6193. *
  6194. * @sa connect(Type &)
  6195. *
  6196. * @tparam Candidate Function or member to connect to the delegate.
  6197. * @tparam Type Type of class or type of payload.
  6198. * @param value_or_instance A valid pointer that fits the purpose.
  6199. */
  6200. template<auto Candidate, typename Type>
  6201. void connect(Type *value_or_instance) ENTT_NOEXCEPT {
  6202. data = value_or_instance;
  6203. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Type *, Args...>) {
  6204. fn = [](const void *payload, Args... args) -> Ret {
  6205. Type *curr = static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(payload));
  6206. return Ret(std::invoke(Candidate, curr, std::forward<Args>(args)...));
  6207. };
  6208. } else {
  6209. fn = wrap<Candidate>(value_or_instance, internal::index_sequence_for(internal::function_pointer_t<decltype(Candidate), Type>{}));
  6210. }
  6211. }
  6212. /**
  6213. * @brief Connects an user defined function with optional payload to a
  6214. * delegate.
  6215. *
  6216. * The delegate isn't responsible for the connected object or the payload.
  6217. * Users must always guarantee that the lifetime of an instance overcomes
  6218. * the one of the delegate.<br/>
  6219. * The payload is returned as the first argument to the target function in
  6220. * all cases.
  6221. *
  6222. * @param function Function to connect to the delegate.
  6223. * @param payload User defined arbitrary data.
  6224. */
  6225. void connect(function_type *function, const void *payload = nullptr) ENTT_NOEXCEPT {
  6226. fn = function;
  6227. data = payload;
  6228. }
  6229. /**
  6230. * @brief Resets a delegate.
  6231. *
  6232. * After a reset, a delegate cannot be invoked anymore.
  6233. */
  6234. void reset() ENTT_NOEXCEPT {
  6235. fn = nullptr;
  6236. data = nullptr;
  6237. }
  6238. /**
  6239. * @brief Returns the instance or the payload linked to a delegate, if any.
  6240. * @return An opaque pointer to the underlying data.
  6241. */
  6242. [[nodiscard]] const void * instance() const ENTT_NOEXCEPT {
  6243. return data;
  6244. }
  6245. /**
  6246. * @brief Triggers a delegate.
  6247. *
  6248. * The delegate invokes the underlying function and returns the result.
  6249. *
  6250. * @warning
  6251. * Attempting to trigger an invalid delegate results in undefined
  6252. * behavior.
  6253. *
  6254. * @param args Arguments to use to invoke the underlying function.
  6255. * @return The value returned by the underlying function.
  6256. */
  6257. Ret operator()(Args... args) const {
  6258. ENTT_ASSERT(static_cast<bool>(*this), "Uninitialized delegate");
  6259. return fn(data, std::forward<Args>(args)...);
  6260. }
  6261. /**
  6262. * @brief Checks whether a delegate actually stores a listener.
  6263. * @return False if the delegate is empty, true otherwise.
  6264. */
  6265. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  6266. // no need to test also data
  6267. return !(fn == nullptr);
  6268. }
  6269. /**
  6270. * @brief Compares the contents of two delegates.
  6271. * @param other Delegate with which to compare.
  6272. * @return False if the two contents differ, true otherwise.
  6273. */
  6274. [[nodiscard]] bool operator==(const delegate<Ret(Args...)> &other) const ENTT_NOEXCEPT {
  6275. return fn == other.fn && data == other.data;
  6276. }
  6277. private:
  6278. function_type *fn;
  6279. const void *data;
  6280. };
  6281. /**
  6282. * @brief Compares the contents of two delegates.
  6283. * @tparam Ret Return type of a function type.
  6284. * @tparam Args Types of arguments of a function type.
  6285. * @param lhs A valid delegate object.
  6286. * @param rhs A valid delegate object.
  6287. * @return True if the two contents differ, false otherwise.
  6288. */
  6289. template<typename Ret, typename... Args>
  6290. [[nodiscard]] bool operator!=(const delegate<Ret(Args...)> &lhs, const delegate<Ret(Args...)> &rhs) ENTT_NOEXCEPT {
  6291. return !(lhs == rhs);
  6292. }
  6293. /**
  6294. * @brief Deduction guide.
  6295. * @tparam Candidate Function or member to connect to the delegate.
  6296. */
  6297. template<auto Candidate>
  6298. delegate(connect_arg_t<Candidate>)
  6299. -> delegate<std::remove_pointer_t<internal::function_pointer_t<decltype(Candidate)>>>;
  6300. /**
  6301. * @brief Deduction guide.
  6302. * @tparam Candidate Function or member to connect to the delegate.
  6303. * @tparam Type Type of class or type of payload.
  6304. */
  6305. template<auto Candidate, typename Type>
  6306. delegate(connect_arg_t<Candidate>, Type &&)
  6307. -> delegate<std::remove_pointer_t<internal::function_pointer_t<decltype(Candidate), Type>>>;
  6308. /**
  6309. * @brief Deduction guide.
  6310. * @tparam Ret Return type of a function type.
  6311. * @tparam Args Types of arguments of a function type.
  6312. */
  6313. template<typename Ret, typename... Args>
  6314. delegate(Ret(*)(const void *, Args...), const void * = nullptr)
  6315. -> delegate<Ret(Args...)>;
  6316. }
  6317. #endif
  6318. // #include "fwd.hpp"
  6319. #ifndef ENTT_SIGNAL_FWD_HPP
  6320. #define ENTT_SIGNAL_FWD_HPP
  6321. namespace entt {
  6322. template<typename>
  6323. class delegate;
  6324. class dispatcher;
  6325. template<typename>
  6326. class emitter;
  6327. class connection;
  6328. struct scoped_connection;
  6329. template<typename>
  6330. class sink;
  6331. template<typename>
  6332. class sigh;
  6333. }
  6334. #endif
  6335. namespace entt {
  6336. /**
  6337. * @brief Sink class.
  6338. *
  6339. * Primary template isn't defined on purpose. All the specializations give a
  6340. * compile-time error unless the template parameter is a function type.
  6341. *
  6342. * @tparam Function A valid function type.
  6343. */
  6344. template<typename Function>
  6345. class sink;
  6346. /**
  6347. * @brief Unmanaged signal handler.
  6348. *
  6349. * Primary template isn't defined on purpose. All the specializations give a
  6350. * compile-time error unless the template parameter is a function type.
  6351. *
  6352. * @tparam Function A valid function type.
  6353. */
  6354. template<typename Function>
  6355. class sigh;
  6356. /**
  6357. * @brief Unmanaged signal handler.
  6358. *
  6359. * It works directly with references to classes and pointers to member functions
  6360. * as well as pointers to free functions. Users of this class are in charge of
  6361. * disconnecting instances before deleting them.
  6362. *
  6363. * This class serves mainly two purposes:
  6364. *
  6365. * * Creating signals to use later to notify a bunch of listeners.
  6366. * * Collecting results from a set of functions like in a voting system.
  6367. *
  6368. * @tparam Ret Return type of a function type.
  6369. * @tparam Args Types of arguments of a function type.
  6370. */
  6371. template<typename Ret, typename... Args>
  6372. class sigh<Ret(Args...)> {
  6373. /*! @brief A sink is allowed to modify a signal. */
  6374. friend class sink<Ret(Args...)>;
  6375. public:
  6376. /*! @brief Unsigned integer type. */
  6377. using size_type = std::size_t;
  6378. /*! @brief Sink type. */
  6379. using sink_type = sink<Ret(Args...)>;
  6380. /**
  6381. * @brief Instance type when it comes to connecting member functions.
  6382. * @tparam Class Type of class to which the member function belongs.
  6383. */
  6384. template<typename Class>
  6385. using instance_type = Class *;
  6386. /**
  6387. * @brief Number of listeners connected to the signal.
  6388. * @return Number of listeners currently connected.
  6389. */
  6390. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  6391. return calls.size();
  6392. }
  6393. /**
  6394. * @brief Returns false if at least a listener is connected to the signal.
  6395. * @return True if the signal has no listeners connected, false otherwise.
  6396. */
  6397. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  6398. return calls.empty();
  6399. }
  6400. /**
  6401. * @brief Triggers a signal.
  6402. *
  6403. * All the listeners are notified. Order isn't guaranteed.
  6404. *
  6405. * @param args Arguments to use to invoke listeners.
  6406. */
  6407. void publish(Args... args) const {
  6408. for(auto &&call: std::as_const(calls)) {
  6409. call(args...);
  6410. }
  6411. }
  6412. /**
  6413. * @brief Collects return values from the listeners.
  6414. *
  6415. * The collector must expose a call operator with the following properties:
  6416. *
  6417. * * The return type is either `void` or such that it's convertible to
  6418. * `bool`. In the second case, a true value will stop the iteration.
  6419. * * The list of parameters is empty if `Ret` is `void`, otherwise it
  6420. * contains a single element such that `Ret` is convertible to it.
  6421. *
  6422. * @tparam Func Type of collector to use, if any.
  6423. * @param func A valid function object.
  6424. * @param args Arguments to use to invoke listeners.
  6425. */
  6426. template<typename Func>
  6427. void collect(Func func, Args... args) const {
  6428. for(auto &&call: calls) {
  6429. if constexpr(std::is_void_v<Ret>) {
  6430. if constexpr(std::is_invocable_r_v<bool, Func>) {
  6431. call(args...);
  6432. if(func()) { break; }
  6433. } else {
  6434. call(args...);
  6435. func();
  6436. }
  6437. } else {
  6438. if constexpr(std::is_invocable_r_v<bool, Func, Ret>) {
  6439. if(func(call(args...))) { break; }
  6440. } else {
  6441. func(call(args...));
  6442. }
  6443. }
  6444. }
  6445. }
  6446. private:
  6447. std::vector<delegate<Ret(Args...)>> calls;
  6448. };
  6449. /**
  6450. * @brief Connection class.
  6451. *
  6452. * Opaque object the aim of which is to allow users to release an already
  6453. * estabilished connection without having to keep a reference to the signal or
  6454. * the sink that generated it.
  6455. */
  6456. class connection {
  6457. /*! @brief A sink is allowed to create connection objects. */
  6458. template<typename>
  6459. friend class sink;
  6460. connection(delegate<void(void *)> fn, void *ref)
  6461. : disconnect{fn}, signal{ref}
  6462. {}
  6463. public:
  6464. /*! @brief Default constructor. */
  6465. connection() = default;
  6466. /**
  6467. * @brief Checks whether a connection is properly initialized.
  6468. * @return True if the connection is properly initialized, false otherwise.
  6469. */
  6470. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  6471. return static_cast<bool>(disconnect);
  6472. }
  6473. /*! @brief Breaks the connection. */
  6474. void release() {
  6475. if(disconnect) {
  6476. disconnect(signal);
  6477. disconnect.reset();
  6478. }
  6479. }
  6480. private:
  6481. delegate<void(void *)> disconnect;
  6482. void *signal{};
  6483. };
  6484. /**
  6485. * @brief Scoped connection class.
  6486. *
  6487. * Opaque object the aim of which is to allow users to release an already
  6488. * estabilished connection without having to keep a reference to the signal or
  6489. * the sink that generated it.<br/>
  6490. * A scoped connection automatically breaks the link between the two objects
  6491. * when it goes out of scope.
  6492. */
  6493. struct scoped_connection {
  6494. /*! @brief Default constructor. */
  6495. scoped_connection() = default;
  6496. /**
  6497. * @brief Constructs a scoped connection from a basic connection.
  6498. * @param other A valid connection object.
  6499. */
  6500. scoped_connection(const connection &other)
  6501. : conn{other}
  6502. {}
  6503. /*! @brief Default copy constructor, deleted on purpose. */
  6504. scoped_connection(const scoped_connection &) = delete;
  6505. /*! @brief Automatically breaks the link on destruction. */
  6506. ~scoped_connection() {
  6507. conn.release();
  6508. }
  6509. /**
  6510. * @brief Default copy assignment operator, deleted on purpose.
  6511. * @return This scoped connection.
  6512. */
  6513. scoped_connection & operator=(const scoped_connection &) = delete;
  6514. /**
  6515. * @brief Acquires a connection.
  6516. * @param other The connection object to acquire.
  6517. * @return This scoped connection.
  6518. */
  6519. scoped_connection & operator=(connection other) {
  6520. conn = std::move(other);
  6521. return *this;
  6522. }
  6523. /**
  6524. * @brief Checks whether a scoped connection is properly initialized.
  6525. * @return True if the connection is properly initialized, false otherwise.
  6526. */
  6527. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  6528. return static_cast<bool>(conn);
  6529. }
  6530. /*! @brief Breaks the connection. */
  6531. void release() {
  6532. conn.release();
  6533. }
  6534. private:
  6535. connection conn;
  6536. };
  6537. /**
  6538. * @brief Sink class.
  6539. *
  6540. * A sink is used to connect listeners to signals and to disconnect them.<br/>
  6541. * The function type for a listener is the one of the signal to which it
  6542. * belongs.
  6543. *
  6544. * The clear separation between a signal and a sink permits to store the former
  6545. * as private data member without exposing the publish functionality to the
  6546. * users of the class.
  6547. *
  6548. * @warning
  6549. * Lifetime of a sink must not overcome that of the signal to which it refers.
  6550. * In any other case, attempting to use a sink results in undefined behavior.
  6551. *
  6552. * @tparam Ret Return type of a function type.
  6553. * @tparam Args Types of arguments of a function type.
  6554. */
  6555. template<typename Ret, typename... Args>
  6556. class sink<Ret(Args...)> {
  6557. using signal_type = sigh<Ret(Args...)>;
  6558. using difference_type = typename std::iterator_traits<typename decltype(signal_type::calls)::iterator>::difference_type;
  6559. template<auto Candidate, typename Type>
  6560. static void release(Type value_or_instance, void *signal) {
  6561. sink{*static_cast<signal_type *>(signal)}.disconnect<Candidate>(value_or_instance);
  6562. }
  6563. template<auto Candidate>
  6564. static void release(void *signal) {
  6565. sink{*static_cast<signal_type *>(signal)}.disconnect<Candidate>();
  6566. }
  6567. public:
  6568. /**
  6569. * @brief Constructs a sink that is allowed to modify a given signal.
  6570. * @param ref A valid reference to a signal object.
  6571. */
  6572. sink(sigh<Ret(Args...)> &ref) ENTT_NOEXCEPT
  6573. : offset{},
  6574. signal{&ref}
  6575. {}
  6576. /**
  6577. * @brief Returns false if at least a listener is connected to the sink.
  6578. * @return True if the sink has no listeners connected, false otherwise.
  6579. */
  6580. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  6581. return signal->calls.empty();
  6582. }
  6583. /**
  6584. * @brief Returns a sink that connects before a given free function or an
  6585. * unbound member.
  6586. * @tparam Function A valid free function pointer.
  6587. * @return A properly initialized sink object.
  6588. */
  6589. template<auto Function>
  6590. [[nodiscard]] sink before() {
  6591. delegate<Ret(Args...)> call{};
  6592. call.template connect<Function>();
  6593. const auto &calls = signal->calls;
  6594. const auto it = std::find(calls.cbegin(), calls.cend(), std::move(call));
  6595. sink other{*this};
  6596. other.offset = std::distance(it, calls.cend());
  6597. return other;
  6598. }
  6599. /**
  6600. * @brief Returns a sink that connects before a free function with payload
  6601. * or a bound member.
  6602. * @tparam Candidate Member or free function to look for.
  6603. * @tparam Type Type of class or type of payload.
  6604. * @param value_or_instance A valid object that fits the purpose.
  6605. * @return A properly initialized sink object.
  6606. */
  6607. template<auto Candidate, typename Type>
  6608. [[nodiscard]] sink before(Type &&value_or_instance) {
  6609. delegate<Ret(Args...)> call{};
  6610. call.template connect<Candidate>(value_or_instance);
  6611. const auto &calls = signal->calls;
  6612. const auto it = std::find(calls.cbegin(), calls.cend(), std::move(call));
  6613. sink other{*this};
  6614. other.offset = std::distance(it, calls.cend());
  6615. return other;
  6616. }
  6617. /**
  6618. * @brief Returns a sink that connects before a given instance or specific
  6619. * payload.
  6620. * @tparam Type Type of class or type of payload.
  6621. * @param value_or_instance A valid object that fits the purpose.
  6622. * @return A properly initialized sink object.
  6623. */
  6624. template<typename Type>
  6625. [[nodiscard]] sink before(Type &value_or_instance) {
  6626. return before(&value_or_instance);
  6627. }
  6628. /**
  6629. * @brief Returns a sink that connects before a given instance or specific
  6630. * payload.
  6631. * @tparam Type Type of class or type of payload.
  6632. * @param value_or_instance A valid pointer that fits the purpose.
  6633. * @return A properly initialized sink object.
  6634. */
  6635. template<typename Type>
  6636. [[nodiscard]] sink before(Type *value_or_instance) {
  6637. sink other{*this};
  6638. if(value_or_instance) {
  6639. const auto &calls = signal->calls;
  6640. const auto it = std::find_if(calls.cbegin(), calls.cend(), [value_or_instance](const auto &delegate) {
  6641. return delegate.instance() == value_or_instance;
  6642. });
  6643. other.offset = std::distance(it, calls.cend());
  6644. }
  6645. return other;
  6646. }
  6647. /**
  6648. * @brief Returns a sink that connects before anything else.
  6649. * @return A properly initialized sink object.
  6650. */
  6651. [[nodiscard]] sink before() {
  6652. sink other{*this};
  6653. other.offset = signal->calls.size();
  6654. return other;
  6655. }
  6656. /**
  6657. * @brief Connects a free function or an unbound member to a signal.
  6658. *
  6659. * The signal handler performs checks to avoid multiple connections for the
  6660. * same function.
  6661. *
  6662. * @tparam Candidate Function or member to connect to the signal.
  6663. * @return A properly initialized connection object.
  6664. */
  6665. template<auto Candidate>
  6666. connection connect() {
  6667. disconnect<Candidate>();
  6668. delegate<Ret(Args...)> call{};
  6669. call.template connect<Candidate>();
  6670. signal->calls.insert(signal->calls.end() - offset, std::move(call));
  6671. delegate<void(void *)> conn{};
  6672. conn.template connect<&release<Candidate>>();
  6673. return { std::move(conn), signal };
  6674. }
  6675. /**
  6676. * @brief Connects a free function with payload or a bound member to a
  6677. * signal.
  6678. *
  6679. * The signal isn't responsible for the connected object or the payload.
  6680. * Users must always guarantee that the lifetime of the instance overcomes
  6681. * the one of the signal. On the other side, the signal handler performs
  6682. * checks to avoid multiple connections for the same function.<br/>
  6683. * When used to connect a free function with payload, its signature must be
  6684. * such that the instance is the first argument before the ones used to
  6685. * define the signal itself.
  6686. *
  6687. * @tparam Candidate Function or member to connect to the signal.
  6688. * @tparam Type Type of class or type of payload.
  6689. * @param value_or_instance A valid object that fits the purpose.
  6690. * @return A properly initialized connection object.
  6691. */
  6692. template<auto Candidate, typename Type>
  6693. connection connect(Type &&value_or_instance) {
  6694. disconnect<Candidate>(value_or_instance);
  6695. delegate<Ret(Args...)> call{};
  6696. call.template connect<Candidate>(value_or_instance);
  6697. signal->calls.insert(signal->calls.end() - offset, std::move(call));
  6698. delegate<void(void *)> conn{};
  6699. conn.template connect<&release<Candidate, Type>>(value_or_instance);
  6700. return { std::move(conn), signal };
  6701. }
  6702. /**
  6703. * @brief Disconnects a free function or an unbound member from a signal.
  6704. * @tparam Candidate Function or member to disconnect from the signal.
  6705. */
  6706. template<auto Candidate>
  6707. void disconnect() {
  6708. auto &calls = signal->calls;
  6709. delegate<Ret(Args...)> call{};
  6710. call.template connect<Candidate>();
  6711. calls.erase(std::remove(calls.begin(), calls.end(), std::move(call)), calls.end());
  6712. }
  6713. /**
  6714. * @brief Disconnects a free function with payload or a bound member from a
  6715. * signal.
  6716. * @tparam Candidate Function or member to disconnect from the signal.
  6717. * @tparam Type Type of class or type of payload.
  6718. * @param value_or_instance A valid object that fits the purpose.
  6719. */
  6720. template<auto Candidate, typename Type>
  6721. void disconnect(Type &&value_or_instance) {
  6722. auto &calls = signal->calls;
  6723. delegate<Ret(Args...)> call{};
  6724. call.template connect<Candidate>(value_or_instance);
  6725. calls.erase(std::remove(calls.begin(), calls.end(), std::move(call)), calls.end());
  6726. }
  6727. /**
  6728. * @brief Disconnects free functions with payload or bound members from a
  6729. * signal.
  6730. * @tparam Type Type of class or type of payload.
  6731. * @param value_or_instance A valid object that fits the purpose.
  6732. */
  6733. template<typename Type>
  6734. void disconnect(Type &value_or_instance) {
  6735. disconnect(&value_or_instance);
  6736. }
  6737. /**
  6738. * @brief Disconnects free functions with payload or bound members from a
  6739. * signal.
  6740. * @tparam Type Type of class or type of payload.
  6741. * @param value_or_instance A valid object that fits the purpose.
  6742. */
  6743. template<typename Type>
  6744. void disconnect(Type *value_or_instance) {
  6745. if(value_or_instance) {
  6746. auto &calls = signal->calls;
  6747. calls.erase(std::remove_if(calls.begin(), calls.end(), [value_or_instance](const auto &delegate) {
  6748. return delegate.instance() == value_or_instance;
  6749. }), calls.end());
  6750. }
  6751. }
  6752. /*! @brief Disconnects all the listeners from a signal. */
  6753. void disconnect() {
  6754. signal->calls.clear();
  6755. }
  6756. private:
  6757. difference_type offset;
  6758. signal_type *signal;
  6759. };
  6760. /**
  6761. * @brief Deduction guide.
  6762. *
  6763. * It allows to deduce the function type of a sink directly from the signal it
  6764. * refers to.
  6765. *
  6766. * @tparam Ret Return type of a function type.
  6767. * @tparam Args Types of arguments of a function type.
  6768. */
  6769. template<typename Ret, typename... Args>
  6770. sink(sigh<Ret(Args...)> &)
  6771. -> sink<Ret(Args...)>;
  6772. }
  6773. #endif
  6774. // #include "component.hpp"
  6775. #ifndef ENTT_ENTITY_COMPONENT_HPP
  6776. #define ENTT_ENTITY_COMPONENT_HPP
  6777. #include <type_traits>
  6778. // #include "../config/config.h"
  6779. namespace entt {
  6780. /*! @brief Commonly used default traits for all types. */
  6781. struct basic_component_traits {
  6782. /*! @brief Pointer stability, default is `std::false_type`. */
  6783. using in_place_delete = std::false_type;
  6784. /*! @brief Empty type optimization, default is `ENTT_IGNORE_IF_EMPTY`. */
  6785. using ignore_if_empty = ENTT_IGNORE_IF_EMPTY;
  6786. };
  6787. /**
  6788. * @brief Common way to access various properties of components.
  6789. * @tparam Type Type of component.
  6790. */
  6791. template<typename Type, typename = void>
  6792. struct component_traits: basic_component_traits {
  6793. static_assert(std::is_same_v<std::decay_t<Type>, Type>, "Unsupported type");
  6794. };
  6795. }
  6796. #endif
  6797. // #include "entity.hpp"
  6798. // #include "fwd.hpp"
  6799. // #include "sparse_set.hpp"
  6800. namespace entt {
  6801. /**
  6802. * @brief Basic storage implementation.
  6803. *
  6804. * This class is a refinement of a sparse set that associates an object to an
  6805. * entity. The main purpose of this class is to extend sparse sets to store
  6806. * components in a registry. It guarantees fast access both to the elements and
  6807. * to the entities.
  6808. *
  6809. * @note
  6810. * Entities and objects have the same order.
  6811. *
  6812. * @note
  6813. * Internal data structures arrange elements to maximize performance. There are
  6814. * no guarantees that objects are returned in the insertion order when iterate
  6815. * a storage. Do not make assumption on the order in any case.
  6816. *
  6817. * @warning
  6818. * Empty types aren't explicitly instantiated. Therefore, many of the functions
  6819. * normally available for non-empty types will not be available for empty ones.
  6820. *
  6821. * @sa sparse_set<Entity>
  6822. *
  6823. * @tparam Entity A valid entity type (see entt_traits for more details).
  6824. * @tparam Type Type of objects assigned to the entities.
  6825. * @tparam Allocator Type of allocator used to manage memory and elements.
  6826. */
  6827. template<typename Entity, typename Type, typename Allocator, typename = void>
  6828. class basic_storage_impl: public basic_sparse_set<Entity, typename std::allocator_traits<Allocator>::template rebind_alloc<Entity>> {
  6829. static constexpr auto packed_page = ENTT_PACKED_PAGE;
  6830. using comp_traits = component_traits<Type>;
  6831. using underlying_type = basic_sparse_set<Entity, typename std::allocator_traits<Allocator>::template rebind_alloc<Entity>>;
  6832. using difference_type = typename entt_traits<Entity>::difference_type;
  6833. using alloc_traits = typename std::allocator_traits<Allocator>::template rebind_traits<Type>;
  6834. using alloc_pointer = typename alloc_traits::pointer;
  6835. using alloc_const_pointer = typename alloc_traits::const_pointer;
  6836. using bucket_alloc_traits = typename std::allocator_traits<Allocator>::template rebind_traits<alloc_pointer>;
  6837. using bucket_alloc_pointer = typename bucket_alloc_traits::pointer;
  6838. using bucket_alloc_const_type = typename std::allocator_traits<Allocator>::template rebind_alloc<alloc_const_pointer>;
  6839. using bucket_alloc_const_pointer = typename std::allocator_traits<bucket_alloc_const_type>::const_pointer;
  6840. static_assert(alloc_traits::propagate_on_container_move_assignment::value);
  6841. static_assert(bucket_alloc_traits::propagate_on_container_move_assignment::value);
  6842. template<typename Value>
  6843. struct storage_iterator final {
  6844. using difference_type = typename basic_storage_impl::difference_type;
  6845. using value_type = Value;
  6846. using pointer = value_type *;
  6847. using reference = value_type &;
  6848. using iterator_category = std::random_access_iterator_tag;
  6849. storage_iterator() ENTT_NOEXCEPT = default;
  6850. storage_iterator(bucket_alloc_pointer const *ref, const typename basic_storage_impl::difference_type idx) ENTT_NOEXCEPT
  6851. : packed{ref},
  6852. index{idx}
  6853. {}
  6854. storage_iterator & operator++() ENTT_NOEXCEPT {
  6855. return --index, *this;
  6856. }
  6857. storage_iterator operator++(int) ENTT_NOEXCEPT {
  6858. storage_iterator orig = *this;
  6859. return ++(*this), orig;
  6860. }
  6861. storage_iterator & operator--() ENTT_NOEXCEPT {
  6862. return ++index, *this;
  6863. }
  6864. storage_iterator operator--(int) ENTT_NOEXCEPT {
  6865. storage_iterator orig = *this;
  6866. return operator--(), orig;
  6867. }
  6868. storage_iterator & operator+=(const difference_type value) ENTT_NOEXCEPT {
  6869. index -= value;
  6870. return *this;
  6871. }
  6872. storage_iterator operator+(const difference_type value) const ENTT_NOEXCEPT {
  6873. storage_iterator copy = *this;
  6874. return (copy += value);
  6875. }
  6876. storage_iterator & operator-=(const difference_type value) ENTT_NOEXCEPT {
  6877. return (*this += -value);
  6878. }
  6879. storage_iterator operator-(const difference_type value) const ENTT_NOEXCEPT {
  6880. return (*this + -value);
  6881. }
  6882. difference_type operator-(const storage_iterator &other) const ENTT_NOEXCEPT {
  6883. return other.index - index;
  6884. }
  6885. [[nodiscard]] reference operator[](const difference_type value) const ENTT_NOEXCEPT {
  6886. const auto pos = size_type(index-value-1);
  6887. return (*packed)[page(pos)][offset(pos)];
  6888. }
  6889. [[nodiscard]] bool operator==(const storage_iterator &other) const ENTT_NOEXCEPT {
  6890. return other.index == index;
  6891. }
  6892. [[nodiscard]] bool operator!=(const storage_iterator &other) const ENTT_NOEXCEPT {
  6893. return !(*this == other);
  6894. }
  6895. [[nodiscard]] bool operator<(const storage_iterator &other) const ENTT_NOEXCEPT {
  6896. return index > other.index;
  6897. }
  6898. [[nodiscard]] bool operator>(const storage_iterator &other) const ENTT_NOEXCEPT {
  6899. return index < other.index;
  6900. }
  6901. [[nodiscard]] bool operator<=(const storage_iterator &other) const ENTT_NOEXCEPT {
  6902. return !(*this > other);
  6903. }
  6904. [[nodiscard]] bool operator>=(const storage_iterator &other) const ENTT_NOEXCEPT {
  6905. return !(*this < other);
  6906. }
  6907. [[nodiscard]] pointer operator->() const ENTT_NOEXCEPT {
  6908. const auto pos = size_type(index-1u);
  6909. return std::addressof((*packed)[page(pos)][offset(pos)]);
  6910. }
  6911. [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
  6912. return *operator->();
  6913. }
  6914. private:
  6915. bucket_alloc_pointer const *packed;
  6916. difference_type index;
  6917. };
  6918. [[nodiscard]] static auto page(const std::size_t pos) ENTT_NOEXCEPT {
  6919. return pos / packed_page;
  6920. }
  6921. [[nodiscard]] static auto offset(const std::size_t pos) ENTT_NOEXCEPT {
  6922. return pos & (packed_page - 1);
  6923. }
  6924. void release_memory() {
  6925. if(packed) {
  6926. // no-throw stable erase iteration
  6927. underlying_type::clear();
  6928. for(size_type pos{}; pos < bucket; ++pos) {
  6929. alloc_traits::deallocate(allocator, packed[pos], packed_page);
  6930. bucket_alloc_traits::destroy(bucket_allocator, std::addressof(packed[pos]));
  6931. }
  6932. bucket_alloc_traits::deallocate(bucket_allocator, packed, bucket);
  6933. }
  6934. }
  6935. void assure_at_least(const std::size_t last) {
  6936. if(const auto idx = page(last - 1u); !(idx < bucket)) {
  6937. const size_type sz = idx + 1u;
  6938. const auto mem = bucket_alloc_traits::allocate(bucket_allocator, sz);
  6939. std::uninitialized_copy(packed, packed + bucket, mem);
  6940. size_type pos{};
  6941. ENTT_TRY {
  6942. for(pos = bucket; pos < sz; ++pos) {
  6943. auto pg = alloc_traits::allocate(allocator, packed_page);
  6944. bucket_alloc_traits::construct(bucket_allocator, std::addressof(mem[pos]), pg);
  6945. }
  6946. } ENTT_CATCH {
  6947. for(auto next = bucket; next < pos; ++next) {
  6948. alloc_traits::deallocate(allocator, mem[next], packed_page);
  6949. }
  6950. std::destroy(mem, mem + pos);
  6951. bucket_alloc_traits::deallocate(bucket_allocator, mem, sz);
  6952. ENTT_THROW;
  6953. }
  6954. std::destroy(packed, packed + bucket);
  6955. bucket_alloc_traits::deallocate(bucket_allocator, packed, bucket);
  6956. packed = mem;
  6957. bucket = sz;
  6958. }
  6959. }
  6960. void release_unused_pages() {
  6961. if(const auto length = underlying_type::size() / packed_page; length < bucket) {
  6962. const auto mem = bucket_alloc_traits::allocate(bucket_allocator, length);
  6963. std::uninitialized_copy(packed, packed + length, mem);
  6964. for(auto pos = length; pos < bucket; ++pos) {
  6965. alloc_traits::deallocate(allocator, packed[pos], packed_page);
  6966. bucket_alloc_traits::destroy(bucket_allocator, std::addressof(packed[pos]));
  6967. }
  6968. bucket_alloc_traits::deallocate(bucket_allocator, packed, bucket);
  6969. packed = mem;
  6970. bucket = length;
  6971. }
  6972. }
  6973. template<typename... Args>
  6974. auto & push_at(const std::size_t pos, Args &&... args) {
  6975. ENTT_ASSERT(pos < (bucket * packed_page), "Out of bounds index");
  6976. auto *instance = std::addressof(packed[page(pos)][offset(pos)]);
  6977. if constexpr(std::is_aggregate_v<value_type>) {
  6978. alloc_traits::construct(allocator, instance, Type{std::forward<Args>(args)...});
  6979. } else {
  6980. alloc_traits::construct(allocator, instance, std::forward<Args>(args)...);
  6981. }
  6982. return *instance;
  6983. }
  6984. void pop_at(const std::size_t pos) {
  6985. alloc_traits::destroy(allocator, std::addressof(packed[page(pos)][offset(pos)]));
  6986. }
  6987. protected:
  6988. /*! @copydoc basic_sparse_set::swap_at */
  6989. void swap_at(const std::size_t lhs, const std::size_t rhs) final {
  6990. std::swap(packed[page(lhs)][offset(lhs)], packed[page(rhs)][offset(rhs)]);
  6991. }
  6992. /*! @copydoc basic_sparse_set::move_and_pop */
  6993. void move_and_pop(const std::size_t from, const std::size_t to) final {
  6994. push_at(to, std::move(packed[page(from)][offset(from)]));
  6995. pop_at(from);
  6996. }
  6997. /*! @copydoc basic_sparse_set::swap_and_pop */
  6998. void swap_and_pop(const Entity entt, void *ud) override {
  6999. const auto pos = underlying_type::index(entt);
  7000. const auto last = underlying_type::size() - 1u;
  7001. auto &&elem = packed[page(pos)][offset(pos)];
  7002. // support for nosy destructors
  7003. [[maybe_unused]] auto unused = std::move(elem);
  7004. elem = std::move(packed[page(last)][offset(last)]);
  7005. pop_at(last);
  7006. underlying_type::swap_and_pop(entt, ud);
  7007. }
  7008. /*! @copydoc basic_sparse_set::in_place_pop */
  7009. void in_place_pop(const Entity entt, void *ud) override {
  7010. const auto pos = underlying_type::index(entt);
  7011. underlying_type::in_place_pop(entt, ud);
  7012. // support for nosy destructors
  7013. pop_at(pos);
  7014. }
  7015. public:
  7016. /*! @brief Allocator type. */
  7017. using allocator_type = typename alloc_traits::allocator_type;
  7018. /*! @brief Type of the objects assigned to entities. */
  7019. using value_type = Type;
  7020. /*! @brief Underlying entity identifier. */
  7021. using entity_type = Entity;
  7022. /*! @brief Unsigned integer type. */
  7023. using size_type = std::size_t;
  7024. /*! @brief Pointer type to contained elements. */
  7025. using pointer = bucket_alloc_pointer;
  7026. /*! @brief Constant pointer type to contained elements. */
  7027. using const_pointer = bucket_alloc_const_pointer;
  7028. /*! @brief Random access iterator type. */
  7029. using iterator = storage_iterator<value_type>;
  7030. /*! @brief Constant random access iterator type. */
  7031. using const_iterator = storage_iterator<const value_type>;
  7032. /*! @brief Reverse iterator type. */
  7033. using reverse_iterator = std::reverse_iterator<iterator>;
  7034. /*! @brief Constant reverse iterator type. */
  7035. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  7036. /**
  7037. * @brief Default constructor.
  7038. * @param alloc Allocator to use (possibly default-constructed).
  7039. */
  7040. explicit basic_storage_impl(const allocator_type &alloc = {})
  7041. : underlying_type{deletion_policy{comp_traits::in_place_delete::value}, alloc},
  7042. allocator{alloc},
  7043. bucket_allocator{alloc},
  7044. packed{bucket_alloc_traits::allocate(bucket_allocator, 0u)},
  7045. bucket{}
  7046. {}
  7047. /**
  7048. * @brief Move constructor.
  7049. * @param other The instance to move from.
  7050. */
  7051. basic_storage_impl(basic_storage_impl &&other) ENTT_NOEXCEPT
  7052. : underlying_type{std::move(other)},
  7053. allocator{std::move(other.allocator)},
  7054. bucket_allocator{std::move(other.bucket_allocator)},
  7055. packed{std::exchange(other.packed, bucket_alloc_pointer{})},
  7056. bucket{std::exchange(other.bucket, 0u)}
  7057. {}
  7058. /*! @brief Default destructor. */
  7059. ~basic_storage_impl() override {
  7060. release_memory();
  7061. }
  7062. /**
  7063. * @brief Move assignment operator.
  7064. * @param other The instance to move from.
  7065. * @return This sparse set.
  7066. */
  7067. basic_storage_impl & operator=(basic_storage_impl &&other) ENTT_NOEXCEPT {
  7068. release_memory();
  7069. underlying_type::operator=(std::move(other));
  7070. allocator = std::move(other.allocator);
  7071. bucket_allocator = std::move(other.bucket_allocator);
  7072. packed = std::exchange(other.packed, bucket_alloc_pointer{});
  7073. bucket = std::exchange(other.bucket, 0u);
  7074. return *this;
  7075. }
  7076. /**
  7077. * @brief Increases the capacity of a storage.
  7078. *
  7079. * If the new capacity is greater than the current capacity, new storage is
  7080. * allocated, otherwise the method does nothing.
  7081. *
  7082. * @param cap Desired capacity.
  7083. */
  7084. void reserve(const size_type cap) {
  7085. underlying_type::reserve(cap);
  7086. if(cap > underlying_type::size()) {
  7087. assure_at_least(cap);
  7088. }
  7089. }
  7090. /**
  7091. * @brief Returns the number of elements that a storage has currently
  7092. * allocated space for.
  7093. * @return Capacity of the storage.
  7094. */
  7095. [[nodiscard]] size_type capacity() const ENTT_NOEXCEPT {
  7096. return bucket * packed_page;
  7097. }
  7098. /*! @brief Requests the removal of unused capacity. */
  7099. void shrink_to_fit() {
  7100. underlying_type::shrink_to_fit();
  7101. release_unused_pages();
  7102. }
  7103. /**
  7104. * @brief Direct access to the array of objects.
  7105. * @return A pointer to the array of objects.
  7106. */
  7107. [[nodiscard]] const_pointer raw() const ENTT_NOEXCEPT {
  7108. return packed;
  7109. }
  7110. /*! @copydoc raw */
  7111. [[nodiscard]] pointer raw() ENTT_NOEXCEPT {
  7112. return packed;
  7113. }
  7114. /**
  7115. * @brief Returns an iterator to the beginning.
  7116. *
  7117. * The returned iterator points to the first instance of the internal array.
  7118. * If the storage is empty, the returned iterator will be equal to `end()`.
  7119. *
  7120. * @return An iterator to the first instance of the internal array.
  7121. */
  7122. [[nodiscard]] const_iterator cbegin() const ENTT_NOEXCEPT {
  7123. const difference_type pos = underlying_type::size();
  7124. return const_iterator{std::addressof(packed), pos};
  7125. }
  7126. /*! @copydoc cbegin */
  7127. [[nodiscard]] const_iterator begin() const ENTT_NOEXCEPT {
  7128. return cbegin();
  7129. }
  7130. /*! @copydoc begin */
  7131. [[nodiscard]] iterator begin() ENTT_NOEXCEPT {
  7132. const difference_type pos = underlying_type::size();
  7133. return iterator{std::addressof(packed), pos};
  7134. }
  7135. /**
  7136. * @brief Returns an iterator to the end.
  7137. *
  7138. * The returned iterator points to the element following the last instance
  7139. * of the internal array. Attempting to dereference the returned iterator
  7140. * results in undefined behavior.
  7141. *
  7142. * @return An iterator to the element following the last instance of the
  7143. * internal array.
  7144. */
  7145. [[nodiscard]] const_iterator cend() const ENTT_NOEXCEPT {
  7146. return const_iterator{std::addressof(packed), {}};
  7147. }
  7148. /*! @copydoc cend */
  7149. [[nodiscard]] const_iterator end() const ENTT_NOEXCEPT {
  7150. return cend();
  7151. }
  7152. /*! @copydoc end */
  7153. [[nodiscard]] iterator end() ENTT_NOEXCEPT {
  7154. return iterator{std::addressof(packed), {}};
  7155. }
  7156. /**
  7157. * @brief Returns a reverse iterator to the beginning.
  7158. *
  7159. * The returned iterator points to the first instance of the reversed
  7160. * internal array. If the storage is empty, the returned iterator will be
  7161. * equal to `rend()`.
  7162. *
  7163. * @return An iterator to the first instance of the reversed internal array.
  7164. */
  7165. [[nodiscard]] const_reverse_iterator crbegin() const ENTT_NOEXCEPT {
  7166. return std::make_reverse_iterator(cend());
  7167. }
  7168. /*! @copydoc crbegin */
  7169. [[nodiscard]] const_reverse_iterator rbegin() const ENTT_NOEXCEPT {
  7170. return crbegin();
  7171. }
  7172. /*! @copydoc rbegin */
  7173. [[nodiscard]] reverse_iterator rbegin() ENTT_NOEXCEPT {
  7174. return std::make_reverse_iterator(end());
  7175. }
  7176. /**
  7177. * @brief Returns a reverse iterator to the end.
  7178. *
  7179. * The returned iterator points to the element following the last instance
  7180. * of the reversed internal array. Attempting to dereference the returned
  7181. * iterator results in undefined behavior.
  7182. *
  7183. * @return An iterator to the element following the last instance of the
  7184. * reversed internal array.
  7185. */
  7186. [[nodiscard]] const_reverse_iterator crend() const ENTT_NOEXCEPT {
  7187. return std::make_reverse_iterator(cbegin());
  7188. }
  7189. /*! @copydoc crend */
  7190. [[nodiscard]] const_reverse_iterator rend() const ENTT_NOEXCEPT {
  7191. return crend();
  7192. }
  7193. /*! @copydoc rend */
  7194. [[nodiscard]] reverse_iterator rend() ENTT_NOEXCEPT {
  7195. return std::make_reverse_iterator(begin());
  7196. }
  7197. /**
  7198. * @brief Returns the object assigned to an entity.
  7199. *
  7200. * @warning
  7201. * Attempting to use an entity that doesn't belong to the storage results in
  7202. * undefined behavior.
  7203. *
  7204. * @param entt A valid entity identifier.
  7205. * @return The object assigned to the entity.
  7206. */
  7207. [[nodiscard]] const value_type & get(const entity_type entt) const ENTT_NOEXCEPT {
  7208. const auto idx = underlying_type::index(entt);
  7209. return packed[page(idx)][offset(idx)];
  7210. }
  7211. /*! @copydoc get */
  7212. [[nodiscard]] value_type & get(const entity_type entt) ENTT_NOEXCEPT {
  7213. return const_cast<value_type &>(std::as_const(*this).get(entt));
  7214. }
  7215. /**
  7216. * @brief Assigns an entity to a storage and constructs its object.
  7217. *
  7218. * This version accept both types that can be constructed in place directly
  7219. * and types like aggregates that do not work well with a placement new as
  7220. * performed usually under the hood during an _emplace back_.
  7221. *
  7222. * @warning
  7223. * Attempting to use an entity that already belongs to the storage results
  7224. * in undefined behavior.
  7225. *
  7226. * @tparam Args Types of arguments to use to construct the object.
  7227. * @param entt A valid entity identifier.
  7228. * @param args Parameters to use to construct an object for the entity.
  7229. * @return A reference to the newly created object.
  7230. */
  7231. template<typename... Args>
  7232. value_type & emplace(const entity_type entt, Args &&... args) {
  7233. const auto pos = underlying_type::slot();
  7234. assure_at_least(pos + 1u);
  7235. auto &value = push_at(pos, std::forward<Args>(args)...);
  7236. ENTT_TRY {
  7237. [[maybe_unused]] const auto curr = underlying_type::emplace(entt);
  7238. ENTT_ASSERT(pos == curr, "Misplaced component");
  7239. } ENTT_CATCH {
  7240. pop_at(pos);
  7241. ENTT_THROW;
  7242. }
  7243. return value;
  7244. }
  7245. /**
  7246. * @brief Updates the instance assigned to a given entity in-place.
  7247. * @tparam Func Types of the function objects to invoke.
  7248. * @param entt A valid entity identifier.
  7249. * @param func Valid function objects.
  7250. * @return A reference to the updated instance.
  7251. */
  7252. template<typename... Func>
  7253. decltype(auto) patch(const entity_type entt, Func &&... func) {
  7254. const auto idx = underlying_type::index(entt);
  7255. auto &&elem = packed[page(idx)][offset(idx)];
  7256. (std::forward<Func>(func)(elem), ...);
  7257. return elem;
  7258. }
  7259. /**
  7260. * @brief Assigns one or more entities to a storage and constructs their
  7261. * objects from a given instance.
  7262. *
  7263. * @warning
  7264. * Attempting to assign an entity that already belongs to the storage
  7265. * results in undefined behavior.
  7266. *
  7267. * @tparam It Type of input iterator.
  7268. * @param first An iterator to the first element of the range of entities.
  7269. * @param last An iterator past the last element of the range of entities.
  7270. * @param value An instance of the object to construct.
  7271. */
  7272. template<typename It>
  7273. void insert(It first, It last, const value_type &value = {}) {
  7274. const auto cap = underlying_type::size() + std::distance(first, last);
  7275. underlying_type::reserve(cap);
  7276. assure_at_least(cap);
  7277. for(; first != last; ++first) {
  7278. push_at(underlying_type::size(), value);
  7279. ENTT_TRY {
  7280. underlying_type::emplace_back(*first);
  7281. } ENTT_CATCH {
  7282. pop_at(underlying_type::size());
  7283. ENTT_THROW;
  7284. }
  7285. }
  7286. }
  7287. /**
  7288. * @brief Assigns one or more entities to a storage and constructs their
  7289. * objects from a given range.
  7290. *
  7291. * @sa construct
  7292. *
  7293. * @tparam EIt Type of input iterator.
  7294. * @tparam CIt Type of input iterator.
  7295. * @param first An iterator to the first element of the range of entities.
  7296. * @param last An iterator past the last element of the range of entities.
  7297. * @param from An iterator to the first element of the range of objects.
  7298. */
  7299. template<typename EIt, typename CIt, typename = std::enable_if_t<std::is_same_v<std::decay_t<typename std::iterator_traits<CIt>::value_type>, value_type>>>
  7300. void insert(EIt first, EIt last, CIt from) {
  7301. const auto cap = underlying_type::size() + std::distance(first, last);
  7302. underlying_type::reserve(cap);
  7303. assure_at_least(cap);
  7304. for(; first != last; ++first, ++from) {
  7305. push_at(underlying_type::size(), *from);
  7306. ENTT_TRY {
  7307. underlying_type::emplace_back(*first);
  7308. } ENTT_CATCH {
  7309. pop_at(underlying_type::size());
  7310. ENTT_THROW;
  7311. }
  7312. }
  7313. }
  7314. /**
  7315. * @brief Sort elements according to the given comparison function.
  7316. *
  7317. * The comparison function object must return `true` if the first element
  7318. * is _less_ than the second one, `false` otherwise. The signature of the
  7319. * comparison function should be equivalent to one of the following:
  7320. *
  7321. * @code{.cpp}
  7322. * bool(const Entity, const Entity);
  7323. * bool(const Type &, const Type &);
  7324. * @endcode
  7325. *
  7326. * Moreover, the comparison function object shall induce a
  7327. * _strict weak ordering_ on the values.
  7328. *
  7329. * The sort function oject must offer a member function template
  7330. * `operator()` that accepts three arguments:
  7331. *
  7332. * * An iterator to the first element of the range to sort.
  7333. * * An iterator past the last element of the range to sort.
  7334. * * A comparison function to use to compare the elements.
  7335. *
  7336. * @warning
  7337. * Empty types are never instantiated. Therefore, only comparison function
  7338. * objects that require to return entities rather than components are
  7339. * accepted.
  7340. *
  7341. * @tparam Compare Type of comparison function object.
  7342. * @tparam Sort Type of sort function object.
  7343. * @tparam Args Types of arguments to forward to the sort function object.
  7344. * @param length Number of elements to sort.
  7345. * @param compare A valid comparison function object.
  7346. * @param algo A valid sort function object.
  7347. * @param args Arguments to forward to the sort function object, if any.
  7348. */
  7349. template<typename Compare, typename Sort = std_sort, typename... Args>
  7350. void sort_n(const size_type length, Compare compare, Sort algo = Sort{}, Args &&... args) {
  7351. if constexpr(std::is_invocable_v<Compare, const value_type &, const value_type &>) {
  7352. underlying_type::sort_n(length, [this, compare = std::move(compare)](const auto lhs, const auto rhs) {
  7353. const auto ilhs = underlying_type::index(lhs), irhs = underlying_type::index(rhs);
  7354. return compare(std::as_const(packed[page(ilhs)][offset(ilhs)]), std::as_const(packed[page(irhs)][offset(irhs)]));
  7355. }, std::move(algo), std::forward<Args>(args)...);
  7356. } else {
  7357. underlying_type::sort_n(length, std::move(compare), std::move(algo), std::forward<Args>(args)...);
  7358. }
  7359. }
  7360. /**
  7361. * @brief Sort all elements according to the given comparison function.
  7362. *
  7363. * @sa sort_n
  7364. *
  7365. * @tparam Compare Type of comparison function object.
  7366. * @tparam Sort Type of sort function object.
  7367. * @tparam Args Types of arguments to forward to the sort function object.
  7368. * @param compare A valid comparison function object.
  7369. * @param algo A valid sort function object.
  7370. * @param args Arguments to forward to the sort function object, if any.
  7371. */
  7372. template<typename Compare, typename Sort = std_sort, typename... Args>
  7373. void sort(Compare compare, Sort algo = Sort{}, Args &&... args) {
  7374. sort_n(underlying_type::size(), std::move(compare), std::move(algo), std::forward<Args>(args)...);
  7375. }
  7376. private:
  7377. typename alloc_traits::allocator_type allocator;
  7378. typename bucket_alloc_traits::allocator_type bucket_allocator;
  7379. bucket_alloc_pointer packed;
  7380. size_type bucket;
  7381. };
  7382. /*! @copydoc basic_storage_impl */
  7383. template<typename Entity, typename Type, typename Allocator>
  7384. class basic_storage_impl<Entity, Type, Allocator, std::enable_if_t<component_traits<Type>::ignore_if_empty::value && std::is_empty_v<Type>>>
  7385. : public basic_sparse_set<Entity, typename std::allocator_traits<Allocator>::template rebind_alloc<Entity>>
  7386. {
  7387. using comp_traits = component_traits<Type>;
  7388. using underlying_type = basic_sparse_set<Entity, typename std::allocator_traits<Allocator>::template rebind_alloc<Entity>>;
  7389. using alloc_traits = typename std::allocator_traits<Allocator>::template rebind_traits<Type>;
  7390. public:
  7391. /*! @brief Allocator type. */
  7392. using allocator_type = typename alloc_traits::allocator_type;
  7393. /*! @brief Type of the objects assigned to entities. */
  7394. using value_type = Type;
  7395. /*! @brief Underlying entity identifier. */
  7396. using entity_type = Entity;
  7397. /*! @brief Unsigned integer type. */
  7398. using size_type = std::size_t;
  7399. /**
  7400. * @brief Default constructor.
  7401. * @param alloc Allocator to use (possibly default-constructed).
  7402. */
  7403. explicit basic_storage_impl(const allocator_type &alloc = {})
  7404. : underlying_type{deletion_policy{comp_traits::in_place_delete::value}, alloc}
  7405. {}
  7406. /**
  7407. * @brief Fake get function.
  7408. *
  7409. * @warning
  7410. * Attempting to use an entity that doesn't belong to the storage results in
  7411. * undefined behavior.
  7412. *
  7413. * @param entt A valid entity identifier.
  7414. */
  7415. void get([[maybe_unused]] const entity_type entt) const ENTT_NOEXCEPT {
  7416. ENTT_ASSERT(underlying_type::contains(entt), "Storage does not contain entity");
  7417. }
  7418. /**
  7419. * @brief Assigns an entity to a storage and constructs its object.
  7420. *
  7421. * @warning
  7422. * Attempting to use an entity that already belongs to the storage results
  7423. * in undefined behavior.
  7424. *
  7425. * @tparam Args Types of arguments to use to construct the object.
  7426. * @param entt A valid entity identifier.
  7427. * @param args Parameters to use to construct an object for the entity.
  7428. */
  7429. template<typename... Args>
  7430. void emplace(const entity_type entt, Args &&... args) {
  7431. [[maybe_unused]] value_type instance{std::forward<Args>(args)...};
  7432. underlying_type::emplace(entt);
  7433. }
  7434. /**
  7435. * @brief Updates the instance assigned to a given entity in-place.
  7436. * @tparam Func Types of the function objects to invoke.
  7437. * @param entt A valid entity identifier.
  7438. * @param func Valid function objects.
  7439. */
  7440. template<typename... Func>
  7441. void patch([[maybe_unused]] const entity_type entt, Func &&... func) {
  7442. ENTT_ASSERT(underlying_type::contains(entt), "Storage does not contain entity");
  7443. (std::forward<Func>(func)(), ...);
  7444. }
  7445. /**
  7446. * @brief Assigns one or more entities to a storage.
  7447. *
  7448. * @warning
  7449. * Attempting to assign an entity that already belongs to the storage
  7450. * results in undefined behavior.
  7451. *
  7452. * @tparam It Type of input iterator.
  7453. * @param first An iterator to the first element of the range of entities.
  7454. * @param last An iterator past the last element of the range of entities.
  7455. */
  7456. template<typename It>
  7457. void insert(It first, It last, const value_type & = {}) {
  7458. underlying_type::insert(first, last);
  7459. }
  7460. };
  7461. /**
  7462. * @brief Mixin type to use to wrap basic storage classes.
  7463. * @tparam Type The type of the underlying storage.
  7464. */
  7465. template<typename Type>
  7466. struct storage_adapter_mixin: Type {
  7467. static_assert(std::is_same_v<typename Type::value_type, std::decay_t<typename Type::value_type>>, "Invalid object type");
  7468. /*! @brief Type of the objects assigned to entities. */
  7469. using value_type = typename Type::value_type;
  7470. /*! @brief Underlying entity identifier. */
  7471. using entity_type = typename Type::entity_type;
  7472. /*! @brief Inherited constructors. */
  7473. using Type::Type;
  7474. /**
  7475. * @brief Assigns entities to a storage.
  7476. * @tparam Args Types of arguments to use to construct the object.
  7477. * @param entt A valid entity identifier.
  7478. * @param args Parameters to use to initialize the object.
  7479. * @return A reference to the newly created object.
  7480. */
  7481. template<typename... Args>
  7482. decltype(auto) emplace(basic_registry<entity_type> &, const entity_type entt, Args &&... args) {
  7483. return Type::emplace(entt, std::forward<Args>(args)...);
  7484. }
  7485. /**
  7486. * @brief Assigns entities to a storage.
  7487. * @tparam It Type of input iterator.
  7488. * @tparam Args Types of arguments to use to construct the objects assigned
  7489. * to the entities.
  7490. * @param first An iterator to the first element of the range of entities.
  7491. * @param last An iterator past the last element of the range of entities.
  7492. * @param args Parameters to use to initialize the objects assigned to the
  7493. * entities.
  7494. */
  7495. template<typename It, typename... Args>
  7496. void insert(basic_registry<entity_type> &, It first, It last, Args &&... args) {
  7497. Type::insert(first, last, std::forward<Args>(args)...);
  7498. }
  7499. /**
  7500. * @brief Patches the given instance for an entity.
  7501. * @tparam Func Types of the function objects to invoke.
  7502. * @param entt A valid entity identifier.
  7503. * @param func Valid function objects.
  7504. * @return A reference to the patched instance.
  7505. */
  7506. template<typename... Func>
  7507. decltype(auto) patch(basic_registry<entity_type> &, const entity_type entt, Func &&... func) {
  7508. return Type::patch(entt, std::forward<Func>(func)...);
  7509. }
  7510. };
  7511. /**
  7512. * @brief Mixin type to use to add signal support to storage types.
  7513. * @tparam Type The type of the underlying storage.
  7514. */
  7515. template<typename Type>
  7516. class sigh_storage_mixin final: public Type {
  7517. /*! @copydoc basic_sparse_set::swap_and_pop */
  7518. void swap_and_pop(const typename Type::entity_type entt, void *ud) final {
  7519. ENTT_ASSERT(ud != nullptr, "Invalid pointer to registry");
  7520. destruction.publish(*static_cast<basic_registry<typename Type::entity_type> *>(ud), entt);
  7521. Type::swap_and_pop(entt, ud);
  7522. }
  7523. /*! @copydoc basic_sparse_set::in_place_pop */
  7524. void in_place_pop(const typename Type::entity_type entt, void *ud) final {
  7525. ENTT_ASSERT(ud != nullptr, "Invalid pointer to registry");
  7526. destruction.publish(*static_cast<basic_registry<typename Type::entity_type> *>(ud), entt);
  7527. Type::in_place_pop(entt, ud);
  7528. }
  7529. public:
  7530. /*! @brief Underlying value type. */
  7531. using value_type = typename Type::value_type;
  7532. /*! @brief Underlying entity identifier. */
  7533. using entity_type = typename Type::entity_type;
  7534. /*! @brief Inherited constructors. */
  7535. using Type::Type;
  7536. /**
  7537. * @brief Returns a sink object.
  7538. *
  7539. * The sink returned by this function can be used to receive notifications
  7540. * whenever a new instance is created and assigned to an entity.<br/>
  7541. * The function type for a listener is equivalent to:
  7542. *
  7543. * @code{.cpp}
  7544. * void(basic_registry<entity_type> &, entity_type);
  7545. * @endcode
  7546. *
  7547. * Listeners are invoked **after** the object has been assigned to the
  7548. * entity.
  7549. *
  7550. * @sa sink
  7551. *
  7552. * @return A temporary sink object.
  7553. */
  7554. [[nodiscard]] auto on_construct() ENTT_NOEXCEPT {
  7555. return sink{construction};
  7556. }
  7557. /**
  7558. * @brief Returns a sink object.
  7559. *
  7560. * The sink returned by this function can be used to receive notifications
  7561. * whenever an instance is explicitly updated.<br/>
  7562. * The function type for a listener is equivalent to:
  7563. *
  7564. * @code{.cpp}
  7565. * void(basic_registry<entity_type> &, entity_type);
  7566. * @endcode
  7567. *
  7568. * Listeners are invoked **after** the object has been updated.
  7569. *
  7570. * @sa sink
  7571. *
  7572. * @return A temporary sink object.
  7573. */
  7574. [[nodiscard]] auto on_update() ENTT_NOEXCEPT {
  7575. return sink{update};
  7576. }
  7577. /**
  7578. * @brief Returns a sink object.
  7579. *
  7580. * The sink returned by this function can be used to receive notifications
  7581. * whenever an instance is removed from an entity and thus destroyed.<br/>
  7582. * The function type for a listener is equivalent to:
  7583. *
  7584. * @code{.cpp}
  7585. * void(basic_registry<entity_type> &, entity_type);
  7586. * @endcode
  7587. *
  7588. * Listeners are invoked **before** the object has been removed from the
  7589. * entity.
  7590. *
  7591. * @sa sink
  7592. *
  7593. * @return A temporary sink object.
  7594. */
  7595. [[nodiscard]] auto on_destroy() ENTT_NOEXCEPT {
  7596. return sink{destruction};
  7597. }
  7598. /**
  7599. * @brief Assigns entities to a storage.
  7600. * @tparam Args Types of arguments to use to construct the object.
  7601. * @param owner The registry that issued the request.
  7602. * @param entt A valid entity identifier.
  7603. * @param args Parameters to use to initialize the object.
  7604. * @return A reference to the newly created object.
  7605. */
  7606. template<typename... Args>
  7607. decltype(auto) emplace(basic_registry<entity_type> &owner, const entity_type entt, Args &&... args) {
  7608. Type::emplace(entt, std::forward<Args>(args)...);
  7609. construction.publish(owner, entt);
  7610. return this->get(entt);
  7611. }
  7612. /**
  7613. * @brief Assigns entities to a storage.
  7614. * @tparam It Type of input iterator.
  7615. * @tparam Args Types of arguments to use to construct the objects assigned
  7616. * to the entities.
  7617. * @param owner The registry that issued the request.
  7618. * @param first An iterator to the first element of the range of entities.
  7619. * @param last An iterator past the last element of the range of entities.
  7620. * @param args Parameters to use to initialize the objects assigned to the
  7621. * entities.
  7622. */
  7623. template<typename It, typename... Args>
  7624. void insert(basic_registry<entity_type> &owner, It first, It last, Args &&... args) {
  7625. Type::insert(first, last, std::forward<Args>(args)...);
  7626. if(!construction.empty()) {
  7627. for(; first != last; ++first) {
  7628. construction.publish(owner, *first);
  7629. }
  7630. }
  7631. }
  7632. /**
  7633. * @brief Patches the given instance for an entity.
  7634. * @tparam Func Types of the function objects to invoke.
  7635. * @param owner The registry that issued the request.
  7636. * @param entt A valid entity identifier.
  7637. * @param func Valid function objects.
  7638. * @return A reference to the patched instance.
  7639. */
  7640. template<typename... Func>
  7641. decltype(auto) patch(basic_registry<entity_type> &owner, const entity_type entt, Func &&... func) {
  7642. Type::patch(entt, std::forward<Func>(func)...);
  7643. update.publish(owner, entt);
  7644. return this->get(entt);
  7645. }
  7646. private:
  7647. sigh<void(basic_registry<entity_type> &, const entity_type)> construction{};
  7648. sigh<void(basic_registry<entity_type> &, const entity_type)> destruction{};
  7649. sigh<void(basic_registry<entity_type> &, const entity_type)> update{};
  7650. };
  7651. /**
  7652. * @brief Storage implementation dispatcher.
  7653. * @tparam Entity A valid entity type (see entt_traits for more details).
  7654. * @tparam Type Type of objects assigned to the entities.
  7655. * @tparam Allocator Type of allocator used to manage memory and elements.
  7656. */
  7657. template<typename Entity, typename Type, typename Allocator>
  7658. struct basic_storage: basic_storage_impl<Entity, Type, Allocator> {
  7659. using basic_storage_impl<Entity, Type, Allocator>::basic_storage_impl;
  7660. };
  7661. /**
  7662. * @brief Provides a common way to access certain properties of storage types.
  7663. * @tparam Entity A valid entity type (see entt_traits for more details).
  7664. * @tparam Type Type of objects managed by the storage class.
  7665. */
  7666. template<typename Entity, typename Type, typename = void>
  7667. struct storage_traits {
  7668. /*! @brief Resulting type after component-to-storage conversion. */
  7669. using storage_type = sigh_storage_mixin<basic_storage<Entity, Type>>;
  7670. };
  7671. /**
  7672. * @brief Gets the element assigned to an entity from a storage, if any.
  7673. * @tparam Type Storage type.
  7674. * @param container A valid instance of a storage class.
  7675. * @param entt A valid entity identifier.
  7676. * @return A possibly empty tuple containing the requested element.
  7677. */
  7678. template<typename Type>
  7679. [[nodiscard]] auto get_as_tuple([[maybe_unused]] Type &container, [[maybe_unused]] const typename Type::entity_type entt) {
  7680. static_assert(std::is_same_v<std::remove_const_t<Type>, typename storage_traits<typename Type::entity_type, typename Type::value_type>::storage_type>, "Invalid storage");
  7681. if constexpr(std::is_void_v<decltype(container.get({}))>) {
  7682. return std::make_tuple();
  7683. } else {
  7684. return std::forward_as_tuple(container.get(entt));
  7685. }
  7686. }
  7687. }
  7688. #endif
  7689. // #include "utility.hpp"
  7690. #ifndef ENTT_ENTITY_UTILITY_HPP
  7691. #define ENTT_ENTITY_UTILITY_HPP
  7692. // #include "../core/type_traits.hpp"
  7693. namespace entt {
  7694. /**
  7695. * @brief Alias for exclusion lists.
  7696. * @tparam Type List of types.
  7697. */
  7698. template<typename... Type>
  7699. struct exclude_t: type_list<Type...> {};
  7700. /**
  7701. * @brief Variable template for exclusion lists.
  7702. * @tparam Type List of types.
  7703. */
  7704. template<typename... Type>
  7705. inline constexpr exclude_t<Type...> exclude{};
  7706. /**
  7707. * @brief Alias for lists of observed components.
  7708. * @tparam Type List of types.
  7709. */
  7710. template<typename... Type>
  7711. struct get_t: type_list<Type...>{};
  7712. /**
  7713. * @brief Variable template for lists of observed components.
  7714. * @tparam Type List of types.
  7715. */
  7716. template<typename... Type>
  7717. inline constexpr get_t<Type...> get{};
  7718. }
  7719. #endif
  7720. namespace entt {
  7721. /**
  7722. * @brief Group.
  7723. *
  7724. * Primary template isn't defined on purpose. All the specializations give a
  7725. * compile-time error, but for a few reasonable cases.
  7726. */
  7727. template<typename...>
  7728. class basic_group;
  7729. /**
  7730. * @brief Non-owning group.
  7731. *
  7732. * A non-owning group returns all entities and only the entities that have at
  7733. * least the given components. Moreover, it's guaranteed that the entity list
  7734. * is tightly packed in memory for fast iterations.
  7735. *
  7736. * @b Important
  7737. *
  7738. * Iterators aren't invalidated if:
  7739. *
  7740. * * New instances of the given components are created and assigned to entities.
  7741. * * The entity currently pointed is modified (as an example, if one of the
  7742. * given components is removed from the entity to which the iterator points).
  7743. * * The entity currently pointed is destroyed.
  7744. *
  7745. * In all other cases, modifying the pools iterated by the group in any way
  7746. * invalidates all the iterators and using them results in undefined behavior.
  7747. *
  7748. * @note
  7749. * Groups share references to the underlying data structures of the registry
  7750. * that generated them. Therefore any change to the entities and to the
  7751. * components made by means of the registry are immediately reflected by all the
  7752. * groups.<br/>
  7753. * Moreover, sorting a non-owning group affects all the instances of the same
  7754. * group (it means that users don't have to call `sort` on each instance to sort
  7755. * all of them because they _share_ entities and components).
  7756. *
  7757. * @warning
  7758. * Lifetime of a group must not overcome that of the registry that generated it.
  7759. * In any other case, attempting to use a group results in undefined behavior.
  7760. *
  7761. * @tparam Entity A valid entity type (see entt_traits for more details).
  7762. * @tparam Exclude Types of components used to filter the group.
  7763. * @tparam Get Type of components observed by the group.
  7764. */
  7765. template<typename Entity, typename... Exclude, typename... Get>
  7766. class basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>> final {
  7767. /*! @brief A registry is allowed to create groups. */
  7768. friend class basic_registry<Entity>;
  7769. using basic_common_type = basic_sparse_set<Entity>;
  7770. template<typename Component>
  7771. using storage_type = constness_as_t<typename storage_traits<Entity, std::remove_const_t<Component>>::storage_type, Component>;
  7772. class iterable final {
  7773. template<typename It>
  7774. struct iterable_iterator final {
  7775. using difference_type = std::ptrdiff_t;
  7776. using value_type = decltype(std::tuple_cat(std::tuple<Entity>{}, std::declval<basic_group>().get({})));
  7777. using pointer = void;
  7778. using reference = value_type;
  7779. using iterator_category = std::input_iterator_tag;
  7780. template<typename... Args>
  7781. iterable_iterator(It from, const std::tuple<storage_type<Get> *...> &args) ENTT_NOEXCEPT
  7782. : it{from},
  7783. pools{args}
  7784. {}
  7785. iterable_iterator & operator++() ENTT_NOEXCEPT {
  7786. return ++it, *this;
  7787. }
  7788. iterable_iterator operator++(int) ENTT_NOEXCEPT {
  7789. iterable_iterator orig = *this;
  7790. return ++(*this), orig;
  7791. }
  7792. [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
  7793. const auto entt = *it;
  7794. return std::tuple_cat(std::make_tuple(entt), get_as_tuple(*std::get<storage_type<Get> *>(pools), entt)...);
  7795. }
  7796. [[nodiscard]] bool operator==(const iterable_iterator &other) const ENTT_NOEXCEPT {
  7797. return other.it == it;
  7798. }
  7799. [[nodiscard]] bool operator!=(const iterable_iterator &other) const ENTT_NOEXCEPT {
  7800. return !(*this == other);
  7801. }
  7802. private:
  7803. It it;
  7804. std::tuple<storage_type<Get> *...> pools;
  7805. };
  7806. public:
  7807. using iterator = iterable_iterator<typename basic_common_type::iterator>;
  7808. using reverse_iterator = iterable_iterator<typename basic_common_type::reverse_iterator>;
  7809. iterable(basic_common_type * const ref, const std::tuple<storage_type<Get> *...> &cpools)
  7810. : handler{ref},
  7811. pools{cpools}
  7812. {}
  7813. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  7814. return handler ? iterator{handler->begin(), pools} : iterator{{}, pools};
  7815. }
  7816. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  7817. return handler ? iterator{handler->end(), pools} : iterator{{}, pools};
  7818. }
  7819. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  7820. return handler ? reverse_iterator{handler->rbegin(), pools} : reverse_iterator{{}, pools};
  7821. }
  7822. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  7823. return handler ? reverse_iterator{handler->rend(), pools} : reverse_iterator{{}, pools};
  7824. }
  7825. private:
  7826. basic_common_type * const handler;
  7827. const std::tuple<storage_type<Get> *...> pools;
  7828. };
  7829. basic_group(basic_common_type &ref, storage_type<Get> &... gpool) ENTT_NOEXCEPT
  7830. : handler{&ref},
  7831. pools{&gpool...}
  7832. {}
  7833. public:
  7834. /*! @brief Underlying entity identifier. */
  7835. using entity_type = Entity;
  7836. /*! @brief Unsigned integer type. */
  7837. using size_type = std::size_t;
  7838. /*! @brief Random access iterator type. */
  7839. using iterator = typename basic_common_type::iterator;
  7840. /*! @brief Reversed iterator type. */
  7841. using reverse_iterator = typename basic_common_type::reverse_iterator;
  7842. /*! @brief Iterable group type. */
  7843. using iterable_group = iterable;
  7844. /*! @brief Default constructor to use to create empty, invalid groups. */
  7845. basic_group() ENTT_NOEXCEPT
  7846. : handler{}
  7847. {}
  7848. /**
  7849. * @brief Returns the number of entities that have the given components.
  7850. * @return Number of entities that have the given components.
  7851. */
  7852. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  7853. return *this ? handler->size() : size_type{};
  7854. }
  7855. /**
  7856. * @brief Returns the number of elements that a group has currently
  7857. * allocated space for.
  7858. * @return Capacity of the group.
  7859. */
  7860. [[nodiscard]] size_type capacity() const ENTT_NOEXCEPT {
  7861. return *this ? handler->capacity() : size_type{};
  7862. }
  7863. /*! @brief Requests the removal of unused capacity. */
  7864. void shrink_to_fit() {
  7865. if(*this) {
  7866. handler->shrink_to_fit();
  7867. }
  7868. }
  7869. /**
  7870. * @brief Checks whether a group is empty.
  7871. * @return True if the group is empty, false otherwise.
  7872. */
  7873. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  7874. return !*this || handler->empty();
  7875. }
  7876. /**
  7877. * @brief Direct access to the list of entities.
  7878. *
  7879. * The returned pointer is such that range `[data(), data() + size())` is
  7880. * always a valid range, even if the container is empty.
  7881. *
  7882. * @return A pointer to the array of entities.
  7883. */
  7884. [[nodiscard]] auto data() const ENTT_NOEXCEPT {
  7885. return *this ? handler->data() : nullptr;
  7886. }
  7887. /**
  7888. * @brief Returns an iterator to the first entity of the group.
  7889. *
  7890. * The returned iterator points to the first entity of the group. If the
  7891. * group is empty, the returned iterator will be equal to `end()`.
  7892. *
  7893. * @return An iterator to the first entity of the group.
  7894. */
  7895. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  7896. return *this ? handler->begin() : iterator{};
  7897. }
  7898. /**
  7899. * @brief Returns an iterator that is past the last entity of the group.
  7900. *
  7901. * The returned iterator points to the entity following the last entity of
  7902. * the group. Attempting to dereference the returned iterator results in
  7903. * undefined behavior.
  7904. *
  7905. * @return An iterator to the entity following the last entity of the
  7906. * group.
  7907. */
  7908. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  7909. return *this ? handler->end() : iterator{};
  7910. }
  7911. /**
  7912. * @brief Returns an iterator to the first entity of the reversed group.
  7913. *
  7914. * The returned iterator points to the first entity of the reversed group.
  7915. * If the group is empty, the returned iterator will be equal to `rend()`.
  7916. *
  7917. * @return An iterator to the first entity of the reversed group.
  7918. */
  7919. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  7920. return *this ? handler->rbegin() : reverse_iterator{};
  7921. }
  7922. /**
  7923. * @brief Returns an iterator that is past the last entity of the reversed
  7924. * group.
  7925. *
  7926. * The returned iterator points to the entity following the last entity of
  7927. * the reversed group. Attempting to dereference the returned iterator
  7928. * results in undefined behavior.
  7929. *
  7930. * @return An iterator to the entity following the last entity of the
  7931. * reversed group.
  7932. */
  7933. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  7934. return *this ? handler->rend() : reverse_iterator{};
  7935. }
  7936. /**
  7937. * @brief Returns the first entity of the group, if any.
  7938. * @return The first entity of the group if one exists, the null entity
  7939. * otherwise.
  7940. */
  7941. [[nodiscard]] entity_type front() const {
  7942. const auto it = begin();
  7943. return it != end() ? *it : null;
  7944. }
  7945. /**
  7946. * @brief Returns the last entity of the group, if any.
  7947. * @return The last entity of the group if one exists, the null entity
  7948. * otherwise.
  7949. */
  7950. [[nodiscard]] entity_type back() const {
  7951. const auto it = rbegin();
  7952. return it != rend() ? *it : null;
  7953. }
  7954. /**
  7955. * @brief Finds an entity.
  7956. * @param entt A valid entity identifier.
  7957. * @return An iterator to the given entity if it's found, past the end
  7958. * iterator otherwise.
  7959. */
  7960. [[nodiscard]] iterator find(const entity_type entt) const {
  7961. const auto it = *this ? handler->find(entt) : iterator{};
  7962. return it != end() && *it == entt ? it : end();
  7963. }
  7964. /**
  7965. * @brief Returns the identifier that occupies the given position.
  7966. * @param pos Position of the element to return.
  7967. * @return The identifier that occupies the given position.
  7968. */
  7969. [[nodiscard]] entity_type operator[](const size_type pos) const {
  7970. return begin()[pos];
  7971. }
  7972. /**
  7973. * @brief Checks if a group is properly initialized.
  7974. * @return True if the group is properly initialized, false otherwise.
  7975. */
  7976. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  7977. return handler != nullptr;
  7978. }
  7979. /**
  7980. * @brief Checks if a group contains an entity.
  7981. * @param entt A valid entity identifier.
  7982. * @return True if the group contains the given entity, false otherwise.
  7983. */
  7984. [[nodiscard]] bool contains(const entity_type entt) const {
  7985. return *this && handler->contains(entt);
  7986. }
  7987. /**
  7988. * @brief Returns the components assigned to the given entity.
  7989. *
  7990. * Prefer this function instead of `registry::get` during iterations. It has
  7991. * far better performance than its counterpart.
  7992. *
  7993. * @warning
  7994. * Attempting to use an invalid component type results in a compilation
  7995. * error. Attempting to use an entity that doesn't belong to the group
  7996. * results in undefined behavior.
  7997. *
  7998. * @tparam Component Types of components to get.
  7999. * @param entt A valid entity identifier.
  8000. * @return The components assigned to the entity.
  8001. */
  8002. template<typename... Component>
  8003. [[nodiscard]] decltype(auto) get(const entity_type entt) const {
  8004. ENTT_ASSERT(contains(entt), "Group does not contain entity");
  8005. if constexpr(sizeof...(Component) == 0) {
  8006. return std::tuple_cat(get_as_tuple(*std::get<storage_type<Get> *>(pools), entt)...);
  8007. } else if constexpr(sizeof...(Component) == 1) {
  8008. return (std::get<storage_type<Component> *>(pools)->get(entt), ...);
  8009. } else {
  8010. return std::tuple_cat(get_as_tuple(*std::get<storage_type<Component> *>(pools), entt)...);
  8011. }
  8012. }
  8013. /**
  8014. * @brief Iterates entities and components and applies the given function
  8015. * object to them.
  8016. *
  8017. * The function object is invoked for each entity. It is provided with the
  8018. * entity itself and a set of references to non-empty components. The
  8019. * _constness_ of the components is as requested.<br/>
  8020. * The signature of the function must be equivalent to one of the following
  8021. * forms:
  8022. *
  8023. * @code{.cpp}
  8024. * void(const entity_type, Type &...);
  8025. * void(Type &...);
  8026. * @endcode
  8027. *
  8028. * @note
  8029. * Empty types aren't explicitly instantiated and therefore they are never
  8030. * returned during iterations.
  8031. *
  8032. * @tparam Func Type of the function object to invoke.
  8033. * @param func A valid function object.
  8034. */
  8035. template<typename Func>
  8036. void each(Func func) const {
  8037. for(const auto entt: *this) {
  8038. if constexpr(is_applicable_v<Func, decltype(std::tuple_cat(std::tuple<entity_type>{}, std::declval<basic_group>().get({})))>) {
  8039. std::apply(func, std::tuple_cat(std::make_tuple(entt), get(entt)));
  8040. } else {
  8041. std::apply(func, get(entt));
  8042. }
  8043. }
  8044. }
  8045. /**
  8046. * @brief Returns an iterable object to use to _visit_ the group.
  8047. *
  8048. * The iterable object returns tuples that contain the current entity and a
  8049. * set of references to its non-empty components. The _constness_ of the
  8050. * components is as requested.
  8051. *
  8052. * @note
  8053. * Empty types aren't explicitly instantiated and therefore they are never
  8054. * returned during iterations.
  8055. *
  8056. * @return An iterable object to use to _visit_ the group.
  8057. */
  8058. [[nodiscard]] iterable_group each() const ENTT_NOEXCEPT {
  8059. return iterable_group{handler, pools};
  8060. }
  8061. /**
  8062. * @brief Sort a group according to the given comparison function.
  8063. *
  8064. * Sort the group so that iterating it with a couple of iterators returns
  8065. * entities and components in the expected order. See `begin` and `end` for
  8066. * more details.
  8067. *
  8068. * The comparison function object must return `true` if the first element
  8069. * is _less_ than the second one, `false` otherwise. The signature of the
  8070. * comparison function should be equivalent to one of the following:
  8071. *
  8072. * @code{.cpp}
  8073. * bool(std::tuple<Component &...>, std::tuple<Component &...>);
  8074. * bool(const Component &..., const Component &...);
  8075. * bool(const Entity, const Entity);
  8076. * @endcode
  8077. *
  8078. * Where `Component` are such that they are iterated by the group.<br/>
  8079. * Moreover, the comparison function object shall induce a
  8080. * _strict weak ordering_ on the values.
  8081. *
  8082. * The sort function oject must offer a member function template
  8083. * `operator()` that accepts three arguments:
  8084. *
  8085. * * An iterator to the first element of the range to sort.
  8086. * * An iterator past the last element of the range to sort.
  8087. * * A comparison function to use to compare the elements.
  8088. *
  8089. * @tparam Component Optional types of components to compare.
  8090. * @tparam Compare Type of comparison function object.
  8091. * @tparam Sort Type of sort function object.
  8092. * @tparam Args Types of arguments to forward to the sort function object.
  8093. * @param compare A valid comparison function object.
  8094. * @param algo A valid sort function object.
  8095. * @param args Arguments to forward to the sort function object, if any.
  8096. */
  8097. template<typename... Component, typename Compare, typename Sort = std_sort, typename... Args>
  8098. void sort(Compare compare, Sort algo = Sort{}, Args &&... args) {
  8099. if(*this) {
  8100. if constexpr(sizeof...(Component) == 0) {
  8101. static_assert(std::is_invocable_v<Compare, const entity_type, const entity_type>, "Invalid comparison function");
  8102. handler->sort(std::move(compare), std::move(algo), std::forward<Args>(args)...);
  8103. } else if constexpr(sizeof...(Component) == 1) {
  8104. handler->sort([this, compare = std::move(compare)](const entity_type lhs, const entity_type rhs) {
  8105. return compare((std::get<storage_type<Component> *>(pools)->get(lhs), ...), (std::get<storage_type<Component> *>(pools)->get(rhs), ...));
  8106. }, std::move(algo), std::forward<Args>(args)...);
  8107. } else {
  8108. handler->sort([this, compare = std::move(compare)](const entity_type lhs, const entity_type rhs) {
  8109. return compare(std::forward_as_tuple(std::get<storage_type<Component> *>(pools)->get(lhs)...), std::forward_as_tuple(std::get<storage_type<Component> *>(pools)->get(rhs)...));
  8110. }, std::move(algo), std::forward<Args>(args)...);
  8111. }
  8112. }
  8113. }
  8114. /**
  8115. * @brief Sort the shared pool of entities according to the given component.
  8116. *
  8117. * Non-owning groups of the same type share with the registry a pool of
  8118. * entities with its own order that doesn't depend on the order of any pool
  8119. * of components. Users can order the underlying data structure so that it
  8120. * respects the order of the pool of the given component.
  8121. *
  8122. * @note
  8123. * The shared pool of entities and thus its order is affected by the changes
  8124. * to each and every pool that it tracks. Therefore changes to those pools
  8125. * can quickly ruin the order imposed to the pool of entities shared between
  8126. * the non-owning groups.
  8127. *
  8128. * @tparam Component Type of component to use to impose the order.
  8129. */
  8130. template<typename Component>
  8131. void sort() const {
  8132. if(*this) {
  8133. handler->respect(*std::get<storage_type<Component> *>(pools));
  8134. }
  8135. }
  8136. private:
  8137. basic_common_type * const handler;
  8138. const std::tuple<storage_type<Get> *...> pools;
  8139. };
  8140. /**
  8141. * @brief Owning group.
  8142. *
  8143. * Owning groups return all entities and only the entities that have at least
  8144. * the given components. Moreover:
  8145. *
  8146. * * It's guaranteed that the entity list is tightly packed in memory for fast
  8147. * iterations.
  8148. * * It's guaranteed that the lists of owned components are tightly packed in
  8149. * memory for even faster iterations and to allow direct access.
  8150. * * They stay true to the order of the owned components and all instances have
  8151. * the same order in memory.
  8152. *
  8153. * The more types of components are owned by a group, the faster it is to
  8154. * iterate them.
  8155. *
  8156. * @b Important
  8157. *
  8158. * Iterators aren't invalidated if:
  8159. *
  8160. * * New instances of the given components are created and assigned to entities.
  8161. * * The entity currently pointed is modified (as an example, if one of the
  8162. * given components is removed from the entity to which the iterator points).
  8163. * * The entity currently pointed is destroyed.
  8164. *
  8165. * In all other cases, modifying the pools iterated by the group in any way
  8166. * invalidates all the iterators and using them results in undefined behavior.
  8167. *
  8168. * @note
  8169. * Groups share references to the underlying data structures of the registry
  8170. * that generated them. Therefore any change to the entities and to the
  8171. * components made by means of the registry are immediately reflected by all the
  8172. * groups.
  8173. * Moreover, sorting an owning group affects all the instance of the same group
  8174. * (it means that users don't have to call `sort` on each instance to sort all
  8175. * of them because they share the underlying data structure).
  8176. *
  8177. * @warning
  8178. * Lifetime of a group must not overcome that of the registry that generated it.
  8179. * In any other case, attempting to use a group results in undefined behavior.
  8180. *
  8181. * @tparam Entity A valid entity type (see entt_traits for more details).
  8182. * @tparam Exclude Types of components used to filter the group.
  8183. * @tparam Get Types of components observed by the group.
  8184. * @tparam Owned Types of components owned by the group.
  8185. */
  8186. template<typename Entity, typename... Exclude, typename... Get, typename... Owned>
  8187. class basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>, Owned...> final {
  8188. /*! @brief A registry is allowed to create groups. */
  8189. friend class basic_registry<Entity>;
  8190. using basic_common_type = basic_sparse_set<Entity>;
  8191. template<typename Component>
  8192. using storage_type = constness_as_t<typename storage_traits<Entity, std::remove_const_t<Component>>::storage_type, Component>;
  8193. class iterable final {
  8194. template<typename, typename>
  8195. struct iterable_iterator;
  8196. template<typename It, typename... OIt>
  8197. struct iterable_iterator<It, type_list<OIt...>> final {
  8198. using difference_type = std::ptrdiff_t;
  8199. using value_type = decltype(std::tuple_cat(std::tuple<Entity>{}, std::declval<basic_group>().get({})));
  8200. using pointer = void;
  8201. using reference = value_type;
  8202. using iterator_category = std::input_iterator_tag;
  8203. template<typename... Other>
  8204. iterable_iterator(It from, const std::tuple<Other...> &other, const std::tuple<storage_type<Get> *...> &cpools) ENTT_NOEXCEPT
  8205. : it{from},
  8206. owned{std::get<OIt>(other)...},
  8207. get{cpools}
  8208. {}
  8209. iterable_iterator & operator++() ENTT_NOEXCEPT {
  8210. return ++it, (++std::get<OIt>(owned), ...), *this;
  8211. }
  8212. iterable_iterator operator++(int) ENTT_NOEXCEPT {
  8213. iterable_iterator orig = *this;
  8214. return ++(*this), orig;
  8215. }
  8216. [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
  8217. return std::tuple_cat(
  8218. std::make_tuple(*it),
  8219. std::forward_as_tuple(*std::get<OIt>(owned)...),
  8220. get_as_tuple(*std::get<storage_type<Get> *>(get), *it)...
  8221. );
  8222. }
  8223. [[nodiscard]] bool operator==(const iterable_iterator &other) const ENTT_NOEXCEPT {
  8224. return other.it == it;
  8225. }
  8226. [[nodiscard]] bool operator!=(const iterable_iterator &other) const ENTT_NOEXCEPT {
  8227. return !(*this == other);
  8228. }
  8229. private:
  8230. It it;
  8231. std::tuple<OIt...> owned;
  8232. std::tuple<storage_type<Get> *...> get;
  8233. };
  8234. public:
  8235. using iterator = iterable_iterator<
  8236. typename basic_common_type::iterator,
  8237. type_list_cat_t<std::conditional_t<std::is_void_v<decltype(std::declval<storage_type<Owned>>().get({}))>, type_list<>, type_list<decltype(std::declval<storage_type<Owned>>().end())>>...>
  8238. >;
  8239. using reverse_iterator = iterable_iterator<
  8240. typename basic_common_type::reverse_iterator,
  8241. type_list_cat_t<std::conditional_t<std::is_void_v<decltype(std::declval<storage_type<Owned>>().get({}))>, type_list<>, type_list<decltype(std::declval<storage_type<Owned>>().rbegin())>>...>
  8242. >;
  8243. iterable(std::tuple<storage_type<Owned> *..., storage_type<Get> *...> cpools, const std::size_t * const extent)
  8244. : pools{cpools},
  8245. length{extent}
  8246. {}
  8247. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  8248. return length ? iterator{
  8249. std::get<0>(pools)->basic_common_type::end() - *length,
  8250. std::make_tuple((std::get<storage_type<Owned> *>(pools)->end() - *length)...),
  8251. std::make_tuple(std::get<storage_type<Get> *>(pools)...)
  8252. } : iterator{{}, std::make_tuple(decltype(std::get<storage_type<Owned> *>(pools)->end()){}...), std::make_tuple(std::get<storage_type<Get> *>(pools)...)};
  8253. }
  8254. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  8255. return length ? iterator{
  8256. std::get<0>(pools)->basic_common_type::end(),
  8257. std::make_tuple((std::get<storage_type<Owned> *>(pools)->end())...),
  8258. std::make_tuple(std::get<storage_type<Get> *>(pools)...)
  8259. } : iterator{{}, std::make_tuple(decltype(std::get<storage_type<Owned> *>(pools)->end()){}...), std::make_tuple(std::get<storage_type<Get> *>(pools)...)};
  8260. }
  8261. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  8262. return length ? reverse_iterator{
  8263. std::get<0>(pools)->basic_common_type::rbegin(),
  8264. std::make_tuple((std::get<storage_type<Owned> *>(pools)->rbegin())...),
  8265. std::make_tuple(std::get<storage_type<Get> *>(pools)...)
  8266. } : reverse_iterator{{}, std::make_tuple(decltype(std::get<storage_type<Owned> *>(pools)->rbegin()){}...), std::make_tuple(std::get<storage_type<Get> *>(pools)...)};
  8267. }
  8268. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  8269. return length ? reverse_iterator{
  8270. std::get<0>(pools)->basic_common_type::rbegin() + *length,
  8271. std::make_tuple((std::get<storage_type<Owned> *>(pools)->rbegin() + *length)...),
  8272. std::make_tuple(std::get<storage_type<Get> *>(pools)...)
  8273. } : reverse_iterator{{}, std::make_tuple(decltype(std::get<storage_type<Owned> *>(pools)->rbegin()){}...), std::make_tuple(std::get<storage_type<Get> *>(pools)...)};
  8274. }
  8275. private:
  8276. const std::tuple<storage_type<Owned> *..., storage_type<Get> *...> pools;
  8277. const std::size_t * const length;
  8278. };
  8279. basic_group(const std::size_t &extent, storage_type<Owned> &... opool, storage_type<Get> &... gpool) ENTT_NOEXCEPT
  8280. : pools{&opool..., &gpool...},
  8281. length{&extent}
  8282. {}
  8283. public:
  8284. /*! @brief Underlying entity identifier. */
  8285. using entity_type = Entity;
  8286. /*! @brief Unsigned integer type. */
  8287. using size_type = std::size_t;
  8288. /*! @brief Random access iterator type. */
  8289. using iterator = typename basic_common_type::iterator;
  8290. /*! @brief Reversed iterator type. */
  8291. using reverse_iterator = typename basic_common_type::reverse_iterator;
  8292. /*! @brief Iterable group type. */
  8293. using iterable_group = iterable;
  8294. /*! @brief Default constructor to use to create empty, invalid groups. */
  8295. basic_group() ENTT_NOEXCEPT
  8296. : length{}
  8297. {}
  8298. /**
  8299. * @brief Returns the number of entities that have the given components.
  8300. * @return Number of entities that have the given components.
  8301. */
  8302. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  8303. return *this ? *length : size_type{};
  8304. }
  8305. /**
  8306. * @brief Checks whether a group is empty.
  8307. * @return True if the group is empty, false otherwise.
  8308. */
  8309. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  8310. return !*this || !*length;
  8311. }
  8312. /**
  8313. * @brief Direct access to the raw representation offered by the storage.
  8314. *
  8315. * @warning
  8316. * This function is only available for owned types.
  8317. *
  8318. * @tparam Component Type of component in which one is interested.
  8319. * @return A pointer to the array of components.
  8320. */
  8321. template<typename Component>
  8322. [[nodiscard]] auto raw() const ENTT_NOEXCEPT {
  8323. static_assert((std::is_same_v<Component, Owned> || ...), "Non-owned type");
  8324. auto *cpool = std::get<storage_type<Component> *>(pools);
  8325. return cpool ? cpool->raw() : decltype(cpool->raw()){};
  8326. }
  8327. /**
  8328. * @brief Direct access to the list of entities.
  8329. *
  8330. * The returned pointer is such that range `[data(), data() + size())` is
  8331. * always a valid range, even if the container is empty.
  8332. *
  8333. * @return A pointer to the array of entities.
  8334. */
  8335. [[nodiscard]] auto data() const ENTT_NOEXCEPT {
  8336. return *this ? std::get<0>(pools)->data() : nullptr;
  8337. }
  8338. /**
  8339. * @brief Returns an iterator to the first entity of the group.
  8340. *
  8341. * The returned iterator points to the first entity of the group. If the
  8342. * group is empty, the returned iterator will be equal to `end()`.
  8343. *
  8344. * @return An iterator to the first entity of the group.
  8345. */
  8346. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  8347. return *this ? (std::get<0>(pools)->basic_common_type::end() - *length) : iterator{};
  8348. }
  8349. /**
  8350. * @brief Returns an iterator that is past the last entity of the group.
  8351. *
  8352. * The returned iterator points to the entity following the last entity of
  8353. * the group. Attempting to dereference the returned iterator results in
  8354. * undefined behavior.
  8355. *
  8356. * @return An iterator to the entity following the last entity of the
  8357. * group.
  8358. */
  8359. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  8360. return *this ? std::get<0>(pools)->basic_common_type::end() : iterator{};
  8361. }
  8362. /**
  8363. * @brief Returns an iterator to the first entity of the reversed group.
  8364. *
  8365. * The returned iterator points to the first entity of the reversed group.
  8366. * If the group is empty, the returned iterator will be equal to `rend()`.
  8367. *
  8368. * @return An iterator to the first entity of the reversed group.
  8369. */
  8370. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  8371. return *this ? std::get<0>(pools)->basic_common_type::rbegin() : reverse_iterator{};
  8372. }
  8373. /**
  8374. * @brief Returns an iterator that is past the last entity of the reversed
  8375. * group.
  8376. *
  8377. * The returned iterator points to the entity following the last entity of
  8378. * the reversed group. Attempting to dereference the returned iterator
  8379. * results in undefined behavior.
  8380. *
  8381. * @return An iterator to the entity following the last entity of the
  8382. * reversed group.
  8383. */
  8384. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  8385. return *this ? (std::get<0>(pools)->basic_common_type::rbegin() + *length) : reverse_iterator{};
  8386. }
  8387. /**
  8388. * @brief Returns the first entity of the group, if any.
  8389. * @return The first entity of the group if one exists, the null entity
  8390. * otherwise.
  8391. */
  8392. [[nodiscard]] entity_type front() const {
  8393. const auto it = begin();
  8394. return it != end() ? *it : null;
  8395. }
  8396. /**
  8397. * @brief Returns the last entity of the group, if any.
  8398. * @return The last entity of the group if one exists, the null entity
  8399. * otherwise.
  8400. */
  8401. [[nodiscard]] entity_type back() const {
  8402. const auto it = rbegin();
  8403. return it != rend() ? *it : null;
  8404. }
  8405. /**
  8406. * @brief Finds an entity.
  8407. * @param entt A valid entity identifier.
  8408. * @return An iterator to the given entity if it's found, past the end
  8409. * iterator otherwise.
  8410. */
  8411. [[nodiscard]] iterator find(const entity_type entt) const {
  8412. const auto it = *this ? std::get<0>(pools)->find(entt) : iterator{};
  8413. return it != end() && it >= begin() && *it == entt ? it : end();
  8414. }
  8415. /**
  8416. * @brief Returns the identifier that occupies the given position.
  8417. * @param pos Position of the element to return.
  8418. * @return The identifier that occupies the given position.
  8419. */
  8420. [[nodiscard]] entity_type operator[](const size_type pos) const {
  8421. return begin()[pos];
  8422. }
  8423. /**
  8424. * @brief Checks if a group is properly initialized.
  8425. * @return True if the group is properly initialized, false otherwise.
  8426. */
  8427. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  8428. return length != nullptr;
  8429. }
  8430. /**
  8431. * @brief Checks if a group contains an entity.
  8432. * @param entt A valid entity identifier.
  8433. * @return True if the group contains the given entity, false otherwise.
  8434. */
  8435. [[nodiscard]] bool contains(const entity_type entt) const {
  8436. return *this && std::get<0>(pools)->contains(entt) && (std::get<0>(pools)->index(entt) < (*length));
  8437. }
  8438. /**
  8439. * @brief Returns the components assigned to the given entity.
  8440. *
  8441. * Prefer this function instead of `registry::get` during iterations. It has
  8442. * far better performance than its counterpart.
  8443. *
  8444. * @warning
  8445. * Attempting to use an invalid component type results in a compilation
  8446. * error. Attempting to use an entity that doesn't belong to the group
  8447. * results in undefined behavior.
  8448. *
  8449. * @tparam Component Types of components to get.
  8450. * @param entt A valid entity identifier.
  8451. * @return The components assigned to the entity.
  8452. */
  8453. template<typename... Component>
  8454. [[nodiscard]] decltype(auto) get(const entity_type entt) const {
  8455. ENTT_ASSERT(contains(entt), "Group does not contain entity");
  8456. if constexpr(sizeof...(Component) == 0) {
  8457. return std::tuple_cat(get_as_tuple(*std::get<storage_type<Owned> *>(pools), entt)..., get_as_tuple(*std::get<storage_type<Get> *>(pools), entt)...);
  8458. } else if constexpr(sizeof...(Component) == 1) {
  8459. return (std::get<storage_type<Component> *>(pools)->get(entt), ...);
  8460. } else {
  8461. return std::tuple_cat(get_as_tuple(*std::get<storage_type<Component> *>(pools), entt)...);
  8462. }
  8463. }
  8464. /**
  8465. * @brief Iterates entities and components and applies the given function
  8466. * object to them.
  8467. *
  8468. * The function object is invoked for each entity. It is provided with the
  8469. * entity itself and a set of references to non-empty components. The
  8470. * _constness_ of the components is as requested.<br/>
  8471. * The signature of the function must be equivalent to one of the following
  8472. * forms:
  8473. *
  8474. * @code{.cpp}
  8475. * void(const entity_type, Type &...);
  8476. * void(Type &...);
  8477. * @endcode
  8478. *
  8479. * @note
  8480. * Empty types aren't explicitly instantiated and therefore they are never
  8481. * returned during iterations.
  8482. *
  8483. * @tparam Func Type of the function object to invoke.
  8484. * @param func A valid function object.
  8485. */
  8486. template<typename Func>
  8487. void each(Func func) const {
  8488. for(auto args: each()) {
  8489. if constexpr(is_applicable_v<Func, decltype(std::tuple_cat(std::tuple<entity_type>{}, std::declval<basic_group>().get({})))>) {
  8490. std::apply(func, args);
  8491. } else {
  8492. std::apply([&func](auto, auto &&... less) { func(std::forward<decltype(less)>(less)...); }, args);
  8493. }
  8494. }
  8495. }
  8496. /**
  8497. * @brief Returns an iterable object to use to _visit_ the group.
  8498. *
  8499. * The iterable object returns tuples that contain the current entity and a
  8500. * set of references to its non-empty components. The _constness_ of the
  8501. * components is as requested.
  8502. *
  8503. * @note
  8504. * Empty types aren't explicitly instantiated and therefore they are never
  8505. * returned during iterations.
  8506. *
  8507. * @return An iterable object to use to _visit_ the group.
  8508. */
  8509. [[nodiscard]] iterable_group each() const ENTT_NOEXCEPT {
  8510. return iterable_group{pools, length};
  8511. }
  8512. /**
  8513. * @brief Sort a group according to the given comparison function.
  8514. *
  8515. * Sort the group so that iterating it with a couple of iterators returns
  8516. * entities and components in the expected order. See `begin` and `end` for
  8517. * more details.
  8518. *
  8519. * The comparison function object must return `true` if the first element
  8520. * is _less_ than the second one, `false` otherwise. The signature of the
  8521. * comparison function should be equivalent to one of the following:
  8522. *
  8523. * @code{.cpp}
  8524. * bool(std::tuple<Component &...>, std::tuple<Component &...>);
  8525. * bool(const Component &, const Component &);
  8526. * bool(const Entity, const Entity);
  8527. * @endcode
  8528. *
  8529. * Where `Component` are either owned types or not but still such that they
  8530. * are iterated by the group.<br/>
  8531. * Moreover, the comparison function object shall induce a
  8532. * _strict weak ordering_ on the values.
  8533. *
  8534. * The sort function oject must offer a member function template
  8535. * `operator()` that accepts three arguments:
  8536. *
  8537. * * An iterator to the first element of the range to sort.
  8538. * * An iterator past the last element of the range to sort.
  8539. * * A comparison function to use to compare the elements.
  8540. *
  8541. * @tparam Component Optional types of components to compare.
  8542. * @tparam Compare Type of comparison function object.
  8543. * @tparam Sort Type of sort function object.
  8544. * @tparam Args Types of arguments to forward to the sort function object.
  8545. * @param compare A valid comparison function object.
  8546. * @param algo A valid sort function object.
  8547. * @param args Arguments to forward to the sort function object, if any.
  8548. */
  8549. template<typename... Component, typename Compare, typename Sort = std_sort, typename... Args>
  8550. void sort(Compare compare, Sort algo = Sort{}, Args &&... args) const {
  8551. auto *cpool = std::get<0>(pools);
  8552. if constexpr(sizeof...(Component) == 0) {
  8553. static_assert(std::is_invocable_v<Compare, const entity_type, const entity_type>, "Invalid comparison function");
  8554. cpool->sort_n(*length, std::move(compare), std::move(algo), std::forward<Args>(args)...);
  8555. } else if constexpr(sizeof...(Component) == 1) {
  8556. cpool->sort_n(*length, [this, compare = std::move(compare)](const entity_type lhs, const entity_type rhs) {
  8557. return compare((std::get<storage_type<Component> *>(pools)->get(lhs), ...), (std::get<storage_type<Component> *>(pools)->get(rhs), ...));
  8558. }, std::move(algo), std::forward<Args>(args)...);
  8559. } else {
  8560. cpool->sort_n(*length, [this, compare = std::move(compare)](const entity_type lhs, const entity_type rhs) {
  8561. return compare(std::forward_as_tuple(std::get<storage_type<Component> *>(pools)->get(lhs)...), std::forward_as_tuple(std::get<storage_type<Component> *>(pools)->get(rhs)...));
  8562. }, std::move(algo), std::forward<Args>(args)...);
  8563. }
  8564. [this](auto *head, auto *... other) {
  8565. for(auto next = *length; next; --next) {
  8566. const auto pos = next - 1;
  8567. [[maybe_unused]] const auto entt = head->data()[pos];
  8568. (other->swap(other->data()[pos], entt), ...);
  8569. }
  8570. }(std::get<storage_type<Owned> *>(pools)...);
  8571. }
  8572. private:
  8573. const std::tuple<storage_type<Owned> *..., storage_type<Get> *...> pools;
  8574. const size_type * const length;
  8575. };
  8576. }
  8577. #endif
  8578. // #include "entity/handle.hpp"
  8579. #ifndef ENTT_ENTITY_HANDLE_HPP
  8580. #define ENTT_ENTITY_HANDLE_HPP
  8581. #include <tuple>
  8582. #include <type_traits>
  8583. #include <utility>
  8584. // #include "../config/config.h"
  8585. // #include "../core/type_traits.hpp"
  8586. // #include "fwd.hpp"
  8587. // #include "registry.hpp"
  8588. #ifndef ENTT_ENTITY_REGISTRY_HPP
  8589. #define ENTT_ENTITY_REGISTRY_HPP
  8590. #include <algorithm>
  8591. #include <cstddef>
  8592. #include <iterator>
  8593. #include <memory>
  8594. #include <tuple>
  8595. #include <type_traits>
  8596. #include <utility>
  8597. #include <vector>
  8598. // #include "../config/config.h"
  8599. // #include "../core/algorithm.hpp"
  8600. // #include "../core/any.hpp"
  8601. #ifndef ENTT_CORE_ANY_HPP
  8602. #define ENTT_CORE_ANY_HPP
  8603. #include <cstddef>
  8604. #include <functional>
  8605. #include <memory>
  8606. #include <type_traits>
  8607. #include <utility>
  8608. // #include "../config/config.h"
  8609. // #include "../core/utility.hpp"
  8610. #ifndef ENTT_CORE_UTILITY_HPP
  8611. #define ENTT_CORE_UTILITY_HPP
  8612. #include <utility>
  8613. // #include "../config/config.h"
  8614. #ifndef ENTT_CONFIG_CONFIG_H
  8615. #define ENTT_CONFIG_CONFIG_H
  8616. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  8617. # define ENTT_NOEXCEPT noexcept
  8618. # define ENTT_THROW throw
  8619. # define ENTT_TRY try
  8620. # define ENTT_CATCH catch(...)
  8621. #else
  8622. # define ENTT_NOEXCEPT
  8623. # define ENTT_THROW
  8624. # define ENTT_TRY if(true)
  8625. # define ENTT_CATCH if(false)
  8626. #endif
  8627. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  8628. # include <new>
  8629. # define ENTT_LAUNDER(expr) std::launder(expr)
  8630. #else
  8631. # define ENTT_LAUNDER(expr) expr
  8632. #endif
  8633. #ifndef ENTT_USE_ATOMIC
  8634. # define ENTT_MAYBE_ATOMIC(Type) Type
  8635. #else
  8636. # include <atomic>
  8637. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  8638. #endif
  8639. #ifndef ENTT_ID_TYPE
  8640. # include <cstdint>
  8641. # define ENTT_ID_TYPE std::uint32_t
  8642. #endif
  8643. #ifdef ENTT_SPARSE_PAGE
  8644. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  8645. #else
  8646. # define ENTT_SPARSE_PAGE 4096
  8647. #endif
  8648. #ifdef ENTT_PACKED_PAGE
  8649. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  8650. #else
  8651. # define ENTT_PACKED_PAGE 1024
  8652. #endif
  8653. #ifdef ENTT_DISABLE_ASSERT
  8654. # undef ENTT_ASSERT
  8655. # define ENTT_ASSERT(...) (void(0))
  8656. #elif !defined ENTT_ASSERT
  8657. # include <cassert>
  8658. # define ENTT_ASSERT(condition, ...) assert(condition)
  8659. #endif
  8660. #ifdef ENTT_NO_ETO
  8661. # include <type_traits>
  8662. # define ENTT_IGNORE_IF_EMPTY std::false_type
  8663. #else
  8664. # include <type_traits>
  8665. # define ENTT_IGNORE_IF_EMPTY std::true_type
  8666. #endif
  8667. #ifndef ENTT_STANDARD_CPP
  8668. # if defined __clang__ || defined __GNUC__
  8669. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  8670. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  8671. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  8672. # elif defined _MSC_VER
  8673. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  8674. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  8675. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  8676. # endif
  8677. #endif
  8678. #endif
  8679. namespace entt {
  8680. /*! @brief Identity function object (waiting for C++20). */
  8681. struct identity {
  8682. /**
  8683. * @brief Returns its argument unchanged.
  8684. * @tparam Type Type of the argument.
  8685. * @param value The actual argument.
  8686. * @return The submitted value as-is.
  8687. */
  8688. template<class Type>
  8689. [[nodiscard]] constexpr Type && operator()(Type &&value) const ENTT_NOEXCEPT {
  8690. return std::forward<Type>(value);
  8691. }
  8692. };
  8693. /**
  8694. * @brief Constant utility to disambiguate overloaded members of a class.
  8695. * @tparam Type Type of the desired overload.
  8696. * @tparam Class Type of class to which the member belongs.
  8697. * @param member A valid pointer to a member.
  8698. * @return Pointer to the member.
  8699. */
  8700. template<typename Type, typename Class>
  8701. [[nodiscard]] constexpr auto overload(Type Class:: *member) ENTT_NOEXCEPT { return member; }
  8702. /**
  8703. * @brief Constant utility to disambiguate overloaded functions.
  8704. * @tparam Func Function type of the desired overload.
  8705. * @param func A valid pointer to a function.
  8706. * @return Pointer to the function.
  8707. */
  8708. template<typename Func>
  8709. [[nodiscard]] constexpr auto overload(Func *func) ENTT_NOEXCEPT { return func; }
  8710. /**
  8711. * @brief Helper type for visitors.
  8712. * @tparam Func Types of function objects.
  8713. */
  8714. template<class... Func>
  8715. struct overloaded: Func... {
  8716. using Func::operator()...;
  8717. };
  8718. /**
  8719. * @brief Deduction guide.
  8720. * @tparam Func Types of function objects.
  8721. */
  8722. template<class... Func>
  8723. overloaded(Func...)
  8724. -> overloaded<Func...>;
  8725. /**
  8726. * @brief Basic implementation of a y-combinator.
  8727. * @tparam Func Type of a potentially recursive function.
  8728. */
  8729. template<class Func>
  8730. struct y_combinator {
  8731. /**
  8732. * @brief Constructs a y-combinator from a given function.
  8733. * @param recursive A potentially recursive function.
  8734. */
  8735. y_combinator(Func recursive):
  8736. func{std::move(recursive)}
  8737. {}
  8738. /**
  8739. * @brief Invokes a y-combinator and therefore its underlying function.
  8740. * @tparam Args Types of arguments to use to invoke the underlying function.
  8741. * @param args Parameters to use to invoke the underlying function.
  8742. * @return Return value of the underlying function, if any.
  8743. */
  8744. template <class... Args>
  8745. decltype(auto) operator()(Args &&... args) const {
  8746. return func(*this, std::forward<Args>(args)...);
  8747. }
  8748. /*! @copydoc operator()() */
  8749. template <class... Args>
  8750. decltype(auto) operator()(Args &&... args) {
  8751. return func(*this, std::forward<Args>(args)...);
  8752. }
  8753. private:
  8754. Func func;
  8755. };
  8756. }
  8757. #endif
  8758. // #include "fwd.hpp"
  8759. // #include "type_info.hpp"
  8760. #ifndef ENTT_CORE_TYPE_INFO_HPP
  8761. #define ENTT_CORE_TYPE_INFO_HPP
  8762. #include <string_view>
  8763. #include <type_traits>
  8764. // #include "../config/config.h"
  8765. // #include "../core/attribute.h"
  8766. #ifndef ENTT_CORE_ATTRIBUTE_H
  8767. #define ENTT_CORE_ATTRIBUTE_H
  8768. #ifndef ENTT_EXPORT
  8769. # if defined _WIN32 || defined __CYGWIN__ || defined _MSC_VER
  8770. # define ENTT_EXPORT __declspec(dllexport)
  8771. # define ENTT_IMPORT __declspec(dllimport)
  8772. # define ENTT_HIDDEN
  8773. # elif defined __GNUC__ && __GNUC__ >= 4
  8774. # define ENTT_EXPORT __attribute__((visibility("default")))
  8775. # define ENTT_IMPORT __attribute__((visibility("default")))
  8776. # define ENTT_HIDDEN __attribute__((visibility("hidden")))
  8777. # else /* Unsupported compiler */
  8778. # define ENTT_EXPORT
  8779. # define ENTT_IMPORT
  8780. # define ENTT_HIDDEN
  8781. # endif
  8782. #endif
  8783. #ifndef ENTT_API
  8784. # if defined ENTT_API_EXPORT
  8785. # define ENTT_API ENTT_EXPORT
  8786. # elif defined ENTT_API_IMPORT
  8787. # define ENTT_API ENTT_IMPORT
  8788. # else /* No API */
  8789. # define ENTT_API
  8790. # endif
  8791. #endif
  8792. #endif
  8793. // #include "hashed_string.hpp"
  8794. #ifndef ENTT_CORE_HASHED_STRING_HPP
  8795. #define ENTT_CORE_HASHED_STRING_HPP
  8796. #include <cstddef>
  8797. #include <cstdint>
  8798. // #include "../config/config.h"
  8799. // #include "fwd.hpp"
  8800. namespace entt {
  8801. /**
  8802. * @cond TURN_OFF_DOXYGEN
  8803. * Internal details not to be documented.
  8804. */
  8805. namespace internal {
  8806. template<typename>
  8807. struct fnv1a_traits;
  8808. template<>
  8809. struct fnv1a_traits<std::uint32_t> {
  8810. using type = std::uint32_t;
  8811. static constexpr std::uint32_t offset = 2166136261;
  8812. static constexpr std::uint32_t prime = 16777619;
  8813. };
  8814. template<>
  8815. struct fnv1a_traits<std::uint64_t> {
  8816. using type = std::uint64_t;
  8817. static constexpr std::uint64_t offset = 14695981039346656037ull;
  8818. static constexpr std::uint64_t prime = 1099511628211ull;
  8819. };
  8820. }
  8821. /**
  8822. * Internal details not to be documented.
  8823. * @endcond
  8824. */
  8825. /**
  8826. * @brief Zero overhead unique identifier.
  8827. *
  8828. * A hashed string is a compile-time tool that allows users to use
  8829. * human-readable identifers in the codebase while using their numeric
  8830. * counterparts at runtime.<br/>
  8831. * Because of that, a hashed string can also be used in constant expressions if
  8832. * required.
  8833. *
  8834. * @tparam Char Character type.
  8835. */
  8836. template<typename Char>
  8837. class basic_hashed_string {
  8838. using traits_type = internal::fnv1a_traits<id_type>;
  8839. struct const_wrapper {
  8840. // non-explicit constructor on purpose
  8841. constexpr const_wrapper(const Char *curr) ENTT_NOEXCEPT: str{curr} {}
  8842. const Char *str;
  8843. };
  8844. // Fowler–Noll–Vo hash function v. 1a - the good
  8845. [[nodiscard]] static constexpr id_type helper(const Char *curr) ENTT_NOEXCEPT {
  8846. auto value = traits_type::offset;
  8847. while(*curr != 0) {
  8848. value = (value ^ static_cast<traits_type::type>(*(curr++))) * traits_type::prime;
  8849. }
  8850. return value;
  8851. }
  8852. public:
  8853. /*! @brief Character type. */
  8854. using value_type = Char;
  8855. /*! @brief Unsigned integer type. */
  8856. using hash_type = id_type;
  8857. /**
  8858. * @brief Returns directly the numeric representation of a string view.
  8859. * @param str Human-readable identifer.
  8860. * @param size Length of the string to hash.
  8861. * @return The numeric representation of the string.
  8862. */
  8863. [[nodiscard]] static constexpr hash_type value(const value_type *str, std::size_t size) ENTT_NOEXCEPT {
  8864. id_type partial{traits_type::offset};
  8865. while(size--) { partial = (partial^(str++)[0])*traits_type::prime; }
  8866. return partial;
  8867. }
  8868. /**
  8869. * @brief Returns directly the numeric representation of a string.
  8870. *
  8871. * Forcing template resolution avoids implicit conversions. An
  8872. * human-readable identifier can be anything but a plain, old bunch of
  8873. * characters.<br/>
  8874. * Example of use:
  8875. * @code{.cpp}
  8876. * const auto value = basic_hashed_string<char>::to_value("my.png");
  8877. * @endcode
  8878. *
  8879. * @tparam N Number of characters of the identifier.
  8880. * @param str Human-readable identifer.
  8881. * @return The numeric representation of the string.
  8882. */
  8883. template<std::size_t N>
  8884. [[nodiscard]] static constexpr hash_type value(const value_type (&str)[N]) ENTT_NOEXCEPT {
  8885. return helper(str);
  8886. }
  8887. /**
  8888. * @brief Returns directly the numeric representation of a string.
  8889. * @param wrapper Helps achieving the purpose by relying on overloading.
  8890. * @return The numeric representation of the string.
  8891. */
  8892. [[nodiscard]] static hash_type value(const_wrapper wrapper) ENTT_NOEXCEPT {
  8893. return helper(wrapper.str);
  8894. }
  8895. /*! @brief Constructs an empty hashed string. */
  8896. constexpr basic_hashed_string() ENTT_NOEXCEPT
  8897. : str{nullptr}, hash{}
  8898. {}
  8899. /**
  8900. * @brief Constructs a hashed string from an array of const characters.
  8901. *
  8902. * Forcing template resolution avoids implicit conversions. An
  8903. * human-readable identifier can be anything but a plain, old bunch of
  8904. * characters.<br/>
  8905. * Example of use:
  8906. * @code{.cpp}
  8907. * basic_hashed_string<char> hs{"my.png"};
  8908. * @endcode
  8909. *
  8910. * @tparam N Number of characters of the identifier.
  8911. * @param curr Human-readable identifer.
  8912. */
  8913. template<std::size_t N>
  8914. constexpr basic_hashed_string(const value_type (&curr)[N]) ENTT_NOEXCEPT
  8915. : str{curr}, hash{helper(curr)}
  8916. {}
  8917. /**
  8918. * @brief Explicit constructor on purpose to avoid constructing a hashed
  8919. * string directly from a `const value_type *`.
  8920. * @param wrapper Helps achieving the purpose by relying on overloading.
  8921. */
  8922. explicit constexpr basic_hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
  8923. : str{wrapper.str}, hash{helper(wrapper.str)}
  8924. {}
  8925. /**
  8926. * @brief Returns the human-readable representation of a hashed string.
  8927. * @return The string used to initialize the instance.
  8928. */
  8929. [[nodiscard]] constexpr const value_type * data() const ENTT_NOEXCEPT {
  8930. return str;
  8931. }
  8932. /**
  8933. * @brief Returns the numeric representation of a hashed string.
  8934. * @return The numeric representation of the instance.
  8935. */
  8936. [[nodiscard]] constexpr hash_type value() const ENTT_NOEXCEPT {
  8937. return hash;
  8938. }
  8939. /*! @copydoc data */
  8940. [[nodiscard]] constexpr operator const value_type *() const ENTT_NOEXCEPT { return data(); }
  8941. /**
  8942. * @brief Returns the numeric representation of a hashed string.
  8943. * @return The numeric representation of the instance.
  8944. */
  8945. [[nodiscard]] constexpr operator hash_type() const ENTT_NOEXCEPT { return value(); }
  8946. /**
  8947. * @brief Compares two hashed strings.
  8948. * @param other Hashed string with which to compare.
  8949. * @return True if the two hashed strings are identical, false otherwise.
  8950. */
  8951. [[nodiscard]] constexpr bool operator==(const basic_hashed_string &other) const ENTT_NOEXCEPT {
  8952. return hash == other.hash;
  8953. }
  8954. private:
  8955. const value_type *str;
  8956. hash_type hash;
  8957. };
  8958. /**
  8959. * @brief Deduction guide.
  8960. *
  8961. * It allows to deduce the character type of the hashed string directly from a
  8962. * human-readable identifer provided to the constructor.
  8963. *
  8964. * @tparam Char Character type.
  8965. * @tparam N Number of characters of the identifier.
  8966. * @param str Human-readable identifer.
  8967. */
  8968. template<typename Char, std::size_t N>
  8969. basic_hashed_string(const Char (&str)[N])
  8970. -> basic_hashed_string<Char>;
  8971. /**
  8972. * @brief Compares two hashed strings.
  8973. * @tparam Char Character type.
  8974. * @param lhs A valid hashed string.
  8975. * @param rhs A valid hashed string.
  8976. * @return True if the two hashed strings are identical, false otherwise.
  8977. */
  8978. template<typename Char>
  8979. [[nodiscard]] constexpr bool operator!=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) ENTT_NOEXCEPT {
  8980. return !(lhs == rhs);
  8981. }
  8982. /*! @brief Aliases for common character types. */
  8983. using hashed_string = basic_hashed_string<char>;
  8984. /*! @brief Aliases for common character types. */
  8985. using hashed_wstring = basic_hashed_string<wchar_t>;
  8986. inline namespace literals {
  8987. /**
  8988. * @brief User defined literal for hashed strings.
  8989. * @param str The literal without its suffix.
  8990. * @return A properly initialized hashed string.
  8991. */
  8992. [[nodiscard]] constexpr entt::hashed_string operator"" _hs(const char *str, std::size_t) ENTT_NOEXCEPT {
  8993. return entt::hashed_string{str};
  8994. }
  8995. /**
  8996. * @brief User defined literal for hashed wstrings.
  8997. * @param str The literal without its suffix.
  8998. * @return A properly initialized hashed wstring.
  8999. */
  9000. [[nodiscard]] constexpr entt::hashed_wstring operator"" _hws(const wchar_t *str, std::size_t) ENTT_NOEXCEPT {
  9001. return entt::hashed_wstring{str};
  9002. }
  9003. }
  9004. }
  9005. #endif
  9006. // #include "fwd.hpp"
  9007. namespace entt {
  9008. /**
  9009. * @cond TURN_OFF_DOXYGEN
  9010. * Internal details not to be documented.
  9011. */
  9012. namespace internal {
  9013. struct ENTT_API type_seq final {
  9014. [[nodiscard]] static id_type next() ENTT_NOEXCEPT {
  9015. static ENTT_MAYBE_ATOMIC(id_type) value{};
  9016. return value++;
  9017. }
  9018. };
  9019. template<typename Type>
  9020. [[nodiscard]] constexpr auto stripped_type_name() ENTT_NOEXCEPT {
  9021. #if defined ENTT_PRETTY_FUNCTION
  9022. std::string_view pretty_function{ENTT_PRETTY_FUNCTION};
  9023. auto first = pretty_function.find_first_not_of(' ', pretty_function.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX)+1);
  9024. auto value = pretty_function.substr(first, pretty_function.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
  9025. return value;
  9026. #else
  9027. return std::string_view{""};
  9028. #endif
  9029. }
  9030. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  9031. [[nodiscard]] static constexpr std::string_view type_name(int) ENTT_NOEXCEPT {
  9032. constexpr auto value = stripped_type_name<Type>();
  9033. return value;
  9034. }
  9035. template<typename Type>
  9036. [[nodiscard]] static std::string_view type_name(char) ENTT_NOEXCEPT {
  9037. static const auto value = stripped_type_name<Type>();
  9038. return value;
  9039. }
  9040. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  9041. [[nodiscard]] static constexpr id_type type_hash(int) ENTT_NOEXCEPT {
  9042. constexpr auto stripped = stripped_type_name<Type>();
  9043. constexpr auto value = hashed_string::value(stripped.data(), stripped.size());
  9044. return value;
  9045. }
  9046. template<typename Type>
  9047. [[nodiscard]] static id_type type_hash(char) ENTT_NOEXCEPT {
  9048. static const auto value = [](const auto stripped) {
  9049. return hashed_string::value(stripped.data(), stripped.size());
  9050. }(stripped_type_name<Type>());
  9051. return value;
  9052. }
  9053. }
  9054. /**
  9055. * Internal details not to be documented.
  9056. * @endcond
  9057. */
  9058. /**
  9059. * @brief Type sequential identifier.
  9060. * @tparam Type Type for which to generate a sequential identifier.
  9061. */
  9062. template<typename Type, typename = void>
  9063. struct ENTT_API type_seq final {
  9064. /**
  9065. * @brief Returns the sequential identifier of a given type.
  9066. * @return The sequential identifier of a given type.
  9067. */
  9068. [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
  9069. static const id_type value = internal::type_seq::next();
  9070. return value;
  9071. }
  9072. /*! @copydoc value */
  9073. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  9074. };
  9075. /**
  9076. * @brief Type hash.
  9077. * @tparam Type Type for which to generate a hash value.
  9078. */
  9079. template<typename Type, typename = void>
  9080. struct type_hash final {
  9081. /**
  9082. * @brief Returns the numeric representation of a given type.
  9083. * @return The numeric representation of the given type.
  9084. */
  9085. #if defined ENTT_PRETTY_FUNCTION
  9086. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  9087. return internal::type_hash<Type>(0);
  9088. #else
  9089. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  9090. return type_seq<Type>::value();
  9091. #endif
  9092. }
  9093. /*! @copydoc value */
  9094. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  9095. };
  9096. /**
  9097. * @brief Type name.
  9098. * @tparam Type Type for which to generate a name.
  9099. */
  9100. template<typename Type, typename = void>
  9101. struct type_name final {
  9102. /**
  9103. * @brief Returns the name of a given type.
  9104. * @return The name of the given type.
  9105. */
  9106. [[nodiscard]] static constexpr std::string_view value() ENTT_NOEXCEPT {
  9107. return internal::type_name<Type>(0);
  9108. }
  9109. /*! @copydoc value */
  9110. [[nodiscard]] constexpr operator std::string_view() const ENTT_NOEXCEPT { return value(); }
  9111. };
  9112. /*! @brief Implementation specific information about a type. */
  9113. class type_info final {
  9114. template<typename>
  9115. friend type_info type_id() ENTT_NOEXCEPT;
  9116. type_info(id_type seq_v, id_type hash_v, std::string_view name_v) ENTT_NOEXCEPT
  9117. : seq_value{seq_v},
  9118. hash_value{hash_v},
  9119. name_value{name_v}
  9120. {}
  9121. public:
  9122. /*! @brief Default constructor. */
  9123. type_info() ENTT_NOEXCEPT
  9124. : type_info({}, {}, {})
  9125. {}
  9126. /*! @brief Default copy constructor. */
  9127. type_info(const type_info &) ENTT_NOEXCEPT = default;
  9128. /*! @brief Default move constructor. */
  9129. type_info(type_info &&) ENTT_NOEXCEPT = default;
  9130. /**
  9131. * @brief Default copy assignment operator.
  9132. * @return This type info object.
  9133. */
  9134. type_info & operator=(const type_info &) ENTT_NOEXCEPT = default;
  9135. /**
  9136. * @brief Default move assignment operator.
  9137. * @return This type info object.
  9138. */
  9139. type_info & operator=(type_info &&) ENTT_NOEXCEPT = default;
  9140. /**
  9141. * @brief Checks if a type info object is properly initialized.
  9142. * @return True if the object is properly initialized, false otherwise.
  9143. */
  9144. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  9145. return name_value.data() != nullptr;
  9146. }
  9147. /**
  9148. * @brief Type sequential identifier.
  9149. * @return Type sequential identifier.
  9150. */
  9151. [[nodiscard]] id_type seq() const ENTT_NOEXCEPT {
  9152. return seq_value;
  9153. }
  9154. /**
  9155. * @brief Type hash.
  9156. * @return Type hash.
  9157. */
  9158. [[nodiscard]] id_type hash() const ENTT_NOEXCEPT {
  9159. return hash_value;
  9160. }
  9161. /**
  9162. * @brief Type name.
  9163. * @return Type name.
  9164. */
  9165. [[nodiscard]] std::string_view name() const ENTT_NOEXCEPT {
  9166. return name_value;
  9167. }
  9168. /**
  9169. * @brief Compares the contents of two type info objects.
  9170. * @param other Object with which to compare.
  9171. * @return False if the two contents differ, true otherwise.
  9172. */
  9173. [[nodiscard]] bool operator==(const type_info &other) const ENTT_NOEXCEPT {
  9174. return hash_value == other.hash_value;
  9175. }
  9176. private:
  9177. id_type seq_value;
  9178. id_type hash_value;
  9179. std::string_view name_value;
  9180. };
  9181. /**
  9182. * @brief Compares the contents of two type info objects.
  9183. * @param lhs A type info object.
  9184. * @param rhs A type info object.
  9185. * @return True if the two contents differ, false otherwise.
  9186. */
  9187. [[nodiscard]] inline bool operator!=(const type_info &lhs, const type_info &rhs) ENTT_NOEXCEPT {
  9188. return !(lhs == rhs);
  9189. }
  9190. /**
  9191. * @brief Returns the type info object for a given type.
  9192. * @tparam Type Type for which to generate a type info object.
  9193. * @return The type info object for the given type.
  9194. */
  9195. template<typename Type>
  9196. [[nodiscard]] type_info type_id() ENTT_NOEXCEPT {
  9197. return type_info{
  9198. type_seq<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  9199. type_hash<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  9200. type_name<std::remove_cv_t<std::remove_reference_t<Type>>>::value()
  9201. };
  9202. }
  9203. }
  9204. #endif
  9205. // #include "type_traits.hpp"
  9206. #ifndef ENTT_CORE_TYPE_TRAITS_HPP
  9207. #define ENTT_CORE_TYPE_TRAITS_HPP
  9208. #include <cstddef>
  9209. #include <iterator>
  9210. #include <type_traits>
  9211. #include <utility>
  9212. // #include "../config/config.h"
  9213. // #include "fwd.hpp"
  9214. namespace entt {
  9215. /**
  9216. * @brief Utility class to disambiguate overloaded functions.
  9217. * @tparam N Number of choices available.
  9218. */
  9219. template<std::size_t N>
  9220. struct choice_t
  9221. // Unfortunately, doxygen cannot parse such a construct.
  9222. /*! @cond TURN_OFF_DOXYGEN */
  9223. : choice_t<N-1>
  9224. /*! @endcond */
  9225. {};
  9226. /*! @copybrief choice_t */
  9227. template<>
  9228. struct choice_t<0> {};
  9229. /**
  9230. * @brief Variable template for the choice trick.
  9231. * @tparam N Number of choices available.
  9232. */
  9233. template<std::size_t N>
  9234. inline constexpr choice_t<N> choice{};
  9235. /**
  9236. * @brief Identity type trait.
  9237. *
  9238. * Useful to establish non-deduced contexts in template argument deduction
  9239. * (waiting for C++20) or to provide types through function arguments.
  9240. *
  9241. * @tparam Type A type.
  9242. */
  9243. template<typename Type>
  9244. struct type_identity {
  9245. /*! @brief Identity type. */
  9246. using type = Type;
  9247. };
  9248. /**
  9249. * @brief Helper type.
  9250. * @tparam Type A type.
  9251. */
  9252. template<typename Type>
  9253. using type_identity_t = typename type_identity<Type>::type;
  9254. /**
  9255. * @brief A type-only `sizeof` wrapper that returns 0 where `sizeof` complains.
  9256. * @tparam Type The type of which to return the size.
  9257. * @tparam The size of the type if `sizeof` accepts it, 0 otherwise.
  9258. */
  9259. template<typename Type, typename = void>
  9260. struct size_of: std::integral_constant<std::size_t, 0u> {};
  9261. /*! @copydoc size_of */
  9262. template<typename Type>
  9263. struct size_of<Type, std::void_t<decltype(sizeof(Type))>>
  9264. : std::integral_constant<std::size_t, sizeof(Type)>
  9265. {};
  9266. /**
  9267. * @brief Helper variable template.
  9268. * @tparam Type The type of which to return the size.
  9269. */
  9270. template<class Type>
  9271. inline constexpr std::size_t size_of_v = size_of<Type>::value;
  9272. /**
  9273. * @brief Using declaration to be used to _repeat_ the same type a number of
  9274. * times equal to the size of a given parameter pack.
  9275. * @tparam Type A type to repeat.
  9276. */
  9277. template<typename Type, typename>
  9278. using unpack_as_t = Type;
  9279. /**
  9280. * @brief Helper variable template to be used to _repeat_ the same value a
  9281. * number of times equal to the size of a given parameter pack.
  9282. * @tparam Value A value to repeat.
  9283. */
  9284. template<auto Value, typename>
  9285. inline constexpr auto unpack_as_v = Value;
  9286. /**
  9287. * @brief Wraps a static constant.
  9288. * @tparam Value A static constant.
  9289. */
  9290. template<auto Value>
  9291. using integral_constant = std::integral_constant<decltype(Value), Value>;
  9292. /**
  9293. * @brief Alias template to facilitate the creation of named values.
  9294. * @tparam Value A constant value at least convertible to `id_type`.
  9295. */
  9296. template<id_type Value>
  9297. using tag = integral_constant<Value>;
  9298. /**
  9299. * @brief A class to use to push around lists of types, nothing more.
  9300. * @tparam Type Types provided by the type list.
  9301. */
  9302. template<typename... Type>
  9303. struct type_list {
  9304. /*! @brief Type list type. */
  9305. using type = type_list;
  9306. /*! @brief Compile-time number of elements in the type list. */
  9307. static constexpr auto size = sizeof...(Type);
  9308. };
  9309. /*! @brief Primary template isn't defined on purpose. */
  9310. template<std::size_t, typename>
  9311. struct type_list_element;
  9312. /**
  9313. * @brief Provides compile-time indexed access to the types of a type list.
  9314. * @tparam Index Index of the type to return.
  9315. * @tparam Type First type provided by the type list.
  9316. * @tparam Other Other types provided by the type list.
  9317. */
  9318. template<std::size_t Index, typename Type, typename... Other>
  9319. struct type_list_element<Index, type_list<Type, Other...>>
  9320. : type_list_element<Index - 1u, type_list<Other...>>
  9321. {};
  9322. /**
  9323. * @brief Provides compile-time indexed access to the types of a type list.
  9324. * @tparam Type First type provided by the type list.
  9325. * @tparam Other Other types provided by the type list.
  9326. */
  9327. template<typename Type, typename... Other>
  9328. struct type_list_element<0u, type_list<Type, Other...>> {
  9329. /*! @brief Searched type. */
  9330. using type = Type;
  9331. };
  9332. /**
  9333. * @brief Helper type.
  9334. * @tparam Index Index of the type to return.
  9335. * @tparam List Type list to search into.
  9336. */
  9337. template<std::size_t Index, typename List>
  9338. using type_list_element_t = typename type_list_element<Index, List>::type;
  9339. /**
  9340. * @brief Concatenates multiple type lists.
  9341. * @tparam Type Types provided by the first type list.
  9342. * @tparam Other Types provided by the second type list.
  9343. * @return A type list composed by the types of both the type lists.
  9344. */
  9345. template<typename... Type, typename... Other>
  9346. constexpr type_list<Type..., Other...> operator+(type_list<Type...>, type_list<Other...>) { return {}; }
  9347. /*! @brief Primary template isn't defined on purpose. */
  9348. template<typename...>
  9349. struct type_list_cat;
  9350. /*! @brief Concatenates multiple type lists. */
  9351. template<>
  9352. struct type_list_cat<> {
  9353. /*! @brief A type list composed by the types of all the type lists. */
  9354. using type = type_list<>;
  9355. };
  9356. /**
  9357. * @brief Concatenates multiple type lists.
  9358. * @tparam Type Types provided by the first type list.
  9359. * @tparam Other Types provided by the second type list.
  9360. * @tparam List Other type lists, if any.
  9361. */
  9362. template<typename... Type, typename... Other, typename... List>
  9363. struct type_list_cat<type_list<Type...>, type_list<Other...>, List...> {
  9364. /*! @brief A type list composed by the types of all the type lists. */
  9365. using type = typename type_list_cat<type_list<Type..., Other...>, List...>::type;
  9366. };
  9367. /**
  9368. * @brief Concatenates multiple type lists.
  9369. * @tparam Type Types provided by the type list.
  9370. */
  9371. template<typename... Type>
  9372. struct type_list_cat<type_list<Type...>> {
  9373. /*! @brief A type list composed by the types of all the type lists. */
  9374. using type = type_list<Type...>;
  9375. };
  9376. /**
  9377. * @brief Helper type.
  9378. * @tparam List Type lists to concatenate.
  9379. */
  9380. template<typename... List>
  9381. using type_list_cat_t = typename type_list_cat<List...>::type;
  9382. /*! @brief Primary template isn't defined on purpose. */
  9383. template<typename>
  9384. struct type_list_unique;
  9385. /**
  9386. * @brief Removes duplicates types from a type list.
  9387. * @tparam Type One of the types provided by the given type list.
  9388. * @tparam Other The other types provided by the given type list.
  9389. */
  9390. template<typename Type, typename... Other>
  9391. struct type_list_unique<type_list<Type, Other...>> {
  9392. /*! @brief A type list without duplicate types. */
  9393. using type = std::conditional_t<
  9394. std::disjunction_v<std::is_same<Type, Other>...>,
  9395. typename type_list_unique<type_list<Other...>>::type,
  9396. type_list_cat_t<type_list<Type>, typename type_list_unique<type_list<Other...>>::type>
  9397. >;
  9398. };
  9399. /*! @brief Removes duplicates types from a type list. */
  9400. template<>
  9401. struct type_list_unique<type_list<>> {
  9402. /*! @brief A type list without duplicate types. */
  9403. using type = type_list<>;
  9404. };
  9405. /**
  9406. * @brief Helper type.
  9407. * @tparam Type A type list.
  9408. */
  9409. template<typename Type>
  9410. using type_list_unique_t = typename type_list_unique<Type>::type;
  9411. /**
  9412. * @brief Provides the member constant `value` to true if a type list contains a
  9413. * given type, false otherwise.
  9414. * @tparam List Type list.
  9415. * @tparam Type Type to look for.
  9416. */
  9417. template<typename List, typename Type>
  9418. struct type_list_contains;
  9419. /**
  9420. * @copybrief type_list_contains
  9421. * @tparam Type Types provided by the type list.
  9422. * @tparam Other Type to look for.
  9423. */
  9424. template<typename... Type, typename Other>
  9425. struct type_list_contains<type_list<Type...>, Other>: std::disjunction<std::is_same<Type, Other>...> {};
  9426. /**
  9427. * @brief Helper variable template.
  9428. * @tparam List Type list.
  9429. * @tparam Type Type to look for.
  9430. */
  9431. template<class List, typename Type>
  9432. inline constexpr bool type_list_contains_v = type_list_contains<List, Type>::value;
  9433. /*! @brief Primary template isn't defined on purpose. */
  9434. template<typename...>
  9435. struct type_list_diff;
  9436. /**
  9437. * @brief Computes the difference between two type lists.
  9438. * @tparam Type Types provided by the first type list.
  9439. * @tparam Other Types provided by the second type list.
  9440. */
  9441. template<typename... Type, typename... Other>
  9442. struct type_list_diff<type_list<Type...>, type_list<Other...>> {
  9443. /*! @brief A type list that is the difference between the two type lists. */
  9444. using type = type_list_cat_t<std::conditional_t<type_list_contains_v<type_list<Other...>, Type>, type_list<>, type_list<Type>>...>;
  9445. };
  9446. /**
  9447. * @brief Helper type.
  9448. * @tparam List Type lists between which to compute the difference.
  9449. */
  9450. template<typename... List>
  9451. using type_list_diff_t = typename type_list_diff<List...>::type;
  9452. /**
  9453. * @brief A class to use to push around lists of constant values, nothing more.
  9454. * @tparam Value Values provided by the value list.
  9455. */
  9456. template<auto... Value>
  9457. struct value_list {
  9458. /*! @brief Value list type. */
  9459. using type = value_list;
  9460. /*! @brief Compile-time number of elements in the value list. */
  9461. static constexpr auto size = sizeof...(Value);
  9462. };
  9463. /*! @brief Primary template isn't defined on purpose. */
  9464. template<std::size_t, typename>
  9465. struct value_list_element;
  9466. /**
  9467. * @brief Provides compile-time indexed access to the values of a value list.
  9468. * @tparam Index Index of the value to return.
  9469. * @tparam Value First value provided by the value list.
  9470. * @tparam Other Other values provided by the value list.
  9471. */
  9472. template<std::size_t Index, auto Value, auto... Other>
  9473. struct value_list_element<Index, value_list<Value, Other...>>
  9474. : value_list_element<Index - 1u, value_list<Other...>>
  9475. {};
  9476. /**
  9477. * @brief Provides compile-time indexed access to the types of a type list.
  9478. * @tparam Value First value provided by the value list.
  9479. * @tparam Other Other values provided by the value list.
  9480. */
  9481. template<auto Value, auto... Other>
  9482. struct value_list_element<0u, value_list<Value, Other...>> {
  9483. /*! @brief Searched value. */
  9484. static constexpr auto value = Value;
  9485. };
  9486. /**
  9487. * @brief Helper type.
  9488. * @tparam Index Index of the value to return.
  9489. * @tparam List Value list to search into.
  9490. */
  9491. template<std::size_t Index, typename List>
  9492. inline constexpr auto value_list_element_v = value_list_element<Index, List>::value;
  9493. /**
  9494. * @brief Concatenates multiple value lists.
  9495. * @tparam Value Values provided by the first value list.
  9496. * @tparam Other Values provided by the second value list.
  9497. * @return A value list composed by the values of both the value lists.
  9498. */
  9499. template<auto... Value, auto... Other>
  9500. constexpr value_list<Value..., Other...> operator+(value_list<Value...>, value_list<Other...>) { return {}; }
  9501. /*! @brief Primary template isn't defined on purpose. */
  9502. template<typename...>
  9503. struct value_list_cat;
  9504. /*! @brief Concatenates multiple value lists. */
  9505. template<>
  9506. struct value_list_cat<> {
  9507. /*! @brief A value list composed by the values of all the value lists. */
  9508. using type = value_list<>;
  9509. };
  9510. /**
  9511. * @brief Concatenates multiple value lists.
  9512. * @tparam Value Values provided by the first value list.
  9513. * @tparam Other Values provided by the second value list.
  9514. * @tparam List Other value lists, if any.
  9515. */
  9516. template<auto... Value, auto... Other, typename... List>
  9517. struct value_list_cat<value_list<Value...>, value_list<Other...>, List...> {
  9518. /*! @brief A value list composed by the values of all the value lists. */
  9519. using type = typename value_list_cat<value_list<Value..., Other...>, List...>::type;
  9520. };
  9521. /**
  9522. * @brief Concatenates multiple value lists.
  9523. * @tparam Value Values provided by the value list.
  9524. */
  9525. template<auto... Value>
  9526. struct value_list_cat<value_list<Value...>> {
  9527. /*! @brief A value list composed by the values of all the value lists. */
  9528. using type = value_list<Value...>;
  9529. };
  9530. /**
  9531. * @brief Helper type.
  9532. * @tparam List Value lists to concatenate.
  9533. */
  9534. template<typename... List>
  9535. using value_list_cat_t = typename value_list_cat<List...>::type;
  9536. /**
  9537. * @cond TURN_OFF_DOXYGEN
  9538. * Internal details not to be documented.
  9539. */
  9540. namespace internal {
  9541. template<typename>
  9542. [[nodiscard]] constexpr bool is_equality_comparable(...) { return false; }
  9543. template<typename Type>
  9544. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<0>)
  9545. -> decltype(std::declval<Type>() == std::declval<Type>()) { return true; }
  9546. template<typename Type>
  9547. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<1>)
  9548. -> decltype(std::declval<typename Type::value_type>(), std::declval<Type>() == std::declval<Type>()) {
  9549. if constexpr(std::is_same_v<typename Type::value_type, Type>) {
  9550. return is_equality_comparable<Type>(choice<0>);
  9551. } else {
  9552. return is_equality_comparable<typename Type::value_type>(choice<2>);
  9553. }
  9554. }
  9555. template<typename Type>
  9556. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<2>)
  9557. -> decltype(std::declval<typename Type::mapped_type>(), std::declval<Type>() == std::declval<Type>()) {
  9558. return is_equality_comparable<typename Type::key_type>(choice<2>) && is_equality_comparable<typename Type::mapped_type>(choice<2>);
  9559. }
  9560. }
  9561. /**
  9562. * Internal details not to be documented.
  9563. * @endcond
  9564. */
  9565. /**
  9566. * @brief Provides the member constant `value` to true if a given type is
  9567. * equality comparable, false otherwise.
  9568. * @tparam Type The type to test.
  9569. */
  9570. template<typename Type, typename = void>
  9571. struct is_equality_comparable: std::bool_constant<internal::is_equality_comparable<Type>(choice<2>)> {};
  9572. /**
  9573. * @brief Helper variable template.
  9574. * @tparam Type The type to test.
  9575. */
  9576. template<class Type>
  9577. inline constexpr bool is_equality_comparable_v = is_equality_comparable<Type>::value;
  9578. /*! @brief Same as std::is_invocable, but with tuples. */
  9579. template<typename, typename>
  9580. struct is_applicable: std::false_type {};
  9581. /**
  9582. * @copybrief is_applicable
  9583. * @tparam Func A valid function type.
  9584. * @tparam Tuple Tuple-like type.
  9585. * @tparam Args The list of arguments to use to probe the function type.
  9586. */
  9587. template<typename Func, template<typename...> class Tuple, typename... Args>
  9588. struct is_applicable<Func, Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  9589. /**
  9590. * @copybrief is_applicable
  9591. * @tparam Func A valid function type.
  9592. * @tparam Tuple Tuple-like type.
  9593. * @tparam Args The list of arguments to use to probe the function type.
  9594. */
  9595. template<typename Func, template<typename...> class Tuple, typename... Args>
  9596. struct is_applicable<Func, const Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  9597. /**
  9598. * @brief Helper variable template.
  9599. * @tparam Func A valid function type.
  9600. * @tparam Args The list of arguments to use to probe the function type.
  9601. */
  9602. template<typename Func, typename Args>
  9603. inline constexpr bool is_applicable_v = is_applicable<Func, Args>::value;
  9604. /*! @brief Same as std::is_invocable_r, but with tuples for arguments. */
  9605. template<typename, typename, typename>
  9606. struct is_applicable_r: std::false_type {};
  9607. /**
  9608. * @copybrief is_applicable_r
  9609. * @tparam Ret The type to which the return type of the function should be
  9610. * convertible.
  9611. * @tparam Func A valid function type.
  9612. * @tparam Args The list of arguments to use to probe the function type.
  9613. */
  9614. template<typename Ret, typename Func, typename... Args>
  9615. struct is_applicable_r<Ret, Func, std::tuple<Args...>>: std::is_invocable_r<Ret, Func, Args...> {};
  9616. /**
  9617. * @brief Helper variable template.
  9618. * @tparam Ret The type to which the return type of the function should be
  9619. * convertible.
  9620. * @tparam Func A valid function type.
  9621. * @tparam Args The list of arguments to use to probe the function type.
  9622. */
  9623. template<typename Ret, typename Func, typename Args>
  9624. inline constexpr bool is_applicable_r_v = is_applicable_r<Ret, Func, Args>::value;
  9625. /**
  9626. * @brief Provides the member constant `value` to true if a given type is
  9627. * complete, false otherwise.
  9628. * @tparam Type The type to test.
  9629. */
  9630. template<typename Type, typename = void>
  9631. struct is_complete: std::false_type {};
  9632. /*! @copydoc is_complete */
  9633. template<typename Type>
  9634. struct is_complete<Type, std::void_t<decltype(sizeof(Type))>>: std::true_type {};
  9635. /**
  9636. * @brief Helper variable template.
  9637. * @tparam Type The type to test.
  9638. */
  9639. template<typename Type>
  9640. inline constexpr bool is_complete_v = is_complete<Type>::value;
  9641. /**
  9642. * @brief Provides the member constant `value` to true if a given type is an
  9643. * iterator, false otherwise.
  9644. * @tparam Type The type to test.
  9645. */
  9646. template<typename Type, typename = void>
  9647. struct is_iterator: std::false_type {};
  9648. /*! @copydoc is_iterator */
  9649. template<typename Type>
  9650. struct is_iterator<Type, std::void_t<typename std::iterator_traits<Type>::iterator_category>>
  9651. : std::true_type
  9652. {};
  9653. /**
  9654. * @brief Helper variable template.
  9655. * @tparam Type The type to test.
  9656. */
  9657. template<typename Type>
  9658. inline constexpr bool is_iterator_v = is_iterator<Type>::value;
  9659. /**
  9660. * @brief Provides the member constant `value` to true if a given type is of the
  9661. * required iterator type, false otherwise.
  9662. * @tparam Type The type to test.
  9663. * @tparam It Required iterator type.
  9664. */
  9665. template<typename Type, typename It, typename = void>
  9666. struct is_iterator_type: std::false_type {};
  9667. /*! @copydoc is_iterator_type */
  9668. template<typename Type, typename It>
  9669. struct is_iterator_type<Type, It, std::enable_if_t<is_iterator_v<Type> && std::is_same_v<Type, It>>>
  9670. : std::true_type
  9671. {};
  9672. /*! @copydoc is_iterator_type */
  9673. template<typename Type, typename It>
  9674. struct is_iterator_type<Type, It, std::enable_if_t<!std::is_same_v<Type, It>, std::void_t<typename It::iterator_type>>>
  9675. : is_iterator_type<Type, typename It::iterator_type>
  9676. {};
  9677. /**
  9678. * @brief Helper variable template.
  9679. * @tparam Type The type to test.
  9680. * @tparam It Required iterator type.
  9681. */
  9682. template<typename Type, typename It>
  9683. inline constexpr bool is_iterator_type_v = is_iterator_type<Type, It>::value;
  9684. /**
  9685. * @brief Transcribes the constness of a type to another type.
  9686. * @tparam To The type to which to transcribe the constness.
  9687. * @tparam From The type from which to transcribe the constness.
  9688. */
  9689. template<typename To, typename From>
  9690. struct constness_as {
  9691. /*! @brief The type resulting from the transcription of the constness. */
  9692. using type = std::remove_const_t<To>;
  9693. };
  9694. /*! @copydoc constness_as */
  9695. template<typename To, typename From>
  9696. struct constness_as<To, const From> {
  9697. /*! @brief The type resulting from the transcription of the constness. */
  9698. using type = std::add_const_t<To>;
  9699. };
  9700. /**
  9701. * @brief Alias template to facilitate the transcription of the constness.
  9702. * @tparam To The type to which to transcribe the constness.
  9703. * @tparam From The type from which to transcribe the constness.
  9704. */
  9705. template<typename To, typename From>
  9706. using constness_as_t = typename constness_as<To, From>::type;
  9707. /**
  9708. * @brief Extracts the class of a non-static member object or function.
  9709. * @tparam Member A pointer to a non-static member object or function.
  9710. */
  9711. template<typename Member>
  9712. class member_class {
  9713. static_assert(std::is_member_pointer_v<Member>, "Invalid pointer type to non-static member object or function");
  9714. template<typename Class, typename Ret, typename... Args>
  9715. static Class * clazz(Ret(Class:: *)(Args...));
  9716. template<typename Class, typename Ret, typename... Args>
  9717. static Class * clazz(Ret(Class:: *)(Args...) const);
  9718. template<typename Class, typename Type>
  9719. static Class * clazz(Type Class:: *);
  9720. public:
  9721. /*! @brief The class of the given non-static member object or function. */
  9722. using type = std::remove_pointer_t<decltype(clazz(std::declval<Member>()))>;
  9723. };
  9724. /**
  9725. * @brief Helper type.
  9726. * @tparam Member A pointer to a non-static member object or function.
  9727. */
  9728. template<typename Member>
  9729. using member_class_t = typename member_class<Member>::type;
  9730. }
  9731. #endif
  9732. namespace entt {
  9733. /**
  9734. * @brief A SBO friendly, type-safe container for single values of any type.
  9735. * @tparam Len Size of the storage reserved for the small buffer optimization.
  9736. * @tparam Align Optional alignment requirement.
  9737. */
  9738. template<std::size_t Len, std::size_t Align>
  9739. class basic_any {
  9740. enum class operation: std::uint8_t { COPY, MOVE, DTOR, COMP, ADDR, CADDR, TYPE };
  9741. enum class policy: std::uint8_t { OWNER, REF, CREF };
  9742. using storage_type = std::aligned_storage_t<Len + !Len, Align>;
  9743. using vtable_type = const void *(const operation, const basic_any &, void *);
  9744. template<typename Type>
  9745. static constexpr bool in_situ = Len && alignof(Type) <= alignof(storage_type) && sizeof(Type) <= sizeof(storage_type) && std::is_nothrow_move_constructible_v<Type>;
  9746. template<typename Type>
  9747. [[nodiscard]] static constexpr policy type_to_policy() {
  9748. if constexpr(std::is_lvalue_reference_v<Type>) {
  9749. if constexpr(std::is_const_v<std::remove_reference_t<Type>>) {
  9750. return policy::CREF;
  9751. } else {
  9752. return policy::REF;
  9753. }
  9754. } else {
  9755. return policy::OWNER;
  9756. }
  9757. }
  9758. template<typename Type>
  9759. [[nodiscard]] static bool compare(const void *lhs, const void *rhs) {
  9760. if constexpr(!std::is_function_v<Type> && is_equality_comparable_v<Type>) {
  9761. return *static_cast<const Type *>(lhs) == *static_cast<const Type *>(rhs);
  9762. } else {
  9763. return lhs == rhs;
  9764. }
  9765. }
  9766. template<typename Type>
  9767. static const void * basic_vtable([[maybe_unused]] const operation op, [[maybe_unused]] const basic_any &from, [[maybe_unused]] void *to) {
  9768. static_assert(std::is_same_v<std::remove_reference_t<std::remove_const_t<Type>>, Type>, "Invalid type");
  9769. if constexpr(!std::is_void_v<Type>) {
  9770. const Type *instance = (in_situ<Type> && from.mode == policy::OWNER)
  9771. ? ENTT_LAUNDER(reinterpret_cast<const Type *>(&from.storage))
  9772. : static_cast<const Type *>(from.instance);
  9773. switch(op) {
  9774. case operation::COPY:
  9775. if constexpr(std::is_copy_constructible_v<Type>) {
  9776. static_cast<basic_any *>(to)->emplace<Type>(*instance);
  9777. }
  9778. break;
  9779. case operation::MOVE:
  9780. if constexpr(in_situ<Type>) {
  9781. if(from.mode == policy::OWNER) {
  9782. return new (&static_cast<basic_any *>(to)->storage) Type{std::move(*const_cast<Type *>(instance))};
  9783. }
  9784. }
  9785. return (static_cast<basic_any *>(to)->instance = std::exchange(const_cast<basic_any &>(from).instance, nullptr));
  9786. case operation::DTOR:
  9787. if(from.mode == policy::OWNER) {
  9788. if constexpr(in_situ<Type>) {
  9789. instance->~Type();
  9790. } else if constexpr(std::is_array_v<Type>) {
  9791. delete[] instance;
  9792. } else {
  9793. delete instance;
  9794. }
  9795. }
  9796. break;
  9797. case operation::COMP:
  9798. return compare<Type>(instance, (*static_cast<const basic_any **>(to))->data()) ? to : nullptr;
  9799. case operation::ADDR:
  9800. if(from.mode == policy::CREF) {
  9801. return nullptr;
  9802. }
  9803. [[fallthrough]];
  9804. case operation::CADDR:
  9805. return instance;
  9806. case operation::TYPE:
  9807. *static_cast<type_info *>(to) = type_id<Type>();
  9808. break;
  9809. }
  9810. }
  9811. return nullptr;
  9812. }
  9813. template<typename Type, typename... Args>
  9814. void initialize([[maybe_unused]] Args &&... args) {
  9815. if constexpr(!std::is_void_v<Type>) {
  9816. if constexpr(std::is_lvalue_reference_v<Type>) {
  9817. static_assert(sizeof...(Args) == 1u && (std::is_lvalue_reference_v<Args> && ...), "Invalid arguments");
  9818. instance = (std::addressof(args), ...);
  9819. } else if constexpr(in_situ<Type>) {
  9820. if constexpr(sizeof...(Args) != 0u && std::is_aggregate_v<Type>) {
  9821. new (&storage) Type{std::forward<Args>(args)...};
  9822. } else {
  9823. new (&storage) Type(std::forward<Args>(args)...);
  9824. }
  9825. } else {
  9826. if constexpr(sizeof...(Args) != 0u && std::is_aggregate_v<Type>) {
  9827. instance = new Type{std::forward<Args>(args)...};
  9828. } else {
  9829. instance = new Type(std::forward<Args>(args)...);
  9830. }
  9831. }
  9832. }
  9833. }
  9834. basic_any(const basic_any &other, const policy pol) ENTT_NOEXCEPT
  9835. : instance{other.data()},
  9836. vtable{other.vtable},
  9837. mode{pol}
  9838. {}
  9839. public:
  9840. /*! @brief Size of the internal storage. */
  9841. static constexpr auto length = Len;
  9842. /*! @brief Alignment requirement. */
  9843. static constexpr auto alignment = Align;
  9844. /*! @brief Default constructor. */
  9845. basic_any() ENTT_NOEXCEPT
  9846. : instance{},
  9847. vtable{&basic_vtable<void>},
  9848. mode{policy::OWNER}
  9849. {}
  9850. /**
  9851. * @brief Constructs a wrapper by directly initializing the new object.
  9852. * @tparam Type Type of object to use to initialize the wrapper.
  9853. * @tparam Args Types of arguments to use to construct the new instance.
  9854. * @param args Parameters to use to construct the instance.
  9855. */
  9856. template<typename Type, typename... Args>
  9857. explicit basic_any(std::in_place_type_t<Type>, Args &&... args)
  9858. : instance{},
  9859. vtable{&basic_vtable<std::remove_const_t<std::remove_reference_t<Type>>>},
  9860. mode{type_to_policy<Type>()}
  9861. {
  9862. initialize<Type>(std::forward<Args>(args)...);
  9863. }
  9864. /**
  9865. * @brief Constructs a wrapper that holds an unmanaged object.
  9866. * @tparam Type Type of object to use to initialize the wrapper.
  9867. * @param value An instance of an object to use to initialize the wrapper.
  9868. */
  9869. template<typename Type>
  9870. basic_any(std::reference_wrapper<Type> value) ENTT_NOEXCEPT
  9871. : basic_any{}
  9872. {
  9873. // invokes deprecated assignment operator (and avoids issues with vs2017)
  9874. *this = value;
  9875. }
  9876. /**
  9877. * @brief Constructs a wrapper from a given value.
  9878. * @tparam Type Type of object to use to initialize the wrapper.
  9879. * @param value An instance of an object to use to initialize the wrapper.
  9880. */
  9881. template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::decay_t<Type>, basic_any>>>
  9882. basic_any(Type &&value)
  9883. : instance{},
  9884. vtable{&basic_vtable<std::decay_t<Type>>},
  9885. mode{policy::OWNER}
  9886. {
  9887. initialize<std::decay_t<Type>>(std::forward<Type>(value));
  9888. }
  9889. /**
  9890. * @brief Copy constructor.
  9891. * @param other The instance to copy from.
  9892. */
  9893. basic_any(const basic_any &other)
  9894. : instance{},
  9895. vtable{&basic_vtable<void>},
  9896. mode{policy::OWNER}
  9897. {
  9898. other.vtable(operation::COPY, other, this);
  9899. }
  9900. /**
  9901. * @brief Move constructor.
  9902. * @param other The instance to move from.
  9903. */
  9904. basic_any(basic_any &&other) ENTT_NOEXCEPT
  9905. : instance{},
  9906. vtable{other.vtable},
  9907. mode{other.mode}
  9908. {
  9909. vtable(operation::MOVE, other, this);
  9910. }
  9911. /*! @brief Frees the internal storage, whatever it means. */
  9912. ~basic_any() {
  9913. vtable(operation::DTOR, *this, nullptr);
  9914. }
  9915. /**
  9916. * @brief Copy assignment operator.
  9917. * @param other The instance to copy from.
  9918. * @return This any object.
  9919. */
  9920. basic_any & operator=(const basic_any &other) {
  9921. reset();
  9922. other.vtable(operation::COPY, other, this);
  9923. return *this;
  9924. }
  9925. /**
  9926. * @brief Move assignment operator.
  9927. * @param other The instance to move from.
  9928. * @return This any object.
  9929. */
  9930. basic_any & operator=(basic_any &&other) ENTT_NOEXCEPT {
  9931. std::exchange(vtable, other.vtable)(operation::DTOR, *this, nullptr);
  9932. other.vtable(operation::MOVE, other, this);
  9933. mode = other.mode;
  9934. return *this;
  9935. }
  9936. /**
  9937. * @brief Value assignment operator.
  9938. * @tparam Type Type of object to use to initialize the wrapper.
  9939. * @param value An instance of an object to use to initialize the wrapper.
  9940. * @return This any object.
  9941. */
  9942. template<typename Type>
  9943. [[deprecated("Use std::in_place_type<T &>, entt::make_any<T &>, emplace<Type &> or forward_as_any instead")]]
  9944. basic_any & operator=(std::reference_wrapper<Type> value) ENTT_NOEXCEPT {
  9945. emplace<Type &>(value.get());
  9946. return *this;
  9947. }
  9948. /**
  9949. * @brief Value assignment operator.
  9950. * @tparam Type Type of object to use to initialize the wrapper.
  9951. * @param value An instance of an object to use to initialize the wrapper.
  9952. * @return This any object.
  9953. */
  9954. template<typename Type>
  9955. std::enable_if_t<!std::is_same_v<std::decay_t<Type>, basic_any>, basic_any &>
  9956. operator=(Type &&value) {
  9957. emplace<std::decay_t<Type>>(std::forward<Type>(value));
  9958. return *this;
  9959. }
  9960. /**
  9961. * @brief Returns the type of the contained object.
  9962. * @return The type of the contained object, if any.
  9963. */
  9964. [[nodiscard]] type_info type() const ENTT_NOEXCEPT {
  9965. type_info info{};
  9966. vtable(operation::TYPE, *this, &info);
  9967. return info;
  9968. }
  9969. /**
  9970. * @brief Returns an opaque pointer to the contained instance.
  9971. * @return An opaque pointer the contained instance, if any.
  9972. */
  9973. [[nodiscard]] const void * data() const ENTT_NOEXCEPT {
  9974. return vtable(operation::CADDR, *this, nullptr);
  9975. }
  9976. /*! @copydoc data */
  9977. [[nodiscard]] void * data() ENTT_NOEXCEPT {
  9978. return const_cast<void *>(vtable(operation::ADDR, *this, nullptr));
  9979. }
  9980. /**
  9981. * @brief Replaces the contained object by creating a new instance directly.
  9982. * @tparam Type Type of object to use to initialize the wrapper.
  9983. * @tparam Args Types of arguments to use to construct the new instance.
  9984. * @param args Parameters to use to construct the instance.
  9985. */
  9986. template<typename Type, typename... Args>
  9987. void emplace(Args &&... args) {
  9988. std::exchange(vtable, &basic_vtable<std::remove_const_t<std::remove_reference_t<Type>>>)(operation::DTOR, *this, nullptr);
  9989. mode = type_to_policy<Type>();
  9990. initialize<Type>(std::forward<Args>(args)...);
  9991. }
  9992. /*! @brief Destroys contained object */
  9993. void reset() {
  9994. std::exchange(vtable, &basic_vtable<void>)(operation::DTOR, *this, nullptr);
  9995. mode = policy::OWNER;
  9996. }
  9997. /**
  9998. * @brief Returns false if a wrapper is empty, true otherwise.
  9999. * @return False if the wrapper is empty, true otherwise.
  10000. */
  10001. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  10002. return !(vtable(operation::CADDR, *this, nullptr) == nullptr);
  10003. }
  10004. /**
  10005. * @brief Checks if two wrappers differ in their content.
  10006. * @param other Wrapper with which to compare.
  10007. * @return False if the two objects differ in their content, true otherwise.
  10008. */
  10009. bool operator==(const basic_any &other) const ENTT_NOEXCEPT {
  10010. const basic_any *trampoline = &other;
  10011. return type() == other.type() && (vtable(operation::COMP, *this, &trampoline) || !other.data());
  10012. }
  10013. /**
  10014. * @brief Aliasing constructor.
  10015. * @return A wrapper that shares a reference to an unmanaged object.
  10016. */
  10017. [[nodiscard]] basic_any as_ref() ENTT_NOEXCEPT {
  10018. return basic_any{*this, (mode == policy::CREF ? policy::CREF : policy::REF)};
  10019. }
  10020. /*! @copydoc as_ref */
  10021. [[nodiscard]] basic_any as_ref() const ENTT_NOEXCEPT {
  10022. return basic_any{*this, policy::CREF};
  10023. }
  10024. /**
  10025. * @brief Returns true if a wrapper owns its object, false otherwise.
  10026. * @return True if the wrapper owns its object, false otherwise.
  10027. */
  10028. [[nodiscard]] bool owner() const ENTT_NOEXCEPT {
  10029. return (mode == policy::OWNER);
  10030. }
  10031. private:
  10032. union { const void *instance; storage_type storage; };
  10033. vtable_type *vtable;
  10034. policy mode;
  10035. };
  10036. /**
  10037. * @brief Checks if two wrappers differ in their content.
  10038. * @tparam Len Size of the storage reserved for the small buffer optimization.
  10039. * @tparam Align Alignment requirement.
  10040. * @param lhs A wrapper, either empty or not.
  10041. * @param rhs A wrapper, either empty or not.
  10042. * @return True if the two wrappers differ in their content, false otherwise.
  10043. */
  10044. template<std::size_t Len, std::size_t Align>
  10045. [[nodiscard]] inline bool operator!=(const basic_any<Len, Align> &lhs, const basic_any<Len, Align> &rhs) ENTT_NOEXCEPT {
  10046. return !(lhs == rhs);
  10047. }
  10048. /**
  10049. * @brief Performs type-safe access to the contained object.
  10050. * @tparam Type Type to which conversion is required.
  10051. * @tparam Len Size of the storage reserved for the small buffer optimization.
  10052. * @tparam Align Alignment requirement.
  10053. * @param data Target any object.
  10054. * @return The element converted to the requested type.
  10055. */
  10056. template<typename Type, std::size_t Len, std::size_t Align>
  10057. Type any_cast(const basic_any<Len, Align> &data) ENTT_NOEXCEPT {
  10058. const auto * const instance = any_cast<std::remove_reference_t<Type>>(&data);
  10059. ENTT_ASSERT(instance, "Invalid instance");
  10060. return static_cast<Type>(*instance);
  10061. }
  10062. /*! @copydoc any_cast */
  10063. template<typename Type, std::size_t Len, std::size_t Align>
  10064. Type any_cast(basic_any<Len, Align> &data) ENTT_NOEXCEPT {
  10065. // forces const on non-reference types to make them work also with wrappers for const references
  10066. auto * const instance = any_cast<std::remove_reference_t<const Type>>(&data);
  10067. ENTT_ASSERT(instance, "Invalid instance");
  10068. return static_cast<Type>(*instance);
  10069. }
  10070. /*! @copydoc any_cast */
  10071. template<typename Type, std::size_t Len, std::size_t Align>
  10072. Type any_cast(basic_any<Len, Align> &&data) ENTT_NOEXCEPT {
  10073. // forces const on non-reference types to make them work also with wrappers for const references
  10074. auto * const instance = any_cast<std::remove_reference_t<const Type>>(&data);
  10075. ENTT_ASSERT(instance, "Invalid instance");
  10076. return static_cast<Type>(std::move(*instance));
  10077. }
  10078. /*! @copydoc any_cast */
  10079. template<typename Type, std::size_t Len, std::size_t Align>
  10080. const Type * any_cast(const basic_any<Len, Align> *data) ENTT_NOEXCEPT {
  10081. return (data->type() == type_id<Type>() ? static_cast<const Type *>(data->data()) : nullptr);
  10082. }
  10083. /*! @copydoc any_cast */
  10084. template<typename Type, std::size_t Len, std::size_t Align>
  10085. Type * any_cast(basic_any<Len, Align> *data) ENTT_NOEXCEPT {
  10086. // last attempt to make wrappers for const references return their values
  10087. return (data->type() == type_id<Type>() ? static_cast<Type *>(static_cast<constness_as_t<basic_any<Len, Align>, Type> *>(data)->data()) : nullptr);
  10088. }
  10089. /**
  10090. * @brief Constructs a wrapper from a given type, passing it all arguments.
  10091. * @tparam Type Type of object to use to initialize the wrapper.
  10092. * @tparam Len Size of the storage reserved for the small buffer optimization.
  10093. * @tparam Align Optional alignment requirement.
  10094. * @tparam Args Types of arguments to use to construct the new instance.
  10095. * @param args Parameters to use to construct the instance.
  10096. * @return A properly initialized wrapper for an object of the given type.
  10097. */
  10098. template<typename Type, std::size_t Len = basic_any<>::length, std::size_t Align = basic_any<Len>::alignment, typename... Args>
  10099. basic_any<Len, Align> make_any(Args &&... args) {
  10100. return basic_any<Len, Align>{std::in_place_type<Type>, std::forward<Args>(args)...};
  10101. }
  10102. /**
  10103. * @brief Forwards its argument and avoids copies for lvalue references.
  10104. * @tparam Len Size of the storage reserved for the small buffer optimization.
  10105. * @tparam Align Optional alignment requirement.
  10106. * @tparam Type Type of argument to use to construct the new instance.
  10107. * @param value Parameter to use to construct the instance.
  10108. * @return A properly initialized and not necessarily owning wrapper.
  10109. */
  10110. template<std::size_t Len = basic_any<>::length, std::size_t Align = basic_any<Len>::alignment, typename Type>
  10111. basic_any<Len, Align> forward_as_any(Type &&value) {
  10112. return basic_any<Len, Align>{std::in_place_type<std::conditional_t<std::is_rvalue_reference_v<Type>, std::decay_t<Type>, Type>>, std::forward<Type>(value)};
  10113. }
  10114. }
  10115. #endif
  10116. // #include "../core/fwd.hpp"
  10117. // #include "../core/type_info.hpp"
  10118. #ifndef ENTT_CORE_TYPE_INFO_HPP
  10119. #define ENTT_CORE_TYPE_INFO_HPP
  10120. #include <string_view>
  10121. #include <type_traits>
  10122. // #include "../config/config.h"
  10123. // #include "../core/attribute.h"
  10124. // #include "hashed_string.hpp"
  10125. // #include "fwd.hpp"
  10126. namespace entt {
  10127. /**
  10128. * @cond TURN_OFF_DOXYGEN
  10129. * Internal details not to be documented.
  10130. */
  10131. namespace internal {
  10132. struct ENTT_API type_seq final {
  10133. [[nodiscard]] static id_type next() ENTT_NOEXCEPT {
  10134. static ENTT_MAYBE_ATOMIC(id_type) value{};
  10135. return value++;
  10136. }
  10137. };
  10138. template<typename Type>
  10139. [[nodiscard]] constexpr auto stripped_type_name() ENTT_NOEXCEPT {
  10140. #if defined ENTT_PRETTY_FUNCTION
  10141. std::string_view pretty_function{ENTT_PRETTY_FUNCTION};
  10142. auto first = pretty_function.find_first_not_of(' ', pretty_function.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX)+1);
  10143. auto value = pretty_function.substr(first, pretty_function.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
  10144. return value;
  10145. #else
  10146. return std::string_view{""};
  10147. #endif
  10148. }
  10149. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  10150. [[nodiscard]] static constexpr std::string_view type_name(int) ENTT_NOEXCEPT {
  10151. constexpr auto value = stripped_type_name<Type>();
  10152. return value;
  10153. }
  10154. template<typename Type>
  10155. [[nodiscard]] static std::string_view type_name(char) ENTT_NOEXCEPT {
  10156. static const auto value = stripped_type_name<Type>();
  10157. return value;
  10158. }
  10159. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  10160. [[nodiscard]] static constexpr id_type type_hash(int) ENTT_NOEXCEPT {
  10161. constexpr auto stripped = stripped_type_name<Type>();
  10162. constexpr auto value = hashed_string::value(stripped.data(), stripped.size());
  10163. return value;
  10164. }
  10165. template<typename Type>
  10166. [[nodiscard]] static id_type type_hash(char) ENTT_NOEXCEPT {
  10167. static const auto value = [](const auto stripped) {
  10168. return hashed_string::value(stripped.data(), stripped.size());
  10169. }(stripped_type_name<Type>());
  10170. return value;
  10171. }
  10172. }
  10173. /**
  10174. * Internal details not to be documented.
  10175. * @endcond
  10176. */
  10177. /**
  10178. * @brief Type sequential identifier.
  10179. * @tparam Type Type for which to generate a sequential identifier.
  10180. */
  10181. template<typename Type, typename = void>
  10182. struct ENTT_API type_seq final {
  10183. /**
  10184. * @brief Returns the sequential identifier of a given type.
  10185. * @return The sequential identifier of a given type.
  10186. */
  10187. [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
  10188. static const id_type value = internal::type_seq::next();
  10189. return value;
  10190. }
  10191. /*! @copydoc value */
  10192. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  10193. };
  10194. /**
  10195. * @brief Type hash.
  10196. * @tparam Type Type for which to generate a hash value.
  10197. */
  10198. template<typename Type, typename = void>
  10199. struct type_hash final {
  10200. /**
  10201. * @brief Returns the numeric representation of a given type.
  10202. * @return The numeric representation of the given type.
  10203. */
  10204. #if defined ENTT_PRETTY_FUNCTION
  10205. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  10206. return internal::type_hash<Type>(0);
  10207. #else
  10208. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  10209. return type_seq<Type>::value();
  10210. #endif
  10211. }
  10212. /*! @copydoc value */
  10213. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  10214. };
  10215. /**
  10216. * @brief Type name.
  10217. * @tparam Type Type for which to generate a name.
  10218. */
  10219. template<typename Type, typename = void>
  10220. struct type_name final {
  10221. /**
  10222. * @brief Returns the name of a given type.
  10223. * @return The name of the given type.
  10224. */
  10225. [[nodiscard]] static constexpr std::string_view value() ENTT_NOEXCEPT {
  10226. return internal::type_name<Type>(0);
  10227. }
  10228. /*! @copydoc value */
  10229. [[nodiscard]] constexpr operator std::string_view() const ENTT_NOEXCEPT { return value(); }
  10230. };
  10231. /*! @brief Implementation specific information about a type. */
  10232. class type_info final {
  10233. template<typename>
  10234. friend type_info type_id() ENTT_NOEXCEPT;
  10235. type_info(id_type seq_v, id_type hash_v, std::string_view name_v) ENTT_NOEXCEPT
  10236. : seq_value{seq_v},
  10237. hash_value{hash_v},
  10238. name_value{name_v}
  10239. {}
  10240. public:
  10241. /*! @brief Default constructor. */
  10242. type_info() ENTT_NOEXCEPT
  10243. : type_info({}, {}, {})
  10244. {}
  10245. /*! @brief Default copy constructor. */
  10246. type_info(const type_info &) ENTT_NOEXCEPT = default;
  10247. /*! @brief Default move constructor. */
  10248. type_info(type_info &&) ENTT_NOEXCEPT = default;
  10249. /**
  10250. * @brief Default copy assignment operator.
  10251. * @return This type info object.
  10252. */
  10253. type_info & operator=(const type_info &) ENTT_NOEXCEPT = default;
  10254. /**
  10255. * @brief Default move assignment operator.
  10256. * @return This type info object.
  10257. */
  10258. type_info & operator=(type_info &&) ENTT_NOEXCEPT = default;
  10259. /**
  10260. * @brief Checks if a type info object is properly initialized.
  10261. * @return True if the object is properly initialized, false otherwise.
  10262. */
  10263. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  10264. return name_value.data() != nullptr;
  10265. }
  10266. /**
  10267. * @brief Type sequential identifier.
  10268. * @return Type sequential identifier.
  10269. */
  10270. [[nodiscard]] id_type seq() const ENTT_NOEXCEPT {
  10271. return seq_value;
  10272. }
  10273. /**
  10274. * @brief Type hash.
  10275. * @return Type hash.
  10276. */
  10277. [[nodiscard]] id_type hash() const ENTT_NOEXCEPT {
  10278. return hash_value;
  10279. }
  10280. /**
  10281. * @brief Type name.
  10282. * @return Type name.
  10283. */
  10284. [[nodiscard]] std::string_view name() const ENTT_NOEXCEPT {
  10285. return name_value;
  10286. }
  10287. /**
  10288. * @brief Compares the contents of two type info objects.
  10289. * @param other Object with which to compare.
  10290. * @return False if the two contents differ, true otherwise.
  10291. */
  10292. [[nodiscard]] bool operator==(const type_info &other) const ENTT_NOEXCEPT {
  10293. return hash_value == other.hash_value;
  10294. }
  10295. private:
  10296. id_type seq_value;
  10297. id_type hash_value;
  10298. std::string_view name_value;
  10299. };
  10300. /**
  10301. * @brief Compares the contents of two type info objects.
  10302. * @param lhs A type info object.
  10303. * @param rhs A type info object.
  10304. * @return True if the two contents differ, false otherwise.
  10305. */
  10306. [[nodiscard]] inline bool operator!=(const type_info &lhs, const type_info &rhs) ENTT_NOEXCEPT {
  10307. return !(lhs == rhs);
  10308. }
  10309. /**
  10310. * @brief Returns the type info object for a given type.
  10311. * @tparam Type Type for which to generate a type info object.
  10312. * @return The type info object for the given type.
  10313. */
  10314. template<typename Type>
  10315. [[nodiscard]] type_info type_id() ENTT_NOEXCEPT {
  10316. return type_info{
  10317. type_seq<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  10318. type_hash<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  10319. type_name<std::remove_cv_t<std::remove_reference_t<Type>>>::value()
  10320. };
  10321. }
  10322. }
  10323. #endif
  10324. // #include "../core/type_traits.hpp"
  10325. // #include "component.hpp"
  10326. // #include "entity.hpp"
  10327. // #include "fwd.hpp"
  10328. // #include "group.hpp"
  10329. #ifndef ENTT_ENTITY_GROUP_HPP
  10330. #define ENTT_ENTITY_GROUP_HPP
  10331. #include <tuple>
  10332. #include <utility>
  10333. #include <type_traits>
  10334. // #include "../config/config.h"
  10335. // #include "../core/type_traits.hpp"
  10336. // #include "entity.hpp"
  10337. // #include "fwd.hpp"
  10338. // #include "sparse_set.hpp"
  10339. // #include "storage.hpp"
  10340. // #include "utility.hpp"
  10341. namespace entt {
  10342. /**
  10343. * @brief Group.
  10344. *
  10345. * Primary template isn't defined on purpose. All the specializations give a
  10346. * compile-time error, but for a few reasonable cases.
  10347. */
  10348. template<typename...>
  10349. class basic_group;
  10350. /**
  10351. * @brief Non-owning group.
  10352. *
  10353. * A non-owning group returns all entities and only the entities that have at
  10354. * least the given components. Moreover, it's guaranteed that the entity list
  10355. * is tightly packed in memory for fast iterations.
  10356. *
  10357. * @b Important
  10358. *
  10359. * Iterators aren't invalidated if:
  10360. *
  10361. * * New instances of the given components are created and assigned to entities.
  10362. * * The entity currently pointed is modified (as an example, if one of the
  10363. * given components is removed from the entity to which the iterator points).
  10364. * * The entity currently pointed is destroyed.
  10365. *
  10366. * In all other cases, modifying the pools iterated by the group in any way
  10367. * invalidates all the iterators and using them results in undefined behavior.
  10368. *
  10369. * @note
  10370. * Groups share references to the underlying data structures of the registry
  10371. * that generated them. Therefore any change to the entities and to the
  10372. * components made by means of the registry are immediately reflected by all the
  10373. * groups.<br/>
  10374. * Moreover, sorting a non-owning group affects all the instances of the same
  10375. * group (it means that users don't have to call `sort` on each instance to sort
  10376. * all of them because they _share_ entities and components).
  10377. *
  10378. * @warning
  10379. * Lifetime of a group must not overcome that of the registry that generated it.
  10380. * In any other case, attempting to use a group results in undefined behavior.
  10381. *
  10382. * @tparam Entity A valid entity type (see entt_traits for more details).
  10383. * @tparam Exclude Types of components used to filter the group.
  10384. * @tparam Get Type of components observed by the group.
  10385. */
  10386. template<typename Entity, typename... Exclude, typename... Get>
  10387. class basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>> final {
  10388. /*! @brief A registry is allowed to create groups. */
  10389. friend class basic_registry<Entity>;
  10390. using basic_common_type = basic_sparse_set<Entity>;
  10391. template<typename Component>
  10392. using storage_type = constness_as_t<typename storage_traits<Entity, std::remove_const_t<Component>>::storage_type, Component>;
  10393. class iterable final {
  10394. template<typename It>
  10395. struct iterable_iterator final {
  10396. using difference_type = std::ptrdiff_t;
  10397. using value_type = decltype(std::tuple_cat(std::tuple<Entity>{}, std::declval<basic_group>().get({})));
  10398. using pointer = void;
  10399. using reference = value_type;
  10400. using iterator_category = std::input_iterator_tag;
  10401. template<typename... Args>
  10402. iterable_iterator(It from, const std::tuple<storage_type<Get> *...> &args) ENTT_NOEXCEPT
  10403. : it{from},
  10404. pools{args}
  10405. {}
  10406. iterable_iterator & operator++() ENTT_NOEXCEPT {
  10407. return ++it, *this;
  10408. }
  10409. iterable_iterator operator++(int) ENTT_NOEXCEPT {
  10410. iterable_iterator orig = *this;
  10411. return ++(*this), orig;
  10412. }
  10413. [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
  10414. const auto entt = *it;
  10415. return std::tuple_cat(std::make_tuple(entt), get_as_tuple(*std::get<storage_type<Get> *>(pools), entt)...);
  10416. }
  10417. [[nodiscard]] bool operator==(const iterable_iterator &other) const ENTT_NOEXCEPT {
  10418. return other.it == it;
  10419. }
  10420. [[nodiscard]] bool operator!=(const iterable_iterator &other) const ENTT_NOEXCEPT {
  10421. return !(*this == other);
  10422. }
  10423. private:
  10424. It it;
  10425. std::tuple<storage_type<Get> *...> pools;
  10426. };
  10427. public:
  10428. using iterator = iterable_iterator<typename basic_common_type::iterator>;
  10429. using reverse_iterator = iterable_iterator<typename basic_common_type::reverse_iterator>;
  10430. iterable(basic_common_type * const ref, const std::tuple<storage_type<Get> *...> &cpools)
  10431. : handler{ref},
  10432. pools{cpools}
  10433. {}
  10434. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  10435. return handler ? iterator{handler->begin(), pools} : iterator{{}, pools};
  10436. }
  10437. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  10438. return handler ? iterator{handler->end(), pools} : iterator{{}, pools};
  10439. }
  10440. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  10441. return handler ? reverse_iterator{handler->rbegin(), pools} : reverse_iterator{{}, pools};
  10442. }
  10443. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  10444. return handler ? reverse_iterator{handler->rend(), pools} : reverse_iterator{{}, pools};
  10445. }
  10446. private:
  10447. basic_common_type * const handler;
  10448. const std::tuple<storage_type<Get> *...> pools;
  10449. };
  10450. basic_group(basic_common_type &ref, storage_type<Get> &... gpool) ENTT_NOEXCEPT
  10451. : handler{&ref},
  10452. pools{&gpool...}
  10453. {}
  10454. public:
  10455. /*! @brief Underlying entity identifier. */
  10456. using entity_type = Entity;
  10457. /*! @brief Unsigned integer type. */
  10458. using size_type = std::size_t;
  10459. /*! @brief Random access iterator type. */
  10460. using iterator = typename basic_common_type::iterator;
  10461. /*! @brief Reversed iterator type. */
  10462. using reverse_iterator = typename basic_common_type::reverse_iterator;
  10463. /*! @brief Iterable group type. */
  10464. using iterable_group = iterable;
  10465. /*! @brief Default constructor to use to create empty, invalid groups. */
  10466. basic_group() ENTT_NOEXCEPT
  10467. : handler{}
  10468. {}
  10469. /**
  10470. * @brief Returns the number of entities that have the given components.
  10471. * @return Number of entities that have the given components.
  10472. */
  10473. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  10474. return *this ? handler->size() : size_type{};
  10475. }
  10476. /**
  10477. * @brief Returns the number of elements that a group has currently
  10478. * allocated space for.
  10479. * @return Capacity of the group.
  10480. */
  10481. [[nodiscard]] size_type capacity() const ENTT_NOEXCEPT {
  10482. return *this ? handler->capacity() : size_type{};
  10483. }
  10484. /*! @brief Requests the removal of unused capacity. */
  10485. void shrink_to_fit() {
  10486. if(*this) {
  10487. handler->shrink_to_fit();
  10488. }
  10489. }
  10490. /**
  10491. * @brief Checks whether a group is empty.
  10492. * @return True if the group is empty, false otherwise.
  10493. */
  10494. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  10495. return !*this || handler->empty();
  10496. }
  10497. /**
  10498. * @brief Direct access to the list of entities.
  10499. *
  10500. * The returned pointer is such that range `[data(), data() + size())` is
  10501. * always a valid range, even if the container is empty.
  10502. *
  10503. * @return A pointer to the array of entities.
  10504. */
  10505. [[nodiscard]] auto data() const ENTT_NOEXCEPT {
  10506. return *this ? handler->data() : nullptr;
  10507. }
  10508. /**
  10509. * @brief Returns an iterator to the first entity of the group.
  10510. *
  10511. * The returned iterator points to the first entity of the group. If the
  10512. * group is empty, the returned iterator will be equal to `end()`.
  10513. *
  10514. * @return An iterator to the first entity of the group.
  10515. */
  10516. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  10517. return *this ? handler->begin() : iterator{};
  10518. }
  10519. /**
  10520. * @brief Returns an iterator that is past the last entity of the group.
  10521. *
  10522. * The returned iterator points to the entity following the last entity of
  10523. * the group. Attempting to dereference the returned iterator results in
  10524. * undefined behavior.
  10525. *
  10526. * @return An iterator to the entity following the last entity of the
  10527. * group.
  10528. */
  10529. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  10530. return *this ? handler->end() : iterator{};
  10531. }
  10532. /**
  10533. * @brief Returns an iterator to the first entity of the reversed group.
  10534. *
  10535. * The returned iterator points to the first entity of the reversed group.
  10536. * If the group is empty, the returned iterator will be equal to `rend()`.
  10537. *
  10538. * @return An iterator to the first entity of the reversed group.
  10539. */
  10540. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  10541. return *this ? handler->rbegin() : reverse_iterator{};
  10542. }
  10543. /**
  10544. * @brief Returns an iterator that is past the last entity of the reversed
  10545. * group.
  10546. *
  10547. * The returned iterator points to the entity following the last entity of
  10548. * the reversed group. Attempting to dereference the returned iterator
  10549. * results in undefined behavior.
  10550. *
  10551. * @return An iterator to the entity following the last entity of the
  10552. * reversed group.
  10553. */
  10554. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  10555. return *this ? handler->rend() : reverse_iterator{};
  10556. }
  10557. /**
  10558. * @brief Returns the first entity of the group, if any.
  10559. * @return The first entity of the group if one exists, the null entity
  10560. * otherwise.
  10561. */
  10562. [[nodiscard]] entity_type front() const {
  10563. const auto it = begin();
  10564. return it != end() ? *it : null;
  10565. }
  10566. /**
  10567. * @brief Returns the last entity of the group, if any.
  10568. * @return The last entity of the group if one exists, the null entity
  10569. * otherwise.
  10570. */
  10571. [[nodiscard]] entity_type back() const {
  10572. const auto it = rbegin();
  10573. return it != rend() ? *it : null;
  10574. }
  10575. /**
  10576. * @brief Finds an entity.
  10577. * @param entt A valid entity identifier.
  10578. * @return An iterator to the given entity if it's found, past the end
  10579. * iterator otherwise.
  10580. */
  10581. [[nodiscard]] iterator find(const entity_type entt) const {
  10582. const auto it = *this ? handler->find(entt) : iterator{};
  10583. return it != end() && *it == entt ? it : end();
  10584. }
  10585. /**
  10586. * @brief Returns the identifier that occupies the given position.
  10587. * @param pos Position of the element to return.
  10588. * @return The identifier that occupies the given position.
  10589. */
  10590. [[nodiscard]] entity_type operator[](const size_type pos) const {
  10591. return begin()[pos];
  10592. }
  10593. /**
  10594. * @brief Checks if a group is properly initialized.
  10595. * @return True if the group is properly initialized, false otherwise.
  10596. */
  10597. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  10598. return handler != nullptr;
  10599. }
  10600. /**
  10601. * @brief Checks if a group contains an entity.
  10602. * @param entt A valid entity identifier.
  10603. * @return True if the group contains the given entity, false otherwise.
  10604. */
  10605. [[nodiscard]] bool contains(const entity_type entt) const {
  10606. return *this && handler->contains(entt);
  10607. }
  10608. /**
  10609. * @brief Returns the components assigned to the given entity.
  10610. *
  10611. * Prefer this function instead of `registry::get` during iterations. It has
  10612. * far better performance than its counterpart.
  10613. *
  10614. * @warning
  10615. * Attempting to use an invalid component type results in a compilation
  10616. * error. Attempting to use an entity that doesn't belong to the group
  10617. * results in undefined behavior.
  10618. *
  10619. * @tparam Component Types of components to get.
  10620. * @param entt A valid entity identifier.
  10621. * @return The components assigned to the entity.
  10622. */
  10623. template<typename... Component>
  10624. [[nodiscard]] decltype(auto) get(const entity_type entt) const {
  10625. ENTT_ASSERT(contains(entt), "Group does not contain entity");
  10626. if constexpr(sizeof...(Component) == 0) {
  10627. return std::tuple_cat(get_as_tuple(*std::get<storage_type<Get> *>(pools), entt)...);
  10628. } else if constexpr(sizeof...(Component) == 1) {
  10629. return (std::get<storage_type<Component> *>(pools)->get(entt), ...);
  10630. } else {
  10631. return std::tuple_cat(get_as_tuple(*std::get<storage_type<Component> *>(pools), entt)...);
  10632. }
  10633. }
  10634. /**
  10635. * @brief Iterates entities and components and applies the given function
  10636. * object to them.
  10637. *
  10638. * The function object is invoked for each entity. It is provided with the
  10639. * entity itself and a set of references to non-empty components. The
  10640. * _constness_ of the components is as requested.<br/>
  10641. * The signature of the function must be equivalent to one of the following
  10642. * forms:
  10643. *
  10644. * @code{.cpp}
  10645. * void(const entity_type, Type &...);
  10646. * void(Type &...);
  10647. * @endcode
  10648. *
  10649. * @note
  10650. * Empty types aren't explicitly instantiated and therefore they are never
  10651. * returned during iterations.
  10652. *
  10653. * @tparam Func Type of the function object to invoke.
  10654. * @param func A valid function object.
  10655. */
  10656. template<typename Func>
  10657. void each(Func func) const {
  10658. for(const auto entt: *this) {
  10659. if constexpr(is_applicable_v<Func, decltype(std::tuple_cat(std::tuple<entity_type>{}, std::declval<basic_group>().get({})))>) {
  10660. std::apply(func, std::tuple_cat(std::make_tuple(entt), get(entt)));
  10661. } else {
  10662. std::apply(func, get(entt));
  10663. }
  10664. }
  10665. }
  10666. /**
  10667. * @brief Returns an iterable object to use to _visit_ the group.
  10668. *
  10669. * The iterable object returns tuples that contain the current entity and a
  10670. * set of references to its non-empty components. The _constness_ of the
  10671. * components is as requested.
  10672. *
  10673. * @note
  10674. * Empty types aren't explicitly instantiated and therefore they are never
  10675. * returned during iterations.
  10676. *
  10677. * @return An iterable object to use to _visit_ the group.
  10678. */
  10679. [[nodiscard]] iterable_group each() const ENTT_NOEXCEPT {
  10680. return iterable_group{handler, pools};
  10681. }
  10682. /**
  10683. * @brief Sort a group according to the given comparison function.
  10684. *
  10685. * Sort the group so that iterating it with a couple of iterators returns
  10686. * entities and components in the expected order. See `begin` and `end` for
  10687. * more details.
  10688. *
  10689. * The comparison function object must return `true` if the first element
  10690. * is _less_ than the second one, `false` otherwise. The signature of the
  10691. * comparison function should be equivalent to one of the following:
  10692. *
  10693. * @code{.cpp}
  10694. * bool(std::tuple<Component &...>, std::tuple<Component &...>);
  10695. * bool(const Component &..., const Component &...);
  10696. * bool(const Entity, const Entity);
  10697. * @endcode
  10698. *
  10699. * Where `Component` are such that they are iterated by the group.<br/>
  10700. * Moreover, the comparison function object shall induce a
  10701. * _strict weak ordering_ on the values.
  10702. *
  10703. * The sort function oject must offer a member function template
  10704. * `operator()` that accepts three arguments:
  10705. *
  10706. * * An iterator to the first element of the range to sort.
  10707. * * An iterator past the last element of the range to sort.
  10708. * * A comparison function to use to compare the elements.
  10709. *
  10710. * @tparam Component Optional types of components to compare.
  10711. * @tparam Compare Type of comparison function object.
  10712. * @tparam Sort Type of sort function object.
  10713. * @tparam Args Types of arguments to forward to the sort function object.
  10714. * @param compare A valid comparison function object.
  10715. * @param algo A valid sort function object.
  10716. * @param args Arguments to forward to the sort function object, if any.
  10717. */
  10718. template<typename... Component, typename Compare, typename Sort = std_sort, typename... Args>
  10719. void sort(Compare compare, Sort algo = Sort{}, Args &&... args) {
  10720. if(*this) {
  10721. if constexpr(sizeof...(Component) == 0) {
  10722. static_assert(std::is_invocable_v<Compare, const entity_type, const entity_type>, "Invalid comparison function");
  10723. handler->sort(std::move(compare), std::move(algo), std::forward<Args>(args)...);
  10724. } else if constexpr(sizeof...(Component) == 1) {
  10725. handler->sort([this, compare = std::move(compare)](const entity_type lhs, const entity_type rhs) {
  10726. return compare((std::get<storage_type<Component> *>(pools)->get(lhs), ...), (std::get<storage_type<Component> *>(pools)->get(rhs), ...));
  10727. }, std::move(algo), std::forward<Args>(args)...);
  10728. } else {
  10729. handler->sort([this, compare = std::move(compare)](const entity_type lhs, const entity_type rhs) {
  10730. return compare(std::forward_as_tuple(std::get<storage_type<Component> *>(pools)->get(lhs)...), std::forward_as_tuple(std::get<storage_type<Component> *>(pools)->get(rhs)...));
  10731. }, std::move(algo), std::forward<Args>(args)...);
  10732. }
  10733. }
  10734. }
  10735. /**
  10736. * @brief Sort the shared pool of entities according to the given component.
  10737. *
  10738. * Non-owning groups of the same type share with the registry a pool of
  10739. * entities with its own order that doesn't depend on the order of any pool
  10740. * of components. Users can order the underlying data structure so that it
  10741. * respects the order of the pool of the given component.
  10742. *
  10743. * @note
  10744. * The shared pool of entities and thus its order is affected by the changes
  10745. * to each and every pool that it tracks. Therefore changes to those pools
  10746. * can quickly ruin the order imposed to the pool of entities shared between
  10747. * the non-owning groups.
  10748. *
  10749. * @tparam Component Type of component to use to impose the order.
  10750. */
  10751. template<typename Component>
  10752. void sort() const {
  10753. if(*this) {
  10754. handler->respect(*std::get<storage_type<Component> *>(pools));
  10755. }
  10756. }
  10757. private:
  10758. basic_common_type * const handler;
  10759. const std::tuple<storage_type<Get> *...> pools;
  10760. };
  10761. /**
  10762. * @brief Owning group.
  10763. *
  10764. * Owning groups return all entities and only the entities that have at least
  10765. * the given components. Moreover:
  10766. *
  10767. * * It's guaranteed that the entity list is tightly packed in memory for fast
  10768. * iterations.
  10769. * * It's guaranteed that the lists of owned components are tightly packed in
  10770. * memory for even faster iterations and to allow direct access.
  10771. * * They stay true to the order of the owned components and all instances have
  10772. * the same order in memory.
  10773. *
  10774. * The more types of components are owned by a group, the faster it is to
  10775. * iterate them.
  10776. *
  10777. * @b Important
  10778. *
  10779. * Iterators aren't invalidated if:
  10780. *
  10781. * * New instances of the given components are created and assigned to entities.
  10782. * * The entity currently pointed is modified (as an example, if one of the
  10783. * given components is removed from the entity to which the iterator points).
  10784. * * The entity currently pointed is destroyed.
  10785. *
  10786. * In all other cases, modifying the pools iterated by the group in any way
  10787. * invalidates all the iterators and using them results in undefined behavior.
  10788. *
  10789. * @note
  10790. * Groups share references to the underlying data structures of the registry
  10791. * that generated them. Therefore any change to the entities and to the
  10792. * components made by means of the registry are immediately reflected by all the
  10793. * groups.
  10794. * Moreover, sorting an owning group affects all the instance of the same group
  10795. * (it means that users don't have to call `sort` on each instance to sort all
  10796. * of them because they share the underlying data structure).
  10797. *
  10798. * @warning
  10799. * Lifetime of a group must not overcome that of the registry that generated it.
  10800. * In any other case, attempting to use a group results in undefined behavior.
  10801. *
  10802. * @tparam Entity A valid entity type (see entt_traits for more details).
  10803. * @tparam Exclude Types of components used to filter the group.
  10804. * @tparam Get Types of components observed by the group.
  10805. * @tparam Owned Types of components owned by the group.
  10806. */
  10807. template<typename Entity, typename... Exclude, typename... Get, typename... Owned>
  10808. class basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>, Owned...> final {
  10809. /*! @brief A registry is allowed to create groups. */
  10810. friend class basic_registry<Entity>;
  10811. using basic_common_type = basic_sparse_set<Entity>;
  10812. template<typename Component>
  10813. using storage_type = constness_as_t<typename storage_traits<Entity, std::remove_const_t<Component>>::storage_type, Component>;
  10814. class iterable final {
  10815. template<typename, typename>
  10816. struct iterable_iterator;
  10817. template<typename It, typename... OIt>
  10818. struct iterable_iterator<It, type_list<OIt...>> final {
  10819. using difference_type = std::ptrdiff_t;
  10820. using value_type = decltype(std::tuple_cat(std::tuple<Entity>{}, std::declval<basic_group>().get({})));
  10821. using pointer = void;
  10822. using reference = value_type;
  10823. using iterator_category = std::input_iterator_tag;
  10824. template<typename... Other>
  10825. iterable_iterator(It from, const std::tuple<Other...> &other, const std::tuple<storage_type<Get> *...> &cpools) ENTT_NOEXCEPT
  10826. : it{from},
  10827. owned{std::get<OIt>(other)...},
  10828. get{cpools}
  10829. {}
  10830. iterable_iterator & operator++() ENTT_NOEXCEPT {
  10831. return ++it, (++std::get<OIt>(owned), ...), *this;
  10832. }
  10833. iterable_iterator operator++(int) ENTT_NOEXCEPT {
  10834. iterable_iterator orig = *this;
  10835. return ++(*this), orig;
  10836. }
  10837. [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
  10838. return std::tuple_cat(
  10839. std::make_tuple(*it),
  10840. std::forward_as_tuple(*std::get<OIt>(owned)...),
  10841. get_as_tuple(*std::get<storage_type<Get> *>(get), *it)...
  10842. );
  10843. }
  10844. [[nodiscard]] bool operator==(const iterable_iterator &other) const ENTT_NOEXCEPT {
  10845. return other.it == it;
  10846. }
  10847. [[nodiscard]] bool operator!=(const iterable_iterator &other) const ENTT_NOEXCEPT {
  10848. return !(*this == other);
  10849. }
  10850. private:
  10851. It it;
  10852. std::tuple<OIt...> owned;
  10853. std::tuple<storage_type<Get> *...> get;
  10854. };
  10855. public:
  10856. using iterator = iterable_iterator<
  10857. typename basic_common_type::iterator,
  10858. type_list_cat_t<std::conditional_t<std::is_void_v<decltype(std::declval<storage_type<Owned>>().get({}))>, type_list<>, type_list<decltype(std::declval<storage_type<Owned>>().end())>>...>
  10859. >;
  10860. using reverse_iterator = iterable_iterator<
  10861. typename basic_common_type::reverse_iterator,
  10862. type_list_cat_t<std::conditional_t<std::is_void_v<decltype(std::declval<storage_type<Owned>>().get({}))>, type_list<>, type_list<decltype(std::declval<storage_type<Owned>>().rbegin())>>...>
  10863. >;
  10864. iterable(std::tuple<storage_type<Owned> *..., storage_type<Get> *...> cpools, const std::size_t * const extent)
  10865. : pools{cpools},
  10866. length{extent}
  10867. {}
  10868. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  10869. return length ? iterator{
  10870. std::get<0>(pools)->basic_common_type::end() - *length,
  10871. std::make_tuple((std::get<storage_type<Owned> *>(pools)->end() - *length)...),
  10872. std::make_tuple(std::get<storage_type<Get> *>(pools)...)
  10873. } : iterator{{}, std::make_tuple(decltype(std::get<storage_type<Owned> *>(pools)->end()){}...), std::make_tuple(std::get<storage_type<Get> *>(pools)...)};
  10874. }
  10875. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  10876. return length ? iterator{
  10877. std::get<0>(pools)->basic_common_type::end(),
  10878. std::make_tuple((std::get<storage_type<Owned> *>(pools)->end())...),
  10879. std::make_tuple(std::get<storage_type<Get> *>(pools)...)
  10880. } : iterator{{}, std::make_tuple(decltype(std::get<storage_type<Owned> *>(pools)->end()){}...), std::make_tuple(std::get<storage_type<Get> *>(pools)...)};
  10881. }
  10882. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  10883. return length ? reverse_iterator{
  10884. std::get<0>(pools)->basic_common_type::rbegin(),
  10885. std::make_tuple((std::get<storage_type<Owned> *>(pools)->rbegin())...),
  10886. std::make_tuple(std::get<storage_type<Get> *>(pools)...)
  10887. } : reverse_iterator{{}, std::make_tuple(decltype(std::get<storage_type<Owned> *>(pools)->rbegin()){}...), std::make_tuple(std::get<storage_type<Get> *>(pools)...)};
  10888. }
  10889. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  10890. return length ? reverse_iterator{
  10891. std::get<0>(pools)->basic_common_type::rbegin() + *length,
  10892. std::make_tuple((std::get<storage_type<Owned> *>(pools)->rbegin() + *length)...),
  10893. std::make_tuple(std::get<storage_type<Get> *>(pools)...)
  10894. } : reverse_iterator{{}, std::make_tuple(decltype(std::get<storage_type<Owned> *>(pools)->rbegin()){}...), std::make_tuple(std::get<storage_type<Get> *>(pools)...)};
  10895. }
  10896. private:
  10897. const std::tuple<storage_type<Owned> *..., storage_type<Get> *...> pools;
  10898. const std::size_t * const length;
  10899. };
  10900. basic_group(const std::size_t &extent, storage_type<Owned> &... opool, storage_type<Get> &... gpool) ENTT_NOEXCEPT
  10901. : pools{&opool..., &gpool...},
  10902. length{&extent}
  10903. {}
  10904. public:
  10905. /*! @brief Underlying entity identifier. */
  10906. using entity_type = Entity;
  10907. /*! @brief Unsigned integer type. */
  10908. using size_type = std::size_t;
  10909. /*! @brief Random access iterator type. */
  10910. using iterator = typename basic_common_type::iterator;
  10911. /*! @brief Reversed iterator type. */
  10912. using reverse_iterator = typename basic_common_type::reverse_iterator;
  10913. /*! @brief Iterable group type. */
  10914. using iterable_group = iterable;
  10915. /*! @brief Default constructor to use to create empty, invalid groups. */
  10916. basic_group() ENTT_NOEXCEPT
  10917. : length{}
  10918. {}
  10919. /**
  10920. * @brief Returns the number of entities that have the given components.
  10921. * @return Number of entities that have the given components.
  10922. */
  10923. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  10924. return *this ? *length : size_type{};
  10925. }
  10926. /**
  10927. * @brief Checks whether a group is empty.
  10928. * @return True if the group is empty, false otherwise.
  10929. */
  10930. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  10931. return !*this || !*length;
  10932. }
  10933. /**
  10934. * @brief Direct access to the raw representation offered by the storage.
  10935. *
  10936. * @warning
  10937. * This function is only available for owned types.
  10938. *
  10939. * @tparam Component Type of component in which one is interested.
  10940. * @return A pointer to the array of components.
  10941. */
  10942. template<typename Component>
  10943. [[nodiscard]] auto raw() const ENTT_NOEXCEPT {
  10944. static_assert((std::is_same_v<Component, Owned> || ...), "Non-owned type");
  10945. auto *cpool = std::get<storage_type<Component> *>(pools);
  10946. return cpool ? cpool->raw() : decltype(cpool->raw()){};
  10947. }
  10948. /**
  10949. * @brief Direct access to the list of entities.
  10950. *
  10951. * The returned pointer is such that range `[data(), data() + size())` is
  10952. * always a valid range, even if the container is empty.
  10953. *
  10954. * @return A pointer to the array of entities.
  10955. */
  10956. [[nodiscard]] auto data() const ENTT_NOEXCEPT {
  10957. return *this ? std::get<0>(pools)->data() : nullptr;
  10958. }
  10959. /**
  10960. * @brief Returns an iterator to the first entity of the group.
  10961. *
  10962. * The returned iterator points to the first entity of the group. If the
  10963. * group is empty, the returned iterator will be equal to `end()`.
  10964. *
  10965. * @return An iterator to the first entity of the group.
  10966. */
  10967. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  10968. return *this ? (std::get<0>(pools)->basic_common_type::end() - *length) : iterator{};
  10969. }
  10970. /**
  10971. * @brief Returns an iterator that is past the last entity of the group.
  10972. *
  10973. * The returned iterator points to the entity following the last entity of
  10974. * the group. Attempting to dereference the returned iterator results in
  10975. * undefined behavior.
  10976. *
  10977. * @return An iterator to the entity following the last entity of the
  10978. * group.
  10979. */
  10980. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  10981. return *this ? std::get<0>(pools)->basic_common_type::end() : iterator{};
  10982. }
  10983. /**
  10984. * @brief Returns an iterator to the first entity of the reversed group.
  10985. *
  10986. * The returned iterator points to the first entity of the reversed group.
  10987. * If the group is empty, the returned iterator will be equal to `rend()`.
  10988. *
  10989. * @return An iterator to the first entity of the reversed group.
  10990. */
  10991. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  10992. return *this ? std::get<0>(pools)->basic_common_type::rbegin() : reverse_iterator{};
  10993. }
  10994. /**
  10995. * @brief Returns an iterator that is past the last entity of the reversed
  10996. * group.
  10997. *
  10998. * The returned iterator points to the entity following the last entity of
  10999. * the reversed group. Attempting to dereference the returned iterator
  11000. * results in undefined behavior.
  11001. *
  11002. * @return An iterator to the entity following the last entity of the
  11003. * reversed group.
  11004. */
  11005. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  11006. return *this ? (std::get<0>(pools)->basic_common_type::rbegin() + *length) : reverse_iterator{};
  11007. }
  11008. /**
  11009. * @brief Returns the first entity of the group, if any.
  11010. * @return The first entity of the group if one exists, the null entity
  11011. * otherwise.
  11012. */
  11013. [[nodiscard]] entity_type front() const {
  11014. const auto it = begin();
  11015. return it != end() ? *it : null;
  11016. }
  11017. /**
  11018. * @brief Returns the last entity of the group, if any.
  11019. * @return The last entity of the group if one exists, the null entity
  11020. * otherwise.
  11021. */
  11022. [[nodiscard]] entity_type back() const {
  11023. const auto it = rbegin();
  11024. return it != rend() ? *it : null;
  11025. }
  11026. /**
  11027. * @brief Finds an entity.
  11028. * @param entt A valid entity identifier.
  11029. * @return An iterator to the given entity if it's found, past the end
  11030. * iterator otherwise.
  11031. */
  11032. [[nodiscard]] iterator find(const entity_type entt) const {
  11033. const auto it = *this ? std::get<0>(pools)->find(entt) : iterator{};
  11034. return it != end() && it >= begin() && *it == entt ? it : end();
  11035. }
  11036. /**
  11037. * @brief Returns the identifier that occupies the given position.
  11038. * @param pos Position of the element to return.
  11039. * @return The identifier that occupies the given position.
  11040. */
  11041. [[nodiscard]] entity_type operator[](const size_type pos) const {
  11042. return begin()[pos];
  11043. }
  11044. /**
  11045. * @brief Checks if a group is properly initialized.
  11046. * @return True if the group is properly initialized, false otherwise.
  11047. */
  11048. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  11049. return length != nullptr;
  11050. }
  11051. /**
  11052. * @brief Checks if a group contains an entity.
  11053. * @param entt A valid entity identifier.
  11054. * @return True if the group contains the given entity, false otherwise.
  11055. */
  11056. [[nodiscard]] bool contains(const entity_type entt) const {
  11057. return *this && std::get<0>(pools)->contains(entt) && (std::get<0>(pools)->index(entt) < (*length));
  11058. }
  11059. /**
  11060. * @brief Returns the components assigned to the given entity.
  11061. *
  11062. * Prefer this function instead of `registry::get` during iterations. It has
  11063. * far better performance than its counterpart.
  11064. *
  11065. * @warning
  11066. * Attempting to use an invalid component type results in a compilation
  11067. * error. Attempting to use an entity that doesn't belong to the group
  11068. * results in undefined behavior.
  11069. *
  11070. * @tparam Component Types of components to get.
  11071. * @param entt A valid entity identifier.
  11072. * @return The components assigned to the entity.
  11073. */
  11074. template<typename... Component>
  11075. [[nodiscard]] decltype(auto) get(const entity_type entt) const {
  11076. ENTT_ASSERT(contains(entt), "Group does not contain entity");
  11077. if constexpr(sizeof...(Component) == 0) {
  11078. return std::tuple_cat(get_as_tuple(*std::get<storage_type<Owned> *>(pools), entt)..., get_as_tuple(*std::get<storage_type<Get> *>(pools), entt)...);
  11079. } else if constexpr(sizeof...(Component) == 1) {
  11080. return (std::get<storage_type<Component> *>(pools)->get(entt), ...);
  11081. } else {
  11082. return std::tuple_cat(get_as_tuple(*std::get<storage_type<Component> *>(pools), entt)...);
  11083. }
  11084. }
  11085. /**
  11086. * @brief Iterates entities and components and applies the given function
  11087. * object to them.
  11088. *
  11089. * The function object is invoked for each entity. It is provided with the
  11090. * entity itself and a set of references to non-empty components. The
  11091. * _constness_ of the components is as requested.<br/>
  11092. * The signature of the function must be equivalent to one of the following
  11093. * forms:
  11094. *
  11095. * @code{.cpp}
  11096. * void(const entity_type, Type &...);
  11097. * void(Type &...);
  11098. * @endcode
  11099. *
  11100. * @note
  11101. * Empty types aren't explicitly instantiated and therefore they are never
  11102. * returned during iterations.
  11103. *
  11104. * @tparam Func Type of the function object to invoke.
  11105. * @param func A valid function object.
  11106. */
  11107. template<typename Func>
  11108. void each(Func func) const {
  11109. for(auto args: each()) {
  11110. if constexpr(is_applicable_v<Func, decltype(std::tuple_cat(std::tuple<entity_type>{}, std::declval<basic_group>().get({})))>) {
  11111. std::apply(func, args);
  11112. } else {
  11113. std::apply([&func](auto, auto &&... less) { func(std::forward<decltype(less)>(less)...); }, args);
  11114. }
  11115. }
  11116. }
  11117. /**
  11118. * @brief Returns an iterable object to use to _visit_ the group.
  11119. *
  11120. * The iterable object returns tuples that contain the current entity and a
  11121. * set of references to its non-empty components. The _constness_ of the
  11122. * components is as requested.
  11123. *
  11124. * @note
  11125. * Empty types aren't explicitly instantiated and therefore they are never
  11126. * returned during iterations.
  11127. *
  11128. * @return An iterable object to use to _visit_ the group.
  11129. */
  11130. [[nodiscard]] iterable_group each() const ENTT_NOEXCEPT {
  11131. return iterable_group{pools, length};
  11132. }
  11133. /**
  11134. * @brief Sort a group according to the given comparison function.
  11135. *
  11136. * Sort the group so that iterating it with a couple of iterators returns
  11137. * entities and components in the expected order. See `begin` and `end` for
  11138. * more details.
  11139. *
  11140. * The comparison function object must return `true` if the first element
  11141. * is _less_ than the second one, `false` otherwise. The signature of the
  11142. * comparison function should be equivalent to one of the following:
  11143. *
  11144. * @code{.cpp}
  11145. * bool(std::tuple<Component &...>, std::tuple<Component &...>);
  11146. * bool(const Component &, const Component &);
  11147. * bool(const Entity, const Entity);
  11148. * @endcode
  11149. *
  11150. * Where `Component` are either owned types or not but still such that they
  11151. * are iterated by the group.<br/>
  11152. * Moreover, the comparison function object shall induce a
  11153. * _strict weak ordering_ on the values.
  11154. *
  11155. * The sort function oject must offer a member function template
  11156. * `operator()` that accepts three arguments:
  11157. *
  11158. * * An iterator to the first element of the range to sort.
  11159. * * An iterator past the last element of the range to sort.
  11160. * * A comparison function to use to compare the elements.
  11161. *
  11162. * @tparam Component Optional types of components to compare.
  11163. * @tparam Compare Type of comparison function object.
  11164. * @tparam Sort Type of sort function object.
  11165. * @tparam Args Types of arguments to forward to the sort function object.
  11166. * @param compare A valid comparison function object.
  11167. * @param algo A valid sort function object.
  11168. * @param args Arguments to forward to the sort function object, if any.
  11169. */
  11170. template<typename... Component, typename Compare, typename Sort = std_sort, typename... Args>
  11171. void sort(Compare compare, Sort algo = Sort{}, Args &&... args) const {
  11172. auto *cpool = std::get<0>(pools);
  11173. if constexpr(sizeof...(Component) == 0) {
  11174. static_assert(std::is_invocable_v<Compare, const entity_type, const entity_type>, "Invalid comparison function");
  11175. cpool->sort_n(*length, std::move(compare), std::move(algo), std::forward<Args>(args)...);
  11176. } else if constexpr(sizeof...(Component) == 1) {
  11177. cpool->sort_n(*length, [this, compare = std::move(compare)](const entity_type lhs, const entity_type rhs) {
  11178. return compare((std::get<storage_type<Component> *>(pools)->get(lhs), ...), (std::get<storage_type<Component> *>(pools)->get(rhs), ...));
  11179. }, std::move(algo), std::forward<Args>(args)...);
  11180. } else {
  11181. cpool->sort_n(*length, [this, compare = std::move(compare)](const entity_type lhs, const entity_type rhs) {
  11182. return compare(std::forward_as_tuple(std::get<storage_type<Component> *>(pools)->get(lhs)...), std::forward_as_tuple(std::get<storage_type<Component> *>(pools)->get(rhs)...));
  11183. }, std::move(algo), std::forward<Args>(args)...);
  11184. }
  11185. [this](auto *head, auto *... other) {
  11186. for(auto next = *length; next; --next) {
  11187. const auto pos = next - 1;
  11188. [[maybe_unused]] const auto entt = head->data()[pos];
  11189. (other->swap(other->data()[pos], entt), ...);
  11190. }
  11191. }(std::get<storage_type<Owned> *>(pools)...);
  11192. }
  11193. private:
  11194. const std::tuple<storage_type<Owned> *..., storage_type<Get> *...> pools;
  11195. const size_type * const length;
  11196. };
  11197. }
  11198. #endif
  11199. // #include "poly_storage.hpp"
  11200. #ifndef ENTT_ENTITY_POLY_STORAGE_HPP
  11201. #define ENTT_ENTITY_POLY_STORAGE_HPP
  11202. #include <cstddef>
  11203. #include <tuple>
  11204. // #include "../core/type_info.hpp"
  11205. // #include "../core/type_traits.hpp"
  11206. // #include "../poly/poly.hpp"
  11207. #ifndef ENTT_POLY_POLY_HPP
  11208. #define ENTT_POLY_POLY_HPP
  11209. #include <cstddef>
  11210. #include <functional>
  11211. #include <tuple>
  11212. #include <type_traits>
  11213. #include <utility>
  11214. // #include "../config/config.h"
  11215. #ifndef ENTT_CONFIG_CONFIG_H
  11216. #define ENTT_CONFIG_CONFIG_H
  11217. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  11218. # define ENTT_NOEXCEPT noexcept
  11219. # define ENTT_THROW throw
  11220. # define ENTT_TRY try
  11221. # define ENTT_CATCH catch(...)
  11222. #else
  11223. # define ENTT_NOEXCEPT
  11224. # define ENTT_THROW
  11225. # define ENTT_TRY if(true)
  11226. # define ENTT_CATCH if(false)
  11227. #endif
  11228. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  11229. # include <new>
  11230. # define ENTT_LAUNDER(expr) std::launder(expr)
  11231. #else
  11232. # define ENTT_LAUNDER(expr) expr
  11233. #endif
  11234. #ifndef ENTT_USE_ATOMIC
  11235. # define ENTT_MAYBE_ATOMIC(Type) Type
  11236. #else
  11237. # include <atomic>
  11238. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  11239. #endif
  11240. #ifndef ENTT_ID_TYPE
  11241. # include <cstdint>
  11242. # define ENTT_ID_TYPE std::uint32_t
  11243. #endif
  11244. #ifdef ENTT_SPARSE_PAGE
  11245. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  11246. #else
  11247. # define ENTT_SPARSE_PAGE 4096
  11248. #endif
  11249. #ifdef ENTT_PACKED_PAGE
  11250. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  11251. #else
  11252. # define ENTT_PACKED_PAGE 1024
  11253. #endif
  11254. #ifdef ENTT_DISABLE_ASSERT
  11255. # undef ENTT_ASSERT
  11256. # define ENTT_ASSERT(...) (void(0))
  11257. #elif !defined ENTT_ASSERT
  11258. # include <cassert>
  11259. # define ENTT_ASSERT(condition, ...) assert(condition)
  11260. #endif
  11261. #ifdef ENTT_NO_ETO
  11262. # include <type_traits>
  11263. # define ENTT_IGNORE_IF_EMPTY std::false_type
  11264. #else
  11265. # include <type_traits>
  11266. # define ENTT_IGNORE_IF_EMPTY std::true_type
  11267. #endif
  11268. #ifndef ENTT_STANDARD_CPP
  11269. # if defined __clang__ || defined __GNUC__
  11270. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  11271. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  11272. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  11273. # elif defined _MSC_VER
  11274. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  11275. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  11276. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  11277. # endif
  11278. #endif
  11279. #endif
  11280. // #include "../core/any.hpp"
  11281. #ifndef ENTT_CORE_ANY_HPP
  11282. #define ENTT_CORE_ANY_HPP
  11283. #include <cstddef>
  11284. #include <functional>
  11285. #include <memory>
  11286. #include <type_traits>
  11287. #include <utility>
  11288. // #include "../config/config.h"
  11289. #ifndef ENTT_CONFIG_CONFIG_H
  11290. #define ENTT_CONFIG_CONFIG_H
  11291. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  11292. # define ENTT_NOEXCEPT noexcept
  11293. # define ENTT_THROW throw
  11294. # define ENTT_TRY try
  11295. # define ENTT_CATCH catch(...)
  11296. #else
  11297. # define ENTT_NOEXCEPT
  11298. # define ENTT_THROW
  11299. # define ENTT_TRY if(true)
  11300. # define ENTT_CATCH if(false)
  11301. #endif
  11302. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  11303. # include <new>
  11304. # define ENTT_LAUNDER(expr) std::launder(expr)
  11305. #else
  11306. # define ENTT_LAUNDER(expr) expr
  11307. #endif
  11308. #ifndef ENTT_USE_ATOMIC
  11309. # define ENTT_MAYBE_ATOMIC(Type) Type
  11310. #else
  11311. # include <atomic>
  11312. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  11313. #endif
  11314. #ifndef ENTT_ID_TYPE
  11315. # include <cstdint>
  11316. # define ENTT_ID_TYPE std::uint32_t
  11317. #endif
  11318. #ifdef ENTT_SPARSE_PAGE
  11319. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  11320. #else
  11321. # define ENTT_SPARSE_PAGE 4096
  11322. #endif
  11323. #ifdef ENTT_PACKED_PAGE
  11324. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  11325. #else
  11326. # define ENTT_PACKED_PAGE 1024
  11327. #endif
  11328. #ifdef ENTT_DISABLE_ASSERT
  11329. # undef ENTT_ASSERT
  11330. # define ENTT_ASSERT(...) (void(0))
  11331. #elif !defined ENTT_ASSERT
  11332. # include <cassert>
  11333. # define ENTT_ASSERT(condition, ...) assert(condition)
  11334. #endif
  11335. #ifdef ENTT_NO_ETO
  11336. # include <type_traits>
  11337. # define ENTT_IGNORE_IF_EMPTY std::false_type
  11338. #else
  11339. # include <type_traits>
  11340. # define ENTT_IGNORE_IF_EMPTY std::true_type
  11341. #endif
  11342. #ifndef ENTT_STANDARD_CPP
  11343. # if defined __clang__ || defined __GNUC__
  11344. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  11345. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  11346. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  11347. # elif defined _MSC_VER
  11348. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  11349. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  11350. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  11351. # endif
  11352. #endif
  11353. #endif
  11354. // #include "../core/utility.hpp"
  11355. #ifndef ENTT_CORE_UTILITY_HPP
  11356. #define ENTT_CORE_UTILITY_HPP
  11357. #include <utility>
  11358. // #include "../config/config.h"
  11359. #ifndef ENTT_CONFIG_CONFIG_H
  11360. #define ENTT_CONFIG_CONFIG_H
  11361. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  11362. # define ENTT_NOEXCEPT noexcept
  11363. # define ENTT_THROW throw
  11364. # define ENTT_TRY try
  11365. # define ENTT_CATCH catch(...)
  11366. #else
  11367. # define ENTT_NOEXCEPT
  11368. # define ENTT_THROW
  11369. # define ENTT_TRY if(true)
  11370. # define ENTT_CATCH if(false)
  11371. #endif
  11372. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  11373. # include <new>
  11374. # define ENTT_LAUNDER(expr) std::launder(expr)
  11375. #else
  11376. # define ENTT_LAUNDER(expr) expr
  11377. #endif
  11378. #ifndef ENTT_USE_ATOMIC
  11379. # define ENTT_MAYBE_ATOMIC(Type) Type
  11380. #else
  11381. # include <atomic>
  11382. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  11383. #endif
  11384. #ifndef ENTT_ID_TYPE
  11385. # include <cstdint>
  11386. # define ENTT_ID_TYPE std::uint32_t
  11387. #endif
  11388. #ifdef ENTT_SPARSE_PAGE
  11389. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  11390. #else
  11391. # define ENTT_SPARSE_PAGE 4096
  11392. #endif
  11393. #ifdef ENTT_PACKED_PAGE
  11394. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  11395. #else
  11396. # define ENTT_PACKED_PAGE 1024
  11397. #endif
  11398. #ifdef ENTT_DISABLE_ASSERT
  11399. # undef ENTT_ASSERT
  11400. # define ENTT_ASSERT(...) (void(0))
  11401. #elif !defined ENTT_ASSERT
  11402. # include <cassert>
  11403. # define ENTT_ASSERT(condition, ...) assert(condition)
  11404. #endif
  11405. #ifdef ENTT_NO_ETO
  11406. # include <type_traits>
  11407. # define ENTT_IGNORE_IF_EMPTY std::false_type
  11408. #else
  11409. # include <type_traits>
  11410. # define ENTT_IGNORE_IF_EMPTY std::true_type
  11411. #endif
  11412. #ifndef ENTT_STANDARD_CPP
  11413. # if defined __clang__ || defined __GNUC__
  11414. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  11415. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  11416. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  11417. # elif defined _MSC_VER
  11418. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  11419. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  11420. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  11421. # endif
  11422. #endif
  11423. #endif
  11424. namespace entt {
  11425. /*! @brief Identity function object (waiting for C++20). */
  11426. struct identity {
  11427. /**
  11428. * @brief Returns its argument unchanged.
  11429. * @tparam Type Type of the argument.
  11430. * @param value The actual argument.
  11431. * @return The submitted value as-is.
  11432. */
  11433. template<class Type>
  11434. [[nodiscard]] constexpr Type && operator()(Type &&value) const ENTT_NOEXCEPT {
  11435. return std::forward<Type>(value);
  11436. }
  11437. };
  11438. /**
  11439. * @brief Constant utility to disambiguate overloaded members of a class.
  11440. * @tparam Type Type of the desired overload.
  11441. * @tparam Class Type of class to which the member belongs.
  11442. * @param member A valid pointer to a member.
  11443. * @return Pointer to the member.
  11444. */
  11445. template<typename Type, typename Class>
  11446. [[nodiscard]] constexpr auto overload(Type Class:: *member) ENTT_NOEXCEPT { return member; }
  11447. /**
  11448. * @brief Constant utility to disambiguate overloaded functions.
  11449. * @tparam Func Function type of the desired overload.
  11450. * @param func A valid pointer to a function.
  11451. * @return Pointer to the function.
  11452. */
  11453. template<typename Func>
  11454. [[nodiscard]] constexpr auto overload(Func *func) ENTT_NOEXCEPT { return func; }
  11455. /**
  11456. * @brief Helper type for visitors.
  11457. * @tparam Func Types of function objects.
  11458. */
  11459. template<class... Func>
  11460. struct overloaded: Func... {
  11461. using Func::operator()...;
  11462. };
  11463. /**
  11464. * @brief Deduction guide.
  11465. * @tparam Func Types of function objects.
  11466. */
  11467. template<class... Func>
  11468. overloaded(Func...)
  11469. -> overloaded<Func...>;
  11470. /**
  11471. * @brief Basic implementation of a y-combinator.
  11472. * @tparam Func Type of a potentially recursive function.
  11473. */
  11474. template<class Func>
  11475. struct y_combinator {
  11476. /**
  11477. * @brief Constructs a y-combinator from a given function.
  11478. * @param recursive A potentially recursive function.
  11479. */
  11480. y_combinator(Func recursive):
  11481. func{std::move(recursive)}
  11482. {}
  11483. /**
  11484. * @brief Invokes a y-combinator and therefore its underlying function.
  11485. * @tparam Args Types of arguments to use to invoke the underlying function.
  11486. * @param args Parameters to use to invoke the underlying function.
  11487. * @return Return value of the underlying function, if any.
  11488. */
  11489. template <class... Args>
  11490. decltype(auto) operator()(Args &&... args) const {
  11491. return func(*this, std::forward<Args>(args)...);
  11492. }
  11493. /*! @copydoc operator()() */
  11494. template <class... Args>
  11495. decltype(auto) operator()(Args &&... args) {
  11496. return func(*this, std::forward<Args>(args)...);
  11497. }
  11498. private:
  11499. Func func;
  11500. };
  11501. }
  11502. #endif
  11503. // #include "fwd.hpp"
  11504. #ifndef ENTT_CORE_FWD_HPP
  11505. #define ENTT_CORE_FWD_HPP
  11506. #include <type_traits>
  11507. // #include "../config/config.h"
  11508. namespace entt {
  11509. template<std::size_t Len = sizeof(double[2]), std::size_t = alignof(typename std::aligned_storage_t<Len + !Len>)>
  11510. class basic_any;
  11511. /*! @brief Alias declaration for type identifiers. */
  11512. using id_type = ENTT_ID_TYPE;
  11513. /*! @brief Alias declaration for the most common use case. */
  11514. using any = basic_any<>;
  11515. }
  11516. #endif
  11517. // #include "type_info.hpp"
  11518. #ifndef ENTT_CORE_TYPE_INFO_HPP
  11519. #define ENTT_CORE_TYPE_INFO_HPP
  11520. #include <string_view>
  11521. #include <type_traits>
  11522. // #include "../config/config.h"
  11523. // #include "../core/attribute.h"
  11524. #ifndef ENTT_CORE_ATTRIBUTE_H
  11525. #define ENTT_CORE_ATTRIBUTE_H
  11526. #ifndef ENTT_EXPORT
  11527. # if defined _WIN32 || defined __CYGWIN__ || defined _MSC_VER
  11528. # define ENTT_EXPORT __declspec(dllexport)
  11529. # define ENTT_IMPORT __declspec(dllimport)
  11530. # define ENTT_HIDDEN
  11531. # elif defined __GNUC__ && __GNUC__ >= 4
  11532. # define ENTT_EXPORT __attribute__((visibility("default")))
  11533. # define ENTT_IMPORT __attribute__((visibility("default")))
  11534. # define ENTT_HIDDEN __attribute__((visibility("hidden")))
  11535. # else /* Unsupported compiler */
  11536. # define ENTT_EXPORT
  11537. # define ENTT_IMPORT
  11538. # define ENTT_HIDDEN
  11539. # endif
  11540. #endif
  11541. #ifndef ENTT_API
  11542. # if defined ENTT_API_EXPORT
  11543. # define ENTT_API ENTT_EXPORT
  11544. # elif defined ENTT_API_IMPORT
  11545. # define ENTT_API ENTT_IMPORT
  11546. # else /* No API */
  11547. # define ENTT_API
  11548. # endif
  11549. #endif
  11550. #endif
  11551. // #include "hashed_string.hpp"
  11552. #ifndef ENTT_CORE_HASHED_STRING_HPP
  11553. #define ENTT_CORE_HASHED_STRING_HPP
  11554. #include <cstddef>
  11555. #include <cstdint>
  11556. // #include "../config/config.h"
  11557. // #include "fwd.hpp"
  11558. namespace entt {
  11559. /**
  11560. * @cond TURN_OFF_DOXYGEN
  11561. * Internal details not to be documented.
  11562. */
  11563. namespace internal {
  11564. template<typename>
  11565. struct fnv1a_traits;
  11566. template<>
  11567. struct fnv1a_traits<std::uint32_t> {
  11568. using type = std::uint32_t;
  11569. static constexpr std::uint32_t offset = 2166136261;
  11570. static constexpr std::uint32_t prime = 16777619;
  11571. };
  11572. template<>
  11573. struct fnv1a_traits<std::uint64_t> {
  11574. using type = std::uint64_t;
  11575. static constexpr std::uint64_t offset = 14695981039346656037ull;
  11576. static constexpr std::uint64_t prime = 1099511628211ull;
  11577. };
  11578. }
  11579. /**
  11580. * Internal details not to be documented.
  11581. * @endcond
  11582. */
  11583. /**
  11584. * @brief Zero overhead unique identifier.
  11585. *
  11586. * A hashed string is a compile-time tool that allows users to use
  11587. * human-readable identifers in the codebase while using their numeric
  11588. * counterparts at runtime.<br/>
  11589. * Because of that, a hashed string can also be used in constant expressions if
  11590. * required.
  11591. *
  11592. * @tparam Char Character type.
  11593. */
  11594. template<typename Char>
  11595. class basic_hashed_string {
  11596. using traits_type = internal::fnv1a_traits<id_type>;
  11597. struct const_wrapper {
  11598. // non-explicit constructor on purpose
  11599. constexpr const_wrapper(const Char *curr) ENTT_NOEXCEPT: str{curr} {}
  11600. const Char *str;
  11601. };
  11602. // Fowler–Noll–Vo hash function v. 1a - the good
  11603. [[nodiscard]] static constexpr id_type helper(const Char *curr) ENTT_NOEXCEPT {
  11604. auto value = traits_type::offset;
  11605. while(*curr != 0) {
  11606. value = (value ^ static_cast<traits_type::type>(*(curr++))) * traits_type::prime;
  11607. }
  11608. return value;
  11609. }
  11610. public:
  11611. /*! @brief Character type. */
  11612. using value_type = Char;
  11613. /*! @brief Unsigned integer type. */
  11614. using hash_type = id_type;
  11615. /**
  11616. * @brief Returns directly the numeric representation of a string view.
  11617. * @param str Human-readable identifer.
  11618. * @param size Length of the string to hash.
  11619. * @return The numeric representation of the string.
  11620. */
  11621. [[nodiscard]] static constexpr hash_type value(const value_type *str, std::size_t size) ENTT_NOEXCEPT {
  11622. id_type partial{traits_type::offset};
  11623. while(size--) { partial = (partial^(str++)[0])*traits_type::prime; }
  11624. return partial;
  11625. }
  11626. /**
  11627. * @brief Returns directly the numeric representation of a string.
  11628. *
  11629. * Forcing template resolution avoids implicit conversions. An
  11630. * human-readable identifier can be anything but a plain, old bunch of
  11631. * characters.<br/>
  11632. * Example of use:
  11633. * @code{.cpp}
  11634. * const auto value = basic_hashed_string<char>::to_value("my.png");
  11635. * @endcode
  11636. *
  11637. * @tparam N Number of characters of the identifier.
  11638. * @param str Human-readable identifer.
  11639. * @return The numeric representation of the string.
  11640. */
  11641. template<std::size_t N>
  11642. [[nodiscard]] static constexpr hash_type value(const value_type (&str)[N]) ENTT_NOEXCEPT {
  11643. return helper(str);
  11644. }
  11645. /**
  11646. * @brief Returns directly the numeric representation of a string.
  11647. * @param wrapper Helps achieving the purpose by relying on overloading.
  11648. * @return The numeric representation of the string.
  11649. */
  11650. [[nodiscard]] static hash_type value(const_wrapper wrapper) ENTT_NOEXCEPT {
  11651. return helper(wrapper.str);
  11652. }
  11653. /*! @brief Constructs an empty hashed string. */
  11654. constexpr basic_hashed_string() ENTT_NOEXCEPT
  11655. : str{nullptr}, hash{}
  11656. {}
  11657. /**
  11658. * @brief Constructs a hashed string from an array of const characters.
  11659. *
  11660. * Forcing template resolution avoids implicit conversions. An
  11661. * human-readable identifier can be anything but a plain, old bunch of
  11662. * characters.<br/>
  11663. * Example of use:
  11664. * @code{.cpp}
  11665. * basic_hashed_string<char> hs{"my.png"};
  11666. * @endcode
  11667. *
  11668. * @tparam N Number of characters of the identifier.
  11669. * @param curr Human-readable identifer.
  11670. */
  11671. template<std::size_t N>
  11672. constexpr basic_hashed_string(const value_type (&curr)[N]) ENTT_NOEXCEPT
  11673. : str{curr}, hash{helper(curr)}
  11674. {}
  11675. /**
  11676. * @brief Explicit constructor on purpose to avoid constructing a hashed
  11677. * string directly from a `const value_type *`.
  11678. * @param wrapper Helps achieving the purpose by relying on overloading.
  11679. */
  11680. explicit constexpr basic_hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
  11681. : str{wrapper.str}, hash{helper(wrapper.str)}
  11682. {}
  11683. /**
  11684. * @brief Returns the human-readable representation of a hashed string.
  11685. * @return The string used to initialize the instance.
  11686. */
  11687. [[nodiscard]] constexpr const value_type * data() const ENTT_NOEXCEPT {
  11688. return str;
  11689. }
  11690. /**
  11691. * @brief Returns the numeric representation of a hashed string.
  11692. * @return The numeric representation of the instance.
  11693. */
  11694. [[nodiscard]] constexpr hash_type value() const ENTT_NOEXCEPT {
  11695. return hash;
  11696. }
  11697. /*! @copydoc data */
  11698. [[nodiscard]] constexpr operator const value_type *() const ENTT_NOEXCEPT { return data(); }
  11699. /**
  11700. * @brief Returns the numeric representation of a hashed string.
  11701. * @return The numeric representation of the instance.
  11702. */
  11703. [[nodiscard]] constexpr operator hash_type() const ENTT_NOEXCEPT { return value(); }
  11704. /**
  11705. * @brief Compares two hashed strings.
  11706. * @param other Hashed string with which to compare.
  11707. * @return True if the two hashed strings are identical, false otherwise.
  11708. */
  11709. [[nodiscard]] constexpr bool operator==(const basic_hashed_string &other) const ENTT_NOEXCEPT {
  11710. return hash == other.hash;
  11711. }
  11712. private:
  11713. const value_type *str;
  11714. hash_type hash;
  11715. };
  11716. /**
  11717. * @brief Deduction guide.
  11718. *
  11719. * It allows to deduce the character type of the hashed string directly from a
  11720. * human-readable identifer provided to the constructor.
  11721. *
  11722. * @tparam Char Character type.
  11723. * @tparam N Number of characters of the identifier.
  11724. * @param str Human-readable identifer.
  11725. */
  11726. template<typename Char, std::size_t N>
  11727. basic_hashed_string(const Char (&str)[N])
  11728. -> basic_hashed_string<Char>;
  11729. /**
  11730. * @brief Compares two hashed strings.
  11731. * @tparam Char Character type.
  11732. * @param lhs A valid hashed string.
  11733. * @param rhs A valid hashed string.
  11734. * @return True if the two hashed strings are identical, false otherwise.
  11735. */
  11736. template<typename Char>
  11737. [[nodiscard]] constexpr bool operator!=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) ENTT_NOEXCEPT {
  11738. return !(lhs == rhs);
  11739. }
  11740. /*! @brief Aliases for common character types. */
  11741. using hashed_string = basic_hashed_string<char>;
  11742. /*! @brief Aliases for common character types. */
  11743. using hashed_wstring = basic_hashed_string<wchar_t>;
  11744. inline namespace literals {
  11745. /**
  11746. * @brief User defined literal for hashed strings.
  11747. * @param str The literal without its suffix.
  11748. * @return A properly initialized hashed string.
  11749. */
  11750. [[nodiscard]] constexpr entt::hashed_string operator"" _hs(const char *str, std::size_t) ENTT_NOEXCEPT {
  11751. return entt::hashed_string{str};
  11752. }
  11753. /**
  11754. * @brief User defined literal for hashed wstrings.
  11755. * @param str The literal without its suffix.
  11756. * @return A properly initialized hashed wstring.
  11757. */
  11758. [[nodiscard]] constexpr entt::hashed_wstring operator"" _hws(const wchar_t *str, std::size_t) ENTT_NOEXCEPT {
  11759. return entt::hashed_wstring{str};
  11760. }
  11761. }
  11762. }
  11763. #endif
  11764. // #include "fwd.hpp"
  11765. namespace entt {
  11766. /**
  11767. * @cond TURN_OFF_DOXYGEN
  11768. * Internal details not to be documented.
  11769. */
  11770. namespace internal {
  11771. struct ENTT_API type_seq final {
  11772. [[nodiscard]] static id_type next() ENTT_NOEXCEPT {
  11773. static ENTT_MAYBE_ATOMIC(id_type) value{};
  11774. return value++;
  11775. }
  11776. };
  11777. template<typename Type>
  11778. [[nodiscard]] constexpr auto stripped_type_name() ENTT_NOEXCEPT {
  11779. #if defined ENTT_PRETTY_FUNCTION
  11780. std::string_view pretty_function{ENTT_PRETTY_FUNCTION};
  11781. auto first = pretty_function.find_first_not_of(' ', pretty_function.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX)+1);
  11782. auto value = pretty_function.substr(first, pretty_function.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
  11783. return value;
  11784. #else
  11785. return std::string_view{""};
  11786. #endif
  11787. }
  11788. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  11789. [[nodiscard]] static constexpr std::string_view type_name(int) ENTT_NOEXCEPT {
  11790. constexpr auto value = stripped_type_name<Type>();
  11791. return value;
  11792. }
  11793. template<typename Type>
  11794. [[nodiscard]] static std::string_view type_name(char) ENTT_NOEXCEPT {
  11795. static const auto value = stripped_type_name<Type>();
  11796. return value;
  11797. }
  11798. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  11799. [[nodiscard]] static constexpr id_type type_hash(int) ENTT_NOEXCEPT {
  11800. constexpr auto stripped = stripped_type_name<Type>();
  11801. constexpr auto value = hashed_string::value(stripped.data(), stripped.size());
  11802. return value;
  11803. }
  11804. template<typename Type>
  11805. [[nodiscard]] static id_type type_hash(char) ENTT_NOEXCEPT {
  11806. static const auto value = [](const auto stripped) {
  11807. return hashed_string::value(stripped.data(), stripped.size());
  11808. }(stripped_type_name<Type>());
  11809. return value;
  11810. }
  11811. }
  11812. /**
  11813. * Internal details not to be documented.
  11814. * @endcond
  11815. */
  11816. /**
  11817. * @brief Type sequential identifier.
  11818. * @tparam Type Type for which to generate a sequential identifier.
  11819. */
  11820. template<typename Type, typename = void>
  11821. struct ENTT_API type_seq final {
  11822. /**
  11823. * @brief Returns the sequential identifier of a given type.
  11824. * @return The sequential identifier of a given type.
  11825. */
  11826. [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
  11827. static const id_type value = internal::type_seq::next();
  11828. return value;
  11829. }
  11830. /*! @copydoc value */
  11831. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  11832. };
  11833. /**
  11834. * @brief Type hash.
  11835. * @tparam Type Type for which to generate a hash value.
  11836. */
  11837. template<typename Type, typename = void>
  11838. struct type_hash final {
  11839. /**
  11840. * @brief Returns the numeric representation of a given type.
  11841. * @return The numeric representation of the given type.
  11842. */
  11843. #if defined ENTT_PRETTY_FUNCTION
  11844. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  11845. return internal::type_hash<Type>(0);
  11846. #else
  11847. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  11848. return type_seq<Type>::value();
  11849. #endif
  11850. }
  11851. /*! @copydoc value */
  11852. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  11853. };
  11854. /**
  11855. * @brief Type name.
  11856. * @tparam Type Type for which to generate a name.
  11857. */
  11858. template<typename Type, typename = void>
  11859. struct type_name final {
  11860. /**
  11861. * @brief Returns the name of a given type.
  11862. * @return The name of the given type.
  11863. */
  11864. [[nodiscard]] static constexpr std::string_view value() ENTT_NOEXCEPT {
  11865. return internal::type_name<Type>(0);
  11866. }
  11867. /*! @copydoc value */
  11868. [[nodiscard]] constexpr operator std::string_view() const ENTT_NOEXCEPT { return value(); }
  11869. };
  11870. /*! @brief Implementation specific information about a type. */
  11871. class type_info final {
  11872. template<typename>
  11873. friend type_info type_id() ENTT_NOEXCEPT;
  11874. type_info(id_type seq_v, id_type hash_v, std::string_view name_v) ENTT_NOEXCEPT
  11875. : seq_value{seq_v},
  11876. hash_value{hash_v},
  11877. name_value{name_v}
  11878. {}
  11879. public:
  11880. /*! @brief Default constructor. */
  11881. type_info() ENTT_NOEXCEPT
  11882. : type_info({}, {}, {})
  11883. {}
  11884. /*! @brief Default copy constructor. */
  11885. type_info(const type_info &) ENTT_NOEXCEPT = default;
  11886. /*! @brief Default move constructor. */
  11887. type_info(type_info &&) ENTT_NOEXCEPT = default;
  11888. /**
  11889. * @brief Default copy assignment operator.
  11890. * @return This type info object.
  11891. */
  11892. type_info & operator=(const type_info &) ENTT_NOEXCEPT = default;
  11893. /**
  11894. * @brief Default move assignment operator.
  11895. * @return This type info object.
  11896. */
  11897. type_info & operator=(type_info &&) ENTT_NOEXCEPT = default;
  11898. /**
  11899. * @brief Checks if a type info object is properly initialized.
  11900. * @return True if the object is properly initialized, false otherwise.
  11901. */
  11902. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  11903. return name_value.data() != nullptr;
  11904. }
  11905. /**
  11906. * @brief Type sequential identifier.
  11907. * @return Type sequential identifier.
  11908. */
  11909. [[nodiscard]] id_type seq() const ENTT_NOEXCEPT {
  11910. return seq_value;
  11911. }
  11912. /**
  11913. * @brief Type hash.
  11914. * @return Type hash.
  11915. */
  11916. [[nodiscard]] id_type hash() const ENTT_NOEXCEPT {
  11917. return hash_value;
  11918. }
  11919. /**
  11920. * @brief Type name.
  11921. * @return Type name.
  11922. */
  11923. [[nodiscard]] std::string_view name() const ENTT_NOEXCEPT {
  11924. return name_value;
  11925. }
  11926. /**
  11927. * @brief Compares the contents of two type info objects.
  11928. * @param other Object with which to compare.
  11929. * @return False if the two contents differ, true otherwise.
  11930. */
  11931. [[nodiscard]] bool operator==(const type_info &other) const ENTT_NOEXCEPT {
  11932. return hash_value == other.hash_value;
  11933. }
  11934. private:
  11935. id_type seq_value;
  11936. id_type hash_value;
  11937. std::string_view name_value;
  11938. };
  11939. /**
  11940. * @brief Compares the contents of two type info objects.
  11941. * @param lhs A type info object.
  11942. * @param rhs A type info object.
  11943. * @return True if the two contents differ, false otherwise.
  11944. */
  11945. [[nodiscard]] inline bool operator!=(const type_info &lhs, const type_info &rhs) ENTT_NOEXCEPT {
  11946. return !(lhs == rhs);
  11947. }
  11948. /**
  11949. * @brief Returns the type info object for a given type.
  11950. * @tparam Type Type for which to generate a type info object.
  11951. * @return The type info object for the given type.
  11952. */
  11953. template<typename Type>
  11954. [[nodiscard]] type_info type_id() ENTT_NOEXCEPT {
  11955. return type_info{
  11956. type_seq<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  11957. type_hash<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  11958. type_name<std::remove_cv_t<std::remove_reference_t<Type>>>::value()
  11959. };
  11960. }
  11961. }
  11962. #endif
  11963. // #include "type_traits.hpp"
  11964. #ifndef ENTT_CORE_TYPE_TRAITS_HPP
  11965. #define ENTT_CORE_TYPE_TRAITS_HPP
  11966. #include <cstddef>
  11967. #include <iterator>
  11968. #include <type_traits>
  11969. #include <utility>
  11970. // #include "../config/config.h"
  11971. // #include "fwd.hpp"
  11972. namespace entt {
  11973. /**
  11974. * @brief Utility class to disambiguate overloaded functions.
  11975. * @tparam N Number of choices available.
  11976. */
  11977. template<std::size_t N>
  11978. struct choice_t
  11979. // Unfortunately, doxygen cannot parse such a construct.
  11980. /*! @cond TURN_OFF_DOXYGEN */
  11981. : choice_t<N-1>
  11982. /*! @endcond */
  11983. {};
  11984. /*! @copybrief choice_t */
  11985. template<>
  11986. struct choice_t<0> {};
  11987. /**
  11988. * @brief Variable template for the choice trick.
  11989. * @tparam N Number of choices available.
  11990. */
  11991. template<std::size_t N>
  11992. inline constexpr choice_t<N> choice{};
  11993. /**
  11994. * @brief Identity type trait.
  11995. *
  11996. * Useful to establish non-deduced contexts in template argument deduction
  11997. * (waiting for C++20) or to provide types through function arguments.
  11998. *
  11999. * @tparam Type A type.
  12000. */
  12001. template<typename Type>
  12002. struct type_identity {
  12003. /*! @brief Identity type. */
  12004. using type = Type;
  12005. };
  12006. /**
  12007. * @brief Helper type.
  12008. * @tparam Type A type.
  12009. */
  12010. template<typename Type>
  12011. using type_identity_t = typename type_identity<Type>::type;
  12012. /**
  12013. * @brief A type-only `sizeof` wrapper that returns 0 where `sizeof` complains.
  12014. * @tparam Type The type of which to return the size.
  12015. * @tparam The size of the type if `sizeof` accepts it, 0 otherwise.
  12016. */
  12017. template<typename Type, typename = void>
  12018. struct size_of: std::integral_constant<std::size_t, 0u> {};
  12019. /*! @copydoc size_of */
  12020. template<typename Type>
  12021. struct size_of<Type, std::void_t<decltype(sizeof(Type))>>
  12022. : std::integral_constant<std::size_t, sizeof(Type)>
  12023. {};
  12024. /**
  12025. * @brief Helper variable template.
  12026. * @tparam Type The type of which to return the size.
  12027. */
  12028. template<class Type>
  12029. inline constexpr std::size_t size_of_v = size_of<Type>::value;
  12030. /**
  12031. * @brief Using declaration to be used to _repeat_ the same type a number of
  12032. * times equal to the size of a given parameter pack.
  12033. * @tparam Type A type to repeat.
  12034. */
  12035. template<typename Type, typename>
  12036. using unpack_as_t = Type;
  12037. /**
  12038. * @brief Helper variable template to be used to _repeat_ the same value a
  12039. * number of times equal to the size of a given parameter pack.
  12040. * @tparam Value A value to repeat.
  12041. */
  12042. template<auto Value, typename>
  12043. inline constexpr auto unpack_as_v = Value;
  12044. /**
  12045. * @brief Wraps a static constant.
  12046. * @tparam Value A static constant.
  12047. */
  12048. template<auto Value>
  12049. using integral_constant = std::integral_constant<decltype(Value), Value>;
  12050. /**
  12051. * @brief Alias template to facilitate the creation of named values.
  12052. * @tparam Value A constant value at least convertible to `id_type`.
  12053. */
  12054. template<id_type Value>
  12055. using tag = integral_constant<Value>;
  12056. /**
  12057. * @brief A class to use to push around lists of types, nothing more.
  12058. * @tparam Type Types provided by the type list.
  12059. */
  12060. template<typename... Type>
  12061. struct type_list {
  12062. /*! @brief Type list type. */
  12063. using type = type_list;
  12064. /*! @brief Compile-time number of elements in the type list. */
  12065. static constexpr auto size = sizeof...(Type);
  12066. };
  12067. /*! @brief Primary template isn't defined on purpose. */
  12068. template<std::size_t, typename>
  12069. struct type_list_element;
  12070. /**
  12071. * @brief Provides compile-time indexed access to the types of a type list.
  12072. * @tparam Index Index of the type to return.
  12073. * @tparam Type First type provided by the type list.
  12074. * @tparam Other Other types provided by the type list.
  12075. */
  12076. template<std::size_t Index, typename Type, typename... Other>
  12077. struct type_list_element<Index, type_list<Type, Other...>>
  12078. : type_list_element<Index - 1u, type_list<Other...>>
  12079. {};
  12080. /**
  12081. * @brief Provides compile-time indexed access to the types of a type list.
  12082. * @tparam Type First type provided by the type list.
  12083. * @tparam Other Other types provided by the type list.
  12084. */
  12085. template<typename Type, typename... Other>
  12086. struct type_list_element<0u, type_list<Type, Other...>> {
  12087. /*! @brief Searched type. */
  12088. using type = Type;
  12089. };
  12090. /**
  12091. * @brief Helper type.
  12092. * @tparam Index Index of the type to return.
  12093. * @tparam List Type list to search into.
  12094. */
  12095. template<std::size_t Index, typename List>
  12096. using type_list_element_t = typename type_list_element<Index, List>::type;
  12097. /**
  12098. * @brief Concatenates multiple type lists.
  12099. * @tparam Type Types provided by the first type list.
  12100. * @tparam Other Types provided by the second type list.
  12101. * @return A type list composed by the types of both the type lists.
  12102. */
  12103. template<typename... Type, typename... Other>
  12104. constexpr type_list<Type..., Other...> operator+(type_list<Type...>, type_list<Other...>) { return {}; }
  12105. /*! @brief Primary template isn't defined on purpose. */
  12106. template<typename...>
  12107. struct type_list_cat;
  12108. /*! @brief Concatenates multiple type lists. */
  12109. template<>
  12110. struct type_list_cat<> {
  12111. /*! @brief A type list composed by the types of all the type lists. */
  12112. using type = type_list<>;
  12113. };
  12114. /**
  12115. * @brief Concatenates multiple type lists.
  12116. * @tparam Type Types provided by the first type list.
  12117. * @tparam Other Types provided by the second type list.
  12118. * @tparam List Other type lists, if any.
  12119. */
  12120. template<typename... Type, typename... Other, typename... List>
  12121. struct type_list_cat<type_list<Type...>, type_list<Other...>, List...> {
  12122. /*! @brief A type list composed by the types of all the type lists. */
  12123. using type = typename type_list_cat<type_list<Type..., Other...>, List...>::type;
  12124. };
  12125. /**
  12126. * @brief Concatenates multiple type lists.
  12127. * @tparam Type Types provided by the type list.
  12128. */
  12129. template<typename... Type>
  12130. struct type_list_cat<type_list<Type...>> {
  12131. /*! @brief A type list composed by the types of all the type lists. */
  12132. using type = type_list<Type...>;
  12133. };
  12134. /**
  12135. * @brief Helper type.
  12136. * @tparam List Type lists to concatenate.
  12137. */
  12138. template<typename... List>
  12139. using type_list_cat_t = typename type_list_cat<List...>::type;
  12140. /*! @brief Primary template isn't defined on purpose. */
  12141. template<typename>
  12142. struct type_list_unique;
  12143. /**
  12144. * @brief Removes duplicates types from a type list.
  12145. * @tparam Type One of the types provided by the given type list.
  12146. * @tparam Other The other types provided by the given type list.
  12147. */
  12148. template<typename Type, typename... Other>
  12149. struct type_list_unique<type_list<Type, Other...>> {
  12150. /*! @brief A type list without duplicate types. */
  12151. using type = std::conditional_t<
  12152. std::disjunction_v<std::is_same<Type, Other>...>,
  12153. typename type_list_unique<type_list<Other...>>::type,
  12154. type_list_cat_t<type_list<Type>, typename type_list_unique<type_list<Other...>>::type>
  12155. >;
  12156. };
  12157. /*! @brief Removes duplicates types from a type list. */
  12158. template<>
  12159. struct type_list_unique<type_list<>> {
  12160. /*! @brief A type list without duplicate types. */
  12161. using type = type_list<>;
  12162. };
  12163. /**
  12164. * @brief Helper type.
  12165. * @tparam Type A type list.
  12166. */
  12167. template<typename Type>
  12168. using type_list_unique_t = typename type_list_unique<Type>::type;
  12169. /**
  12170. * @brief Provides the member constant `value` to true if a type list contains a
  12171. * given type, false otherwise.
  12172. * @tparam List Type list.
  12173. * @tparam Type Type to look for.
  12174. */
  12175. template<typename List, typename Type>
  12176. struct type_list_contains;
  12177. /**
  12178. * @copybrief type_list_contains
  12179. * @tparam Type Types provided by the type list.
  12180. * @tparam Other Type to look for.
  12181. */
  12182. template<typename... Type, typename Other>
  12183. struct type_list_contains<type_list<Type...>, Other>: std::disjunction<std::is_same<Type, Other>...> {};
  12184. /**
  12185. * @brief Helper variable template.
  12186. * @tparam List Type list.
  12187. * @tparam Type Type to look for.
  12188. */
  12189. template<class List, typename Type>
  12190. inline constexpr bool type_list_contains_v = type_list_contains<List, Type>::value;
  12191. /*! @brief Primary template isn't defined on purpose. */
  12192. template<typename...>
  12193. struct type_list_diff;
  12194. /**
  12195. * @brief Computes the difference between two type lists.
  12196. * @tparam Type Types provided by the first type list.
  12197. * @tparam Other Types provided by the second type list.
  12198. */
  12199. template<typename... Type, typename... Other>
  12200. struct type_list_diff<type_list<Type...>, type_list<Other...>> {
  12201. /*! @brief A type list that is the difference between the two type lists. */
  12202. using type = type_list_cat_t<std::conditional_t<type_list_contains_v<type_list<Other...>, Type>, type_list<>, type_list<Type>>...>;
  12203. };
  12204. /**
  12205. * @brief Helper type.
  12206. * @tparam List Type lists between which to compute the difference.
  12207. */
  12208. template<typename... List>
  12209. using type_list_diff_t = typename type_list_diff<List...>::type;
  12210. /**
  12211. * @brief A class to use to push around lists of constant values, nothing more.
  12212. * @tparam Value Values provided by the value list.
  12213. */
  12214. template<auto... Value>
  12215. struct value_list {
  12216. /*! @brief Value list type. */
  12217. using type = value_list;
  12218. /*! @brief Compile-time number of elements in the value list. */
  12219. static constexpr auto size = sizeof...(Value);
  12220. };
  12221. /*! @brief Primary template isn't defined on purpose. */
  12222. template<std::size_t, typename>
  12223. struct value_list_element;
  12224. /**
  12225. * @brief Provides compile-time indexed access to the values of a value list.
  12226. * @tparam Index Index of the value to return.
  12227. * @tparam Value First value provided by the value list.
  12228. * @tparam Other Other values provided by the value list.
  12229. */
  12230. template<std::size_t Index, auto Value, auto... Other>
  12231. struct value_list_element<Index, value_list<Value, Other...>>
  12232. : value_list_element<Index - 1u, value_list<Other...>>
  12233. {};
  12234. /**
  12235. * @brief Provides compile-time indexed access to the types of a type list.
  12236. * @tparam Value First value provided by the value list.
  12237. * @tparam Other Other values provided by the value list.
  12238. */
  12239. template<auto Value, auto... Other>
  12240. struct value_list_element<0u, value_list<Value, Other...>> {
  12241. /*! @brief Searched value. */
  12242. static constexpr auto value = Value;
  12243. };
  12244. /**
  12245. * @brief Helper type.
  12246. * @tparam Index Index of the value to return.
  12247. * @tparam List Value list to search into.
  12248. */
  12249. template<std::size_t Index, typename List>
  12250. inline constexpr auto value_list_element_v = value_list_element<Index, List>::value;
  12251. /**
  12252. * @brief Concatenates multiple value lists.
  12253. * @tparam Value Values provided by the first value list.
  12254. * @tparam Other Values provided by the second value list.
  12255. * @return A value list composed by the values of both the value lists.
  12256. */
  12257. template<auto... Value, auto... Other>
  12258. constexpr value_list<Value..., Other...> operator+(value_list<Value...>, value_list<Other...>) { return {}; }
  12259. /*! @brief Primary template isn't defined on purpose. */
  12260. template<typename...>
  12261. struct value_list_cat;
  12262. /*! @brief Concatenates multiple value lists. */
  12263. template<>
  12264. struct value_list_cat<> {
  12265. /*! @brief A value list composed by the values of all the value lists. */
  12266. using type = value_list<>;
  12267. };
  12268. /**
  12269. * @brief Concatenates multiple value lists.
  12270. * @tparam Value Values provided by the first value list.
  12271. * @tparam Other Values provided by the second value list.
  12272. * @tparam List Other value lists, if any.
  12273. */
  12274. template<auto... Value, auto... Other, typename... List>
  12275. struct value_list_cat<value_list<Value...>, value_list<Other...>, List...> {
  12276. /*! @brief A value list composed by the values of all the value lists. */
  12277. using type = typename value_list_cat<value_list<Value..., Other...>, List...>::type;
  12278. };
  12279. /**
  12280. * @brief Concatenates multiple value lists.
  12281. * @tparam Value Values provided by the value list.
  12282. */
  12283. template<auto... Value>
  12284. struct value_list_cat<value_list<Value...>> {
  12285. /*! @brief A value list composed by the values of all the value lists. */
  12286. using type = value_list<Value...>;
  12287. };
  12288. /**
  12289. * @brief Helper type.
  12290. * @tparam List Value lists to concatenate.
  12291. */
  12292. template<typename... List>
  12293. using value_list_cat_t = typename value_list_cat<List...>::type;
  12294. /**
  12295. * @cond TURN_OFF_DOXYGEN
  12296. * Internal details not to be documented.
  12297. */
  12298. namespace internal {
  12299. template<typename>
  12300. [[nodiscard]] constexpr bool is_equality_comparable(...) { return false; }
  12301. template<typename Type>
  12302. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<0>)
  12303. -> decltype(std::declval<Type>() == std::declval<Type>()) { return true; }
  12304. template<typename Type>
  12305. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<1>)
  12306. -> decltype(std::declval<typename Type::value_type>(), std::declval<Type>() == std::declval<Type>()) {
  12307. if constexpr(std::is_same_v<typename Type::value_type, Type>) {
  12308. return is_equality_comparable<Type>(choice<0>);
  12309. } else {
  12310. return is_equality_comparable<typename Type::value_type>(choice<2>);
  12311. }
  12312. }
  12313. template<typename Type>
  12314. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<2>)
  12315. -> decltype(std::declval<typename Type::mapped_type>(), std::declval<Type>() == std::declval<Type>()) {
  12316. return is_equality_comparable<typename Type::key_type>(choice<2>) && is_equality_comparable<typename Type::mapped_type>(choice<2>);
  12317. }
  12318. }
  12319. /**
  12320. * Internal details not to be documented.
  12321. * @endcond
  12322. */
  12323. /**
  12324. * @brief Provides the member constant `value` to true if a given type is
  12325. * equality comparable, false otherwise.
  12326. * @tparam Type The type to test.
  12327. */
  12328. template<typename Type, typename = void>
  12329. struct is_equality_comparable: std::bool_constant<internal::is_equality_comparable<Type>(choice<2>)> {};
  12330. /**
  12331. * @brief Helper variable template.
  12332. * @tparam Type The type to test.
  12333. */
  12334. template<class Type>
  12335. inline constexpr bool is_equality_comparable_v = is_equality_comparable<Type>::value;
  12336. /*! @brief Same as std::is_invocable, but with tuples. */
  12337. template<typename, typename>
  12338. struct is_applicable: std::false_type {};
  12339. /**
  12340. * @copybrief is_applicable
  12341. * @tparam Func A valid function type.
  12342. * @tparam Tuple Tuple-like type.
  12343. * @tparam Args The list of arguments to use to probe the function type.
  12344. */
  12345. template<typename Func, template<typename...> class Tuple, typename... Args>
  12346. struct is_applicable<Func, Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  12347. /**
  12348. * @copybrief is_applicable
  12349. * @tparam Func A valid function type.
  12350. * @tparam Tuple Tuple-like type.
  12351. * @tparam Args The list of arguments to use to probe the function type.
  12352. */
  12353. template<typename Func, template<typename...> class Tuple, typename... Args>
  12354. struct is_applicable<Func, const Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  12355. /**
  12356. * @brief Helper variable template.
  12357. * @tparam Func A valid function type.
  12358. * @tparam Args The list of arguments to use to probe the function type.
  12359. */
  12360. template<typename Func, typename Args>
  12361. inline constexpr bool is_applicable_v = is_applicable<Func, Args>::value;
  12362. /*! @brief Same as std::is_invocable_r, but with tuples for arguments. */
  12363. template<typename, typename, typename>
  12364. struct is_applicable_r: std::false_type {};
  12365. /**
  12366. * @copybrief is_applicable_r
  12367. * @tparam Ret The type to which the return type of the function should be
  12368. * convertible.
  12369. * @tparam Func A valid function type.
  12370. * @tparam Args The list of arguments to use to probe the function type.
  12371. */
  12372. template<typename Ret, typename Func, typename... Args>
  12373. struct is_applicable_r<Ret, Func, std::tuple<Args...>>: std::is_invocable_r<Ret, Func, Args...> {};
  12374. /**
  12375. * @brief Helper variable template.
  12376. * @tparam Ret The type to which the return type of the function should be
  12377. * convertible.
  12378. * @tparam Func A valid function type.
  12379. * @tparam Args The list of arguments to use to probe the function type.
  12380. */
  12381. template<typename Ret, typename Func, typename Args>
  12382. inline constexpr bool is_applicable_r_v = is_applicable_r<Ret, Func, Args>::value;
  12383. /**
  12384. * @brief Provides the member constant `value` to true if a given type is
  12385. * complete, false otherwise.
  12386. * @tparam Type The type to test.
  12387. */
  12388. template<typename Type, typename = void>
  12389. struct is_complete: std::false_type {};
  12390. /*! @copydoc is_complete */
  12391. template<typename Type>
  12392. struct is_complete<Type, std::void_t<decltype(sizeof(Type))>>: std::true_type {};
  12393. /**
  12394. * @brief Helper variable template.
  12395. * @tparam Type The type to test.
  12396. */
  12397. template<typename Type>
  12398. inline constexpr bool is_complete_v = is_complete<Type>::value;
  12399. /**
  12400. * @brief Provides the member constant `value` to true if a given type is an
  12401. * iterator, false otherwise.
  12402. * @tparam Type The type to test.
  12403. */
  12404. template<typename Type, typename = void>
  12405. struct is_iterator: std::false_type {};
  12406. /*! @copydoc is_iterator */
  12407. template<typename Type>
  12408. struct is_iterator<Type, std::void_t<typename std::iterator_traits<Type>::iterator_category>>
  12409. : std::true_type
  12410. {};
  12411. /**
  12412. * @brief Helper variable template.
  12413. * @tparam Type The type to test.
  12414. */
  12415. template<typename Type>
  12416. inline constexpr bool is_iterator_v = is_iterator<Type>::value;
  12417. /**
  12418. * @brief Provides the member constant `value` to true if a given type is of the
  12419. * required iterator type, false otherwise.
  12420. * @tparam Type The type to test.
  12421. * @tparam It Required iterator type.
  12422. */
  12423. template<typename Type, typename It, typename = void>
  12424. struct is_iterator_type: std::false_type {};
  12425. /*! @copydoc is_iterator_type */
  12426. template<typename Type, typename It>
  12427. struct is_iterator_type<Type, It, std::enable_if_t<is_iterator_v<Type> && std::is_same_v<Type, It>>>
  12428. : std::true_type
  12429. {};
  12430. /*! @copydoc is_iterator_type */
  12431. template<typename Type, typename It>
  12432. struct is_iterator_type<Type, It, std::enable_if_t<!std::is_same_v<Type, It>, std::void_t<typename It::iterator_type>>>
  12433. : is_iterator_type<Type, typename It::iterator_type>
  12434. {};
  12435. /**
  12436. * @brief Helper variable template.
  12437. * @tparam Type The type to test.
  12438. * @tparam It Required iterator type.
  12439. */
  12440. template<typename Type, typename It>
  12441. inline constexpr bool is_iterator_type_v = is_iterator_type<Type, It>::value;
  12442. /**
  12443. * @brief Transcribes the constness of a type to another type.
  12444. * @tparam To The type to which to transcribe the constness.
  12445. * @tparam From The type from which to transcribe the constness.
  12446. */
  12447. template<typename To, typename From>
  12448. struct constness_as {
  12449. /*! @brief The type resulting from the transcription of the constness. */
  12450. using type = std::remove_const_t<To>;
  12451. };
  12452. /*! @copydoc constness_as */
  12453. template<typename To, typename From>
  12454. struct constness_as<To, const From> {
  12455. /*! @brief The type resulting from the transcription of the constness. */
  12456. using type = std::add_const_t<To>;
  12457. };
  12458. /**
  12459. * @brief Alias template to facilitate the transcription of the constness.
  12460. * @tparam To The type to which to transcribe the constness.
  12461. * @tparam From The type from which to transcribe the constness.
  12462. */
  12463. template<typename To, typename From>
  12464. using constness_as_t = typename constness_as<To, From>::type;
  12465. /**
  12466. * @brief Extracts the class of a non-static member object or function.
  12467. * @tparam Member A pointer to a non-static member object or function.
  12468. */
  12469. template<typename Member>
  12470. class member_class {
  12471. static_assert(std::is_member_pointer_v<Member>, "Invalid pointer type to non-static member object or function");
  12472. template<typename Class, typename Ret, typename... Args>
  12473. static Class * clazz(Ret(Class:: *)(Args...));
  12474. template<typename Class, typename Ret, typename... Args>
  12475. static Class * clazz(Ret(Class:: *)(Args...) const);
  12476. template<typename Class, typename Type>
  12477. static Class * clazz(Type Class:: *);
  12478. public:
  12479. /*! @brief The class of the given non-static member object or function. */
  12480. using type = std::remove_pointer_t<decltype(clazz(std::declval<Member>()))>;
  12481. };
  12482. /**
  12483. * @brief Helper type.
  12484. * @tparam Member A pointer to a non-static member object or function.
  12485. */
  12486. template<typename Member>
  12487. using member_class_t = typename member_class<Member>::type;
  12488. }
  12489. #endif
  12490. namespace entt {
  12491. /**
  12492. * @brief A SBO friendly, type-safe container for single values of any type.
  12493. * @tparam Len Size of the storage reserved for the small buffer optimization.
  12494. * @tparam Align Optional alignment requirement.
  12495. */
  12496. template<std::size_t Len, std::size_t Align>
  12497. class basic_any {
  12498. enum class operation: std::uint8_t { COPY, MOVE, DTOR, COMP, ADDR, CADDR, TYPE };
  12499. enum class policy: std::uint8_t { OWNER, REF, CREF };
  12500. using storage_type = std::aligned_storage_t<Len + !Len, Align>;
  12501. using vtable_type = const void *(const operation, const basic_any &, void *);
  12502. template<typename Type>
  12503. static constexpr bool in_situ = Len && alignof(Type) <= alignof(storage_type) && sizeof(Type) <= sizeof(storage_type) && std::is_nothrow_move_constructible_v<Type>;
  12504. template<typename Type>
  12505. [[nodiscard]] static constexpr policy type_to_policy() {
  12506. if constexpr(std::is_lvalue_reference_v<Type>) {
  12507. if constexpr(std::is_const_v<std::remove_reference_t<Type>>) {
  12508. return policy::CREF;
  12509. } else {
  12510. return policy::REF;
  12511. }
  12512. } else {
  12513. return policy::OWNER;
  12514. }
  12515. }
  12516. template<typename Type>
  12517. [[nodiscard]] static bool compare(const void *lhs, const void *rhs) {
  12518. if constexpr(!std::is_function_v<Type> && is_equality_comparable_v<Type>) {
  12519. return *static_cast<const Type *>(lhs) == *static_cast<const Type *>(rhs);
  12520. } else {
  12521. return lhs == rhs;
  12522. }
  12523. }
  12524. template<typename Type>
  12525. static const void * basic_vtable([[maybe_unused]] const operation op, [[maybe_unused]] const basic_any &from, [[maybe_unused]] void *to) {
  12526. static_assert(std::is_same_v<std::remove_reference_t<std::remove_const_t<Type>>, Type>, "Invalid type");
  12527. if constexpr(!std::is_void_v<Type>) {
  12528. const Type *instance = (in_situ<Type> && from.mode == policy::OWNER)
  12529. ? ENTT_LAUNDER(reinterpret_cast<const Type *>(&from.storage))
  12530. : static_cast<const Type *>(from.instance);
  12531. switch(op) {
  12532. case operation::COPY:
  12533. if constexpr(std::is_copy_constructible_v<Type>) {
  12534. static_cast<basic_any *>(to)->emplace<Type>(*instance);
  12535. }
  12536. break;
  12537. case operation::MOVE:
  12538. if constexpr(in_situ<Type>) {
  12539. if(from.mode == policy::OWNER) {
  12540. return new (&static_cast<basic_any *>(to)->storage) Type{std::move(*const_cast<Type *>(instance))};
  12541. }
  12542. }
  12543. return (static_cast<basic_any *>(to)->instance = std::exchange(const_cast<basic_any &>(from).instance, nullptr));
  12544. case operation::DTOR:
  12545. if(from.mode == policy::OWNER) {
  12546. if constexpr(in_situ<Type>) {
  12547. instance->~Type();
  12548. } else if constexpr(std::is_array_v<Type>) {
  12549. delete[] instance;
  12550. } else {
  12551. delete instance;
  12552. }
  12553. }
  12554. break;
  12555. case operation::COMP:
  12556. return compare<Type>(instance, (*static_cast<const basic_any **>(to))->data()) ? to : nullptr;
  12557. case operation::ADDR:
  12558. if(from.mode == policy::CREF) {
  12559. return nullptr;
  12560. }
  12561. [[fallthrough]];
  12562. case operation::CADDR:
  12563. return instance;
  12564. case operation::TYPE:
  12565. *static_cast<type_info *>(to) = type_id<Type>();
  12566. break;
  12567. }
  12568. }
  12569. return nullptr;
  12570. }
  12571. template<typename Type, typename... Args>
  12572. void initialize([[maybe_unused]] Args &&... args) {
  12573. if constexpr(!std::is_void_v<Type>) {
  12574. if constexpr(std::is_lvalue_reference_v<Type>) {
  12575. static_assert(sizeof...(Args) == 1u && (std::is_lvalue_reference_v<Args> && ...), "Invalid arguments");
  12576. instance = (std::addressof(args), ...);
  12577. } else if constexpr(in_situ<Type>) {
  12578. if constexpr(sizeof...(Args) != 0u && std::is_aggregate_v<Type>) {
  12579. new (&storage) Type{std::forward<Args>(args)...};
  12580. } else {
  12581. new (&storage) Type(std::forward<Args>(args)...);
  12582. }
  12583. } else {
  12584. if constexpr(sizeof...(Args) != 0u && std::is_aggregate_v<Type>) {
  12585. instance = new Type{std::forward<Args>(args)...};
  12586. } else {
  12587. instance = new Type(std::forward<Args>(args)...);
  12588. }
  12589. }
  12590. }
  12591. }
  12592. basic_any(const basic_any &other, const policy pol) ENTT_NOEXCEPT
  12593. : instance{other.data()},
  12594. vtable{other.vtable},
  12595. mode{pol}
  12596. {}
  12597. public:
  12598. /*! @brief Size of the internal storage. */
  12599. static constexpr auto length = Len;
  12600. /*! @brief Alignment requirement. */
  12601. static constexpr auto alignment = Align;
  12602. /*! @brief Default constructor. */
  12603. basic_any() ENTT_NOEXCEPT
  12604. : instance{},
  12605. vtable{&basic_vtable<void>},
  12606. mode{policy::OWNER}
  12607. {}
  12608. /**
  12609. * @brief Constructs a wrapper by directly initializing the new object.
  12610. * @tparam Type Type of object to use to initialize the wrapper.
  12611. * @tparam Args Types of arguments to use to construct the new instance.
  12612. * @param args Parameters to use to construct the instance.
  12613. */
  12614. template<typename Type, typename... Args>
  12615. explicit basic_any(std::in_place_type_t<Type>, Args &&... args)
  12616. : instance{},
  12617. vtable{&basic_vtable<std::remove_const_t<std::remove_reference_t<Type>>>},
  12618. mode{type_to_policy<Type>()}
  12619. {
  12620. initialize<Type>(std::forward<Args>(args)...);
  12621. }
  12622. /**
  12623. * @brief Constructs a wrapper that holds an unmanaged object.
  12624. * @tparam Type Type of object to use to initialize the wrapper.
  12625. * @param value An instance of an object to use to initialize the wrapper.
  12626. */
  12627. template<typename Type>
  12628. basic_any(std::reference_wrapper<Type> value) ENTT_NOEXCEPT
  12629. : basic_any{}
  12630. {
  12631. // invokes deprecated assignment operator (and avoids issues with vs2017)
  12632. *this = value;
  12633. }
  12634. /**
  12635. * @brief Constructs a wrapper from a given value.
  12636. * @tparam Type Type of object to use to initialize the wrapper.
  12637. * @param value An instance of an object to use to initialize the wrapper.
  12638. */
  12639. template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::decay_t<Type>, basic_any>>>
  12640. basic_any(Type &&value)
  12641. : instance{},
  12642. vtable{&basic_vtable<std::decay_t<Type>>},
  12643. mode{policy::OWNER}
  12644. {
  12645. initialize<std::decay_t<Type>>(std::forward<Type>(value));
  12646. }
  12647. /**
  12648. * @brief Copy constructor.
  12649. * @param other The instance to copy from.
  12650. */
  12651. basic_any(const basic_any &other)
  12652. : instance{},
  12653. vtable{&basic_vtable<void>},
  12654. mode{policy::OWNER}
  12655. {
  12656. other.vtable(operation::COPY, other, this);
  12657. }
  12658. /**
  12659. * @brief Move constructor.
  12660. * @param other The instance to move from.
  12661. */
  12662. basic_any(basic_any &&other) ENTT_NOEXCEPT
  12663. : instance{},
  12664. vtable{other.vtable},
  12665. mode{other.mode}
  12666. {
  12667. vtable(operation::MOVE, other, this);
  12668. }
  12669. /*! @brief Frees the internal storage, whatever it means. */
  12670. ~basic_any() {
  12671. vtable(operation::DTOR, *this, nullptr);
  12672. }
  12673. /**
  12674. * @brief Copy assignment operator.
  12675. * @param other The instance to copy from.
  12676. * @return This any object.
  12677. */
  12678. basic_any & operator=(const basic_any &other) {
  12679. reset();
  12680. other.vtable(operation::COPY, other, this);
  12681. return *this;
  12682. }
  12683. /**
  12684. * @brief Move assignment operator.
  12685. * @param other The instance to move from.
  12686. * @return This any object.
  12687. */
  12688. basic_any & operator=(basic_any &&other) ENTT_NOEXCEPT {
  12689. std::exchange(vtable, other.vtable)(operation::DTOR, *this, nullptr);
  12690. other.vtable(operation::MOVE, other, this);
  12691. mode = other.mode;
  12692. return *this;
  12693. }
  12694. /**
  12695. * @brief Value assignment operator.
  12696. * @tparam Type Type of object to use to initialize the wrapper.
  12697. * @param value An instance of an object to use to initialize the wrapper.
  12698. * @return This any object.
  12699. */
  12700. template<typename Type>
  12701. [[deprecated("Use std::in_place_type<T &>, entt::make_any<T &>, emplace<Type &> or forward_as_any instead")]]
  12702. basic_any & operator=(std::reference_wrapper<Type> value) ENTT_NOEXCEPT {
  12703. emplace<Type &>(value.get());
  12704. return *this;
  12705. }
  12706. /**
  12707. * @brief Value assignment operator.
  12708. * @tparam Type Type of object to use to initialize the wrapper.
  12709. * @param value An instance of an object to use to initialize the wrapper.
  12710. * @return This any object.
  12711. */
  12712. template<typename Type>
  12713. std::enable_if_t<!std::is_same_v<std::decay_t<Type>, basic_any>, basic_any &>
  12714. operator=(Type &&value) {
  12715. emplace<std::decay_t<Type>>(std::forward<Type>(value));
  12716. return *this;
  12717. }
  12718. /**
  12719. * @brief Returns the type of the contained object.
  12720. * @return The type of the contained object, if any.
  12721. */
  12722. [[nodiscard]] type_info type() const ENTT_NOEXCEPT {
  12723. type_info info{};
  12724. vtable(operation::TYPE, *this, &info);
  12725. return info;
  12726. }
  12727. /**
  12728. * @brief Returns an opaque pointer to the contained instance.
  12729. * @return An opaque pointer the contained instance, if any.
  12730. */
  12731. [[nodiscard]] const void * data() const ENTT_NOEXCEPT {
  12732. return vtable(operation::CADDR, *this, nullptr);
  12733. }
  12734. /*! @copydoc data */
  12735. [[nodiscard]] void * data() ENTT_NOEXCEPT {
  12736. return const_cast<void *>(vtable(operation::ADDR, *this, nullptr));
  12737. }
  12738. /**
  12739. * @brief Replaces the contained object by creating a new instance directly.
  12740. * @tparam Type Type of object to use to initialize the wrapper.
  12741. * @tparam Args Types of arguments to use to construct the new instance.
  12742. * @param args Parameters to use to construct the instance.
  12743. */
  12744. template<typename Type, typename... Args>
  12745. void emplace(Args &&... args) {
  12746. std::exchange(vtable, &basic_vtable<std::remove_const_t<std::remove_reference_t<Type>>>)(operation::DTOR, *this, nullptr);
  12747. mode = type_to_policy<Type>();
  12748. initialize<Type>(std::forward<Args>(args)...);
  12749. }
  12750. /*! @brief Destroys contained object */
  12751. void reset() {
  12752. std::exchange(vtable, &basic_vtable<void>)(operation::DTOR, *this, nullptr);
  12753. mode = policy::OWNER;
  12754. }
  12755. /**
  12756. * @brief Returns false if a wrapper is empty, true otherwise.
  12757. * @return False if the wrapper is empty, true otherwise.
  12758. */
  12759. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  12760. return !(vtable(operation::CADDR, *this, nullptr) == nullptr);
  12761. }
  12762. /**
  12763. * @brief Checks if two wrappers differ in their content.
  12764. * @param other Wrapper with which to compare.
  12765. * @return False if the two objects differ in their content, true otherwise.
  12766. */
  12767. bool operator==(const basic_any &other) const ENTT_NOEXCEPT {
  12768. const basic_any *trampoline = &other;
  12769. return type() == other.type() && (vtable(operation::COMP, *this, &trampoline) || !other.data());
  12770. }
  12771. /**
  12772. * @brief Aliasing constructor.
  12773. * @return A wrapper that shares a reference to an unmanaged object.
  12774. */
  12775. [[nodiscard]] basic_any as_ref() ENTT_NOEXCEPT {
  12776. return basic_any{*this, (mode == policy::CREF ? policy::CREF : policy::REF)};
  12777. }
  12778. /*! @copydoc as_ref */
  12779. [[nodiscard]] basic_any as_ref() const ENTT_NOEXCEPT {
  12780. return basic_any{*this, policy::CREF};
  12781. }
  12782. /**
  12783. * @brief Returns true if a wrapper owns its object, false otherwise.
  12784. * @return True if the wrapper owns its object, false otherwise.
  12785. */
  12786. [[nodiscard]] bool owner() const ENTT_NOEXCEPT {
  12787. return (mode == policy::OWNER);
  12788. }
  12789. private:
  12790. union { const void *instance; storage_type storage; };
  12791. vtable_type *vtable;
  12792. policy mode;
  12793. };
  12794. /**
  12795. * @brief Checks if two wrappers differ in their content.
  12796. * @tparam Len Size of the storage reserved for the small buffer optimization.
  12797. * @tparam Align Alignment requirement.
  12798. * @param lhs A wrapper, either empty or not.
  12799. * @param rhs A wrapper, either empty or not.
  12800. * @return True if the two wrappers differ in their content, false otherwise.
  12801. */
  12802. template<std::size_t Len, std::size_t Align>
  12803. [[nodiscard]] inline bool operator!=(const basic_any<Len, Align> &lhs, const basic_any<Len, Align> &rhs) ENTT_NOEXCEPT {
  12804. return !(lhs == rhs);
  12805. }
  12806. /**
  12807. * @brief Performs type-safe access to the contained object.
  12808. * @tparam Type Type to which conversion is required.
  12809. * @tparam Len Size of the storage reserved for the small buffer optimization.
  12810. * @tparam Align Alignment requirement.
  12811. * @param data Target any object.
  12812. * @return The element converted to the requested type.
  12813. */
  12814. template<typename Type, std::size_t Len, std::size_t Align>
  12815. Type any_cast(const basic_any<Len, Align> &data) ENTT_NOEXCEPT {
  12816. const auto * const instance = any_cast<std::remove_reference_t<Type>>(&data);
  12817. ENTT_ASSERT(instance, "Invalid instance");
  12818. return static_cast<Type>(*instance);
  12819. }
  12820. /*! @copydoc any_cast */
  12821. template<typename Type, std::size_t Len, std::size_t Align>
  12822. Type any_cast(basic_any<Len, Align> &data) ENTT_NOEXCEPT {
  12823. // forces const on non-reference types to make them work also with wrappers for const references
  12824. auto * const instance = any_cast<std::remove_reference_t<const Type>>(&data);
  12825. ENTT_ASSERT(instance, "Invalid instance");
  12826. return static_cast<Type>(*instance);
  12827. }
  12828. /*! @copydoc any_cast */
  12829. template<typename Type, std::size_t Len, std::size_t Align>
  12830. Type any_cast(basic_any<Len, Align> &&data) ENTT_NOEXCEPT {
  12831. // forces const on non-reference types to make them work also with wrappers for const references
  12832. auto * const instance = any_cast<std::remove_reference_t<const Type>>(&data);
  12833. ENTT_ASSERT(instance, "Invalid instance");
  12834. return static_cast<Type>(std::move(*instance));
  12835. }
  12836. /*! @copydoc any_cast */
  12837. template<typename Type, std::size_t Len, std::size_t Align>
  12838. const Type * any_cast(const basic_any<Len, Align> *data) ENTT_NOEXCEPT {
  12839. return (data->type() == type_id<Type>() ? static_cast<const Type *>(data->data()) : nullptr);
  12840. }
  12841. /*! @copydoc any_cast */
  12842. template<typename Type, std::size_t Len, std::size_t Align>
  12843. Type * any_cast(basic_any<Len, Align> *data) ENTT_NOEXCEPT {
  12844. // last attempt to make wrappers for const references return their values
  12845. return (data->type() == type_id<Type>() ? static_cast<Type *>(static_cast<constness_as_t<basic_any<Len, Align>, Type> *>(data)->data()) : nullptr);
  12846. }
  12847. /**
  12848. * @brief Constructs a wrapper from a given type, passing it all arguments.
  12849. * @tparam Type Type of object to use to initialize the wrapper.
  12850. * @tparam Len Size of the storage reserved for the small buffer optimization.
  12851. * @tparam Align Optional alignment requirement.
  12852. * @tparam Args Types of arguments to use to construct the new instance.
  12853. * @param args Parameters to use to construct the instance.
  12854. * @return A properly initialized wrapper for an object of the given type.
  12855. */
  12856. template<typename Type, std::size_t Len = basic_any<>::length, std::size_t Align = basic_any<Len>::alignment, typename... Args>
  12857. basic_any<Len, Align> make_any(Args &&... args) {
  12858. return basic_any<Len, Align>{std::in_place_type<Type>, std::forward<Args>(args)...};
  12859. }
  12860. /**
  12861. * @brief Forwards its argument and avoids copies for lvalue references.
  12862. * @tparam Len Size of the storage reserved for the small buffer optimization.
  12863. * @tparam Align Optional alignment requirement.
  12864. * @tparam Type Type of argument to use to construct the new instance.
  12865. * @param value Parameter to use to construct the instance.
  12866. * @return A properly initialized and not necessarily owning wrapper.
  12867. */
  12868. template<std::size_t Len = basic_any<>::length, std::size_t Align = basic_any<Len>::alignment, typename Type>
  12869. basic_any<Len, Align> forward_as_any(Type &&value) {
  12870. return basic_any<Len, Align>{std::in_place_type<std::conditional_t<std::is_rvalue_reference_v<Type>, std::decay_t<Type>, Type>>, std::forward<Type>(value)};
  12871. }
  12872. }
  12873. #endif
  12874. // #include "../core/type_info.hpp"
  12875. #ifndef ENTT_CORE_TYPE_INFO_HPP
  12876. #define ENTT_CORE_TYPE_INFO_HPP
  12877. #include <string_view>
  12878. #include <type_traits>
  12879. // #include "../config/config.h"
  12880. // #include "../core/attribute.h"
  12881. // #include "hashed_string.hpp"
  12882. // #include "fwd.hpp"
  12883. namespace entt {
  12884. /**
  12885. * @cond TURN_OFF_DOXYGEN
  12886. * Internal details not to be documented.
  12887. */
  12888. namespace internal {
  12889. struct ENTT_API type_seq final {
  12890. [[nodiscard]] static id_type next() ENTT_NOEXCEPT {
  12891. static ENTT_MAYBE_ATOMIC(id_type) value{};
  12892. return value++;
  12893. }
  12894. };
  12895. template<typename Type>
  12896. [[nodiscard]] constexpr auto stripped_type_name() ENTT_NOEXCEPT {
  12897. #if defined ENTT_PRETTY_FUNCTION
  12898. std::string_view pretty_function{ENTT_PRETTY_FUNCTION};
  12899. auto first = pretty_function.find_first_not_of(' ', pretty_function.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX)+1);
  12900. auto value = pretty_function.substr(first, pretty_function.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
  12901. return value;
  12902. #else
  12903. return std::string_view{""};
  12904. #endif
  12905. }
  12906. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  12907. [[nodiscard]] static constexpr std::string_view type_name(int) ENTT_NOEXCEPT {
  12908. constexpr auto value = stripped_type_name<Type>();
  12909. return value;
  12910. }
  12911. template<typename Type>
  12912. [[nodiscard]] static std::string_view type_name(char) ENTT_NOEXCEPT {
  12913. static const auto value = stripped_type_name<Type>();
  12914. return value;
  12915. }
  12916. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  12917. [[nodiscard]] static constexpr id_type type_hash(int) ENTT_NOEXCEPT {
  12918. constexpr auto stripped = stripped_type_name<Type>();
  12919. constexpr auto value = hashed_string::value(stripped.data(), stripped.size());
  12920. return value;
  12921. }
  12922. template<typename Type>
  12923. [[nodiscard]] static id_type type_hash(char) ENTT_NOEXCEPT {
  12924. static const auto value = [](const auto stripped) {
  12925. return hashed_string::value(stripped.data(), stripped.size());
  12926. }(stripped_type_name<Type>());
  12927. return value;
  12928. }
  12929. }
  12930. /**
  12931. * Internal details not to be documented.
  12932. * @endcond
  12933. */
  12934. /**
  12935. * @brief Type sequential identifier.
  12936. * @tparam Type Type for which to generate a sequential identifier.
  12937. */
  12938. template<typename Type, typename = void>
  12939. struct ENTT_API type_seq final {
  12940. /**
  12941. * @brief Returns the sequential identifier of a given type.
  12942. * @return The sequential identifier of a given type.
  12943. */
  12944. [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
  12945. static const id_type value = internal::type_seq::next();
  12946. return value;
  12947. }
  12948. /*! @copydoc value */
  12949. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  12950. };
  12951. /**
  12952. * @brief Type hash.
  12953. * @tparam Type Type for which to generate a hash value.
  12954. */
  12955. template<typename Type, typename = void>
  12956. struct type_hash final {
  12957. /**
  12958. * @brief Returns the numeric representation of a given type.
  12959. * @return The numeric representation of the given type.
  12960. */
  12961. #if defined ENTT_PRETTY_FUNCTION
  12962. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  12963. return internal::type_hash<Type>(0);
  12964. #else
  12965. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  12966. return type_seq<Type>::value();
  12967. #endif
  12968. }
  12969. /*! @copydoc value */
  12970. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  12971. };
  12972. /**
  12973. * @brief Type name.
  12974. * @tparam Type Type for which to generate a name.
  12975. */
  12976. template<typename Type, typename = void>
  12977. struct type_name final {
  12978. /**
  12979. * @brief Returns the name of a given type.
  12980. * @return The name of the given type.
  12981. */
  12982. [[nodiscard]] static constexpr std::string_view value() ENTT_NOEXCEPT {
  12983. return internal::type_name<Type>(0);
  12984. }
  12985. /*! @copydoc value */
  12986. [[nodiscard]] constexpr operator std::string_view() const ENTT_NOEXCEPT { return value(); }
  12987. };
  12988. /*! @brief Implementation specific information about a type. */
  12989. class type_info final {
  12990. template<typename>
  12991. friend type_info type_id() ENTT_NOEXCEPT;
  12992. type_info(id_type seq_v, id_type hash_v, std::string_view name_v) ENTT_NOEXCEPT
  12993. : seq_value{seq_v},
  12994. hash_value{hash_v},
  12995. name_value{name_v}
  12996. {}
  12997. public:
  12998. /*! @brief Default constructor. */
  12999. type_info() ENTT_NOEXCEPT
  13000. : type_info({}, {}, {})
  13001. {}
  13002. /*! @brief Default copy constructor. */
  13003. type_info(const type_info &) ENTT_NOEXCEPT = default;
  13004. /*! @brief Default move constructor. */
  13005. type_info(type_info &&) ENTT_NOEXCEPT = default;
  13006. /**
  13007. * @brief Default copy assignment operator.
  13008. * @return This type info object.
  13009. */
  13010. type_info & operator=(const type_info &) ENTT_NOEXCEPT = default;
  13011. /**
  13012. * @brief Default move assignment operator.
  13013. * @return This type info object.
  13014. */
  13015. type_info & operator=(type_info &&) ENTT_NOEXCEPT = default;
  13016. /**
  13017. * @brief Checks if a type info object is properly initialized.
  13018. * @return True if the object is properly initialized, false otherwise.
  13019. */
  13020. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  13021. return name_value.data() != nullptr;
  13022. }
  13023. /**
  13024. * @brief Type sequential identifier.
  13025. * @return Type sequential identifier.
  13026. */
  13027. [[nodiscard]] id_type seq() const ENTT_NOEXCEPT {
  13028. return seq_value;
  13029. }
  13030. /**
  13031. * @brief Type hash.
  13032. * @return Type hash.
  13033. */
  13034. [[nodiscard]] id_type hash() const ENTT_NOEXCEPT {
  13035. return hash_value;
  13036. }
  13037. /**
  13038. * @brief Type name.
  13039. * @return Type name.
  13040. */
  13041. [[nodiscard]] std::string_view name() const ENTT_NOEXCEPT {
  13042. return name_value;
  13043. }
  13044. /**
  13045. * @brief Compares the contents of two type info objects.
  13046. * @param other Object with which to compare.
  13047. * @return False if the two contents differ, true otherwise.
  13048. */
  13049. [[nodiscard]] bool operator==(const type_info &other) const ENTT_NOEXCEPT {
  13050. return hash_value == other.hash_value;
  13051. }
  13052. private:
  13053. id_type seq_value;
  13054. id_type hash_value;
  13055. std::string_view name_value;
  13056. };
  13057. /**
  13058. * @brief Compares the contents of two type info objects.
  13059. * @param lhs A type info object.
  13060. * @param rhs A type info object.
  13061. * @return True if the two contents differ, false otherwise.
  13062. */
  13063. [[nodiscard]] inline bool operator!=(const type_info &lhs, const type_info &rhs) ENTT_NOEXCEPT {
  13064. return !(lhs == rhs);
  13065. }
  13066. /**
  13067. * @brief Returns the type info object for a given type.
  13068. * @tparam Type Type for which to generate a type info object.
  13069. * @return The type info object for the given type.
  13070. */
  13071. template<typename Type>
  13072. [[nodiscard]] type_info type_id() ENTT_NOEXCEPT {
  13073. return type_info{
  13074. type_seq<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  13075. type_hash<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  13076. type_name<std::remove_cv_t<std::remove_reference_t<Type>>>::value()
  13077. };
  13078. }
  13079. }
  13080. #endif
  13081. // #include "../core/type_traits.hpp"
  13082. #ifndef ENTT_CORE_TYPE_TRAITS_HPP
  13083. #define ENTT_CORE_TYPE_TRAITS_HPP
  13084. #include <cstddef>
  13085. #include <iterator>
  13086. #include <type_traits>
  13087. #include <utility>
  13088. // #include "../config/config.h"
  13089. // #include "fwd.hpp"
  13090. namespace entt {
  13091. /**
  13092. * @brief Utility class to disambiguate overloaded functions.
  13093. * @tparam N Number of choices available.
  13094. */
  13095. template<std::size_t N>
  13096. struct choice_t
  13097. // Unfortunately, doxygen cannot parse such a construct.
  13098. /*! @cond TURN_OFF_DOXYGEN */
  13099. : choice_t<N-1>
  13100. /*! @endcond */
  13101. {};
  13102. /*! @copybrief choice_t */
  13103. template<>
  13104. struct choice_t<0> {};
  13105. /**
  13106. * @brief Variable template for the choice trick.
  13107. * @tparam N Number of choices available.
  13108. */
  13109. template<std::size_t N>
  13110. inline constexpr choice_t<N> choice{};
  13111. /**
  13112. * @brief Identity type trait.
  13113. *
  13114. * Useful to establish non-deduced contexts in template argument deduction
  13115. * (waiting for C++20) or to provide types through function arguments.
  13116. *
  13117. * @tparam Type A type.
  13118. */
  13119. template<typename Type>
  13120. struct type_identity {
  13121. /*! @brief Identity type. */
  13122. using type = Type;
  13123. };
  13124. /**
  13125. * @brief Helper type.
  13126. * @tparam Type A type.
  13127. */
  13128. template<typename Type>
  13129. using type_identity_t = typename type_identity<Type>::type;
  13130. /**
  13131. * @brief A type-only `sizeof` wrapper that returns 0 where `sizeof` complains.
  13132. * @tparam Type The type of which to return the size.
  13133. * @tparam The size of the type if `sizeof` accepts it, 0 otherwise.
  13134. */
  13135. template<typename Type, typename = void>
  13136. struct size_of: std::integral_constant<std::size_t, 0u> {};
  13137. /*! @copydoc size_of */
  13138. template<typename Type>
  13139. struct size_of<Type, std::void_t<decltype(sizeof(Type))>>
  13140. : std::integral_constant<std::size_t, sizeof(Type)>
  13141. {};
  13142. /**
  13143. * @brief Helper variable template.
  13144. * @tparam Type The type of which to return the size.
  13145. */
  13146. template<class Type>
  13147. inline constexpr std::size_t size_of_v = size_of<Type>::value;
  13148. /**
  13149. * @brief Using declaration to be used to _repeat_ the same type a number of
  13150. * times equal to the size of a given parameter pack.
  13151. * @tparam Type A type to repeat.
  13152. */
  13153. template<typename Type, typename>
  13154. using unpack_as_t = Type;
  13155. /**
  13156. * @brief Helper variable template to be used to _repeat_ the same value a
  13157. * number of times equal to the size of a given parameter pack.
  13158. * @tparam Value A value to repeat.
  13159. */
  13160. template<auto Value, typename>
  13161. inline constexpr auto unpack_as_v = Value;
  13162. /**
  13163. * @brief Wraps a static constant.
  13164. * @tparam Value A static constant.
  13165. */
  13166. template<auto Value>
  13167. using integral_constant = std::integral_constant<decltype(Value), Value>;
  13168. /**
  13169. * @brief Alias template to facilitate the creation of named values.
  13170. * @tparam Value A constant value at least convertible to `id_type`.
  13171. */
  13172. template<id_type Value>
  13173. using tag = integral_constant<Value>;
  13174. /**
  13175. * @brief A class to use to push around lists of types, nothing more.
  13176. * @tparam Type Types provided by the type list.
  13177. */
  13178. template<typename... Type>
  13179. struct type_list {
  13180. /*! @brief Type list type. */
  13181. using type = type_list;
  13182. /*! @brief Compile-time number of elements in the type list. */
  13183. static constexpr auto size = sizeof...(Type);
  13184. };
  13185. /*! @brief Primary template isn't defined on purpose. */
  13186. template<std::size_t, typename>
  13187. struct type_list_element;
  13188. /**
  13189. * @brief Provides compile-time indexed access to the types of a type list.
  13190. * @tparam Index Index of the type to return.
  13191. * @tparam Type First type provided by the type list.
  13192. * @tparam Other Other types provided by the type list.
  13193. */
  13194. template<std::size_t Index, typename Type, typename... Other>
  13195. struct type_list_element<Index, type_list<Type, Other...>>
  13196. : type_list_element<Index - 1u, type_list<Other...>>
  13197. {};
  13198. /**
  13199. * @brief Provides compile-time indexed access to the types of a type list.
  13200. * @tparam Type First type provided by the type list.
  13201. * @tparam Other Other types provided by the type list.
  13202. */
  13203. template<typename Type, typename... Other>
  13204. struct type_list_element<0u, type_list<Type, Other...>> {
  13205. /*! @brief Searched type. */
  13206. using type = Type;
  13207. };
  13208. /**
  13209. * @brief Helper type.
  13210. * @tparam Index Index of the type to return.
  13211. * @tparam List Type list to search into.
  13212. */
  13213. template<std::size_t Index, typename List>
  13214. using type_list_element_t = typename type_list_element<Index, List>::type;
  13215. /**
  13216. * @brief Concatenates multiple type lists.
  13217. * @tparam Type Types provided by the first type list.
  13218. * @tparam Other Types provided by the second type list.
  13219. * @return A type list composed by the types of both the type lists.
  13220. */
  13221. template<typename... Type, typename... Other>
  13222. constexpr type_list<Type..., Other...> operator+(type_list<Type...>, type_list<Other...>) { return {}; }
  13223. /*! @brief Primary template isn't defined on purpose. */
  13224. template<typename...>
  13225. struct type_list_cat;
  13226. /*! @brief Concatenates multiple type lists. */
  13227. template<>
  13228. struct type_list_cat<> {
  13229. /*! @brief A type list composed by the types of all the type lists. */
  13230. using type = type_list<>;
  13231. };
  13232. /**
  13233. * @brief Concatenates multiple type lists.
  13234. * @tparam Type Types provided by the first type list.
  13235. * @tparam Other Types provided by the second type list.
  13236. * @tparam List Other type lists, if any.
  13237. */
  13238. template<typename... Type, typename... Other, typename... List>
  13239. struct type_list_cat<type_list<Type...>, type_list<Other...>, List...> {
  13240. /*! @brief A type list composed by the types of all the type lists. */
  13241. using type = typename type_list_cat<type_list<Type..., Other...>, List...>::type;
  13242. };
  13243. /**
  13244. * @brief Concatenates multiple type lists.
  13245. * @tparam Type Types provided by the type list.
  13246. */
  13247. template<typename... Type>
  13248. struct type_list_cat<type_list<Type...>> {
  13249. /*! @brief A type list composed by the types of all the type lists. */
  13250. using type = type_list<Type...>;
  13251. };
  13252. /**
  13253. * @brief Helper type.
  13254. * @tparam List Type lists to concatenate.
  13255. */
  13256. template<typename... List>
  13257. using type_list_cat_t = typename type_list_cat<List...>::type;
  13258. /*! @brief Primary template isn't defined on purpose. */
  13259. template<typename>
  13260. struct type_list_unique;
  13261. /**
  13262. * @brief Removes duplicates types from a type list.
  13263. * @tparam Type One of the types provided by the given type list.
  13264. * @tparam Other The other types provided by the given type list.
  13265. */
  13266. template<typename Type, typename... Other>
  13267. struct type_list_unique<type_list<Type, Other...>> {
  13268. /*! @brief A type list without duplicate types. */
  13269. using type = std::conditional_t<
  13270. std::disjunction_v<std::is_same<Type, Other>...>,
  13271. typename type_list_unique<type_list<Other...>>::type,
  13272. type_list_cat_t<type_list<Type>, typename type_list_unique<type_list<Other...>>::type>
  13273. >;
  13274. };
  13275. /*! @brief Removes duplicates types from a type list. */
  13276. template<>
  13277. struct type_list_unique<type_list<>> {
  13278. /*! @brief A type list without duplicate types. */
  13279. using type = type_list<>;
  13280. };
  13281. /**
  13282. * @brief Helper type.
  13283. * @tparam Type A type list.
  13284. */
  13285. template<typename Type>
  13286. using type_list_unique_t = typename type_list_unique<Type>::type;
  13287. /**
  13288. * @brief Provides the member constant `value` to true if a type list contains a
  13289. * given type, false otherwise.
  13290. * @tparam List Type list.
  13291. * @tparam Type Type to look for.
  13292. */
  13293. template<typename List, typename Type>
  13294. struct type_list_contains;
  13295. /**
  13296. * @copybrief type_list_contains
  13297. * @tparam Type Types provided by the type list.
  13298. * @tparam Other Type to look for.
  13299. */
  13300. template<typename... Type, typename Other>
  13301. struct type_list_contains<type_list<Type...>, Other>: std::disjunction<std::is_same<Type, Other>...> {};
  13302. /**
  13303. * @brief Helper variable template.
  13304. * @tparam List Type list.
  13305. * @tparam Type Type to look for.
  13306. */
  13307. template<class List, typename Type>
  13308. inline constexpr bool type_list_contains_v = type_list_contains<List, Type>::value;
  13309. /*! @brief Primary template isn't defined on purpose. */
  13310. template<typename...>
  13311. struct type_list_diff;
  13312. /**
  13313. * @brief Computes the difference between two type lists.
  13314. * @tparam Type Types provided by the first type list.
  13315. * @tparam Other Types provided by the second type list.
  13316. */
  13317. template<typename... Type, typename... Other>
  13318. struct type_list_diff<type_list<Type...>, type_list<Other...>> {
  13319. /*! @brief A type list that is the difference between the two type lists. */
  13320. using type = type_list_cat_t<std::conditional_t<type_list_contains_v<type_list<Other...>, Type>, type_list<>, type_list<Type>>...>;
  13321. };
  13322. /**
  13323. * @brief Helper type.
  13324. * @tparam List Type lists between which to compute the difference.
  13325. */
  13326. template<typename... List>
  13327. using type_list_diff_t = typename type_list_diff<List...>::type;
  13328. /**
  13329. * @brief A class to use to push around lists of constant values, nothing more.
  13330. * @tparam Value Values provided by the value list.
  13331. */
  13332. template<auto... Value>
  13333. struct value_list {
  13334. /*! @brief Value list type. */
  13335. using type = value_list;
  13336. /*! @brief Compile-time number of elements in the value list. */
  13337. static constexpr auto size = sizeof...(Value);
  13338. };
  13339. /*! @brief Primary template isn't defined on purpose. */
  13340. template<std::size_t, typename>
  13341. struct value_list_element;
  13342. /**
  13343. * @brief Provides compile-time indexed access to the values of a value list.
  13344. * @tparam Index Index of the value to return.
  13345. * @tparam Value First value provided by the value list.
  13346. * @tparam Other Other values provided by the value list.
  13347. */
  13348. template<std::size_t Index, auto Value, auto... Other>
  13349. struct value_list_element<Index, value_list<Value, Other...>>
  13350. : value_list_element<Index - 1u, value_list<Other...>>
  13351. {};
  13352. /**
  13353. * @brief Provides compile-time indexed access to the types of a type list.
  13354. * @tparam Value First value provided by the value list.
  13355. * @tparam Other Other values provided by the value list.
  13356. */
  13357. template<auto Value, auto... Other>
  13358. struct value_list_element<0u, value_list<Value, Other...>> {
  13359. /*! @brief Searched value. */
  13360. static constexpr auto value = Value;
  13361. };
  13362. /**
  13363. * @brief Helper type.
  13364. * @tparam Index Index of the value to return.
  13365. * @tparam List Value list to search into.
  13366. */
  13367. template<std::size_t Index, typename List>
  13368. inline constexpr auto value_list_element_v = value_list_element<Index, List>::value;
  13369. /**
  13370. * @brief Concatenates multiple value lists.
  13371. * @tparam Value Values provided by the first value list.
  13372. * @tparam Other Values provided by the second value list.
  13373. * @return A value list composed by the values of both the value lists.
  13374. */
  13375. template<auto... Value, auto... Other>
  13376. constexpr value_list<Value..., Other...> operator+(value_list<Value...>, value_list<Other...>) { return {}; }
  13377. /*! @brief Primary template isn't defined on purpose. */
  13378. template<typename...>
  13379. struct value_list_cat;
  13380. /*! @brief Concatenates multiple value lists. */
  13381. template<>
  13382. struct value_list_cat<> {
  13383. /*! @brief A value list composed by the values of all the value lists. */
  13384. using type = value_list<>;
  13385. };
  13386. /**
  13387. * @brief Concatenates multiple value lists.
  13388. * @tparam Value Values provided by the first value list.
  13389. * @tparam Other Values provided by the second value list.
  13390. * @tparam List Other value lists, if any.
  13391. */
  13392. template<auto... Value, auto... Other, typename... List>
  13393. struct value_list_cat<value_list<Value...>, value_list<Other...>, List...> {
  13394. /*! @brief A value list composed by the values of all the value lists. */
  13395. using type = typename value_list_cat<value_list<Value..., Other...>, List...>::type;
  13396. };
  13397. /**
  13398. * @brief Concatenates multiple value lists.
  13399. * @tparam Value Values provided by the value list.
  13400. */
  13401. template<auto... Value>
  13402. struct value_list_cat<value_list<Value...>> {
  13403. /*! @brief A value list composed by the values of all the value lists. */
  13404. using type = value_list<Value...>;
  13405. };
  13406. /**
  13407. * @brief Helper type.
  13408. * @tparam List Value lists to concatenate.
  13409. */
  13410. template<typename... List>
  13411. using value_list_cat_t = typename value_list_cat<List...>::type;
  13412. /**
  13413. * @cond TURN_OFF_DOXYGEN
  13414. * Internal details not to be documented.
  13415. */
  13416. namespace internal {
  13417. template<typename>
  13418. [[nodiscard]] constexpr bool is_equality_comparable(...) { return false; }
  13419. template<typename Type>
  13420. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<0>)
  13421. -> decltype(std::declval<Type>() == std::declval<Type>()) { return true; }
  13422. template<typename Type>
  13423. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<1>)
  13424. -> decltype(std::declval<typename Type::value_type>(), std::declval<Type>() == std::declval<Type>()) {
  13425. if constexpr(std::is_same_v<typename Type::value_type, Type>) {
  13426. return is_equality_comparable<Type>(choice<0>);
  13427. } else {
  13428. return is_equality_comparable<typename Type::value_type>(choice<2>);
  13429. }
  13430. }
  13431. template<typename Type>
  13432. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<2>)
  13433. -> decltype(std::declval<typename Type::mapped_type>(), std::declval<Type>() == std::declval<Type>()) {
  13434. return is_equality_comparable<typename Type::key_type>(choice<2>) && is_equality_comparable<typename Type::mapped_type>(choice<2>);
  13435. }
  13436. }
  13437. /**
  13438. * Internal details not to be documented.
  13439. * @endcond
  13440. */
  13441. /**
  13442. * @brief Provides the member constant `value` to true if a given type is
  13443. * equality comparable, false otherwise.
  13444. * @tparam Type The type to test.
  13445. */
  13446. template<typename Type, typename = void>
  13447. struct is_equality_comparable: std::bool_constant<internal::is_equality_comparable<Type>(choice<2>)> {};
  13448. /**
  13449. * @brief Helper variable template.
  13450. * @tparam Type The type to test.
  13451. */
  13452. template<class Type>
  13453. inline constexpr bool is_equality_comparable_v = is_equality_comparable<Type>::value;
  13454. /*! @brief Same as std::is_invocable, but with tuples. */
  13455. template<typename, typename>
  13456. struct is_applicable: std::false_type {};
  13457. /**
  13458. * @copybrief is_applicable
  13459. * @tparam Func A valid function type.
  13460. * @tparam Tuple Tuple-like type.
  13461. * @tparam Args The list of arguments to use to probe the function type.
  13462. */
  13463. template<typename Func, template<typename...> class Tuple, typename... Args>
  13464. struct is_applicable<Func, Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  13465. /**
  13466. * @copybrief is_applicable
  13467. * @tparam Func A valid function type.
  13468. * @tparam Tuple Tuple-like type.
  13469. * @tparam Args The list of arguments to use to probe the function type.
  13470. */
  13471. template<typename Func, template<typename...> class Tuple, typename... Args>
  13472. struct is_applicable<Func, const Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  13473. /**
  13474. * @brief Helper variable template.
  13475. * @tparam Func A valid function type.
  13476. * @tparam Args The list of arguments to use to probe the function type.
  13477. */
  13478. template<typename Func, typename Args>
  13479. inline constexpr bool is_applicable_v = is_applicable<Func, Args>::value;
  13480. /*! @brief Same as std::is_invocable_r, but with tuples for arguments. */
  13481. template<typename, typename, typename>
  13482. struct is_applicable_r: std::false_type {};
  13483. /**
  13484. * @copybrief is_applicable_r
  13485. * @tparam Ret The type to which the return type of the function should be
  13486. * convertible.
  13487. * @tparam Func A valid function type.
  13488. * @tparam Args The list of arguments to use to probe the function type.
  13489. */
  13490. template<typename Ret, typename Func, typename... Args>
  13491. struct is_applicable_r<Ret, Func, std::tuple<Args...>>: std::is_invocable_r<Ret, Func, Args...> {};
  13492. /**
  13493. * @brief Helper variable template.
  13494. * @tparam Ret The type to which the return type of the function should be
  13495. * convertible.
  13496. * @tparam Func A valid function type.
  13497. * @tparam Args The list of arguments to use to probe the function type.
  13498. */
  13499. template<typename Ret, typename Func, typename Args>
  13500. inline constexpr bool is_applicable_r_v = is_applicable_r<Ret, Func, Args>::value;
  13501. /**
  13502. * @brief Provides the member constant `value` to true if a given type is
  13503. * complete, false otherwise.
  13504. * @tparam Type The type to test.
  13505. */
  13506. template<typename Type, typename = void>
  13507. struct is_complete: std::false_type {};
  13508. /*! @copydoc is_complete */
  13509. template<typename Type>
  13510. struct is_complete<Type, std::void_t<decltype(sizeof(Type))>>: std::true_type {};
  13511. /**
  13512. * @brief Helper variable template.
  13513. * @tparam Type The type to test.
  13514. */
  13515. template<typename Type>
  13516. inline constexpr bool is_complete_v = is_complete<Type>::value;
  13517. /**
  13518. * @brief Provides the member constant `value` to true if a given type is an
  13519. * iterator, false otherwise.
  13520. * @tparam Type The type to test.
  13521. */
  13522. template<typename Type, typename = void>
  13523. struct is_iterator: std::false_type {};
  13524. /*! @copydoc is_iterator */
  13525. template<typename Type>
  13526. struct is_iterator<Type, std::void_t<typename std::iterator_traits<Type>::iterator_category>>
  13527. : std::true_type
  13528. {};
  13529. /**
  13530. * @brief Helper variable template.
  13531. * @tparam Type The type to test.
  13532. */
  13533. template<typename Type>
  13534. inline constexpr bool is_iterator_v = is_iterator<Type>::value;
  13535. /**
  13536. * @brief Provides the member constant `value` to true if a given type is of the
  13537. * required iterator type, false otherwise.
  13538. * @tparam Type The type to test.
  13539. * @tparam It Required iterator type.
  13540. */
  13541. template<typename Type, typename It, typename = void>
  13542. struct is_iterator_type: std::false_type {};
  13543. /*! @copydoc is_iterator_type */
  13544. template<typename Type, typename It>
  13545. struct is_iterator_type<Type, It, std::enable_if_t<is_iterator_v<Type> && std::is_same_v<Type, It>>>
  13546. : std::true_type
  13547. {};
  13548. /*! @copydoc is_iterator_type */
  13549. template<typename Type, typename It>
  13550. struct is_iterator_type<Type, It, std::enable_if_t<!std::is_same_v<Type, It>, std::void_t<typename It::iterator_type>>>
  13551. : is_iterator_type<Type, typename It::iterator_type>
  13552. {};
  13553. /**
  13554. * @brief Helper variable template.
  13555. * @tparam Type The type to test.
  13556. * @tparam It Required iterator type.
  13557. */
  13558. template<typename Type, typename It>
  13559. inline constexpr bool is_iterator_type_v = is_iterator_type<Type, It>::value;
  13560. /**
  13561. * @brief Transcribes the constness of a type to another type.
  13562. * @tparam To The type to which to transcribe the constness.
  13563. * @tparam From The type from which to transcribe the constness.
  13564. */
  13565. template<typename To, typename From>
  13566. struct constness_as {
  13567. /*! @brief The type resulting from the transcription of the constness. */
  13568. using type = std::remove_const_t<To>;
  13569. };
  13570. /*! @copydoc constness_as */
  13571. template<typename To, typename From>
  13572. struct constness_as<To, const From> {
  13573. /*! @brief The type resulting from the transcription of the constness. */
  13574. using type = std::add_const_t<To>;
  13575. };
  13576. /**
  13577. * @brief Alias template to facilitate the transcription of the constness.
  13578. * @tparam To The type to which to transcribe the constness.
  13579. * @tparam From The type from which to transcribe the constness.
  13580. */
  13581. template<typename To, typename From>
  13582. using constness_as_t = typename constness_as<To, From>::type;
  13583. /**
  13584. * @brief Extracts the class of a non-static member object or function.
  13585. * @tparam Member A pointer to a non-static member object or function.
  13586. */
  13587. template<typename Member>
  13588. class member_class {
  13589. static_assert(std::is_member_pointer_v<Member>, "Invalid pointer type to non-static member object or function");
  13590. template<typename Class, typename Ret, typename... Args>
  13591. static Class * clazz(Ret(Class:: *)(Args...));
  13592. template<typename Class, typename Ret, typename... Args>
  13593. static Class * clazz(Ret(Class:: *)(Args...) const);
  13594. template<typename Class, typename Type>
  13595. static Class * clazz(Type Class:: *);
  13596. public:
  13597. /*! @brief The class of the given non-static member object or function. */
  13598. using type = std::remove_pointer_t<decltype(clazz(std::declval<Member>()))>;
  13599. };
  13600. /**
  13601. * @brief Helper type.
  13602. * @tparam Member A pointer to a non-static member object or function.
  13603. */
  13604. template<typename Member>
  13605. using member_class_t = typename member_class<Member>::type;
  13606. }
  13607. #endif
  13608. // #include "fwd.hpp"
  13609. #ifndef ENTT_POLY_FWD_HPP
  13610. #define ENTT_POLY_FWD_HPP
  13611. #include <type_traits>
  13612. namespace entt {
  13613. template<typename, std::size_t Len, std::size_t = alignof(typename std::aligned_storage_t<Len + !Len>)>
  13614. class basic_poly;
  13615. /**
  13616. * @brief Alias declaration for the most common use case.
  13617. * @tparam Concept Concept descriptor.
  13618. */
  13619. template<typename Concept>
  13620. using poly = basic_poly<Concept, sizeof(double[2])>;
  13621. }
  13622. #endif
  13623. namespace entt {
  13624. /*! @brief Inspector class used to infer the type of the virtual table. */
  13625. struct poly_inspector {
  13626. /**
  13627. * @brief Generic conversion operator (definition only).
  13628. * @tparam Type Type to which conversion is requested.
  13629. */
  13630. template <class Type>
  13631. operator Type &&() const;
  13632. /**
  13633. * @brief Dummy invocation function (definition only).
  13634. * @tparam Member Index of the function to invoke.
  13635. * @tparam Args Types of arguments to pass to the function.
  13636. * @param args The arguments to pass to the function.
  13637. * @return A poly inspector convertible to any type.
  13638. */
  13639. template<auto Member, typename... Args>
  13640. poly_inspector invoke(Args &&... args) const;
  13641. /*! @copydoc invoke */
  13642. template<auto Member, typename... Args>
  13643. poly_inspector invoke(Args &&... args);
  13644. };
  13645. /**
  13646. * @brief Static virtual table factory.
  13647. * @tparam Concept Concept descriptor.
  13648. * @tparam Len Size of the storage reserved for the small buffer optimization.
  13649. * @tparam Align Alignment requirement.
  13650. */
  13651. template<typename Concept, std::size_t Len, std::size_t Align>
  13652. class poly_vtable {
  13653. using inspector = typename Concept::template type<poly_inspector>;
  13654. template<typename Ret, typename... Args>
  13655. static auto vtable_entry(Ret(*)(inspector &, Args...)) -> Ret(*)(basic_any<Len, Align> &, Args...);
  13656. template<typename Ret, typename... Args>
  13657. static auto vtable_entry(Ret(*)(const inspector &, Args...)) -> Ret(*)(const basic_any<Len, Align> &, Args...);
  13658. template<typename Ret, typename... Args>
  13659. static auto vtable_entry(Ret(*)(Args...)) -> Ret(*)(const basic_any<Len, Align> &, Args...);
  13660. template<typename Ret, typename... Args>
  13661. static auto vtable_entry(Ret(inspector:: *)(Args...)) -> Ret(*)(basic_any<Len, Align> &, Args...);
  13662. template<typename Ret, typename... Args>
  13663. static auto vtable_entry(Ret(inspector:: *)(Args...) const) -> Ret(*)(const basic_any<Len, Align> &, Args...);
  13664. template<auto... Candidate>
  13665. static auto make_vtable(value_list<Candidate...>)
  13666. -> decltype(std::make_tuple(vtable_entry(Candidate)...));
  13667. template<typename... Func>
  13668. [[nodiscard]] static constexpr auto make_vtable(type_list<Func...>) {
  13669. if constexpr(sizeof...(Func) == 0) {
  13670. return decltype(make_vtable(typename Concept::template impl<inspector>{})){};
  13671. } else if constexpr((std::is_function_v<Func> && ...)) {
  13672. return decltype(std::make_tuple(vtable_entry(std::declval<Func inspector:: *>())...)){};
  13673. }
  13674. }
  13675. template<typename Type, auto Candidate, typename Ret, typename Any, typename... Args>
  13676. static void fill_vtable_entry(Ret(* &entry)(Any &, Args...)) {
  13677. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Args...>) {
  13678. entry = +[](Any &, Args... args) -> Ret {
  13679. return std::invoke(Candidate, std::forward<Args>(args)...);
  13680. };
  13681. } else {
  13682. entry = +[](Any &instance, Args... args) -> Ret {
  13683. return static_cast<Ret>(std::invoke(Candidate, any_cast<constness_as_t<Type, Any> &>(instance), std::forward<Args>(args)...));
  13684. };
  13685. }
  13686. }
  13687. template<typename Type, auto... Index>
  13688. [[nodiscard]] static auto fill_vtable(std::index_sequence<Index...>) {
  13689. type impl{};
  13690. (fill_vtable_entry<Type, value_list_element_v<Index, typename Concept::template impl<Type>>>(std::get<Index>(impl)), ...);
  13691. return impl;
  13692. }
  13693. public:
  13694. /*! @brief Virtual table type. */
  13695. using type = decltype(make_vtable(Concept{}));
  13696. /**
  13697. * @brief Returns a static virtual table for a specific concept and type.
  13698. * @tparam Type The type for which to generate the virtual table.
  13699. * @return A static virtual table for the given concept and type.
  13700. */
  13701. template<typename Type>
  13702. [[nodiscard]] static const auto * instance() {
  13703. static_assert(std::is_same_v<Type, std::decay_t<Type>>, "Type differs from its decayed form");
  13704. static const auto vtable = fill_vtable<Type>(std::make_index_sequence<Concept::template impl<Type>::size>{});
  13705. return &vtable;
  13706. }
  13707. };
  13708. /**
  13709. * @brief Poly base class used to inject functionalities into concepts.
  13710. * @tparam Poly The outermost poly class.
  13711. */
  13712. template<typename Poly>
  13713. struct poly_base {
  13714. /**
  13715. * @brief Invokes a function from the static virtual table.
  13716. * @tparam Member Index of the function to invoke.
  13717. * @tparam Args Types of arguments to pass to the function.
  13718. * @param self A reference to the poly object that made the call.
  13719. * @param args The arguments to pass to the function.
  13720. * @return The return value of the invoked function, if any.
  13721. */
  13722. template<auto Member, typename... Args>
  13723. [[nodiscard]] decltype(auto) invoke(const poly_base &self, Args &&... args) const {
  13724. const auto &poly = static_cast<const Poly &>(self);
  13725. return std::get<Member>(*poly.vtable)(poly.storage, std::forward<Args>(args)...);
  13726. }
  13727. /*! @copydoc invoke */
  13728. template<auto Member, typename... Args>
  13729. [[nodiscard]] decltype(auto) invoke(poly_base &self, Args &&... args) {
  13730. auto &poly = static_cast<Poly &>(self);
  13731. return std::get<Member>(*poly.vtable)(poly.storage, std::forward<Args>(args)...);
  13732. }
  13733. };
  13734. /**
  13735. * @brief Shortcut for calling `poly_base<Type>::invoke`.
  13736. * @tparam Member Index of the function to invoke.
  13737. * @tparam Poly A fully defined poly object.
  13738. * @tparam Args Types of arguments to pass to the function.
  13739. * @param self A reference to the poly object that made the call.
  13740. * @param args The arguments to pass to the function.
  13741. * @return The return value of the invoked function, if any.
  13742. */
  13743. template<auto Member, typename Poly, typename... Args>
  13744. decltype(auto) poly_call(Poly &&self, Args &&... args) {
  13745. return std::forward<Poly>(self).template invoke<Member>(self, std::forward<Args>(args)...);
  13746. }
  13747. /**
  13748. * @brief Static polymorphism made simple and within everyone's reach.
  13749. *
  13750. * Static polymorphism is a very powerful tool in C++, albeit sometimes
  13751. * cumbersome to obtain.<br/>
  13752. * This class aims to make it simple and easy to use.
  13753. *
  13754. * @note
  13755. * Both deduced and defined static virtual tables are supported.<br/>
  13756. * Moreover, the `poly` class template also works with unmanaged objects.
  13757. *
  13758. * @tparam Concept Concept descriptor.
  13759. * @tparam Len Size of the storage reserved for the small buffer optimization.
  13760. * @tparam Align Optional alignment requirement.
  13761. */
  13762. template<typename Concept, std::size_t Len, std::size_t Align>
  13763. class basic_poly: private Concept::template type<poly_base<basic_poly<Concept, Len, Align>>> {
  13764. /*! @brief A poly base is allowed to snoop into a poly object. */
  13765. friend struct poly_base<basic_poly>;
  13766. using vtable_type = typename poly_vtable<Concept, Len, Align>::type;
  13767. public:
  13768. /*! @brief Concept type. */
  13769. using concept_type = typename Concept::template type<poly_base<basic_poly>>;
  13770. /*! @brief Default constructor. */
  13771. basic_poly() ENTT_NOEXCEPT
  13772. : storage{},
  13773. vtable{}
  13774. {}
  13775. /**
  13776. * @brief Constructs a poly by directly initializing the new object.
  13777. * @tparam Type Type of object to use to initialize the poly.
  13778. * @tparam Args Types of arguments to use to construct the new instance.
  13779. * @param args Parameters to use to construct the instance.
  13780. */
  13781. template<typename Type, typename... Args>
  13782. explicit basic_poly(std::in_place_type_t<Type>, Args &&... args)
  13783. : storage{std::in_place_type<Type>, std::forward<Args>(args)...},
  13784. vtable{poly_vtable<Concept, Len, Align>::template instance<std::remove_const_t<std::remove_reference_t<Type>>>()}
  13785. {}
  13786. /**
  13787. * @brief Constructs a poly from a given value.
  13788. * @tparam Type Type of object to use to initialize the poly.
  13789. * @param value An instance of an object to use to initialize the poly.
  13790. */
  13791. template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::remove_cv_t<std::remove_reference_t<Type>>, basic_poly>>>
  13792. basic_poly(Type &&value) ENTT_NOEXCEPT
  13793. : basic_poly{std::in_place_type<std::remove_cv_t<std::remove_reference_t<Type>>>, std::forward<Type>(value)}
  13794. {}
  13795. /**
  13796. * @brief Copy constructor.
  13797. * @param other The instance to copy from.
  13798. */
  13799. basic_poly(const basic_poly &other) = default;
  13800. /**
  13801. * @brief Move constructor.
  13802. * @param other The instance to move from.
  13803. */
  13804. basic_poly(basic_poly &&other) ENTT_NOEXCEPT
  13805. : basic_poly{}
  13806. {
  13807. swap(*this, other);
  13808. }
  13809. /**
  13810. * @brief Assignment operator.
  13811. * @param other The instance to assign from.
  13812. * @return This poly object.
  13813. */
  13814. basic_poly & operator=(basic_poly other) {
  13815. swap(other, *this);
  13816. return *this;
  13817. }
  13818. /**
  13819. * @brief Returns the type of the contained object.
  13820. * @return The type of the contained object, if any.
  13821. */
  13822. [[nodiscard]] type_info type() const ENTT_NOEXCEPT {
  13823. return storage.type();
  13824. }
  13825. /**
  13826. * @brief Returns an opaque pointer to the contained instance.
  13827. * @return An opaque pointer the contained instance, if any.
  13828. */
  13829. [[nodiscard]] const void * data() const ENTT_NOEXCEPT {
  13830. return storage.data();
  13831. }
  13832. /*! @copydoc data */
  13833. [[nodiscard]] void * data() ENTT_NOEXCEPT {
  13834. return storage.data();
  13835. }
  13836. /**
  13837. * @brief Replaces the contained object by creating a new instance directly.
  13838. * @tparam Type Type of object to use to initialize the poly.
  13839. * @tparam Args Types of arguments to use to construct the new instance.
  13840. * @param args Parameters to use to construct the instance.
  13841. */
  13842. template<typename Type, typename... Args>
  13843. void emplace(Args &&... args) {
  13844. *this = basic_poly{std::in_place_type<Type>, std::forward<Args>(args)...};
  13845. }
  13846. /*! @brief Destroys contained object */
  13847. void reset() {
  13848. *this = basic_poly{};
  13849. }
  13850. /**
  13851. * @brief Returns false if a poly is empty, true otherwise.
  13852. * @return False if the poly is empty, true otherwise.
  13853. */
  13854. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  13855. return !(vtable == nullptr);
  13856. }
  13857. /**
  13858. * @brief Returns a pointer to the underlying concept.
  13859. * @return A pointer to the underlying concept.
  13860. */
  13861. [[nodiscard]] concept_type * operator->() ENTT_NOEXCEPT {
  13862. return this;
  13863. }
  13864. /*! @copydoc operator-> */
  13865. [[nodiscard]] const concept_type * operator->() const ENTT_NOEXCEPT {
  13866. return this;
  13867. }
  13868. /**
  13869. * @brief Swaps two poly objects.
  13870. * @param lhs A valid poly object.
  13871. * @param rhs A valid poly object.
  13872. */
  13873. friend void swap(basic_poly &lhs, basic_poly &rhs) {
  13874. using std::swap;
  13875. swap(lhs.storage, rhs.storage);
  13876. swap(lhs.vtable, rhs.vtable);
  13877. }
  13878. /**
  13879. * @brief Aliasing constructor.
  13880. * @return A poly that shares a reference to an unmanaged object.
  13881. */
  13882. [[nodiscard]] basic_poly as_ref() ENTT_NOEXCEPT {
  13883. basic_poly ref = std::as_const(*this).as_ref();
  13884. ref.storage = storage.as_ref();
  13885. return ref;
  13886. }
  13887. /*! @copydoc as_ref */
  13888. [[nodiscard]] basic_poly as_ref() const ENTT_NOEXCEPT {
  13889. basic_poly ref{};
  13890. ref.storage = storage.as_ref();
  13891. ref.vtable = vtable;
  13892. return ref;
  13893. }
  13894. private:
  13895. basic_any<Len, Align> storage;
  13896. const vtable_type *vtable;
  13897. };
  13898. }
  13899. #endif
  13900. // #include "fwd.hpp"
  13901. namespace entt {
  13902. /**
  13903. * @brief Basic poly storage implementation.
  13904. * @tparam Entity A valid entity type (see entt_traits for more details).
  13905. */
  13906. template<typename Entity>
  13907. struct Storage: type_list<type_info() const ENTT_NOEXCEPT> {
  13908. /**
  13909. * @brief Concept definition.
  13910. * @tparam Base Opaque base class from which to inherit.
  13911. */
  13912. template<typename Base>
  13913. struct type: Base {
  13914. /**
  13915. * @brief Returns a type info for the contained objects.
  13916. * @return The type info for the contained objects.
  13917. */
  13918. type_info value_type() const ENTT_NOEXCEPT {
  13919. return poly_call<0>(*this);
  13920. }
  13921. };
  13922. /**
  13923. * @brief Concept implementation.
  13924. * @tparam Type Type for which to generate an implementation.
  13925. */
  13926. template<typename Type>
  13927. using impl = value_list<&type_id<typename Type::value_type>>;
  13928. };
  13929. /**
  13930. * @brief Defines the poly storage type associate with a given entity type.
  13931. * @tparam Entity A valid entity type (see entt_traits for more details).
  13932. */
  13933. template<typename Entity, typename = void>
  13934. struct poly_storage_traits {
  13935. /*! @brief Poly storage type for the given entity type. */
  13936. using storage_type = poly<Storage<Entity>>;
  13937. };
  13938. }
  13939. #endif
  13940. // #include "runtime_view.hpp"
  13941. #ifndef ENTT_ENTITY_RUNTIME_VIEW_HPP
  13942. #define ENTT_ENTITY_RUNTIME_VIEW_HPP
  13943. #include <iterator>
  13944. #include <vector>
  13945. #include <utility>
  13946. #include <algorithm>
  13947. #include <type_traits>
  13948. // #include "../config/config.h"
  13949. // #include "entity.hpp"
  13950. // #include "sparse_set.hpp"
  13951. // #include "fwd.hpp"
  13952. namespace entt {
  13953. /**
  13954. * @brief Runtime view.
  13955. *
  13956. * Runtime views iterate over those entities that have at least all the given
  13957. * components in their bags. During initialization, a runtime view looks at the
  13958. * number of entities available for each component and picks up a reference to
  13959. * the smallest set of candidate entities in order to get a performance boost
  13960. * when iterate.<br/>
  13961. * Order of elements during iterations are highly dependent on the order of the
  13962. * underlying data structures. See sparse_set and its specializations for more
  13963. * details.
  13964. *
  13965. * @b Important
  13966. *
  13967. * Iterators aren't invalidated if:
  13968. *
  13969. * * New instances of the given components are created and assigned to entities.
  13970. * * The entity currently pointed is modified (as an example, if one of the
  13971. * given components is removed from the entity to which the iterator points).
  13972. * * The entity currently pointed is destroyed.
  13973. *
  13974. * In all the other cases, modifying the pools of the given components in any
  13975. * way invalidates all the iterators and using them results in undefined
  13976. * behavior.
  13977. *
  13978. * @note
  13979. * Views share references to the underlying data structures of the registry that
  13980. * generated them. Therefore any change to the entities and to the components
  13981. * made by means of the registry are immediately reflected by the views, unless
  13982. * a pool was missing when the view was built (in this case, the view won't
  13983. * have a valid reference and won't be updated accordingly).
  13984. *
  13985. * @warning
  13986. * Lifetime of a view must not overcome that of the registry that generated it.
  13987. * In any other case, attempting to use a view results in undefined behavior.
  13988. *
  13989. * @tparam Entity A valid entity type (see entt_traits for more details).
  13990. */
  13991. template<typename Entity>
  13992. class basic_runtime_view final {
  13993. using basic_common_type = basic_sparse_set<Entity>;
  13994. using underlying_iterator = typename basic_common_type::iterator;
  13995. class view_iterator final {
  13996. [[nodiscard]] bool valid() const {
  13997. const auto entt = *it;
  13998. return (!stable_storage || (entt != tombstone))
  13999. && std::all_of(pools->begin()++, pools->end(), [entt](const auto *curr) { return curr->contains(entt); })
  14000. && std::none_of(filter->cbegin(), filter->cend(), [entt](const auto *curr) { return curr && curr->contains(entt); });
  14001. }
  14002. public:
  14003. using difference_type = typename underlying_iterator::difference_type;
  14004. using value_type = typename underlying_iterator::value_type;
  14005. using pointer = typename underlying_iterator::pointer;
  14006. using reference = typename underlying_iterator::reference;
  14007. using iterator_category = std::bidirectional_iterator_tag;
  14008. view_iterator() ENTT_NOEXCEPT = default;
  14009. view_iterator(const std::vector<const basic_common_type *> &cpools, const std::vector<const basic_common_type *> &ignore, underlying_iterator curr) ENTT_NOEXCEPT
  14010. : pools{&cpools},
  14011. filter{&ignore},
  14012. it{curr},
  14013. stable_storage{std::any_of(pools->cbegin(), pools->cend(), [](const basic_common_type *cpool) { return (cpool->policy() == deletion_policy::in_place); })}
  14014. {
  14015. if(it != (*pools)[0]->end() && !valid()) {
  14016. ++(*this);
  14017. }
  14018. }
  14019. view_iterator & operator++() {
  14020. while(++it != (*pools)[0]->end() && !valid());
  14021. return *this;
  14022. }
  14023. view_iterator operator++(int) {
  14024. view_iterator orig = *this;
  14025. return ++(*this), orig;
  14026. }
  14027. view_iterator & operator--() ENTT_NOEXCEPT {
  14028. while(--it != (*pools)[0]->begin() && !valid());
  14029. return *this;
  14030. }
  14031. view_iterator operator--(int) ENTT_NOEXCEPT {
  14032. view_iterator orig = *this;
  14033. return operator--(), orig;
  14034. }
  14035. [[nodiscard]] bool operator==(const view_iterator &other) const ENTT_NOEXCEPT {
  14036. return other.it == it;
  14037. }
  14038. [[nodiscard]] bool operator!=(const view_iterator &other) const ENTT_NOEXCEPT {
  14039. return !(*this == other);
  14040. }
  14041. [[nodiscard]] pointer operator->() const {
  14042. return it.operator->();
  14043. }
  14044. [[nodiscard]] reference operator*() const {
  14045. return *operator->();
  14046. }
  14047. private:
  14048. const std::vector<const basic_common_type *> *pools;
  14049. const std::vector<const basic_common_type *> *filter;
  14050. underlying_iterator it;
  14051. bool stable_storage;
  14052. };
  14053. [[nodiscard]] bool valid() const {
  14054. return !pools.empty() && pools.front();
  14055. }
  14056. public:
  14057. /*! @brief Underlying entity identifier. */
  14058. using entity_type = Entity;
  14059. /*! @brief Unsigned integer type. */
  14060. using size_type = std::size_t;
  14061. /*! @brief Bidirectional iterator type. */
  14062. using iterator = view_iterator;
  14063. /*! @brief Default constructor to use to create empty, invalid views. */
  14064. basic_runtime_view() ENTT_NOEXCEPT
  14065. : pools{},
  14066. filter{}
  14067. {}
  14068. /**
  14069. * @brief Constructs a runtime view from a set of storage classes.
  14070. * @param cpools The storage for the types to iterate.
  14071. * @param epools The storage for the types used to filter the view.
  14072. */
  14073. basic_runtime_view(std::vector<const basic_common_type *> cpools, std::vector<const basic_common_type *> epools) ENTT_NOEXCEPT
  14074. : pools{std::move(cpools)},
  14075. filter{std::move(epools)}
  14076. {
  14077. // brings the best candidate (if any) on front of the vector
  14078. std::rotate(pools.begin(), std::min_element(pools.begin(), pools.end(), [](const auto *lhs, const auto *rhs) {
  14079. return (!lhs && rhs) || (lhs && rhs && lhs->size() < rhs->size());
  14080. }), pools.end());
  14081. }
  14082. /**
  14083. * @brief Estimates the number of entities iterated by the view.
  14084. * @return Estimated number of entities iterated by the view.
  14085. */
  14086. [[nodiscard]] size_type size_hint() const {
  14087. return valid() ? pools.front()->size() : size_type{};
  14088. }
  14089. /**
  14090. * @brief Returns an iterator to the first entity that has the given
  14091. * components.
  14092. *
  14093. * The returned iterator points to the first entity that has the given
  14094. * components. If the view is empty, the returned iterator will be equal to
  14095. * `end()`.
  14096. *
  14097. * @return An iterator to the first entity that has the given components.
  14098. */
  14099. [[nodiscard]] iterator begin() const {
  14100. return valid() ? iterator{pools, filter, pools[0]->begin()} : iterator{};
  14101. }
  14102. /**
  14103. * @brief Returns an iterator that is past the last entity that has the
  14104. * given components.
  14105. *
  14106. * The returned iterator points to the entity following the last entity that
  14107. * has the given components. Attempting to dereference the returned iterator
  14108. * results in undefined behavior.
  14109. *
  14110. * @return An iterator to the entity following the last entity that has the
  14111. * given components.
  14112. */
  14113. [[nodiscard]] iterator end() const {
  14114. return valid() ? iterator{pools, filter, pools[0]->end()} : iterator{};
  14115. }
  14116. /**
  14117. * @brief Checks if a view contains an entity.
  14118. * @param entt A valid entity identifier.
  14119. * @return True if the view contains the given entity, false otherwise.
  14120. */
  14121. [[nodiscard]] bool contains(const entity_type entt) const {
  14122. return valid() && std::all_of(pools.cbegin(), pools.cend(), [entt](const auto *curr) { return curr->contains(entt); })
  14123. && std::none_of(filter.cbegin(), filter.cend(), [entt](const auto *curr) { return curr && curr->contains(entt); });
  14124. }
  14125. /**
  14126. * @brief Iterates entities and applies the given function object to them.
  14127. *
  14128. * The function object is invoked for each entity. It is provided only with
  14129. * the entity itself. To get the components, users can use the registry with
  14130. * which the view was built.<br/>
  14131. * The signature of the function should be equivalent to the following:
  14132. *
  14133. * @code{.cpp}
  14134. * void(const entity_type);
  14135. * @endcode
  14136. *
  14137. * @tparam Func Type of the function object to invoke.
  14138. * @param func A valid function object.
  14139. */
  14140. template<typename Func>
  14141. void each(Func func) const {
  14142. for(const auto entity: *this) {
  14143. func(entity);
  14144. }
  14145. }
  14146. private:
  14147. std::vector<const basic_common_type *> pools;
  14148. std::vector<const basic_common_type *> filter;
  14149. };
  14150. }
  14151. #endif
  14152. // #include "sparse_set.hpp"
  14153. // #include "storage.hpp"
  14154. // #include "utility.hpp"
  14155. // #include "view.hpp"
  14156. #ifndef ENTT_ENTITY_VIEW_HPP
  14157. #define ENTT_ENTITY_VIEW_HPP
  14158. #include <iterator>
  14159. #include <array>
  14160. #include <tuple>
  14161. #include <utility>
  14162. #include <algorithm>
  14163. #include <type_traits>
  14164. // #include "../config/config.h"
  14165. // #include "../core/type_traits.hpp"
  14166. // #include "component.hpp"
  14167. // #include "entity.hpp"
  14168. // #include "fwd.hpp"
  14169. // #include "sparse_set.hpp"
  14170. // #include "storage.hpp"
  14171. // #include "utility.hpp"
  14172. namespace entt {
  14173. /**
  14174. * @cond TURN_OFF_DOXYGEN
  14175. * Internal details not to be documented.
  14176. */
  14177. namespace internal {
  14178. template<typename Policy, typename It, std::size_t AllOf, std::size_t NoneOf>
  14179. class view_iterator final {
  14180. using basic_common_type = basic_sparse_set<typename std::iterator_traits<It>::value_type>;
  14181. [[nodiscard]] bool valid() const {
  14182. const auto entt = *it;
  14183. return Policy::accept(entt)
  14184. && std::apply([entt](const auto *... curr) { return (curr->contains(entt) && ...); }, pools)
  14185. && std::apply([entt](const auto *... curr) { return (!curr->contains(entt) && ...); }, filter);
  14186. }
  14187. public:
  14188. using iterator_type = It;
  14189. using difference_type = typename std::iterator_traits<It>::difference_type;
  14190. using value_type = typename std::iterator_traits<It>::value_type;
  14191. using pointer = typename std::iterator_traits<It>::pointer;
  14192. using reference = typename std::iterator_traits<It>::reference;
  14193. using iterator_category = std::bidirectional_iterator_tag;
  14194. view_iterator() ENTT_NOEXCEPT
  14195. : first{},
  14196. last{},
  14197. it{},
  14198. pools{},
  14199. filter{}
  14200. {}
  14201. view_iterator(It from, It to, It curr, std::array<const basic_common_type *, AllOf> all_of, std::array<const basic_common_type *, NoneOf> none_of) ENTT_NOEXCEPT
  14202. : first{from},
  14203. last{to},
  14204. it{curr},
  14205. pools{all_of},
  14206. filter{none_of}
  14207. {
  14208. if(it != last && !valid()) {
  14209. ++(*this);
  14210. }
  14211. }
  14212. view_iterator & operator++() ENTT_NOEXCEPT {
  14213. while(++it != last && !valid());
  14214. return *this;
  14215. }
  14216. view_iterator operator++(int) ENTT_NOEXCEPT {
  14217. view_iterator orig = *this;
  14218. return ++(*this), orig;
  14219. }
  14220. view_iterator & operator--() ENTT_NOEXCEPT {
  14221. while(--it != first && !valid());
  14222. return *this;
  14223. }
  14224. view_iterator operator--(int) ENTT_NOEXCEPT {
  14225. view_iterator orig = *this;
  14226. return operator--(), orig;
  14227. }
  14228. [[nodiscard]] bool operator==(const view_iterator &other) const ENTT_NOEXCEPT {
  14229. return other.it == it;
  14230. }
  14231. [[nodiscard]] bool operator!=(const view_iterator &other) const ENTT_NOEXCEPT {
  14232. return !(*this == other);
  14233. }
  14234. [[nodiscard]] pointer operator->() const {
  14235. return &*it;
  14236. }
  14237. [[nodiscard]] reference operator*() const {
  14238. return *operator->();
  14239. }
  14240. private:
  14241. It first;
  14242. It last;
  14243. It it;
  14244. std::array<const basic_common_type *, AllOf> pools;
  14245. std::array<const basic_common_type *, NoneOf> filter;
  14246. };
  14247. }
  14248. /**
  14249. * Internal details not to be documented.
  14250. * @endcond
  14251. */
  14252. /*! @brief Stable storage policy, aimed at pointer stability. */
  14253. struct stable_storage_policy {
  14254. /**
  14255. * @cond TURN_OFF_DOXYGEN
  14256. * Internal details not to be documented.
  14257. */
  14258. template<typename Entity>
  14259. [[nodiscard]] static constexpr bool accept(const Entity entity) ENTT_NOEXCEPT {
  14260. return entity != tombstone;
  14261. }
  14262. /**
  14263. * Internal details not to be documented.
  14264. * @endcond
  14265. */
  14266. };
  14267. /*! @brief Packed storage policy, aimed at faster linear iteration. */
  14268. struct packed_storage_policy {
  14269. /**
  14270. * @cond TURN_OFF_DOXYGEN
  14271. * Internal details not to be documented.
  14272. */
  14273. template<typename Entity>
  14274. [[nodiscard]] static constexpr bool accept(const Entity) ENTT_NOEXCEPT {
  14275. return true;
  14276. }
  14277. /**
  14278. * Internal details not to be documented.
  14279. * @endcond
  14280. */
  14281. };
  14282. /**
  14283. * @brief View implementation.
  14284. *
  14285. * Primary template isn't defined on purpose. All the specializations give a
  14286. * compile-time error, but for a few reasonable cases.
  14287. */
  14288. template<typename...>
  14289. class basic_view_impl;
  14290. /*! @brief View implementation dispatcher. */
  14291. template<typename...>
  14292. struct basic_view;
  14293. /**
  14294. * @brief Multi component view.
  14295. *
  14296. * Multi component views iterate over those entities that have at least all the
  14297. * given components in their bags. During initialization, a multi component view
  14298. * looks at the number of entities available for each component and uses the
  14299. * smallest set in order to get a performance boost when iterate.
  14300. *
  14301. * @b Important
  14302. *
  14303. * Iterators aren't invalidated if:
  14304. *
  14305. * * New instances of the given components are created and assigned to entities.
  14306. * * The entity currently pointed is modified (as an example, if one of the
  14307. * given components is removed from the entity to which the iterator points).
  14308. * * The entity currently pointed is destroyed.
  14309. *
  14310. * In all other cases, modifying the pools iterated by the view in any way
  14311. * invalidates all the iterators and using them results in undefined behavior.
  14312. *
  14313. * @note
  14314. * Views share references to the underlying data structures of the registry that
  14315. * generated them. Therefore any change to the entities and to the components
  14316. * made by means of the registry are immediately reflected by views.
  14317. *
  14318. * @warning
  14319. * Lifetime of a view must not overcome that of the registry that generated it.
  14320. * In any other case, attempting to use a view results in undefined behavior.
  14321. *
  14322. * @tparam Policy Common (stricter) storage policy.
  14323. * @tparam Entity A valid entity type (see entt_traits for more details).
  14324. * @tparam Exclude Types of components used to filter the view.
  14325. * @tparam Component Types of components iterated by the view.
  14326. */
  14327. template<typename Policy, typename Entity, typename... Exclude, typename... Component>
  14328. class basic_view_impl<Policy, Entity, exclude_t<Exclude...>, Component...> {
  14329. using basic_common_type = basic_sparse_set<Entity>;
  14330. template<typename Comp>
  14331. using storage_type = constness_as_t<typename storage_traits<Entity, std::remove_const_t<Comp>>::storage_type, Comp>;
  14332. class iterable final {
  14333. template<typename It>
  14334. struct iterable_iterator final {
  14335. using difference_type = std::ptrdiff_t;
  14336. using value_type = decltype(std::tuple_cat(std::tuple<Entity>{}, std::declval<basic_view_impl>().get({})));
  14337. using pointer = void;
  14338. using reference = value_type;
  14339. using iterator_category = std::input_iterator_tag;
  14340. iterable_iterator(It from, const basic_view_impl *parent) ENTT_NOEXCEPT
  14341. : it{from},
  14342. view{parent}
  14343. {}
  14344. iterable_iterator & operator++() ENTT_NOEXCEPT {
  14345. return ++it, *this;
  14346. }
  14347. iterable_iterator operator++(int) ENTT_NOEXCEPT {
  14348. iterable_iterator orig = *this;
  14349. return ++(*this), orig;
  14350. }
  14351. [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
  14352. return std::tuple_cat(std::make_tuple(*it), view->get(*it));
  14353. }
  14354. [[nodiscard]] bool operator==(const iterable_iterator &other) const ENTT_NOEXCEPT {
  14355. return other.it == it;
  14356. }
  14357. [[nodiscard]] bool operator!=(const iterable_iterator &other) const ENTT_NOEXCEPT {
  14358. return !(*this == other);
  14359. }
  14360. private:
  14361. It it;
  14362. const basic_view_impl *view;
  14363. };
  14364. public:
  14365. using iterator = iterable_iterator<internal::view_iterator<Policy, typename basic_common_type::iterator, sizeof...(Component) - 1u, sizeof...(Exclude)>>;
  14366. using reverse_iterator = iterable_iterator<internal::view_iterator<Policy, typename basic_common_type::reverse_iterator, sizeof...(Component) - 1u, sizeof...(Exclude)>>;
  14367. iterable(const basic_view_impl &parent)
  14368. : view{parent}
  14369. {}
  14370. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  14371. return { view.begin(), &view };
  14372. }
  14373. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  14374. return { view.end(), &view };
  14375. }
  14376. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  14377. return { view.rbegin(), &view };
  14378. }
  14379. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  14380. return { view.rend(), &view };
  14381. }
  14382. private:
  14383. const basic_view_impl view;
  14384. };
  14385. [[nodiscard]] const auto * candidate() const ENTT_NOEXCEPT {
  14386. return (std::min)({ static_cast<const basic_common_type *>(std::get<storage_type<Component> *>(pools))... }, [](const auto *lhs, const auto *rhs) {
  14387. return lhs->size() < rhs->size();
  14388. });
  14389. }
  14390. [[nodiscard]] auto pools_to_unchecked_array() const ENTT_NOEXCEPT {
  14391. std::size_t pos{};
  14392. std::array<const basic_common_type *, sizeof...(Component) - 1u> other{};
  14393. (static_cast<void>(std::get<storage_type<Component> *>(pools) == view ? void() : void(other[pos++] = std::get<storage_type<Component> *>(pools))), ...);
  14394. return other;
  14395. }
  14396. [[nodiscard]] auto filter_to_array() const ENTT_NOEXCEPT {
  14397. return std::array<const basic_common_type *, sizeof...(Exclude)>{std::get<const storage_type<Exclude> *>(filter)...};
  14398. }
  14399. template<typename Comp, typename It>
  14400. [[nodiscard]] auto dispatch_get([[maybe_unused]] It &it, [[maybe_unused]] const Entity entt) const {
  14401. if constexpr(std::is_same_v<typename std::iterator_traits<It>::value_type, typename storage_type<Comp>::value_type>) {
  14402. return std::forward_as_tuple(*it);
  14403. } else {
  14404. return get_as_tuple(*std::get<storage_type<Comp> *>(pools), entt);
  14405. }
  14406. }
  14407. template<typename Comp, typename Func>
  14408. void traverse(Func func) const {
  14409. if constexpr(std::is_void_v<decltype(std::get<storage_type<Comp> *>(pools)->get({}))>) {
  14410. for(const auto entt: static_cast<const basic_common_type &>(*std::get<storage_type<Comp> *>(pools))) {
  14411. if(Policy::accept(entt) && ((std::is_same_v<Comp, Component> || std::get<storage_type<Component> *>(pools)->contains(entt)) && ...)
  14412. && (!std::get<const storage_type<Exclude> *>(filter)->contains(entt) && ...))
  14413. {
  14414. if constexpr(is_applicable_v<Func, decltype(std::tuple_cat(std::tuple<entity_type>{}, std::declval<basic_view_impl>().get({})))>) {
  14415. std::apply(func, std::tuple_cat(std::make_tuple(entt), get(entt)));
  14416. } else {
  14417. std::apply(func, get(entt));
  14418. }
  14419. }
  14420. }
  14421. } else {
  14422. auto it = std::get<storage_type<Comp> *>(pools)->begin();
  14423. for(const auto entt: static_cast<const basic_common_type &>(*std::get<storage_type<Comp> *>(pools))) {
  14424. if(Policy::accept(entt) && ((std::is_same_v<Comp, Component> || std::get<storage_type<Component> *>(pools)->contains(entt)) && ...)
  14425. && (!std::get<const storage_type<Exclude> *>(filter)->contains(entt) && ...))
  14426. {
  14427. if constexpr(is_applicable_v<Func, decltype(std::tuple_cat(std::tuple<entity_type>{}, std::declval<basic_view_impl>().get({})))>) {
  14428. std::apply(func, std::tuple_cat(std::make_tuple(entt), dispatch_get<Component>(it, entt)...));
  14429. } else {
  14430. std::apply(func, std::tuple_cat(dispatch_get<Component>(it, entt)...));
  14431. }
  14432. }
  14433. ++it;
  14434. }
  14435. }
  14436. }
  14437. public:
  14438. /*! @brief Underlying entity identifier. */
  14439. using entity_type = Entity;
  14440. /*! @brief Unsigned integer type. */
  14441. using size_type = std::size_t;
  14442. /*! @brief Bidirectional iterator type. */
  14443. using iterator = internal::view_iterator<Policy, typename basic_common_type::iterator, sizeof...(Component) - 1u, sizeof...(Exclude)>;
  14444. /*! @brief Reverse iterator type. */
  14445. using reverse_iterator = internal::view_iterator<Policy, typename basic_common_type::reverse_iterator, sizeof...(Component) - 1u, sizeof...(Exclude)>;
  14446. /*! @brief Iterable view type. */
  14447. using iterable_view = iterable;
  14448. /*! @brief Default constructor to use to create empty, invalid views. */
  14449. basic_view_impl() ENTT_NOEXCEPT
  14450. : view{}
  14451. {}
  14452. /**
  14453. * @brief Constructs a multi-type view from a set of storage classes.
  14454. * @param component The storage for the types to iterate.
  14455. * @param epool The storage for the types used to filter the view.
  14456. */
  14457. basic_view_impl(storage_type<Component> &... component, const storage_type<Exclude> &... epool) ENTT_NOEXCEPT
  14458. : pools{&component...},
  14459. filter{&epool...},
  14460. view{candidate()}
  14461. {}
  14462. /**
  14463. * @brief Forces the type to use to drive iterations.
  14464. * @tparam Comp Type of component to use to drive the iteration.
  14465. */
  14466. template<typename Comp>
  14467. void use() const ENTT_NOEXCEPT {
  14468. view = std::get<storage_type<Comp> *>(pools);
  14469. }
  14470. /**
  14471. * @brief Estimates the number of entities iterated by the view.
  14472. * @return Estimated number of entities iterated by the view.
  14473. */
  14474. [[nodiscard]] size_type size_hint() const ENTT_NOEXCEPT {
  14475. return view->size();
  14476. }
  14477. /**
  14478. * @brief Returns an iterator to the first entity of the view.
  14479. *
  14480. * The returned iterator points to the first entity of the view. If the view
  14481. * is empty, the returned iterator will be equal to `end()`.
  14482. *
  14483. * @return An iterator to the first entity of the view.
  14484. */
  14485. [[nodiscard]] iterator begin() const {
  14486. return iterator{view->begin(), view->end(), view->begin(), pools_to_unchecked_array(), filter_to_array()};
  14487. }
  14488. /**
  14489. * @brief Returns an iterator that is past the last entity of the view.
  14490. *
  14491. * The returned iterator points to the entity following the last entity of
  14492. * the view. Attempting to dereference the returned iterator results in
  14493. * undefined behavior.
  14494. *
  14495. * @return An iterator to the entity following the last entity of the view.
  14496. */
  14497. [[nodiscard]] iterator end() const {
  14498. return iterator{view->begin(), view->end(), view->end(), pools_to_unchecked_array(), filter_to_array()};
  14499. }
  14500. /**
  14501. * @brief Returns an iterator to the first entity of the reversed view.
  14502. *
  14503. * The returned iterator points to the first entity of the reversed view. If
  14504. * the view is empty, the returned iterator will be equal to `rend()`.
  14505. *
  14506. * @return An iterator to the first entity of the reversed view.
  14507. */
  14508. [[nodiscard]] reverse_iterator rbegin() const {
  14509. return reverse_iterator{view->rbegin(), view->rend(), view->rbegin(), pools_to_unchecked_array(), filter_to_array()};
  14510. }
  14511. /**
  14512. * @brief Returns an iterator that is past the last entity of the reversed
  14513. * view.
  14514. *
  14515. * The returned iterator points to the entity following the last entity of
  14516. * the reversed view. Attempting to dereference the returned iterator
  14517. * results in undefined behavior.
  14518. *
  14519. * @return An iterator to the entity following the last entity of the
  14520. * reversed view.
  14521. */
  14522. [[nodiscard]] reverse_iterator rend() const {
  14523. return reverse_iterator{view->rbegin(), view->rend(), view->rend(), pools_to_unchecked_array(), filter_to_array()};
  14524. }
  14525. /**
  14526. * @brief Returns the first entity of the view, if any.
  14527. * @return The first entity of the view if one exists, the null entity
  14528. * otherwise.
  14529. */
  14530. [[nodiscard]] entity_type front() const {
  14531. const auto it = begin();
  14532. return it != end() ? *it : null;
  14533. }
  14534. /**
  14535. * @brief Returns the last entity of the view, if any.
  14536. * @return The last entity of the view if one exists, the null entity
  14537. * otherwise.
  14538. */
  14539. [[nodiscard]] entity_type back() const {
  14540. const auto it = rbegin();
  14541. return it != rend() ? *it : null;
  14542. }
  14543. /**
  14544. * @brief Finds an entity.
  14545. * @param entt A valid entity identifier.
  14546. * @return An iterator to the given entity if it's found, past the end
  14547. * iterator otherwise.
  14548. */
  14549. [[nodiscard]] iterator find(const entity_type entt) const {
  14550. const auto it = iterator{view->begin(), view->end(), view->find(entt), pools_to_unchecked_array(), filter_to_array()};
  14551. return (it != end() && *it == entt) ? it : end();
  14552. }
  14553. /**
  14554. * @brief Checks if a view is properly initialized.
  14555. * @return True if the view is properly initialized, false otherwise.
  14556. */
  14557. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  14558. return view != nullptr;
  14559. }
  14560. /**
  14561. * @brief Checks if a view contains an entity.
  14562. * @param entt A valid entity identifier.
  14563. * @return True if the view contains the given entity, false otherwise.
  14564. */
  14565. [[nodiscard]] bool contains(const entity_type entt) const {
  14566. return (std::get<storage_type<Component> *>(pools)->contains(entt) && ...) && (!std::get<const storage_type<Exclude> *>(filter)->contains(entt) && ...);
  14567. }
  14568. /**
  14569. * @brief Returns the components assigned to the given entity.
  14570. *
  14571. * Prefer this function instead of `registry::get` during iterations. It has
  14572. * far better performance than its counterpart.
  14573. *
  14574. * @warning
  14575. * Attempting to use an invalid component type results in a compilation
  14576. * error. Attempting to use an entity that doesn't belong to the view
  14577. * results in undefined behavior.
  14578. *
  14579. * @tparam Comp Types of components to get.
  14580. * @param entt A valid entity identifier.
  14581. * @return The components assigned to the entity.
  14582. */
  14583. template<typename... Comp>
  14584. [[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entt) const {
  14585. ENTT_ASSERT(contains(entt), "View does not contain entity");
  14586. if constexpr(sizeof...(Comp) == 0) {
  14587. return std::tuple_cat(get_as_tuple(*std::get<storage_type<Component> *>(pools), entt)...);
  14588. } else if constexpr(sizeof...(Comp) == 1) {
  14589. return (std::get<storage_type<Comp> *>(pools)->get(entt), ...);
  14590. } else {
  14591. return std::tuple_cat(get_as_tuple(*std::get<storage_type<Comp> *>(pools), entt)...);
  14592. }
  14593. }
  14594. /**
  14595. * @brief Iterates entities and components and applies the given function
  14596. * object to them.
  14597. *
  14598. * The function object is invoked for each entity. It is provided with the
  14599. * entity itself and a set of references to non-empty components. The
  14600. * _constness_ of the components is as requested.<br/>
  14601. * The signature of the function must be equivalent to one of the following
  14602. * forms:
  14603. *
  14604. * @code{.cpp}
  14605. * void(const entity_type, Type &...);
  14606. * void(Type &...);
  14607. * @endcode
  14608. *
  14609. * @note
  14610. * Empty types aren't explicitly instantiated and therefore they are never
  14611. * returned during iterations.
  14612. *
  14613. * @tparam Func Type of the function object to invoke.
  14614. * @param func A valid function object.
  14615. */
  14616. template<typename Func>
  14617. void each(Func func) const {
  14618. ((std::get<storage_type<Component> *>(pools) == view ? traverse<Component>(std::move(func)) : void()), ...);
  14619. }
  14620. /**
  14621. * @brief Iterates entities and components and applies the given function
  14622. * object to them.
  14623. *
  14624. * The pool of the suggested component is used to lead the iterations. The
  14625. * returned entities will therefore respect the order of the pool associated
  14626. * with that type.
  14627. *
  14628. * @sa each
  14629. *
  14630. * @tparam Comp Type of component to use to drive the iteration.
  14631. * @tparam Func Type of the function object to invoke.
  14632. * @param func A valid function object.
  14633. */
  14634. template<typename Comp, typename Func>
  14635. void each(Func func) const {
  14636. use<Comp>();
  14637. traverse<Comp>(std::move(func));
  14638. }
  14639. /**
  14640. * @brief Returns an iterable object to use to _visit_ the view.
  14641. *
  14642. * The iterable object returns tuples that contain the current entity and a
  14643. * set of references to its non-empty components. The _constness_ of the
  14644. * components is as requested.
  14645. *
  14646. * @note
  14647. * Empty types aren't explicitly instantiated and therefore they are never
  14648. * returned during iterations.
  14649. *
  14650. * @return An iterable object to use to _visit_ the view.
  14651. */
  14652. [[nodiscard]] iterable_view each() const ENTT_NOEXCEPT {
  14653. return iterable_view{*this};
  14654. }
  14655. /**
  14656. * @brief Returns an iterable object to use to _visit_ the view.
  14657. *
  14658. * The pool of the suggested component is used to lead the iterations. The
  14659. * returned elements will therefore respect the order of the pool associated
  14660. * with that type.
  14661. *
  14662. * @sa each
  14663. *
  14664. * @tparam Comp Type of component to use to drive the iteration.
  14665. * @return An iterable object to use to _visit_ the view.
  14666. */
  14667. template<typename Comp>
  14668. [[nodiscard]] iterable_view each() const ENTT_NOEXCEPT {
  14669. use<Comp>();
  14670. return iterable_view{*this};
  14671. }
  14672. /**
  14673. * @brief Combines two views in a _more specific_ one (friend function).
  14674. * @tparam Id A valid entity type (see entt_traits for more details).
  14675. * @tparam ELhs Filter list of the first view.
  14676. * @tparam CLhs Component list of the first view.
  14677. * @tparam ERhs Filter list of the second view.
  14678. * @tparam CRhs Component list of the second view.
  14679. * @return A more specific view.
  14680. */
  14681. template<typename Id, typename... ELhs, typename... CLhs, typename... ERhs, typename... CRhs>
  14682. friend auto operator|(const basic_view<Id, exclude_t<ELhs...>, CLhs...> &, const basic_view<Id, exclude_t<ERhs...>, CRhs...> &);
  14683. private:
  14684. const std::tuple<storage_type<Component> *...> pools;
  14685. const std::tuple<const storage_type<Exclude> *...> filter;
  14686. mutable const basic_common_type *view;
  14687. };
  14688. /**
  14689. * @brief Single component view specialization.
  14690. *
  14691. * Single component views are specialized in order to get a boost in terms of
  14692. * performance. This kind of views can access the underlying data structure
  14693. * directly and avoid superfluous checks.
  14694. *
  14695. * @b Important
  14696. *
  14697. * Iterators aren't invalidated if:
  14698. *
  14699. * * New instances of the given component are created and assigned to entities.
  14700. * * The entity currently pointed is modified (as an example, the given
  14701. * component is removed from the entity to which the iterator points).
  14702. * * The entity currently pointed is destroyed.
  14703. *
  14704. * In all other cases, modifying the pool iterated by the view in any way
  14705. * invalidates all the iterators and using them results in undefined behavior.
  14706. *
  14707. * @note
  14708. * Views share a reference to the underlying data structure of the registry that
  14709. * generated them. Therefore any change to the entities and to the components
  14710. * made by means of the registry are immediately reflected by views.
  14711. *
  14712. * @warning
  14713. * Lifetime of a view must not overcome that of the registry that generated it.
  14714. * In any other case, attempting to use a view results in undefined behavior.
  14715. *
  14716. * @tparam Entity A valid entity type (see entt_traits for more details).
  14717. * @tparam Component Type of component iterated by the view.
  14718. */
  14719. template<typename Entity, typename Component>
  14720. class basic_view_impl<packed_storage_policy, Entity, exclude_t<>, Component> {
  14721. using basic_common_type = basic_sparse_set<Entity>;
  14722. using storage_type = constness_as_t<typename storage_traits<Entity, std::remove_const_t<Component>>::storage_type, Component>;
  14723. class iterable final {
  14724. template<typename... It>
  14725. struct iterable_iterator final {
  14726. using difference_type = std::ptrdiff_t;
  14727. using value_type = decltype(std::tuple_cat(std::tuple<Entity>{}, std::declval<basic_view_impl>().get({})));
  14728. using pointer = void;
  14729. using reference = value_type;
  14730. using iterator_category = std::input_iterator_tag;
  14731. template<typename... Discard>
  14732. iterable_iterator(It... from, Discard...) ENTT_NOEXCEPT
  14733. : it{from...}
  14734. {}
  14735. iterable_iterator & operator++() ENTT_NOEXCEPT {
  14736. return (++std::get<It>(it), ...), *this;
  14737. }
  14738. iterable_iterator operator++(int) ENTT_NOEXCEPT {
  14739. iterable_iterator orig = *this;
  14740. return ++(*this), orig;
  14741. }
  14742. [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
  14743. return { *std::get<It>(it)... };
  14744. }
  14745. [[nodiscard]] bool operator==(const iterable_iterator &other) const ENTT_NOEXCEPT {
  14746. return std::get<0>(other.it) == std::get<0>(it);
  14747. }
  14748. [[nodiscard]] bool operator!=(const iterable_iterator &other) const ENTT_NOEXCEPT {
  14749. return !(*this == other);
  14750. }
  14751. private:
  14752. std::tuple<It...> it;
  14753. };
  14754. public:
  14755. using iterator = std::conditional_t<
  14756. std::is_void_v<decltype(std::declval<storage_type>().get({}))>,
  14757. iterable_iterator<typename basic_common_type::iterator>,
  14758. iterable_iterator<typename basic_common_type::iterator, decltype(std::declval<storage_type>().begin())>
  14759. >;
  14760. using reverse_iterator = std::conditional_t<
  14761. std::is_void_v<decltype(std::declval<storage_type>().get({}))>,
  14762. iterable_iterator<typename basic_common_type::reverse_iterator>,
  14763. iterable_iterator<typename basic_common_type::reverse_iterator, decltype(std::declval<storage_type>().rbegin())>
  14764. >;
  14765. iterable(storage_type &ref)
  14766. : pool{&ref}
  14767. {}
  14768. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  14769. return iterator{pool->basic_common_type::begin(), pool->begin()};
  14770. }
  14771. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  14772. return iterator{pool->basic_common_type::end(), pool->end()};
  14773. }
  14774. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  14775. return reverse_iterator{pool->basic_common_type::rbegin(), pool->rbegin()};
  14776. }
  14777. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  14778. return reverse_iterator{pool->basic_common_type::rend(), pool->rend()};
  14779. }
  14780. private:
  14781. storage_type * const pool;
  14782. };
  14783. public:
  14784. /*! @brief Underlying entity identifier. */
  14785. using entity_type = Entity;
  14786. /*! @brief Unsigned integer type. */
  14787. using size_type = std::size_t;
  14788. /*! @brief Random access iterator type. */
  14789. using iterator = typename basic_common_type::iterator;
  14790. /*! @brief Reversed iterator type. */
  14791. using reverse_iterator = typename basic_common_type::reverse_iterator;
  14792. /*! @brief Iterable view type. */
  14793. using iterable_view = iterable;
  14794. /*! @brief Default constructor to use to create empty, invalid views. */
  14795. basic_view_impl() ENTT_NOEXCEPT
  14796. : pools{},
  14797. filter{}
  14798. {}
  14799. /**
  14800. * @brief Constructs a single-type view from a storage class.
  14801. * @param ref The storage for the type to iterate.
  14802. */
  14803. basic_view_impl(storage_type &ref) ENTT_NOEXCEPT
  14804. : pools{&ref},
  14805. filter{}
  14806. {}
  14807. /**
  14808. * @brief Returns the number of entities that have the given component.
  14809. * @return Number of entities that have the given component.
  14810. */
  14811. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  14812. return std::get<0>(pools)->size();
  14813. }
  14814. /**
  14815. * @brief Checks whether a view is empty.
  14816. * @return True if the view is empty, false otherwise.
  14817. */
  14818. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  14819. return std::get<0>(pools)->empty();
  14820. }
  14821. /**
  14822. * @brief Direct access to the raw representation offered by the storage.
  14823. * @return A pointer to the array of components.
  14824. */
  14825. [[nodiscard]] auto raw() const ENTT_NOEXCEPT {
  14826. return std::get<0>(pools)->raw();
  14827. }
  14828. /**
  14829. * @brief Direct access to the list of entities.
  14830. *
  14831. * The returned pointer is such that range `[data(), data() + size())` is
  14832. * always a valid range, even if the container is empty.
  14833. *
  14834. * @return A pointer to the array of entities.
  14835. */
  14836. [[nodiscard]] auto data() const ENTT_NOEXCEPT {
  14837. return std::get<0>(pools)->data();
  14838. }
  14839. /**
  14840. * @brief Returns an iterator to the first entity of the view.
  14841. *
  14842. * The returned iterator points to the first entity of the view. If the view
  14843. * is empty, the returned iterator will be equal to `end()`.
  14844. *
  14845. * @return An iterator to the first entity of the view.
  14846. */
  14847. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  14848. return std::get<0>(pools)->basic_common_type::begin();
  14849. }
  14850. /**
  14851. * @brief Returns an iterator that is past the last entity of the view.
  14852. *
  14853. * The returned iterator points to the entity following the last entity of
  14854. * the view. Attempting to dereference the returned iterator results in
  14855. * undefined behavior.
  14856. *
  14857. * @return An iterator to the entity following the last entity of the view.
  14858. */
  14859. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  14860. return std::get<0>(pools)->basic_common_type::end();
  14861. }
  14862. /**
  14863. * @brief Returns an iterator to the first entity of the reversed view.
  14864. *
  14865. * The returned iterator points to the first entity of the reversed view. If
  14866. * the view is empty, the returned iterator will be equal to `rend()`.
  14867. *
  14868. * @return An iterator to the first entity of the reversed view.
  14869. */
  14870. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  14871. return std::get<0>(pools)->basic_common_type::rbegin();
  14872. }
  14873. /**
  14874. * @brief Returns an iterator that is past the last entity of the reversed
  14875. * view.
  14876. *
  14877. * The returned iterator points to the entity following the last entity of
  14878. * the reversed view. Attempting to dereference the returned iterator
  14879. * results in undefined behavior.
  14880. *
  14881. * @return An iterator to the entity following the last entity of the
  14882. * reversed view.
  14883. */
  14884. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  14885. return std::get<0>(pools)->basic_common_type::rend();
  14886. }
  14887. /**
  14888. * @brief Returns the first entity of the view, if any.
  14889. * @return The first entity of the view if one exists, the null entity
  14890. * otherwise.
  14891. */
  14892. [[nodiscard]] entity_type front() const {
  14893. const auto it = begin();
  14894. return it != end() ? *it : null;
  14895. }
  14896. /**
  14897. * @brief Returns the last entity of the view, if any.
  14898. * @return The last entity of the view if one exists, the null entity
  14899. * otherwise.
  14900. */
  14901. [[nodiscard]] entity_type back() const {
  14902. const auto it = rbegin();
  14903. return it != rend() ? *it : null;
  14904. }
  14905. /**
  14906. * @brief Finds an entity.
  14907. * @param entt A valid entity identifier.
  14908. * @return An iterator to the given entity if it's found, past the end
  14909. * iterator otherwise.
  14910. */
  14911. [[nodiscard]] iterator find(const entity_type entt) const {
  14912. const auto it = std::get<0>(pools)->find(entt);
  14913. return it != end() && *it == entt ? it : end();
  14914. }
  14915. /**
  14916. * @brief Returns the identifier that occupies the given position.
  14917. * @param pos Position of the element to return.
  14918. * @return The identifier that occupies the given position.
  14919. */
  14920. [[nodiscard]] entity_type operator[](const size_type pos) const {
  14921. return begin()[pos];
  14922. }
  14923. /**
  14924. * @brief Checks if a view is properly initialized.
  14925. * @return True if the view is properly initialized, false otherwise.
  14926. */
  14927. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  14928. return std::get<0>(pools) != nullptr;
  14929. }
  14930. /**
  14931. * @brief Checks if a view contains an entity.
  14932. * @param entt A valid entity identifier.
  14933. * @return True if the view contains the given entity, false otherwise.
  14934. */
  14935. [[nodiscard]] bool contains(const entity_type entt) const {
  14936. return std::get<0>(pools)->contains(entt);
  14937. }
  14938. /**
  14939. * @brief Returns the component assigned to the given entity.
  14940. *
  14941. * Prefer this function instead of `registry::get` during iterations. It has
  14942. * far better performance than its counterpart.
  14943. *
  14944. * @warning
  14945. * Attempting to use an invalid component type results in a compilation
  14946. * error. Attempting to use an entity that doesn't belong to the view
  14947. * results in undefined behavior.
  14948. *
  14949. * @tparam Comp Types of components to get.
  14950. * @param entt A valid entity identifier.
  14951. * @return The component assigned to the entity.
  14952. */
  14953. template<typename... Comp>
  14954. [[nodiscard]] decltype(auto) get(const entity_type entt) const {
  14955. ENTT_ASSERT(contains(entt), "View does not contain entity");
  14956. if constexpr(sizeof...(Comp) == 0) {
  14957. return get_as_tuple(*std::get<0>(pools), entt);
  14958. } else {
  14959. static_assert(std::is_same_v<Comp..., Component>, "Invalid component type");
  14960. return std::get<0>(pools)->get(entt);
  14961. }
  14962. }
  14963. /**
  14964. * @brief Iterates entities and components and applies the given function
  14965. * object to them.
  14966. *
  14967. * The function object is invoked for each entity. It is provided with the
  14968. * entity itself and a reference to the component if it's a non-empty one.
  14969. * The _constness_ of the component is as requested.<br/>
  14970. * The signature of the function must be equivalent to one of the following
  14971. * forms:
  14972. *
  14973. * @code{.cpp}
  14974. * void(const entity_type, Component &);
  14975. * void(Component &);
  14976. * @endcode
  14977. *
  14978. * @note
  14979. * Empty types aren't explicitly instantiated and therefore they are never
  14980. * returned during iterations.
  14981. *
  14982. * @tparam Func Type of the function object to invoke.
  14983. * @param func A valid function object.
  14984. */
  14985. template<typename Func>
  14986. void each(Func func) const {
  14987. if constexpr(std::is_void_v<decltype(std::get<0>(pools)->get({}))>) {
  14988. if constexpr(std::is_invocable_v<Func>) {
  14989. for(auto pos = size(); pos; --pos) {
  14990. func();
  14991. }
  14992. } else {
  14993. for(auto entity: *this) {
  14994. func(entity);
  14995. }
  14996. }
  14997. } else {
  14998. if constexpr(is_applicable_v<Func, decltype(*each().begin())>) {
  14999. for(const auto pack: each()) {
  15000. std::apply(func, pack);
  15001. }
  15002. } else {
  15003. for(auto &&component: *std::get<0>(pools)) {
  15004. func(component);
  15005. }
  15006. }
  15007. }
  15008. }
  15009. /**
  15010. * @brief Returns an iterable object to use to _visit_ the view.
  15011. *
  15012. * The iterable object returns tuples that contain the current entity and a
  15013. * reference to its component if it's a non-empty one. The _constness_ of
  15014. * the component is as requested.
  15015. *
  15016. * @note
  15017. * Empty types aren't explicitly instantiated and therefore they are never
  15018. * returned during iterations.
  15019. *
  15020. * @return An iterable object to use to _visit_ the view.
  15021. */
  15022. [[nodiscard]] iterable_view each() const ENTT_NOEXCEPT {
  15023. return iterable_view{*std::get<0>(pools)};
  15024. }
  15025. /**
  15026. * @brief Combines two views in a _more specific_ one (friend function).
  15027. * @tparam Id A valid entity type (see entt_traits for more details).
  15028. * @tparam ELhs Filter list of the first view.
  15029. * @tparam CLhs Component list of the first view.
  15030. * @tparam ERhs Filter list of the second view.
  15031. * @tparam CRhs Component list of the second view.
  15032. * @return A more specific view.
  15033. */
  15034. template<typename Id, typename... ELhs, typename... CLhs, typename... ERhs, typename... CRhs>
  15035. friend auto operator|(const basic_view<Id, exclude_t<ELhs...>, CLhs...> &, const basic_view<Id, exclude_t<ERhs...>, CRhs...> &);
  15036. private:
  15037. const std::tuple<storage_type *> pools;
  15038. const std::tuple<> filter;
  15039. };
  15040. /**
  15041. * @brief View implementation dispatcher.
  15042. * @tparam Entity A valid entity type (see entt_traits for more details).
  15043. * @tparam Exclude Types of components used to filter the view.
  15044. * @tparam Component Types of components iterated by the view.
  15045. */
  15046. template<typename Entity, typename... Exclude, typename... Component>
  15047. struct basic_view<Entity, exclude_t<Exclude...>, Component...>
  15048. : basic_view_impl<std::conditional_t<std::disjunction_v<typename component_traits<std::remove_const_t<Component>>::in_place_delete...>, stable_storage_policy, packed_storage_policy>, Entity, exclude_t<Exclude...>, Component...>
  15049. {
  15050. /*! @brief Most restrictive storage policy of all component types. */
  15051. using storage_policy = std::conditional_t<std::disjunction_v<typename component_traits<std::remove_const_t<Component>>::in_place_delete...>, stable_storage_policy, packed_storage_policy>;
  15052. using basic_view_impl<storage_policy, Entity, exclude_t<Exclude...>, Component...>::basic_view_impl;
  15053. };
  15054. /**
  15055. * @brief Deduction guide.
  15056. * @tparam Storage Type of storage classes used to create the view.
  15057. * @param storage The storage for the types to iterate.
  15058. */
  15059. template<typename... Storage>
  15060. basic_view(Storage &... storage)
  15061. -> basic_view<std::common_type_t<typename Storage::entity_type...>, entt::exclude_t<>, constness_as_t<typename Storage::value_type, Storage>...>;
  15062. /**
  15063. * @brief Combines two views in a _more specific_ one.
  15064. * @tparam Entity A valid entity type (see entt_traits for more details).
  15065. * @tparam ELhs Filter list of the first view.
  15066. * @tparam CLhs Component list of the first view.
  15067. * @tparam ERhs Filter list of the second view.
  15068. * @tparam CRhs Component list of the second view.
  15069. * @param lhs A valid reference to the first view.
  15070. * @param rhs A valid reference to the second view.
  15071. * @return A more specific view.
  15072. */
  15073. template<typename Entity, typename... ELhs, typename... CLhs, typename... ERhs, typename... CRhs>
  15074. [[nodiscard]] auto operator|(const basic_view<Entity, exclude_t<ELhs...>, CLhs...> &lhs, const basic_view<Entity, exclude_t<ERhs...>, CRhs...> &rhs) {
  15075. using view_type = basic_view<Entity, exclude_t<ELhs..., ERhs...>, CLhs..., CRhs...>;
  15076. return std::apply([](auto *... storage) { return view_type{*storage...}; }, std::tuple_cat(lhs.pools, rhs.pools, lhs.filter, rhs.filter));
  15077. }
  15078. }
  15079. #endif
  15080. namespace entt {
  15081. /**
  15082. * @brief Fast and reliable entity-component system.
  15083. *
  15084. * The registry is the core class of the entity-component framework.<br/>
  15085. * It stores entities and arranges pools of components on a per request basis.
  15086. * By means of a registry, users can manage entities and components, then create
  15087. * views or groups to iterate them.
  15088. *
  15089. * @tparam Entity A valid entity type (see entt_traits for more details).
  15090. */
  15091. template<typename Entity>
  15092. class basic_registry {
  15093. using traits_type = entt_traits<Entity>;
  15094. using poly_storage_type = typename poly_storage_traits<Entity>::storage_type;
  15095. using basic_common_type = basic_sparse_set<Entity>;
  15096. template<typename Component>
  15097. using storage_type = constness_as_t<typename storage_traits<Entity, std::remove_const_t<Component>>::storage_type, Component>;
  15098. struct pool_data {
  15099. poly_storage_type poly;
  15100. std::unique_ptr<basic_common_type> pool{};
  15101. };
  15102. template<typename...>
  15103. struct group_handler;
  15104. template<typename... Exclude, typename... Get, typename... Owned>
  15105. struct group_handler<exclude_t<Exclude...>, get_t<Get...>, Owned...> {
  15106. static_assert(!std::disjunction_v<typename component_traits<Owned>::in_place_delete...>, "Groups do not support in-place delete");
  15107. static_assert(std::conjunction_v<std::is_same<Owned, std::remove_const_t<Owned>>..., std::is_same<Get, std::remove_const_t<Get>>..., std::is_same<Exclude, std::remove_const_t<Exclude>>...>, "One or more component types are invalid");
  15108. std::conditional_t<sizeof...(Owned) == 0, basic_common_type, std::size_t> current{};
  15109. template<typename Component>
  15110. void maybe_valid_if(basic_registry &owner, const Entity entt) {
  15111. [[maybe_unused]] const auto cpools = std::make_tuple(owner.assure<Owned>()...);
  15112. const auto is_valid = ((std::is_same_v<Component, Owned> || std::get<storage_type<Owned> *>(cpools)->contains(entt)) && ...)
  15113. && ((std::is_same_v<Component, Get> || owner.assure<Get>()->contains(entt)) && ...)
  15114. && ((std::is_same_v<Component, Exclude> || !owner.assure<Exclude>()->contains(entt)) && ...);
  15115. if constexpr(sizeof...(Owned) == 0) {
  15116. if(is_valid && !current.contains(entt)) {
  15117. current.emplace(entt);
  15118. }
  15119. } else {
  15120. if(is_valid && !(std::get<0>(cpools)->index(entt) < current)) {
  15121. const auto pos = current++;
  15122. (std::get<storage_type<Owned> *>(cpools)->swap(std::get<storage_type<Owned> *>(cpools)->data()[pos], entt), ...);
  15123. }
  15124. }
  15125. }
  15126. void discard_if([[maybe_unused]] basic_registry &owner, const Entity entt) {
  15127. if constexpr(sizeof...(Owned) == 0) {
  15128. current.remove(entt);
  15129. } else {
  15130. if(const auto cpools = std::make_tuple(owner.assure<Owned>()...); std::get<0>(cpools)->contains(entt) && (std::get<0>(cpools)->index(entt) < current)) {
  15131. const auto pos = --current;
  15132. (std::get<storage_type<Owned> *>(cpools)->swap(std::get<storage_type<Owned> *>(cpools)->data()[pos], entt), ...);
  15133. }
  15134. }
  15135. }
  15136. };
  15137. struct group_data {
  15138. std::size_t size;
  15139. std::unique_ptr<void, void(*)(void *)> group;
  15140. bool (* owned)(const id_type) ENTT_NOEXCEPT;
  15141. bool (* get)(const id_type) ENTT_NOEXCEPT;
  15142. bool (* exclude)(const id_type) ENTT_NOEXCEPT;
  15143. };
  15144. template<typename Component>
  15145. [[nodiscard]] storage_type<Component> * assure() const {
  15146. static_assert(std::is_same_v<Component, std::decay_t<Component>>, "Non-decayed types not allowed");
  15147. const auto index = type_seq<Component>::value();
  15148. if(!(index < pools.size())) {
  15149. pools.resize(size_type(index)+1u);
  15150. }
  15151. if(auto &&pdata = pools[index]; !pdata.pool) {
  15152. pdata.pool.reset(new storage_type<Component>());
  15153. pdata.poly.template emplace<storage_type<Component> &>(*static_cast<storage_type<Component> *>(pdata.pool.get()));
  15154. }
  15155. return static_cast<storage_type<Component> *>(pools[index].pool.get());
  15156. }
  15157. template<typename Component>
  15158. [[nodiscard]] const storage_type<Component> * pool_if_exists() const ENTT_NOEXCEPT {
  15159. static_assert(std::is_same_v<Component, std::decay_t<Component>>, "Non-decayed types not allowed");
  15160. const auto index = type_seq<Component>::value();
  15161. return (!(index < pools.size()) || !pools[index].pool) ? nullptr : static_cast<const storage_type<Component> *>(pools[index].pool.get());
  15162. }
  15163. auto generate_identifier(const std::size_t pos) ENTT_NOEXCEPT {
  15164. ENTT_ASSERT(pos < traits_type::to_integral(null), "No entities available");
  15165. return traits_type::construct(static_cast<typename traits_type::entity_type>(pos), {});
  15166. }
  15167. auto recycle_identifier() ENTT_NOEXCEPT {
  15168. ENTT_ASSERT(free_list != null, "No entities available");
  15169. const auto curr = traits_type::to_entity(free_list);
  15170. free_list = (tombstone | entities[curr]);
  15171. return (entities[curr] = traits_type::construct(curr, traits_type::to_version(entities[curr])));
  15172. }
  15173. auto release_entity(const Entity entity, const typename traits_type::version_type version) {
  15174. const typename traits_type::version_type vers = version + (version == traits_type::to_version(tombstone));
  15175. entities[traits_type::to_entity(entity)] = traits_type::construct(traits_type::to_entity(free_list), vers);
  15176. free_list = (tombstone | entity);
  15177. return vers;
  15178. }
  15179. public:
  15180. /*! @brief Underlying entity identifier. */
  15181. using entity_type = Entity;
  15182. /*! @brief Underlying version type. */
  15183. using version_type = typename traits_type::version_type;
  15184. /*! @brief Unsigned integer type. */
  15185. using size_type = std::size_t;
  15186. /*! @brief Poly storage type. */
  15187. using poly_storage = typename poly_storage_traits<Entity>::storage_type;
  15188. /**
  15189. * @brief Returns the entity identifier without the version.
  15190. * @param entity An entity identifier, either valid or not.
  15191. * @return The entity identifier without the version.
  15192. */
  15193. [[nodiscard]] static entity_type entity(const entity_type entity) ENTT_NOEXCEPT {
  15194. return traits_type::construct(traits_type::to_entity(entity), {});
  15195. }
  15196. /**
  15197. * @brief Returns the version stored along with an entity identifier.
  15198. * @param entity An entity identifier, either valid or not.
  15199. * @return The version stored along with the given entity identifier.
  15200. */
  15201. [[nodiscard]] static version_type version(const entity_type entity) ENTT_NOEXCEPT {
  15202. return traits_type::to_version(entity);
  15203. }
  15204. /*! @brief Default constructor. */
  15205. basic_registry() = default;
  15206. /*! @brief Default move constructor. */
  15207. basic_registry(basic_registry &&) = default;
  15208. /*! @brief Default move assignment operator. @return This registry. */
  15209. basic_registry & operator=(basic_registry &&) = default;
  15210. /**
  15211. * @brief Prepares a pool for the given type if required.
  15212. * @tparam Component Type of component for which to prepare a pool.
  15213. */
  15214. template<typename Component>
  15215. void prepare() {
  15216. // suppress the warning due to the [[nodiscard]] attribute
  15217. static_cast<void>(assure<Component>());
  15218. }
  15219. /**
  15220. * @brief Returns a poly storage for a given type.
  15221. * @param info The type for which to return a poly storage.
  15222. * @return A valid poly storage if a pool for the given type exists, an
  15223. * empty and thus invalid element otherwise.
  15224. */
  15225. poly_storage & storage(const type_info info) {
  15226. ENTT_ASSERT(info.seq() < pools.size() && pools[info.seq()].poly, "Storage not available");
  15227. return pools[info.seq()].poly;
  15228. }
  15229. /*! @copydoc storage */
  15230. const poly_storage & storage(const type_info info) const {
  15231. ENTT_ASSERT(info.seq() < pools.size() && pools[info.seq()].poly, "Storage not available");
  15232. return pools[info.seq()].poly;
  15233. }
  15234. /**
  15235. * @brief Returns the number of existing components of the given type.
  15236. * @tparam Component Type of component of which to return the size.
  15237. * @return Number of existing components of the given type.
  15238. */
  15239. template<typename Component>
  15240. [[nodiscard]] size_type size() const {
  15241. const auto *cpool = pool_if_exists<Component>();
  15242. return cpool ? cpool->size() : size_type{};
  15243. }
  15244. /**
  15245. * @brief Returns the number of entities created so far.
  15246. * @return Number of entities created so far.
  15247. */
  15248. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  15249. return entities.size();
  15250. }
  15251. /**
  15252. * @brief Returns the number of entities still in use.
  15253. * @return Number of entities still in use.
  15254. */
  15255. [[nodiscard]] size_type alive() const {
  15256. auto sz = entities.size();
  15257. for(auto curr = free_list; curr != null; --sz) {
  15258. curr = entities[traits_type::to_entity(curr)];
  15259. }
  15260. return sz;
  15261. }
  15262. /**
  15263. * @brief Increases the capacity of the registry or of the pools for the
  15264. * given components.
  15265. *
  15266. * If no components are specified, the capacity of the registry is
  15267. * increased, that is the number of entities it contains. Otherwise the
  15268. * capacity of the pools for the given components is increased.<br/>
  15269. * In both cases, if the new capacity is greater than the current capacity,
  15270. * new storage is allocated, otherwise the method does nothing.
  15271. *
  15272. * @tparam Component Types of components for which to reserve storage.
  15273. * @param cap Desired capacity.
  15274. */
  15275. template<typename... Component>
  15276. void reserve(const size_type cap) {
  15277. if constexpr(sizeof...(Component) == 0) {
  15278. entities.reserve(cap);
  15279. } else {
  15280. (assure<Component>()->reserve(cap), ...);
  15281. }
  15282. }
  15283. /**
  15284. * @brief Reserves enough space to store `count` pools.
  15285. * @param count Number of pools to reserve space for.
  15286. */
  15287. [[deprecated("No longer supported")]]
  15288. void reserve_pools(const size_t count) {
  15289. pools.reserve(count);
  15290. }
  15291. /**
  15292. * @brief Returns the capacity of the pool for the given component.
  15293. * @tparam Component Type of component in which one is interested.
  15294. * @return Capacity of the pool of the given component.
  15295. */
  15296. template<typename Component>
  15297. [[nodiscard]] size_type capacity() const {
  15298. const auto *cpool = pool_if_exists<Component>();
  15299. return cpool ? cpool->capacity() : size_type{};
  15300. }
  15301. /**
  15302. * @brief Returns the number of entities that a registry has currently
  15303. * allocated space for.
  15304. * @return Capacity of the registry.
  15305. */
  15306. [[nodiscard]] size_type capacity() const ENTT_NOEXCEPT {
  15307. return entities.capacity();
  15308. }
  15309. /**
  15310. * @brief Requests the removal of unused capacity for the given components.
  15311. * @tparam Component Types of components for which to reclaim unused
  15312. * capacity.
  15313. */
  15314. template<typename... Component>
  15315. void shrink_to_fit() {
  15316. (assure<Component>()->shrink_to_fit(), ...);
  15317. }
  15318. /**
  15319. * @brief Checks whether the registry or the pools of the given components
  15320. * are empty.
  15321. *
  15322. * A registry is considered empty when it doesn't contain entities that are
  15323. * still in use.
  15324. *
  15325. * @tparam Component Types of components in which one is interested.
  15326. * @return True if the registry or the pools of the given components are
  15327. * empty, false otherwise.
  15328. */
  15329. template<typename... Component>
  15330. [[nodiscard]] bool empty() const {
  15331. if constexpr(sizeof...(Component) == 0) {
  15332. return !alive();
  15333. } else {
  15334. return [](const auto *... cpool) { return ((!cpool || cpool->empty()) && ...); }(pool_if_exists<Component>()...);
  15335. }
  15336. }
  15337. /**
  15338. * @brief Direct access to the list of entities of a registry.
  15339. *
  15340. * The returned pointer is such that range `[data(), data() + size())` is
  15341. * always a valid range, even if the container is empty.
  15342. *
  15343. * @warning
  15344. * This list contains both valid and destroyed entities and isn't suitable
  15345. * for direct use.
  15346. *
  15347. * @return A pointer to the array of entities.
  15348. */
  15349. [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT {
  15350. return entities.data();
  15351. }
  15352. /**
  15353. * @brief Returns the head of the list of released entities.
  15354. *
  15355. * This function is intended for use in conjunction with `assign`.<br/>
  15356. * The returned entity has an invalid identifier in all cases.
  15357. *
  15358. * @return The head of the list of released entities.
  15359. */
  15360. [[nodiscard]] entity_type released() const ENTT_NOEXCEPT {
  15361. return free_list;
  15362. }
  15363. /*! @copydoc released */
  15364. [[deprecated("Use ::released instead")]]
  15365. [[nodiscard]] entity_type destroyed() const ENTT_NOEXCEPT {
  15366. return released();
  15367. }
  15368. /**
  15369. * @brief Checks if an entity identifier refers to a valid entity.
  15370. * @param entity An entity identifier, either valid or not.
  15371. * @return True if the identifier is valid, false otherwise.
  15372. */
  15373. [[nodiscard]] bool valid(const entity_type entity) const {
  15374. const auto pos = size_type(traits_type::to_entity(entity));
  15375. return (pos < entities.size() && entities[pos] == entity);
  15376. }
  15377. /**
  15378. * @brief Returns the actual version for an entity identifier.
  15379. *
  15380. * @warning
  15381. * Attempting to use an entity that doesn't belong to the registry results
  15382. * in undefined behavior. An entity belongs to the registry even if it has
  15383. * been previously destroyed and/or recycled.
  15384. *
  15385. * @param entity A valid entity identifier.
  15386. * @return Actual version for the given entity identifier.
  15387. */
  15388. [[nodiscard]] version_type current(const entity_type entity) const {
  15389. const auto pos = size_type(traits_type::to_entity(entity));
  15390. ENTT_ASSERT(pos < entities.size(), "Entity does not exist");
  15391. return version(entities[pos]);
  15392. }
  15393. /**
  15394. * @brief Creates a new entity and returns it.
  15395. *
  15396. * There are two kinds of possible entity identifiers:
  15397. *
  15398. * * Newly created ones in case no entities have been previously destroyed.
  15399. * * Recycled ones with updated versions.
  15400. *
  15401. * @return A valid entity identifier.
  15402. */
  15403. [[nodiscard]] entity_type create() {
  15404. return (free_list == null) ? entities.emplace_back(generate_identifier(entities.size())) : recycle_identifier();
  15405. }
  15406. /**
  15407. * @brief Creates a new entity and returns it.
  15408. *
  15409. * @sa create
  15410. *
  15411. * If the requested entity isn't in use, the suggested identifier is created
  15412. * and returned. Otherwise, a new identifier is generated.
  15413. *
  15414. * @param hint Required entity identifier.
  15415. * @return A valid entity identifier.
  15416. */
  15417. [[nodiscard]] entity_type create(const entity_type hint) {
  15418. const auto length = entities.size();
  15419. if(hint == null || hint == tombstone) {
  15420. return create();
  15421. } else if(const auto req = traits_type::to_entity(hint); !(req < length)) {
  15422. entities.resize(size_type(req) + 1u, null);
  15423. for(auto pos = length; pos < req; ++pos) {
  15424. release_entity(generate_identifier(pos), {});
  15425. }
  15426. return (entities[req] = hint);
  15427. } else if(const auto curr = traits_type::to_entity(entities[req]); req == curr) {
  15428. return create();
  15429. } else {
  15430. auto *it = &free_list;
  15431. for(; traits_type::to_entity(*it) != req; it = &entities[traits_type::to_entity(*it)]);
  15432. *it = traits_type::construct(curr, traits_type::to_version(*it));
  15433. return (entities[req] = hint);
  15434. }
  15435. }
  15436. /**
  15437. * @brief Assigns each element in a range an entity.
  15438. *
  15439. * @sa create
  15440. *
  15441. * @tparam It Type of forward iterator.
  15442. * @param first An iterator to the first element of the range to generate.
  15443. * @param last An iterator past the last element of the range to generate.
  15444. */
  15445. template<typename It>
  15446. void create(It first, It last) {
  15447. for(; free_list != null && first != last; ++first) {
  15448. *first = recycle_identifier();
  15449. }
  15450. const auto length = entities.size();
  15451. entities.resize(length + std::distance(first, last), null);
  15452. for(auto pos = length; first != last; ++first, ++pos) {
  15453. *first = entities[pos] = generate_identifier(pos);
  15454. }
  15455. }
  15456. /**
  15457. * @brief Assigns entities to an empty registry.
  15458. *
  15459. * This function is intended for use in conjunction with `data`, `size` and
  15460. * `destroyed`.<br/>
  15461. * Don't try to inject ranges of randomly generated entities nor the _wrong_
  15462. * head for the list of destroyed entities. There is no guarantee that a
  15463. * registry will continue to work properly in this case.
  15464. *
  15465. * @warning
  15466. * There must be no entities still alive for this to work properly.
  15467. *
  15468. * @tparam It Type of input iterator.
  15469. * @param first An iterator to the first element of the range of entities.
  15470. * @param last An iterator past the last element of the range of entities.
  15471. * @param destroyed The head of the list of destroyed entities.
  15472. */
  15473. template<typename It>
  15474. void assign(It first, It last, const entity_type destroyed) {
  15475. ENTT_ASSERT(!alive(), "Entities still alive");
  15476. entities.assign(first, last);
  15477. free_list = destroyed;
  15478. }
  15479. /**
  15480. * @brief Releases an entity identifier.
  15481. *
  15482. * The version is updated and the identifier can be recycled at any time.
  15483. *
  15484. * @warning
  15485. * Attempting to use an invalid entity results in undefined behavior.
  15486. *
  15487. * @param entity A valid entity identifier.
  15488. * @return The version of the recycled entity.
  15489. */
  15490. version_type release(const entity_type entity) {
  15491. return release(entity, version(entity) + 1u);
  15492. }
  15493. /**
  15494. * @brief Releases an entity identifier.
  15495. *
  15496. * The suggested version or the valid version closest to the suggested one
  15497. * is used instead of the implicitly generated version.
  15498. *
  15499. * @sa release
  15500. *
  15501. * @param entity A valid entity identifier.
  15502. * @param version A desired version upon destruction.
  15503. * @return The version actually assigned to the entity.
  15504. */
  15505. version_type release(const entity_type entity, const version_type version) {
  15506. ENTT_ASSERT(orphan(entity), "Non-orphan entity");
  15507. return release_entity(entity, version);
  15508. }
  15509. /**
  15510. * @brief Releases all entity identifiers in a range.
  15511. *
  15512. * @sa release
  15513. *
  15514. * @tparam It Type of input iterator.
  15515. * @param first An iterator to the first element of the range of entities.
  15516. * @param last An iterator past the last element of the range of entities.
  15517. */
  15518. template<typename It>
  15519. void release(It first, It last) {
  15520. for(; first != last; ++first) {
  15521. release(*first, version(*first) + 1u);
  15522. }
  15523. }
  15524. /**
  15525. * @brief Destroys an entity and releases its identifier.
  15526. *
  15527. * The version is updated and the identifier can be recycled at any time.
  15528. *
  15529. * @warning
  15530. * Adding or removing components to an entity that is being destroyed can
  15531. * result in undefined behavior.
  15532. *
  15533. * @warning
  15534. * Attempting to use an invalid entity results in undefined behavior.
  15535. *
  15536. * @param entity A valid entity identifier.
  15537. * @return The version of the recycled entity.
  15538. */
  15539. version_type destroy(const entity_type entity) {
  15540. return destroy(entity, version(entity) + 1u);
  15541. }
  15542. /**
  15543. * @brief Destroys an entity and releases its identifier.
  15544. *
  15545. * The suggested version or the valid version closest to the suggested one
  15546. * is used instead of the implicitly generated version.
  15547. *
  15548. * @sa destroy
  15549. *
  15550. * @param entity A valid entity identifier.
  15551. * @param version A desired version upon destruction.
  15552. * @return The version actually assigned to the entity.
  15553. */
  15554. version_type destroy(const entity_type entity, const version_type version) {
  15555. ENTT_ASSERT(valid(entity), "Invalid entity");
  15556. for(auto &&pdata: pools) {
  15557. pdata.pool && pdata.pool->remove(entity, this);
  15558. }
  15559. return release_entity(entity, version);
  15560. }
  15561. /**
  15562. * @brief Destroys all entities in a range and releases their identifiers.
  15563. *
  15564. * @sa destroy
  15565. *
  15566. * @tparam It Type of input iterator.
  15567. * @param first An iterator to the first element of the range of entities.
  15568. * @param last An iterator past the last element of the range of entities.
  15569. */
  15570. template<typename It>
  15571. void destroy(It first, It last) {
  15572. if constexpr(is_iterator_type_v<typename basic_common_type::iterator, It>) {
  15573. for(; first != last; ++first) {
  15574. destroy(*first, version(*first) + 1u);
  15575. }
  15576. } else {
  15577. for(auto &&pdata: pools) {
  15578. pdata.pool && pdata.pool->remove(first, last, this);
  15579. }
  15580. release(first, last);
  15581. }
  15582. }
  15583. /**
  15584. * @brief Assigns the given component to an entity.
  15585. *
  15586. * A new instance of the given component is created and initialized with the
  15587. * arguments provided (the component must have a proper constructor or be of
  15588. * aggregate type). Then the component is assigned to the given entity.
  15589. *
  15590. * @warning
  15591. * Attempting to use an invalid entity or to assign a component to an entity
  15592. * that already owns it results in undefined behavior.
  15593. *
  15594. * @tparam Component Type of component to create.
  15595. * @tparam Args Types of arguments to use to construct the component.
  15596. * @param entity A valid entity identifier.
  15597. * @param args Parameters to use to initialize the component.
  15598. * @return A reference to the newly created component.
  15599. */
  15600. template<typename Component, typename... Args>
  15601. decltype(auto) emplace(const entity_type entity, Args &&... args) {
  15602. ENTT_ASSERT(valid(entity), "Invalid entity");
  15603. return assure<Component>()->emplace(*this, entity, std::forward<Args>(args)...);
  15604. }
  15605. /**
  15606. * @brief Assigns each entity in a range the given component.
  15607. *
  15608. * @sa emplace
  15609. *
  15610. * @tparam Component Type of component to create.
  15611. * @tparam It Type of input iterator.
  15612. * @param first An iterator to the first element of the range of entities.
  15613. * @param last An iterator past the last element of the range of entities.
  15614. * @param value An instance of the component to assign.
  15615. */
  15616. template<typename Component, typename It>
  15617. void insert(It first, It last, const Component &value = {}) {
  15618. ENTT_ASSERT(std::all_of(first, last, [this](const auto entity) { return valid(entity); }), "Invalid entity");
  15619. assure<Component>()->insert(*this, first, last, value);
  15620. }
  15621. /**
  15622. * @brief Assigns each entity in a range the given components.
  15623. *
  15624. * @sa emplace
  15625. *
  15626. * @tparam Component Type of component to create.
  15627. * @tparam EIt Type of input iterator.
  15628. * @tparam CIt Type of input iterator.
  15629. * @param first An iterator to the first element of the range of entities.
  15630. * @param last An iterator past the last element of the range of entities.
  15631. * @param from An iterator to the first element of the range of components.
  15632. */
  15633. template<typename Component, typename EIt, typename CIt, typename = std::enable_if_t<std::is_same_v<std::decay_t<typename std::iterator_traits<CIt>::value_type>, Component>>>
  15634. void insert(EIt first, EIt last, CIt from) {
  15635. static_assert(std::is_constructible_v<Component, typename std::iterator_traits<CIt>::value_type>, "Invalid value type");
  15636. ENTT_ASSERT(std::all_of(first, last, [this](const auto entity) { return valid(entity); }), "Invalid entity");
  15637. assure<Component>()->insert(*this, first, last, from);
  15638. }
  15639. /**
  15640. * @brief Assigns or replaces the given component for an entity.
  15641. *
  15642. * Equivalent to the following snippet (pseudocode):
  15643. *
  15644. * @code{.cpp}
  15645. * auto &component = registry.all_of<Component>(entity) ? registry.replace<Component>(entity, args...) : registry.emplace<Component>(entity, args...);
  15646. * @endcode
  15647. *
  15648. * Prefer this function anyway because it has slightly better performance.
  15649. *
  15650. * @warning
  15651. * Attempting to use an invalid entity results in undefined behavior.
  15652. *
  15653. * @tparam Component Type of component to assign or replace.
  15654. * @tparam Args Types of arguments to use to construct the component.
  15655. * @param entity A valid entity identifier.
  15656. * @param args Parameters to use to initialize the component.
  15657. * @return A reference to the newly created component.
  15658. */
  15659. template<typename Component, typename... Args>
  15660. decltype(auto) emplace_or_replace(const entity_type entity, Args &&... args) {
  15661. ENTT_ASSERT(valid(entity), "Invalid entity");
  15662. auto *cpool = assure<Component>();
  15663. return cpool->contains(entity)
  15664. ? cpool->patch(*this, entity, [&args...](auto &... curr) { ((curr = Component{std::forward<Args>(args)...}), ...); })
  15665. : cpool->emplace(*this, entity, std::forward<Args>(args)...);
  15666. }
  15667. /**
  15668. * @brief Patches the given component for an entity.
  15669. *
  15670. * The signature of the functions should be equivalent to the following:
  15671. *
  15672. * @code{.cpp}
  15673. * void(Component &);
  15674. * @endcode
  15675. *
  15676. * @note
  15677. * Empty types aren't explicitly instantiated and therefore they are never
  15678. * returned. However, this function can be used to trigger an update signal
  15679. * for them.
  15680. *
  15681. * @warning
  15682. * Attempting to use an invalid entity or to patch a component of an entity
  15683. * that doesn't own it results in undefined behavior.
  15684. *
  15685. * @tparam Component Type of component to patch.
  15686. * @tparam Func Types of the function objects to invoke.
  15687. * @param entity A valid entity identifier.
  15688. * @param func Valid function objects.
  15689. * @return A reference to the patched component.
  15690. */
  15691. template<typename Component, typename... Func>
  15692. decltype(auto) patch(const entity_type entity, Func &&... func) {
  15693. ENTT_ASSERT(valid(entity), "Invalid entity");
  15694. return assure<Component>()->patch(*this, entity, std::forward<Func>(func)...);
  15695. }
  15696. /**
  15697. * @brief Replaces the given component for an entity.
  15698. *
  15699. * A new instance of the given component is created and initialized with the
  15700. * arguments provided (the component must have a proper constructor or be of
  15701. * aggregate type). Then the component is assigned to the given entity.
  15702. *
  15703. * @warning
  15704. * Attempting to use an invalid entity or to replace a component of an
  15705. * entity that doesn't own it results in undefined behavior.
  15706. *
  15707. * @tparam Component Type of component to replace.
  15708. * @tparam Args Types of arguments to use to construct the component.
  15709. * @param entity A valid entity identifier.
  15710. * @param args Parameters to use to initialize the component.
  15711. * @return A reference to the component being replaced.
  15712. */
  15713. template<typename Component, typename... Args>
  15714. decltype(auto) replace(const entity_type entity, Args &&... args) {
  15715. return assure<Component>()->patch(*this, entity, [&args...](auto &... curr) { ((curr = Component{std::forward<Args>(args)...}), ...); });
  15716. }
  15717. /**
  15718. * @brief Removes the given components from an entity.
  15719. *
  15720. * @warning
  15721. * Attempting to use an invalid entity results in undefined behavior.
  15722. *
  15723. * @tparam Component Types of components to remove.
  15724. * @param entity A valid entity identifier.
  15725. * @return The number of components actually removed.
  15726. */
  15727. template<typename... Component>
  15728. size_type remove(const entity_type entity) {
  15729. ENTT_ASSERT(valid(entity), "Invalid entity");
  15730. static_assert(sizeof...(Component) > 0, "Provide one or more component types");
  15731. return (assure<Component>()->remove(entity, this) + ... + size_type{});
  15732. }
  15733. /**
  15734. * @brief Removes the given components from all the entities in a range.
  15735. *
  15736. * @sa remove
  15737. *
  15738. * @tparam Component Types of components to remove.
  15739. * @tparam It Type of input iterator.
  15740. * @param first An iterator to the first element of the range of entities.
  15741. * @param last An iterator past the last element of the range of entities.
  15742. * @return The number of components actually removed.
  15743. */
  15744. template<typename... Component, typename It>
  15745. size_type remove(It first, It last) {
  15746. static_assert(sizeof...(Component) > 0, "Provide one or more component types");
  15747. const auto cpools = std::make_tuple(assure<Component>()...);
  15748. size_type count{};
  15749. for(; first != last; ++first) {
  15750. const auto entity = *first;
  15751. ENTT_ASSERT(valid(entity), "Invalid entity");
  15752. count += (std::get<storage_type<Component> *>(cpools)->remove(entity, this) + ...);
  15753. }
  15754. return count;
  15755. }
  15756. /**
  15757. * @brief Erases the given components from an entity.
  15758. *
  15759. * @warning
  15760. * Attempting to use an invalid entity or to erase a component from an
  15761. * entity that doesn't own it results in undefined behavior.
  15762. *
  15763. * @tparam Component Types of components to erase.
  15764. * @param entity A valid entity identifier.
  15765. */
  15766. template<typename... Component>
  15767. void erase(const entity_type entity) {
  15768. ENTT_ASSERT(valid(entity), "Invalid entity");
  15769. static_assert(sizeof...(Component) > 0, "Provide one or more component types");
  15770. (assure<Component>()->erase(entity, this), ...);
  15771. }
  15772. /**
  15773. * @brief Erases the given components from all the entities in a range.
  15774. *
  15775. * @sa erase
  15776. *
  15777. * @tparam Component Types of components to erase.
  15778. * @tparam It Type of input iterator.
  15779. * @param first An iterator to the first element of the range of entities.
  15780. * @param last An iterator past the last element of the range of entities.
  15781. */
  15782. template<typename... Component, typename It>
  15783. void erase(It first, It last) {
  15784. static_assert(sizeof...(Component) > 0, "Provide one or more component types");
  15785. const auto cpools = std::make_tuple(assure<Component>()...);
  15786. for(; first != last; ++first) {
  15787. const auto entity = *first;
  15788. ENTT_ASSERT(valid(entity), "Invalid entity");
  15789. (std::get<storage_type<Component> *>(cpools)->erase(entity, this), ...);
  15790. }
  15791. }
  15792. /**
  15793. * @brief Removes all tombstones from a registry or only the pools for the
  15794. * given components.
  15795. * @tparam Component Types of components for which to clear all tombstones.
  15796. */
  15797. template<typename... Component>
  15798. void compact() {
  15799. if constexpr(sizeof...(Component) == 0) {
  15800. for(auto &&pdata: pools) {
  15801. pdata.pool && (pdata.pool->compact(), true);
  15802. }
  15803. } else {
  15804. (assure<Component>()->compact(), ...);
  15805. }
  15806. }
  15807. /*! @copydoc remove */
  15808. template<typename... Component>
  15809. [[deprecated("Use ::remove instead")]]
  15810. size_type remove_if_exists(const entity_type entity) {
  15811. return remove<Component...>(entity);
  15812. }
  15813. /**
  15814. * @brief Removes all the components from an entity and makes it orphaned.
  15815. *
  15816. * @warning
  15817. * In case there are listeners that observe the destruction of components
  15818. * and assign other components to the entity in their bodies, the result of
  15819. * invoking this function may not be as expected. In the worst case, it
  15820. * could lead to undefined behavior.
  15821. *
  15822. * @warning
  15823. * Attempting to use an invalid entity results in undefined behavior.
  15824. *
  15825. * @param entity A valid entity identifier.
  15826. */
  15827. [[deprecated("Use ::destroy(entity)/::create(entity) instead")]]
  15828. void remove_all(const entity_type entity) {
  15829. ENTT_ASSERT(valid(entity), "Invalid entity");
  15830. for(auto &&pdata: pools) {
  15831. pdata.pool && pdata.pool->remove(entity, this);
  15832. }
  15833. }
  15834. /**
  15835. * @brief Checks if an entity has all the given components.
  15836. *
  15837. * @warning
  15838. * Attempting to use an invalid entity results in undefined behavior.
  15839. *
  15840. * @tparam Component Components for which to perform the check.
  15841. * @param entity A valid entity identifier.
  15842. * @return True if the entity has all the components, false otherwise.
  15843. */
  15844. template<typename... Component>
  15845. [[nodiscard]] bool all_of(const entity_type entity) const {
  15846. ENTT_ASSERT(valid(entity), "Invalid entity");
  15847. return [entity](const auto *... cpool) { return ((cpool && cpool->contains(entity)) && ...); }(pool_if_exists<Component>()...);
  15848. }
  15849. /**
  15850. * @brief Checks if an entity has at least one of the given components.
  15851. *
  15852. * @warning
  15853. * Attempting to use an invalid entity results in undefined behavior.
  15854. *
  15855. * @tparam Component Components for which to perform the check.
  15856. * @param entity A valid entity identifier.
  15857. * @return True if the entity has at least one of the given components,
  15858. * false otherwise.
  15859. */
  15860. template<typename... Component>
  15861. [[nodiscard]] bool any_of(const entity_type entity) const {
  15862. ENTT_ASSERT(valid(entity), "Invalid entity");
  15863. return [entity](const auto *... cpool) { return !((!cpool || !cpool->contains(entity)) && ...); }(pool_if_exists<Component>()...);
  15864. }
  15865. /**
  15866. * @brief Returns references to the given components for an entity.
  15867. *
  15868. * @warning
  15869. * Attempting to use an invalid entity or to get a component from an entity
  15870. * that doesn't own it results in undefined behavior.
  15871. *
  15872. * @tparam Component Types of components to get.
  15873. * @param entity A valid entity identifier.
  15874. * @return References to the components owned by the entity.
  15875. */
  15876. template<typename... Component>
  15877. [[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entity) const {
  15878. ENTT_ASSERT(valid(entity), "Invalid entity");
  15879. if constexpr(sizeof...(Component) == 1) {
  15880. const auto *cpool = pool_if_exists<std::remove_const_t<Component>...>();
  15881. ENTT_ASSERT(cpool, "Storage not available");
  15882. return cpool->get(entity);
  15883. } else {
  15884. return std::forward_as_tuple(get<Component>(entity)...);
  15885. }
  15886. }
  15887. /*! @copydoc get */
  15888. template<typename... Component>
  15889. [[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entity) {
  15890. ENTT_ASSERT(valid(entity), "Invalid entity");
  15891. if constexpr(sizeof...(Component) == 1) {
  15892. return (const_cast<Component &>(assure<std::remove_const_t<Component>>()->get(entity)), ...);
  15893. } else {
  15894. return std::forward_as_tuple(get<Component>(entity)...);
  15895. }
  15896. }
  15897. /**
  15898. * @brief Returns a reference to the given component for an entity.
  15899. *
  15900. * In case the entity doesn't own the component, the parameters provided are
  15901. * used to construct it.<br/>
  15902. * Equivalent to the following snippet (pseudocode):
  15903. *
  15904. * @code{.cpp}
  15905. * auto &component = registry.all_of<Component>(entity) ? registry.get<Component>(entity) : registry.emplace<Component>(entity, args...);
  15906. * @endcode
  15907. *
  15908. * Prefer this function anyway because it has slightly better performance.
  15909. *
  15910. * @warning
  15911. * Attempting to use an invalid entity results in undefined behavior.
  15912. *
  15913. * @tparam Component Type of component to get.
  15914. * @tparam Args Types of arguments to use to construct the component.
  15915. * @param entity A valid entity identifier.
  15916. * @param args Parameters to use to initialize the component.
  15917. * @return Reference to the component owned by the entity.
  15918. */
  15919. template<typename Component, typename... Args>
  15920. [[nodiscard]] decltype(auto) get_or_emplace(const entity_type entity, Args &&... args) {
  15921. ENTT_ASSERT(valid(entity), "Invalid entity");
  15922. auto *cpool = assure<Component>();
  15923. return cpool->contains(entity) ? cpool->get(entity) : cpool->emplace(*this, entity, std::forward<Args>(args)...);
  15924. }
  15925. /**
  15926. * @brief Returns pointers to the given components for an entity.
  15927. *
  15928. * @warning
  15929. * Attempting to use an invalid entity results in undefined behavior.
  15930. *
  15931. * @note
  15932. * The registry retains ownership of the pointed-to components.
  15933. *
  15934. * @tparam Component Types of components to get.
  15935. * @param entity A valid entity identifier.
  15936. * @return Pointers to the components owned by the entity.
  15937. */
  15938. template<typename... Component>
  15939. [[nodiscard]] auto try_get([[maybe_unused]] const entity_type entity) const {
  15940. ENTT_ASSERT(valid(entity), "Invalid entity");
  15941. if constexpr(sizeof...(Component) == 1) {
  15942. const auto *cpool = pool_if_exists<std::remove_const_t<Component>...>();
  15943. return (cpool && cpool->contains(entity)) ? &cpool->get(entity) : nullptr;
  15944. } else {
  15945. return std::make_tuple(try_get<Component>(entity)...);
  15946. }
  15947. }
  15948. /*! @copydoc try_get */
  15949. template<typename... Component>
  15950. [[nodiscard]] auto try_get([[maybe_unused]] const entity_type entity) {
  15951. ENTT_ASSERT(valid(entity), "Invalid entity");
  15952. if constexpr(sizeof...(Component) == 1) {
  15953. return (const_cast<Component *>(std::as_const(*this).template try_get<Component>(entity)), ...);
  15954. } else {
  15955. return std::make_tuple(try_get<Component>(entity)...);
  15956. }
  15957. }
  15958. /**
  15959. * @brief Clears a whole registry or the pools for the given components.
  15960. * @tparam Component Types of components to remove from their entities.
  15961. */
  15962. template<typename... Component>
  15963. void clear() {
  15964. if constexpr(sizeof...(Component) == 0) {
  15965. for(auto &&pdata: pools) {
  15966. pdata.pool && (pdata.pool->clear(this), true);
  15967. }
  15968. each([this](const auto entity) { release_entity(entity, version(entity) + 1u); });
  15969. } else {
  15970. (assure<Component>()->clear(this), ...);
  15971. }
  15972. }
  15973. /**
  15974. * @brief Iterates all the entities that are still in use.
  15975. *
  15976. * The function object is invoked for each entity that is still in use.<br/>
  15977. * The signature of the function should be equivalent to the following:
  15978. *
  15979. * @code{.cpp}
  15980. * void(const Entity);
  15981. * @endcode
  15982. *
  15983. * This function is fairly slow and should not be used frequently. However,
  15984. * it's useful for iterating all the entities still in use, regardless of
  15985. * their components.
  15986. *
  15987. * @tparam Func Type of the function object to invoke.
  15988. * @param func A valid function object.
  15989. */
  15990. template<typename Func>
  15991. void each(Func func) const {
  15992. if(free_list == null) {
  15993. for(auto pos = entities.size(); pos; --pos) {
  15994. func(entities[pos-1]);
  15995. }
  15996. } else {
  15997. for(auto pos = entities.size(); pos; --pos) {
  15998. if(const auto entity = entities[pos - 1]; traits_type::to_entity(entity) == (pos - 1)) {
  15999. func(entity);
  16000. }
  16001. }
  16002. }
  16003. }
  16004. /**
  16005. * @brief Checks if an entity has components assigned.
  16006. * @param entity A valid entity identifier.
  16007. * @return True if the entity has no components assigned, false otherwise.
  16008. */
  16009. [[nodiscard]] bool orphan(const entity_type entity) const {
  16010. ENTT_ASSERT(valid(entity), "Invalid entity");
  16011. return std::none_of(pools.cbegin(), pools.cend(), [entity](auto &&pdata) { return pdata.pool && pdata.pool->contains(entity); });
  16012. }
  16013. /**
  16014. * @brief Iterates orphans and applies them the given function object.
  16015. *
  16016. * The function object is invoked for each entity that is still in use and
  16017. * has no components assigned.<br/>
  16018. * The signature of the function should be equivalent to the following:
  16019. *
  16020. * @code{.cpp}
  16021. * void(const Entity);
  16022. * @endcode
  16023. *
  16024. * This function can be very slow and should not be used frequently.
  16025. *
  16026. * @tparam Func Type of the function object to invoke.
  16027. * @param func A valid function object.
  16028. */
  16029. template<typename Func>
  16030. void orphans(Func func) const {
  16031. each([this, &func](const auto entity) {
  16032. if(orphan(entity)) {
  16033. func(entity);
  16034. }
  16035. });
  16036. }
  16037. /**
  16038. * @brief Returns a sink object for the given component.
  16039. *
  16040. * The sink returned by this function can be used to receive notifications
  16041. * whenever a new instance of the given component is created and assigned to
  16042. * an entity.<br/>
  16043. * The function type for a listener is equivalent to:
  16044. *
  16045. * @code{.cpp}
  16046. * void(basic_registry<Entity> &, Entity);
  16047. * @endcode
  16048. *
  16049. * Listeners are invoked **after** the component has been assigned to the
  16050. * entity.
  16051. *
  16052. * @sa sink
  16053. *
  16054. * @tparam Component Type of component of which to get the sink.
  16055. * @return A temporary sink object.
  16056. */
  16057. template<typename Component>
  16058. [[nodiscard]] auto on_construct() {
  16059. return assure<Component>()->on_construct();
  16060. }
  16061. /**
  16062. * @brief Returns a sink object for the given component.
  16063. *
  16064. * The sink returned by this function can be used to receive notifications
  16065. * whenever an instance of the given component is explicitly updated.<br/>
  16066. * The function type for a listener is equivalent to:
  16067. *
  16068. * @code{.cpp}
  16069. * void(basic_registry<Entity> &, Entity);
  16070. * @endcode
  16071. *
  16072. * Listeners are invoked **after** the component has been updated.
  16073. *
  16074. * @sa sink
  16075. *
  16076. * @tparam Component Type of component of which to get the sink.
  16077. * @return A temporary sink object.
  16078. */
  16079. template<typename Component>
  16080. [[nodiscard]] auto on_update() {
  16081. return assure<Component>()->on_update();
  16082. }
  16083. /**
  16084. * @brief Returns a sink object for the given component.
  16085. *
  16086. * The sink returned by this function can be used to receive notifications
  16087. * whenever an instance of the given component is removed from an entity and
  16088. * thus destroyed.<br/>
  16089. * The function type for a listener is equivalent to:
  16090. *
  16091. * @code{.cpp}
  16092. * void(basic_registry<Entity> &, Entity);
  16093. * @endcode
  16094. *
  16095. * Listeners are invoked **before** the component has been removed from the
  16096. * entity.
  16097. *
  16098. * @sa sink
  16099. *
  16100. * @tparam Component Type of component of which to get the sink.
  16101. * @return A temporary sink object.
  16102. */
  16103. template<typename Component>
  16104. [[nodiscard]] auto on_destroy() {
  16105. return assure<Component>()->on_destroy();
  16106. }
  16107. /**
  16108. * @brief Returns a view for the given components.
  16109. *
  16110. * This kind of objects are created on the fly and share with the registry
  16111. * its internal data structures.<br/>
  16112. * Feel free to discard a view after the use. Creating and destroying a view
  16113. * is an incredibly cheap operation because they do not require any type of
  16114. * initialization.<br/>
  16115. * As a rule of thumb, storing a view should never be an option.
  16116. *
  16117. * Views do their best to iterate the smallest set of candidate entities.
  16118. * In particular:
  16119. *
  16120. * * Single component views are incredibly fast and iterate a packed array
  16121. * of entities, all of which has the given component.
  16122. * * Multi component views look at the number of entities available for each
  16123. * component and pick up a reference to the smallest set of candidates to
  16124. * test for the given components.
  16125. *
  16126. * Views in no way affect the functionalities of the registry nor those of
  16127. * the underlying pools.
  16128. *
  16129. * @note
  16130. * Multi component views are pretty fast. However their performance tend to
  16131. * degenerate when the number of components to iterate grows up and the most
  16132. * of the entities have all the given components.<br/>
  16133. * To get a performance boost, consider using a group instead.
  16134. *
  16135. * @tparam Component Type of components used to construct the view.
  16136. * @tparam Exclude Types of components used to filter the view.
  16137. * @return A newly created view.
  16138. */
  16139. template<typename... Component, typename... Exclude>
  16140. [[nodiscard]] basic_view<Entity, exclude_t<Exclude...>, std::add_const_t<Component>...> view(exclude_t<Exclude...> = {}) const {
  16141. static_assert(sizeof...(Component) > 0, "Exclusion-only views are not supported");
  16142. return { *assure<std::remove_const_t<Component>>()..., *assure<Exclude>()... };
  16143. }
  16144. /*! @copydoc view */
  16145. template<typename... Component, typename... Exclude>
  16146. [[nodiscard]] basic_view<Entity, exclude_t<Exclude...>, Component...> view(exclude_t<Exclude...> = {}) {
  16147. static_assert(sizeof...(Component) > 0, "Exclusion-only views are not supported");
  16148. return { *assure<std::remove_const_t<Component>>()..., *assure<Exclude>()... };
  16149. }
  16150. /**
  16151. * @brief Returns a runtime view for the given components.
  16152. *
  16153. * This kind of objects are created on the fly and share with the registry
  16154. * its internal data structures.<br/>
  16155. * Users should throw away the view after use. Fortunately, creating and
  16156. * destroying a runtime view is an incredibly cheap operation because they
  16157. * do not require any type of initialization.<br/>
  16158. * As a rule of thumb, storing a view should never be an option.
  16159. *
  16160. * Runtime views are to be used when users want to construct a view from
  16161. * some external inputs and don't know at compile-time what are the required
  16162. * components.
  16163. *
  16164. * @tparam ItComp Type of input iterator for the components to use to
  16165. * construct the view.
  16166. * @tparam ItExcl Type of input iterator for the components to use to filter
  16167. * the view.
  16168. * @param first An iterator to the first element of the range of components
  16169. * to use to construct the view.
  16170. * @param last An iterator past the last element of the range of components
  16171. * to use to construct the view.
  16172. * @param from An iterator to the first element of the range of components
  16173. * to use to filter the view.
  16174. * @param to An iterator past the last element of the range of components to
  16175. * use to filter the view.
  16176. * @return A newly created runtime view.
  16177. */
  16178. template<typename ItComp, typename ItExcl = id_type *>
  16179. [[nodiscard]] basic_runtime_view<Entity> runtime_view(ItComp first, ItComp last, ItExcl from = {}, ItExcl to = {}) const {
  16180. std::vector<const basic_common_type *> component(std::distance(first, last));
  16181. std::vector<const basic_common_type *> filter(std::distance(from, to));
  16182. std::transform(first, last, component.begin(), [this](const auto ctype) {
  16183. const auto it = std::find_if(pools.cbegin(), pools.cend(), [ctype](auto &&pdata) { return pdata.poly && pdata.poly->value_type().hash() == ctype; });
  16184. return it == pools.cend() ? nullptr : it->pool.get();
  16185. });
  16186. std::transform(from, to, filter.begin(), [this](const auto ctype) {
  16187. const auto it = std::find_if(pools.cbegin(), pools.cend(), [ctype](auto &&pdata) { return pdata.poly && pdata.poly->value_type().hash() == ctype; });
  16188. return it == pools.cend() ? nullptr : it->pool.get();
  16189. });
  16190. return { std::move(component), std::move(filter) };
  16191. }
  16192. /**
  16193. * @brief Returns a group for the given components.
  16194. *
  16195. * This kind of objects are created on the fly and share with the registry
  16196. * its internal data structures.<br/>
  16197. * Feel free to discard a group after the use. Creating and destroying a
  16198. * group is an incredibly cheap operation because they do not require any
  16199. * type of initialization, but for the first time they are requested.<br/>
  16200. * As a rule of thumb, storing a group should never be an option.
  16201. *
  16202. * Groups support exclusion lists and can own types of components. The more
  16203. * types are owned by a group, the faster it is to iterate entities and
  16204. * components.<br/>
  16205. * However, groups also affect some features of the registry such as the
  16206. * creation and destruction of components, which will consequently be
  16207. * slightly slower (nothing that can be noticed in most cases).
  16208. *
  16209. * @note
  16210. * Pools of components that are owned by a group cannot be sorted anymore.
  16211. * The group takes the ownership of the pools and arrange components so as
  16212. * to iterate them as fast as possible.
  16213. *
  16214. * @tparam Owned Types of components owned by the group.
  16215. * @tparam Get Types of components observed by the group.
  16216. * @tparam Exclude Types of components used to filter the group.
  16217. * @return A newly created group.
  16218. */
  16219. template<typename... Owned, typename... Get, typename... Exclude>
  16220. [[nodiscard]] basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>, Owned...> group(get_t<Get...>, exclude_t<Exclude...> = {}) {
  16221. static_assert(sizeof...(Owned) + sizeof...(Get) > 0, "Exclusion-only groups are not supported");
  16222. static_assert(sizeof...(Owned) + sizeof...(Get) + sizeof...(Exclude) > 1, "Single component groups are not allowed");
  16223. using handler_type = group_handler<exclude_t<Exclude...>, get_t<std::remove_const_t<Get>...>, std::remove_const_t<Owned>...>;
  16224. const auto cpools = std::make_tuple(assure<std::remove_const_t<Owned>>()..., assure<std::remove_const_t<Get>>()...);
  16225. constexpr auto size = sizeof...(Owned) + sizeof...(Get) + sizeof...(Exclude);
  16226. handler_type *handler = nullptr;
  16227. if(auto it = std::find_if(groups.cbegin(), groups.cend(), [size](const auto &gdata) {
  16228. return gdata.size == size
  16229. && (gdata.owned(type_hash<std::remove_const_t<Owned>>::value()) && ...)
  16230. && (gdata.get(type_hash<std::remove_const_t<Get>>::value()) && ...)
  16231. && (gdata.exclude(type_hash<Exclude>::value()) && ...);
  16232. }); it != groups.cend())
  16233. {
  16234. handler = static_cast<handler_type *>(it->group.get());
  16235. }
  16236. if(!handler) {
  16237. group_data candidate = {
  16238. size,
  16239. { new handler_type{}, [](void *instance) { delete static_cast<handler_type *>(instance); } },
  16240. []([[maybe_unused]] const id_type ctype) ENTT_NOEXCEPT { return ((ctype == type_hash<std::remove_const_t<Owned>>::value()) || ...); },
  16241. []([[maybe_unused]] const id_type ctype) ENTT_NOEXCEPT { return ((ctype == type_hash<std::remove_const_t<Get>>::value()) || ...); },
  16242. []([[maybe_unused]] const id_type ctype) ENTT_NOEXCEPT { return ((ctype == type_hash<Exclude>::value()) || ...); },
  16243. };
  16244. handler = static_cast<handler_type *>(candidate.group.get());
  16245. const void *maybe_valid_if = nullptr;
  16246. const void *discard_if = nullptr;
  16247. if constexpr(sizeof...(Owned) == 0) {
  16248. groups.push_back(std::move(candidate));
  16249. } else {
  16250. ENTT_ASSERT(std::all_of(groups.cbegin(), groups.cend(), [size](const auto &gdata) {
  16251. const auto overlapping = (0u + ... + gdata.owned(type_hash<std::remove_const_t<Owned>>::value()));
  16252. const auto sz = overlapping + (0u + ... + gdata.get(type_hash<std::remove_const_t<Get>>::value())) + (0u + ... + gdata.exclude(type_hash<Exclude>::value()));
  16253. return !overlapping || ((sz == size) || (sz == gdata.size));
  16254. }), "Conflicting groups");
  16255. const auto next = std::find_if_not(groups.cbegin(), groups.cend(), [size](const auto &gdata) {
  16256. return !(0u + ... + gdata.owned(type_hash<std::remove_const_t<Owned>>::value())) || (size > gdata.size);
  16257. });
  16258. const auto prev = std::find_if(std::make_reverse_iterator(next), groups.crend(), [](const auto &gdata) {
  16259. return (0u + ... + gdata.owned(type_hash<std::remove_const_t<Owned>>::value()));
  16260. });
  16261. maybe_valid_if = (next == groups.cend() ? maybe_valid_if : next->group.get());
  16262. discard_if = (prev == groups.crend() ? discard_if : prev->group.get());
  16263. groups.insert(next, std::move(candidate));
  16264. }
  16265. (on_construct<std::remove_const_t<Owned>>().before(maybe_valid_if).template connect<&handler_type::template maybe_valid_if<std::remove_const_t<Owned>>>(*handler), ...);
  16266. (on_construct<std::remove_const_t<Get>>().before(maybe_valid_if).template connect<&handler_type::template maybe_valid_if<std::remove_const_t<Get>>>(*handler), ...);
  16267. (on_destroy<Exclude>().before(maybe_valid_if).template connect<&handler_type::template maybe_valid_if<Exclude>>(*handler), ...);
  16268. (on_destroy<std::remove_const_t<Owned>>().before(discard_if).template connect<&handler_type::discard_if>(*handler), ...);
  16269. (on_destroy<std::remove_const_t<Get>>().before(discard_if).template connect<&handler_type::discard_if>(*handler), ...);
  16270. (on_construct<Exclude>().before(discard_if).template connect<&handler_type::discard_if>(*handler), ...);
  16271. if constexpr(sizeof...(Owned) == 0) {
  16272. for(const auto entity: view<Owned..., Get...>(exclude<Exclude...>)) {
  16273. handler->current.emplace(entity);
  16274. }
  16275. } else {
  16276. // we cannot iterate backwards because we want to leave behind valid entities in case of owned types
  16277. for(auto *first = std::get<0>(cpools)->data(), *last = first + std::get<0>(cpools)->size(); first != last; ++first) {
  16278. handler->template maybe_valid_if<type_list_element_t<0, type_list<std::remove_const_t<Owned>...>>>(*this, *first);
  16279. }
  16280. }
  16281. }
  16282. return { handler->current, *std::get<storage_type<std::remove_const_t<Owned>> *>(cpools)..., *std::get<storage_type<std::remove_const_t<Get>> *>(cpools)... };
  16283. }
  16284. /**
  16285. * @brief Returns a group for the given components.
  16286. *
  16287. * @sa group
  16288. *
  16289. * @tparam Owned Types of components owned by the group.
  16290. * @tparam Get Types of components observed by the group.
  16291. * @tparam Exclude Types of components used to filter the group.
  16292. * @return A newly created group.
  16293. */
  16294. template<typename... Owned, typename... Get, typename... Exclude>
  16295. [[nodiscard]] basic_group<Entity, exclude_t<Exclude...>, get_t<std::add_const_t<Get>...>, std::add_const_t<Owned>...> group_if_exists(get_t<Get...>, exclude_t<Exclude...> = {}) const {
  16296. if(auto it = std::find_if(groups.cbegin(), groups.cend(), [](const auto &gdata) {
  16297. return gdata.size == (sizeof...(Owned) + sizeof...(Get) + sizeof...(Exclude))
  16298. && (gdata.owned(type_hash<std::remove_const_t<Owned>>::value()) && ...)
  16299. && (gdata.get(type_hash<std::remove_const_t<Get>>::value()) && ...)
  16300. && (gdata.exclude(type_hash<Exclude>::value()) && ...);
  16301. }); it == groups.cend())
  16302. {
  16303. return {};
  16304. } else {
  16305. using handler_type = group_handler<exclude_t<Exclude...>, get_t<std::remove_const_t<Get>...>, std::remove_const_t<Owned>...>;
  16306. return { static_cast<handler_type *>(it->group.get())->current, *pool_if_exists<std::remove_const_t<Owned>>()... , *pool_if_exists<std::remove_const_t<Get>>()... };
  16307. }
  16308. }
  16309. /**
  16310. * @brief Returns a group for the given components.
  16311. *
  16312. * @sa group
  16313. *
  16314. * @tparam Owned Types of components owned by the group.
  16315. * @tparam Exclude Types of components used to filter the group.
  16316. * @return A newly created group.
  16317. */
  16318. template<typename... Owned, typename... Exclude>
  16319. [[nodiscard]] basic_group<Entity, exclude_t<Exclude...>, get_t<>, Owned...> group(exclude_t<Exclude...> = {}) {
  16320. return group<Owned...>(get_t<>{}, exclude<Exclude...>);
  16321. }
  16322. /**
  16323. * @brief Returns a group for the given components.
  16324. *
  16325. * @sa group_if_exists
  16326. *
  16327. * @tparam Owned Types of components owned by the group.
  16328. * @tparam Exclude Types of components used to filter the group.
  16329. * @return A newly created group.
  16330. */
  16331. template<typename... Owned, typename... Exclude>
  16332. [[nodiscard]] basic_group<Entity, exclude_t<Exclude...>, get_t<>, std::add_const_t<Owned>...> group_if_exists(exclude_t<Exclude...> = {}) const {
  16333. return group_if_exists<std::add_const_t<Owned>...>(get_t<>{}, exclude<Exclude...>);
  16334. }
  16335. /**
  16336. * @brief Checks whether the given components belong to any group.
  16337. * @tparam Component Types of components in which one is interested.
  16338. * @return True if the pools of the given components are sortable, false
  16339. * otherwise.
  16340. */
  16341. template<typename... Component>
  16342. [[nodiscard]] bool sortable() const {
  16343. return std::none_of(groups.cbegin(), groups.cend(), [](auto &&gdata) { return (gdata.owned(type_hash<std::remove_const_t<Component>>::value()) || ...); });
  16344. }
  16345. /**
  16346. * @brief Checks whether a group can be sorted.
  16347. * @tparam Owned Types of components owned by the group.
  16348. * @tparam Get Types of components observed by the group.
  16349. * @tparam Exclude Types of components used to filter the group.
  16350. * @return True if the group can be sorted, false otherwise.
  16351. */
  16352. template<typename... Owned, typename... Get, typename... Exclude>
  16353. [[nodiscard]] bool sortable(const basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>, Owned...> &) ENTT_NOEXCEPT {
  16354. constexpr auto size = sizeof...(Owned) + sizeof...(Get) + sizeof...(Exclude);
  16355. return std::find_if(groups.cbegin(), groups.cend(), [size](const auto &gdata) {
  16356. return (0u + ... + gdata.owned(type_hash<std::remove_const_t<Owned>>::value())) && (size < gdata.size);
  16357. }) == groups.cend();
  16358. }
  16359. /**
  16360. * @brief Sorts the pool of entities for the given component.
  16361. *
  16362. * The order of the elements in a pool is highly affected by assignments
  16363. * of components to entities and deletions. Components are arranged to
  16364. * maximize the performance during iterations and users should not make any
  16365. * assumption on the order.<br/>
  16366. * This function can be used to impose an order to the elements in the pool
  16367. * of the given component. The order is kept valid until a component of the
  16368. * given type is assigned or removed from an entity.
  16369. *
  16370. * The comparison function object must return `true` if the first element
  16371. * is _less_ than the second one, `false` otherwise. The signature of the
  16372. * comparison function should be equivalent to one of the following:
  16373. *
  16374. * @code{.cpp}
  16375. * bool(const Entity, const Entity);
  16376. * bool(const Component &, const Component &);
  16377. * @endcode
  16378. *
  16379. * Moreover, the comparison function object shall induce a
  16380. * _strict weak ordering_ on the values.
  16381. *
  16382. * The sort function oject must offer a member function template
  16383. * `operator()` that accepts three arguments:
  16384. *
  16385. * * An iterator to the first element of the range to sort.
  16386. * * An iterator past the last element of the range to sort.
  16387. * * A comparison function to use to compare the elements.
  16388. *
  16389. * The comparison funtion object received by the sort function object hasn't
  16390. * necessarily the type of the one passed along with the other parameters to
  16391. * this member function.
  16392. *
  16393. * @warning
  16394. * Pools of components owned by a group cannot be sorted.
  16395. *
  16396. * @tparam Component Type of components to sort.
  16397. * @tparam Compare Type of comparison function object.
  16398. * @tparam Sort Type of sort function object.
  16399. * @tparam Args Types of arguments to forward to the sort function object.
  16400. * @param compare A valid comparison function object.
  16401. * @param algo A valid sort function object.
  16402. * @param args Arguments to forward to the sort function object, if any.
  16403. */
  16404. template<typename Component, typename Compare, typename Sort = std_sort, typename... Args>
  16405. void sort(Compare compare, Sort algo = Sort{}, Args &&... args) {
  16406. ENTT_ASSERT(sortable<Component>(), "Cannot sort owned storage");
  16407. assure<Component>()->sort(std::move(compare), std::move(algo), std::forward<Args>(args)...);
  16408. }
  16409. /**
  16410. * @brief Sorts two pools of components in the same way.
  16411. *
  16412. * The order of the elements in a pool is highly affected by assignments
  16413. * of components to entities and deletions. Components are arranged to
  16414. * maximize the performance during iterations and users should not make any
  16415. * assumption on the order.
  16416. *
  16417. * It happens that different pools of components must be sorted the same way
  16418. * because of runtime and/or performance constraints. This function can be
  16419. * used to order a pool of components according to the order between the
  16420. * entities in another pool of components.
  16421. *
  16422. * @b How @b it @b works
  16423. *
  16424. * Being `A` and `B` the two sets where `B` is the master (the one the order
  16425. * of which rules) and `A` is the slave (the one to sort), after a call to
  16426. * this function an iterator for `A` will return the entities according to
  16427. * the following rules:
  16428. *
  16429. * * All the entities in `A` that are also in `B` are returned first
  16430. * according to the order they have in `B`.
  16431. * * All the entities in `A` that are not in `B` are returned in no
  16432. * particular order after all the other entities.
  16433. *
  16434. * Any subsequent change to `B` won't affect the order in `A`.
  16435. *
  16436. * @warning
  16437. * Pools of components owned by a group cannot be sorted.
  16438. *
  16439. * @tparam To Type of components to sort.
  16440. * @tparam From Type of components to use to sort.
  16441. */
  16442. template<typename To, typename From>
  16443. void sort() {
  16444. ENTT_ASSERT(sortable<To>(), "Cannot sort owned storage");
  16445. assure<To>()->respect(*assure<From>());
  16446. }
  16447. /**
  16448. * @brief Visits an entity and returns the type info for its components.
  16449. *
  16450. * The signature of the function should be equivalent to the following:
  16451. *
  16452. * @code{.cpp}
  16453. * void(const type_info);
  16454. * @endcode
  16455. *
  16456. * Returned identifiers are those of the components owned by the entity.
  16457. *
  16458. * @sa type_info
  16459. *
  16460. * @warning
  16461. * It's not specified whether a component attached to or removed from the
  16462. * given entity during the visit is returned or not to the caller.
  16463. *
  16464. * @tparam Func Type of the function object to invoke.
  16465. * @param entity A valid entity identifier.
  16466. * @param func A valid function object.
  16467. */
  16468. template<typename Func>
  16469. void visit(entity_type entity, Func func) const {
  16470. for(auto pos = pools.size(); pos; --pos) {
  16471. if(const auto &pdata = pools[pos-1]; pdata.pool && pdata.pool->contains(entity)) {
  16472. func(pdata.poly->value_type());
  16473. }
  16474. }
  16475. }
  16476. /**
  16477. * @brief Visits a registry and returns the type info for its components.
  16478. *
  16479. * The signature of the function should be equivalent to the following:
  16480. *
  16481. * @code{.cpp}
  16482. * void(const type_info);
  16483. * @endcode
  16484. *
  16485. * Returned identifiers are those of the components managed by the registry.
  16486. *
  16487. * @sa type_info
  16488. *
  16489. * @warning
  16490. * It's not specified whether a component for which a pool is created during
  16491. * the visit is returned or not to the caller.
  16492. *
  16493. * @tparam Func Type of the function object to invoke.
  16494. * @param func A valid function object.
  16495. */
  16496. template<typename Func>
  16497. void visit(Func func) const {
  16498. for(auto pos = pools.size(); pos; --pos) {
  16499. if(const auto &pdata = pools[pos-1]; pdata.pool) {
  16500. func(pdata.poly->value_type());
  16501. }
  16502. }
  16503. }
  16504. /**
  16505. * @brief Binds an object to the context of the registry.
  16506. *
  16507. * If the value already exists it is overwritten, otherwise a new instance
  16508. * of the given type is created and initialized with the arguments provided.
  16509. *
  16510. * @tparam Type Type of object to set.
  16511. * @tparam Args Types of arguments to use to construct the object.
  16512. * @param args Parameters to use to initialize the value.
  16513. * @return A reference to the newly created object.
  16514. */
  16515. template<typename Type, typename... Args>
  16516. Type & set(Args &&... args) {
  16517. unset<Type>();
  16518. vars.emplace_back(std::in_place_type<Type>, std::forward<Args>(args)...);
  16519. return any_cast<Type &>(vars.back());
  16520. }
  16521. /**
  16522. * @brief Unsets a context variable if it exists.
  16523. * @tparam Type Type of object to set.
  16524. */
  16525. template<typename Type>
  16526. void unset() {
  16527. vars.erase(std::remove_if(vars.begin(), vars.end(), [type = type_id<Type>()](auto &&var) { return var.type() == type; }), vars.end());
  16528. }
  16529. /**
  16530. * @brief Binds an object to the context of the registry.
  16531. *
  16532. * In case the context doesn't contain the given object, the parameters
  16533. * provided are used to construct it.
  16534. *
  16535. * @tparam Type Type of object to set.
  16536. * @tparam Args Types of arguments to use to construct the object.
  16537. * @param args Parameters to use to initialize the object.
  16538. * @return A reference to the object in the context of the registry.
  16539. */
  16540. template<typename Type, typename... Args>
  16541. [[nodiscard]] Type & ctx_or_set(Args &&... args) {
  16542. auto *value = try_ctx<Type>();
  16543. return value ? *value : set<Type>(std::forward<Args>(args)...);
  16544. }
  16545. /**
  16546. * @brief Returns a pointer to an object in the context of the registry.
  16547. * @tparam Type Type of object to get.
  16548. * @return A pointer to the object if it exists in the context of the
  16549. * registry, a null pointer otherwise.
  16550. */
  16551. template<typename Type>
  16552. [[nodiscard]] std::add_const_t<Type> * try_ctx() const {
  16553. auto it = std::find_if(vars.cbegin(), vars.cend(), [type = type_id<Type>()](auto &&var) { return var.type() == type; });
  16554. return it == vars.cend() ? nullptr : any_cast<std::add_const_t<Type>>(&*it);
  16555. }
  16556. /*! @copydoc try_ctx */
  16557. template<typename Type>
  16558. [[nodiscard]] Type * try_ctx() {
  16559. auto it = std::find_if(vars.begin(), vars.end(), [type = type_id<Type>()](auto &&var) { return var.type() == type; });
  16560. return it == vars.end() ? nullptr : any_cast<Type>(&*it);
  16561. }
  16562. /**
  16563. * @brief Returns a reference to an object in the context of the registry.
  16564. *
  16565. * @warning
  16566. * Attempting to get a context variable that doesn't exist results in
  16567. * undefined behavior.
  16568. *
  16569. * @tparam Type Type of object to get.
  16570. * @return A valid reference to the object in the context of the registry.
  16571. */
  16572. template<typename Type>
  16573. [[nodiscard]] std::add_const_t<Type> & ctx() const {
  16574. auto it = std::find_if(vars.cbegin(), vars.cend(), [type = type_id<Type>()](auto &&var) { return var.type() == type; });
  16575. ENTT_ASSERT(it != vars.cend(), "Invalid instance");
  16576. return any_cast<std::add_const_t<Type> &>(*it);
  16577. }
  16578. /*! @copydoc ctx */
  16579. template<typename Type>
  16580. [[nodiscard]] Type & ctx() {
  16581. auto it = std::find_if(vars.begin(), vars.end(), [type = type_id<Type>()](auto &&var) { return var.type() == type; });
  16582. ENTT_ASSERT(it != vars.end(), "Invalid instance");
  16583. return any_cast<Type &>(*it);
  16584. }
  16585. /**
  16586. * @brief Visits a registry and returns the type info for its context
  16587. * variables.
  16588. *
  16589. * The signature of the function should be equivalent to the following:
  16590. *
  16591. * @code{.cpp}
  16592. * void(const type_info);
  16593. * @endcode
  16594. *
  16595. * Returned identifiers are those of the context variables currently set.
  16596. *
  16597. * @sa type_info
  16598. *
  16599. * @warning
  16600. * It's not specified whether a context variable created during the visit is
  16601. * returned or not to the caller.
  16602. *
  16603. * @tparam Func Type of the function object to invoke.
  16604. * @param func A valid function object.
  16605. */
  16606. template<typename Func>
  16607. void ctx(Func func) const {
  16608. for(auto pos = vars.size(); pos; --pos) {
  16609. func(vars[pos-1].type());
  16610. }
  16611. }
  16612. private:
  16613. std::vector<basic_any<0u>> vars{};
  16614. mutable std::vector<pool_data> pools{};
  16615. std::vector<group_data> groups{};
  16616. std::vector<entity_type> entities{};
  16617. entity_type free_list{tombstone};
  16618. };
  16619. }
  16620. #endif
  16621. namespace entt {
  16622. /**
  16623. * @brief Non-owning handle to an entity.
  16624. *
  16625. * Tiny wrapper around a registry and an entity.
  16626. *
  16627. * @tparam Entity A valid entity type (see entt_traits for more details).
  16628. * @tparam Type Types to which to restrict the scope of a handle.
  16629. */
  16630. template<typename Entity, typename... Type>
  16631. struct basic_handle {
  16632. /*! @brief Type of registry accepted by the handle. */
  16633. using registry_type = constness_as_t<basic_registry<std::remove_const_t<Entity>>, Entity>;
  16634. /*! @brief Underlying entity identifier. */
  16635. using entity_type = typename registry_type::entity_type;
  16636. /*! @brief Underlying version type. */
  16637. using version_type = typename registry_type::version_type;
  16638. /*! @brief Unsigned integer type. */
  16639. using size_type = typename registry_type::size_type;
  16640. /*! @brief Constructs an invalid handle. */
  16641. basic_handle() ENTT_NOEXCEPT
  16642. : reg{}, entt{null}
  16643. {}
  16644. /**
  16645. * @brief Constructs a handle from a given registry and entity.
  16646. * @param ref An instance of the registry class.
  16647. * @param value An entity identifier.
  16648. */
  16649. basic_handle(registry_type &ref, entity_type value) ENTT_NOEXCEPT
  16650. : reg{&ref}, entt{value}
  16651. {}
  16652. /**
  16653. * @brief Compares two handles.
  16654. * @tparam Args Template parameters of the handle with which to compare.
  16655. * @param other Handle with which to compare.
  16656. * @return True if both handles refer to the same registry and the same
  16657. * entity, false otherwise.
  16658. */
  16659. template<typename... Args>
  16660. [[nodiscard]] bool operator==(const basic_handle<Args...> &other) const ENTT_NOEXCEPT {
  16661. return reg == other.registry() && entt == other.entity();
  16662. }
  16663. /**
  16664. * @brief Constructs a const handle from a non-const one.
  16665. * @tparam Other A valid entity type (see entt_traits for more details).
  16666. * @tparam Args Scope of the handle to construct.
  16667. * @return A const handle referring to the same registry and the same
  16668. * entity.
  16669. */
  16670. template<typename Other, typename... Args>
  16671. operator basic_handle<Other, Args...>() const ENTT_NOEXCEPT {
  16672. static_assert(
  16673. (std::is_same_v<Other, Entity> || std::is_same_v<std::remove_const_t<Other>, Entity>)
  16674. && (sizeof...(Type) == 0 || ((sizeof...(Args) != 0 && sizeof...(Args) <= sizeof...(Type)) && ... && (type_list_contains_v<type_list<Type...>, Args>))),
  16675. "Invalid conversion between different handles"
  16676. );
  16677. return reg ? basic_handle<Other, Args...>{*reg, entt} : basic_handle<Other, Args...>{};
  16678. }
  16679. /**
  16680. * @brief Converts a handle to its underlying entity.
  16681. * @return An entity identifier.
  16682. */
  16683. [[nodiscard]] operator entity_type() const ENTT_NOEXCEPT {
  16684. return entity();
  16685. }
  16686. /**
  16687. * @brief Checks if a handle refers to non-null registry pointer and entity.
  16688. * @return True if the handle refers to non-null registry and entity, false otherwise.
  16689. */
  16690. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  16691. return reg && reg->valid(entt);
  16692. }
  16693. /**
  16694. * @brief Checks if a handle refers to a valid entity or not.
  16695. * @return True if the handle refers to a valid entity, false otherwise.
  16696. */
  16697. [[nodiscard]] bool valid() const {
  16698. return reg->valid(entt);
  16699. }
  16700. /**
  16701. * @brief Returns a pointer to the underlying registry, if any.
  16702. * @return A pointer to the underlying registry, if any.
  16703. */
  16704. [[nodiscard]] registry_type * registry() const ENTT_NOEXCEPT {
  16705. return reg;
  16706. }
  16707. /**
  16708. * @brief Returns the entity associated with a handle.
  16709. * @return The entity associated with the handle.
  16710. */
  16711. [[nodiscard]] entity_type entity() const ENTT_NOEXCEPT {
  16712. return entt;
  16713. }
  16714. /**
  16715. * @brief Destroys the entity associated with a handle.
  16716. * @sa basic_registry::destroy
  16717. */
  16718. void destroy() {
  16719. reg->destroy(entt);
  16720. }
  16721. /**
  16722. * @brief Destroys the entity associated with a handle.
  16723. * @sa basic_registry::destroy
  16724. * @param version A desired version upon destruction.
  16725. */
  16726. void destroy(const version_type version) {
  16727. reg->destroy(entt, version);
  16728. }
  16729. /**
  16730. * @brief Assigns the given component to a handle.
  16731. * @sa basic_registry::emplace
  16732. * @tparam Component Type of component to create.
  16733. * @tparam Args Types of arguments to use to construct the component.
  16734. * @param args Parameters to use to initialize the component.
  16735. * @return A reference to the newly created component.
  16736. */
  16737. template<typename Component, typename... Args>
  16738. decltype(auto) emplace(Args &&... args) const {
  16739. static_assert(((sizeof...(Type) == 0) || ... || std::is_same_v<Component, Type>), "Invalid type");
  16740. return reg->template emplace<Component>(entt, std::forward<Args>(args)...);
  16741. }
  16742. /**
  16743. * @brief Assigns or replaces the given component for a handle.
  16744. * @sa basic_registry::emplace_or_replace
  16745. * @tparam Component Type of component to assign or replace.
  16746. * @tparam Args Types of arguments to use to construct the component.
  16747. * @param args Parameters to use to initialize the component.
  16748. * @return A reference to the newly created component.
  16749. */
  16750. template<typename Component, typename... Args>
  16751. decltype(auto) emplace_or_replace(Args &&... args) const {
  16752. static_assert(((sizeof...(Type) == 0) || ... || std::is_same_v<Component, Type>), "Invalid type");
  16753. return reg->template emplace_or_replace<Component>(entt, std::forward<Args>(args)...);
  16754. }
  16755. /**
  16756. * @brief Patches the given component for a handle.
  16757. * @sa basic_registry::patch
  16758. * @tparam Component Type of component to patch.
  16759. * @tparam Func Types of the function objects to invoke.
  16760. * @param func Valid function objects.
  16761. * @return A reference to the patched component.
  16762. */
  16763. template<typename Component, typename... Func>
  16764. decltype(auto) patch(Func &&... func) const {
  16765. static_assert(((sizeof...(Type) == 0) || ... || std::is_same_v<Component, Type>), "Invalid type");
  16766. return reg->template patch<Component>(entt, std::forward<Func>(func)...);
  16767. }
  16768. /**
  16769. * @brief Replaces the given component for a handle.
  16770. * @sa basic_registry::replace
  16771. * @tparam Component Type of component to replace.
  16772. * @tparam Args Types of arguments to use to construct the component.
  16773. * @param args Parameters to use to initialize the component.
  16774. * @return A reference to the component being replaced.
  16775. */
  16776. template<typename Component, typename... Args>
  16777. decltype(auto) replace(Args &&... args) const {
  16778. static_assert(((sizeof...(Type) == 0) || ... || std::is_same_v<Component, Type>), "Invalid type");
  16779. return reg->template replace<Component>(entt, std::forward<Args>(args)...);
  16780. }
  16781. /**
  16782. * @brief Removes the given components from a handle.
  16783. * @sa basic_registry::remove
  16784. * @tparam Component Types of components to remove.
  16785. * @return The number of components actually removed.
  16786. */
  16787. template<typename... Component>
  16788. size_type remove() const {
  16789. static_assert(sizeof...(Type) == 0 || (type_list_contains_v<type_list<Type...>, Component> && ...), "Invalid type");
  16790. return reg->template remove<Component...>(entt);
  16791. }
  16792. /**
  16793. * @brief Erases the given components from a handle.
  16794. * @sa basic_registry::erase
  16795. * @tparam Component Types of components to erase.
  16796. */
  16797. template<typename... Component>
  16798. void erase() const {
  16799. static_assert(sizeof...(Type) == 0 || (type_list_contains_v<type_list<Type...>, Component> && ...), "Invalid type");
  16800. reg->template erase<Component...>(entt);
  16801. }
  16802. /*! @copydoc remove */
  16803. template<typename... Component>
  16804. [[deprecated("Use ::remove instead")]]
  16805. size_type remove_if_exists() const {
  16806. return remove<Component...>();
  16807. }
  16808. /**
  16809. * @brief Removes all the components from a handle and makes it orphaned.
  16810. * @sa basic_registry::remove_all
  16811. */
  16812. [[deprecated("No longer supported")]]
  16813. void remove_all() const {
  16814. static_assert(sizeof...(Type) == 0, "Invalid operation");
  16815. reg->remove_all(entt);
  16816. }
  16817. /**
  16818. * @brief Checks if a handle has all the given components.
  16819. * @sa basic_registry::all_of
  16820. * @tparam Component Components for which to perform the check.
  16821. * @return True if the handle has all the components, false otherwise.
  16822. */
  16823. template<typename... Component>
  16824. [[nodiscard]] decltype(auto) all_of() const {
  16825. return reg->template all_of<Component...>(entt);
  16826. }
  16827. /**
  16828. * @brief Checks if a handle has at least one of the given components.
  16829. * @sa basic_registry::any_of
  16830. * @tparam Component Components for which to perform the check.
  16831. * @return True if the handle has at least one of the given components,
  16832. * false otherwise.
  16833. */
  16834. template<typename... Component>
  16835. [[nodiscard]] decltype(auto) any_of() const {
  16836. return reg->template any_of<Component...>(entt);
  16837. }
  16838. /**
  16839. * @brief Returns references to the given components for a handle.
  16840. * @sa basic_registry::get
  16841. * @tparam Component Types of components to get.
  16842. * @return References to the components owned by the handle.
  16843. */
  16844. template<typename... Component>
  16845. [[nodiscard]] decltype(auto) get() const {
  16846. static_assert(sizeof...(Type) == 0 || (type_list_contains_v<type_list<Type...>, Component> && ...), "Invalid type");
  16847. return reg->template get<Component...>(entt);
  16848. }
  16849. /**
  16850. * @brief Returns a reference to the given component for a handle.
  16851. * @sa basic_registry::get_or_emplace
  16852. * @tparam Component Type of component to get.
  16853. * @tparam Args Types of arguments to use to construct the component.
  16854. * @param args Parameters to use to initialize the component.
  16855. * @return Reference to the component owned by the handle.
  16856. */
  16857. template<typename Component, typename... Args>
  16858. [[nodiscard]] decltype(auto) get_or_emplace(Args &&... args) const {
  16859. static_assert(((sizeof...(Type) == 0) || ... || std::is_same_v<Component, Type>), "Invalid type");
  16860. return reg->template get_or_emplace<Component>(entt, std::forward<Args>(args)...);
  16861. }
  16862. /**
  16863. * @brief Returns pointers to the given components for a handle.
  16864. * @sa basic_registry::try_get
  16865. * @tparam Component Types of components to get.
  16866. * @return Pointers to the components owned by the handle.
  16867. */
  16868. template<typename... Component>
  16869. [[nodiscard]] auto try_get() const {
  16870. static_assert(sizeof...(Type) == 0 || (type_list_contains_v<type_list<Type...>, Component> && ...), "Invalid type");
  16871. return reg->template try_get<Component...>(entt);
  16872. }
  16873. /**
  16874. * @brief Checks if a handle has components assigned.
  16875. * @return True if the handle has no components assigned, false otherwise.
  16876. */
  16877. [[nodiscard]] bool orphan() const {
  16878. return reg->orphan(entt);
  16879. }
  16880. /**
  16881. * @brief Visits a handle and returns the types for its components.
  16882. * @sa basic_registry::visit
  16883. * @tparam Func Type of the function object to invoke.
  16884. * @param func A valid function object.
  16885. */
  16886. template<typename Func>
  16887. void visit(Func &&func) const {
  16888. reg->visit(entt, std::forward<Func>(func));
  16889. }
  16890. private:
  16891. registry_type *reg;
  16892. entity_type entt;
  16893. };
  16894. /**
  16895. * @brief Compares two handles.
  16896. * @tparam Type A valid entity type (see entt_traits for more details).
  16897. * @tparam Other A valid entity type (see entt_traits for more details).
  16898. * @param lhs A valid handle.
  16899. * @param rhs A valid handle.
  16900. * @return False if both handles refer to the same registry and the same
  16901. * entity, true otherwise.
  16902. */
  16903. template<typename Type, typename Other>
  16904. bool operator!=(const basic_handle<Type> &lhs, const basic_handle<Other> &rhs) ENTT_NOEXCEPT {
  16905. return !(lhs == rhs);
  16906. }
  16907. /**
  16908. * @brief Deduction guide.
  16909. * @tparam Entity A valid entity type (see entt_traits for more details).
  16910. */
  16911. template<typename Entity>
  16912. basic_handle(basic_registry<Entity> &, Entity)
  16913. -> basic_handle<Entity>;
  16914. /**
  16915. * @brief Deduction guide.
  16916. * @tparam Entity A valid entity type (see entt_traits for more details).
  16917. */
  16918. template<typename Entity>
  16919. basic_handle(const basic_registry<Entity> &, Entity)
  16920. -> basic_handle<const Entity>;
  16921. }
  16922. #endif
  16923. // #include "entity/helper.hpp"
  16924. #ifndef ENTT_ENTITY_HELPER_HPP
  16925. #define ENTT_ENTITY_HELPER_HPP
  16926. #include <type_traits>
  16927. // #include "../config/config.h"
  16928. // #include "../core/fwd.hpp"
  16929. // #include "../core/type_traits.hpp"
  16930. // #include "../signal/delegate.hpp"
  16931. #ifndef ENTT_SIGNAL_DELEGATE_HPP
  16932. #define ENTT_SIGNAL_DELEGATE_HPP
  16933. #include <tuple>
  16934. #include <cstddef>
  16935. #include <utility>
  16936. #include <functional>
  16937. #include <type_traits>
  16938. // #include "../core/type_traits.hpp"
  16939. // #include "../config/config.h"
  16940. namespace entt {
  16941. /**
  16942. * @cond TURN_OFF_DOXYGEN
  16943. * Internal details not to be documented.
  16944. */
  16945. namespace internal {
  16946. template<typename Ret, typename... Args>
  16947. auto function_pointer(Ret(*)(Args...)) -> Ret(*)(Args...);
  16948. template<typename Ret, typename Type, typename... Args, typename Other>
  16949. auto function_pointer(Ret(*)(Type, Args...), Other &&) -> Ret(*)(Args...);
  16950. template<typename Class, typename Ret, typename... Args, typename... Other>
  16951. auto function_pointer(Ret(Class:: *)(Args...), Other &&...) -> Ret(*)(Args...);
  16952. template<typename Class, typename Ret, typename... Args, typename... Other>
  16953. auto function_pointer(Ret(Class:: *)(Args...) const, Other &&...) -> Ret(*)(Args...);
  16954. template<typename Class, typename Type, typename... Other>
  16955. auto function_pointer(Type Class:: *, Other &&...) -> Type(*)();
  16956. template<typename... Type>
  16957. using function_pointer_t = decltype(internal::function_pointer(std::declval<Type>()...));
  16958. template<typename... Class, typename Ret, typename... Args>
  16959. [[nodiscard]] constexpr auto index_sequence_for(Ret(*)(Args...)) {
  16960. return std::index_sequence_for<Class..., Args...>{};
  16961. }
  16962. }
  16963. /**
  16964. * Internal details not to be documented.
  16965. * @endcond
  16966. */
  16967. /*! @brief Used to wrap a function or a member of a specified type. */
  16968. template<auto>
  16969. struct connect_arg_t {};
  16970. /*! @brief Constant of type connect_arg_t used to disambiguate calls. */
  16971. template<auto Func>
  16972. inline constexpr connect_arg_t<Func> connect_arg{};
  16973. /**
  16974. * @brief Basic delegate implementation.
  16975. *
  16976. * Primary template isn't defined on purpose. All the specializations give a
  16977. * compile-time error unless the template parameter is a function type.
  16978. */
  16979. template<typename>
  16980. class delegate;
  16981. /**
  16982. * @brief Utility class to use to send around functions and members.
  16983. *
  16984. * Unmanaged delegate for function pointers and members. Users of this class are
  16985. * in charge of disconnecting instances before deleting them.
  16986. *
  16987. * A delegate can be used as a general purpose invoker without memory overhead
  16988. * for free functions possibly with payloads and bound or unbound members.
  16989. *
  16990. * @tparam Ret Return type of a function type.
  16991. * @tparam Args Types of arguments of a function type.
  16992. */
  16993. template<typename Ret, typename... Args>
  16994. class delegate<Ret(Args...)> {
  16995. template<auto Candidate, std::size_t... Index>
  16996. [[nodiscard]] auto wrap(std::index_sequence<Index...>) ENTT_NOEXCEPT {
  16997. return [](const void *, Args... args) -> Ret {
  16998. [[maybe_unused]] const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
  16999. return static_cast<Ret>(std::invoke(Candidate, std::forward<type_list_element_t<Index, type_list<Args...>>>(std::get<Index>(arguments))...));
  17000. };
  17001. }
  17002. template<auto Candidate, typename Type, std::size_t... Index>
  17003. [[nodiscard]] auto wrap(Type &, std::index_sequence<Index...>) ENTT_NOEXCEPT {
  17004. return [](const void *payload, Args... args) -> Ret {
  17005. [[maybe_unused]] const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
  17006. Type *curr = static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(payload));
  17007. return static_cast<Ret>(std::invoke(Candidate, *curr, std::forward<type_list_element_t<Index, type_list<Args...>>>(std::get<Index>(arguments))...));
  17008. };
  17009. }
  17010. template<auto Candidate, typename Type, std::size_t... Index>
  17011. [[nodiscard]] auto wrap(Type *, std::index_sequence<Index...>) ENTT_NOEXCEPT {
  17012. return [](const void *payload, Args... args) -> Ret {
  17013. [[maybe_unused]] const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
  17014. Type *curr = static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(payload));
  17015. return static_cast<Ret>(std::invoke(Candidate, curr, std::forward<type_list_element_t<Index, type_list<Args...>>>(std::get<Index>(arguments))...));
  17016. };
  17017. }
  17018. public:
  17019. /*! @brief Function type of the contained target. */
  17020. using function_type = Ret(const void *, Args...);
  17021. /*! @brief Function type of the delegate. */
  17022. using type = Ret(Args...);
  17023. /*! @brief Return type of the delegate. */
  17024. using result_type = Ret;
  17025. /*! @brief Default constructor. */
  17026. delegate() ENTT_NOEXCEPT
  17027. : fn{nullptr}, data{nullptr}
  17028. {}
  17029. /**
  17030. * @brief Constructs a delegate and connects a free function or an unbound
  17031. * member.
  17032. * @tparam Candidate Function or member to connect to the delegate.
  17033. */
  17034. template<auto Candidate>
  17035. delegate(connect_arg_t<Candidate>) ENTT_NOEXCEPT {
  17036. connect<Candidate>();
  17037. }
  17038. /**
  17039. * @brief Constructs a delegate and connects a free function with payload or
  17040. * a bound member.
  17041. * @tparam Candidate Function or member to connect to the delegate.
  17042. * @tparam Type Type of class or type of payload.
  17043. * @param value_or_instance A valid object that fits the purpose.
  17044. */
  17045. template<auto Candidate, typename Type>
  17046. delegate(connect_arg_t<Candidate>, Type &&value_or_instance) ENTT_NOEXCEPT {
  17047. connect<Candidate>(std::forward<Type>(value_or_instance));
  17048. }
  17049. /**
  17050. * @brief Constructs a delegate and connects an user defined function with
  17051. * optional payload.
  17052. * @param function Function to connect to the delegate.
  17053. * @param payload User defined arbitrary data.
  17054. */
  17055. delegate(function_type *function, const void *payload = nullptr) ENTT_NOEXCEPT {
  17056. connect(function, payload);
  17057. }
  17058. /**
  17059. * @brief Connects a free function or an unbound member to a delegate.
  17060. * @tparam Candidate Function or member to connect to the delegate.
  17061. */
  17062. template<auto Candidate>
  17063. void connect() ENTT_NOEXCEPT {
  17064. data = nullptr;
  17065. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Args...>) {
  17066. fn = [](const void *, Args... args) -> Ret {
  17067. return Ret(std::invoke(Candidate, std::forward<Args>(args)...));
  17068. };
  17069. } else if constexpr(std::is_member_pointer_v<decltype(Candidate)>) {
  17070. fn = wrap<Candidate>(internal::index_sequence_for<type_list_element_t<0, type_list<Args...>>>(internal::function_pointer_t<decltype(Candidate)>{}));
  17071. } else {
  17072. fn = wrap<Candidate>(internal::index_sequence_for(internal::function_pointer_t<decltype(Candidate)>{}));
  17073. }
  17074. }
  17075. /**
  17076. * @brief Connects a free function with payload or a bound member to a
  17077. * delegate.
  17078. *
  17079. * The delegate isn't responsible for the connected object or the payload.
  17080. * Users must always guarantee that the lifetime of the instance overcomes
  17081. * the one of the delegate.<br/>
  17082. * When used to connect a free function with payload, its signature must be
  17083. * such that the instance is the first argument before the ones used to
  17084. * define the delegate itself.
  17085. *
  17086. * @tparam Candidate Function or member to connect to the delegate.
  17087. * @tparam Type Type of class or type of payload.
  17088. * @param value_or_instance A valid reference that fits the purpose.
  17089. */
  17090. template<auto Candidate, typename Type>
  17091. void connect(Type &value_or_instance) ENTT_NOEXCEPT {
  17092. data = &value_or_instance;
  17093. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Type &, Args...>) {
  17094. fn = [](const void *payload, Args... args) -> Ret {
  17095. Type *curr = static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(payload));
  17096. return Ret(std::invoke(Candidate, *curr, std::forward<Args>(args)...));
  17097. };
  17098. } else {
  17099. fn = wrap<Candidate>(value_or_instance, internal::index_sequence_for(internal::function_pointer_t<decltype(Candidate), Type>{}));
  17100. }
  17101. }
  17102. /**
  17103. * @brief Connects a free function with payload or a bound member to a
  17104. * delegate.
  17105. *
  17106. * @sa connect(Type &)
  17107. *
  17108. * @tparam Candidate Function or member to connect to the delegate.
  17109. * @tparam Type Type of class or type of payload.
  17110. * @param value_or_instance A valid pointer that fits the purpose.
  17111. */
  17112. template<auto Candidate, typename Type>
  17113. void connect(Type *value_or_instance) ENTT_NOEXCEPT {
  17114. data = value_or_instance;
  17115. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Type *, Args...>) {
  17116. fn = [](const void *payload, Args... args) -> Ret {
  17117. Type *curr = static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(payload));
  17118. return Ret(std::invoke(Candidate, curr, std::forward<Args>(args)...));
  17119. };
  17120. } else {
  17121. fn = wrap<Candidate>(value_or_instance, internal::index_sequence_for(internal::function_pointer_t<decltype(Candidate), Type>{}));
  17122. }
  17123. }
  17124. /**
  17125. * @brief Connects an user defined function with optional payload to a
  17126. * delegate.
  17127. *
  17128. * The delegate isn't responsible for the connected object or the payload.
  17129. * Users must always guarantee that the lifetime of an instance overcomes
  17130. * the one of the delegate.<br/>
  17131. * The payload is returned as the first argument to the target function in
  17132. * all cases.
  17133. *
  17134. * @param function Function to connect to the delegate.
  17135. * @param payload User defined arbitrary data.
  17136. */
  17137. void connect(function_type *function, const void *payload = nullptr) ENTT_NOEXCEPT {
  17138. fn = function;
  17139. data = payload;
  17140. }
  17141. /**
  17142. * @brief Resets a delegate.
  17143. *
  17144. * After a reset, a delegate cannot be invoked anymore.
  17145. */
  17146. void reset() ENTT_NOEXCEPT {
  17147. fn = nullptr;
  17148. data = nullptr;
  17149. }
  17150. /**
  17151. * @brief Returns the instance or the payload linked to a delegate, if any.
  17152. * @return An opaque pointer to the underlying data.
  17153. */
  17154. [[nodiscard]] const void * instance() const ENTT_NOEXCEPT {
  17155. return data;
  17156. }
  17157. /**
  17158. * @brief Triggers a delegate.
  17159. *
  17160. * The delegate invokes the underlying function and returns the result.
  17161. *
  17162. * @warning
  17163. * Attempting to trigger an invalid delegate results in undefined
  17164. * behavior.
  17165. *
  17166. * @param args Arguments to use to invoke the underlying function.
  17167. * @return The value returned by the underlying function.
  17168. */
  17169. Ret operator()(Args... args) const {
  17170. ENTT_ASSERT(static_cast<bool>(*this), "Uninitialized delegate");
  17171. return fn(data, std::forward<Args>(args)...);
  17172. }
  17173. /**
  17174. * @brief Checks whether a delegate actually stores a listener.
  17175. * @return False if the delegate is empty, true otherwise.
  17176. */
  17177. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  17178. // no need to test also data
  17179. return !(fn == nullptr);
  17180. }
  17181. /**
  17182. * @brief Compares the contents of two delegates.
  17183. * @param other Delegate with which to compare.
  17184. * @return False if the two contents differ, true otherwise.
  17185. */
  17186. [[nodiscard]] bool operator==(const delegate<Ret(Args...)> &other) const ENTT_NOEXCEPT {
  17187. return fn == other.fn && data == other.data;
  17188. }
  17189. private:
  17190. function_type *fn;
  17191. const void *data;
  17192. };
  17193. /**
  17194. * @brief Compares the contents of two delegates.
  17195. * @tparam Ret Return type of a function type.
  17196. * @tparam Args Types of arguments of a function type.
  17197. * @param lhs A valid delegate object.
  17198. * @param rhs A valid delegate object.
  17199. * @return True if the two contents differ, false otherwise.
  17200. */
  17201. template<typename Ret, typename... Args>
  17202. [[nodiscard]] bool operator!=(const delegate<Ret(Args...)> &lhs, const delegate<Ret(Args...)> &rhs) ENTT_NOEXCEPT {
  17203. return !(lhs == rhs);
  17204. }
  17205. /**
  17206. * @brief Deduction guide.
  17207. * @tparam Candidate Function or member to connect to the delegate.
  17208. */
  17209. template<auto Candidate>
  17210. delegate(connect_arg_t<Candidate>)
  17211. -> delegate<std::remove_pointer_t<internal::function_pointer_t<decltype(Candidate)>>>;
  17212. /**
  17213. * @brief Deduction guide.
  17214. * @tparam Candidate Function or member to connect to the delegate.
  17215. * @tparam Type Type of class or type of payload.
  17216. */
  17217. template<auto Candidate, typename Type>
  17218. delegate(connect_arg_t<Candidate>, Type &&)
  17219. -> delegate<std::remove_pointer_t<internal::function_pointer_t<decltype(Candidate), Type>>>;
  17220. /**
  17221. * @brief Deduction guide.
  17222. * @tparam Ret Return type of a function type.
  17223. * @tparam Args Types of arguments of a function type.
  17224. */
  17225. template<typename Ret, typename... Args>
  17226. delegate(Ret(*)(const void *, Args...), const void * = nullptr)
  17227. -> delegate<Ret(Args...)>;
  17228. }
  17229. #endif
  17230. // #include "registry.hpp"
  17231. // #include "fwd.hpp"
  17232. namespace entt {
  17233. /**
  17234. * @brief Converts a registry to a view.
  17235. * @tparam Entity A valid entity type (see entt_traits for more details).
  17236. */
  17237. template<typename Entity>
  17238. struct as_view {
  17239. /*! @brief Underlying entity identifier. */
  17240. using entity_type = std::remove_const_t<Entity>;
  17241. /*! @brief Type of registry to convert. */
  17242. using registry_type = constness_as_t<basic_registry<entity_type>, Entity>;
  17243. /**
  17244. * @brief Constructs a converter for a given registry.
  17245. * @param source A valid reference to a registry.
  17246. */
  17247. as_view(registry_type &source) ENTT_NOEXCEPT: reg{source} {}
  17248. /**
  17249. * @brief Conversion function from a registry to a view.
  17250. * @tparam Exclude Types of components used to filter the view.
  17251. * @tparam Component Type of components used to construct the view.
  17252. * @return A newly created view.
  17253. */
  17254. template<typename Exclude, typename... Component>
  17255. operator basic_view<entity_type, Exclude, Component...>() const {
  17256. return reg.template view<Component...>(Exclude{});
  17257. }
  17258. private:
  17259. registry_type &reg;
  17260. };
  17261. /**
  17262. * @brief Deduction guide.
  17263. * @tparam Entity A valid entity type (see entt_traits for more details).
  17264. */
  17265. template<typename Entity>
  17266. as_view(basic_registry<Entity> &) -> as_view<Entity>;
  17267. /**
  17268. * @brief Deduction guide.
  17269. * @tparam Entity A valid entity type (see entt_traits for more details).
  17270. */
  17271. template<typename Entity>
  17272. as_view(const basic_registry<Entity> &) -> as_view<const Entity>;
  17273. /**
  17274. * @brief Converts a registry to a group.
  17275. * @tparam Entity A valid entity type (see entt_traits for more details).
  17276. */
  17277. template<typename Entity>
  17278. struct as_group {
  17279. /*! @brief Underlying entity identifier. */
  17280. using entity_type = std::remove_const_t<Entity>;
  17281. /*! @brief Type of registry to convert. */
  17282. using registry_type = constness_as_t<basic_registry<entity_type>, Entity>;
  17283. /**
  17284. * @brief Constructs a converter for a given registry.
  17285. * @param source A valid reference to a registry.
  17286. */
  17287. as_group(registry_type &source) ENTT_NOEXCEPT: reg{source} {}
  17288. /**
  17289. * @brief Conversion function from a registry to a group.
  17290. * @tparam Exclude Types of components used to filter the group.
  17291. * @tparam Get Types of components observed by the group.
  17292. * @tparam Owned Types of components owned by the group.
  17293. * @return A newly created group.
  17294. */
  17295. template<typename Exclude, typename Get, typename... Owned>
  17296. operator basic_group<entity_type, Exclude, Get, Owned...>() const {
  17297. if constexpr(std::is_const_v<registry_type>) {
  17298. return reg.template group_if_exists<Owned...>(Get{}, Exclude{});
  17299. } else {
  17300. return reg.template group<Owned...>(Get{}, Exclude{});
  17301. }
  17302. }
  17303. private:
  17304. registry_type &reg;
  17305. };
  17306. /**
  17307. * @brief Deduction guide.
  17308. * @tparam Entity A valid entity type (see entt_traits for more details).
  17309. */
  17310. template<typename Entity>
  17311. as_group(basic_registry<Entity> &) -> as_group<Entity>;
  17312. /**
  17313. * @brief Deduction guide.
  17314. * @tparam Entity A valid entity type (see entt_traits for more details).
  17315. */
  17316. template<typename Entity>
  17317. as_group(const basic_registry<Entity> &) -> as_group<const Entity>;
  17318. /**
  17319. * @brief Helper to create a listener that directly invokes a member function.
  17320. * @tparam Member Member function to invoke on a component of the given type.
  17321. * @tparam Entity A valid entity type (see entt_traits for more details).
  17322. * @param reg A registry that contains the given entity and its components.
  17323. * @param entt Entity from which to get the component.
  17324. */
  17325. template<auto Member, typename Entity = entity>
  17326. void invoke(basic_registry<Entity> &reg, const Entity entt) {
  17327. static_assert(std::is_member_function_pointer_v<decltype(Member)>, "Invalid pointer to non-static member function");
  17328. delegate<void(basic_registry<Entity> &, const Entity)> func;
  17329. func.template connect<Member>(reg.template get<member_class_t<decltype(Member)>>(entt));
  17330. func(reg, entt);
  17331. }
  17332. /**
  17333. * @brief Returns the entity associated with a given component.
  17334. *
  17335. * @warning
  17336. * Currently, this function only works correctly with the default pool as it
  17337. * makes assumptions about how the components are laid out.
  17338. *
  17339. * @tparam Entity A valid entity type (see entt_traits for more details).
  17340. * @tparam Component Type of component.
  17341. * @param reg A registry that contains the given entity and its components.
  17342. * @param instance A valid component instance.
  17343. * @return The entity associated with the given component.
  17344. */
  17345. template<typename Entity, typename Component>
  17346. Entity to_entity(const basic_registry<Entity> &reg, const Component &instance) {
  17347. const auto view = reg.template view<const Component>();
  17348. const auto *addr = std::addressof(instance);
  17349. for(auto it = view.rbegin(), last = view.rend(); it < last; it += ENTT_PACKED_PAGE) {
  17350. if(const auto dist = (addr - std::addressof(view.template get<const Component>(*it))); dist >= 0 && dist < ENTT_PACKED_PAGE) {
  17351. return *(it + dist);
  17352. }
  17353. }
  17354. return entt::null;
  17355. }
  17356. }
  17357. #endif
  17358. // #include "entity/observer.hpp"
  17359. #ifndef ENTT_ENTITY_OBSERVER_HPP
  17360. #define ENTT_ENTITY_OBSERVER_HPP
  17361. #include <limits>
  17362. #include <cstddef>
  17363. #include <cstdint>
  17364. #include <utility>
  17365. #include <type_traits>
  17366. // #include "../config/config.h"
  17367. // #include "../core/type_traits.hpp"
  17368. // #include "../signal/delegate.hpp"
  17369. // #include "registry.hpp"
  17370. // #include "storage.hpp"
  17371. // #include "utility.hpp"
  17372. // #include "entity.hpp"
  17373. // #include "fwd.hpp"
  17374. namespace entt {
  17375. /*! @brief Grouping matcher. */
  17376. template<typename...>
  17377. struct matcher {};
  17378. /**
  17379. * @brief Collector.
  17380. *
  17381. * Primary template isn't defined on purpose. All the specializations give a
  17382. * compile-time error, but for a few reasonable cases.
  17383. */
  17384. template<typename...>
  17385. struct basic_collector;
  17386. /**
  17387. * @brief Collector.
  17388. *
  17389. * A collector contains a set of rules (literally, matchers) to use to track
  17390. * entities.<br/>
  17391. * Its main purpose is to generate a descriptor that allows an observer to know
  17392. * how to connect to a registry.
  17393. */
  17394. template<>
  17395. struct basic_collector<> {
  17396. /**
  17397. * @brief Adds a grouping matcher to the collector.
  17398. * @tparam AllOf Types of components tracked by the matcher.
  17399. * @tparam NoneOf Types of components used to filter out entities.
  17400. * @return The updated collector.
  17401. */
  17402. template<typename... AllOf, typename... NoneOf>
  17403. static constexpr auto group(exclude_t<NoneOf...> = {}) ENTT_NOEXCEPT {
  17404. return basic_collector<matcher<type_list<>, type_list<>, type_list<NoneOf...>, AllOf...>>{};
  17405. }
  17406. /**
  17407. * @brief Adds an observing matcher to the collector.
  17408. * @tparam AnyOf Type of component for which changes should be detected.
  17409. * @return The updated collector.
  17410. */
  17411. template<typename AnyOf>
  17412. static constexpr auto update() ENTT_NOEXCEPT {
  17413. return basic_collector<matcher<type_list<>, type_list<>, AnyOf>>{};
  17414. }
  17415. };
  17416. /**
  17417. * @brief Collector.
  17418. * @copydetails basic_collector<>
  17419. * @tparam Reject Untracked types used to filter out entities.
  17420. * @tparam Require Untracked types required by the matcher.
  17421. * @tparam Rule Specific details of the current matcher.
  17422. * @tparam Other Other matchers.
  17423. */
  17424. template<typename... Reject, typename... Require, typename... Rule, typename... Other>
  17425. struct basic_collector<matcher<type_list<Reject...>, type_list<Require...>, Rule...>, Other...> {
  17426. /*! @brief Current matcher. */
  17427. using current_type = matcher<type_list<Reject...>, type_list<Require...>, Rule...>;
  17428. /**
  17429. * @brief Adds a grouping matcher to the collector.
  17430. * @tparam AllOf Types of components tracked by the matcher.
  17431. * @tparam NoneOf Types of components used to filter out entities.
  17432. * @return The updated collector.
  17433. */
  17434. template<typename... AllOf, typename... NoneOf>
  17435. static constexpr auto group(exclude_t<NoneOf...> = {}) ENTT_NOEXCEPT {
  17436. return basic_collector<matcher<type_list<>, type_list<>, type_list<NoneOf...>, AllOf...>, current_type, Other...>{};
  17437. }
  17438. /**
  17439. * @brief Adds an observing matcher to the collector.
  17440. * @tparam AnyOf Type of component for which changes should be detected.
  17441. * @return The updated collector.
  17442. */
  17443. template<typename AnyOf>
  17444. static constexpr auto update() ENTT_NOEXCEPT {
  17445. return basic_collector<matcher<type_list<>, type_list<>, AnyOf>, current_type, Other...>{};
  17446. }
  17447. /**
  17448. * @brief Updates the filter of the last added matcher.
  17449. * @tparam AllOf Types of components required by the matcher.
  17450. * @tparam NoneOf Types of components used to filter out entities.
  17451. * @return The updated collector.
  17452. */
  17453. template<typename... AllOf, typename... NoneOf>
  17454. static constexpr auto where(exclude_t<NoneOf...> = {}) ENTT_NOEXCEPT {
  17455. using extended_type = matcher<type_list<Reject..., NoneOf...>, type_list<Require..., AllOf...>, Rule...>;
  17456. return basic_collector<extended_type, Other...>{};
  17457. }
  17458. };
  17459. /*! @brief Variable template used to ease the definition of collectors. */
  17460. inline constexpr basic_collector<> collector{};
  17461. /**
  17462. * @brief Observer.
  17463. *
  17464. * An observer returns all the entities and only the entities that fit the
  17465. * requirements of at least one matcher. Moreover, it's guaranteed that the
  17466. * entity list is tightly packed in memory for fast iterations.<br/>
  17467. * In general, observers don't stay true to the order of any set of components.
  17468. *
  17469. * Observers work mainly with two types of matchers, provided through a
  17470. * collector:
  17471. *
  17472. * * Observing matcher: an observer will return at least all the living entities
  17473. * for which one or more of the given components have been updated and not yet
  17474. * destroyed.
  17475. * * Grouping matcher: an observer will return at least all the living entities
  17476. * that would have entered the given group if it existed and that would have
  17477. * not yet left it.
  17478. *
  17479. * If an entity respects the requirements of multiple matchers, it will be
  17480. * returned once and only once by the observer in any case.
  17481. *
  17482. * Matchers support also filtering by means of a _where_ clause that accepts
  17483. * both a list of types and an exclusion list.<br/>
  17484. * Whenever a matcher finds that an entity matches its requirements, the
  17485. * condition of the filter is verified before to register the entity itself.
  17486. * Moreover, a registered entity isn't returned by the observer if the condition
  17487. * set by the filter is broken in the meantime.
  17488. *
  17489. * @b Important
  17490. *
  17491. * Iterators aren't invalidated if:
  17492. *
  17493. * * New instances of the given components are created and assigned to entities.
  17494. * * The entity currently pointed is modified (as an example, if one of the
  17495. * given components is removed from the entity to which the iterator points).
  17496. * * The entity currently pointed is destroyed.
  17497. *
  17498. * In all the other cases, modifying the pools of the given components in any
  17499. * way invalidates all the iterators and using them results in undefined
  17500. * behavior.
  17501. *
  17502. * @warning
  17503. * Lifetime of an observer doesn't necessarily have to overcome that of the
  17504. * registry to which it is connected. However, the observer must be disconnected
  17505. * from the registry before being destroyed to avoid crashes due to dangling
  17506. * pointers.
  17507. *
  17508. * @tparam Entity A valid entity type (see entt_traits for more details).
  17509. */
  17510. template<typename Entity>
  17511. class basic_observer {
  17512. using payload_type = std::uint32_t;
  17513. template<typename>
  17514. struct matcher_handler;
  17515. template<typename... Reject, typename... Require, typename AnyOf>
  17516. struct matcher_handler<matcher<type_list<Reject...>, type_list<Require...>, AnyOf>> {
  17517. template<std::size_t Index>
  17518. static void maybe_valid_if(basic_observer &obs, basic_registry<Entity> &reg, const Entity entt) {
  17519. if(reg.template all_of<Require...>(entt) && !reg.template any_of<Reject...>(entt)) {
  17520. if(!obs.storage.contains(entt)) {
  17521. obs.storage.emplace(entt);
  17522. }
  17523. obs.storage.get(entt) |= (1 << Index);
  17524. }
  17525. }
  17526. template<std::size_t Index>
  17527. static void discard_if(basic_observer &obs, basic_registry<Entity> &, const Entity entt) {
  17528. if(obs.storage.contains(entt) && !(obs.storage.get(entt) &= (~(1 << Index)))) {
  17529. obs.storage.erase(entt);
  17530. }
  17531. }
  17532. template<std::size_t Index>
  17533. static void connect(basic_observer &obs, basic_registry<Entity> &reg) {
  17534. (reg.template on_destroy<Require>().template connect<&discard_if<Index>>(obs), ...);
  17535. (reg.template on_construct<Reject>().template connect<&discard_if<Index>>(obs), ...);
  17536. reg.template on_update<AnyOf>().template connect<&maybe_valid_if<Index>>(obs);
  17537. reg.template on_destroy<AnyOf>().template connect<&discard_if<Index>>(obs);
  17538. }
  17539. static void disconnect(basic_observer &obs, basic_registry<Entity> &reg) {
  17540. (reg.template on_destroy<Require>().disconnect(obs), ...);
  17541. (reg.template on_construct<Reject>().disconnect(obs), ...);
  17542. reg.template on_update<AnyOf>().disconnect(obs);
  17543. reg.template on_destroy<AnyOf>().disconnect(obs);
  17544. }
  17545. };
  17546. template<typename... Reject, typename... Require, typename... NoneOf, typename... AllOf>
  17547. struct matcher_handler<matcher<type_list<Reject...>, type_list<Require...>, type_list<NoneOf...>, AllOf...>> {
  17548. template<std::size_t Index, typename... Ignore>
  17549. static void maybe_valid_if(basic_observer &obs, basic_registry<Entity> &reg, const Entity entt) {
  17550. if([&reg, entt]() {
  17551. if constexpr(sizeof...(Ignore) == 0) {
  17552. return reg.template all_of<AllOf..., Require...>(entt) && !reg.template any_of<NoneOf..., Reject...>(entt);
  17553. } else {
  17554. return reg.template all_of<AllOf..., Require...>(entt) && ((std::is_same_v<Ignore..., NoneOf> || !reg.template any_of<NoneOf>(entt)) && ...) && !reg.template any_of<Reject...>(entt);
  17555. }
  17556. }())
  17557. {
  17558. if(!obs.storage.contains(entt)) {
  17559. obs.storage.emplace(entt);
  17560. }
  17561. obs.storage.get(entt) |= (1 << Index);
  17562. }
  17563. }
  17564. template<std::size_t Index>
  17565. static void discard_if(basic_observer &obs, basic_registry<Entity> &, const Entity entt) {
  17566. if(obs.storage.contains(entt) && !(obs.storage.get(entt) &= (~(1 << Index)))) {
  17567. obs.storage.erase(entt);
  17568. }
  17569. }
  17570. template<std::size_t Index>
  17571. static void connect(basic_observer &obs, basic_registry<Entity> &reg) {
  17572. (reg.template on_destroy<Require>().template connect<&discard_if<Index>>(obs), ...);
  17573. (reg.template on_construct<Reject>().template connect<&discard_if<Index>>(obs), ...);
  17574. (reg.template on_construct<AllOf>().template connect<&maybe_valid_if<Index>>(obs), ...);
  17575. (reg.template on_destroy<NoneOf>().template connect<&maybe_valid_if<Index, NoneOf>>(obs), ...);
  17576. (reg.template on_destroy<AllOf>().template connect<&discard_if<Index>>(obs), ...);
  17577. (reg.template on_construct<NoneOf>().template connect<&discard_if<Index>>(obs), ...);
  17578. }
  17579. static void disconnect(basic_observer &obs, basic_registry<Entity> &reg) {
  17580. (reg.template on_destroy<Require>().disconnect(obs), ...);
  17581. (reg.template on_construct<Reject>().disconnect(obs), ...);
  17582. (reg.template on_construct<AllOf>().disconnect(obs), ...);
  17583. (reg.template on_destroy<NoneOf>().disconnect(obs), ...);
  17584. (reg.template on_destroy<AllOf>().disconnect(obs), ...);
  17585. (reg.template on_construct<NoneOf>().disconnect(obs), ...);
  17586. }
  17587. };
  17588. template<typename... Matcher>
  17589. static void disconnect(basic_registry<Entity> &reg, basic_observer &obs) {
  17590. (matcher_handler<Matcher>::disconnect(obs, reg), ...);
  17591. }
  17592. template<typename... Matcher, std::size_t... Index>
  17593. void connect(basic_registry<Entity> &reg, std::index_sequence<Index...>) {
  17594. static_assert(sizeof...(Matcher) < std::numeric_limits<payload_type>::digits, "Too many matchers");
  17595. (matcher_handler<Matcher>::template connect<Index>(*this, reg), ...);
  17596. release.template connect<&basic_observer::disconnect<Matcher...>>(reg);
  17597. }
  17598. public:
  17599. /*! @brief Underlying entity identifier. */
  17600. using entity_type = Entity;
  17601. /*! @brief Unsigned integer type. */
  17602. using size_type = std::size_t;
  17603. /*! @brief Random access iterator type. */
  17604. using iterator = typename basic_sparse_set<Entity>::iterator;
  17605. /*! @brief Default constructor. */
  17606. basic_observer()
  17607. : release{},
  17608. storage{}
  17609. {}
  17610. /*! @brief Default copy constructor, deleted on purpose. */
  17611. basic_observer(const basic_observer &) = delete;
  17612. /*! @brief Default move constructor, deleted on purpose. */
  17613. basic_observer(basic_observer &&) = delete;
  17614. /**
  17615. * @brief Creates an observer and connects it to a given registry.
  17616. * @tparam Matcher Types of matchers to use to initialize the observer.
  17617. * @param reg A valid reference to a registry.
  17618. */
  17619. template<typename... Matcher>
  17620. basic_observer(basic_registry<entity_type> &reg, basic_collector<Matcher...>)
  17621. : basic_observer{}
  17622. {
  17623. connect<Matcher...>(reg, std::index_sequence_for<Matcher...>{});
  17624. }
  17625. /*! @brief Default destructor. */
  17626. ~basic_observer() = default;
  17627. /**
  17628. * @brief Default copy assignment operator, deleted on purpose.
  17629. * @return This observer.
  17630. */
  17631. basic_observer & operator=(const basic_observer &) = delete;
  17632. /**
  17633. * @brief Default move assignment operator, deleted on purpose.
  17634. * @return This observer.
  17635. */
  17636. basic_observer & operator=(basic_observer &&) = delete;
  17637. /**
  17638. * @brief Connects an observer to a given registry.
  17639. * @tparam Matcher Types of matchers to use to initialize the observer.
  17640. * @param reg A valid reference to a registry.
  17641. */
  17642. template<typename... Matcher>
  17643. void connect(basic_registry<entity_type> &reg, basic_collector<Matcher...>) {
  17644. disconnect();
  17645. connect<Matcher...>(reg, std::index_sequence_for<Matcher...>{});
  17646. storage.clear();
  17647. }
  17648. /*! @brief Disconnects an observer from the registry it keeps track of. */
  17649. void disconnect() {
  17650. if(release) {
  17651. release(*this);
  17652. release.reset();
  17653. }
  17654. }
  17655. /**
  17656. * @brief Returns the number of elements in an observer.
  17657. * @return Number of elements.
  17658. */
  17659. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  17660. return storage.size();
  17661. }
  17662. /**
  17663. * @brief Checks whether an observer is empty.
  17664. * @return True if the observer is empty, false otherwise.
  17665. */
  17666. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  17667. return storage.empty();
  17668. }
  17669. /**
  17670. * @brief Direct access to the list of entities of the observer.
  17671. *
  17672. * The returned pointer is such that range `[data(), data() + size())` is
  17673. * always a valid range, even if the container is empty.
  17674. *
  17675. * @note
  17676. * Entities are in the reverse order as returned by the `begin`/`end`
  17677. * iterators.
  17678. *
  17679. * @return A pointer to the array of entities.
  17680. */
  17681. [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT {
  17682. return storage.data();
  17683. }
  17684. /**
  17685. * @brief Returns an iterator to the first entity of the observer.
  17686. *
  17687. * The returned iterator points to the first entity of the observer. If the
  17688. * container is empty, the returned iterator will be equal to `end()`.
  17689. *
  17690. * @return An iterator to the first entity of the observer.
  17691. */
  17692. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  17693. return storage.basic_sparse_set<entity_type>::begin();
  17694. }
  17695. /**
  17696. * @brief Returns an iterator that is past the last entity of the observer.
  17697. *
  17698. * The returned iterator points to the entity following the last entity of
  17699. * the observer. Attempting to dereference the returned iterator results in
  17700. * undefined behavior.
  17701. *
  17702. * @return An iterator to the entity following the last entity of the
  17703. * observer.
  17704. */
  17705. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  17706. return storage.basic_sparse_set<entity_type>::end();
  17707. }
  17708. /*! @brief Clears the underlying container. */
  17709. void clear() ENTT_NOEXCEPT {
  17710. storage.clear();
  17711. }
  17712. /**
  17713. * @brief Iterates entities and applies the given function object to them.
  17714. *
  17715. * The function object is invoked for each entity.<br/>
  17716. * The signature of the function must be equivalent to the following form:
  17717. *
  17718. * @code{.cpp}
  17719. * void(const entity_type);
  17720. * @endcode
  17721. *
  17722. * @tparam Func Type of the function object to invoke.
  17723. * @param func A valid function object.
  17724. */
  17725. template<typename Func>
  17726. void each(Func func) const {
  17727. for(const auto entity: *this) {
  17728. func(entity);
  17729. }
  17730. }
  17731. /**
  17732. * @brief Iterates entities and applies the given function object to them,
  17733. * then clears the observer.
  17734. *
  17735. * @sa each
  17736. *
  17737. * @tparam Func Type of the function object to invoke.
  17738. * @param func A valid function object.
  17739. */
  17740. template<typename Func>
  17741. void each(Func func) {
  17742. std::as_const(*this).each(std::move(func));
  17743. clear();
  17744. }
  17745. private:
  17746. delegate<void(basic_observer &)> release;
  17747. basic_storage<entity_type, payload_type> storage;
  17748. };
  17749. }
  17750. #endif
  17751. // #include "entity/organizer.hpp"
  17752. #ifndef ENTT_ENTITY_ORGANIZER_HPP
  17753. #define ENTT_ENTITY_ORGANIZER_HPP
  17754. #include <cstddef>
  17755. #include <algorithm>
  17756. #include <type_traits>
  17757. #include <unordered_map>
  17758. #include <utility>
  17759. #include <vector>
  17760. // #include "../core/type_info.hpp"
  17761. // #include "../core/type_traits.hpp"
  17762. // #include "fwd.hpp"
  17763. // #include "helper.hpp"
  17764. #ifndef ENTT_ENTITY_HELPER_HPP
  17765. #define ENTT_ENTITY_HELPER_HPP
  17766. #include <type_traits>
  17767. // #include "../config/config.h"
  17768. // #include "../core/fwd.hpp"
  17769. // #include "../core/type_traits.hpp"
  17770. // #include "../signal/delegate.hpp"
  17771. // #include "registry.hpp"
  17772. // #include "fwd.hpp"
  17773. namespace entt {
  17774. /**
  17775. * @brief Converts a registry to a view.
  17776. * @tparam Entity A valid entity type (see entt_traits for more details).
  17777. */
  17778. template<typename Entity>
  17779. struct as_view {
  17780. /*! @brief Underlying entity identifier. */
  17781. using entity_type = std::remove_const_t<Entity>;
  17782. /*! @brief Type of registry to convert. */
  17783. using registry_type = constness_as_t<basic_registry<entity_type>, Entity>;
  17784. /**
  17785. * @brief Constructs a converter for a given registry.
  17786. * @param source A valid reference to a registry.
  17787. */
  17788. as_view(registry_type &source) ENTT_NOEXCEPT: reg{source} {}
  17789. /**
  17790. * @brief Conversion function from a registry to a view.
  17791. * @tparam Exclude Types of components used to filter the view.
  17792. * @tparam Component Type of components used to construct the view.
  17793. * @return A newly created view.
  17794. */
  17795. template<typename Exclude, typename... Component>
  17796. operator basic_view<entity_type, Exclude, Component...>() const {
  17797. return reg.template view<Component...>(Exclude{});
  17798. }
  17799. private:
  17800. registry_type &reg;
  17801. };
  17802. /**
  17803. * @brief Deduction guide.
  17804. * @tparam Entity A valid entity type (see entt_traits for more details).
  17805. */
  17806. template<typename Entity>
  17807. as_view(basic_registry<Entity> &) -> as_view<Entity>;
  17808. /**
  17809. * @brief Deduction guide.
  17810. * @tparam Entity A valid entity type (see entt_traits for more details).
  17811. */
  17812. template<typename Entity>
  17813. as_view(const basic_registry<Entity> &) -> as_view<const Entity>;
  17814. /**
  17815. * @brief Converts a registry to a group.
  17816. * @tparam Entity A valid entity type (see entt_traits for more details).
  17817. */
  17818. template<typename Entity>
  17819. struct as_group {
  17820. /*! @brief Underlying entity identifier. */
  17821. using entity_type = std::remove_const_t<Entity>;
  17822. /*! @brief Type of registry to convert. */
  17823. using registry_type = constness_as_t<basic_registry<entity_type>, Entity>;
  17824. /**
  17825. * @brief Constructs a converter for a given registry.
  17826. * @param source A valid reference to a registry.
  17827. */
  17828. as_group(registry_type &source) ENTT_NOEXCEPT: reg{source} {}
  17829. /**
  17830. * @brief Conversion function from a registry to a group.
  17831. * @tparam Exclude Types of components used to filter the group.
  17832. * @tparam Get Types of components observed by the group.
  17833. * @tparam Owned Types of components owned by the group.
  17834. * @return A newly created group.
  17835. */
  17836. template<typename Exclude, typename Get, typename... Owned>
  17837. operator basic_group<entity_type, Exclude, Get, Owned...>() const {
  17838. if constexpr(std::is_const_v<registry_type>) {
  17839. return reg.template group_if_exists<Owned...>(Get{}, Exclude{});
  17840. } else {
  17841. return reg.template group<Owned...>(Get{}, Exclude{});
  17842. }
  17843. }
  17844. private:
  17845. registry_type &reg;
  17846. };
  17847. /**
  17848. * @brief Deduction guide.
  17849. * @tparam Entity A valid entity type (see entt_traits for more details).
  17850. */
  17851. template<typename Entity>
  17852. as_group(basic_registry<Entity> &) -> as_group<Entity>;
  17853. /**
  17854. * @brief Deduction guide.
  17855. * @tparam Entity A valid entity type (see entt_traits for more details).
  17856. */
  17857. template<typename Entity>
  17858. as_group(const basic_registry<Entity> &) -> as_group<const Entity>;
  17859. /**
  17860. * @brief Helper to create a listener that directly invokes a member function.
  17861. * @tparam Member Member function to invoke on a component of the given type.
  17862. * @tparam Entity A valid entity type (see entt_traits for more details).
  17863. * @param reg A registry that contains the given entity and its components.
  17864. * @param entt Entity from which to get the component.
  17865. */
  17866. template<auto Member, typename Entity = entity>
  17867. void invoke(basic_registry<Entity> &reg, const Entity entt) {
  17868. static_assert(std::is_member_function_pointer_v<decltype(Member)>, "Invalid pointer to non-static member function");
  17869. delegate<void(basic_registry<Entity> &, const Entity)> func;
  17870. func.template connect<Member>(reg.template get<member_class_t<decltype(Member)>>(entt));
  17871. func(reg, entt);
  17872. }
  17873. /**
  17874. * @brief Returns the entity associated with a given component.
  17875. *
  17876. * @warning
  17877. * Currently, this function only works correctly with the default pool as it
  17878. * makes assumptions about how the components are laid out.
  17879. *
  17880. * @tparam Entity A valid entity type (see entt_traits for more details).
  17881. * @tparam Component Type of component.
  17882. * @param reg A registry that contains the given entity and its components.
  17883. * @param instance A valid component instance.
  17884. * @return The entity associated with the given component.
  17885. */
  17886. template<typename Entity, typename Component>
  17887. Entity to_entity(const basic_registry<Entity> &reg, const Component &instance) {
  17888. const auto view = reg.template view<const Component>();
  17889. const auto *addr = std::addressof(instance);
  17890. for(auto it = view.rbegin(), last = view.rend(); it < last; it += ENTT_PACKED_PAGE) {
  17891. if(const auto dist = (addr - std::addressof(view.template get<const Component>(*it))); dist >= 0 && dist < ENTT_PACKED_PAGE) {
  17892. return *(it + dist);
  17893. }
  17894. }
  17895. return entt::null;
  17896. }
  17897. }
  17898. #endif
  17899. namespace entt {
  17900. /**
  17901. * @cond TURN_OFF_DOXYGEN
  17902. * Internal details not to be documented.
  17903. */
  17904. namespace internal {
  17905. template<typename>
  17906. struct is_view: std::false_type {};
  17907. template<typename Entity, typename... Exclude, typename... Component>
  17908. struct is_view<basic_view<Entity, exclude_t<Exclude...>, Component...>>: std::true_type {};
  17909. template<typename Type>
  17910. inline constexpr bool is_view_v = is_view<Type>::value;
  17911. template<typename Type, typename Override>
  17912. struct unpack_type {
  17913. using ro = std::conditional_t<
  17914. type_list_contains_v<Override, std::add_const_t<Type>> || (std::is_const_v<Type> && !type_list_contains_v<Override, std::remove_const_t<Type>>),
  17915. type_list<std::remove_const_t<Type>>,
  17916. type_list<>
  17917. >;
  17918. using rw = std::conditional_t<
  17919. type_list_contains_v<Override, std::remove_const_t<Type>> || (!std::is_const_v<Type> && !type_list_contains_v<Override, std::add_const_t<Type>>),
  17920. type_list<Type>,
  17921. type_list<>
  17922. >;
  17923. };
  17924. template<typename Entity, typename... Override>
  17925. struct unpack_type<basic_registry<Entity>, type_list<Override...>> {
  17926. using ro = type_list<>;
  17927. using rw = type_list<>;
  17928. };
  17929. template<typename Entity, typename... Override>
  17930. struct unpack_type<const basic_registry<Entity>, type_list<Override...>>
  17931. : unpack_type<basic_registry<Entity>, type_list<Override...>>
  17932. {};
  17933. template<typename Entity, typename... Exclude, typename... Component, typename... Override>
  17934. struct unpack_type<basic_view<Entity, exclude_t<Exclude...>, Component...>, type_list<Override...>> {
  17935. using ro = type_list_cat_t<type_list<Exclude...>, typename unpack_type<Component, type_list<Override...>>::ro...>;
  17936. using rw = type_list_cat_t<typename unpack_type<Component, type_list<Override...>>::rw...>;
  17937. };
  17938. template<typename Entity, typename... Exclude, typename... Component, typename... Override>
  17939. struct unpack_type<const basic_view<Entity, exclude_t<Exclude...>, Component...>, type_list<Override...>>
  17940. : unpack_type<basic_view<Entity, exclude_t<Exclude...>, Component...>, type_list<Override...>>
  17941. {};
  17942. template<typename, typename>
  17943. struct resource;
  17944. template<typename... Args, typename... Req>
  17945. struct resource<type_list<Args...>, type_list<Req...>> {
  17946. using args = type_list<std::remove_const_t<Args>...>;
  17947. using ro = type_list_cat_t<typename unpack_type<Args, type_list<Req...>>::ro..., typename unpack_type<Req, type_list<>>::ro...>;
  17948. using rw = type_list_cat_t<typename unpack_type<Args, type_list<Req...>>::rw..., typename unpack_type<Req, type_list<>>::rw...>;
  17949. };
  17950. template<typename... Req, typename Ret, typename... Args>
  17951. resource<type_list<std::remove_reference_t<Args>...>, type_list<Req...>> free_function_to_resource(Ret(*)(Args...));
  17952. template<typename... Req, typename Ret, typename Type, typename... Args>
  17953. resource<type_list<std::remove_reference_t<Args>...>, type_list<Req...>> constrained_function_to_resource(Ret(*)(Type &, Args...));
  17954. template<typename... Req, typename Ret, typename Class, typename... Args>
  17955. resource<type_list<std::remove_reference_t<Args>...>, type_list<Req...>> constrained_function_to_resource(Ret(Class:: *)(Args...));
  17956. template<typename... Req, typename Ret, typename Class, typename... Args>
  17957. resource<type_list<std::remove_reference_t<Args>...>, type_list<Req...>> constrained_function_to_resource(Ret(Class:: *)(Args...) const);
  17958. template<typename... Req>
  17959. resource<type_list<>, type_list<Req...>> to_resource();
  17960. }
  17961. /**
  17962. * Internal details not to be documented.
  17963. * @endcond
  17964. */
  17965. /**
  17966. * @brief Utility class for creating a static task graph.
  17967. *
  17968. * This class offers minimal support (but sufficient in many cases) for creating
  17969. * an execution graph from functions and their requirements on resources.<br/>
  17970. * Note that the resulting tasks aren't executed in any case. This isn't the
  17971. * goal of the tool. Instead, they are returned to the user in the form of a
  17972. * graph that allows for safe execution.
  17973. *
  17974. * @tparam Entity A valid entity type (see entt_traits for more details).
  17975. */
  17976. template<typename Entity>
  17977. class basic_organizer final {
  17978. using callback_type = void(const void *, entt::basic_registry<Entity> &);
  17979. using prepare_type = void(entt::basic_registry<Entity> &);
  17980. using dependency_type = std::size_t(const bool, type_info *, const std::size_t);
  17981. struct vertex_data final {
  17982. std::size_t ro_count{};
  17983. std::size_t rw_count{};
  17984. const char *name{};
  17985. const void *payload{};
  17986. callback_type *callback{};
  17987. dependency_type *dependency;
  17988. prepare_type *prepare{};
  17989. type_info info{};
  17990. };
  17991. template<typename Type>
  17992. [[nodiscard]] static decltype(auto) extract(basic_registry<Entity> &reg) {
  17993. if constexpr(std::is_same_v<Type, basic_registry<Entity>>) {
  17994. return reg;
  17995. } else if constexpr(internal::is_view_v<Type>) {
  17996. return as_view{reg};
  17997. } else {
  17998. return reg.template ctx_or_set<std::remove_reference_t<Type>>();
  17999. }
  18000. }
  18001. template<typename... Args>
  18002. [[nodiscard]] static auto to_args(basic_registry<Entity> &reg, type_list<Args...>) {
  18003. return std::tuple<decltype(extract<Args>(reg))...>(extract<Args>(reg)...);
  18004. }
  18005. template<typename... Type>
  18006. static std::size_t fill_dependencies(type_list<Type...>, [[maybe_unused]] type_info *buffer, [[maybe_unused]] const std::size_t count) {
  18007. if constexpr(sizeof...(Type) == 0u) {
  18008. return {};
  18009. } else {
  18010. type_info info[sizeof...(Type)]{type_id<Type>()...};
  18011. const auto length = (std::min)(count, sizeof...(Type));
  18012. std::copy_n(info, length, buffer);
  18013. return length;
  18014. }
  18015. }
  18016. template<typename... RO, typename... RW>
  18017. void track_dependencies(std::size_t index, const bool requires_registry, type_list<RO...>, type_list<RW...>) {
  18018. dependencies[type_hash<basic_registry<Entity>>::value()].emplace_back(index, requires_registry || (sizeof...(RO) + sizeof...(RW) == 0u));
  18019. (dependencies[type_hash<RO>::value()].emplace_back(index, false), ...);
  18020. (dependencies[type_hash<RW>::value()].emplace_back(index, true), ...);
  18021. }
  18022. [[nodiscard]] std::vector<bool> adjacency_matrix() {
  18023. const auto length = vertices.size();
  18024. std::vector<bool> edges(length * length, false);
  18025. // creates the ajacency matrix
  18026. for(const auto &deps: dependencies) {
  18027. const auto last = deps.second.cend();
  18028. auto it = deps.second.cbegin();
  18029. while(it != last) {
  18030. if(it->second) {
  18031. // rw item
  18032. if(auto curr = it++; it != last) {
  18033. if(it->second) {
  18034. edges[curr->first * length + it->first] = true;
  18035. } else {
  18036. if(const auto next = std::find_if(it, last, [](const auto &elem) { return elem.second; }); next != last) {
  18037. for(; it != next; ++it) {
  18038. edges[curr->first * length + it->first] = true;
  18039. edges[it->first * length + next->first] = true;
  18040. }
  18041. } else {
  18042. for(; it != next; ++it) {
  18043. edges[curr->first * length + it->first] = true;
  18044. }
  18045. }
  18046. }
  18047. }
  18048. } else {
  18049. // ro item, possibly only on first iteration
  18050. if(const auto next = std::find_if(it, last, [](const auto &elem) { return elem.second; }); next != last) {
  18051. for(; it != next; ++it) {
  18052. edges[it->first * length + next->first] = true;
  18053. }
  18054. } else {
  18055. it = last;
  18056. }
  18057. }
  18058. }
  18059. }
  18060. // computes the transitive closure
  18061. for(std::size_t vk{}; vk < length; ++vk) {
  18062. for(std::size_t vi{}; vi < length; ++vi) {
  18063. for(std::size_t vj{}; vj < length; ++vj) {
  18064. edges[vi * length + vj] = edges[vi * length + vj] || (edges[vi * length + vk] && edges[vk * length + vj]);
  18065. }
  18066. }
  18067. }
  18068. // applies the transitive reduction
  18069. for(std::size_t vert{}; vert < length; ++vert) {
  18070. edges[vert * length + vert] = false;
  18071. }
  18072. for(std::size_t vj{}; vj < length; ++vj) {
  18073. for(std::size_t vi{}; vi < length; ++vi) {
  18074. if(edges[vi * length + vj]) {
  18075. for(std::size_t vk{}; vk < length; ++vk) {
  18076. if(edges[vj * length + vk]) {
  18077. edges[vi * length + vk] = false;
  18078. }
  18079. }
  18080. }
  18081. }
  18082. }
  18083. return edges;
  18084. }
  18085. public:
  18086. /*! @brief Underlying entity identifier. */
  18087. using entity_type = Entity;
  18088. /*! @brief Unsigned integer type. */
  18089. using size_type = std::size_t;
  18090. /*! @brief Raw task function type. */
  18091. using function_type = callback_type;
  18092. /*! @brief Vertex type of a task graph defined as an adjacency list. */
  18093. struct vertex {
  18094. /**
  18095. * @brief Constructs a vertex of the task graph.
  18096. * @param vtype True if the vertex is a top-level one, false otherwise.
  18097. * @param data The data associated with the vertex.
  18098. * @param edges The indices of the children in the adjacency list.
  18099. */
  18100. vertex(const bool vtype, vertex_data data, std::vector<std::size_t> edges)
  18101. : is_top_level{vtype},
  18102. node{std::move(data)},
  18103. reachable{std::move(edges)}
  18104. {}
  18105. /**
  18106. * @brief Fills a buffer with the type info objects for the writable
  18107. * resources of a vertex.
  18108. * @param buffer A buffer pre-allocated by the user.
  18109. * @param length The length of the user-supplied buffer.
  18110. * @return The number of type info objects written to the buffer.
  18111. */
  18112. size_type ro_dependency(type_info *buffer, const std::size_t length) const ENTT_NOEXCEPT {
  18113. return node.dependency(false, buffer, length);
  18114. }
  18115. /**
  18116. * @brief Fills a buffer with the type info objects for the read-only
  18117. * resources of a vertex.
  18118. * @param buffer A buffer pre-allocated by the user.
  18119. * @param length The length of the user-supplied buffer.
  18120. * @return The number of type info objects written to the buffer.
  18121. */
  18122. size_type rw_dependency(type_info *buffer, const std::size_t length) const ENTT_NOEXCEPT {
  18123. return node.dependency(true, buffer, length);
  18124. }
  18125. /**
  18126. * @brief Returns the number of read-only resources of a vertex.
  18127. * @return The number of read-only resources of the vertex.
  18128. */
  18129. size_type ro_count() const ENTT_NOEXCEPT {
  18130. return node.ro_count;
  18131. }
  18132. /**
  18133. * @brief Returns the number of writable resources of a vertex.
  18134. * @return The number of writable resources of the vertex.
  18135. */
  18136. size_type rw_count() const ENTT_NOEXCEPT {
  18137. return node.rw_count;
  18138. }
  18139. /**
  18140. * @brief Checks if a vertex is also a top-level one.
  18141. * @return True if the vertex is a top-level one, false otherwise.
  18142. */
  18143. bool top_level() const ENTT_NOEXCEPT {
  18144. return is_top_level;
  18145. }
  18146. /**
  18147. * @brief Returns a type info object associated with a vertex.
  18148. * @return A properly initialized type info object.
  18149. */
  18150. type_info info() const ENTT_NOEXCEPT {
  18151. return node.info;
  18152. }
  18153. /**
  18154. * @brief Returns a user defined name associated with a vertex, if any.
  18155. * @return The user defined name associated with the vertex, if any.
  18156. */
  18157. const char * name() const ENTT_NOEXCEPT {
  18158. return node.name;
  18159. }
  18160. /**
  18161. * @brief Returns the function associated with a vertex.
  18162. * @return The function associated with the vertex.
  18163. */
  18164. function_type * callback() const ENTT_NOEXCEPT {
  18165. return node.callback;
  18166. }
  18167. /**
  18168. * @brief Returns the payload associated with a vertex, if any.
  18169. * @return The payload associated with the vertex, if any.
  18170. */
  18171. const void * data() const ENTT_NOEXCEPT {
  18172. return node.payload;
  18173. }
  18174. /**
  18175. * @brief Returns the list of nodes reachable from a given vertex.
  18176. * @return The list of nodes reachable from the vertex.
  18177. */
  18178. const std::vector<std::size_t> & children() const ENTT_NOEXCEPT {
  18179. return reachable;
  18180. }
  18181. /**
  18182. * @brief Prepares a registry and assures that all required resources
  18183. * are properly instantiated before using them.
  18184. * @param reg A valid registry.
  18185. */
  18186. void prepare(basic_registry<entity_type> &reg) const {
  18187. node.prepare ? node.prepare(reg) : void();
  18188. }
  18189. private:
  18190. bool is_top_level;
  18191. vertex_data node;
  18192. std::vector<std::size_t> reachable;
  18193. };
  18194. /**
  18195. * @brief Adds a free function to the task list.
  18196. * @tparam Candidate Function to add to the task list.
  18197. * @tparam Req Additional requirements and/or override resource access mode.
  18198. * @param name Optional name to associate with the task.
  18199. */
  18200. template<auto Candidate, typename... Req>
  18201. void emplace(const char *name = nullptr) {
  18202. using resource_type = decltype(internal::free_function_to_resource<Req...>(Candidate));
  18203. constexpr auto requires_registry = type_list_contains_v<typename resource_type::args, basic_registry<entity_type>>;
  18204. callback_type *callback = +[](const void *, basic_registry<entity_type> &reg) {
  18205. std::apply(Candidate, to_args(reg, typename resource_type::args{}));
  18206. };
  18207. track_dependencies(vertices.size(), requires_registry, typename resource_type::ro{}, typename resource_type::rw{});
  18208. vertices.push_back({
  18209. resource_type::ro::size,
  18210. resource_type::rw::size,
  18211. name,
  18212. nullptr,
  18213. callback,
  18214. +[](const bool rw, type_info *buffer, const std::size_t length) { return rw ? fill_dependencies(typename resource_type::rw{}, buffer, length) : fill_dependencies(typename resource_type::ro{}, buffer, length); },
  18215. +[](basic_registry<entity_type> &reg) { void(to_args(reg, typename resource_type::args{})); },
  18216. type_id<std::integral_constant<decltype(Candidate), Candidate>>()
  18217. });
  18218. }
  18219. /**
  18220. * @brief Adds a free function with payload or a member function with an
  18221. * instance to the task list.
  18222. * @tparam Candidate Function or member to add to the task list.
  18223. * @tparam Req Additional requirements and/or override resource access mode.
  18224. * @tparam Type Type of class or type of payload.
  18225. * @param value_or_instance A valid object that fits the purpose.
  18226. * @param name Optional name to associate with the task.
  18227. */
  18228. template<auto Candidate, typename... Req, typename Type>
  18229. void emplace(Type &value_or_instance, const char *name = nullptr) {
  18230. using resource_type = decltype(internal::constrained_function_to_resource<Req...>(Candidate));
  18231. constexpr auto requires_registry = type_list_contains_v<typename resource_type::args, basic_registry<entity_type>>;
  18232. callback_type *callback = +[](const void *payload, basic_registry<entity_type> &reg) {
  18233. Type *curr = static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(payload));
  18234. std::apply(Candidate, std::tuple_cat(std::forward_as_tuple(*curr), to_args(reg, typename resource_type::args{})));
  18235. };
  18236. track_dependencies(vertices.size(), requires_registry, typename resource_type::ro{}, typename resource_type::rw{});
  18237. vertices.push_back({
  18238. resource_type::ro::size,
  18239. resource_type::rw::size,
  18240. name,
  18241. &value_or_instance,
  18242. callback,
  18243. +[](const bool rw, type_info *buffer, const std::size_t length) {
  18244. return rw ? fill_dependencies(typename resource_type::rw{}, buffer, length) : fill_dependencies(typename resource_type::ro{}, buffer, length);
  18245. },
  18246. +[](basic_registry<entity_type> &reg) {
  18247. void(to_args(reg, typename resource_type::args{}));
  18248. },
  18249. type_id<std::integral_constant<decltype(Candidate), Candidate>>()
  18250. });
  18251. }
  18252. /**
  18253. * @brief Adds an user defined function with optional payload to the task
  18254. * list.
  18255. * @tparam Req Additional requirements and/or override resource access mode.
  18256. * @param func Function to add to the task list.
  18257. * @param payload User defined arbitrary data.
  18258. * @param name Optional name to associate with the task.
  18259. */
  18260. template<typename... Req>
  18261. void emplace(function_type *func, const void *payload = nullptr, const char *name = nullptr) {
  18262. using resource_type = internal::resource<type_list<>, type_list<Req...>>;
  18263. track_dependencies(vertices.size(), true, typename resource_type::ro{}, typename resource_type::rw{});
  18264. vertices.push_back({
  18265. resource_type::ro::size,
  18266. resource_type::rw::size,
  18267. name,
  18268. payload,
  18269. func,
  18270. +[](const bool rw, type_info *buffer, const std::size_t length) {
  18271. return rw ? fill_dependencies(typename resource_type::rw{}, buffer, length) : fill_dependencies(typename resource_type::ro{}, buffer, length);
  18272. },
  18273. nullptr,
  18274. type_info{}
  18275. });
  18276. }
  18277. /**
  18278. * @brief Generates a task graph for the current content.
  18279. * @return The adjacency list of the task graph.
  18280. */
  18281. std::vector<vertex> graph() {
  18282. const auto edges = adjacency_matrix();
  18283. // creates the adjacency list
  18284. std::vector<vertex> adjacency_list{};
  18285. adjacency_list.reserve(vertices.size());
  18286. for(std::size_t col{}, length = vertices.size(); col < length; ++col) {
  18287. std::vector<std::size_t> reachable{};
  18288. const auto row = col * length;
  18289. bool is_top_level = true;
  18290. for(std::size_t next{}; next < length; ++next) {
  18291. if(edges[row + next]) {
  18292. reachable.push_back(next);
  18293. }
  18294. }
  18295. for(std::size_t next{}; next < length && is_top_level; ++next) {
  18296. is_top_level = !edges[next * length + col];
  18297. }
  18298. adjacency_list.emplace_back(is_top_level, vertices[col], std::move(reachable));
  18299. }
  18300. return adjacency_list;
  18301. }
  18302. /*! @brief Erases all elements from a container. */
  18303. void clear() {
  18304. dependencies.clear();
  18305. vertices.clear();
  18306. }
  18307. private:
  18308. std::unordered_map<entt::id_type, std::vector<std::pair<std::size_t, bool>>> dependencies;
  18309. std::vector<vertex_data> vertices;
  18310. };
  18311. }
  18312. #endif
  18313. // #include "entity/poly_storage.hpp"
  18314. #ifndef ENTT_ENTITY_POLY_STORAGE_HPP
  18315. #define ENTT_ENTITY_POLY_STORAGE_HPP
  18316. #include <cstddef>
  18317. #include <tuple>
  18318. // #include "../core/type_info.hpp"
  18319. // #include "../core/type_traits.hpp"
  18320. // #include "../poly/poly.hpp"
  18321. // #include "fwd.hpp"
  18322. namespace entt {
  18323. /**
  18324. * @brief Basic poly storage implementation.
  18325. * @tparam Entity A valid entity type (see entt_traits for more details).
  18326. */
  18327. template<typename Entity>
  18328. struct Storage: type_list<type_info() const ENTT_NOEXCEPT> {
  18329. /**
  18330. * @brief Concept definition.
  18331. * @tparam Base Opaque base class from which to inherit.
  18332. */
  18333. template<typename Base>
  18334. struct type: Base {
  18335. /**
  18336. * @brief Returns a type info for the contained objects.
  18337. * @return The type info for the contained objects.
  18338. */
  18339. type_info value_type() const ENTT_NOEXCEPT {
  18340. return poly_call<0>(*this);
  18341. }
  18342. };
  18343. /**
  18344. * @brief Concept implementation.
  18345. * @tparam Type Type for which to generate an implementation.
  18346. */
  18347. template<typename Type>
  18348. using impl = value_list<&type_id<typename Type::value_type>>;
  18349. };
  18350. /**
  18351. * @brief Defines the poly storage type associate with a given entity type.
  18352. * @tparam Entity A valid entity type (see entt_traits for more details).
  18353. */
  18354. template<typename Entity, typename = void>
  18355. struct poly_storage_traits {
  18356. /*! @brief Poly storage type for the given entity type. */
  18357. using storage_type = poly<Storage<Entity>>;
  18358. };
  18359. }
  18360. #endif
  18361. // #include "entity/registry.hpp"
  18362. #ifndef ENTT_ENTITY_REGISTRY_HPP
  18363. #define ENTT_ENTITY_REGISTRY_HPP
  18364. #include <algorithm>
  18365. #include <cstddef>
  18366. #include <iterator>
  18367. #include <memory>
  18368. #include <tuple>
  18369. #include <type_traits>
  18370. #include <utility>
  18371. #include <vector>
  18372. // #include "../config/config.h"
  18373. // #include "../core/algorithm.hpp"
  18374. // #include "../core/any.hpp"
  18375. // #include "../core/fwd.hpp"
  18376. // #include "../core/type_info.hpp"
  18377. // #include "../core/type_traits.hpp"
  18378. // #include "component.hpp"
  18379. // #include "entity.hpp"
  18380. // #include "fwd.hpp"
  18381. // #include "group.hpp"
  18382. // #include "poly_storage.hpp"
  18383. // #include "runtime_view.hpp"
  18384. // #include "sparse_set.hpp"
  18385. // #include "storage.hpp"
  18386. // #include "utility.hpp"
  18387. // #include "view.hpp"
  18388. namespace entt {
  18389. /**
  18390. * @brief Fast and reliable entity-component system.
  18391. *
  18392. * The registry is the core class of the entity-component framework.<br/>
  18393. * It stores entities and arranges pools of components on a per request basis.
  18394. * By means of a registry, users can manage entities and components, then create
  18395. * views or groups to iterate them.
  18396. *
  18397. * @tparam Entity A valid entity type (see entt_traits for more details).
  18398. */
  18399. template<typename Entity>
  18400. class basic_registry {
  18401. using traits_type = entt_traits<Entity>;
  18402. using poly_storage_type = typename poly_storage_traits<Entity>::storage_type;
  18403. using basic_common_type = basic_sparse_set<Entity>;
  18404. template<typename Component>
  18405. using storage_type = constness_as_t<typename storage_traits<Entity, std::remove_const_t<Component>>::storage_type, Component>;
  18406. struct pool_data {
  18407. poly_storage_type poly;
  18408. std::unique_ptr<basic_common_type> pool{};
  18409. };
  18410. template<typename...>
  18411. struct group_handler;
  18412. template<typename... Exclude, typename... Get, typename... Owned>
  18413. struct group_handler<exclude_t<Exclude...>, get_t<Get...>, Owned...> {
  18414. static_assert(!std::disjunction_v<typename component_traits<Owned>::in_place_delete...>, "Groups do not support in-place delete");
  18415. static_assert(std::conjunction_v<std::is_same<Owned, std::remove_const_t<Owned>>..., std::is_same<Get, std::remove_const_t<Get>>..., std::is_same<Exclude, std::remove_const_t<Exclude>>...>, "One or more component types are invalid");
  18416. std::conditional_t<sizeof...(Owned) == 0, basic_common_type, std::size_t> current{};
  18417. template<typename Component>
  18418. void maybe_valid_if(basic_registry &owner, const Entity entt) {
  18419. [[maybe_unused]] const auto cpools = std::make_tuple(owner.assure<Owned>()...);
  18420. const auto is_valid = ((std::is_same_v<Component, Owned> || std::get<storage_type<Owned> *>(cpools)->contains(entt)) && ...)
  18421. && ((std::is_same_v<Component, Get> || owner.assure<Get>()->contains(entt)) && ...)
  18422. && ((std::is_same_v<Component, Exclude> || !owner.assure<Exclude>()->contains(entt)) && ...);
  18423. if constexpr(sizeof...(Owned) == 0) {
  18424. if(is_valid && !current.contains(entt)) {
  18425. current.emplace(entt);
  18426. }
  18427. } else {
  18428. if(is_valid && !(std::get<0>(cpools)->index(entt) < current)) {
  18429. const auto pos = current++;
  18430. (std::get<storage_type<Owned> *>(cpools)->swap(std::get<storage_type<Owned> *>(cpools)->data()[pos], entt), ...);
  18431. }
  18432. }
  18433. }
  18434. void discard_if([[maybe_unused]] basic_registry &owner, const Entity entt) {
  18435. if constexpr(sizeof...(Owned) == 0) {
  18436. current.remove(entt);
  18437. } else {
  18438. if(const auto cpools = std::make_tuple(owner.assure<Owned>()...); std::get<0>(cpools)->contains(entt) && (std::get<0>(cpools)->index(entt) < current)) {
  18439. const auto pos = --current;
  18440. (std::get<storage_type<Owned> *>(cpools)->swap(std::get<storage_type<Owned> *>(cpools)->data()[pos], entt), ...);
  18441. }
  18442. }
  18443. }
  18444. };
  18445. struct group_data {
  18446. std::size_t size;
  18447. std::unique_ptr<void, void(*)(void *)> group;
  18448. bool (* owned)(const id_type) ENTT_NOEXCEPT;
  18449. bool (* get)(const id_type) ENTT_NOEXCEPT;
  18450. bool (* exclude)(const id_type) ENTT_NOEXCEPT;
  18451. };
  18452. template<typename Component>
  18453. [[nodiscard]] storage_type<Component> * assure() const {
  18454. static_assert(std::is_same_v<Component, std::decay_t<Component>>, "Non-decayed types not allowed");
  18455. const auto index = type_seq<Component>::value();
  18456. if(!(index < pools.size())) {
  18457. pools.resize(size_type(index)+1u);
  18458. }
  18459. if(auto &&pdata = pools[index]; !pdata.pool) {
  18460. pdata.pool.reset(new storage_type<Component>());
  18461. pdata.poly.template emplace<storage_type<Component> &>(*static_cast<storage_type<Component> *>(pdata.pool.get()));
  18462. }
  18463. return static_cast<storage_type<Component> *>(pools[index].pool.get());
  18464. }
  18465. template<typename Component>
  18466. [[nodiscard]] const storage_type<Component> * pool_if_exists() const ENTT_NOEXCEPT {
  18467. static_assert(std::is_same_v<Component, std::decay_t<Component>>, "Non-decayed types not allowed");
  18468. const auto index = type_seq<Component>::value();
  18469. return (!(index < pools.size()) || !pools[index].pool) ? nullptr : static_cast<const storage_type<Component> *>(pools[index].pool.get());
  18470. }
  18471. auto generate_identifier(const std::size_t pos) ENTT_NOEXCEPT {
  18472. ENTT_ASSERT(pos < traits_type::to_integral(null), "No entities available");
  18473. return traits_type::construct(static_cast<typename traits_type::entity_type>(pos), {});
  18474. }
  18475. auto recycle_identifier() ENTT_NOEXCEPT {
  18476. ENTT_ASSERT(free_list != null, "No entities available");
  18477. const auto curr = traits_type::to_entity(free_list);
  18478. free_list = (tombstone | entities[curr]);
  18479. return (entities[curr] = traits_type::construct(curr, traits_type::to_version(entities[curr])));
  18480. }
  18481. auto release_entity(const Entity entity, const typename traits_type::version_type version) {
  18482. const typename traits_type::version_type vers = version + (version == traits_type::to_version(tombstone));
  18483. entities[traits_type::to_entity(entity)] = traits_type::construct(traits_type::to_entity(free_list), vers);
  18484. free_list = (tombstone | entity);
  18485. return vers;
  18486. }
  18487. public:
  18488. /*! @brief Underlying entity identifier. */
  18489. using entity_type = Entity;
  18490. /*! @brief Underlying version type. */
  18491. using version_type = typename traits_type::version_type;
  18492. /*! @brief Unsigned integer type. */
  18493. using size_type = std::size_t;
  18494. /*! @brief Poly storage type. */
  18495. using poly_storage = typename poly_storage_traits<Entity>::storage_type;
  18496. /**
  18497. * @brief Returns the entity identifier without the version.
  18498. * @param entity An entity identifier, either valid or not.
  18499. * @return The entity identifier without the version.
  18500. */
  18501. [[nodiscard]] static entity_type entity(const entity_type entity) ENTT_NOEXCEPT {
  18502. return traits_type::construct(traits_type::to_entity(entity), {});
  18503. }
  18504. /**
  18505. * @brief Returns the version stored along with an entity identifier.
  18506. * @param entity An entity identifier, either valid or not.
  18507. * @return The version stored along with the given entity identifier.
  18508. */
  18509. [[nodiscard]] static version_type version(const entity_type entity) ENTT_NOEXCEPT {
  18510. return traits_type::to_version(entity);
  18511. }
  18512. /*! @brief Default constructor. */
  18513. basic_registry() = default;
  18514. /*! @brief Default move constructor. */
  18515. basic_registry(basic_registry &&) = default;
  18516. /*! @brief Default move assignment operator. @return This registry. */
  18517. basic_registry & operator=(basic_registry &&) = default;
  18518. /**
  18519. * @brief Prepares a pool for the given type if required.
  18520. * @tparam Component Type of component for which to prepare a pool.
  18521. */
  18522. template<typename Component>
  18523. void prepare() {
  18524. // suppress the warning due to the [[nodiscard]] attribute
  18525. static_cast<void>(assure<Component>());
  18526. }
  18527. /**
  18528. * @brief Returns a poly storage for a given type.
  18529. * @param info The type for which to return a poly storage.
  18530. * @return A valid poly storage if a pool for the given type exists, an
  18531. * empty and thus invalid element otherwise.
  18532. */
  18533. poly_storage & storage(const type_info info) {
  18534. ENTT_ASSERT(info.seq() < pools.size() && pools[info.seq()].poly, "Storage not available");
  18535. return pools[info.seq()].poly;
  18536. }
  18537. /*! @copydoc storage */
  18538. const poly_storage & storage(const type_info info) const {
  18539. ENTT_ASSERT(info.seq() < pools.size() && pools[info.seq()].poly, "Storage not available");
  18540. return pools[info.seq()].poly;
  18541. }
  18542. /**
  18543. * @brief Returns the number of existing components of the given type.
  18544. * @tparam Component Type of component of which to return the size.
  18545. * @return Number of existing components of the given type.
  18546. */
  18547. template<typename Component>
  18548. [[nodiscard]] size_type size() const {
  18549. const auto *cpool = pool_if_exists<Component>();
  18550. return cpool ? cpool->size() : size_type{};
  18551. }
  18552. /**
  18553. * @brief Returns the number of entities created so far.
  18554. * @return Number of entities created so far.
  18555. */
  18556. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  18557. return entities.size();
  18558. }
  18559. /**
  18560. * @brief Returns the number of entities still in use.
  18561. * @return Number of entities still in use.
  18562. */
  18563. [[nodiscard]] size_type alive() const {
  18564. auto sz = entities.size();
  18565. for(auto curr = free_list; curr != null; --sz) {
  18566. curr = entities[traits_type::to_entity(curr)];
  18567. }
  18568. return sz;
  18569. }
  18570. /**
  18571. * @brief Increases the capacity of the registry or of the pools for the
  18572. * given components.
  18573. *
  18574. * If no components are specified, the capacity of the registry is
  18575. * increased, that is the number of entities it contains. Otherwise the
  18576. * capacity of the pools for the given components is increased.<br/>
  18577. * In both cases, if the new capacity is greater than the current capacity,
  18578. * new storage is allocated, otherwise the method does nothing.
  18579. *
  18580. * @tparam Component Types of components for which to reserve storage.
  18581. * @param cap Desired capacity.
  18582. */
  18583. template<typename... Component>
  18584. void reserve(const size_type cap) {
  18585. if constexpr(sizeof...(Component) == 0) {
  18586. entities.reserve(cap);
  18587. } else {
  18588. (assure<Component>()->reserve(cap), ...);
  18589. }
  18590. }
  18591. /**
  18592. * @brief Reserves enough space to store `count` pools.
  18593. * @param count Number of pools to reserve space for.
  18594. */
  18595. [[deprecated("No longer supported")]]
  18596. void reserve_pools(const size_t count) {
  18597. pools.reserve(count);
  18598. }
  18599. /**
  18600. * @brief Returns the capacity of the pool for the given component.
  18601. * @tparam Component Type of component in which one is interested.
  18602. * @return Capacity of the pool of the given component.
  18603. */
  18604. template<typename Component>
  18605. [[nodiscard]] size_type capacity() const {
  18606. const auto *cpool = pool_if_exists<Component>();
  18607. return cpool ? cpool->capacity() : size_type{};
  18608. }
  18609. /**
  18610. * @brief Returns the number of entities that a registry has currently
  18611. * allocated space for.
  18612. * @return Capacity of the registry.
  18613. */
  18614. [[nodiscard]] size_type capacity() const ENTT_NOEXCEPT {
  18615. return entities.capacity();
  18616. }
  18617. /**
  18618. * @brief Requests the removal of unused capacity for the given components.
  18619. * @tparam Component Types of components for which to reclaim unused
  18620. * capacity.
  18621. */
  18622. template<typename... Component>
  18623. void shrink_to_fit() {
  18624. (assure<Component>()->shrink_to_fit(), ...);
  18625. }
  18626. /**
  18627. * @brief Checks whether the registry or the pools of the given components
  18628. * are empty.
  18629. *
  18630. * A registry is considered empty when it doesn't contain entities that are
  18631. * still in use.
  18632. *
  18633. * @tparam Component Types of components in which one is interested.
  18634. * @return True if the registry or the pools of the given components are
  18635. * empty, false otherwise.
  18636. */
  18637. template<typename... Component>
  18638. [[nodiscard]] bool empty() const {
  18639. if constexpr(sizeof...(Component) == 0) {
  18640. return !alive();
  18641. } else {
  18642. return [](const auto *... cpool) { return ((!cpool || cpool->empty()) && ...); }(pool_if_exists<Component>()...);
  18643. }
  18644. }
  18645. /**
  18646. * @brief Direct access to the list of entities of a registry.
  18647. *
  18648. * The returned pointer is such that range `[data(), data() + size())` is
  18649. * always a valid range, even if the container is empty.
  18650. *
  18651. * @warning
  18652. * This list contains both valid and destroyed entities and isn't suitable
  18653. * for direct use.
  18654. *
  18655. * @return A pointer to the array of entities.
  18656. */
  18657. [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT {
  18658. return entities.data();
  18659. }
  18660. /**
  18661. * @brief Returns the head of the list of released entities.
  18662. *
  18663. * This function is intended for use in conjunction with `assign`.<br/>
  18664. * The returned entity has an invalid identifier in all cases.
  18665. *
  18666. * @return The head of the list of released entities.
  18667. */
  18668. [[nodiscard]] entity_type released() const ENTT_NOEXCEPT {
  18669. return free_list;
  18670. }
  18671. /*! @copydoc released */
  18672. [[deprecated("Use ::released instead")]]
  18673. [[nodiscard]] entity_type destroyed() const ENTT_NOEXCEPT {
  18674. return released();
  18675. }
  18676. /**
  18677. * @brief Checks if an entity identifier refers to a valid entity.
  18678. * @param entity An entity identifier, either valid or not.
  18679. * @return True if the identifier is valid, false otherwise.
  18680. */
  18681. [[nodiscard]] bool valid(const entity_type entity) const {
  18682. const auto pos = size_type(traits_type::to_entity(entity));
  18683. return (pos < entities.size() && entities[pos] == entity);
  18684. }
  18685. /**
  18686. * @brief Returns the actual version for an entity identifier.
  18687. *
  18688. * @warning
  18689. * Attempting to use an entity that doesn't belong to the registry results
  18690. * in undefined behavior. An entity belongs to the registry even if it has
  18691. * been previously destroyed and/or recycled.
  18692. *
  18693. * @param entity A valid entity identifier.
  18694. * @return Actual version for the given entity identifier.
  18695. */
  18696. [[nodiscard]] version_type current(const entity_type entity) const {
  18697. const auto pos = size_type(traits_type::to_entity(entity));
  18698. ENTT_ASSERT(pos < entities.size(), "Entity does not exist");
  18699. return version(entities[pos]);
  18700. }
  18701. /**
  18702. * @brief Creates a new entity and returns it.
  18703. *
  18704. * There are two kinds of possible entity identifiers:
  18705. *
  18706. * * Newly created ones in case no entities have been previously destroyed.
  18707. * * Recycled ones with updated versions.
  18708. *
  18709. * @return A valid entity identifier.
  18710. */
  18711. [[nodiscard]] entity_type create() {
  18712. return (free_list == null) ? entities.emplace_back(generate_identifier(entities.size())) : recycle_identifier();
  18713. }
  18714. /**
  18715. * @brief Creates a new entity and returns it.
  18716. *
  18717. * @sa create
  18718. *
  18719. * If the requested entity isn't in use, the suggested identifier is created
  18720. * and returned. Otherwise, a new identifier is generated.
  18721. *
  18722. * @param hint Required entity identifier.
  18723. * @return A valid entity identifier.
  18724. */
  18725. [[nodiscard]] entity_type create(const entity_type hint) {
  18726. const auto length = entities.size();
  18727. if(hint == null || hint == tombstone) {
  18728. return create();
  18729. } else if(const auto req = traits_type::to_entity(hint); !(req < length)) {
  18730. entities.resize(size_type(req) + 1u, null);
  18731. for(auto pos = length; pos < req; ++pos) {
  18732. release_entity(generate_identifier(pos), {});
  18733. }
  18734. return (entities[req] = hint);
  18735. } else if(const auto curr = traits_type::to_entity(entities[req]); req == curr) {
  18736. return create();
  18737. } else {
  18738. auto *it = &free_list;
  18739. for(; traits_type::to_entity(*it) != req; it = &entities[traits_type::to_entity(*it)]);
  18740. *it = traits_type::construct(curr, traits_type::to_version(*it));
  18741. return (entities[req] = hint);
  18742. }
  18743. }
  18744. /**
  18745. * @brief Assigns each element in a range an entity.
  18746. *
  18747. * @sa create
  18748. *
  18749. * @tparam It Type of forward iterator.
  18750. * @param first An iterator to the first element of the range to generate.
  18751. * @param last An iterator past the last element of the range to generate.
  18752. */
  18753. template<typename It>
  18754. void create(It first, It last) {
  18755. for(; free_list != null && first != last; ++first) {
  18756. *first = recycle_identifier();
  18757. }
  18758. const auto length = entities.size();
  18759. entities.resize(length + std::distance(first, last), null);
  18760. for(auto pos = length; first != last; ++first, ++pos) {
  18761. *first = entities[pos] = generate_identifier(pos);
  18762. }
  18763. }
  18764. /**
  18765. * @brief Assigns entities to an empty registry.
  18766. *
  18767. * This function is intended for use in conjunction with `data`, `size` and
  18768. * `destroyed`.<br/>
  18769. * Don't try to inject ranges of randomly generated entities nor the _wrong_
  18770. * head for the list of destroyed entities. There is no guarantee that a
  18771. * registry will continue to work properly in this case.
  18772. *
  18773. * @warning
  18774. * There must be no entities still alive for this to work properly.
  18775. *
  18776. * @tparam It Type of input iterator.
  18777. * @param first An iterator to the first element of the range of entities.
  18778. * @param last An iterator past the last element of the range of entities.
  18779. * @param destroyed The head of the list of destroyed entities.
  18780. */
  18781. template<typename It>
  18782. void assign(It first, It last, const entity_type destroyed) {
  18783. ENTT_ASSERT(!alive(), "Entities still alive");
  18784. entities.assign(first, last);
  18785. free_list = destroyed;
  18786. }
  18787. /**
  18788. * @brief Releases an entity identifier.
  18789. *
  18790. * The version is updated and the identifier can be recycled at any time.
  18791. *
  18792. * @warning
  18793. * Attempting to use an invalid entity results in undefined behavior.
  18794. *
  18795. * @param entity A valid entity identifier.
  18796. * @return The version of the recycled entity.
  18797. */
  18798. version_type release(const entity_type entity) {
  18799. return release(entity, version(entity) + 1u);
  18800. }
  18801. /**
  18802. * @brief Releases an entity identifier.
  18803. *
  18804. * The suggested version or the valid version closest to the suggested one
  18805. * is used instead of the implicitly generated version.
  18806. *
  18807. * @sa release
  18808. *
  18809. * @param entity A valid entity identifier.
  18810. * @param version A desired version upon destruction.
  18811. * @return The version actually assigned to the entity.
  18812. */
  18813. version_type release(const entity_type entity, const version_type version) {
  18814. ENTT_ASSERT(orphan(entity), "Non-orphan entity");
  18815. return release_entity(entity, version);
  18816. }
  18817. /**
  18818. * @brief Releases all entity identifiers in a range.
  18819. *
  18820. * @sa release
  18821. *
  18822. * @tparam It Type of input iterator.
  18823. * @param first An iterator to the first element of the range of entities.
  18824. * @param last An iterator past the last element of the range of entities.
  18825. */
  18826. template<typename It>
  18827. void release(It first, It last) {
  18828. for(; first != last; ++first) {
  18829. release(*first, version(*first) + 1u);
  18830. }
  18831. }
  18832. /**
  18833. * @brief Destroys an entity and releases its identifier.
  18834. *
  18835. * The version is updated and the identifier can be recycled at any time.
  18836. *
  18837. * @warning
  18838. * Adding or removing components to an entity that is being destroyed can
  18839. * result in undefined behavior.
  18840. *
  18841. * @warning
  18842. * Attempting to use an invalid entity results in undefined behavior.
  18843. *
  18844. * @param entity A valid entity identifier.
  18845. * @return The version of the recycled entity.
  18846. */
  18847. version_type destroy(const entity_type entity) {
  18848. return destroy(entity, version(entity) + 1u);
  18849. }
  18850. /**
  18851. * @brief Destroys an entity and releases its identifier.
  18852. *
  18853. * The suggested version or the valid version closest to the suggested one
  18854. * is used instead of the implicitly generated version.
  18855. *
  18856. * @sa destroy
  18857. *
  18858. * @param entity A valid entity identifier.
  18859. * @param version A desired version upon destruction.
  18860. * @return The version actually assigned to the entity.
  18861. */
  18862. version_type destroy(const entity_type entity, const version_type version) {
  18863. ENTT_ASSERT(valid(entity), "Invalid entity");
  18864. for(auto &&pdata: pools) {
  18865. pdata.pool && pdata.pool->remove(entity, this);
  18866. }
  18867. return release_entity(entity, version);
  18868. }
  18869. /**
  18870. * @brief Destroys all entities in a range and releases their identifiers.
  18871. *
  18872. * @sa destroy
  18873. *
  18874. * @tparam It Type of input iterator.
  18875. * @param first An iterator to the first element of the range of entities.
  18876. * @param last An iterator past the last element of the range of entities.
  18877. */
  18878. template<typename It>
  18879. void destroy(It first, It last) {
  18880. if constexpr(is_iterator_type_v<typename basic_common_type::iterator, It>) {
  18881. for(; first != last; ++first) {
  18882. destroy(*first, version(*first) + 1u);
  18883. }
  18884. } else {
  18885. for(auto &&pdata: pools) {
  18886. pdata.pool && pdata.pool->remove(first, last, this);
  18887. }
  18888. release(first, last);
  18889. }
  18890. }
  18891. /**
  18892. * @brief Assigns the given component to an entity.
  18893. *
  18894. * A new instance of the given component is created and initialized with the
  18895. * arguments provided (the component must have a proper constructor or be of
  18896. * aggregate type). Then the component is assigned to the given entity.
  18897. *
  18898. * @warning
  18899. * Attempting to use an invalid entity or to assign a component to an entity
  18900. * that already owns it results in undefined behavior.
  18901. *
  18902. * @tparam Component Type of component to create.
  18903. * @tparam Args Types of arguments to use to construct the component.
  18904. * @param entity A valid entity identifier.
  18905. * @param args Parameters to use to initialize the component.
  18906. * @return A reference to the newly created component.
  18907. */
  18908. template<typename Component, typename... Args>
  18909. decltype(auto) emplace(const entity_type entity, Args &&... args) {
  18910. ENTT_ASSERT(valid(entity), "Invalid entity");
  18911. return assure<Component>()->emplace(*this, entity, std::forward<Args>(args)...);
  18912. }
  18913. /**
  18914. * @brief Assigns each entity in a range the given component.
  18915. *
  18916. * @sa emplace
  18917. *
  18918. * @tparam Component Type of component to create.
  18919. * @tparam It Type of input iterator.
  18920. * @param first An iterator to the first element of the range of entities.
  18921. * @param last An iterator past the last element of the range of entities.
  18922. * @param value An instance of the component to assign.
  18923. */
  18924. template<typename Component, typename It>
  18925. void insert(It first, It last, const Component &value = {}) {
  18926. ENTT_ASSERT(std::all_of(first, last, [this](const auto entity) { return valid(entity); }), "Invalid entity");
  18927. assure<Component>()->insert(*this, first, last, value);
  18928. }
  18929. /**
  18930. * @brief Assigns each entity in a range the given components.
  18931. *
  18932. * @sa emplace
  18933. *
  18934. * @tparam Component Type of component to create.
  18935. * @tparam EIt Type of input iterator.
  18936. * @tparam CIt Type of input iterator.
  18937. * @param first An iterator to the first element of the range of entities.
  18938. * @param last An iterator past the last element of the range of entities.
  18939. * @param from An iterator to the first element of the range of components.
  18940. */
  18941. template<typename Component, typename EIt, typename CIt, typename = std::enable_if_t<std::is_same_v<std::decay_t<typename std::iterator_traits<CIt>::value_type>, Component>>>
  18942. void insert(EIt first, EIt last, CIt from) {
  18943. static_assert(std::is_constructible_v<Component, typename std::iterator_traits<CIt>::value_type>, "Invalid value type");
  18944. ENTT_ASSERT(std::all_of(first, last, [this](const auto entity) { return valid(entity); }), "Invalid entity");
  18945. assure<Component>()->insert(*this, first, last, from);
  18946. }
  18947. /**
  18948. * @brief Assigns or replaces the given component for an entity.
  18949. *
  18950. * Equivalent to the following snippet (pseudocode):
  18951. *
  18952. * @code{.cpp}
  18953. * auto &component = registry.all_of<Component>(entity) ? registry.replace<Component>(entity, args...) : registry.emplace<Component>(entity, args...);
  18954. * @endcode
  18955. *
  18956. * Prefer this function anyway because it has slightly better performance.
  18957. *
  18958. * @warning
  18959. * Attempting to use an invalid entity results in undefined behavior.
  18960. *
  18961. * @tparam Component Type of component to assign or replace.
  18962. * @tparam Args Types of arguments to use to construct the component.
  18963. * @param entity A valid entity identifier.
  18964. * @param args Parameters to use to initialize the component.
  18965. * @return A reference to the newly created component.
  18966. */
  18967. template<typename Component, typename... Args>
  18968. decltype(auto) emplace_or_replace(const entity_type entity, Args &&... args) {
  18969. ENTT_ASSERT(valid(entity), "Invalid entity");
  18970. auto *cpool = assure<Component>();
  18971. return cpool->contains(entity)
  18972. ? cpool->patch(*this, entity, [&args...](auto &... curr) { ((curr = Component{std::forward<Args>(args)...}), ...); })
  18973. : cpool->emplace(*this, entity, std::forward<Args>(args)...);
  18974. }
  18975. /**
  18976. * @brief Patches the given component for an entity.
  18977. *
  18978. * The signature of the functions should be equivalent to the following:
  18979. *
  18980. * @code{.cpp}
  18981. * void(Component &);
  18982. * @endcode
  18983. *
  18984. * @note
  18985. * Empty types aren't explicitly instantiated and therefore they are never
  18986. * returned. However, this function can be used to trigger an update signal
  18987. * for them.
  18988. *
  18989. * @warning
  18990. * Attempting to use an invalid entity or to patch a component of an entity
  18991. * that doesn't own it results in undefined behavior.
  18992. *
  18993. * @tparam Component Type of component to patch.
  18994. * @tparam Func Types of the function objects to invoke.
  18995. * @param entity A valid entity identifier.
  18996. * @param func Valid function objects.
  18997. * @return A reference to the patched component.
  18998. */
  18999. template<typename Component, typename... Func>
  19000. decltype(auto) patch(const entity_type entity, Func &&... func) {
  19001. ENTT_ASSERT(valid(entity), "Invalid entity");
  19002. return assure<Component>()->patch(*this, entity, std::forward<Func>(func)...);
  19003. }
  19004. /**
  19005. * @brief Replaces the given component for an entity.
  19006. *
  19007. * A new instance of the given component is created and initialized with the
  19008. * arguments provided (the component must have a proper constructor or be of
  19009. * aggregate type). Then the component is assigned to the given entity.
  19010. *
  19011. * @warning
  19012. * Attempting to use an invalid entity or to replace a component of an
  19013. * entity that doesn't own it results in undefined behavior.
  19014. *
  19015. * @tparam Component Type of component to replace.
  19016. * @tparam Args Types of arguments to use to construct the component.
  19017. * @param entity A valid entity identifier.
  19018. * @param args Parameters to use to initialize the component.
  19019. * @return A reference to the component being replaced.
  19020. */
  19021. template<typename Component, typename... Args>
  19022. decltype(auto) replace(const entity_type entity, Args &&... args) {
  19023. return assure<Component>()->patch(*this, entity, [&args...](auto &... curr) { ((curr = Component{std::forward<Args>(args)...}), ...); });
  19024. }
  19025. /**
  19026. * @brief Removes the given components from an entity.
  19027. *
  19028. * @warning
  19029. * Attempting to use an invalid entity results in undefined behavior.
  19030. *
  19031. * @tparam Component Types of components to remove.
  19032. * @param entity A valid entity identifier.
  19033. * @return The number of components actually removed.
  19034. */
  19035. template<typename... Component>
  19036. size_type remove(const entity_type entity) {
  19037. ENTT_ASSERT(valid(entity), "Invalid entity");
  19038. static_assert(sizeof...(Component) > 0, "Provide one or more component types");
  19039. return (assure<Component>()->remove(entity, this) + ... + size_type{});
  19040. }
  19041. /**
  19042. * @brief Removes the given components from all the entities in a range.
  19043. *
  19044. * @sa remove
  19045. *
  19046. * @tparam Component Types of components to remove.
  19047. * @tparam It Type of input iterator.
  19048. * @param first An iterator to the first element of the range of entities.
  19049. * @param last An iterator past the last element of the range of entities.
  19050. * @return The number of components actually removed.
  19051. */
  19052. template<typename... Component, typename It>
  19053. size_type remove(It first, It last) {
  19054. static_assert(sizeof...(Component) > 0, "Provide one or more component types");
  19055. const auto cpools = std::make_tuple(assure<Component>()...);
  19056. size_type count{};
  19057. for(; first != last; ++first) {
  19058. const auto entity = *first;
  19059. ENTT_ASSERT(valid(entity), "Invalid entity");
  19060. count += (std::get<storage_type<Component> *>(cpools)->remove(entity, this) + ...);
  19061. }
  19062. return count;
  19063. }
  19064. /**
  19065. * @brief Erases the given components from an entity.
  19066. *
  19067. * @warning
  19068. * Attempting to use an invalid entity or to erase a component from an
  19069. * entity that doesn't own it results in undefined behavior.
  19070. *
  19071. * @tparam Component Types of components to erase.
  19072. * @param entity A valid entity identifier.
  19073. */
  19074. template<typename... Component>
  19075. void erase(const entity_type entity) {
  19076. ENTT_ASSERT(valid(entity), "Invalid entity");
  19077. static_assert(sizeof...(Component) > 0, "Provide one or more component types");
  19078. (assure<Component>()->erase(entity, this), ...);
  19079. }
  19080. /**
  19081. * @brief Erases the given components from all the entities in a range.
  19082. *
  19083. * @sa erase
  19084. *
  19085. * @tparam Component Types of components to erase.
  19086. * @tparam It Type of input iterator.
  19087. * @param first An iterator to the first element of the range of entities.
  19088. * @param last An iterator past the last element of the range of entities.
  19089. */
  19090. template<typename... Component, typename It>
  19091. void erase(It first, It last) {
  19092. static_assert(sizeof...(Component) > 0, "Provide one or more component types");
  19093. const auto cpools = std::make_tuple(assure<Component>()...);
  19094. for(; first != last; ++first) {
  19095. const auto entity = *first;
  19096. ENTT_ASSERT(valid(entity), "Invalid entity");
  19097. (std::get<storage_type<Component> *>(cpools)->erase(entity, this), ...);
  19098. }
  19099. }
  19100. /**
  19101. * @brief Removes all tombstones from a registry or only the pools for the
  19102. * given components.
  19103. * @tparam Component Types of components for which to clear all tombstones.
  19104. */
  19105. template<typename... Component>
  19106. void compact() {
  19107. if constexpr(sizeof...(Component) == 0) {
  19108. for(auto &&pdata: pools) {
  19109. pdata.pool && (pdata.pool->compact(), true);
  19110. }
  19111. } else {
  19112. (assure<Component>()->compact(), ...);
  19113. }
  19114. }
  19115. /*! @copydoc remove */
  19116. template<typename... Component>
  19117. [[deprecated("Use ::remove instead")]]
  19118. size_type remove_if_exists(const entity_type entity) {
  19119. return remove<Component...>(entity);
  19120. }
  19121. /**
  19122. * @brief Removes all the components from an entity and makes it orphaned.
  19123. *
  19124. * @warning
  19125. * In case there are listeners that observe the destruction of components
  19126. * and assign other components to the entity in their bodies, the result of
  19127. * invoking this function may not be as expected. In the worst case, it
  19128. * could lead to undefined behavior.
  19129. *
  19130. * @warning
  19131. * Attempting to use an invalid entity results in undefined behavior.
  19132. *
  19133. * @param entity A valid entity identifier.
  19134. */
  19135. [[deprecated("Use ::destroy(entity)/::create(entity) instead")]]
  19136. void remove_all(const entity_type entity) {
  19137. ENTT_ASSERT(valid(entity), "Invalid entity");
  19138. for(auto &&pdata: pools) {
  19139. pdata.pool && pdata.pool->remove(entity, this);
  19140. }
  19141. }
  19142. /**
  19143. * @brief Checks if an entity has all the given components.
  19144. *
  19145. * @warning
  19146. * Attempting to use an invalid entity results in undefined behavior.
  19147. *
  19148. * @tparam Component Components for which to perform the check.
  19149. * @param entity A valid entity identifier.
  19150. * @return True if the entity has all the components, false otherwise.
  19151. */
  19152. template<typename... Component>
  19153. [[nodiscard]] bool all_of(const entity_type entity) const {
  19154. ENTT_ASSERT(valid(entity), "Invalid entity");
  19155. return [entity](const auto *... cpool) { return ((cpool && cpool->contains(entity)) && ...); }(pool_if_exists<Component>()...);
  19156. }
  19157. /**
  19158. * @brief Checks if an entity has at least one of the given components.
  19159. *
  19160. * @warning
  19161. * Attempting to use an invalid entity results in undefined behavior.
  19162. *
  19163. * @tparam Component Components for which to perform the check.
  19164. * @param entity A valid entity identifier.
  19165. * @return True if the entity has at least one of the given components,
  19166. * false otherwise.
  19167. */
  19168. template<typename... Component>
  19169. [[nodiscard]] bool any_of(const entity_type entity) const {
  19170. ENTT_ASSERT(valid(entity), "Invalid entity");
  19171. return [entity](const auto *... cpool) { return !((!cpool || !cpool->contains(entity)) && ...); }(pool_if_exists<Component>()...);
  19172. }
  19173. /**
  19174. * @brief Returns references to the given components for an entity.
  19175. *
  19176. * @warning
  19177. * Attempting to use an invalid entity or to get a component from an entity
  19178. * that doesn't own it results in undefined behavior.
  19179. *
  19180. * @tparam Component Types of components to get.
  19181. * @param entity A valid entity identifier.
  19182. * @return References to the components owned by the entity.
  19183. */
  19184. template<typename... Component>
  19185. [[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entity) const {
  19186. ENTT_ASSERT(valid(entity), "Invalid entity");
  19187. if constexpr(sizeof...(Component) == 1) {
  19188. const auto *cpool = pool_if_exists<std::remove_const_t<Component>...>();
  19189. ENTT_ASSERT(cpool, "Storage not available");
  19190. return cpool->get(entity);
  19191. } else {
  19192. return std::forward_as_tuple(get<Component>(entity)...);
  19193. }
  19194. }
  19195. /*! @copydoc get */
  19196. template<typename... Component>
  19197. [[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entity) {
  19198. ENTT_ASSERT(valid(entity), "Invalid entity");
  19199. if constexpr(sizeof...(Component) == 1) {
  19200. return (const_cast<Component &>(assure<std::remove_const_t<Component>>()->get(entity)), ...);
  19201. } else {
  19202. return std::forward_as_tuple(get<Component>(entity)...);
  19203. }
  19204. }
  19205. /**
  19206. * @brief Returns a reference to the given component for an entity.
  19207. *
  19208. * In case the entity doesn't own the component, the parameters provided are
  19209. * used to construct it.<br/>
  19210. * Equivalent to the following snippet (pseudocode):
  19211. *
  19212. * @code{.cpp}
  19213. * auto &component = registry.all_of<Component>(entity) ? registry.get<Component>(entity) : registry.emplace<Component>(entity, args...);
  19214. * @endcode
  19215. *
  19216. * Prefer this function anyway because it has slightly better performance.
  19217. *
  19218. * @warning
  19219. * Attempting to use an invalid entity results in undefined behavior.
  19220. *
  19221. * @tparam Component Type of component to get.
  19222. * @tparam Args Types of arguments to use to construct the component.
  19223. * @param entity A valid entity identifier.
  19224. * @param args Parameters to use to initialize the component.
  19225. * @return Reference to the component owned by the entity.
  19226. */
  19227. template<typename Component, typename... Args>
  19228. [[nodiscard]] decltype(auto) get_or_emplace(const entity_type entity, Args &&... args) {
  19229. ENTT_ASSERT(valid(entity), "Invalid entity");
  19230. auto *cpool = assure<Component>();
  19231. return cpool->contains(entity) ? cpool->get(entity) : cpool->emplace(*this, entity, std::forward<Args>(args)...);
  19232. }
  19233. /**
  19234. * @brief Returns pointers to the given components for an entity.
  19235. *
  19236. * @warning
  19237. * Attempting to use an invalid entity results in undefined behavior.
  19238. *
  19239. * @note
  19240. * The registry retains ownership of the pointed-to components.
  19241. *
  19242. * @tparam Component Types of components to get.
  19243. * @param entity A valid entity identifier.
  19244. * @return Pointers to the components owned by the entity.
  19245. */
  19246. template<typename... Component>
  19247. [[nodiscard]] auto try_get([[maybe_unused]] const entity_type entity) const {
  19248. ENTT_ASSERT(valid(entity), "Invalid entity");
  19249. if constexpr(sizeof...(Component) == 1) {
  19250. const auto *cpool = pool_if_exists<std::remove_const_t<Component>...>();
  19251. return (cpool && cpool->contains(entity)) ? &cpool->get(entity) : nullptr;
  19252. } else {
  19253. return std::make_tuple(try_get<Component>(entity)...);
  19254. }
  19255. }
  19256. /*! @copydoc try_get */
  19257. template<typename... Component>
  19258. [[nodiscard]] auto try_get([[maybe_unused]] const entity_type entity) {
  19259. ENTT_ASSERT(valid(entity), "Invalid entity");
  19260. if constexpr(sizeof...(Component) == 1) {
  19261. return (const_cast<Component *>(std::as_const(*this).template try_get<Component>(entity)), ...);
  19262. } else {
  19263. return std::make_tuple(try_get<Component>(entity)...);
  19264. }
  19265. }
  19266. /**
  19267. * @brief Clears a whole registry or the pools for the given components.
  19268. * @tparam Component Types of components to remove from their entities.
  19269. */
  19270. template<typename... Component>
  19271. void clear() {
  19272. if constexpr(sizeof...(Component) == 0) {
  19273. for(auto &&pdata: pools) {
  19274. pdata.pool && (pdata.pool->clear(this), true);
  19275. }
  19276. each([this](const auto entity) { release_entity(entity, version(entity) + 1u); });
  19277. } else {
  19278. (assure<Component>()->clear(this), ...);
  19279. }
  19280. }
  19281. /**
  19282. * @brief Iterates all the entities that are still in use.
  19283. *
  19284. * The function object is invoked for each entity that is still in use.<br/>
  19285. * The signature of the function should be equivalent to the following:
  19286. *
  19287. * @code{.cpp}
  19288. * void(const Entity);
  19289. * @endcode
  19290. *
  19291. * This function is fairly slow and should not be used frequently. However,
  19292. * it's useful for iterating all the entities still in use, regardless of
  19293. * their components.
  19294. *
  19295. * @tparam Func Type of the function object to invoke.
  19296. * @param func A valid function object.
  19297. */
  19298. template<typename Func>
  19299. void each(Func func) const {
  19300. if(free_list == null) {
  19301. for(auto pos = entities.size(); pos; --pos) {
  19302. func(entities[pos-1]);
  19303. }
  19304. } else {
  19305. for(auto pos = entities.size(); pos; --pos) {
  19306. if(const auto entity = entities[pos - 1]; traits_type::to_entity(entity) == (pos - 1)) {
  19307. func(entity);
  19308. }
  19309. }
  19310. }
  19311. }
  19312. /**
  19313. * @brief Checks if an entity has components assigned.
  19314. * @param entity A valid entity identifier.
  19315. * @return True if the entity has no components assigned, false otherwise.
  19316. */
  19317. [[nodiscard]] bool orphan(const entity_type entity) const {
  19318. ENTT_ASSERT(valid(entity), "Invalid entity");
  19319. return std::none_of(pools.cbegin(), pools.cend(), [entity](auto &&pdata) { return pdata.pool && pdata.pool->contains(entity); });
  19320. }
  19321. /**
  19322. * @brief Iterates orphans and applies them the given function object.
  19323. *
  19324. * The function object is invoked for each entity that is still in use and
  19325. * has no components assigned.<br/>
  19326. * The signature of the function should be equivalent to the following:
  19327. *
  19328. * @code{.cpp}
  19329. * void(const Entity);
  19330. * @endcode
  19331. *
  19332. * This function can be very slow and should not be used frequently.
  19333. *
  19334. * @tparam Func Type of the function object to invoke.
  19335. * @param func A valid function object.
  19336. */
  19337. template<typename Func>
  19338. void orphans(Func func) const {
  19339. each([this, &func](const auto entity) {
  19340. if(orphan(entity)) {
  19341. func(entity);
  19342. }
  19343. });
  19344. }
  19345. /**
  19346. * @brief Returns a sink object for the given component.
  19347. *
  19348. * The sink returned by this function can be used to receive notifications
  19349. * whenever a new instance of the given component is created and assigned to
  19350. * an entity.<br/>
  19351. * The function type for a listener is equivalent to:
  19352. *
  19353. * @code{.cpp}
  19354. * void(basic_registry<Entity> &, Entity);
  19355. * @endcode
  19356. *
  19357. * Listeners are invoked **after** the component has been assigned to the
  19358. * entity.
  19359. *
  19360. * @sa sink
  19361. *
  19362. * @tparam Component Type of component of which to get the sink.
  19363. * @return A temporary sink object.
  19364. */
  19365. template<typename Component>
  19366. [[nodiscard]] auto on_construct() {
  19367. return assure<Component>()->on_construct();
  19368. }
  19369. /**
  19370. * @brief Returns a sink object for the given component.
  19371. *
  19372. * The sink returned by this function can be used to receive notifications
  19373. * whenever an instance of the given component is explicitly updated.<br/>
  19374. * The function type for a listener is equivalent to:
  19375. *
  19376. * @code{.cpp}
  19377. * void(basic_registry<Entity> &, Entity);
  19378. * @endcode
  19379. *
  19380. * Listeners are invoked **after** the component has been updated.
  19381. *
  19382. * @sa sink
  19383. *
  19384. * @tparam Component Type of component of which to get the sink.
  19385. * @return A temporary sink object.
  19386. */
  19387. template<typename Component>
  19388. [[nodiscard]] auto on_update() {
  19389. return assure<Component>()->on_update();
  19390. }
  19391. /**
  19392. * @brief Returns a sink object for the given component.
  19393. *
  19394. * The sink returned by this function can be used to receive notifications
  19395. * whenever an instance of the given component is removed from an entity and
  19396. * thus destroyed.<br/>
  19397. * The function type for a listener is equivalent to:
  19398. *
  19399. * @code{.cpp}
  19400. * void(basic_registry<Entity> &, Entity);
  19401. * @endcode
  19402. *
  19403. * Listeners are invoked **before** the component has been removed from the
  19404. * entity.
  19405. *
  19406. * @sa sink
  19407. *
  19408. * @tparam Component Type of component of which to get the sink.
  19409. * @return A temporary sink object.
  19410. */
  19411. template<typename Component>
  19412. [[nodiscard]] auto on_destroy() {
  19413. return assure<Component>()->on_destroy();
  19414. }
  19415. /**
  19416. * @brief Returns a view for the given components.
  19417. *
  19418. * This kind of objects are created on the fly and share with the registry
  19419. * its internal data structures.<br/>
  19420. * Feel free to discard a view after the use. Creating and destroying a view
  19421. * is an incredibly cheap operation because they do not require any type of
  19422. * initialization.<br/>
  19423. * As a rule of thumb, storing a view should never be an option.
  19424. *
  19425. * Views do their best to iterate the smallest set of candidate entities.
  19426. * In particular:
  19427. *
  19428. * * Single component views are incredibly fast and iterate a packed array
  19429. * of entities, all of which has the given component.
  19430. * * Multi component views look at the number of entities available for each
  19431. * component and pick up a reference to the smallest set of candidates to
  19432. * test for the given components.
  19433. *
  19434. * Views in no way affect the functionalities of the registry nor those of
  19435. * the underlying pools.
  19436. *
  19437. * @note
  19438. * Multi component views are pretty fast. However their performance tend to
  19439. * degenerate when the number of components to iterate grows up and the most
  19440. * of the entities have all the given components.<br/>
  19441. * To get a performance boost, consider using a group instead.
  19442. *
  19443. * @tparam Component Type of components used to construct the view.
  19444. * @tparam Exclude Types of components used to filter the view.
  19445. * @return A newly created view.
  19446. */
  19447. template<typename... Component, typename... Exclude>
  19448. [[nodiscard]] basic_view<Entity, exclude_t<Exclude...>, std::add_const_t<Component>...> view(exclude_t<Exclude...> = {}) const {
  19449. static_assert(sizeof...(Component) > 0, "Exclusion-only views are not supported");
  19450. return { *assure<std::remove_const_t<Component>>()..., *assure<Exclude>()... };
  19451. }
  19452. /*! @copydoc view */
  19453. template<typename... Component, typename... Exclude>
  19454. [[nodiscard]] basic_view<Entity, exclude_t<Exclude...>, Component...> view(exclude_t<Exclude...> = {}) {
  19455. static_assert(sizeof...(Component) > 0, "Exclusion-only views are not supported");
  19456. return { *assure<std::remove_const_t<Component>>()..., *assure<Exclude>()... };
  19457. }
  19458. /**
  19459. * @brief Returns a runtime view for the given components.
  19460. *
  19461. * This kind of objects are created on the fly and share with the registry
  19462. * its internal data structures.<br/>
  19463. * Users should throw away the view after use. Fortunately, creating and
  19464. * destroying a runtime view is an incredibly cheap operation because they
  19465. * do not require any type of initialization.<br/>
  19466. * As a rule of thumb, storing a view should never be an option.
  19467. *
  19468. * Runtime views are to be used when users want to construct a view from
  19469. * some external inputs and don't know at compile-time what are the required
  19470. * components.
  19471. *
  19472. * @tparam ItComp Type of input iterator for the components to use to
  19473. * construct the view.
  19474. * @tparam ItExcl Type of input iterator for the components to use to filter
  19475. * the view.
  19476. * @param first An iterator to the first element of the range of components
  19477. * to use to construct the view.
  19478. * @param last An iterator past the last element of the range of components
  19479. * to use to construct the view.
  19480. * @param from An iterator to the first element of the range of components
  19481. * to use to filter the view.
  19482. * @param to An iterator past the last element of the range of components to
  19483. * use to filter the view.
  19484. * @return A newly created runtime view.
  19485. */
  19486. template<typename ItComp, typename ItExcl = id_type *>
  19487. [[nodiscard]] basic_runtime_view<Entity> runtime_view(ItComp first, ItComp last, ItExcl from = {}, ItExcl to = {}) const {
  19488. std::vector<const basic_common_type *> component(std::distance(first, last));
  19489. std::vector<const basic_common_type *> filter(std::distance(from, to));
  19490. std::transform(first, last, component.begin(), [this](const auto ctype) {
  19491. const auto it = std::find_if(pools.cbegin(), pools.cend(), [ctype](auto &&pdata) { return pdata.poly && pdata.poly->value_type().hash() == ctype; });
  19492. return it == pools.cend() ? nullptr : it->pool.get();
  19493. });
  19494. std::transform(from, to, filter.begin(), [this](const auto ctype) {
  19495. const auto it = std::find_if(pools.cbegin(), pools.cend(), [ctype](auto &&pdata) { return pdata.poly && pdata.poly->value_type().hash() == ctype; });
  19496. return it == pools.cend() ? nullptr : it->pool.get();
  19497. });
  19498. return { std::move(component), std::move(filter) };
  19499. }
  19500. /**
  19501. * @brief Returns a group for the given components.
  19502. *
  19503. * This kind of objects are created on the fly and share with the registry
  19504. * its internal data structures.<br/>
  19505. * Feel free to discard a group after the use. Creating and destroying a
  19506. * group is an incredibly cheap operation because they do not require any
  19507. * type of initialization, but for the first time they are requested.<br/>
  19508. * As a rule of thumb, storing a group should never be an option.
  19509. *
  19510. * Groups support exclusion lists and can own types of components. The more
  19511. * types are owned by a group, the faster it is to iterate entities and
  19512. * components.<br/>
  19513. * However, groups also affect some features of the registry such as the
  19514. * creation and destruction of components, which will consequently be
  19515. * slightly slower (nothing that can be noticed in most cases).
  19516. *
  19517. * @note
  19518. * Pools of components that are owned by a group cannot be sorted anymore.
  19519. * The group takes the ownership of the pools and arrange components so as
  19520. * to iterate them as fast as possible.
  19521. *
  19522. * @tparam Owned Types of components owned by the group.
  19523. * @tparam Get Types of components observed by the group.
  19524. * @tparam Exclude Types of components used to filter the group.
  19525. * @return A newly created group.
  19526. */
  19527. template<typename... Owned, typename... Get, typename... Exclude>
  19528. [[nodiscard]] basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>, Owned...> group(get_t<Get...>, exclude_t<Exclude...> = {}) {
  19529. static_assert(sizeof...(Owned) + sizeof...(Get) > 0, "Exclusion-only groups are not supported");
  19530. static_assert(sizeof...(Owned) + sizeof...(Get) + sizeof...(Exclude) > 1, "Single component groups are not allowed");
  19531. using handler_type = group_handler<exclude_t<Exclude...>, get_t<std::remove_const_t<Get>...>, std::remove_const_t<Owned>...>;
  19532. const auto cpools = std::make_tuple(assure<std::remove_const_t<Owned>>()..., assure<std::remove_const_t<Get>>()...);
  19533. constexpr auto size = sizeof...(Owned) + sizeof...(Get) + sizeof...(Exclude);
  19534. handler_type *handler = nullptr;
  19535. if(auto it = std::find_if(groups.cbegin(), groups.cend(), [size](const auto &gdata) {
  19536. return gdata.size == size
  19537. && (gdata.owned(type_hash<std::remove_const_t<Owned>>::value()) && ...)
  19538. && (gdata.get(type_hash<std::remove_const_t<Get>>::value()) && ...)
  19539. && (gdata.exclude(type_hash<Exclude>::value()) && ...);
  19540. }); it != groups.cend())
  19541. {
  19542. handler = static_cast<handler_type *>(it->group.get());
  19543. }
  19544. if(!handler) {
  19545. group_data candidate = {
  19546. size,
  19547. { new handler_type{}, [](void *instance) { delete static_cast<handler_type *>(instance); } },
  19548. []([[maybe_unused]] const id_type ctype) ENTT_NOEXCEPT { return ((ctype == type_hash<std::remove_const_t<Owned>>::value()) || ...); },
  19549. []([[maybe_unused]] const id_type ctype) ENTT_NOEXCEPT { return ((ctype == type_hash<std::remove_const_t<Get>>::value()) || ...); },
  19550. []([[maybe_unused]] const id_type ctype) ENTT_NOEXCEPT { return ((ctype == type_hash<Exclude>::value()) || ...); },
  19551. };
  19552. handler = static_cast<handler_type *>(candidate.group.get());
  19553. const void *maybe_valid_if = nullptr;
  19554. const void *discard_if = nullptr;
  19555. if constexpr(sizeof...(Owned) == 0) {
  19556. groups.push_back(std::move(candidate));
  19557. } else {
  19558. ENTT_ASSERT(std::all_of(groups.cbegin(), groups.cend(), [size](const auto &gdata) {
  19559. const auto overlapping = (0u + ... + gdata.owned(type_hash<std::remove_const_t<Owned>>::value()));
  19560. const auto sz = overlapping + (0u + ... + gdata.get(type_hash<std::remove_const_t<Get>>::value())) + (0u + ... + gdata.exclude(type_hash<Exclude>::value()));
  19561. return !overlapping || ((sz == size) || (sz == gdata.size));
  19562. }), "Conflicting groups");
  19563. const auto next = std::find_if_not(groups.cbegin(), groups.cend(), [size](const auto &gdata) {
  19564. return !(0u + ... + gdata.owned(type_hash<std::remove_const_t<Owned>>::value())) || (size > gdata.size);
  19565. });
  19566. const auto prev = std::find_if(std::make_reverse_iterator(next), groups.crend(), [](const auto &gdata) {
  19567. return (0u + ... + gdata.owned(type_hash<std::remove_const_t<Owned>>::value()));
  19568. });
  19569. maybe_valid_if = (next == groups.cend() ? maybe_valid_if : next->group.get());
  19570. discard_if = (prev == groups.crend() ? discard_if : prev->group.get());
  19571. groups.insert(next, std::move(candidate));
  19572. }
  19573. (on_construct<std::remove_const_t<Owned>>().before(maybe_valid_if).template connect<&handler_type::template maybe_valid_if<std::remove_const_t<Owned>>>(*handler), ...);
  19574. (on_construct<std::remove_const_t<Get>>().before(maybe_valid_if).template connect<&handler_type::template maybe_valid_if<std::remove_const_t<Get>>>(*handler), ...);
  19575. (on_destroy<Exclude>().before(maybe_valid_if).template connect<&handler_type::template maybe_valid_if<Exclude>>(*handler), ...);
  19576. (on_destroy<std::remove_const_t<Owned>>().before(discard_if).template connect<&handler_type::discard_if>(*handler), ...);
  19577. (on_destroy<std::remove_const_t<Get>>().before(discard_if).template connect<&handler_type::discard_if>(*handler), ...);
  19578. (on_construct<Exclude>().before(discard_if).template connect<&handler_type::discard_if>(*handler), ...);
  19579. if constexpr(sizeof...(Owned) == 0) {
  19580. for(const auto entity: view<Owned..., Get...>(exclude<Exclude...>)) {
  19581. handler->current.emplace(entity);
  19582. }
  19583. } else {
  19584. // we cannot iterate backwards because we want to leave behind valid entities in case of owned types
  19585. for(auto *first = std::get<0>(cpools)->data(), *last = first + std::get<0>(cpools)->size(); first != last; ++first) {
  19586. handler->template maybe_valid_if<type_list_element_t<0, type_list<std::remove_const_t<Owned>...>>>(*this, *first);
  19587. }
  19588. }
  19589. }
  19590. return { handler->current, *std::get<storage_type<std::remove_const_t<Owned>> *>(cpools)..., *std::get<storage_type<std::remove_const_t<Get>> *>(cpools)... };
  19591. }
  19592. /**
  19593. * @brief Returns a group for the given components.
  19594. *
  19595. * @sa group
  19596. *
  19597. * @tparam Owned Types of components owned by the group.
  19598. * @tparam Get Types of components observed by the group.
  19599. * @tparam Exclude Types of components used to filter the group.
  19600. * @return A newly created group.
  19601. */
  19602. template<typename... Owned, typename... Get, typename... Exclude>
  19603. [[nodiscard]] basic_group<Entity, exclude_t<Exclude...>, get_t<std::add_const_t<Get>...>, std::add_const_t<Owned>...> group_if_exists(get_t<Get...>, exclude_t<Exclude...> = {}) const {
  19604. if(auto it = std::find_if(groups.cbegin(), groups.cend(), [](const auto &gdata) {
  19605. return gdata.size == (sizeof...(Owned) + sizeof...(Get) + sizeof...(Exclude))
  19606. && (gdata.owned(type_hash<std::remove_const_t<Owned>>::value()) && ...)
  19607. && (gdata.get(type_hash<std::remove_const_t<Get>>::value()) && ...)
  19608. && (gdata.exclude(type_hash<Exclude>::value()) && ...);
  19609. }); it == groups.cend())
  19610. {
  19611. return {};
  19612. } else {
  19613. using handler_type = group_handler<exclude_t<Exclude...>, get_t<std::remove_const_t<Get>...>, std::remove_const_t<Owned>...>;
  19614. return { static_cast<handler_type *>(it->group.get())->current, *pool_if_exists<std::remove_const_t<Owned>>()... , *pool_if_exists<std::remove_const_t<Get>>()... };
  19615. }
  19616. }
  19617. /**
  19618. * @brief Returns a group for the given components.
  19619. *
  19620. * @sa group
  19621. *
  19622. * @tparam Owned Types of components owned by the group.
  19623. * @tparam Exclude Types of components used to filter the group.
  19624. * @return A newly created group.
  19625. */
  19626. template<typename... Owned, typename... Exclude>
  19627. [[nodiscard]] basic_group<Entity, exclude_t<Exclude...>, get_t<>, Owned...> group(exclude_t<Exclude...> = {}) {
  19628. return group<Owned...>(get_t<>{}, exclude<Exclude...>);
  19629. }
  19630. /**
  19631. * @brief Returns a group for the given components.
  19632. *
  19633. * @sa group_if_exists
  19634. *
  19635. * @tparam Owned Types of components owned by the group.
  19636. * @tparam Exclude Types of components used to filter the group.
  19637. * @return A newly created group.
  19638. */
  19639. template<typename... Owned, typename... Exclude>
  19640. [[nodiscard]] basic_group<Entity, exclude_t<Exclude...>, get_t<>, std::add_const_t<Owned>...> group_if_exists(exclude_t<Exclude...> = {}) const {
  19641. return group_if_exists<std::add_const_t<Owned>...>(get_t<>{}, exclude<Exclude...>);
  19642. }
  19643. /**
  19644. * @brief Checks whether the given components belong to any group.
  19645. * @tparam Component Types of components in which one is interested.
  19646. * @return True if the pools of the given components are sortable, false
  19647. * otherwise.
  19648. */
  19649. template<typename... Component>
  19650. [[nodiscard]] bool sortable() const {
  19651. return std::none_of(groups.cbegin(), groups.cend(), [](auto &&gdata) { return (gdata.owned(type_hash<std::remove_const_t<Component>>::value()) || ...); });
  19652. }
  19653. /**
  19654. * @brief Checks whether a group can be sorted.
  19655. * @tparam Owned Types of components owned by the group.
  19656. * @tparam Get Types of components observed by the group.
  19657. * @tparam Exclude Types of components used to filter the group.
  19658. * @return True if the group can be sorted, false otherwise.
  19659. */
  19660. template<typename... Owned, typename... Get, typename... Exclude>
  19661. [[nodiscard]] bool sortable(const basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>, Owned...> &) ENTT_NOEXCEPT {
  19662. constexpr auto size = sizeof...(Owned) + sizeof...(Get) + sizeof...(Exclude);
  19663. return std::find_if(groups.cbegin(), groups.cend(), [size](const auto &gdata) {
  19664. return (0u + ... + gdata.owned(type_hash<std::remove_const_t<Owned>>::value())) && (size < gdata.size);
  19665. }) == groups.cend();
  19666. }
  19667. /**
  19668. * @brief Sorts the pool of entities for the given component.
  19669. *
  19670. * The order of the elements in a pool is highly affected by assignments
  19671. * of components to entities and deletions. Components are arranged to
  19672. * maximize the performance during iterations and users should not make any
  19673. * assumption on the order.<br/>
  19674. * This function can be used to impose an order to the elements in the pool
  19675. * of the given component. The order is kept valid until a component of the
  19676. * given type is assigned or removed from an entity.
  19677. *
  19678. * The comparison function object must return `true` if the first element
  19679. * is _less_ than the second one, `false` otherwise. The signature of the
  19680. * comparison function should be equivalent to one of the following:
  19681. *
  19682. * @code{.cpp}
  19683. * bool(const Entity, const Entity);
  19684. * bool(const Component &, const Component &);
  19685. * @endcode
  19686. *
  19687. * Moreover, the comparison function object shall induce a
  19688. * _strict weak ordering_ on the values.
  19689. *
  19690. * The sort function oject must offer a member function template
  19691. * `operator()` that accepts three arguments:
  19692. *
  19693. * * An iterator to the first element of the range to sort.
  19694. * * An iterator past the last element of the range to sort.
  19695. * * A comparison function to use to compare the elements.
  19696. *
  19697. * The comparison funtion object received by the sort function object hasn't
  19698. * necessarily the type of the one passed along with the other parameters to
  19699. * this member function.
  19700. *
  19701. * @warning
  19702. * Pools of components owned by a group cannot be sorted.
  19703. *
  19704. * @tparam Component Type of components to sort.
  19705. * @tparam Compare Type of comparison function object.
  19706. * @tparam Sort Type of sort function object.
  19707. * @tparam Args Types of arguments to forward to the sort function object.
  19708. * @param compare A valid comparison function object.
  19709. * @param algo A valid sort function object.
  19710. * @param args Arguments to forward to the sort function object, if any.
  19711. */
  19712. template<typename Component, typename Compare, typename Sort = std_sort, typename... Args>
  19713. void sort(Compare compare, Sort algo = Sort{}, Args &&... args) {
  19714. ENTT_ASSERT(sortable<Component>(), "Cannot sort owned storage");
  19715. assure<Component>()->sort(std::move(compare), std::move(algo), std::forward<Args>(args)...);
  19716. }
  19717. /**
  19718. * @brief Sorts two pools of components in the same way.
  19719. *
  19720. * The order of the elements in a pool is highly affected by assignments
  19721. * of components to entities and deletions. Components are arranged to
  19722. * maximize the performance during iterations and users should not make any
  19723. * assumption on the order.
  19724. *
  19725. * It happens that different pools of components must be sorted the same way
  19726. * because of runtime and/or performance constraints. This function can be
  19727. * used to order a pool of components according to the order between the
  19728. * entities in another pool of components.
  19729. *
  19730. * @b How @b it @b works
  19731. *
  19732. * Being `A` and `B` the two sets where `B` is the master (the one the order
  19733. * of which rules) and `A` is the slave (the one to sort), after a call to
  19734. * this function an iterator for `A` will return the entities according to
  19735. * the following rules:
  19736. *
  19737. * * All the entities in `A` that are also in `B` are returned first
  19738. * according to the order they have in `B`.
  19739. * * All the entities in `A` that are not in `B` are returned in no
  19740. * particular order after all the other entities.
  19741. *
  19742. * Any subsequent change to `B` won't affect the order in `A`.
  19743. *
  19744. * @warning
  19745. * Pools of components owned by a group cannot be sorted.
  19746. *
  19747. * @tparam To Type of components to sort.
  19748. * @tparam From Type of components to use to sort.
  19749. */
  19750. template<typename To, typename From>
  19751. void sort() {
  19752. ENTT_ASSERT(sortable<To>(), "Cannot sort owned storage");
  19753. assure<To>()->respect(*assure<From>());
  19754. }
  19755. /**
  19756. * @brief Visits an entity and returns the type info for its components.
  19757. *
  19758. * The signature of the function should be equivalent to the following:
  19759. *
  19760. * @code{.cpp}
  19761. * void(const type_info);
  19762. * @endcode
  19763. *
  19764. * Returned identifiers are those of the components owned by the entity.
  19765. *
  19766. * @sa type_info
  19767. *
  19768. * @warning
  19769. * It's not specified whether a component attached to or removed from the
  19770. * given entity during the visit is returned or not to the caller.
  19771. *
  19772. * @tparam Func Type of the function object to invoke.
  19773. * @param entity A valid entity identifier.
  19774. * @param func A valid function object.
  19775. */
  19776. template<typename Func>
  19777. void visit(entity_type entity, Func func) const {
  19778. for(auto pos = pools.size(); pos; --pos) {
  19779. if(const auto &pdata = pools[pos-1]; pdata.pool && pdata.pool->contains(entity)) {
  19780. func(pdata.poly->value_type());
  19781. }
  19782. }
  19783. }
  19784. /**
  19785. * @brief Visits a registry and returns the type info for its components.
  19786. *
  19787. * The signature of the function should be equivalent to the following:
  19788. *
  19789. * @code{.cpp}
  19790. * void(const type_info);
  19791. * @endcode
  19792. *
  19793. * Returned identifiers are those of the components managed by the registry.
  19794. *
  19795. * @sa type_info
  19796. *
  19797. * @warning
  19798. * It's not specified whether a component for which a pool is created during
  19799. * the visit is returned or not to the caller.
  19800. *
  19801. * @tparam Func Type of the function object to invoke.
  19802. * @param func A valid function object.
  19803. */
  19804. template<typename Func>
  19805. void visit(Func func) const {
  19806. for(auto pos = pools.size(); pos; --pos) {
  19807. if(const auto &pdata = pools[pos-1]; pdata.pool) {
  19808. func(pdata.poly->value_type());
  19809. }
  19810. }
  19811. }
  19812. /**
  19813. * @brief Binds an object to the context of the registry.
  19814. *
  19815. * If the value already exists it is overwritten, otherwise a new instance
  19816. * of the given type is created and initialized with the arguments provided.
  19817. *
  19818. * @tparam Type Type of object to set.
  19819. * @tparam Args Types of arguments to use to construct the object.
  19820. * @param args Parameters to use to initialize the value.
  19821. * @return A reference to the newly created object.
  19822. */
  19823. template<typename Type, typename... Args>
  19824. Type & set(Args &&... args) {
  19825. unset<Type>();
  19826. vars.emplace_back(std::in_place_type<Type>, std::forward<Args>(args)...);
  19827. return any_cast<Type &>(vars.back());
  19828. }
  19829. /**
  19830. * @brief Unsets a context variable if it exists.
  19831. * @tparam Type Type of object to set.
  19832. */
  19833. template<typename Type>
  19834. void unset() {
  19835. vars.erase(std::remove_if(vars.begin(), vars.end(), [type = type_id<Type>()](auto &&var) { return var.type() == type; }), vars.end());
  19836. }
  19837. /**
  19838. * @brief Binds an object to the context of the registry.
  19839. *
  19840. * In case the context doesn't contain the given object, the parameters
  19841. * provided are used to construct it.
  19842. *
  19843. * @tparam Type Type of object to set.
  19844. * @tparam Args Types of arguments to use to construct the object.
  19845. * @param args Parameters to use to initialize the object.
  19846. * @return A reference to the object in the context of the registry.
  19847. */
  19848. template<typename Type, typename... Args>
  19849. [[nodiscard]] Type & ctx_or_set(Args &&... args) {
  19850. auto *value = try_ctx<Type>();
  19851. return value ? *value : set<Type>(std::forward<Args>(args)...);
  19852. }
  19853. /**
  19854. * @brief Returns a pointer to an object in the context of the registry.
  19855. * @tparam Type Type of object to get.
  19856. * @return A pointer to the object if it exists in the context of the
  19857. * registry, a null pointer otherwise.
  19858. */
  19859. template<typename Type>
  19860. [[nodiscard]] std::add_const_t<Type> * try_ctx() const {
  19861. auto it = std::find_if(vars.cbegin(), vars.cend(), [type = type_id<Type>()](auto &&var) { return var.type() == type; });
  19862. return it == vars.cend() ? nullptr : any_cast<std::add_const_t<Type>>(&*it);
  19863. }
  19864. /*! @copydoc try_ctx */
  19865. template<typename Type>
  19866. [[nodiscard]] Type * try_ctx() {
  19867. auto it = std::find_if(vars.begin(), vars.end(), [type = type_id<Type>()](auto &&var) { return var.type() == type; });
  19868. return it == vars.end() ? nullptr : any_cast<Type>(&*it);
  19869. }
  19870. /**
  19871. * @brief Returns a reference to an object in the context of the registry.
  19872. *
  19873. * @warning
  19874. * Attempting to get a context variable that doesn't exist results in
  19875. * undefined behavior.
  19876. *
  19877. * @tparam Type Type of object to get.
  19878. * @return A valid reference to the object in the context of the registry.
  19879. */
  19880. template<typename Type>
  19881. [[nodiscard]] std::add_const_t<Type> & ctx() const {
  19882. auto it = std::find_if(vars.cbegin(), vars.cend(), [type = type_id<Type>()](auto &&var) { return var.type() == type; });
  19883. ENTT_ASSERT(it != vars.cend(), "Invalid instance");
  19884. return any_cast<std::add_const_t<Type> &>(*it);
  19885. }
  19886. /*! @copydoc ctx */
  19887. template<typename Type>
  19888. [[nodiscard]] Type & ctx() {
  19889. auto it = std::find_if(vars.begin(), vars.end(), [type = type_id<Type>()](auto &&var) { return var.type() == type; });
  19890. ENTT_ASSERT(it != vars.end(), "Invalid instance");
  19891. return any_cast<Type &>(*it);
  19892. }
  19893. /**
  19894. * @brief Visits a registry and returns the type info for its context
  19895. * variables.
  19896. *
  19897. * The signature of the function should be equivalent to the following:
  19898. *
  19899. * @code{.cpp}
  19900. * void(const type_info);
  19901. * @endcode
  19902. *
  19903. * Returned identifiers are those of the context variables currently set.
  19904. *
  19905. * @sa type_info
  19906. *
  19907. * @warning
  19908. * It's not specified whether a context variable created during the visit is
  19909. * returned or not to the caller.
  19910. *
  19911. * @tparam Func Type of the function object to invoke.
  19912. * @param func A valid function object.
  19913. */
  19914. template<typename Func>
  19915. void ctx(Func func) const {
  19916. for(auto pos = vars.size(); pos; --pos) {
  19917. func(vars[pos-1].type());
  19918. }
  19919. }
  19920. private:
  19921. std::vector<basic_any<0u>> vars{};
  19922. mutable std::vector<pool_data> pools{};
  19923. std::vector<group_data> groups{};
  19924. std::vector<entity_type> entities{};
  19925. entity_type free_list{tombstone};
  19926. };
  19927. }
  19928. #endif
  19929. // #include "entity/runtime_view.hpp"
  19930. #ifndef ENTT_ENTITY_RUNTIME_VIEW_HPP
  19931. #define ENTT_ENTITY_RUNTIME_VIEW_HPP
  19932. #include <iterator>
  19933. #include <vector>
  19934. #include <utility>
  19935. #include <algorithm>
  19936. #include <type_traits>
  19937. // #include "../config/config.h"
  19938. // #include "entity.hpp"
  19939. // #include "sparse_set.hpp"
  19940. // #include "fwd.hpp"
  19941. namespace entt {
  19942. /**
  19943. * @brief Runtime view.
  19944. *
  19945. * Runtime views iterate over those entities that have at least all the given
  19946. * components in their bags. During initialization, a runtime view looks at the
  19947. * number of entities available for each component and picks up a reference to
  19948. * the smallest set of candidate entities in order to get a performance boost
  19949. * when iterate.<br/>
  19950. * Order of elements during iterations are highly dependent on the order of the
  19951. * underlying data structures. See sparse_set and its specializations for more
  19952. * details.
  19953. *
  19954. * @b Important
  19955. *
  19956. * Iterators aren't invalidated if:
  19957. *
  19958. * * New instances of the given components are created and assigned to entities.
  19959. * * The entity currently pointed is modified (as an example, if one of the
  19960. * given components is removed from the entity to which the iterator points).
  19961. * * The entity currently pointed is destroyed.
  19962. *
  19963. * In all the other cases, modifying the pools of the given components in any
  19964. * way invalidates all the iterators and using them results in undefined
  19965. * behavior.
  19966. *
  19967. * @note
  19968. * Views share references to the underlying data structures of the registry that
  19969. * generated them. Therefore any change to the entities and to the components
  19970. * made by means of the registry are immediately reflected by the views, unless
  19971. * a pool was missing when the view was built (in this case, the view won't
  19972. * have a valid reference and won't be updated accordingly).
  19973. *
  19974. * @warning
  19975. * Lifetime of a view must not overcome that of the registry that generated it.
  19976. * In any other case, attempting to use a view results in undefined behavior.
  19977. *
  19978. * @tparam Entity A valid entity type (see entt_traits for more details).
  19979. */
  19980. template<typename Entity>
  19981. class basic_runtime_view final {
  19982. using basic_common_type = basic_sparse_set<Entity>;
  19983. using underlying_iterator = typename basic_common_type::iterator;
  19984. class view_iterator final {
  19985. [[nodiscard]] bool valid() const {
  19986. const auto entt = *it;
  19987. return (!stable_storage || (entt != tombstone))
  19988. && std::all_of(pools->begin()++, pools->end(), [entt](const auto *curr) { return curr->contains(entt); })
  19989. && std::none_of(filter->cbegin(), filter->cend(), [entt](const auto *curr) { return curr && curr->contains(entt); });
  19990. }
  19991. public:
  19992. using difference_type = typename underlying_iterator::difference_type;
  19993. using value_type = typename underlying_iterator::value_type;
  19994. using pointer = typename underlying_iterator::pointer;
  19995. using reference = typename underlying_iterator::reference;
  19996. using iterator_category = std::bidirectional_iterator_tag;
  19997. view_iterator() ENTT_NOEXCEPT = default;
  19998. view_iterator(const std::vector<const basic_common_type *> &cpools, const std::vector<const basic_common_type *> &ignore, underlying_iterator curr) ENTT_NOEXCEPT
  19999. : pools{&cpools},
  20000. filter{&ignore},
  20001. it{curr},
  20002. stable_storage{std::any_of(pools->cbegin(), pools->cend(), [](const basic_common_type *cpool) { return (cpool->policy() == deletion_policy::in_place); })}
  20003. {
  20004. if(it != (*pools)[0]->end() && !valid()) {
  20005. ++(*this);
  20006. }
  20007. }
  20008. view_iterator & operator++() {
  20009. while(++it != (*pools)[0]->end() && !valid());
  20010. return *this;
  20011. }
  20012. view_iterator operator++(int) {
  20013. view_iterator orig = *this;
  20014. return ++(*this), orig;
  20015. }
  20016. view_iterator & operator--() ENTT_NOEXCEPT {
  20017. while(--it != (*pools)[0]->begin() && !valid());
  20018. return *this;
  20019. }
  20020. view_iterator operator--(int) ENTT_NOEXCEPT {
  20021. view_iterator orig = *this;
  20022. return operator--(), orig;
  20023. }
  20024. [[nodiscard]] bool operator==(const view_iterator &other) const ENTT_NOEXCEPT {
  20025. return other.it == it;
  20026. }
  20027. [[nodiscard]] bool operator!=(const view_iterator &other) const ENTT_NOEXCEPT {
  20028. return !(*this == other);
  20029. }
  20030. [[nodiscard]] pointer operator->() const {
  20031. return it.operator->();
  20032. }
  20033. [[nodiscard]] reference operator*() const {
  20034. return *operator->();
  20035. }
  20036. private:
  20037. const std::vector<const basic_common_type *> *pools;
  20038. const std::vector<const basic_common_type *> *filter;
  20039. underlying_iterator it;
  20040. bool stable_storage;
  20041. };
  20042. [[nodiscard]] bool valid() const {
  20043. return !pools.empty() && pools.front();
  20044. }
  20045. public:
  20046. /*! @brief Underlying entity identifier. */
  20047. using entity_type = Entity;
  20048. /*! @brief Unsigned integer type. */
  20049. using size_type = std::size_t;
  20050. /*! @brief Bidirectional iterator type. */
  20051. using iterator = view_iterator;
  20052. /*! @brief Default constructor to use to create empty, invalid views. */
  20053. basic_runtime_view() ENTT_NOEXCEPT
  20054. : pools{},
  20055. filter{}
  20056. {}
  20057. /**
  20058. * @brief Constructs a runtime view from a set of storage classes.
  20059. * @param cpools The storage for the types to iterate.
  20060. * @param epools The storage for the types used to filter the view.
  20061. */
  20062. basic_runtime_view(std::vector<const basic_common_type *> cpools, std::vector<const basic_common_type *> epools) ENTT_NOEXCEPT
  20063. : pools{std::move(cpools)},
  20064. filter{std::move(epools)}
  20065. {
  20066. // brings the best candidate (if any) on front of the vector
  20067. std::rotate(pools.begin(), std::min_element(pools.begin(), pools.end(), [](const auto *lhs, const auto *rhs) {
  20068. return (!lhs && rhs) || (lhs && rhs && lhs->size() < rhs->size());
  20069. }), pools.end());
  20070. }
  20071. /**
  20072. * @brief Estimates the number of entities iterated by the view.
  20073. * @return Estimated number of entities iterated by the view.
  20074. */
  20075. [[nodiscard]] size_type size_hint() const {
  20076. return valid() ? pools.front()->size() : size_type{};
  20077. }
  20078. /**
  20079. * @brief Returns an iterator to the first entity that has the given
  20080. * components.
  20081. *
  20082. * The returned iterator points to the first entity that has the given
  20083. * components. If the view is empty, the returned iterator will be equal to
  20084. * `end()`.
  20085. *
  20086. * @return An iterator to the first entity that has the given components.
  20087. */
  20088. [[nodiscard]] iterator begin() const {
  20089. return valid() ? iterator{pools, filter, pools[0]->begin()} : iterator{};
  20090. }
  20091. /**
  20092. * @brief Returns an iterator that is past the last entity that has the
  20093. * given components.
  20094. *
  20095. * The returned iterator points to the entity following the last entity that
  20096. * has the given components. Attempting to dereference the returned iterator
  20097. * results in undefined behavior.
  20098. *
  20099. * @return An iterator to the entity following the last entity that has the
  20100. * given components.
  20101. */
  20102. [[nodiscard]] iterator end() const {
  20103. return valid() ? iterator{pools, filter, pools[0]->end()} : iterator{};
  20104. }
  20105. /**
  20106. * @brief Checks if a view contains an entity.
  20107. * @param entt A valid entity identifier.
  20108. * @return True if the view contains the given entity, false otherwise.
  20109. */
  20110. [[nodiscard]] bool contains(const entity_type entt) const {
  20111. return valid() && std::all_of(pools.cbegin(), pools.cend(), [entt](const auto *curr) { return curr->contains(entt); })
  20112. && std::none_of(filter.cbegin(), filter.cend(), [entt](const auto *curr) { return curr && curr->contains(entt); });
  20113. }
  20114. /**
  20115. * @brief Iterates entities and applies the given function object to them.
  20116. *
  20117. * The function object is invoked for each entity. It is provided only with
  20118. * the entity itself. To get the components, users can use the registry with
  20119. * which the view was built.<br/>
  20120. * The signature of the function should be equivalent to the following:
  20121. *
  20122. * @code{.cpp}
  20123. * void(const entity_type);
  20124. * @endcode
  20125. *
  20126. * @tparam Func Type of the function object to invoke.
  20127. * @param func A valid function object.
  20128. */
  20129. template<typename Func>
  20130. void each(Func func) const {
  20131. for(const auto entity: *this) {
  20132. func(entity);
  20133. }
  20134. }
  20135. private:
  20136. std::vector<const basic_common_type *> pools;
  20137. std::vector<const basic_common_type *> filter;
  20138. };
  20139. }
  20140. #endif
  20141. // #include "entity/snapshot.hpp"
  20142. #ifndef ENTT_ENTITY_SNAPSHOT_HPP
  20143. #define ENTT_ENTITY_SNAPSHOT_HPP
  20144. #include <array>
  20145. #include <cstddef>
  20146. #include <iterator>
  20147. #include <tuple>
  20148. #include <type_traits>
  20149. #include <unordered_map>
  20150. #include <utility>
  20151. #include <vector>
  20152. // #include "../config/config.h"
  20153. // #include "../core/type_traits.hpp"
  20154. // #include "entity.hpp"
  20155. // #include "fwd.hpp"
  20156. // #include "registry.hpp"
  20157. namespace entt {
  20158. /**
  20159. * @brief Utility class to create snapshots from a registry.
  20160. *
  20161. * A _snapshot_ can be either a dump of the entire registry or a narrower
  20162. * selection of components of interest.<br/>
  20163. * This type can be used in both cases if provided with a correctly configured
  20164. * output archive.
  20165. *
  20166. * @tparam Entity A valid entity type (see entt_traits for more details).
  20167. */
  20168. template<typename Entity>
  20169. class basic_snapshot {
  20170. using traits_type = entt_traits<Entity>;
  20171. template<typename Component, typename Archive, typename It>
  20172. void get(Archive &archive, std::size_t sz, It first, It last) const {
  20173. const auto view = reg->template view<std::add_const_t<Component>>();
  20174. archive(typename traits_type::entity_type(sz));
  20175. while(first != last) {
  20176. const auto entt = *(first++);
  20177. if(reg->template all_of<Component>(entt)) {
  20178. std::apply(archive, std::tuple_cat(std::make_tuple(entt), view.get(entt)));
  20179. }
  20180. }
  20181. }
  20182. template<typename... Component, typename Archive, typename It, std::size_t... Index>
  20183. void component(Archive &archive, It first, It last, std::index_sequence<Index...>) const {
  20184. std::array<std::size_t, sizeof...(Index)> size{};
  20185. auto begin = first;
  20186. while(begin != last) {
  20187. const auto entt = *(begin++);
  20188. ((reg->template all_of<Component>(entt) ? ++size[Index] : size[Index]), ...);
  20189. }
  20190. (get<Component>(archive, size[Index], first, last), ...);
  20191. }
  20192. public:
  20193. /*! @brief Underlying entity identifier. */
  20194. using entity_type = Entity;
  20195. /**
  20196. * @brief Constructs an instance that is bound to a given registry.
  20197. * @param source A valid reference to a registry.
  20198. */
  20199. basic_snapshot(const basic_registry<entity_type> &source) ENTT_NOEXCEPT
  20200. : reg{&source}
  20201. {}
  20202. /*! @brief Default move constructor. */
  20203. basic_snapshot(basic_snapshot &&) = default;
  20204. /*! @brief Default move assignment operator. @return This snapshot. */
  20205. basic_snapshot & operator=(basic_snapshot &&) = default;
  20206. /**
  20207. * @brief Puts aside all the entities from the underlying registry.
  20208. *
  20209. * Entities are serialized along with their versions. Destroyed entities are
  20210. * taken in consideration as well by this function.
  20211. *
  20212. * @tparam Archive Type of output archive.
  20213. * @param archive A valid reference to an output archive.
  20214. * @return An object of this type to continue creating the snapshot.
  20215. */
  20216. template<typename Archive>
  20217. const basic_snapshot & entities(Archive &archive) const {
  20218. const auto sz = reg->size();
  20219. archive(typename traits_type::entity_type(sz));
  20220. for(auto first = reg->data(), last = first + sz; first != last; ++first) {
  20221. archive(*first);
  20222. }
  20223. archive(reg->released());
  20224. return *this;
  20225. }
  20226. /**
  20227. * @brief Puts aside the given components.
  20228. *
  20229. * Each instance is serialized together with the entity to which it belongs.
  20230. * Entities are serialized along with their versions.
  20231. *
  20232. * @tparam Component Types of components to serialize.
  20233. * @tparam Archive Type of output archive.
  20234. * @param archive A valid reference to an output archive.
  20235. * @return An object of this type to continue creating the snapshot.
  20236. */
  20237. template<typename... Component, typename Archive>
  20238. const basic_snapshot & component(Archive &archive) const {
  20239. if constexpr(sizeof...(Component) == 1u) {
  20240. const auto view = reg->template view<const Component...>();
  20241. (component<Component>(archive, view.data(), view.data() + view.size()), ...);
  20242. return *this;
  20243. } else {
  20244. (component<Component>(archive), ...);
  20245. return *this;
  20246. }
  20247. }
  20248. /**
  20249. * @brief Puts aside the given components for the entities in a range.
  20250. *
  20251. * Each instance is serialized together with the entity to which it belongs.
  20252. * Entities are serialized along with their versions.
  20253. *
  20254. * @tparam Component Types of components to serialize.
  20255. * @tparam Archive Type of output archive.
  20256. * @tparam It Type of input iterator.
  20257. * @param archive A valid reference to an output archive.
  20258. * @param first An iterator to the first element of the range to serialize.
  20259. * @param last An iterator past the last element of the range to serialize.
  20260. * @return An object of this type to continue creating the snapshot.
  20261. */
  20262. template<typename... Component, typename Archive, typename It>
  20263. const basic_snapshot & component(Archive &archive, It first, It last) const {
  20264. component<Component...>(archive, first, last, std::index_sequence_for<Component...>{});
  20265. return *this;
  20266. }
  20267. private:
  20268. const basic_registry<entity_type> *reg;
  20269. };
  20270. /**
  20271. * @brief Utility class to restore a snapshot as a whole.
  20272. *
  20273. * A snapshot loader requires that the destination registry be empty and loads
  20274. * all the data at once while keeping intact the identifiers that the entities
  20275. * originally had.<br/>
  20276. * An example of use is the implementation of a save/restore utility.
  20277. *
  20278. * @tparam Entity A valid entity type (see entt_traits for more details).
  20279. */
  20280. template<typename Entity>
  20281. class basic_snapshot_loader {
  20282. using traits_type = entt_traits<Entity>;
  20283. template<typename Type, typename Archive>
  20284. void assign(Archive &archive) const {
  20285. typename traits_type::entity_type length{};
  20286. archive(length);
  20287. entity_type entt{};
  20288. if constexpr(std::tuple_size_v<decltype(reg->template view<Type>().get({}))> == 0) {
  20289. while(length--) {
  20290. archive(entt);
  20291. const auto entity = reg->valid(entt) ? entt : reg->create(entt);
  20292. ENTT_ASSERT(entity == entt, "Entity not available for use");
  20293. reg->template emplace<Type>(entity);
  20294. }
  20295. } else {
  20296. Type instance{};
  20297. while(length--) {
  20298. archive(entt, instance);
  20299. const auto entity = reg->valid(entt) ? entt : reg->create(entt);
  20300. ENTT_ASSERT(entity == entt, "Entity not available for use");
  20301. reg->template emplace<Type>(entity, std::move(instance));
  20302. }
  20303. }
  20304. }
  20305. public:
  20306. /*! @brief Underlying entity identifier. */
  20307. using entity_type = Entity;
  20308. /**
  20309. * @brief Constructs an instance that is bound to a given registry.
  20310. * @param source A valid reference to a registry.
  20311. */
  20312. basic_snapshot_loader(basic_registry<entity_type> &source) ENTT_NOEXCEPT
  20313. : reg{&source}
  20314. {
  20315. // restoring a snapshot as a whole requires a clean registry
  20316. ENTT_ASSERT(reg->empty(), "Registry must be empty");
  20317. }
  20318. /*! @brief Default move constructor. */
  20319. basic_snapshot_loader(basic_snapshot_loader &&) = default;
  20320. /*! @brief Default move assignment operator. @return This loader. */
  20321. basic_snapshot_loader & operator=(basic_snapshot_loader &&) = default;
  20322. /**
  20323. * @brief Restores entities that were in use during serialization.
  20324. *
  20325. * This function restores the entities that were in use during serialization
  20326. * and gives them the versions they originally had.
  20327. *
  20328. * @tparam Archive Type of input archive.
  20329. * @param archive A valid reference to an input archive.
  20330. * @return A valid loader to continue restoring data.
  20331. */
  20332. template<typename Archive>
  20333. const basic_snapshot_loader & entities(Archive &archive) const {
  20334. typename traits_type::entity_type length{};
  20335. archive(length);
  20336. std::vector<entity_type> all(length);
  20337. for(decltype(length) pos{}; pos < length; ++pos) {
  20338. archive(all[pos]);
  20339. }
  20340. entity_type destroyed;
  20341. archive(destroyed);
  20342. reg->assign(all.cbegin(), all.cend(), destroyed);
  20343. return *this;
  20344. }
  20345. /**
  20346. * @brief Restores components and assigns them to the right entities.
  20347. *
  20348. * The template parameter list must be exactly the same used during
  20349. * serialization. In the event that the entity to which the component is
  20350. * assigned doesn't exist yet, the loader will take care to create it with
  20351. * the version it originally had.
  20352. *
  20353. * @tparam Component Types of components to restore.
  20354. * @tparam Archive Type of input archive.
  20355. * @param archive A valid reference to an input archive.
  20356. * @return A valid loader to continue restoring data.
  20357. */
  20358. template<typename... Component, typename Archive>
  20359. const basic_snapshot_loader & component(Archive &archive) const {
  20360. (assign<Component>(archive), ...);
  20361. return *this;
  20362. }
  20363. /**
  20364. * @brief Destroys those entities that have no components.
  20365. *
  20366. * In case all the entities were serialized but only part of the components
  20367. * was saved, it could happen that some of the entities have no components
  20368. * once restored.<br/>
  20369. * This functions helps to identify and destroy those entities.
  20370. *
  20371. * @return A valid loader to continue restoring data.
  20372. */
  20373. const basic_snapshot_loader & orphans() const {
  20374. reg->orphans([this](const auto entt) {
  20375. reg->release(entt);
  20376. });
  20377. return *this;
  20378. }
  20379. private:
  20380. basic_registry<entity_type> *reg;
  20381. };
  20382. /**
  20383. * @brief Utility class for _continuous loading_.
  20384. *
  20385. * A _continuous loader_ is designed to load data from a source registry to a
  20386. * (possibly) non-empty destination. The loader can accommodate in a registry
  20387. * more than one snapshot in a sort of _continuous loading_ that updates the
  20388. * destination one step at a time.<br/>
  20389. * Identifiers that entities originally had are not transferred to the target.
  20390. * Instead, the loader maps remote identifiers to local ones while restoring a
  20391. * snapshot.<br/>
  20392. * An example of use is the implementation of a client-server applications with
  20393. * the requirement of transferring somehow parts of the representation side to
  20394. * side.
  20395. *
  20396. * @tparam Entity A valid entity type (see entt_traits for more details).
  20397. */
  20398. template<typename Entity>
  20399. class basic_continuous_loader {
  20400. using traits_type = entt_traits<Entity>;
  20401. void destroy(Entity entt) {
  20402. if(const auto it = remloc.find(entt); it == remloc.cend()) {
  20403. const auto local = reg->create();
  20404. remloc.emplace(entt, std::make_pair(local, true));
  20405. reg->destroy(local);
  20406. }
  20407. }
  20408. void restore(Entity entt) {
  20409. const auto it = remloc.find(entt);
  20410. if(it == remloc.cend()) {
  20411. const auto local = reg->create();
  20412. remloc.emplace(entt, std::make_pair(local, true));
  20413. } else {
  20414. if(!reg->valid(remloc[entt].first)) {
  20415. remloc[entt].first = reg->create();
  20416. }
  20417. // set the dirty flag
  20418. remloc[entt].second = true;
  20419. }
  20420. }
  20421. template<typename Container>
  20422. auto update(int, Container &container)
  20423. -> decltype(typename Container::mapped_type{}, void()) {
  20424. // map like container
  20425. Container other;
  20426. for(auto &&pair: container) {
  20427. using first_type = std::remove_const_t<typename std::decay_t<decltype(pair)>::first_type>;
  20428. using second_type = typename std::decay_t<decltype(pair)>::second_type;
  20429. if constexpr(std::is_same_v<first_type, entity_type> && std::is_same_v<second_type, entity_type>) {
  20430. other.emplace(map(pair.first), map(pair.second));
  20431. } else if constexpr(std::is_same_v<first_type, entity_type>) {
  20432. other.emplace(map(pair.first), std::move(pair.second));
  20433. } else {
  20434. static_assert(std::is_same_v<second_type, entity_type>, "Neither the key nor the value are of entity type");
  20435. other.emplace(std::move(pair.first), map(pair.second));
  20436. }
  20437. }
  20438. std::swap(container, other);
  20439. }
  20440. template<typename Container>
  20441. auto update(char, Container &container)
  20442. -> decltype(typename Container::value_type{}, void()) {
  20443. // vector like container
  20444. static_assert(std::is_same_v<typename Container::value_type, entity_type>, "Invalid value type");
  20445. for(auto &&entt: container) {
  20446. entt = map(entt);
  20447. }
  20448. }
  20449. template<typename Other, typename Type, typename Member>
  20450. void update([[maybe_unused]] Other &instance, [[maybe_unused]] Member Type:: *member) {
  20451. if constexpr(!std::is_same_v<Other, Type>) {
  20452. return;
  20453. } else if constexpr(std::is_same_v<Member, entity_type>) {
  20454. instance.*member = map(instance.*member);
  20455. } else {
  20456. // maybe a container? let's try...
  20457. update(0, instance.*member);
  20458. }
  20459. }
  20460. template<typename Component>
  20461. void remove_if_exists() {
  20462. for(auto &&ref: remloc) {
  20463. const auto local = ref.second.first;
  20464. if(reg->valid(local)) {
  20465. reg->template remove<Component>(local);
  20466. }
  20467. }
  20468. }
  20469. template<typename Other, typename Archive, typename... Type, typename... Member>
  20470. void assign(Archive &archive, [[maybe_unused]] Member Type:: *... member) {
  20471. typename traits_type::entity_type length{};
  20472. archive(length);
  20473. entity_type entt{};
  20474. if constexpr(std::tuple_size_v<decltype(reg->template view<Other>().get({}))> == 0) {
  20475. while(length--) {
  20476. archive(entt);
  20477. restore(entt);
  20478. reg->template emplace_or_replace<Other>(map(entt));
  20479. }
  20480. } else {
  20481. Other instance{};
  20482. while(length--) {
  20483. archive(entt, instance);
  20484. (update(instance, member), ...);
  20485. restore(entt);
  20486. reg->template emplace_or_replace<Other>(map(entt), std::move(instance));
  20487. }
  20488. }
  20489. }
  20490. public:
  20491. /*! @brief Underlying entity identifier. */
  20492. using entity_type = Entity;
  20493. /**
  20494. * @brief Constructs an instance that is bound to a given registry.
  20495. * @param source A valid reference to a registry.
  20496. */
  20497. basic_continuous_loader(basic_registry<entity_type> &source) ENTT_NOEXCEPT
  20498. : reg{&source}
  20499. {}
  20500. /*! @brief Default move constructor. */
  20501. basic_continuous_loader(basic_continuous_loader &&) = default;
  20502. /*! @brief Default move assignment operator. @return This loader. */
  20503. basic_continuous_loader & operator=(basic_continuous_loader &&) = default;
  20504. /**
  20505. * @brief Restores entities that were in use during serialization.
  20506. *
  20507. * This function restores the entities that were in use during serialization
  20508. * and creates local counterparts for them if required.
  20509. *
  20510. * @tparam Archive Type of input archive.
  20511. * @param archive A valid reference to an input archive.
  20512. * @return A non-const reference to this loader.
  20513. */
  20514. template<typename Archive>
  20515. basic_continuous_loader & entities(Archive &archive) {
  20516. typename traits_type::entity_type length{};
  20517. entity_type entt{};
  20518. archive(length);
  20519. for(decltype(length) pos{}; pos < length; ++pos) {
  20520. archive(entt);
  20521. if(const auto entity = traits_type::to_entity(entt); entity == pos) {
  20522. restore(entt);
  20523. } else {
  20524. destroy(entt);
  20525. }
  20526. }
  20527. // discards the head of the list of destroyed entities
  20528. archive(entt);
  20529. return *this;
  20530. }
  20531. /**
  20532. * @brief Restores components and assigns them to the right entities.
  20533. *
  20534. * The template parameter list must be exactly the same used during
  20535. * serialization. In the event that the entity to which the component is
  20536. * assigned doesn't exist yet, the loader will take care to create a local
  20537. * counterpart for it.<br/>
  20538. * Members can be either data members of type entity_type or containers of
  20539. * entities. In both cases, the loader will visit them and update the
  20540. * entities by replacing each one with its local counterpart.
  20541. *
  20542. * @tparam Component Type of component to restore.
  20543. * @tparam Archive Type of input archive.
  20544. * @tparam Type Types of components to update with local counterparts.
  20545. * @tparam Member Types of members to update with their local counterparts.
  20546. * @param archive A valid reference to an input archive.
  20547. * @param member Members to update with their local counterparts.
  20548. * @return A non-const reference to this loader.
  20549. */
  20550. template<typename... Component, typename Archive, typename... Type, typename... Member>
  20551. basic_continuous_loader & component(Archive &archive, Member Type:: *... member) {
  20552. (remove_if_exists<Component>(), ...);
  20553. (assign<Component>(archive, member...), ...);
  20554. return *this;
  20555. }
  20556. /**
  20557. * @brief Helps to purge entities that no longer have a conterpart.
  20558. *
  20559. * Users should invoke this member function after restoring each snapshot,
  20560. * unless they know exactly what they are doing.
  20561. *
  20562. * @return A non-const reference to this loader.
  20563. */
  20564. basic_continuous_loader & shrink() {
  20565. auto it = remloc.begin();
  20566. while(it != remloc.cend()) {
  20567. const auto local = it->second.first;
  20568. bool &dirty = it->second.second;
  20569. if(dirty) {
  20570. dirty = false;
  20571. ++it;
  20572. } else {
  20573. if(reg->valid(local)) {
  20574. reg->destroy(local);
  20575. }
  20576. it = remloc.erase(it);
  20577. }
  20578. }
  20579. return *this;
  20580. }
  20581. /**
  20582. * @brief Destroys those entities that have no components.
  20583. *
  20584. * In case all the entities were serialized but only part of the components
  20585. * was saved, it could happen that some of the entities have no components
  20586. * once restored.<br/>
  20587. * This functions helps to identify and destroy those entities.
  20588. *
  20589. * @return A non-const reference to this loader.
  20590. */
  20591. basic_continuous_loader & orphans() {
  20592. reg->orphans([this](const auto entt) {
  20593. reg->release(entt);
  20594. });
  20595. return *this;
  20596. }
  20597. /**
  20598. * @brief Tests if a loader knows about a given entity.
  20599. * @param entt An entity identifier.
  20600. * @return True if `entity` is managed by the loader, false otherwise.
  20601. */
  20602. [[nodiscard]] bool contains(entity_type entt) const ENTT_NOEXCEPT {
  20603. return (remloc.find(entt) != remloc.cend());
  20604. }
  20605. /**
  20606. * @brief Returns the identifier to which an entity refers.
  20607. * @param entt An entity identifier.
  20608. * @return The local identifier if any, the null entity otherwise.
  20609. */
  20610. [[nodiscard]] entity_type map(entity_type entt) const ENTT_NOEXCEPT {
  20611. const auto it = remloc.find(entt);
  20612. entity_type other = null;
  20613. if(it != remloc.cend()) {
  20614. other = it->second.first;
  20615. }
  20616. return other;
  20617. }
  20618. private:
  20619. std::unordered_map<entity_type, std::pair<entity_type, bool>> remloc;
  20620. basic_registry<entity_type> *reg;
  20621. };
  20622. }
  20623. #endif
  20624. // #include "entity/sparse_set.hpp"
  20625. #ifndef ENTT_ENTITY_SPARSE_SET_HPP
  20626. #define ENTT_ENTITY_SPARSE_SET_HPP
  20627. #include <cstddef>
  20628. #include <iterator>
  20629. #include <memory>
  20630. #include <type_traits>
  20631. #include <utility>
  20632. // #include "../config/config.h"
  20633. // #include "../core/algorithm.hpp"
  20634. // #include "../core/fwd.hpp"
  20635. // #include "entity.hpp"
  20636. // #include "fwd.hpp"
  20637. namespace entt {
  20638. /*! @brief Sparse set deletion policy. */
  20639. enum class deletion_policy: std::uint8_t {
  20640. /*! @brief Swap-and-pop deletion policy. */
  20641. swap_and_pop = 0u,
  20642. /*! @brief In-place deletion policy. */
  20643. in_place = 1u
  20644. };
  20645. /**
  20646. * @brief Basic sparse set implementation.
  20647. *
  20648. * Sparse set or packed array or whatever is the name users give it.<br/>
  20649. * Two arrays: an _external_ one and an _internal_ one; a _sparse_ one and a
  20650. * _packed_ one; one used for direct access through contiguous memory, the other
  20651. * one used to get the data through an extra level of indirection.<br/>
  20652. * This is largely used by the registry to offer users the fastest access ever
  20653. * to the components. Views and groups in general are almost entirely designed
  20654. * around sparse sets.
  20655. *
  20656. * This type of data structure is widely documented in the literature and on the
  20657. * web. This is nothing more than a customized implementation suitable for the
  20658. * purpose of the framework.
  20659. *
  20660. * @note
  20661. * Internal data structures arrange elements to maximize performance. There are
  20662. * no guarantees that entities are returned in the insertion order when iterate
  20663. * a sparse set. Do not make assumption on the order in any case.
  20664. *
  20665. * @tparam Entity A valid entity type (see entt_traits for more details).
  20666. * @tparam Allocator Type of allocator used to manage memory and elements.
  20667. */
  20668. template<typename Entity, typename Allocator>
  20669. class basic_sparse_set {
  20670. static constexpr auto growth_factor = 1.5;
  20671. static constexpr auto sparse_page = ENTT_SPARSE_PAGE;
  20672. using traits_type = entt_traits<Entity>;
  20673. using alloc_traits = typename std::allocator_traits<Allocator>::template rebind_traits<Entity>;
  20674. using alloc_pointer = typename alloc_traits::pointer;
  20675. using alloc_const_pointer = typename alloc_traits::const_pointer;
  20676. using bucket_alloc_traits = typename std::allocator_traits<Allocator>::template rebind_traits<alloc_pointer>;
  20677. using bucket_alloc_pointer = typename bucket_alloc_traits::pointer;
  20678. static_assert(alloc_traits::propagate_on_container_move_assignment::value);
  20679. static_assert(bucket_alloc_traits::propagate_on_container_move_assignment::value);
  20680. struct sparse_set_iterator final {
  20681. using difference_type = typename traits_type::difference_type;
  20682. using value_type = Entity;
  20683. using pointer = const value_type *;
  20684. using reference = const value_type &;
  20685. using iterator_category = std::random_access_iterator_tag;
  20686. sparse_set_iterator() ENTT_NOEXCEPT = default;
  20687. sparse_set_iterator(const alloc_const_pointer *ref, const difference_type idx) ENTT_NOEXCEPT
  20688. : packed{ref},
  20689. index{idx}
  20690. {}
  20691. sparse_set_iterator & operator++() ENTT_NOEXCEPT {
  20692. return --index, *this;
  20693. }
  20694. sparse_set_iterator operator++(int) ENTT_NOEXCEPT {
  20695. iterator orig = *this;
  20696. return ++(*this), orig;
  20697. }
  20698. sparse_set_iterator & operator--() ENTT_NOEXCEPT {
  20699. return ++index, *this;
  20700. }
  20701. sparse_set_iterator operator--(int) ENTT_NOEXCEPT {
  20702. sparse_set_iterator orig = *this;
  20703. return operator--(), orig;
  20704. }
  20705. sparse_set_iterator & operator+=(const difference_type value) ENTT_NOEXCEPT {
  20706. index -= value;
  20707. return *this;
  20708. }
  20709. sparse_set_iterator operator+(const difference_type value) const ENTT_NOEXCEPT {
  20710. sparse_set_iterator copy = *this;
  20711. return (copy += value);
  20712. }
  20713. sparse_set_iterator & operator-=(const difference_type value) ENTT_NOEXCEPT {
  20714. return (*this += -value);
  20715. }
  20716. sparse_set_iterator operator-(const difference_type value) const ENTT_NOEXCEPT {
  20717. return (*this + -value);
  20718. }
  20719. difference_type operator-(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  20720. return other.index - index;
  20721. }
  20722. [[nodiscard]] reference operator[](const difference_type value) const {
  20723. const auto pos = size_type(index-value-1u);
  20724. return (*packed)[pos];
  20725. }
  20726. [[nodiscard]] bool operator==(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  20727. return other.index == index;
  20728. }
  20729. [[nodiscard]] bool operator!=(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  20730. return !(*this == other);
  20731. }
  20732. [[nodiscard]] bool operator<(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  20733. return index > other.index;
  20734. }
  20735. [[nodiscard]] bool operator>(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  20736. return index < other.index;
  20737. }
  20738. [[nodiscard]] bool operator<=(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  20739. return !(*this > other);
  20740. }
  20741. [[nodiscard]] bool operator>=(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  20742. return !(*this < other);
  20743. }
  20744. [[nodiscard]] pointer operator->() const {
  20745. const auto pos = size_type(index-1u);
  20746. return std::addressof((*packed)[pos]);
  20747. }
  20748. [[nodiscard]] reference operator*() const {
  20749. return *operator->();
  20750. }
  20751. private:
  20752. const alloc_const_pointer *packed;
  20753. difference_type index;
  20754. };
  20755. [[nodiscard]] static auto page(const Entity entt) ENTT_NOEXCEPT {
  20756. return size_type{traits_type::to_entity(entt) / sparse_page};
  20757. }
  20758. [[nodiscard]] static auto offset(const Entity entt) ENTT_NOEXCEPT {
  20759. return size_type{traits_type::to_entity(entt) & (sparse_page - 1)};
  20760. }
  20761. [[nodiscard]] auto assure_page(const std::size_t idx) {
  20762. if(!(idx < bucket)) {
  20763. const size_type sz = idx + 1u;
  20764. const auto mem = bucket_alloc_traits::allocate(bucket_allocator, sz);
  20765. std::uninitialized_value_construct(mem + bucket, mem + sz);
  20766. std::uninitialized_copy(sparse, sparse + bucket, mem);
  20767. std::destroy(sparse, sparse + bucket);
  20768. bucket_alloc_traits::deallocate(bucket_allocator, sparse, bucket);
  20769. sparse = mem;
  20770. bucket = sz;
  20771. }
  20772. if(!sparse[idx]) {
  20773. sparse[idx] = alloc_traits::allocate(allocator, sparse_page);
  20774. std::uninitialized_fill(sparse[idx], sparse[idx] + sparse_page, null);
  20775. }
  20776. return sparse[idx];
  20777. }
  20778. void resize_packed(const std::size_t req) {
  20779. ENTT_ASSERT((req != reserved) && !(req < count), "Invalid request");
  20780. const auto mem = alloc_traits::allocate(allocator, req);
  20781. std::uninitialized_copy(packed, packed + count, mem);
  20782. std::uninitialized_fill(mem + count, mem + req, tombstone);
  20783. std::destroy(packed, packed + reserved);
  20784. alloc_traits::deallocate(allocator, packed, reserved);
  20785. packed = mem;
  20786. reserved = req;
  20787. }
  20788. void release_memory() {
  20789. if(packed) {
  20790. for(size_type pos{}; pos < bucket; ++pos) {
  20791. if(sparse[pos]) {
  20792. std::destroy(sparse[pos], sparse[pos] + sparse_page);
  20793. alloc_traits::deallocate(allocator, sparse[pos], sparse_page);
  20794. }
  20795. }
  20796. std::destroy(packed, packed + reserved);
  20797. std::destroy(sparse, sparse + bucket);
  20798. alloc_traits::deallocate(allocator, packed, reserved);
  20799. bucket_alloc_traits::deallocate(bucket_allocator, sparse, bucket);
  20800. }
  20801. }
  20802. protected:
  20803. /**
  20804. * @brief Swaps two entities in the internal packed array.
  20805. * @param lhs A valid position of an entity within storage.
  20806. * @param rhs A valid position of an entity within storage.
  20807. */
  20808. virtual void swap_at([[maybe_unused]] const std::size_t lhs, [[maybe_unused]] const std::size_t rhs) {}
  20809. /**
  20810. * @brief Moves an entity in the internal packed array.
  20811. * @param from A valid position of an entity within storage.
  20812. * @param to A valid position of an entity within storage.
  20813. */
  20814. virtual void move_and_pop([[maybe_unused]] const std::size_t from, [[maybe_unused]] const std::size_t to) {}
  20815. /**
  20816. * @brief Attempts to erase an entity from the internal packed array.
  20817. * @param entt A valid entity identifier.
  20818. * @param ud Optional user data that are forwarded as-is to derived classes.
  20819. */
  20820. virtual void swap_and_pop(const Entity entt, [[maybe_unused]] void *ud) {
  20821. auto &ref = sparse[page(entt)][offset(entt)];
  20822. const auto pos = size_type{traits_type::to_entity(ref)};
  20823. ENTT_ASSERT(packed[pos] == entt, "Invalid entity identifier");
  20824. auto &last = packed[--count];
  20825. packed[pos] = last;
  20826. sparse[page(last)][offset(last)] = ref;
  20827. // lazy self-assignment guard
  20828. ref = null;
  20829. // unnecessary but it helps to detect nasty bugs
  20830. ENTT_ASSERT((last = tombstone, true), "");
  20831. }
  20832. /**
  20833. * @brief Attempts to erase an entity from the internal packed array.
  20834. * @param entt A valid entity identifier.
  20835. * @param ud Optional user data that are forwarded as-is to derived classes.
  20836. */
  20837. virtual void in_place_pop(const Entity entt, [[maybe_unused]] void *ud) {
  20838. auto &ref = sparse[page(entt)][offset(entt)];
  20839. const auto pos = size_type{traits_type::to_entity(ref)};
  20840. ENTT_ASSERT(packed[pos] == entt, "Invalid entity identifier");
  20841. packed[pos] = std::exchange(free_list, traits_type::construct(static_cast<typename traits_type::entity_type>(pos)));
  20842. // lazy self-assignment guard
  20843. ref = null;
  20844. }
  20845. public:
  20846. /*! @brief Allocator type. */
  20847. using allocator_type = typename alloc_traits::allocator_type;
  20848. /*! @brief Underlying entity identifier. */
  20849. using entity_type = Entity;
  20850. /*! @brief Unsigned integer type. */
  20851. using size_type = std::size_t;
  20852. /*! @brief Pointer type to contained entities. */
  20853. using pointer = alloc_const_pointer;
  20854. /*! @brief Random access iterator type. */
  20855. using iterator = sparse_set_iterator;
  20856. /*! @brief Reverse iterator type. */
  20857. using reverse_iterator = std::reverse_iterator<iterator>;
  20858. /**
  20859. * @brief Constructs an empty container with the given policy and allocator.
  20860. * @param pol Type of deletion policy.
  20861. * @param alloc Allocator to use (possibly default-constructed).
  20862. */
  20863. explicit basic_sparse_set(deletion_policy pol, const allocator_type &alloc = {})
  20864. : allocator{alloc},
  20865. bucket_allocator{alloc},
  20866. sparse{bucket_alloc_traits::allocate(bucket_allocator, 0u)},
  20867. packed{alloc_traits::allocate(allocator, 0u)},
  20868. bucket{0u},
  20869. count{0u},
  20870. reserved{0u},
  20871. free_list{tombstone},
  20872. mode{pol}
  20873. {}
  20874. /**
  20875. * @brief Constructs an empty container with the given allocator.
  20876. * @param alloc Allocator to use (possibly default-constructed).
  20877. */
  20878. explicit basic_sparse_set(const allocator_type &alloc = {})
  20879. : basic_sparse_set{deletion_policy::swap_and_pop, alloc}
  20880. {}
  20881. /**
  20882. * @brief Move constructor.
  20883. * @param other The instance to move from.
  20884. */
  20885. basic_sparse_set(basic_sparse_set &&other) ENTT_NOEXCEPT
  20886. : allocator{std::move(other.allocator)},
  20887. bucket_allocator{std::move(other.bucket_allocator)},
  20888. sparse{std::exchange(other.sparse, bucket_alloc_pointer{})},
  20889. packed{std::exchange(other.packed, alloc_pointer{})},
  20890. bucket{std::exchange(other.bucket, 0u)},
  20891. count{std::exchange(other.count, 0u)},
  20892. reserved{std::exchange(other.reserved, 0u)},
  20893. free_list{std::exchange(other.free_list, tombstone)},
  20894. mode{other.mode}
  20895. {}
  20896. /*! @brief Default destructor. */
  20897. virtual ~basic_sparse_set() {
  20898. release_memory();
  20899. }
  20900. /**
  20901. * @brief Move assignment operator.
  20902. * @param other The instance to move from.
  20903. * @return This sparse set.
  20904. */
  20905. basic_sparse_set & operator=(basic_sparse_set &&other) ENTT_NOEXCEPT {
  20906. release_memory();
  20907. allocator = std::move(other.allocator);
  20908. bucket_allocator = std::move(other.bucket_allocator);
  20909. sparse = std::exchange(other.sparse, bucket_alloc_pointer{});
  20910. packed = std::exchange(other.packed, alloc_pointer{});
  20911. bucket = std::exchange(other.bucket, 0u);
  20912. count = std::exchange(other.count, 0u);
  20913. reserved = std::exchange(other.reserved, 0u);
  20914. free_list = std::exchange(other.free_list, tombstone);
  20915. mode = other.mode;
  20916. return *this;
  20917. }
  20918. /**
  20919. * @brief Returns the deletion policy of a sparse set.
  20920. * @return The deletion policy of the sparse set.
  20921. */
  20922. [[nodiscard]] deletion_policy policy() const ENTT_NOEXCEPT {
  20923. return mode;
  20924. }
  20925. /**
  20926. * @brief Returns the next slot available for insertion.
  20927. * @return The next slot available for insertion.
  20928. */
  20929. [[nodiscard]] size_type slot() const ENTT_NOEXCEPT {
  20930. return free_list == null ? count : size_type{traits_type::to_entity(free_list)};
  20931. }
  20932. /**
  20933. * @brief Increases the capacity of a sparse set.
  20934. *
  20935. * If the new capacity is greater than the current capacity, new storage is
  20936. * allocated, otherwise the method does nothing.
  20937. *
  20938. * @param cap Desired capacity.
  20939. */
  20940. void reserve(const size_type cap) {
  20941. if(cap > reserved) {
  20942. resize_packed(cap);
  20943. }
  20944. }
  20945. /**
  20946. * @brief Returns the number of elements that a sparse set has currently
  20947. * allocated space for.
  20948. * @return Capacity of the sparse set.
  20949. */
  20950. [[nodiscard]] size_type capacity() const ENTT_NOEXCEPT {
  20951. return reserved;
  20952. }
  20953. /*! @brief Requests the removal of unused capacity. */
  20954. void shrink_to_fit() {
  20955. if(count < reserved) {
  20956. resize_packed(count);
  20957. }
  20958. }
  20959. /**
  20960. * @brief Returns the extent of a sparse set.
  20961. *
  20962. * The extent of a sparse set is also the size of the internal sparse array.
  20963. * There is no guarantee that the internal packed array has the same size.
  20964. * Usually the size of the internal sparse array is equal or greater than
  20965. * the one of the internal packed array.
  20966. *
  20967. * @return Extent of the sparse set.
  20968. */
  20969. [[nodiscard]] size_type extent() const ENTT_NOEXCEPT {
  20970. return bucket * sparse_page;
  20971. }
  20972. /**
  20973. * @brief Returns the number of elements in a sparse set.
  20974. *
  20975. * The number of elements is also the size of the internal packed array.
  20976. * There is no guarantee that the internal sparse array has the same size.
  20977. * Usually the size of the internal sparse array is equal or greater than
  20978. * the one of the internal packed array.
  20979. *
  20980. * @return Number of elements.
  20981. */
  20982. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  20983. return count;
  20984. }
  20985. /**
  20986. * @brief Checks whether a sparse set is empty.
  20987. * @return True if the sparse set is empty, false otherwise.
  20988. */
  20989. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  20990. return (count == size_type{});
  20991. }
  20992. /**
  20993. * @brief Direct access to the internal packed array.
  20994. * @return A pointer to the internal packed array.
  20995. */
  20996. [[nodiscard]] pointer data() const ENTT_NOEXCEPT {
  20997. return packed;
  20998. }
  20999. /**
  21000. * @brief Returns an iterator to the beginning.
  21001. *
  21002. * The returned iterator points to the first entity of the internal packed
  21003. * array. If the sparse set is empty, the returned iterator will be equal to
  21004. * `end()`.
  21005. *
  21006. * @return An iterator to the first entity of the internal packed array.
  21007. */
  21008. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  21009. return iterator{std::addressof(packed), static_cast<typename traits_type::difference_type>(count)};
  21010. }
  21011. /**
  21012. * @brief Returns an iterator to the end.
  21013. *
  21014. * The returned iterator points to the element following the last entity in
  21015. * the internal packed array. Attempting to dereference the returned
  21016. * iterator results in undefined behavior.
  21017. *
  21018. * @return An iterator to the element following the last entity of the
  21019. * internal packed array.
  21020. */
  21021. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  21022. return iterator{std::addressof(packed), {}};
  21023. }
  21024. /**
  21025. * @brief Returns a reverse iterator to the beginning.
  21026. *
  21027. * The returned iterator points to the first entity of the reversed internal
  21028. * packed array. If the sparse set is empty, the returned iterator will be
  21029. * equal to `rend()`.
  21030. *
  21031. * @return An iterator to the first entity of the reversed internal packed
  21032. * array.
  21033. */
  21034. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  21035. return std::make_reverse_iterator(end());
  21036. }
  21037. /**
  21038. * @brief Returns a reverse iterator to the end.
  21039. *
  21040. * The returned iterator points to the element following the last entity in
  21041. * the reversed internal packed array. Attempting to dereference the
  21042. * returned iterator results in undefined behavior.
  21043. *
  21044. * @return An iterator to the element following the last entity of the
  21045. * reversed internal packed array.
  21046. */
  21047. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  21048. return std::make_reverse_iterator(begin());
  21049. }
  21050. /**
  21051. * @brief Finds an entity.
  21052. * @param entt A valid entity identifier.
  21053. * @return An iterator to the given entity if it's found, past the end
  21054. * iterator otherwise.
  21055. */
  21056. [[nodiscard]] iterator find(const entity_type entt) const ENTT_NOEXCEPT {
  21057. return contains(entt) ? --(end() - index(entt)) : end();
  21058. }
  21059. /**
  21060. * @brief Checks if a sparse set contains an entity.
  21061. * @param entt A valid entity identifier.
  21062. * @return True if the sparse set contains the entity, false otherwise.
  21063. */
  21064. [[nodiscard]] bool contains(const entity_type entt) const ENTT_NOEXCEPT {
  21065. ENTT_ASSERT(entt != tombstone && entt != null, "Invalid entity");
  21066. const auto curr = page(entt);
  21067. // testing versions permits to avoid accessing the packed array
  21068. return (curr < bucket && sparse[curr] && sparse[curr][offset(entt)] != null);
  21069. }
  21070. /**
  21071. * @brief Returns the position of an entity in a sparse set.
  21072. *
  21073. * @warning
  21074. * Attempting to get the position of an entity that doesn't belong to the
  21075. * sparse set results in undefined behavior.
  21076. *
  21077. * @param entt A valid entity identifier.
  21078. * @return The position of the entity in the sparse set.
  21079. */
  21080. [[nodiscard]] size_type index(const entity_type entt) const ENTT_NOEXCEPT {
  21081. ENTT_ASSERT(contains(entt), "Set does not contain entity");
  21082. return size_type{traits_type::to_entity(sparse[page(entt)][offset(entt)])};
  21083. }
  21084. /**
  21085. * @brief Returns the entity at specified location, with bounds checking.
  21086. * @param pos The position for which to return the entity.
  21087. * @return The entity at specified location if any, a null entity otherwise.
  21088. */
  21089. [[nodiscard]] entity_type at(const size_type pos) const ENTT_NOEXCEPT {
  21090. return pos < count ? packed[pos] : null;
  21091. }
  21092. /**
  21093. * @brief Returns the entity at specified location, without bounds checking.
  21094. * @param pos The position for which to return the entity.
  21095. * @return The entity at specified location.
  21096. */
  21097. [[nodiscard]] entity_type operator[](const size_type pos) const ENTT_NOEXCEPT {
  21098. ENTT_ASSERT(pos < count, "Position is out of bounds");
  21099. return packed[pos];
  21100. }
  21101. /**
  21102. * @brief Appends an entity to a sparse set.
  21103. *
  21104. * @warning
  21105. * Attempting to assign an entity that already belongs to the sparse set
  21106. * results in undefined behavior.
  21107. *
  21108. * @param entt A valid entity identifier.
  21109. * @return The slot used for insertion.
  21110. */
  21111. size_type emplace_back(const entity_type entt) {
  21112. ENTT_ASSERT(!contains(entt), "Set already contains entity");
  21113. if(count == reserved) {
  21114. const size_type sz = static_cast<size_type>(reserved * growth_factor);
  21115. resize_packed(sz + !(sz > reserved));
  21116. }
  21117. assure_page(page(entt))[offset(entt)] = traits_type::construct(static_cast<typename traits_type::entity_type>(count));
  21118. packed[count] = entt;
  21119. return count++;
  21120. }
  21121. /**
  21122. * @brief Assigns an entity to a sparse set.
  21123. *
  21124. * @warning
  21125. * Attempting to assign an entity that already belongs to the sparse set
  21126. * results in undefined behavior.
  21127. *
  21128. * @param entt A valid entity identifier.
  21129. * @return The slot used for insertion.
  21130. */
  21131. size_type emplace(const entity_type entt) {
  21132. if(free_list == null) {
  21133. return emplace_back(entt);
  21134. } else {
  21135. ENTT_ASSERT(!contains(entt), "Set already contains entity");
  21136. const auto pos = size_type{traits_type::to_entity(free_list)};
  21137. sparse[page(entt)][offset(entt)] = traits_type::construct(static_cast<typename traits_type::entity_type>(pos));
  21138. free_list = std::exchange(packed[pos], entt);
  21139. return pos;
  21140. }
  21141. }
  21142. /**
  21143. * @brief Assigns one or more entities to a sparse set.
  21144. *
  21145. * @warning
  21146. * Attempting to assign an entity that already belongs to the sparse set
  21147. * results in undefined behavior.
  21148. *
  21149. * @tparam It Type of input iterator.
  21150. * @param first An iterator to the first element of the range of entities.
  21151. * @param last An iterator past the last element of the range of entities.
  21152. */
  21153. template<typename It>
  21154. void insert(It first, It last) {
  21155. reserve(count + std::distance(first, last));
  21156. for(; first != last; ++first) {
  21157. const auto entt = *first;
  21158. ENTT_ASSERT(!contains(entt), "Set already contains entity");
  21159. assure_page(page(entt))[offset(entt)] = traits_type::construct(static_cast<typename traits_type::entity_type>(count));
  21160. packed[count++] = entt;
  21161. }
  21162. }
  21163. /**
  21164. * @brief Erases an entity from a sparse set.
  21165. *
  21166. * @warning
  21167. * Attempting to erase an entity that doesn't belong to the sparse set
  21168. * results in undefined behavior.
  21169. *
  21170. * @param entt A valid entity identifier.
  21171. * @param ud Optional user data that are forwarded as-is to derived classes.
  21172. */
  21173. void erase(const entity_type entt, void *ud = nullptr) {
  21174. ENTT_ASSERT(contains(entt), "Set does not contain entity");
  21175. (mode == deletion_policy::in_place) ? in_place_pop(entt, ud) : swap_and_pop(entt, ud);
  21176. }
  21177. /**
  21178. * @brief Erases entities from a set.
  21179. *
  21180. * @sa erase
  21181. *
  21182. * @tparam It Type of input iterator.
  21183. * @param first An iterator to the first element of the range of entities.
  21184. * @param last An iterator past the last element of the range of entities.
  21185. * @param ud Optional user data that are forwarded as-is to derived classes.
  21186. */
  21187. template<typename It>
  21188. void erase(It first, It last, void *ud = nullptr) {
  21189. for(; first != last; ++first) {
  21190. erase(*first, ud);
  21191. }
  21192. }
  21193. /**
  21194. * @brief Removes an entity from a sparse set if it exists.
  21195. * @param entt A valid entity identifier.
  21196. * @param ud Optional user data that are forwarded as-is to derived classes.
  21197. * @return True if the entity is actually removed, false otherwise.
  21198. */
  21199. bool remove(const entity_type entt, void *ud = nullptr) {
  21200. return contains(entt) && (erase(entt, ud), true);
  21201. }
  21202. /**
  21203. * @brief Removes entities from a sparse set if they exist.
  21204. * @tparam It Type of input iterator.
  21205. * @param first An iterator to the first element of the range of entities.
  21206. * @param last An iterator past the last element of the range of entities.
  21207. * @param ud Optional user data that are forwarded as-is to derived classes.
  21208. * @return The number of entities actually removed.
  21209. */
  21210. template<typename It>
  21211. size_type remove(It first, It last, void *ud = nullptr) {
  21212. size_type found{};
  21213. for(; first != last; ++first) {
  21214. found += remove(*first, ud);
  21215. }
  21216. return found;
  21217. }
  21218. /*! @brief Removes all tombstones from the packed array of a sparse set. */
  21219. void compact() {
  21220. size_type next = count;
  21221. for(; next && packed[next - 1u] == tombstone; --next);
  21222. for(auto *it = &free_list; *it != null && next; it = std::addressof(packed[traits_type::to_entity(*it)])) {
  21223. if(const size_type pos = traits_type::to_entity(*it); pos < next) {
  21224. --next;
  21225. move_and_pop(next, pos);
  21226. std::swap(packed[next], packed[pos]);
  21227. sparse[page(packed[pos])][offset(packed[pos])] = traits_type::construct(static_cast<const typename traits_type::entity_type>(pos));
  21228. *it = traits_type::construct(static_cast<typename traits_type::entity_type>(next));
  21229. for(; next && packed[next - 1u] == tombstone; --next);
  21230. }
  21231. }
  21232. free_list = tombstone;
  21233. count = next;
  21234. }
  21235. /**
  21236. * @copybrief swap_at
  21237. *
  21238. * For what it's worth, this function affects both the internal sparse array
  21239. * and the internal packed array. Users should not care of that anyway.
  21240. *
  21241. * @warning
  21242. * Attempting to swap entities that don't belong to the sparse set results
  21243. * in undefined behavior.
  21244. *
  21245. * @param lhs A valid entity identifier.
  21246. * @param rhs A valid entity identifier.
  21247. */
  21248. void swap(const entity_type lhs, const entity_type rhs) {
  21249. ENTT_ASSERT(contains(lhs), "Set does not contain entity");
  21250. ENTT_ASSERT(contains(rhs), "Set does not contain entity");
  21251. auto &entt = sparse[page(lhs)][offset(lhs)];
  21252. auto &other = sparse[page(rhs)][offset(rhs)];
  21253. const auto from = size_type{traits_type::to_entity(entt)};
  21254. const auto to = size_type{traits_type::to_entity(other)};
  21255. // basic no-leak guarantee (with invalid state) if swapping throws
  21256. swap_at(from, to);
  21257. std::swap(entt, other);
  21258. std::swap(packed[from], packed[to]);
  21259. }
  21260. /**
  21261. * @brief Sort the first count elements according to the given comparison
  21262. * function.
  21263. *
  21264. * The comparison function object must return `true` if the first element
  21265. * is _less_ than the second one, `false` otherwise. The signature of the
  21266. * comparison function should be equivalent to the following:
  21267. *
  21268. * @code{.cpp}
  21269. * bool(const Entity, const Entity);
  21270. * @endcode
  21271. *
  21272. * Moreover, the comparison function object shall induce a
  21273. * _strict weak ordering_ on the values.
  21274. *
  21275. * The sort function object must offer a member function template
  21276. * `operator()` that accepts three arguments:
  21277. *
  21278. * * An iterator to the first element of the range to sort.
  21279. * * An iterator past the last element of the range to sort.
  21280. * * A comparison function to use to compare the elements.
  21281. *
  21282. * @tparam Compare Type of comparison function object.
  21283. * @tparam Sort Type of sort function object.
  21284. * @tparam Args Types of arguments to forward to the sort function object.
  21285. * @param length Number of elements to sort.
  21286. * @param compare A valid comparison function object.
  21287. * @param algo A valid sort function object.
  21288. * @param args Arguments to forward to the sort function object, if any.
  21289. */
  21290. template<typename Compare, typename Sort = std_sort, typename... Args>
  21291. void sort_n(const size_type length, Compare compare, Sort algo = Sort{}, Args &&... args) {
  21292. // basic no-leak guarantee (with invalid state) if sorting throws
  21293. ENTT_ASSERT(!(length > count), "Length exceeds the number of elements");
  21294. compact();
  21295. algo(std::make_reverse_iterator(packed + length), std::make_reverse_iterator(packed), std::move(compare), std::forward<Args>(args)...);
  21296. for(size_type pos{}; pos < length; ++pos) {
  21297. auto curr = pos;
  21298. auto next = index(packed[curr]);
  21299. while(curr != next) {
  21300. const auto idx = index(packed[next]);
  21301. const auto entt = packed[curr];
  21302. swap_at(next, idx);
  21303. sparse[page(entt)][offset(entt)] = traits_type::construct(static_cast<typename traits_type::entity_type>(curr));
  21304. curr = std::exchange(next, idx);
  21305. }
  21306. }
  21307. }
  21308. /**
  21309. * @brief Sort all elements according to the given comparison function.
  21310. *
  21311. * @sa sort_n
  21312. *
  21313. * @tparam Compare Type of comparison function object.
  21314. * @tparam Sort Type of sort function object.
  21315. * @tparam Args Types of arguments to forward to the sort function object.
  21316. * @param compare A valid comparison function object.
  21317. * @param algo A valid sort function object.
  21318. * @param args Arguments to forward to the sort function object, if any.
  21319. */
  21320. template<typename Compare, typename Sort = std_sort, typename... Args>
  21321. void sort(Compare compare, Sort algo = Sort{}, Args &&... args) {
  21322. sort_n(count, std::move(compare), std::move(algo), std::forward<Args>(args)...);
  21323. }
  21324. /**
  21325. * @brief Sort entities according to their order in another sparse set.
  21326. *
  21327. * Entities that are part of both the sparse sets are ordered internally
  21328. * according to the order they have in `other`. All the other entities goes
  21329. * to the end of the list and there are no guarantees on their order.<br/>
  21330. * In other terms, this function can be used to impose the same order on two
  21331. * sets by using one of them as a master and the other one as a slave.
  21332. *
  21333. * Iterating the sparse set with a couple of iterators returns elements in
  21334. * the expected order after a call to `respect`. See `begin` and `end` for
  21335. * more details.
  21336. *
  21337. * @param other The sparse sets that imposes the order of the entities.
  21338. */
  21339. void respect(const basic_sparse_set &other) {
  21340. compact();
  21341. const auto to = other.end();
  21342. auto from = other.begin();
  21343. for(size_type pos = count - 1; pos && from != to; ++from) {
  21344. if(contains(*from)) {
  21345. if(*from != packed[pos]) {
  21346. // basic no-leak guarantee (with invalid state) if swapping throws
  21347. swap(packed[pos], *from);
  21348. }
  21349. --pos;
  21350. }
  21351. }
  21352. }
  21353. /**
  21354. * @brief Clears a sparse set.
  21355. * @param ud Optional user data that are forwarded as-is to derived classes.
  21356. */
  21357. void clear(void *ud = nullptr) {
  21358. for(auto &&entity: *this) {
  21359. if(entity != tombstone) {
  21360. in_place_pop(entity, ud);
  21361. }
  21362. }
  21363. free_list = tombstone;
  21364. count = 0u;
  21365. }
  21366. private:
  21367. typename alloc_traits::allocator_type allocator;
  21368. typename bucket_alloc_traits::allocator_type bucket_allocator;
  21369. bucket_alloc_pointer sparse;
  21370. alloc_pointer packed;
  21371. std::size_t bucket;
  21372. std::size_t count;
  21373. std::size_t reserved;
  21374. entity_type free_list;
  21375. deletion_policy mode;
  21376. };
  21377. }
  21378. #endif
  21379. // #include "entity/storage.hpp"
  21380. #ifndef ENTT_ENTITY_STORAGE_HPP
  21381. #define ENTT_ENTITY_STORAGE_HPP
  21382. #include <cstddef>
  21383. #include <iterator>
  21384. #include <memory>
  21385. #include <tuple>
  21386. #include <type_traits>
  21387. #include <utility>
  21388. // #include "../config/config.h"
  21389. // #include "../core/algorithm.hpp"
  21390. // #include "../core/fwd.hpp"
  21391. // #include "../core/type_traits.hpp"
  21392. // #include "../signal/sigh.hpp"
  21393. // #include "component.hpp"
  21394. // #include "entity.hpp"
  21395. // #include "fwd.hpp"
  21396. // #include "sparse_set.hpp"
  21397. namespace entt {
  21398. /**
  21399. * @brief Basic storage implementation.
  21400. *
  21401. * This class is a refinement of a sparse set that associates an object to an
  21402. * entity. The main purpose of this class is to extend sparse sets to store
  21403. * components in a registry. It guarantees fast access both to the elements and
  21404. * to the entities.
  21405. *
  21406. * @note
  21407. * Entities and objects have the same order.
  21408. *
  21409. * @note
  21410. * Internal data structures arrange elements to maximize performance. There are
  21411. * no guarantees that objects are returned in the insertion order when iterate
  21412. * a storage. Do not make assumption on the order in any case.
  21413. *
  21414. * @warning
  21415. * Empty types aren't explicitly instantiated. Therefore, many of the functions
  21416. * normally available for non-empty types will not be available for empty ones.
  21417. *
  21418. * @sa sparse_set<Entity>
  21419. *
  21420. * @tparam Entity A valid entity type (see entt_traits for more details).
  21421. * @tparam Type Type of objects assigned to the entities.
  21422. * @tparam Allocator Type of allocator used to manage memory and elements.
  21423. */
  21424. template<typename Entity, typename Type, typename Allocator, typename = void>
  21425. class basic_storage_impl: public basic_sparse_set<Entity, typename std::allocator_traits<Allocator>::template rebind_alloc<Entity>> {
  21426. static constexpr auto packed_page = ENTT_PACKED_PAGE;
  21427. using comp_traits = component_traits<Type>;
  21428. using underlying_type = basic_sparse_set<Entity, typename std::allocator_traits<Allocator>::template rebind_alloc<Entity>>;
  21429. using difference_type = typename entt_traits<Entity>::difference_type;
  21430. using alloc_traits = typename std::allocator_traits<Allocator>::template rebind_traits<Type>;
  21431. using alloc_pointer = typename alloc_traits::pointer;
  21432. using alloc_const_pointer = typename alloc_traits::const_pointer;
  21433. using bucket_alloc_traits = typename std::allocator_traits<Allocator>::template rebind_traits<alloc_pointer>;
  21434. using bucket_alloc_pointer = typename bucket_alloc_traits::pointer;
  21435. using bucket_alloc_const_type = typename std::allocator_traits<Allocator>::template rebind_alloc<alloc_const_pointer>;
  21436. using bucket_alloc_const_pointer = typename std::allocator_traits<bucket_alloc_const_type>::const_pointer;
  21437. static_assert(alloc_traits::propagate_on_container_move_assignment::value);
  21438. static_assert(bucket_alloc_traits::propagate_on_container_move_assignment::value);
  21439. template<typename Value>
  21440. struct storage_iterator final {
  21441. using difference_type = typename basic_storage_impl::difference_type;
  21442. using value_type = Value;
  21443. using pointer = value_type *;
  21444. using reference = value_type &;
  21445. using iterator_category = std::random_access_iterator_tag;
  21446. storage_iterator() ENTT_NOEXCEPT = default;
  21447. storage_iterator(bucket_alloc_pointer const *ref, const typename basic_storage_impl::difference_type idx) ENTT_NOEXCEPT
  21448. : packed{ref},
  21449. index{idx}
  21450. {}
  21451. storage_iterator & operator++() ENTT_NOEXCEPT {
  21452. return --index, *this;
  21453. }
  21454. storage_iterator operator++(int) ENTT_NOEXCEPT {
  21455. storage_iterator orig = *this;
  21456. return ++(*this), orig;
  21457. }
  21458. storage_iterator & operator--() ENTT_NOEXCEPT {
  21459. return ++index, *this;
  21460. }
  21461. storage_iterator operator--(int) ENTT_NOEXCEPT {
  21462. storage_iterator orig = *this;
  21463. return operator--(), orig;
  21464. }
  21465. storage_iterator & operator+=(const difference_type value) ENTT_NOEXCEPT {
  21466. index -= value;
  21467. return *this;
  21468. }
  21469. storage_iterator operator+(const difference_type value) const ENTT_NOEXCEPT {
  21470. storage_iterator copy = *this;
  21471. return (copy += value);
  21472. }
  21473. storage_iterator & operator-=(const difference_type value) ENTT_NOEXCEPT {
  21474. return (*this += -value);
  21475. }
  21476. storage_iterator operator-(const difference_type value) const ENTT_NOEXCEPT {
  21477. return (*this + -value);
  21478. }
  21479. difference_type operator-(const storage_iterator &other) const ENTT_NOEXCEPT {
  21480. return other.index - index;
  21481. }
  21482. [[nodiscard]] reference operator[](const difference_type value) const ENTT_NOEXCEPT {
  21483. const auto pos = size_type(index-value-1);
  21484. return (*packed)[page(pos)][offset(pos)];
  21485. }
  21486. [[nodiscard]] bool operator==(const storage_iterator &other) const ENTT_NOEXCEPT {
  21487. return other.index == index;
  21488. }
  21489. [[nodiscard]] bool operator!=(const storage_iterator &other) const ENTT_NOEXCEPT {
  21490. return !(*this == other);
  21491. }
  21492. [[nodiscard]] bool operator<(const storage_iterator &other) const ENTT_NOEXCEPT {
  21493. return index > other.index;
  21494. }
  21495. [[nodiscard]] bool operator>(const storage_iterator &other) const ENTT_NOEXCEPT {
  21496. return index < other.index;
  21497. }
  21498. [[nodiscard]] bool operator<=(const storage_iterator &other) const ENTT_NOEXCEPT {
  21499. return !(*this > other);
  21500. }
  21501. [[nodiscard]] bool operator>=(const storage_iterator &other) const ENTT_NOEXCEPT {
  21502. return !(*this < other);
  21503. }
  21504. [[nodiscard]] pointer operator->() const ENTT_NOEXCEPT {
  21505. const auto pos = size_type(index-1u);
  21506. return std::addressof((*packed)[page(pos)][offset(pos)]);
  21507. }
  21508. [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
  21509. return *operator->();
  21510. }
  21511. private:
  21512. bucket_alloc_pointer const *packed;
  21513. difference_type index;
  21514. };
  21515. [[nodiscard]] static auto page(const std::size_t pos) ENTT_NOEXCEPT {
  21516. return pos / packed_page;
  21517. }
  21518. [[nodiscard]] static auto offset(const std::size_t pos) ENTT_NOEXCEPT {
  21519. return pos & (packed_page - 1);
  21520. }
  21521. void release_memory() {
  21522. if(packed) {
  21523. // no-throw stable erase iteration
  21524. underlying_type::clear();
  21525. for(size_type pos{}; pos < bucket; ++pos) {
  21526. alloc_traits::deallocate(allocator, packed[pos], packed_page);
  21527. bucket_alloc_traits::destroy(bucket_allocator, std::addressof(packed[pos]));
  21528. }
  21529. bucket_alloc_traits::deallocate(bucket_allocator, packed, bucket);
  21530. }
  21531. }
  21532. void assure_at_least(const std::size_t last) {
  21533. if(const auto idx = page(last - 1u); !(idx < bucket)) {
  21534. const size_type sz = idx + 1u;
  21535. const auto mem = bucket_alloc_traits::allocate(bucket_allocator, sz);
  21536. std::uninitialized_copy(packed, packed + bucket, mem);
  21537. size_type pos{};
  21538. ENTT_TRY {
  21539. for(pos = bucket; pos < sz; ++pos) {
  21540. auto pg = alloc_traits::allocate(allocator, packed_page);
  21541. bucket_alloc_traits::construct(bucket_allocator, std::addressof(mem[pos]), pg);
  21542. }
  21543. } ENTT_CATCH {
  21544. for(auto next = bucket; next < pos; ++next) {
  21545. alloc_traits::deallocate(allocator, mem[next], packed_page);
  21546. }
  21547. std::destroy(mem, mem + pos);
  21548. bucket_alloc_traits::deallocate(bucket_allocator, mem, sz);
  21549. ENTT_THROW;
  21550. }
  21551. std::destroy(packed, packed + bucket);
  21552. bucket_alloc_traits::deallocate(bucket_allocator, packed, bucket);
  21553. packed = mem;
  21554. bucket = sz;
  21555. }
  21556. }
  21557. void release_unused_pages() {
  21558. if(const auto length = underlying_type::size() / packed_page; length < bucket) {
  21559. const auto mem = bucket_alloc_traits::allocate(bucket_allocator, length);
  21560. std::uninitialized_copy(packed, packed + length, mem);
  21561. for(auto pos = length; pos < bucket; ++pos) {
  21562. alloc_traits::deallocate(allocator, packed[pos], packed_page);
  21563. bucket_alloc_traits::destroy(bucket_allocator, std::addressof(packed[pos]));
  21564. }
  21565. bucket_alloc_traits::deallocate(bucket_allocator, packed, bucket);
  21566. packed = mem;
  21567. bucket = length;
  21568. }
  21569. }
  21570. template<typename... Args>
  21571. auto & push_at(const std::size_t pos, Args &&... args) {
  21572. ENTT_ASSERT(pos < (bucket * packed_page), "Out of bounds index");
  21573. auto *instance = std::addressof(packed[page(pos)][offset(pos)]);
  21574. if constexpr(std::is_aggregate_v<value_type>) {
  21575. alloc_traits::construct(allocator, instance, Type{std::forward<Args>(args)...});
  21576. } else {
  21577. alloc_traits::construct(allocator, instance, std::forward<Args>(args)...);
  21578. }
  21579. return *instance;
  21580. }
  21581. void pop_at(const std::size_t pos) {
  21582. alloc_traits::destroy(allocator, std::addressof(packed[page(pos)][offset(pos)]));
  21583. }
  21584. protected:
  21585. /*! @copydoc basic_sparse_set::swap_at */
  21586. void swap_at(const std::size_t lhs, const std::size_t rhs) final {
  21587. std::swap(packed[page(lhs)][offset(lhs)], packed[page(rhs)][offset(rhs)]);
  21588. }
  21589. /*! @copydoc basic_sparse_set::move_and_pop */
  21590. void move_and_pop(const std::size_t from, const std::size_t to) final {
  21591. push_at(to, std::move(packed[page(from)][offset(from)]));
  21592. pop_at(from);
  21593. }
  21594. /*! @copydoc basic_sparse_set::swap_and_pop */
  21595. void swap_and_pop(const Entity entt, void *ud) override {
  21596. const auto pos = underlying_type::index(entt);
  21597. const auto last = underlying_type::size() - 1u;
  21598. auto &&elem = packed[page(pos)][offset(pos)];
  21599. // support for nosy destructors
  21600. [[maybe_unused]] auto unused = std::move(elem);
  21601. elem = std::move(packed[page(last)][offset(last)]);
  21602. pop_at(last);
  21603. underlying_type::swap_and_pop(entt, ud);
  21604. }
  21605. /*! @copydoc basic_sparse_set::in_place_pop */
  21606. void in_place_pop(const Entity entt, void *ud) override {
  21607. const auto pos = underlying_type::index(entt);
  21608. underlying_type::in_place_pop(entt, ud);
  21609. // support for nosy destructors
  21610. pop_at(pos);
  21611. }
  21612. public:
  21613. /*! @brief Allocator type. */
  21614. using allocator_type = typename alloc_traits::allocator_type;
  21615. /*! @brief Type of the objects assigned to entities. */
  21616. using value_type = Type;
  21617. /*! @brief Underlying entity identifier. */
  21618. using entity_type = Entity;
  21619. /*! @brief Unsigned integer type. */
  21620. using size_type = std::size_t;
  21621. /*! @brief Pointer type to contained elements. */
  21622. using pointer = bucket_alloc_pointer;
  21623. /*! @brief Constant pointer type to contained elements. */
  21624. using const_pointer = bucket_alloc_const_pointer;
  21625. /*! @brief Random access iterator type. */
  21626. using iterator = storage_iterator<value_type>;
  21627. /*! @brief Constant random access iterator type. */
  21628. using const_iterator = storage_iterator<const value_type>;
  21629. /*! @brief Reverse iterator type. */
  21630. using reverse_iterator = std::reverse_iterator<iterator>;
  21631. /*! @brief Constant reverse iterator type. */
  21632. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  21633. /**
  21634. * @brief Default constructor.
  21635. * @param alloc Allocator to use (possibly default-constructed).
  21636. */
  21637. explicit basic_storage_impl(const allocator_type &alloc = {})
  21638. : underlying_type{deletion_policy{comp_traits::in_place_delete::value}, alloc},
  21639. allocator{alloc},
  21640. bucket_allocator{alloc},
  21641. packed{bucket_alloc_traits::allocate(bucket_allocator, 0u)},
  21642. bucket{}
  21643. {}
  21644. /**
  21645. * @brief Move constructor.
  21646. * @param other The instance to move from.
  21647. */
  21648. basic_storage_impl(basic_storage_impl &&other) ENTT_NOEXCEPT
  21649. : underlying_type{std::move(other)},
  21650. allocator{std::move(other.allocator)},
  21651. bucket_allocator{std::move(other.bucket_allocator)},
  21652. packed{std::exchange(other.packed, bucket_alloc_pointer{})},
  21653. bucket{std::exchange(other.bucket, 0u)}
  21654. {}
  21655. /*! @brief Default destructor. */
  21656. ~basic_storage_impl() override {
  21657. release_memory();
  21658. }
  21659. /**
  21660. * @brief Move assignment operator.
  21661. * @param other The instance to move from.
  21662. * @return This sparse set.
  21663. */
  21664. basic_storage_impl & operator=(basic_storage_impl &&other) ENTT_NOEXCEPT {
  21665. release_memory();
  21666. underlying_type::operator=(std::move(other));
  21667. allocator = std::move(other.allocator);
  21668. bucket_allocator = std::move(other.bucket_allocator);
  21669. packed = std::exchange(other.packed, bucket_alloc_pointer{});
  21670. bucket = std::exchange(other.bucket, 0u);
  21671. return *this;
  21672. }
  21673. /**
  21674. * @brief Increases the capacity of a storage.
  21675. *
  21676. * If the new capacity is greater than the current capacity, new storage is
  21677. * allocated, otherwise the method does nothing.
  21678. *
  21679. * @param cap Desired capacity.
  21680. */
  21681. void reserve(const size_type cap) {
  21682. underlying_type::reserve(cap);
  21683. if(cap > underlying_type::size()) {
  21684. assure_at_least(cap);
  21685. }
  21686. }
  21687. /**
  21688. * @brief Returns the number of elements that a storage has currently
  21689. * allocated space for.
  21690. * @return Capacity of the storage.
  21691. */
  21692. [[nodiscard]] size_type capacity() const ENTT_NOEXCEPT {
  21693. return bucket * packed_page;
  21694. }
  21695. /*! @brief Requests the removal of unused capacity. */
  21696. void shrink_to_fit() {
  21697. underlying_type::shrink_to_fit();
  21698. release_unused_pages();
  21699. }
  21700. /**
  21701. * @brief Direct access to the array of objects.
  21702. * @return A pointer to the array of objects.
  21703. */
  21704. [[nodiscard]] const_pointer raw() const ENTT_NOEXCEPT {
  21705. return packed;
  21706. }
  21707. /*! @copydoc raw */
  21708. [[nodiscard]] pointer raw() ENTT_NOEXCEPT {
  21709. return packed;
  21710. }
  21711. /**
  21712. * @brief Returns an iterator to the beginning.
  21713. *
  21714. * The returned iterator points to the first instance of the internal array.
  21715. * If the storage is empty, the returned iterator will be equal to `end()`.
  21716. *
  21717. * @return An iterator to the first instance of the internal array.
  21718. */
  21719. [[nodiscard]] const_iterator cbegin() const ENTT_NOEXCEPT {
  21720. const difference_type pos = underlying_type::size();
  21721. return const_iterator{std::addressof(packed), pos};
  21722. }
  21723. /*! @copydoc cbegin */
  21724. [[nodiscard]] const_iterator begin() const ENTT_NOEXCEPT {
  21725. return cbegin();
  21726. }
  21727. /*! @copydoc begin */
  21728. [[nodiscard]] iterator begin() ENTT_NOEXCEPT {
  21729. const difference_type pos = underlying_type::size();
  21730. return iterator{std::addressof(packed), pos};
  21731. }
  21732. /**
  21733. * @brief Returns an iterator to the end.
  21734. *
  21735. * The returned iterator points to the element following the last instance
  21736. * of the internal array. Attempting to dereference the returned iterator
  21737. * results in undefined behavior.
  21738. *
  21739. * @return An iterator to the element following the last instance of the
  21740. * internal array.
  21741. */
  21742. [[nodiscard]] const_iterator cend() const ENTT_NOEXCEPT {
  21743. return const_iterator{std::addressof(packed), {}};
  21744. }
  21745. /*! @copydoc cend */
  21746. [[nodiscard]] const_iterator end() const ENTT_NOEXCEPT {
  21747. return cend();
  21748. }
  21749. /*! @copydoc end */
  21750. [[nodiscard]] iterator end() ENTT_NOEXCEPT {
  21751. return iterator{std::addressof(packed), {}};
  21752. }
  21753. /**
  21754. * @brief Returns a reverse iterator to the beginning.
  21755. *
  21756. * The returned iterator points to the first instance of the reversed
  21757. * internal array. If the storage is empty, the returned iterator will be
  21758. * equal to `rend()`.
  21759. *
  21760. * @return An iterator to the first instance of the reversed internal array.
  21761. */
  21762. [[nodiscard]] const_reverse_iterator crbegin() const ENTT_NOEXCEPT {
  21763. return std::make_reverse_iterator(cend());
  21764. }
  21765. /*! @copydoc crbegin */
  21766. [[nodiscard]] const_reverse_iterator rbegin() const ENTT_NOEXCEPT {
  21767. return crbegin();
  21768. }
  21769. /*! @copydoc rbegin */
  21770. [[nodiscard]] reverse_iterator rbegin() ENTT_NOEXCEPT {
  21771. return std::make_reverse_iterator(end());
  21772. }
  21773. /**
  21774. * @brief Returns a reverse iterator to the end.
  21775. *
  21776. * The returned iterator points to the element following the last instance
  21777. * of the reversed internal array. Attempting to dereference the returned
  21778. * iterator results in undefined behavior.
  21779. *
  21780. * @return An iterator to the element following the last instance of the
  21781. * reversed internal array.
  21782. */
  21783. [[nodiscard]] const_reverse_iterator crend() const ENTT_NOEXCEPT {
  21784. return std::make_reverse_iterator(cbegin());
  21785. }
  21786. /*! @copydoc crend */
  21787. [[nodiscard]] const_reverse_iterator rend() const ENTT_NOEXCEPT {
  21788. return crend();
  21789. }
  21790. /*! @copydoc rend */
  21791. [[nodiscard]] reverse_iterator rend() ENTT_NOEXCEPT {
  21792. return std::make_reverse_iterator(begin());
  21793. }
  21794. /**
  21795. * @brief Returns the object assigned to an entity.
  21796. *
  21797. * @warning
  21798. * Attempting to use an entity that doesn't belong to the storage results in
  21799. * undefined behavior.
  21800. *
  21801. * @param entt A valid entity identifier.
  21802. * @return The object assigned to the entity.
  21803. */
  21804. [[nodiscard]] const value_type & get(const entity_type entt) const ENTT_NOEXCEPT {
  21805. const auto idx = underlying_type::index(entt);
  21806. return packed[page(idx)][offset(idx)];
  21807. }
  21808. /*! @copydoc get */
  21809. [[nodiscard]] value_type & get(const entity_type entt) ENTT_NOEXCEPT {
  21810. return const_cast<value_type &>(std::as_const(*this).get(entt));
  21811. }
  21812. /**
  21813. * @brief Assigns an entity to a storage and constructs its object.
  21814. *
  21815. * This version accept both types that can be constructed in place directly
  21816. * and types like aggregates that do not work well with a placement new as
  21817. * performed usually under the hood during an _emplace back_.
  21818. *
  21819. * @warning
  21820. * Attempting to use an entity that already belongs to the storage results
  21821. * in undefined behavior.
  21822. *
  21823. * @tparam Args Types of arguments to use to construct the object.
  21824. * @param entt A valid entity identifier.
  21825. * @param args Parameters to use to construct an object for the entity.
  21826. * @return A reference to the newly created object.
  21827. */
  21828. template<typename... Args>
  21829. value_type & emplace(const entity_type entt, Args &&... args) {
  21830. const auto pos = underlying_type::slot();
  21831. assure_at_least(pos + 1u);
  21832. auto &value = push_at(pos, std::forward<Args>(args)...);
  21833. ENTT_TRY {
  21834. [[maybe_unused]] const auto curr = underlying_type::emplace(entt);
  21835. ENTT_ASSERT(pos == curr, "Misplaced component");
  21836. } ENTT_CATCH {
  21837. pop_at(pos);
  21838. ENTT_THROW;
  21839. }
  21840. return value;
  21841. }
  21842. /**
  21843. * @brief Updates the instance assigned to a given entity in-place.
  21844. * @tparam Func Types of the function objects to invoke.
  21845. * @param entt A valid entity identifier.
  21846. * @param func Valid function objects.
  21847. * @return A reference to the updated instance.
  21848. */
  21849. template<typename... Func>
  21850. decltype(auto) patch(const entity_type entt, Func &&... func) {
  21851. const auto idx = underlying_type::index(entt);
  21852. auto &&elem = packed[page(idx)][offset(idx)];
  21853. (std::forward<Func>(func)(elem), ...);
  21854. return elem;
  21855. }
  21856. /**
  21857. * @brief Assigns one or more entities to a storage and constructs their
  21858. * objects from a given instance.
  21859. *
  21860. * @warning
  21861. * Attempting to assign an entity that already belongs to the storage
  21862. * results in undefined behavior.
  21863. *
  21864. * @tparam It Type of input iterator.
  21865. * @param first An iterator to the first element of the range of entities.
  21866. * @param last An iterator past the last element of the range of entities.
  21867. * @param value An instance of the object to construct.
  21868. */
  21869. template<typename It>
  21870. void insert(It first, It last, const value_type &value = {}) {
  21871. const auto cap = underlying_type::size() + std::distance(first, last);
  21872. underlying_type::reserve(cap);
  21873. assure_at_least(cap);
  21874. for(; first != last; ++first) {
  21875. push_at(underlying_type::size(), value);
  21876. ENTT_TRY {
  21877. underlying_type::emplace_back(*first);
  21878. } ENTT_CATCH {
  21879. pop_at(underlying_type::size());
  21880. ENTT_THROW;
  21881. }
  21882. }
  21883. }
  21884. /**
  21885. * @brief Assigns one or more entities to a storage and constructs their
  21886. * objects from a given range.
  21887. *
  21888. * @sa construct
  21889. *
  21890. * @tparam EIt Type of input iterator.
  21891. * @tparam CIt Type of input iterator.
  21892. * @param first An iterator to the first element of the range of entities.
  21893. * @param last An iterator past the last element of the range of entities.
  21894. * @param from An iterator to the first element of the range of objects.
  21895. */
  21896. template<typename EIt, typename CIt, typename = std::enable_if_t<std::is_same_v<std::decay_t<typename std::iterator_traits<CIt>::value_type>, value_type>>>
  21897. void insert(EIt first, EIt last, CIt from) {
  21898. const auto cap = underlying_type::size() + std::distance(first, last);
  21899. underlying_type::reserve(cap);
  21900. assure_at_least(cap);
  21901. for(; first != last; ++first, ++from) {
  21902. push_at(underlying_type::size(), *from);
  21903. ENTT_TRY {
  21904. underlying_type::emplace_back(*first);
  21905. } ENTT_CATCH {
  21906. pop_at(underlying_type::size());
  21907. ENTT_THROW;
  21908. }
  21909. }
  21910. }
  21911. /**
  21912. * @brief Sort elements according to the given comparison function.
  21913. *
  21914. * The comparison function object must return `true` if the first element
  21915. * is _less_ than the second one, `false` otherwise. The signature of the
  21916. * comparison function should be equivalent to one of the following:
  21917. *
  21918. * @code{.cpp}
  21919. * bool(const Entity, const Entity);
  21920. * bool(const Type &, const Type &);
  21921. * @endcode
  21922. *
  21923. * Moreover, the comparison function object shall induce a
  21924. * _strict weak ordering_ on the values.
  21925. *
  21926. * The sort function oject must offer a member function template
  21927. * `operator()` that accepts three arguments:
  21928. *
  21929. * * An iterator to the first element of the range to sort.
  21930. * * An iterator past the last element of the range to sort.
  21931. * * A comparison function to use to compare the elements.
  21932. *
  21933. * @warning
  21934. * Empty types are never instantiated. Therefore, only comparison function
  21935. * objects that require to return entities rather than components are
  21936. * accepted.
  21937. *
  21938. * @tparam Compare Type of comparison function object.
  21939. * @tparam Sort Type of sort function object.
  21940. * @tparam Args Types of arguments to forward to the sort function object.
  21941. * @param length Number of elements to sort.
  21942. * @param compare A valid comparison function object.
  21943. * @param algo A valid sort function object.
  21944. * @param args Arguments to forward to the sort function object, if any.
  21945. */
  21946. template<typename Compare, typename Sort = std_sort, typename... Args>
  21947. void sort_n(const size_type length, Compare compare, Sort algo = Sort{}, Args &&... args) {
  21948. if constexpr(std::is_invocable_v<Compare, const value_type &, const value_type &>) {
  21949. underlying_type::sort_n(length, [this, compare = std::move(compare)](const auto lhs, const auto rhs) {
  21950. const auto ilhs = underlying_type::index(lhs), irhs = underlying_type::index(rhs);
  21951. return compare(std::as_const(packed[page(ilhs)][offset(ilhs)]), std::as_const(packed[page(irhs)][offset(irhs)]));
  21952. }, std::move(algo), std::forward<Args>(args)...);
  21953. } else {
  21954. underlying_type::sort_n(length, std::move(compare), std::move(algo), std::forward<Args>(args)...);
  21955. }
  21956. }
  21957. /**
  21958. * @brief Sort all elements according to the given comparison function.
  21959. *
  21960. * @sa sort_n
  21961. *
  21962. * @tparam Compare Type of comparison function object.
  21963. * @tparam Sort Type of sort function object.
  21964. * @tparam Args Types of arguments to forward to the sort function object.
  21965. * @param compare A valid comparison function object.
  21966. * @param algo A valid sort function object.
  21967. * @param args Arguments to forward to the sort function object, if any.
  21968. */
  21969. template<typename Compare, typename Sort = std_sort, typename... Args>
  21970. void sort(Compare compare, Sort algo = Sort{}, Args &&... args) {
  21971. sort_n(underlying_type::size(), std::move(compare), std::move(algo), std::forward<Args>(args)...);
  21972. }
  21973. private:
  21974. typename alloc_traits::allocator_type allocator;
  21975. typename bucket_alloc_traits::allocator_type bucket_allocator;
  21976. bucket_alloc_pointer packed;
  21977. size_type bucket;
  21978. };
  21979. /*! @copydoc basic_storage_impl */
  21980. template<typename Entity, typename Type, typename Allocator>
  21981. class basic_storage_impl<Entity, Type, Allocator, std::enable_if_t<component_traits<Type>::ignore_if_empty::value && std::is_empty_v<Type>>>
  21982. : public basic_sparse_set<Entity, typename std::allocator_traits<Allocator>::template rebind_alloc<Entity>>
  21983. {
  21984. using comp_traits = component_traits<Type>;
  21985. using underlying_type = basic_sparse_set<Entity, typename std::allocator_traits<Allocator>::template rebind_alloc<Entity>>;
  21986. using alloc_traits = typename std::allocator_traits<Allocator>::template rebind_traits<Type>;
  21987. public:
  21988. /*! @brief Allocator type. */
  21989. using allocator_type = typename alloc_traits::allocator_type;
  21990. /*! @brief Type of the objects assigned to entities. */
  21991. using value_type = Type;
  21992. /*! @brief Underlying entity identifier. */
  21993. using entity_type = Entity;
  21994. /*! @brief Unsigned integer type. */
  21995. using size_type = std::size_t;
  21996. /**
  21997. * @brief Default constructor.
  21998. * @param alloc Allocator to use (possibly default-constructed).
  21999. */
  22000. explicit basic_storage_impl(const allocator_type &alloc = {})
  22001. : underlying_type{deletion_policy{comp_traits::in_place_delete::value}, alloc}
  22002. {}
  22003. /**
  22004. * @brief Fake get function.
  22005. *
  22006. * @warning
  22007. * Attempting to use an entity that doesn't belong to the storage results in
  22008. * undefined behavior.
  22009. *
  22010. * @param entt A valid entity identifier.
  22011. */
  22012. void get([[maybe_unused]] const entity_type entt) const ENTT_NOEXCEPT {
  22013. ENTT_ASSERT(underlying_type::contains(entt), "Storage does not contain entity");
  22014. }
  22015. /**
  22016. * @brief Assigns an entity to a storage and constructs its object.
  22017. *
  22018. * @warning
  22019. * Attempting to use an entity that already belongs to the storage results
  22020. * in undefined behavior.
  22021. *
  22022. * @tparam Args Types of arguments to use to construct the object.
  22023. * @param entt A valid entity identifier.
  22024. * @param args Parameters to use to construct an object for the entity.
  22025. */
  22026. template<typename... Args>
  22027. void emplace(const entity_type entt, Args &&... args) {
  22028. [[maybe_unused]] value_type instance{std::forward<Args>(args)...};
  22029. underlying_type::emplace(entt);
  22030. }
  22031. /**
  22032. * @brief Updates the instance assigned to a given entity in-place.
  22033. * @tparam Func Types of the function objects to invoke.
  22034. * @param entt A valid entity identifier.
  22035. * @param func Valid function objects.
  22036. */
  22037. template<typename... Func>
  22038. void patch([[maybe_unused]] const entity_type entt, Func &&... func) {
  22039. ENTT_ASSERT(underlying_type::contains(entt), "Storage does not contain entity");
  22040. (std::forward<Func>(func)(), ...);
  22041. }
  22042. /**
  22043. * @brief Assigns one or more entities to a storage.
  22044. *
  22045. * @warning
  22046. * Attempting to assign an entity that already belongs to the storage
  22047. * results in undefined behavior.
  22048. *
  22049. * @tparam It Type of input iterator.
  22050. * @param first An iterator to the first element of the range of entities.
  22051. * @param last An iterator past the last element of the range of entities.
  22052. */
  22053. template<typename It>
  22054. void insert(It first, It last, const value_type & = {}) {
  22055. underlying_type::insert(first, last);
  22056. }
  22057. };
  22058. /**
  22059. * @brief Mixin type to use to wrap basic storage classes.
  22060. * @tparam Type The type of the underlying storage.
  22061. */
  22062. template<typename Type>
  22063. struct storage_adapter_mixin: Type {
  22064. static_assert(std::is_same_v<typename Type::value_type, std::decay_t<typename Type::value_type>>, "Invalid object type");
  22065. /*! @brief Type of the objects assigned to entities. */
  22066. using value_type = typename Type::value_type;
  22067. /*! @brief Underlying entity identifier. */
  22068. using entity_type = typename Type::entity_type;
  22069. /*! @brief Inherited constructors. */
  22070. using Type::Type;
  22071. /**
  22072. * @brief Assigns entities to a storage.
  22073. * @tparam Args Types of arguments to use to construct the object.
  22074. * @param entt A valid entity identifier.
  22075. * @param args Parameters to use to initialize the object.
  22076. * @return A reference to the newly created object.
  22077. */
  22078. template<typename... Args>
  22079. decltype(auto) emplace(basic_registry<entity_type> &, const entity_type entt, Args &&... args) {
  22080. return Type::emplace(entt, std::forward<Args>(args)...);
  22081. }
  22082. /**
  22083. * @brief Assigns entities to a storage.
  22084. * @tparam It Type of input iterator.
  22085. * @tparam Args Types of arguments to use to construct the objects assigned
  22086. * to the entities.
  22087. * @param first An iterator to the first element of the range of entities.
  22088. * @param last An iterator past the last element of the range of entities.
  22089. * @param args Parameters to use to initialize the objects assigned to the
  22090. * entities.
  22091. */
  22092. template<typename It, typename... Args>
  22093. void insert(basic_registry<entity_type> &, It first, It last, Args &&... args) {
  22094. Type::insert(first, last, std::forward<Args>(args)...);
  22095. }
  22096. /**
  22097. * @brief Patches the given instance for an entity.
  22098. * @tparam Func Types of the function objects to invoke.
  22099. * @param entt A valid entity identifier.
  22100. * @param func Valid function objects.
  22101. * @return A reference to the patched instance.
  22102. */
  22103. template<typename... Func>
  22104. decltype(auto) patch(basic_registry<entity_type> &, const entity_type entt, Func &&... func) {
  22105. return Type::patch(entt, std::forward<Func>(func)...);
  22106. }
  22107. };
  22108. /**
  22109. * @brief Mixin type to use to add signal support to storage types.
  22110. * @tparam Type The type of the underlying storage.
  22111. */
  22112. template<typename Type>
  22113. class sigh_storage_mixin final: public Type {
  22114. /*! @copydoc basic_sparse_set::swap_and_pop */
  22115. void swap_and_pop(const typename Type::entity_type entt, void *ud) final {
  22116. ENTT_ASSERT(ud != nullptr, "Invalid pointer to registry");
  22117. destruction.publish(*static_cast<basic_registry<typename Type::entity_type> *>(ud), entt);
  22118. Type::swap_and_pop(entt, ud);
  22119. }
  22120. /*! @copydoc basic_sparse_set::in_place_pop */
  22121. void in_place_pop(const typename Type::entity_type entt, void *ud) final {
  22122. ENTT_ASSERT(ud != nullptr, "Invalid pointer to registry");
  22123. destruction.publish(*static_cast<basic_registry<typename Type::entity_type> *>(ud), entt);
  22124. Type::in_place_pop(entt, ud);
  22125. }
  22126. public:
  22127. /*! @brief Underlying value type. */
  22128. using value_type = typename Type::value_type;
  22129. /*! @brief Underlying entity identifier. */
  22130. using entity_type = typename Type::entity_type;
  22131. /*! @brief Inherited constructors. */
  22132. using Type::Type;
  22133. /**
  22134. * @brief Returns a sink object.
  22135. *
  22136. * The sink returned by this function can be used to receive notifications
  22137. * whenever a new instance is created and assigned to an entity.<br/>
  22138. * The function type for a listener is equivalent to:
  22139. *
  22140. * @code{.cpp}
  22141. * void(basic_registry<entity_type> &, entity_type);
  22142. * @endcode
  22143. *
  22144. * Listeners are invoked **after** the object has been assigned to the
  22145. * entity.
  22146. *
  22147. * @sa sink
  22148. *
  22149. * @return A temporary sink object.
  22150. */
  22151. [[nodiscard]] auto on_construct() ENTT_NOEXCEPT {
  22152. return sink{construction};
  22153. }
  22154. /**
  22155. * @brief Returns a sink object.
  22156. *
  22157. * The sink returned by this function can be used to receive notifications
  22158. * whenever an instance is explicitly updated.<br/>
  22159. * The function type for a listener is equivalent to:
  22160. *
  22161. * @code{.cpp}
  22162. * void(basic_registry<entity_type> &, entity_type);
  22163. * @endcode
  22164. *
  22165. * Listeners are invoked **after** the object has been updated.
  22166. *
  22167. * @sa sink
  22168. *
  22169. * @return A temporary sink object.
  22170. */
  22171. [[nodiscard]] auto on_update() ENTT_NOEXCEPT {
  22172. return sink{update};
  22173. }
  22174. /**
  22175. * @brief Returns a sink object.
  22176. *
  22177. * The sink returned by this function can be used to receive notifications
  22178. * whenever an instance is removed from an entity and thus destroyed.<br/>
  22179. * The function type for a listener is equivalent to:
  22180. *
  22181. * @code{.cpp}
  22182. * void(basic_registry<entity_type> &, entity_type);
  22183. * @endcode
  22184. *
  22185. * Listeners are invoked **before** the object has been removed from the
  22186. * entity.
  22187. *
  22188. * @sa sink
  22189. *
  22190. * @return A temporary sink object.
  22191. */
  22192. [[nodiscard]] auto on_destroy() ENTT_NOEXCEPT {
  22193. return sink{destruction};
  22194. }
  22195. /**
  22196. * @brief Assigns entities to a storage.
  22197. * @tparam Args Types of arguments to use to construct the object.
  22198. * @param owner The registry that issued the request.
  22199. * @param entt A valid entity identifier.
  22200. * @param args Parameters to use to initialize the object.
  22201. * @return A reference to the newly created object.
  22202. */
  22203. template<typename... Args>
  22204. decltype(auto) emplace(basic_registry<entity_type> &owner, const entity_type entt, Args &&... args) {
  22205. Type::emplace(entt, std::forward<Args>(args)...);
  22206. construction.publish(owner, entt);
  22207. return this->get(entt);
  22208. }
  22209. /**
  22210. * @brief Assigns entities to a storage.
  22211. * @tparam It Type of input iterator.
  22212. * @tparam Args Types of arguments to use to construct the objects assigned
  22213. * to the entities.
  22214. * @param owner The registry that issued the request.
  22215. * @param first An iterator to the first element of the range of entities.
  22216. * @param last An iterator past the last element of the range of entities.
  22217. * @param args Parameters to use to initialize the objects assigned to the
  22218. * entities.
  22219. */
  22220. template<typename It, typename... Args>
  22221. void insert(basic_registry<entity_type> &owner, It first, It last, Args &&... args) {
  22222. Type::insert(first, last, std::forward<Args>(args)...);
  22223. if(!construction.empty()) {
  22224. for(; first != last; ++first) {
  22225. construction.publish(owner, *first);
  22226. }
  22227. }
  22228. }
  22229. /**
  22230. * @brief Patches the given instance for an entity.
  22231. * @tparam Func Types of the function objects to invoke.
  22232. * @param owner The registry that issued the request.
  22233. * @param entt A valid entity identifier.
  22234. * @param func Valid function objects.
  22235. * @return A reference to the patched instance.
  22236. */
  22237. template<typename... Func>
  22238. decltype(auto) patch(basic_registry<entity_type> &owner, const entity_type entt, Func &&... func) {
  22239. Type::patch(entt, std::forward<Func>(func)...);
  22240. update.publish(owner, entt);
  22241. return this->get(entt);
  22242. }
  22243. private:
  22244. sigh<void(basic_registry<entity_type> &, const entity_type)> construction{};
  22245. sigh<void(basic_registry<entity_type> &, const entity_type)> destruction{};
  22246. sigh<void(basic_registry<entity_type> &, const entity_type)> update{};
  22247. };
  22248. /**
  22249. * @brief Storage implementation dispatcher.
  22250. * @tparam Entity A valid entity type (see entt_traits for more details).
  22251. * @tparam Type Type of objects assigned to the entities.
  22252. * @tparam Allocator Type of allocator used to manage memory and elements.
  22253. */
  22254. template<typename Entity, typename Type, typename Allocator>
  22255. struct basic_storage: basic_storage_impl<Entity, Type, Allocator> {
  22256. using basic_storage_impl<Entity, Type, Allocator>::basic_storage_impl;
  22257. };
  22258. /**
  22259. * @brief Provides a common way to access certain properties of storage types.
  22260. * @tparam Entity A valid entity type (see entt_traits for more details).
  22261. * @tparam Type Type of objects managed by the storage class.
  22262. */
  22263. template<typename Entity, typename Type, typename = void>
  22264. struct storage_traits {
  22265. /*! @brief Resulting type after component-to-storage conversion. */
  22266. using storage_type = sigh_storage_mixin<basic_storage<Entity, Type>>;
  22267. };
  22268. /**
  22269. * @brief Gets the element assigned to an entity from a storage, if any.
  22270. * @tparam Type Storage type.
  22271. * @param container A valid instance of a storage class.
  22272. * @param entt A valid entity identifier.
  22273. * @return A possibly empty tuple containing the requested element.
  22274. */
  22275. template<typename Type>
  22276. [[nodiscard]] auto get_as_tuple([[maybe_unused]] Type &container, [[maybe_unused]] const typename Type::entity_type entt) {
  22277. static_assert(std::is_same_v<std::remove_const_t<Type>, typename storage_traits<typename Type::entity_type, typename Type::value_type>::storage_type>, "Invalid storage");
  22278. if constexpr(std::is_void_v<decltype(container.get({}))>) {
  22279. return std::make_tuple();
  22280. } else {
  22281. return std::forward_as_tuple(container.get(entt));
  22282. }
  22283. }
  22284. }
  22285. #endif
  22286. // #include "entity/utility.hpp"
  22287. #ifndef ENTT_ENTITY_UTILITY_HPP
  22288. #define ENTT_ENTITY_UTILITY_HPP
  22289. // #include "../core/type_traits.hpp"
  22290. namespace entt {
  22291. /**
  22292. * @brief Alias for exclusion lists.
  22293. * @tparam Type List of types.
  22294. */
  22295. template<typename... Type>
  22296. struct exclude_t: type_list<Type...> {};
  22297. /**
  22298. * @brief Variable template for exclusion lists.
  22299. * @tparam Type List of types.
  22300. */
  22301. template<typename... Type>
  22302. inline constexpr exclude_t<Type...> exclude{};
  22303. /**
  22304. * @brief Alias for lists of observed components.
  22305. * @tparam Type List of types.
  22306. */
  22307. template<typename... Type>
  22308. struct get_t: type_list<Type...>{};
  22309. /**
  22310. * @brief Variable template for lists of observed components.
  22311. * @tparam Type List of types.
  22312. */
  22313. template<typename... Type>
  22314. inline constexpr get_t<Type...> get{};
  22315. }
  22316. #endif
  22317. // #include "entity/view.hpp"
  22318. #ifndef ENTT_ENTITY_VIEW_HPP
  22319. #define ENTT_ENTITY_VIEW_HPP
  22320. #include <iterator>
  22321. #include <array>
  22322. #include <tuple>
  22323. #include <utility>
  22324. #include <algorithm>
  22325. #include <type_traits>
  22326. // #include "../config/config.h"
  22327. // #include "../core/type_traits.hpp"
  22328. // #include "component.hpp"
  22329. // #include "entity.hpp"
  22330. // #include "fwd.hpp"
  22331. // #include "sparse_set.hpp"
  22332. // #include "storage.hpp"
  22333. // #include "utility.hpp"
  22334. namespace entt {
  22335. /**
  22336. * @cond TURN_OFF_DOXYGEN
  22337. * Internal details not to be documented.
  22338. */
  22339. namespace internal {
  22340. template<typename Policy, typename It, std::size_t AllOf, std::size_t NoneOf>
  22341. class view_iterator final {
  22342. using basic_common_type = basic_sparse_set<typename std::iterator_traits<It>::value_type>;
  22343. [[nodiscard]] bool valid() const {
  22344. const auto entt = *it;
  22345. return Policy::accept(entt)
  22346. && std::apply([entt](const auto *... curr) { return (curr->contains(entt) && ...); }, pools)
  22347. && std::apply([entt](const auto *... curr) { return (!curr->contains(entt) && ...); }, filter);
  22348. }
  22349. public:
  22350. using iterator_type = It;
  22351. using difference_type = typename std::iterator_traits<It>::difference_type;
  22352. using value_type = typename std::iterator_traits<It>::value_type;
  22353. using pointer = typename std::iterator_traits<It>::pointer;
  22354. using reference = typename std::iterator_traits<It>::reference;
  22355. using iterator_category = std::bidirectional_iterator_tag;
  22356. view_iterator() ENTT_NOEXCEPT
  22357. : first{},
  22358. last{},
  22359. it{},
  22360. pools{},
  22361. filter{}
  22362. {}
  22363. view_iterator(It from, It to, It curr, std::array<const basic_common_type *, AllOf> all_of, std::array<const basic_common_type *, NoneOf> none_of) ENTT_NOEXCEPT
  22364. : first{from},
  22365. last{to},
  22366. it{curr},
  22367. pools{all_of},
  22368. filter{none_of}
  22369. {
  22370. if(it != last && !valid()) {
  22371. ++(*this);
  22372. }
  22373. }
  22374. view_iterator & operator++() ENTT_NOEXCEPT {
  22375. while(++it != last && !valid());
  22376. return *this;
  22377. }
  22378. view_iterator operator++(int) ENTT_NOEXCEPT {
  22379. view_iterator orig = *this;
  22380. return ++(*this), orig;
  22381. }
  22382. view_iterator & operator--() ENTT_NOEXCEPT {
  22383. while(--it != first && !valid());
  22384. return *this;
  22385. }
  22386. view_iterator operator--(int) ENTT_NOEXCEPT {
  22387. view_iterator orig = *this;
  22388. return operator--(), orig;
  22389. }
  22390. [[nodiscard]] bool operator==(const view_iterator &other) const ENTT_NOEXCEPT {
  22391. return other.it == it;
  22392. }
  22393. [[nodiscard]] bool operator!=(const view_iterator &other) const ENTT_NOEXCEPT {
  22394. return !(*this == other);
  22395. }
  22396. [[nodiscard]] pointer operator->() const {
  22397. return &*it;
  22398. }
  22399. [[nodiscard]] reference operator*() const {
  22400. return *operator->();
  22401. }
  22402. private:
  22403. It first;
  22404. It last;
  22405. It it;
  22406. std::array<const basic_common_type *, AllOf> pools;
  22407. std::array<const basic_common_type *, NoneOf> filter;
  22408. };
  22409. }
  22410. /**
  22411. * Internal details not to be documented.
  22412. * @endcond
  22413. */
  22414. /*! @brief Stable storage policy, aimed at pointer stability. */
  22415. struct stable_storage_policy {
  22416. /**
  22417. * @cond TURN_OFF_DOXYGEN
  22418. * Internal details not to be documented.
  22419. */
  22420. template<typename Entity>
  22421. [[nodiscard]] static constexpr bool accept(const Entity entity) ENTT_NOEXCEPT {
  22422. return entity != tombstone;
  22423. }
  22424. /**
  22425. * Internal details not to be documented.
  22426. * @endcond
  22427. */
  22428. };
  22429. /*! @brief Packed storage policy, aimed at faster linear iteration. */
  22430. struct packed_storage_policy {
  22431. /**
  22432. * @cond TURN_OFF_DOXYGEN
  22433. * Internal details not to be documented.
  22434. */
  22435. template<typename Entity>
  22436. [[nodiscard]] static constexpr bool accept(const Entity) ENTT_NOEXCEPT {
  22437. return true;
  22438. }
  22439. /**
  22440. * Internal details not to be documented.
  22441. * @endcond
  22442. */
  22443. };
  22444. /**
  22445. * @brief View implementation.
  22446. *
  22447. * Primary template isn't defined on purpose. All the specializations give a
  22448. * compile-time error, but for a few reasonable cases.
  22449. */
  22450. template<typename...>
  22451. class basic_view_impl;
  22452. /*! @brief View implementation dispatcher. */
  22453. template<typename...>
  22454. struct basic_view;
  22455. /**
  22456. * @brief Multi component view.
  22457. *
  22458. * Multi component views iterate over those entities that have at least all the
  22459. * given components in their bags. During initialization, a multi component view
  22460. * looks at the number of entities available for each component and uses the
  22461. * smallest set in order to get a performance boost when iterate.
  22462. *
  22463. * @b Important
  22464. *
  22465. * Iterators aren't invalidated if:
  22466. *
  22467. * * New instances of the given components are created and assigned to entities.
  22468. * * The entity currently pointed is modified (as an example, if one of the
  22469. * given components is removed from the entity to which the iterator points).
  22470. * * The entity currently pointed is destroyed.
  22471. *
  22472. * In all other cases, modifying the pools iterated by the view in any way
  22473. * invalidates all the iterators and using them results in undefined behavior.
  22474. *
  22475. * @note
  22476. * Views share references to the underlying data structures of the registry that
  22477. * generated them. Therefore any change to the entities and to the components
  22478. * made by means of the registry are immediately reflected by views.
  22479. *
  22480. * @warning
  22481. * Lifetime of a view must not overcome that of the registry that generated it.
  22482. * In any other case, attempting to use a view results in undefined behavior.
  22483. *
  22484. * @tparam Policy Common (stricter) storage policy.
  22485. * @tparam Entity A valid entity type (see entt_traits for more details).
  22486. * @tparam Exclude Types of components used to filter the view.
  22487. * @tparam Component Types of components iterated by the view.
  22488. */
  22489. template<typename Policy, typename Entity, typename... Exclude, typename... Component>
  22490. class basic_view_impl<Policy, Entity, exclude_t<Exclude...>, Component...> {
  22491. using basic_common_type = basic_sparse_set<Entity>;
  22492. template<typename Comp>
  22493. using storage_type = constness_as_t<typename storage_traits<Entity, std::remove_const_t<Comp>>::storage_type, Comp>;
  22494. class iterable final {
  22495. template<typename It>
  22496. struct iterable_iterator final {
  22497. using difference_type = std::ptrdiff_t;
  22498. using value_type = decltype(std::tuple_cat(std::tuple<Entity>{}, std::declval<basic_view_impl>().get({})));
  22499. using pointer = void;
  22500. using reference = value_type;
  22501. using iterator_category = std::input_iterator_tag;
  22502. iterable_iterator(It from, const basic_view_impl *parent) ENTT_NOEXCEPT
  22503. : it{from},
  22504. view{parent}
  22505. {}
  22506. iterable_iterator & operator++() ENTT_NOEXCEPT {
  22507. return ++it, *this;
  22508. }
  22509. iterable_iterator operator++(int) ENTT_NOEXCEPT {
  22510. iterable_iterator orig = *this;
  22511. return ++(*this), orig;
  22512. }
  22513. [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
  22514. return std::tuple_cat(std::make_tuple(*it), view->get(*it));
  22515. }
  22516. [[nodiscard]] bool operator==(const iterable_iterator &other) const ENTT_NOEXCEPT {
  22517. return other.it == it;
  22518. }
  22519. [[nodiscard]] bool operator!=(const iterable_iterator &other) const ENTT_NOEXCEPT {
  22520. return !(*this == other);
  22521. }
  22522. private:
  22523. It it;
  22524. const basic_view_impl *view;
  22525. };
  22526. public:
  22527. using iterator = iterable_iterator<internal::view_iterator<Policy, typename basic_common_type::iterator, sizeof...(Component) - 1u, sizeof...(Exclude)>>;
  22528. using reverse_iterator = iterable_iterator<internal::view_iterator<Policy, typename basic_common_type::reverse_iterator, sizeof...(Component) - 1u, sizeof...(Exclude)>>;
  22529. iterable(const basic_view_impl &parent)
  22530. : view{parent}
  22531. {}
  22532. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  22533. return { view.begin(), &view };
  22534. }
  22535. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  22536. return { view.end(), &view };
  22537. }
  22538. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  22539. return { view.rbegin(), &view };
  22540. }
  22541. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  22542. return { view.rend(), &view };
  22543. }
  22544. private:
  22545. const basic_view_impl view;
  22546. };
  22547. [[nodiscard]] const auto * candidate() const ENTT_NOEXCEPT {
  22548. return (std::min)({ static_cast<const basic_common_type *>(std::get<storage_type<Component> *>(pools))... }, [](const auto *lhs, const auto *rhs) {
  22549. return lhs->size() < rhs->size();
  22550. });
  22551. }
  22552. [[nodiscard]] auto pools_to_unchecked_array() const ENTT_NOEXCEPT {
  22553. std::size_t pos{};
  22554. std::array<const basic_common_type *, sizeof...(Component) - 1u> other{};
  22555. (static_cast<void>(std::get<storage_type<Component> *>(pools) == view ? void() : void(other[pos++] = std::get<storage_type<Component> *>(pools))), ...);
  22556. return other;
  22557. }
  22558. [[nodiscard]] auto filter_to_array() const ENTT_NOEXCEPT {
  22559. return std::array<const basic_common_type *, sizeof...(Exclude)>{std::get<const storage_type<Exclude> *>(filter)...};
  22560. }
  22561. template<typename Comp, typename It>
  22562. [[nodiscard]] auto dispatch_get([[maybe_unused]] It &it, [[maybe_unused]] const Entity entt) const {
  22563. if constexpr(std::is_same_v<typename std::iterator_traits<It>::value_type, typename storage_type<Comp>::value_type>) {
  22564. return std::forward_as_tuple(*it);
  22565. } else {
  22566. return get_as_tuple(*std::get<storage_type<Comp> *>(pools), entt);
  22567. }
  22568. }
  22569. template<typename Comp, typename Func>
  22570. void traverse(Func func) const {
  22571. if constexpr(std::is_void_v<decltype(std::get<storage_type<Comp> *>(pools)->get({}))>) {
  22572. for(const auto entt: static_cast<const basic_common_type &>(*std::get<storage_type<Comp> *>(pools))) {
  22573. if(Policy::accept(entt) && ((std::is_same_v<Comp, Component> || std::get<storage_type<Component> *>(pools)->contains(entt)) && ...)
  22574. && (!std::get<const storage_type<Exclude> *>(filter)->contains(entt) && ...))
  22575. {
  22576. if constexpr(is_applicable_v<Func, decltype(std::tuple_cat(std::tuple<entity_type>{}, std::declval<basic_view_impl>().get({})))>) {
  22577. std::apply(func, std::tuple_cat(std::make_tuple(entt), get(entt)));
  22578. } else {
  22579. std::apply(func, get(entt));
  22580. }
  22581. }
  22582. }
  22583. } else {
  22584. auto it = std::get<storage_type<Comp> *>(pools)->begin();
  22585. for(const auto entt: static_cast<const basic_common_type &>(*std::get<storage_type<Comp> *>(pools))) {
  22586. if(Policy::accept(entt) && ((std::is_same_v<Comp, Component> || std::get<storage_type<Component> *>(pools)->contains(entt)) && ...)
  22587. && (!std::get<const storage_type<Exclude> *>(filter)->contains(entt) && ...))
  22588. {
  22589. if constexpr(is_applicable_v<Func, decltype(std::tuple_cat(std::tuple<entity_type>{}, std::declval<basic_view_impl>().get({})))>) {
  22590. std::apply(func, std::tuple_cat(std::make_tuple(entt), dispatch_get<Component>(it, entt)...));
  22591. } else {
  22592. std::apply(func, std::tuple_cat(dispatch_get<Component>(it, entt)...));
  22593. }
  22594. }
  22595. ++it;
  22596. }
  22597. }
  22598. }
  22599. public:
  22600. /*! @brief Underlying entity identifier. */
  22601. using entity_type = Entity;
  22602. /*! @brief Unsigned integer type. */
  22603. using size_type = std::size_t;
  22604. /*! @brief Bidirectional iterator type. */
  22605. using iterator = internal::view_iterator<Policy, typename basic_common_type::iterator, sizeof...(Component) - 1u, sizeof...(Exclude)>;
  22606. /*! @brief Reverse iterator type. */
  22607. using reverse_iterator = internal::view_iterator<Policy, typename basic_common_type::reverse_iterator, sizeof...(Component) - 1u, sizeof...(Exclude)>;
  22608. /*! @brief Iterable view type. */
  22609. using iterable_view = iterable;
  22610. /*! @brief Default constructor to use to create empty, invalid views. */
  22611. basic_view_impl() ENTT_NOEXCEPT
  22612. : view{}
  22613. {}
  22614. /**
  22615. * @brief Constructs a multi-type view from a set of storage classes.
  22616. * @param component The storage for the types to iterate.
  22617. * @param epool The storage for the types used to filter the view.
  22618. */
  22619. basic_view_impl(storage_type<Component> &... component, const storage_type<Exclude> &... epool) ENTT_NOEXCEPT
  22620. : pools{&component...},
  22621. filter{&epool...},
  22622. view{candidate()}
  22623. {}
  22624. /**
  22625. * @brief Forces the type to use to drive iterations.
  22626. * @tparam Comp Type of component to use to drive the iteration.
  22627. */
  22628. template<typename Comp>
  22629. void use() const ENTT_NOEXCEPT {
  22630. view = std::get<storage_type<Comp> *>(pools);
  22631. }
  22632. /**
  22633. * @brief Estimates the number of entities iterated by the view.
  22634. * @return Estimated number of entities iterated by the view.
  22635. */
  22636. [[nodiscard]] size_type size_hint() const ENTT_NOEXCEPT {
  22637. return view->size();
  22638. }
  22639. /**
  22640. * @brief Returns an iterator to the first entity of the view.
  22641. *
  22642. * The returned iterator points to the first entity of the view. If the view
  22643. * is empty, the returned iterator will be equal to `end()`.
  22644. *
  22645. * @return An iterator to the first entity of the view.
  22646. */
  22647. [[nodiscard]] iterator begin() const {
  22648. return iterator{view->begin(), view->end(), view->begin(), pools_to_unchecked_array(), filter_to_array()};
  22649. }
  22650. /**
  22651. * @brief Returns an iterator that is past the last entity of the view.
  22652. *
  22653. * The returned iterator points to the entity following the last entity of
  22654. * the view. Attempting to dereference the returned iterator results in
  22655. * undefined behavior.
  22656. *
  22657. * @return An iterator to the entity following the last entity of the view.
  22658. */
  22659. [[nodiscard]] iterator end() const {
  22660. return iterator{view->begin(), view->end(), view->end(), pools_to_unchecked_array(), filter_to_array()};
  22661. }
  22662. /**
  22663. * @brief Returns an iterator to the first entity of the reversed view.
  22664. *
  22665. * The returned iterator points to the first entity of the reversed view. If
  22666. * the view is empty, the returned iterator will be equal to `rend()`.
  22667. *
  22668. * @return An iterator to the first entity of the reversed view.
  22669. */
  22670. [[nodiscard]] reverse_iterator rbegin() const {
  22671. return reverse_iterator{view->rbegin(), view->rend(), view->rbegin(), pools_to_unchecked_array(), filter_to_array()};
  22672. }
  22673. /**
  22674. * @brief Returns an iterator that is past the last entity of the reversed
  22675. * view.
  22676. *
  22677. * The returned iterator points to the entity following the last entity of
  22678. * the reversed view. Attempting to dereference the returned iterator
  22679. * results in undefined behavior.
  22680. *
  22681. * @return An iterator to the entity following the last entity of the
  22682. * reversed view.
  22683. */
  22684. [[nodiscard]] reverse_iterator rend() const {
  22685. return reverse_iterator{view->rbegin(), view->rend(), view->rend(), pools_to_unchecked_array(), filter_to_array()};
  22686. }
  22687. /**
  22688. * @brief Returns the first entity of the view, if any.
  22689. * @return The first entity of the view if one exists, the null entity
  22690. * otherwise.
  22691. */
  22692. [[nodiscard]] entity_type front() const {
  22693. const auto it = begin();
  22694. return it != end() ? *it : null;
  22695. }
  22696. /**
  22697. * @brief Returns the last entity of the view, if any.
  22698. * @return The last entity of the view if one exists, the null entity
  22699. * otherwise.
  22700. */
  22701. [[nodiscard]] entity_type back() const {
  22702. const auto it = rbegin();
  22703. return it != rend() ? *it : null;
  22704. }
  22705. /**
  22706. * @brief Finds an entity.
  22707. * @param entt A valid entity identifier.
  22708. * @return An iterator to the given entity if it's found, past the end
  22709. * iterator otherwise.
  22710. */
  22711. [[nodiscard]] iterator find(const entity_type entt) const {
  22712. const auto it = iterator{view->begin(), view->end(), view->find(entt), pools_to_unchecked_array(), filter_to_array()};
  22713. return (it != end() && *it == entt) ? it : end();
  22714. }
  22715. /**
  22716. * @brief Checks if a view is properly initialized.
  22717. * @return True if the view is properly initialized, false otherwise.
  22718. */
  22719. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  22720. return view != nullptr;
  22721. }
  22722. /**
  22723. * @brief Checks if a view contains an entity.
  22724. * @param entt A valid entity identifier.
  22725. * @return True if the view contains the given entity, false otherwise.
  22726. */
  22727. [[nodiscard]] bool contains(const entity_type entt) const {
  22728. return (std::get<storage_type<Component> *>(pools)->contains(entt) && ...) && (!std::get<const storage_type<Exclude> *>(filter)->contains(entt) && ...);
  22729. }
  22730. /**
  22731. * @brief Returns the components assigned to the given entity.
  22732. *
  22733. * Prefer this function instead of `registry::get` during iterations. It has
  22734. * far better performance than its counterpart.
  22735. *
  22736. * @warning
  22737. * Attempting to use an invalid component type results in a compilation
  22738. * error. Attempting to use an entity that doesn't belong to the view
  22739. * results in undefined behavior.
  22740. *
  22741. * @tparam Comp Types of components to get.
  22742. * @param entt A valid entity identifier.
  22743. * @return The components assigned to the entity.
  22744. */
  22745. template<typename... Comp>
  22746. [[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entt) const {
  22747. ENTT_ASSERT(contains(entt), "View does not contain entity");
  22748. if constexpr(sizeof...(Comp) == 0) {
  22749. return std::tuple_cat(get_as_tuple(*std::get<storage_type<Component> *>(pools), entt)...);
  22750. } else if constexpr(sizeof...(Comp) == 1) {
  22751. return (std::get<storage_type<Comp> *>(pools)->get(entt), ...);
  22752. } else {
  22753. return std::tuple_cat(get_as_tuple(*std::get<storage_type<Comp> *>(pools), entt)...);
  22754. }
  22755. }
  22756. /**
  22757. * @brief Iterates entities and components and applies the given function
  22758. * object to them.
  22759. *
  22760. * The function object is invoked for each entity. It is provided with the
  22761. * entity itself and a set of references to non-empty components. The
  22762. * _constness_ of the components is as requested.<br/>
  22763. * The signature of the function must be equivalent to one of the following
  22764. * forms:
  22765. *
  22766. * @code{.cpp}
  22767. * void(const entity_type, Type &...);
  22768. * void(Type &...);
  22769. * @endcode
  22770. *
  22771. * @note
  22772. * Empty types aren't explicitly instantiated and therefore they are never
  22773. * returned during iterations.
  22774. *
  22775. * @tparam Func Type of the function object to invoke.
  22776. * @param func A valid function object.
  22777. */
  22778. template<typename Func>
  22779. void each(Func func) const {
  22780. ((std::get<storage_type<Component> *>(pools) == view ? traverse<Component>(std::move(func)) : void()), ...);
  22781. }
  22782. /**
  22783. * @brief Iterates entities and components and applies the given function
  22784. * object to them.
  22785. *
  22786. * The pool of the suggested component is used to lead the iterations. The
  22787. * returned entities will therefore respect the order of the pool associated
  22788. * with that type.
  22789. *
  22790. * @sa each
  22791. *
  22792. * @tparam Comp Type of component to use to drive the iteration.
  22793. * @tparam Func Type of the function object to invoke.
  22794. * @param func A valid function object.
  22795. */
  22796. template<typename Comp, typename Func>
  22797. void each(Func func) const {
  22798. use<Comp>();
  22799. traverse<Comp>(std::move(func));
  22800. }
  22801. /**
  22802. * @brief Returns an iterable object to use to _visit_ the view.
  22803. *
  22804. * The iterable object returns tuples that contain the current entity and a
  22805. * set of references to its non-empty components. The _constness_ of the
  22806. * components is as requested.
  22807. *
  22808. * @note
  22809. * Empty types aren't explicitly instantiated and therefore they are never
  22810. * returned during iterations.
  22811. *
  22812. * @return An iterable object to use to _visit_ the view.
  22813. */
  22814. [[nodiscard]] iterable_view each() const ENTT_NOEXCEPT {
  22815. return iterable_view{*this};
  22816. }
  22817. /**
  22818. * @brief Returns an iterable object to use to _visit_ the view.
  22819. *
  22820. * The pool of the suggested component is used to lead the iterations. The
  22821. * returned elements will therefore respect the order of the pool associated
  22822. * with that type.
  22823. *
  22824. * @sa each
  22825. *
  22826. * @tparam Comp Type of component to use to drive the iteration.
  22827. * @return An iterable object to use to _visit_ the view.
  22828. */
  22829. template<typename Comp>
  22830. [[nodiscard]] iterable_view each() const ENTT_NOEXCEPT {
  22831. use<Comp>();
  22832. return iterable_view{*this};
  22833. }
  22834. /**
  22835. * @brief Combines two views in a _more specific_ one (friend function).
  22836. * @tparam Id A valid entity type (see entt_traits for more details).
  22837. * @tparam ELhs Filter list of the first view.
  22838. * @tparam CLhs Component list of the first view.
  22839. * @tparam ERhs Filter list of the second view.
  22840. * @tparam CRhs Component list of the second view.
  22841. * @return A more specific view.
  22842. */
  22843. template<typename Id, typename... ELhs, typename... CLhs, typename... ERhs, typename... CRhs>
  22844. friend auto operator|(const basic_view<Id, exclude_t<ELhs...>, CLhs...> &, const basic_view<Id, exclude_t<ERhs...>, CRhs...> &);
  22845. private:
  22846. const std::tuple<storage_type<Component> *...> pools;
  22847. const std::tuple<const storage_type<Exclude> *...> filter;
  22848. mutable const basic_common_type *view;
  22849. };
  22850. /**
  22851. * @brief Single component view specialization.
  22852. *
  22853. * Single component views are specialized in order to get a boost in terms of
  22854. * performance. This kind of views can access the underlying data structure
  22855. * directly and avoid superfluous checks.
  22856. *
  22857. * @b Important
  22858. *
  22859. * Iterators aren't invalidated if:
  22860. *
  22861. * * New instances of the given component are created and assigned to entities.
  22862. * * The entity currently pointed is modified (as an example, the given
  22863. * component is removed from the entity to which the iterator points).
  22864. * * The entity currently pointed is destroyed.
  22865. *
  22866. * In all other cases, modifying the pool iterated by the view in any way
  22867. * invalidates all the iterators and using them results in undefined behavior.
  22868. *
  22869. * @note
  22870. * Views share a reference to the underlying data structure of the registry that
  22871. * generated them. Therefore any change to the entities and to the components
  22872. * made by means of the registry are immediately reflected by views.
  22873. *
  22874. * @warning
  22875. * Lifetime of a view must not overcome that of the registry that generated it.
  22876. * In any other case, attempting to use a view results in undefined behavior.
  22877. *
  22878. * @tparam Entity A valid entity type (see entt_traits for more details).
  22879. * @tparam Component Type of component iterated by the view.
  22880. */
  22881. template<typename Entity, typename Component>
  22882. class basic_view_impl<packed_storage_policy, Entity, exclude_t<>, Component> {
  22883. using basic_common_type = basic_sparse_set<Entity>;
  22884. using storage_type = constness_as_t<typename storage_traits<Entity, std::remove_const_t<Component>>::storage_type, Component>;
  22885. class iterable final {
  22886. template<typename... It>
  22887. struct iterable_iterator final {
  22888. using difference_type = std::ptrdiff_t;
  22889. using value_type = decltype(std::tuple_cat(std::tuple<Entity>{}, std::declval<basic_view_impl>().get({})));
  22890. using pointer = void;
  22891. using reference = value_type;
  22892. using iterator_category = std::input_iterator_tag;
  22893. template<typename... Discard>
  22894. iterable_iterator(It... from, Discard...) ENTT_NOEXCEPT
  22895. : it{from...}
  22896. {}
  22897. iterable_iterator & operator++() ENTT_NOEXCEPT {
  22898. return (++std::get<It>(it), ...), *this;
  22899. }
  22900. iterable_iterator operator++(int) ENTT_NOEXCEPT {
  22901. iterable_iterator orig = *this;
  22902. return ++(*this), orig;
  22903. }
  22904. [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
  22905. return { *std::get<It>(it)... };
  22906. }
  22907. [[nodiscard]] bool operator==(const iterable_iterator &other) const ENTT_NOEXCEPT {
  22908. return std::get<0>(other.it) == std::get<0>(it);
  22909. }
  22910. [[nodiscard]] bool operator!=(const iterable_iterator &other) const ENTT_NOEXCEPT {
  22911. return !(*this == other);
  22912. }
  22913. private:
  22914. std::tuple<It...> it;
  22915. };
  22916. public:
  22917. using iterator = std::conditional_t<
  22918. std::is_void_v<decltype(std::declval<storage_type>().get({}))>,
  22919. iterable_iterator<typename basic_common_type::iterator>,
  22920. iterable_iterator<typename basic_common_type::iterator, decltype(std::declval<storage_type>().begin())>
  22921. >;
  22922. using reverse_iterator = std::conditional_t<
  22923. std::is_void_v<decltype(std::declval<storage_type>().get({}))>,
  22924. iterable_iterator<typename basic_common_type::reverse_iterator>,
  22925. iterable_iterator<typename basic_common_type::reverse_iterator, decltype(std::declval<storage_type>().rbegin())>
  22926. >;
  22927. iterable(storage_type &ref)
  22928. : pool{&ref}
  22929. {}
  22930. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  22931. return iterator{pool->basic_common_type::begin(), pool->begin()};
  22932. }
  22933. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  22934. return iterator{pool->basic_common_type::end(), pool->end()};
  22935. }
  22936. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  22937. return reverse_iterator{pool->basic_common_type::rbegin(), pool->rbegin()};
  22938. }
  22939. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  22940. return reverse_iterator{pool->basic_common_type::rend(), pool->rend()};
  22941. }
  22942. private:
  22943. storage_type * const pool;
  22944. };
  22945. public:
  22946. /*! @brief Underlying entity identifier. */
  22947. using entity_type = Entity;
  22948. /*! @brief Unsigned integer type. */
  22949. using size_type = std::size_t;
  22950. /*! @brief Random access iterator type. */
  22951. using iterator = typename basic_common_type::iterator;
  22952. /*! @brief Reversed iterator type. */
  22953. using reverse_iterator = typename basic_common_type::reverse_iterator;
  22954. /*! @brief Iterable view type. */
  22955. using iterable_view = iterable;
  22956. /*! @brief Default constructor to use to create empty, invalid views. */
  22957. basic_view_impl() ENTT_NOEXCEPT
  22958. : pools{},
  22959. filter{}
  22960. {}
  22961. /**
  22962. * @brief Constructs a single-type view from a storage class.
  22963. * @param ref The storage for the type to iterate.
  22964. */
  22965. basic_view_impl(storage_type &ref) ENTT_NOEXCEPT
  22966. : pools{&ref},
  22967. filter{}
  22968. {}
  22969. /**
  22970. * @brief Returns the number of entities that have the given component.
  22971. * @return Number of entities that have the given component.
  22972. */
  22973. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  22974. return std::get<0>(pools)->size();
  22975. }
  22976. /**
  22977. * @brief Checks whether a view is empty.
  22978. * @return True if the view is empty, false otherwise.
  22979. */
  22980. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  22981. return std::get<0>(pools)->empty();
  22982. }
  22983. /**
  22984. * @brief Direct access to the raw representation offered by the storage.
  22985. * @return A pointer to the array of components.
  22986. */
  22987. [[nodiscard]] auto raw() const ENTT_NOEXCEPT {
  22988. return std::get<0>(pools)->raw();
  22989. }
  22990. /**
  22991. * @brief Direct access to the list of entities.
  22992. *
  22993. * The returned pointer is such that range `[data(), data() + size())` is
  22994. * always a valid range, even if the container is empty.
  22995. *
  22996. * @return A pointer to the array of entities.
  22997. */
  22998. [[nodiscard]] auto data() const ENTT_NOEXCEPT {
  22999. return std::get<0>(pools)->data();
  23000. }
  23001. /**
  23002. * @brief Returns an iterator to the first entity of the view.
  23003. *
  23004. * The returned iterator points to the first entity of the view. If the view
  23005. * is empty, the returned iterator will be equal to `end()`.
  23006. *
  23007. * @return An iterator to the first entity of the view.
  23008. */
  23009. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  23010. return std::get<0>(pools)->basic_common_type::begin();
  23011. }
  23012. /**
  23013. * @brief Returns an iterator that is past the last entity of the view.
  23014. *
  23015. * The returned iterator points to the entity following the last entity of
  23016. * the view. Attempting to dereference the returned iterator results in
  23017. * undefined behavior.
  23018. *
  23019. * @return An iterator to the entity following the last entity of the view.
  23020. */
  23021. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  23022. return std::get<0>(pools)->basic_common_type::end();
  23023. }
  23024. /**
  23025. * @brief Returns an iterator to the first entity of the reversed view.
  23026. *
  23027. * The returned iterator points to the first entity of the reversed view. If
  23028. * the view is empty, the returned iterator will be equal to `rend()`.
  23029. *
  23030. * @return An iterator to the first entity of the reversed view.
  23031. */
  23032. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  23033. return std::get<0>(pools)->basic_common_type::rbegin();
  23034. }
  23035. /**
  23036. * @brief Returns an iterator that is past the last entity of the reversed
  23037. * view.
  23038. *
  23039. * The returned iterator points to the entity following the last entity of
  23040. * the reversed view. Attempting to dereference the returned iterator
  23041. * results in undefined behavior.
  23042. *
  23043. * @return An iterator to the entity following the last entity of the
  23044. * reversed view.
  23045. */
  23046. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  23047. return std::get<0>(pools)->basic_common_type::rend();
  23048. }
  23049. /**
  23050. * @brief Returns the first entity of the view, if any.
  23051. * @return The first entity of the view if one exists, the null entity
  23052. * otherwise.
  23053. */
  23054. [[nodiscard]] entity_type front() const {
  23055. const auto it = begin();
  23056. return it != end() ? *it : null;
  23057. }
  23058. /**
  23059. * @brief Returns the last entity of the view, if any.
  23060. * @return The last entity of the view if one exists, the null entity
  23061. * otherwise.
  23062. */
  23063. [[nodiscard]] entity_type back() const {
  23064. const auto it = rbegin();
  23065. return it != rend() ? *it : null;
  23066. }
  23067. /**
  23068. * @brief Finds an entity.
  23069. * @param entt A valid entity identifier.
  23070. * @return An iterator to the given entity if it's found, past the end
  23071. * iterator otherwise.
  23072. */
  23073. [[nodiscard]] iterator find(const entity_type entt) const {
  23074. const auto it = std::get<0>(pools)->find(entt);
  23075. return it != end() && *it == entt ? it : end();
  23076. }
  23077. /**
  23078. * @brief Returns the identifier that occupies the given position.
  23079. * @param pos Position of the element to return.
  23080. * @return The identifier that occupies the given position.
  23081. */
  23082. [[nodiscard]] entity_type operator[](const size_type pos) const {
  23083. return begin()[pos];
  23084. }
  23085. /**
  23086. * @brief Checks if a view is properly initialized.
  23087. * @return True if the view is properly initialized, false otherwise.
  23088. */
  23089. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  23090. return std::get<0>(pools) != nullptr;
  23091. }
  23092. /**
  23093. * @brief Checks if a view contains an entity.
  23094. * @param entt A valid entity identifier.
  23095. * @return True if the view contains the given entity, false otherwise.
  23096. */
  23097. [[nodiscard]] bool contains(const entity_type entt) const {
  23098. return std::get<0>(pools)->contains(entt);
  23099. }
  23100. /**
  23101. * @brief Returns the component assigned to the given entity.
  23102. *
  23103. * Prefer this function instead of `registry::get` during iterations. It has
  23104. * far better performance than its counterpart.
  23105. *
  23106. * @warning
  23107. * Attempting to use an invalid component type results in a compilation
  23108. * error. Attempting to use an entity that doesn't belong to the view
  23109. * results in undefined behavior.
  23110. *
  23111. * @tparam Comp Types of components to get.
  23112. * @param entt A valid entity identifier.
  23113. * @return The component assigned to the entity.
  23114. */
  23115. template<typename... Comp>
  23116. [[nodiscard]] decltype(auto) get(const entity_type entt) const {
  23117. ENTT_ASSERT(contains(entt), "View does not contain entity");
  23118. if constexpr(sizeof...(Comp) == 0) {
  23119. return get_as_tuple(*std::get<0>(pools), entt);
  23120. } else {
  23121. static_assert(std::is_same_v<Comp..., Component>, "Invalid component type");
  23122. return std::get<0>(pools)->get(entt);
  23123. }
  23124. }
  23125. /**
  23126. * @brief Iterates entities and components and applies the given function
  23127. * object to them.
  23128. *
  23129. * The function object is invoked for each entity. It is provided with the
  23130. * entity itself and a reference to the component if it's a non-empty one.
  23131. * The _constness_ of the component is as requested.<br/>
  23132. * The signature of the function must be equivalent to one of the following
  23133. * forms:
  23134. *
  23135. * @code{.cpp}
  23136. * void(const entity_type, Component &);
  23137. * void(Component &);
  23138. * @endcode
  23139. *
  23140. * @note
  23141. * Empty types aren't explicitly instantiated and therefore they are never
  23142. * returned during iterations.
  23143. *
  23144. * @tparam Func Type of the function object to invoke.
  23145. * @param func A valid function object.
  23146. */
  23147. template<typename Func>
  23148. void each(Func func) const {
  23149. if constexpr(std::is_void_v<decltype(std::get<0>(pools)->get({}))>) {
  23150. if constexpr(std::is_invocable_v<Func>) {
  23151. for(auto pos = size(); pos; --pos) {
  23152. func();
  23153. }
  23154. } else {
  23155. for(auto entity: *this) {
  23156. func(entity);
  23157. }
  23158. }
  23159. } else {
  23160. if constexpr(is_applicable_v<Func, decltype(*each().begin())>) {
  23161. for(const auto pack: each()) {
  23162. std::apply(func, pack);
  23163. }
  23164. } else {
  23165. for(auto &&component: *std::get<0>(pools)) {
  23166. func(component);
  23167. }
  23168. }
  23169. }
  23170. }
  23171. /**
  23172. * @brief Returns an iterable object to use to _visit_ the view.
  23173. *
  23174. * The iterable object returns tuples that contain the current entity and a
  23175. * reference to its component if it's a non-empty one. The _constness_ of
  23176. * the component is as requested.
  23177. *
  23178. * @note
  23179. * Empty types aren't explicitly instantiated and therefore they are never
  23180. * returned during iterations.
  23181. *
  23182. * @return An iterable object to use to _visit_ the view.
  23183. */
  23184. [[nodiscard]] iterable_view each() const ENTT_NOEXCEPT {
  23185. return iterable_view{*std::get<0>(pools)};
  23186. }
  23187. /**
  23188. * @brief Combines two views in a _more specific_ one (friend function).
  23189. * @tparam Id A valid entity type (see entt_traits for more details).
  23190. * @tparam ELhs Filter list of the first view.
  23191. * @tparam CLhs Component list of the first view.
  23192. * @tparam ERhs Filter list of the second view.
  23193. * @tparam CRhs Component list of the second view.
  23194. * @return A more specific view.
  23195. */
  23196. template<typename Id, typename... ELhs, typename... CLhs, typename... ERhs, typename... CRhs>
  23197. friend auto operator|(const basic_view<Id, exclude_t<ELhs...>, CLhs...> &, const basic_view<Id, exclude_t<ERhs...>, CRhs...> &);
  23198. private:
  23199. const std::tuple<storage_type *> pools;
  23200. const std::tuple<> filter;
  23201. };
  23202. /**
  23203. * @brief View implementation dispatcher.
  23204. * @tparam Entity A valid entity type (see entt_traits for more details).
  23205. * @tparam Exclude Types of components used to filter the view.
  23206. * @tparam Component Types of components iterated by the view.
  23207. */
  23208. template<typename Entity, typename... Exclude, typename... Component>
  23209. struct basic_view<Entity, exclude_t<Exclude...>, Component...>
  23210. : basic_view_impl<std::conditional_t<std::disjunction_v<typename component_traits<std::remove_const_t<Component>>::in_place_delete...>, stable_storage_policy, packed_storage_policy>, Entity, exclude_t<Exclude...>, Component...>
  23211. {
  23212. /*! @brief Most restrictive storage policy of all component types. */
  23213. using storage_policy = std::conditional_t<std::disjunction_v<typename component_traits<std::remove_const_t<Component>>::in_place_delete...>, stable_storage_policy, packed_storage_policy>;
  23214. using basic_view_impl<storage_policy, Entity, exclude_t<Exclude...>, Component...>::basic_view_impl;
  23215. };
  23216. /**
  23217. * @brief Deduction guide.
  23218. * @tparam Storage Type of storage classes used to create the view.
  23219. * @param storage The storage for the types to iterate.
  23220. */
  23221. template<typename... Storage>
  23222. basic_view(Storage &... storage)
  23223. -> basic_view<std::common_type_t<typename Storage::entity_type...>, entt::exclude_t<>, constness_as_t<typename Storage::value_type, Storage>...>;
  23224. /**
  23225. * @brief Combines two views in a _more specific_ one.
  23226. * @tparam Entity A valid entity type (see entt_traits for more details).
  23227. * @tparam ELhs Filter list of the first view.
  23228. * @tparam CLhs Component list of the first view.
  23229. * @tparam ERhs Filter list of the second view.
  23230. * @tparam CRhs Component list of the second view.
  23231. * @param lhs A valid reference to the first view.
  23232. * @param rhs A valid reference to the second view.
  23233. * @return A more specific view.
  23234. */
  23235. template<typename Entity, typename... ELhs, typename... CLhs, typename... ERhs, typename... CRhs>
  23236. [[nodiscard]] auto operator|(const basic_view<Entity, exclude_t<ELhs...>, CLhs...> &lhs, const basic_view<Entity, exclude_t<ERhs...>, CRhs...> &rhs) {
  23237. using view_type = basic_view<Entity, exclude_t<ELhs..., ERhs...>, CLhs..., CRhs...>;
  23238. return std::apply([](auto *... storage) { return view_type{*storage...}; }, std::tuple_cat(lhs.pools, rhs.pools, lhs.filter, rhs.filter));
  23239. }
  23240. }
  23241. #endif
  23242. // #include "locator/locator.hpp"
  23243. #ifndef ENTT_LOCATOR_LOCATOR_HPP
  23244. #define ENTT_LOCATOR_LOCATOR_HPP
  23245. #include <memory>
  23246. #include <utility>
  23247. // #include "../config/config.h"
  23248. #ifndef ENTT_CONFIG_CONFIG_H
  23249. #define ENTT_CONFIG_CONFIG_H
  23250. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  23251. # define ENTT_NOEXCEPT noexcept
  23252. # define ENTT_THROW throw
  23253. # define ENTT_TRY try
  23254. # define ENTT_CATCH catch(...)
  23255. #else
  23256. # define ENTT_NOEXCEPT
  23257. # define ENTT_THROW
  23258. # define ENTT_TRY if(true)
  23259. # define ENTT_CATCH if(false)
  23260. #endif
  23261. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  23262. # include <new>
  23263. # define ENTT_LAUNDER(expr) std::launder(expr)
  23264. #else
  23265. # define ENTT_LAUNDER(expr) expr
  23266. #endif
  23267. #ifndef ENTT_USE_ATOMIC
  23268. # define ENTT_MAYBE_ATOMIC(Type) Type
  23269. #else
  23270. # include <atomic>
  23271. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  23272. #endif
  23273. #ifndef ENTT_ID_TYPE
  23274. # include <cstdint>
  23275. # define ENTT_ID_TYPE std::uint32_t
  23276. #endif
  23277. #ifdef ENTT_SPARSE_PAGE
  23278. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  23279. #else
  23280. # define ENTT_SPARSE_PAGE 4096
  23281. #endif
  23282. #ifdef ENTT_PACKED_PAGE
  23283. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  23284. #else
  23285. # define ENTT_PACKED_PAGE 1024
  23286. #endif
  23287. #ifdef ENTT_DISABLE_ASSERT
  23288. # undef ENTT_ASSERT
  23289. # define ENTT_ASSERT(...) (void(0))
  23290. #elif !defined ENTT_ASSERT
  23291. # include <cassert>
  23292. # define ENTT_ASSERT(condition, ...) assert(condition)
  23293. #endif
  23294. #ifdef ENTT_NO_ETO
  23295. # include <type_traits>
  23296. # define ENTT_IGNORE_IF_EMPTY std::false_type
  23297. #else
  23298. # include <type_traits>
  23299. # define ENTT_IGNORE_IF_EMPTY std::true_type
  23300. #endif
  23301. #ifndef ENTT_STANDARD_CPP
  23302. # if defined __clang__ || defined __GNUC__
  23303. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  23304. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  23305. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  23306. # elif defined _MSC_VER
  23307. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  23308. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  23309. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  23310. # endif
  23311. #endif
  23312. #endif
  23313. namespace entt {
  23314. /**
  23315. * @brief Service locator, nothing more.
  23316. *
  23317. * A service locator can be used to do what it promises: locate services.<br/>
  23318. * Usually service locators are tightly bound to the services they expose and
  23319. * thus it's hard to define a general purpose class to do that. This template
  23320. * based implementation tries to fill the gap and to get rid of the burden of
  23321. * defining a different specific locator for each application.
  23322. *
  23323. * @tparam Service Type of service managed by the locator.
  23324. */
  23325. template<typename Service>
  23326. struct service_locator {
  23327. /*! @brief Type of service offered. */
  23328. using service_type = Service;
  23329. /*! @brief Default constructor, deleted on purpose. */
  23330. service_locator() = delete;
  23331. /*! @brief Default destructor, deleted on purpose. */
  23332. ~service_locator() = delete;
  23333. /**
  23334. * @brief Tests if a valid service implementation is set.
  23335. * @return True if the service is set, false otherwise.
  23336. */
  23337. [[nodiscard]] static bool empty() ENTT_NOEXCEPT {
  23338. return !static_cast<bool>(service);
  23339. }
  23340. /**
  23341. * @brief Returns a weak pointer to a service implementation, if any.
  23342. *
  23343. * Clients of a service shouldn't retain references to it. The recommended
  23344. * way is to retrieve the service implementation currently set each and
  23345. * every time the need of using it arises. Otherwise users can incur in
  23346. * unexpected behaviors.
  23347. *
  23348. * @return A reference to the service implementation currently set, if any.
  23349. */
  23350. [[nodiscard]] static std::weak_ptr<Service> get() ENTT_NOEXCEPT {
  23351. return service;
  23352. }
  23353. /**
  23354. * @brief Returns a weak reference to a service implementation, if any.
  23355. *
  23356. * Clients of a service shouldn't retain references to it. The recommended
  23357. * way is to retrieve the service implementation currently set each and
  23358. * every time the need of using it arises. Otherwise users can incur in
  23359. * unexpected behaviors.
  23360. *
  23361. * @warning
  23362. * In case no service implementation has been set, a call to this function
  23363. * results in undefined behavior.
  23364. *
  23365. * @return A reference to the service implementation currently set, if any.
  23366. */
  23367. [[nodiscard]] static Service & ref() ENTT_NOEXCEPT {
  23368. return *service;
  23369. }
  23370. /**
  23371. * @brief Sets or replaces a service.
  23372. * @tparam Impl Type of the new service to use.
  23373. * @tparam Args Types of arguments to use to construct the service.
  23374. * @param args Parameters to use to construct the service.
  23375. */
  23376. template<typename Impl = Service, typename... Args>
  23377. static void set(Args &&... args) {
  23378. service = std::make_shared<Impl>(std::forward<Args>(args)...);
  23379. }
  23380. /**
  23381. * @brief Sets or replaces a service.
  23382. * @param ptr Service to use to replace the current one.
  23383. */
  23384. static void set(std::shared_ptr<Service> ptr) {
  23385. ENTT_ASSERT(static_cast<bool>(ptr), "Null service not allowed");
  23386. service = std::move(ptr);
  23387. }
  23388. /**
  23389. * @brief Resets a service.
  23390. *
  23391. * The service is no longer valid after a reset.
  23392. */
  23393. static void reset() {
  23394. service.reset();
  23395. }
  23396. private:
  23397. inline static std::shared_ptr<Service> service = nullptr;
  23398. };
  23399. }
  23400. #endif
  23401. // #include "meta/adl_pointer.hpp"
  23402. #ifndef ENTT_META_ADL_POINTER_HPP
  23403. #define ENTT_META_ADL_POINTER_HPP
  23404. namespace entt {
  23405. /**
  23406. * @brief ADL based lookup function for dereferencing meta pointer-like types.
  23407. * @tparam Type Element type.
  23408. * @param value A pointer-like object.
  23409. * @return The value returned from the dereferenced pointer.
  23410. */
  23411. template<typename Type>
  23412. decltype(auto) dereference_meta_pointer_like(const Type &value) {
  23413. return *value;
  23414. }
  23415. /**
  23416. * @brief Fake ADL based lookup function for meta pointer-like types.
  23417. * @tparam Type Element type.
  23418. */
  23419. template<typename Type>
  23420. struct adl_meta_pointer_like {
  23421. /**
  23422. * @brief Uses the default ADL based lookup method to resolve the call.
  23423. * @param value A pointer-like object.
  23424. * @return The value returned from the dereferenced pointer.
  23425. */
  23426. static decltype(auto) dereference(const Type &value) {
  23427. return dereference_meta_pointer_like(value);
  23428. }
  23429. };
  23430. }
  23431. #endif
  23432. // #include "meta/container.hpp"
  23433. #ifndef ENTT_META_CONTAINER_HPP
  23434. #define ENTT_META_CONTAINER_HPP
  23435. #include <array>
  23436. #include <map>
  23437. #include <set>
  23438. #include <type_traits>
  23439. #include <unordered_map>
  23440. #include <unordered_set>
  23441. #include <utility>
  23442. #include <vector>
  23443. // #include "../config/config.h"
  23444. #ifndef ENTT_CONFIG_CONFIG_H
  23445. #define ENTT_CONFIG_CONFIG_H
  23446. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  23447. # define ENTT_NOEXCEPT noexcept
  23448. # define ENTT_THROW throw
  23449. # define ENTT_TRY try
  23450. # define ENTT_CATCH catch(...)
  23451. #else
  23452. # define ENTT_NOEXCEPT
  23453. # define ENTT_THROW
  23454. # define ENTT_TRY if(true)
  23455. # define ENTT_CATCH if(false)
  23456. #endif
  23457. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  23458. # include <new>
  23459. # define ENTT_LAUNDER(expr) std::launder(expr)
  23460. #else
  23461. # define ENTT_LAUNDER(expr) expr
  23462. #endif
  23463. #ifndef ENTT_USE_ATOMIC
  23464. # define ENTT_MAYBE_ATOMIC(Type) Type
  23465. #else
  23466. # include <atomic>
  23467. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  23468. #endif
  23469. #ifndef ENTT_ID_TYPE
  23470. # include <cstdint>
  23471. # define ENTT_ID_TYPE std::uint32_t
  23472. #endif
  23473. #ifdef ENTT_SPARSE_PAGE
  23474. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  23475. #else
  23476. # define ENTT_SPARSE_PAGE 4096
  23477. #endif
  23478. #ifdef ENTT_PACKED_PAGE
  23479. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  23480. #else
  23481. # define ENTT_PACKED_PAGE 1024
  23482. #endif
  23483. #ifdef ENTT_DISABLE_ASSERT
  23484. # undef ENTT_ASSERT
  23485. # define ENTT_ASSERT(...) (void(0))
  23486. #elif !defined ENTT_ASSERT
  23487. # include <cassert>
  23488. # define ENTT_ASSERT(condition, ...) assert(condition)
  23489. #endif
  23490. #ifdef ENTT_NO_ETO
  23491. # include <type_traits>
  23492. # define ENTT_IGNORE_IF_EMPTY std::false_type
  23493. #else
  23494. # include <type_traits>
  23495. # define ENTT_IGNORE_IF_EMPTY std::true_type
  23496. #endif
  23497. #ifndef ENTT_STANDARD_CPP
  23498. # if defined __clang__ || defined __GNUC__
  23499. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  23500. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  23501. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  23502. # elif defined _MSC_VER
  23503. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  23504. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  23505. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  23506. # endif
  23507. #endif
  23508. #endif
  23509. // #include "../core/type_traits.hpp"
  23510. #ifndef ENTT_CORE_TYPE_TRAITS_HPP
  23511. #define ENTT_CORE_TYPE_TRAITS_HPP
  23512. #include <cstddef>
  23513. #include <iterator>
  23514. #include <type_traits>
  23515. #include <utility>
  23516. // #include "../config/config.h"
  23517. #ifndef ENTT_CONFIG_CONFIG_H
  23518. #define ENTT_CONFIG_CONFIG_H
  23519. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  23520. # define ENTT_NOEXCEPT noexcept
  23521. # define ENTT_THROW throw
  23522. # define ENTT_TRY try
  23523. # define ENTT_CATCH catch(...)
  23524. #else
  23525. # define ENTT_NOEXCEPT
  23526. # define ENTT_THROW
  23527. # define ENTT_TRY if(true)
  23528. # define ENTT_CATCH if(false)
  23529. #endif
  23530. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  23531. # include <new>
  23532. # define ENTT_LAUNDER(expr) std::launder(expr)
  23533. #else
  23534. # define ENTT_LAUNDER(expr) expr
  23535. #endif
  23536. #ifndef ENTT_USE_ATOMIC
  23537. # define ENTT_MAYBE_ATOMIC(Type) Type
  23538. #else
  23539. # include <atomic>
  23540. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  23541. #endif
  23542. #ifndef ENTT_ID_TYPE
  23543. # include <cstdint>
  23544. # define ENTT_ID_TYPE std::uint32_t
  23545. #endif
  23546. #ifdef ENTT_SPARSE_PAGE
  23547. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  23548. #else
  23549. # define ENTT_SPARSE_PAGE 4096
  23550. #endif
  23551. #ifdef ENTT_PACKED_PAGE
  23552. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  23553. #else
  23554. # define ENTT_PACKED_PAGE 1024
  23555. #endif
  23556. #ifdef ENTT_DISABLE_ASSERT
  23557. # undef ENTT_ASSERT
  23558. # define ENTT_ASSERT(...) (void(0))
  23559. #elif !defined ENTT_ASSERT
  23560. # include <cassert>
  23561. # define ENTT_ASSERT(condition, ...) assert(condition)
  23562. #endif
  23563. #ifdef ENTT_NO_ETO
  23564. # include <type_traits>
  23565. # define ENTT_IGNORE_IF_EMPTY std::false_type
  23566. #else
  23567. # include <type_traits>
  23568. # define ENTT_IGNORE_IF_EMPTY std::true_type
  23569. #endif
  23570. #ifndef ENTT_STANDARD_CPP
  23571. # if defined __clang__ || defined __GNUC__
  23572. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  23573. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  23574. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  23575. # elif defined _MSC_VER
  23576. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  23577. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  23578. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  23579. # endif
  23580. #endif
  23581. #endif
  23582. // #include "fwd.hpp"
  23583. #ifndef ENTT_CORE_FWD_HPP
  23584. #define ENTT_CORE_FWD_HPP
  23585. #include <type_traits>
  23586. // #include "../config/config.h"
  23587. namespace entt {
  23588. template<std::size_t Len = sizeof(double[2]), std::size_t = alignof(typename std::aligned_storage_t<Len + !Len>)>
  23589. class basic_any;
  23590. /*! @brief Alias declaration for type identifiers. */
  23591. using id_type = ENTT_ID_TYPE;
  23592. /*! @brief Alias declaration for the most common use case. */
  23593. using any = basic_any<>;
  23594. }
  23595. #endif
  23596. namespace entt {
  23597. /**
  23598. * @brief Utility class to disambiguate overloaded functions.
  23599. * @tparam N Number of choices available.
  23600. */
  23601. template<std::size_t N>
  23602. struct choice_t
  23603. // Unfortunately, doxygen cannot parse such a construct.
  23604. /*! @cond TURN_OFF_DOXYGEN */
  23605. : choice_t<N-1>
  23606. /*! @endcond */
  23607. {};
  23608. /*! @copybrief choice_t */
  23609. template<>
  23610. struct choice_t<0> {};
  23611. /**
  23612. * @brief Variable template for the choice trick.
  23613. * @tparam N Number of choices available.
  23614. */
  23615. template<std::size_t N>
  23616. inline constexpr choice_t<N> choice{};
  23617. /**
  23618. * @brief Identity type trait.
  23619. *
  23620. * Useful to establish non-deduced contexts in template argument deduction
  23621. * (waiting for C++20) or to provide types through function arguments.
  23622. *
  23623. * @tparam Type A type.
  23624. */
  23625. template<typename Type>
  23626. struct type_identity {
  23627. /*! @brief Identity type. */
  23628. using type = Type;
  23629. };
  23630. /**
  23631. * @brief Helper type.
  23632. * @tparam Type A type.
  23633. */
  23634. template<typename Type>
  23635. using type_identity_t = typename type_identity<Type>::type;
  23636. /**
  23637. * @brief A type-only `sizeof` wrapper that returns 0 where `sizeof` complains.
  23638. * @tparam Type The type of which to return the size.
  23639. * @tparam The size of the type if `sizeof` accepts it, 0 otherwise.
  23640. */
  23641. template<typename Type, typename = void>
  23642. struct size_of: std::integral_constant<std::size_t, 0u> {};
  23643. /*! @copydoc size_of */
  23644. template<typename Type>
  23645. struct size_of<Type, std::void_t<decltype(sizeof(Type))>>
  23646. : std::integral_constant<std::size_t, sizeof(Type)>
  23647. {};
  23648. /**
  23649. * @brief Helper variable template.
  23650. * @tparam Type The type of which to return the size.
  23651. */
  23652. template<class Type>
  23653. inline constexpr std::size_t size_of_v = size_of<Type>::value;
  23654. /**
  23655. * @brief Using declaration to be used to _repeat_ the same type a number of
  23656. * times equal to the size of a given parameter pack.
  23657. * @tparam Type A type to repeat.
  23658. */
  23659. template<typename Type, typename>
  23660. using unpack_as_t = Type;
  23661. /**
  23662. * @brief Helper variable template to be used to _repeat_ the same value a
  23663. * number of times equal to the size of a given parameter pack.
  23664. * @tparam Value A value to repeat.
  23665. */
  23666. template<auto Value, typename>
  23667. inline constexpr auto unpack_as_v = Value;
  23668. /**
  23669. * @brief Wraps a static constant.
  23670. * @tparam Value A static constant.
  23671. */
  23672. template<auto Value>
  23673. using integral_constant = std::integral_constant<decltype(Value), Value>;
  23674. /**
  23675. * @brief Alias template to facilitate the creation of named values.
  23676. * @tparam Value A constant value at least convertible to `id_type`.
  23677. */
  23678. template<id_type Value>
  23679. using tag = integral_constant<Value>;
  23680. /**
  23681. * @brief A class to use to push around lists of types, nothing more.
  23682. * @tparam Type Types provided by the type list.
  23683. */
  23684. template<typename... Type>
  23685. struct type_list {
  23686. /*! @brief Type list type. */
  23687. using type = type_list;
  23688. /*! @brief Compile-time number of elements in the type list. */
  23689. static constexpr auto size = sizeof...(Type);
  23690. };
  23691. /*! @brief Primary template isn't defined on purpose. */
  23692. template<std::size_t, typename>
  23693. struct type_list_element;
  23694. /**
  23695. * @brief Provides compile-time indexed access to the types of a type list.
  23696. * @tparam Index Index of the type to return.
  23697. * @tparam Type First type provided by the type list.
  23698. * @tparam Other Other types provided by the type list.
  23699. */
  23700. template<std::size_t Index, typename Type, typename... Other>
  23701. struct type_list_element<Index, type_list<Type, Other...>>
  23702. : type_list_element<Index - 1u, type_list<Other...>>
  23703. {};
  23704. /**
  23705. * @brief Provides compile-time indexed access to the types of a type list.
  23706. * @tparam Type First type provided by the type list.
  23707. * @tparam Other Other types provided by the type list.
  23708. */
  23709. template<typename Type, typename... Other>
  23710. struct type_list_element<0u, type_list<Type, Other...>> {
  23711. /*! @brief Searched type. */
  23712. using type = Type;
  23713. };
  23714. /**
  23715. * @brief Helper type.
  23716. * @tparam Index Index of the type to return.
  23717. * @tparam List Type list to search into.
  23718. */
  23719. template<std::size_t Index, typename List>
  23720. using type_list_element_t = typename type_list_element<Index, List>::type;
  23721. /**
  23722. * @brief Concatenates multiple type lists.
  23723. * @tparam Type Types provided by the first type list.
  23724. * @tparam Other Types provided by the second type list.
  23725. * @return A type list composed by the types of both the type lists.
  23726. */
  23727. template<typename... Type, typename... Other>
  23728. constexpr type_list<Type..., Other...> operator+(type_list<Type...>, type_list<Other...>) { return {}; }
  23729. /*! @brief Primary template isn't defined on purpose. */
  23730. template<typename...>
  23731. struct type_list_cat;
  23732. /*! @brief Concatenates multiple type lists. */
  23733. template<>
  23734. struct type_list_cat<> {
  23735. /*! @brief A type list composed by the types of all the type lists. */
  23736. using type = type_list<>;
  23737. };
  23738. /**
  23739. * @brief Concatenates multiple type lists.
  23740. * @tparam Type Types provided by the first type list.
  23741. * @tparam Other Types provided by the second type list.
  23742. * @tparam List Other type lists, if any.
  23743. */
  23744. template<typename... Type, typename... Other, typename... List>
  23745. struct type_list_cat<type_list<Type...>, type_list<Other...>, List...> {
  23746. /*! @brief A type list composed by the types of all the type lists. */
  23747. using type = typename type_list_cat<type_list<Type..., Other...>, List...>::type;
  23748. };
  23749. /**
  23750. * @brief Concatenates multiple type lists.
  23751. * @tparam Type Types provided by the type list.
  23752. */
  23753. template<typename... Type>
  23754. struct type_list_cat<type_list<Type...>> {
  23755. /*! @brief A type list composed by the types of all the type lists. */
  23756. using type = type_list<Type...>;
  23757. };
  23758. /**
  23759. * @brief Helper type.
  23760. * @tparam List Type lists to concatenate.
  23761. */
  23762. template<typename... List>
  23763. using type_list_cat_t = typename type_list_cat<List...>::type;
  23764. /*! @brief Primary template isn't defined on purpose. */
  23765. template<typename>
  23766. struct type_list_unique;
  23767. /**
  23768. * @brief Removes duplicates types from a type list.
  23769. * @tparam Type One of the types provided by the given type list.
  23770. * @tparam Other The other types provided by the given type list.
  23771. */
  23772. template<typename Type, typename... Other>
  23773. struct type_list_unique<type_list<Type, Other...>> {
  23774. /*! @brief A type list without duplicate types. */
  23775. using type = std::conditional_t<
  23776. std::disjunction_v<std::is_same<Type, Other>...>,
  23777. typename type_list_unique<type_list<Other...>>::type,
  23778. type_list_cat_t<type_list<Type>, typename type_list_unique<type_list<Other...>>::type>
  23779. >;
  23780. };
  23781. /*! @brief Removes duplicates types from a type list. */
  23782. template<>
  23783. struct type_list_unique<type_list<>> {
  23784. /*! @brief A type list without duplicate types. */
  23785. using type = type_list<>;
  23786. };
  23787. /**
  23788. * @brief Helper type.
  23789. * @tparam Type A type list.
  23790. */
  23791. template<typename Type>
  23792. using type_list_unique_t = typename type_list_unique<Type>::type;
  23793. /**
  23794. * @brief Provides the member constant `value` to true if a type list contains a
  23795. * given type, false otherwise.
  23796. * @tparam List Type list.
  23797. * @tparam Type Type to look for.
  23798. */
  23799. template<typename List, typename Type>
  23800. struct type_list_contains;
  23801. /**
  23802. * @copybrief type_list_contains
  23803. * @tparam Type Types provided by the type list.
  23804. * @tparam Other Type to look for.
  23805. */
  23806. template<typename... Type, typename Other>
  23807. struct type_list_contains<type_list<Type...>, Other>: std::disjunction<std::is_same<Type, Other>...> {};
  23808. /**
  23809. * @brief Helper variable template.
  23810. * @tparam List Type list.
  23811. * @tparam Type Type to look for.
  23812. */
  23813. template<class List, typename Type>
  23814. inline constexpr bool type_list_contains_v = type_list_contains<List, Type>::value;
  23815. /*! @brief Primary template isn't defined on purpose. */
  23816. template<typename...>
  23817. struct type_list_diff;
  23818. /**
  23819. * @brief Computes the difference between two type lists.
  23820. * @tparam Type Types provided by the first type list.
  23821. * @tparam Other Types provided by the second type list.
  23822. */
  23823. template<typename... Type, typename... Other>
  23824. struct type_list_diff<type_list<Type...>, type_list<Other...>> {
  23825. /*! @brief A type list that is the difference between the two type lists. */
  23826. using type = type_list_cat_t<std::conditional_t<type_list_contains_v<type_list<Other...>, Type>, type_list<>, type_list<Type>>...>;
  23827. };
  23828. /**
  23829. * @brief Helper type.
  23830. * @tparam List Type lists between which to compute the difference.
  23831. */
  23832. template<typename... List>
  23833. using type_list_diff_t = typename type_list_diff<List...>::type;
  23834. /**
  23835. * @brief A class to use to push around lists of constant values, nothing more.
  23836. * @tparam Value Values provided by the value list.
  23837. */
  23838. template<auto... Value>
  23839. struct value_list {
  23840. /*! @brief Value list type. */
  23841. using type = value_list;
  23842. /*! @brief Compile-time number of elements in the value list. */
  23843. static constexpr auto size = sizeof...(Value);
  23844. };
  23845. /*! @brief Primary template isn't defined on purpose. */
  23846. template<std::size_t, typename>
  23847. struct value_list_element;
  23848. /**
  23849. * @brief Provides compile-time indexed access to the values of a value list.
  23850. * @tparam Index Index of the value to return.
  23851. * @tparam Value First value provided by the value list.
  23852. * @tparam Other Other values provided by the value list.
  23853. */
  23854. template<std::size_t Index, auto Value, auto... Other>
  23855. struct value_list_element<Index, value_list<Value, Other...>>
  23856. : value_list_element<Index - 1u, value_list<Other...>>
  23857. {};
  23858. /**
  23859. * @brief Provides compile-time indexed access to the types of a type list.
  23860. * @tparam Value First value provided by the value list.
  23861. * @tparam Other Other values provided by the value list.
  23862. */
  23863. template<auto Value, auto... Other>
  23864. struct value_list_element<0u, value_list<Value, Other...>> {
  23865. /*! @brief Searched value. */
  23866. static constexpr auto value = Value;
  23867. };
  23868. /**
  23869. * @brief Helper type.
  23870. * @tparam Index Index of the value to return.
  23871. * @tparam List Value list to search into.
  23872. */
  23873. template<std::size_t Index, typename List>
  23874. inline constexpr auto value_list_element_v = value_list_element<Index, List>::value;
  23875. /**
  23876. * @brief Concatenates multiple value lists.
  23877. * @tparam Value Values provided by the first value list.
  23878. * @tparam Other Values provided by the second value list.
  23879. * @return A value list composed by the values of both the value lists.
  23880. */
  23881. template<auto... Value, auto... Other>
  23882. constexpr value_list<Value..., Other...> operator+(value_list<Value...>, value_list<Other...>) { return {}; }
  23883. /*! @brief Primary template isn't defined on purpose. */
  23884. template<typename...>
  23885. struct value_list_cat;
  23886. /*! @brief Concatenates multiple value lists. */
  23887. template<>
  23888. struct value_list_cat<> {
  23889. /*! @brief A value list composed by the values of all the value lists. */
  23890. using type = value_list<>;
  23891. };
  23892. /**
  23893. * @brief Concatenates multiple value lists.
  23894. * @tparam Value Values provided by the first value list.
  23895. * @tparam Other Values provided by the second value list.
  23896. * @tparam List Other value lists, if any.
  23897. */
  23898. template<auto... Value, auto... Other, typename... List>
  23899. struct value_list_cat<value_list<Value...>, value_list<Other...>, List...> {
  23900. /*! @brief A value list composed by the values of all the value lists. */
  23901. using type = typename value_list_cat<value_list<Value..., Other...>, List...>::type;
  23902. };
  23903. /**
  23904. * @brief Concatenates multiple value lists.
  23905. * @tparam Value Values provided by the value list.
  23906. */
  23907. template<auto... Value>
  23908. struct value_list_cat<value_list<Value...>> {
  23909. /*! @brief A value list composed by the values of all the value lists. */
  23910. using type = value_list<Value...>;
  23911. };
  23912. /**
  23913. * @brief Helper type.
  23914. * @tparam List Value lists to concatenate.
  23915. */
  23916. template<typename... List>
  23917. using value_list_cat_t = typename value_list_cat<List...>::type;
  23918. /**
  23919. * @cond TURN_OFF_DOXYGEN
  23920. * Internal details not to be documented.
  23921. */
  23922. namespace internal {
  23923. template<typename>
  23924. [[nodiscard]] constexpr bool is_equality_comparable(...) { return false; }
  23925. template<typename Type>
  23926. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<0>)
  23927. -> decltype(std::declval<Type>() == std::declval<Type>()) { return true; }
  23928. template<typename Type>
  23929. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<1>)
  23930. -> decltype(std::declval<typename Type::value_type>(), std::declval<Type>() == std::declval<Type>()) {
  23931. if constexpr(std::is_same_v<typename Type::value_type, Type>) {
  23932. return is_equality_comparable<Type>(choice<0>);
  23933. } else {
  23934. return is_equality_comparable<typename Type::value_type>(choice<2>);
  23935. }
  23936. }
  23937. template<typename Type>
  23938. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<2>)
  23939. -> decltype(std::declval<typename Type::mapped_type>(), std::declval<Type>() == std::declval<Type>()) {
  23940. return is_equality_comparable<typename Type::key_type>(choice<2>) && is_equality_comparable<typename Type::mapped_type>(choice<2>);
  23941. }
  23942. }
  23943. /**
  23944. * Internal details not to be documented.
  23945. * @endcond
  23946. */
  23947. /**
  23948. * @brief Provides the member constant `value` to true if a given type is
  23949. * equality comparable, false otherwise.
  23950. * @tparam Type The type to test.
  23951. */
  23952. template<typename Type, typename = void>
  23953. struct is_equality_comparable: std::bool_constant<internal::is_equality_comparable<Type>(choice<2>)> {};
  23954. /**
  23955. * @brief Helper variable template.
  23956. * @tparam Type The type to test.
  23957. */
  23958. template<class Type>
  23959. inline constexpr bool is_equality_comparable_v = is_equality_comparable<Type>::value;
  23960. /*! @brief Same as std::is_invocable, but with tuples. */
  23961. template<typename, typename>
  23962. struct is_applicable: std::false_type {};
  23963. /**
  23964. * @copybrief is_applicable
  23965. * @tparam Func A valid function type.
  23966. * @tparam Tuple Tuple-like type.
  23967. * @tparam Args The list of arguments to use to probe the function type.
  23968. */
  23969. template<typename Func, template<typename...> class Tuple, typename... Args>
  23970. struct is_applicable<Func, Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  23971. /**
  23972. * @copybrief is_applicable
  23973. * @tparam Func A valid function type.
  23974. * @tparam Tuple Tuple-like type.
  23975. * @tparam Args The list of arguments to use to probe the function type.
  23976. */
  23977. template<typename Func, template<typename...> class Tuple, typename... Args>
  23978. struct is_applicable<Func, const Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  23979. /**
  23980. * @brief Helper variable template.
  23981. * @tparam Func A valid function type.
  23982. * @tparam Args The list of arguments to use to probe the function type.
  23983. */
  23984. template<typename Func, typename Args>
  23985. inline constexpr bool is_applicable_v = is_applicable<Func, Args>::value;
  23986. /*! @brief Same as std::is_invocable_r, but with tuples for arguments. */
  23987. template<typename, typename, typename>
  23988. struct is_applicable_r: std::false_type {};
  23989. /**
  23990. * @copybrief is_applicable_r
  23991. * @tparam Ret The type to which the return type of the function should be
  23992. * convertible.
  23993. * @tparam Func A valid function type.
  23994. * @tparam Args The list of arguments to use to probe the function type.
  23995. */
  23996. template<typename Ret, typename Func, typename... Args>
  23997. struct is_applicable_r<Ret, Func, std::tuple<Args...>>: std::is_invocable_r<Ret, Func, Args...> {};
  23998. /**
  23999. * @brief Helper variable template.
  24000. * @tparam Ret The type to which the return type of the function should be
  24001. * convertible.
  24002. * @tparam Func A valid function type.
  24003. * @tparam Args The list of arguments to use to probe the function type.
  24004. */
  24005. template<typename Ret, typename Func, typename Args>
  24006. inline constexpr bool is_applicable_r_v = is_applicable_r<Ret, Func, Args>::value;
  24007. /**
  24008. * @brief Provides the member constant `value` to true if a given type is
  24009. * complete, false otherwise.
  24010. * @tparam Type The type to test.
  24011. */
  24012. template<typename Type, typename = void>
  24013. struct is_complete: std::false_type {};
  24014. /*! @copydoc is_complete */
  24015. template<typename Type>
  24016. struct is_complete<Type, std::void_t<decltype(sizeof(Type))>>: std::true_type {};
  24017. /**
  24018. * @brief Helper variable template.
  24019. * @tparam Type The type to test.
  24020. */
  24021. template<typename Type>
  24022. inline constexpr bool is_complete_v = is_complete<Type>::value;
  24023. /**
  24024. * @brief Provides the member constant `value` to true if a given type is an
  24025. * iterator, false otherwise.
  24026. * @tparam Type The type to test.
  24027. */
  24028. template<typename Type, typename = void>
  24029. struct is_iterator: std::false_type {};
  24030. /*! @copydoc is_iterator */
  24031. template<typename Type>
  24032. struct is_iterator<Type, std::void_t<typename std::iterator_traits<Type>::iterator_category>>
  24033. : std::true_type
  24034. {};
  24035. /**
  24036. * @brief Helper variable template.
  24037. * @tparam Type The type to test.
  24038. */
  24039. template<typename Type>
  24040. inline constexpr bool is_iterator_v = is_iterator<Type>::value;
  24041. /**
  24042. * @brief Provides the member constant `value` to true if a given type is of the
  24043. * required iterator type, false otherwise.
  24044. * @tparam Type The type to test.
  24045. * @tparam It Required iterator type.
  24046. */
  24047. template<typename Type, typename It, typename = void>
  24048. struct is_iterator_type: std::false_type {};
  24049. /*! @copydoc is_iterator_type */
  24050. template<typename Type, typename It>
  24051. struct is_iterator_type<Type, It, std::enable_if_t<is_iterator_v<Type> && std::is_same_v<Type, It>>>
  24052. : std::true_type
  24053. {};
  24054. /*! @copydoc is_iterator_type */
  24055. template<typename Type, typename It>
  24056. struct is_iterator_type<Type, It, std::enable_if_t<!std::is_same_v<Type, It>, std::void_t<typename It::iterator_type>>>
  24057. : is_iterator_type<Type, typename It::iterator_type>
  24058. {};
  24059. /**
  24060. * @brief Helper variable template.
  24061. * @tparam Type The type to test.
  24062. * @tparam It Required iterator type.
  24063. */
  24064. template<typename Type, typename It>
  24065. inline constexpr bool is_iterator_type_v = is_iterator_type<Type, It>::value;
  24066. /**
  24067. * @brief Transcribes the constness of a type to another type.
  24068. * @tparam To The type to which to transcribe the constness.
  24069. * @tparam From The type from which to transcribe the constness.
  24070. */
  24071. template<typename To, typename From>
  24072. struct constness_as {
  24073. /*! @brief The type resulting from the transcription of the constness. */
  24074. using type = std::remove_const_t<To>;
  24075. };
  24076. /*! @copydoc constness_as */
  24077. template<typename To, typename From>
  24078. struct constness_as<To, const From> {
  24079. /*! @brief The type resulting from the transcription of the constness. */
  24080. using type = std::add_const_t<To>;
  24081. };
  24082. /**
  24083. * @brief Alias template to facilitate the transcription of the constness.
  24084. * @tparam To The type to which to transcribe the constness.
  24085. * @tparam From The type from which to transcribe the constness.
  24086. */
  24087. template<typename To, typename From>
  24088. using constness_as_t = typename constness_as<To, From>::type;
  24089. /**
  24090. * @brief Extracts the class of a non-static member object or function.
  24091. * @tparam Member A pointer to a non-static member object or function.
  24092. */
  24093. template<typename Member>
  24094. class member_class {
  24095. static_assert(std::is_member_pointer_v<Member>, "Invalid pointer type to non-static member object or function");
  24096. template<typename Class, typename Ret, typename... Args>
  24097. static Class * clazz(Ret(Class:: *)(Args...));
  24098. template<typename Class, typename Ret, typename... Args>
  24099. static Class * clazz(Ret(Class:: *)(Args...) const);
  24100. template<typename Class, typename Type>
  24101. static Class * clazz(Type Class:: *);
  24102. public:
  24103. /*! @brief The class of the given non-static member object or function. */
  24104. using type = std::remove_pointer_t<decltype(clazz(std::declval<Member>()))>;
  24105. };
  24106. /**
  24107. * @brief Helper type.
  24108. * @tparam Member A pointer to a non-static member object or function.
  24109. */
  24110. template<typename Member>
  24111. using member_class_t = typename member_class<Member>::type;
  24112. }
  24113. #endif
  24114. // #include "type_traits.hpp"
  24115. #ifndef ENTT_META_TYPE_TRAITS_HPP
  24116. #define ENTT_META_TYPE_TRAITS_HPP
  24117. #include <type_traits>
  24118. namespace entt {
  24119. /**
  24120. * @brief Traits class template to be specialized to enable support for meta
  24121. * template information.
  24122. */
  24123. template<typename>
  24124. struct meta_template_traits;
  24125. /**
  24126. * @brief Traits class template to be specialized to enable support for meta
  24127. * sequence containers.
  24128. */
  24129. template<typename>
  24130. struct meta_sequence_container_traits;
  24131. /**
  24132. * @brief Traits class template to be specialized to enable support for meta
  24133. * associative containers.
  24134. */
  24135. template<typename>
  24136. struct meta_associative_container_traits;
  24137. /**
  24138. * @brief Provides the member constant `value` to true if a meta associative
  24139. * container claims to wrap a key-only type, false otherwise.
  24140. * @tparam Type Potentially key-only meta associative container type.
  24141. */
  24142. template<typename, typename = void>
  24143. struct is_key_only_meta_associative_container: std::true_type {};
  24144. /*! @copydoc is_key_only_meta_associative_container */
  24145. template<typename Type>
  24146. struct is_key_only_meta_associative_container<Type, std::void_t<typename meta_associative_container_traits<Type>::type::mapped_type>>
  24147. : std::false_type
  24148. {};
  24149. /**
  24150. * @brief Helper variable template.
  24151. * @tparam Type Potentially key-only meta associative container type.
  24152. */
  24153. template<typename Type>
  24154. inline constexpr auto is_key_only_meta_associative_container_v = is_key_only_meta_associative_container<Type>::value;
  24155. /**
  24156. * @brief Provides the member constant `value` to true if a given type is a
  24157. * pointer-like type from the point of view of the meta system, false otherwise.
  24158. * @tparam Type Potentially pointer-like type.
  24159. */
  24160. template<typename>
  24161. struct is_meta_pointer_like: std::false_type {};
  24162. /**
  24163. * @brief Partial specialization to ensure that const pointer-like types are
  24164. * also accepted.
  24165. * @tparam Type Potentially pointer-like type.
  24166. */
  24167. template<typename Type>
  24168. struct is_meta_pointer_like<const Type>: is_meta_pointer_like<Type> {};
  24169. /**
  24170. * @brief Helper variable template.
  24171. * @tparam Type Potentially pointer-like type.
  24172. */
  24173. template<typename Type>
  24174. inline constexpr auto is_meta_pointer_like_v = is_meta_pointer_like<Type>::value;
  24175. }
  24176. #endif
  24177. namespace entt {
  24178. /**
  24179. * @brief Container traits.
  24180. * @tparam Container Type of the underlying container.
  24181. * @tparam Trait Traits associated with the underlying container.
  24182. */
  24183. template<typename Container, template<typename> class... Trait>
  24184. struct meta_container_traits: public Trait<Container>... {
  24185. /*! @brief Type of container. */
  24186. using type = Container;
  24187. };
  24188. /**
  24189. * @brief Basic STL-compatible container traits
  24190. * @tparam Container The type of the container.
  24191. */
  24192. template<typename Container>
  24193. struct basic_container {
  24194. /**
  24195. * @brief Returns the size of the given container.
  24196. * @param cont The container for which to return the size.
  24197. * @return The size of the given container.
  24198. */
  24199. [[nodiscard]] static typename Container::size_type size(const Container &cont) ENTT_NOEXCEPT {
  24200. return cont.size();
  24201. }
  24202. /**
  24203. * @brief Returns an iterator to the first element of the given container.
  24204. * @param cont The container for which to return the iterator.
  24205. * @return An iterator to the first element of the given container.
  24206. */
  24207. [[nodiscard]] static typename Container::iterator begin(Container &cont) {
  24208. return cont.begin();
  24209. }
  24210. /**
  24211. * @brief Returns an iterator to the first element of the given container.
  24212. * @param cont The container for which to return the iterator.
  24213. * @return An iterator to the first element of the given container.
  24214. */
  24215. [[nodiscard]] static typename Container::const_iterator cbegin(const Container &cont) {
  24216. return cont.begin();
  24217. }
  24218. /**
  24219. * @brief Returns an iterator past the last element of the given container.
  24220. * @param cont The container for which to return the iterator.
  24221. * @return An iterator past the last element of the given container.
  24222. */
  24223. [[nodiscard]] static typename Container::iterator end(Container &cont) {
  24224. return cont.end();
  24225. }
  24226. /**
  24227. * @brief Returns an iterator past the last element of the given container.
  24228. * @param cont The container for which to return the iterator.
  24229. * @return An iterator past the last element of the given container.
  24230. */
  24231. [[nodiscard]] static typename Container::const_iterator cend(const Container &cont) {
  24232. return cont.end();
  24233. }
  24234. };
  24235. /**
  24236. * @brief Basic STL-compatible associative container traits
  24237. * @tparam Container The type of the container.
  24238. */
  24239. template<typename Container>
  24240. struct basic_associative_container {
  24241. /**
  24242. * @brief Returns an iterator to the element with key equivalent to the
  24243. * given one, if any.
  24244. * @param cont The container in which to search for the element.
  24245. * @param key The key of the element to search.
  24246. * @return An iterator to the element with the given key, if any.
  24247. */
  24248. [[nodiscard]] static typename Container::iterator find(Container &cont, const typename Container::key_type &key) {
  24249. return cont.find(key);
  24250. }
  24251. /*! @copydoc find */
  24252. [[nodiscard]] static typename Container::const_iterator cfind(const Container &cont, const typename Container::key_type &key) {
  24253. return cont.find(key);
  24254. }
  24255. };
  24256. /**
  24257. * @brief Basic STL-compatible dynamic container traits
  24258. * @tparam Container The type of the container.
  24259. */
  24260. template<typename Container>
  24261. struct basic_dynamic_container {
  24262. /**
  24263. * @brief Clears the content of the given container.
  24264. * @param cont The container for which to clear the content.
  24265. * @return True in case of success, false otherwise.
  24266. */
  24267. [[nodiscard]] static bool clear([[maybe_unused]] Container &cont) {
  24268. return cont.clear(), true;
  24269. }
  24270. };
  24271. /**
  24272. * @brief Basic STL-compatible dynamic associative container traits
  24273. * @tparam Container The type of the container.
  24274. */
  24275. template<typename Container>
  24276. struct basic_dynamic_associative_container {
  24277. /**
  24278. * @brief Removes the specified element from the given container.
  24279. * @param cont The container from which to remove the element.
  24280. * @param key The element to remove.
  24281. * @return A bool denoting whether the removal took place.
  24282. */
  24283. [[nodiscard]] static bool erase([[maybe_unused]] Container &cont, [[maybe_unused]] const typename Container::key_type &key) {
  24284. const auto sz = cont.size();
  24285. return cont.erase(key) != sz;
  24286. }
  24287. };
  24288. /**
  24289. * @brief Basic STL-compatible sequence container traits
  24290. * @tparam Container The type of the container.
  24291. */
  24292. template<typename Container>
  24293. struct basic_sequence_container {
  24294. /**
  24295. * @brief Returns a reference to the element at the specified location of
  24296. * the given container (no bounds checking is performed).
  24297. * @param cont The container from which to get the element.
  24298. * @param pos The position of the element to return.
  24299. * @return A reference to the requested element.
  24300. */
  24301. [[nodiscard]] static typename Container::reference get(Container &cont, typename Container::size_type pos) {
  24302. return cont[pos];
  24303. }
  24304. /*! @copydoc get */
  24305. [[nodiscard]] static typename Container::const_reference cget(const Container &cont, typename Container::size_type pos) {
  24306. return cont[pos];
  24307. }
  24308. };
  24309. /**
  24310. * @brief STL-compatible dynamic associative key-only container traits
  24311. * @tparam Container The type of the container.
  24312. */
  24313. template<typename Container>
  24314. struct dynamic_associative_key_only_container {
  24315. /**
  24316. * @brief Inserts an element into the given container.
  24317. * @param cont The container in which to insert the element.
  24318. * @param key The element to insert.
  24319. * @return A bool denoting whether the insertion took place.
  24320. */
  24321. [[nodiscard]] static bool insert([[maybe_unused]] Container &cont, [[maybe_unused]] const typename Container::key_type &key) {
  24322. return cont.insert(key).second;
  24323. }
  24324. };
  24325. /**
  24326. * @brief STL-compatible dynamic key-value associative container traits
  24327. * @tparam Container The type of the container.
  24328. */
  24329. template<typename Container>
  24330. struct dynamic_associative_key_value_container {
  24331. /**
  24332. * @brief Inserts an element (a key/value pair) into the given container.
  24333. * @param cont The container in which to insert the element.
  24334. * @param key The key of the element to insert.
  24335. * @param value The value of the element to insert.
  24336. * @return A bool denoting whether the insertion took place.
  24337. */
  24338. [[nodiscard]] static bool insert([[maybe_unused]] Container &cont, [[maybe_unused]] const typename Container::key_type &key, [[maybe_unused]] const typename Container::mapped_type &value) {
  24339. return cont.insert(std::make_pair(key, value)).second;
  24340. }
  24341. };
  24342. /**
  24343. * @brief STL-compatible dynamic sequence container traits
  24344. * @tparam Container The type of the container.
  24345. */
  24346. template<typename Container>
  24347. struct dynamic_sequence_container {
  24348. /**
  24349. * @brief Resizes the given container to contain the given number of
  24350. * elements.
  24351. * @param cont The container to resize.
  24352. * @param sz The new size of the container.
  24353. * @return True in case of success, false otherwise.
  24354. */
  24355. [[nodiscard]] static bool resize([[maybe_unused]] Container &cont, [[maybe_unused]] typename Container::size_type sz) {
  24356. return cont.resize(sz), true;
  24357. }
  24358. /**
  24359. * @brief Inserts an element at the specified location of the given
  24360. * container.
  24361. * @param cont The container into which to insert the element.
  24362. * @param it Iterator before which the element will be inserted.
  24363. * @param value Element value to insert.
  24364. * @return A pair consisting of an iterator to the inserted element (in case
  24365. * of success) and a bool denoting whether the insertion took place.
  24366. */
  24367. [[nodiscard]] static std::pair<typename Container::iterator, bool> insert([[maybe_unused]] Container &cont, [[maybe_unused]] typename Container::const_iterator it, [[maybe_unused]] const typename Container::value_type &value) {
  24368. return { cont.insert(it, value), true };
  24369. }
  24370. /**
  24371. * @brief Removes the element at the specified location from the given
  24372. * container.
  24373. * @param cont The container from which to remove the element.
  24374. * @param it Iterator to the element to remove.
  24375. * @return A pair consisting of an iterator following the last removed
  24376. * element (in case of success) and a bool denoting whether the insertion
  24377. * took place.
  24378. */
  24379. [[nodiscard]] static std::pair<typename Container::iterator, bool> erase([[maybe_unused]] Container &cont, [[maybe_unused]] typename Container::const_iterator it) {
  24380. return { cont.erase(it), true };
  24381. }
  24382. };
  24383. /**
  24384. * @brief STL-compatible fixed sequence container traits
  24385. * @tparam Container The type of the container.
  24386. */
  24387. template<typename Container>
  24388. struct fixed_sequence_container {
  24389. /**
  24390. * @brief Does nothing.
  24391. * @return False to indicate failure in all cases.
  24392. */
  24393. [[nodiscard]] static bool resize(const Container &, typename Container::size_type) {
  24394. return false;
  24395. }
  24396. /**
  24397. * @brief Does nothing.
  24398. * @return False to indicate failure in all cases.
  24399. */
  24400. [[nodiscard]] static bool clear(const Container &) {
  24401. return false;
  24402. }
  24403. /**
  24404. * @brief Does nothing.
  24405. * @return A pair consisting of an invalid iterator and a false value to
  24406. * indicate failure in all cases.
  24407. */
  24408. [[nodiscard]] static std::pair<typename Container::iterator, bool> insert(const Container &, typename Container::const_iterator, const typename Container::value_type &) {
  24409. return { {}, false };
  24410. }
  24411. /**
  24412. * @brief Does nothing.
  24413. * @return A pair consisting of an invalid iterator and a false value to
  24414. * indicate failure in all cases.
  24415. */
  24416. [[nodiscard]] static std::pair<typename Container::iterator, bool> erase(const Container &, typename Container::const_iterator) {
  24417. return { {}, false };
  24418. }
  24419. };
  24420. /**
  24421. * @brief Meta sequence container traits for `std::vector`s of any type.
  24422. * @tparam Type The type of elements.
  24423. * @tparam Args Other arguments.
  24424. */
  24425. template<typename Type, typename... Args>
  24426. struct meta_sequence_container_traits<std::vector<Type, Args...>>
  24427. : meta_container_traits<
  24428. std::vector<Type, Args...>,
  24429. basic_container,
  24430. basic_dynamic_container,
  24431. basic_sequence_container,
  24432. dynamic_sequence_container
  24433. >
  24434. {};
  24435. /**
  24436. * @brief Meta sequence container traits for `std::array`s of any type.
  24437. * @tparam Type The type of elements.
  24438. * @tparam N The number of elements.
  24439. */
  24440. template<typename Type, auto N>
  24441. struct meta_sequence_container_traits<std::array<Type, N>>
  24442. : meta_container_traits<
  24443. std::array<Type, N>,
  24444. basic_container,
  24445. basic_sequence_container,
  24446. fixed_sequence_container
  24447. >
  24448. {};
  24449. /**
  24450. * @brief Meta associative container traits for `std::map`s of any type.
  24451. * @tparam Key The key type of elements.
  24452. * @tparam Value The value type of elements.
  24453. * @tparam Args Other arguments.
  24454. */
  24455. template<typename Key, typename Value, typename... Args>
  24456. struct meta_associative_container_traits<std::map<Key, Value, Args...>>
  24457. : meta_container_traits<
  24458. std::map<Key, Value, Args...>,
  24459. basic_container,
  24460. basic_associative_container,
  24461. basic_dynamic_container,
  24462. basic_dynamic_associative_container,
  24463. dynamic_associative_key_value_container
  24464. >
  24465. {};
  24466. /**
  24467. * @brief Meta associative container traits for `std::unordered_map`s of any
  24468. * type.
  24469. * @tparam Key The key type of elements.
  24470. * @tparam Value The value type of elements.
  24471. * @tparam Args Other arguments.
  24472. */
  24473. template<typename Key, typename Value, typename... Args>
  24474. struct meta_associative_container_traits<std::unordered_map<Key, Value, Args...>>
  24475. : meta_container_traits<
  24476. std::unordered_map<Key, Value, Args...>,
  24477. basic_container,
  24478. basic_associative_container,
  24479. basic_dynamic_container,
  24480. basic_dynamic_associative_container,
  24481. dynamic_associative_key_value_container
  24482. >
  24483. {};
  24484. /**
  24485. * @brief Meta associative container traits for `std::set`s of any type.
  24486. * @tparam Key The type of elements.
  24487. * @tparam Args Other arguments.
  24488. */
  24489. template<typename Key, typename... Args>
  24490. struct meta_associative_container_traits<std::set<Key, Args...>>
  24491. : meta_container_traits<
  24492. std::set<Key, Args...>,
  24493. basic_container,
  24494. basic_associative_container,
  24495. basic_dynamic_container,
  24496. basic_dynamic_associative_container,
  24497. dynamic_associative_key_only_container
  24498. >
  24499. {};
  24500. /**
  24501. * @brief Meta associative container traits for `std::unordered_set`s of any
  24502. * type.
  24503. * @tparam Key The type of elements.
  24504. * @tparam Args Other arguments.
  24505. */
  24506. template<typename Key, typename... Args>
  24507. struct meta_associative_container_traits<std::unordered_set<Key, Args...>>
  24508. : meta_container_traits<
  24509. std::unordered_set<Key, Args...>,
  24510. basic_container,
  24511. basic_associative_container,
  24512. basic_dynamic_container,
  24513. basic_dynamic_associative_container,
  24514. dynamic_associative_key_only_container
  24515. >
  24516. {};
  24517. }
  24518. #endif
  24519. // #include "meta/ctx.hpp"
  24520. #ifndef ENTT_META_CTX_HPP
  24521. #define ENTT_META_CTX_HPP
  24522. // #include "../core/attribute.h"
  24523. #ifndef ENTT_CORE_ATTRIBUTE_H
  24524. #define ENTT_CORE_ATTRIBUTE_H
  24525. #ifndef ENTT_EXPORT
  24526. # if defined _WIN32 || defined __CYGWIN__ || defined _MSC_VER
  24527. # define ENTT_EXPORT __declspec(dllexport)
  24528. # define ENTT_IMPORT __declspec(dllimport)
  24529. # define ENTT_HIDDEN
  24530. # elif defined __GNUC__ && __GNUC__ >= 4
  24531. # define ENTT_EXPORT __attribute__((visibility("default")))
  24532. # define ENTT_IMPORT __attribute__((visibility("default")))
  24533. # define ENTT_HIDDEN __attribute__((visibility("hidden")))
  24534. # else /* Unsupported compiler */
  24535. # define ENTT_EXPORT
  24536. # define ENTT_IMPORT
  24537. # define ENTT_HIDDEN
  24538. # endif
  24539. #endif
  24540. #ifndef ENTT_API
  24541. # if defined ENTT_API_EXPORT
  24542. # define ENTT_API ENTT_EXPORT
  24543. # elif defined ENTT_API_IMPORT
  24544. # define ENTT_API ENTT_IMPORT
  24545. # else /* No API */
  24546. # define ENTT_API
  24547. # endif
  24548. #endif
  24549. #endif
  24550. // #include "../config/config.h"
  24551. namespace entt {
  24552. /**
  24553. * @cond TURN_OFF_DOXYGEN
  24554. * Internal details not to be documented.
  24555. */
  24556. namespace internal {
  24557. struct meta_type_node;
  24558. struct ENTT_API meta_context {
  24559. // we could use the lines below but VS2017 returns with an ICE if combined with ENTT_API despite the code being valid C++
  24560. // inline static meta_type_node *local = nullptr;
  24561. // inline static meta_type_node **global = &local;
  24562. [[nodiscard]] static meta_type_node * & local() ENTT_NOEXCEPT {
  24563. static meta_type_node *chain = nullptr;
  24564. return chain;
  24565. }
  24566. [[nodiscard]] static meta_type_node ** & global() ENTT_NOEXCEPT {
  24567. static meta_type_node **chain = &local();
  24568. return chain;
  24569. }
  24570. };
  24571. }
  24572. /**
  24573. * Internal details not to be documented.
  24574. * @endcond
  24575. */
  24576. /*! @brief Opaque container for a meta context. */
  24577. struct meta_ctx {
  24578. /**
  24579. * @brief Binds the meta system to a given context.
  24580. * @param other A valid context to which to bind.
  24581. */
  24582. static void bind(meta_ctx other) ENTT_NOEXCEPT {
  24583. internal::meta_context::global() = other.ctx;
  24584. }
  24585. private:
  24586. internal::meta_type_node **ctx{&internal::meta_context::local()};
  24587. };
  24588. }
  24589. #endif
  24590. // #include "meta/factory.hpp"
  24591. #ifndef ENTT_META_FACTORY_HPP
  24592. #define ENTT_META_FACTORY_HPP
  24593. #include <cstddef>
  24594. #include <tuple>
  24595. #include <type_traits>
  24596. #include <utility>
  24597. // #include "../config/config.h"
  24598. // #include "../core/fwd.hpp"
  24599. #ifndef ENTT_CORE_FWD_HPP
  24600. #define ENTT_CORE_FWD_HPP
  24601. #include <type_traits>
  24602. // #include "../config/config.h"
  24603. namespace entt {
  24604. template<std::size_t Len = sizeof(double[2]), std::size_t = alignof(typename std::aligned_storage_t<Len + !Len>)>
  24605. class basic_any;
  24606. /*! @brief Alias declaration for type identifiers. */
  24607. using id_type = ENTT_ID_TYPE;
  24608. /*! @brief Alias declaration for the most common use case. */
  24609. using any = basic_any<>;
  24610. }
  24611. #endif
  24612. // #include "../core/type_info.hpp"
  24613. #ifndef ENTT_CORE_TYPE_INFO_HPP
  24614. #define ENTT_CORE_TYPE_INFO_HPP
  24615. #include <string_view>
  24616. #include <type_traits>
  24617. // #include "../config/config.h"
  24618. // #include "../core/attribute.h"
  24619. #ifndef ENTT_CORE_ATTRIBUTE_H
  24620. #define ENTT_CORE_ATTRIBUTE_H
  24621. #ifndef ENTT_EXPORT
  24622. # if defined _WIN32 || defined __CYGWIN__ || defined _MSC_VER
  24623. # define ENTT_EXPORT __declspec(dllexport)
  24624. # define ENTT_IMPORT __declspec(dllimport)
  24625. # define ENTT_HIDDEN
  24626. # elif defined __GNUC__ && __GNUC__ >= 4
  24627. # define ENTT_EXPORT __attribute__((visibility("default")))
  24628. # define ENTT_IMPORT __attribute__((visibility("default")))
  24629. # define ENTT_HIDDEN __attribute__((visibility("hidden")))
  24630. # else /* Unsupported compiler */
  24631. # define ENTT_EXPORT
  24632. # define ENTT_IMPORT
  24633. # define ENTT_HIDDEN
  24634. # endif
  24635. #endif
  24636. #ifndef ENTT_API
  24637. # if defined ENTT_API_EXPORT
  24638. # define ENTT_API ENTT_EXPORT
  24639. # elif defined ENTT_API_IMPORT
  24640. # define ENTT_API ENTT_IMPORT
  24641. # else /* No API */
  24642. # define ENTT_API
  24643. # endif
  24644. #endif
  24645. #endif
  24646. // #include "hashed_string.hpp"
  24647. #ifndef ENTT_CORE_HASHED_STRING_HPP
  24648. #define ENTT_CORE_HASHED_STRING_HPP
  24649. #include <cstddef>
  24650. #include <cstdint>
  24651. // #include "../config/config.h"
  24652. // #include "fwd.hpp"
  24653. namespace entt {
  24654. /**
  24655. * @cond TURN_OFF_DOXYGEN
  24656. * Internal details not to be documented.
  24657. */
  24658. namespace internal {
  24659. template<typename>
  24660. struct fnv1a_traits;
  24661. template<>
  24662. struct fnv1a_traits<std::uint32_t> {
  24663. using type = std::uint32_t;
  24664. static constexpr std::uint32_t offset = 2166136261;
  24665. static constexpr std::uint32_t prime = 16777619;
  24666. };
  24667. template<>
  24668. struct fnv1a_traits<std::uint64_t> {
  24669. using type = std::uint64_t;
  24670. static constexpr std::uint64_t offset = 14695981039346656037ull;
  24671. static constexpr std::uint64_t prime = 1099511628211ull;
  24672. };
  24673. }
  24674. /**
  24675. * Internal details not to be documented.
  24676. * @endcond
  24677. */
  24678. /**
  24679. * @brief Zero overhead unique identifier.
  24680. *
  24681. * A hashed string is a compile-time tool that allows users to use
  24682. * human-readable identifers in the codebase while using their numeric
  24683. * counterparts at runtime.<br/>
  24684. * Because of that, a hashed string can also be used in constant expressions if
  24685. * required.
  24686. *
  24687. * @tparam Char Character type.
  24688. */
  24689. template<typename Char>
  24690. class basic_hashed_string {
  24691. using traits_type = internal::fnv1a_traits<id_type>;
  24692. struct const_wrapper {
  24693. // non-explicit constructor on purpose
  24694. constexpr const_wrapper(const Char *curr) ENTT_NOEXCEPT: str{curr} {}
  24695. const Char *str;
  24696. };
  24697. // Fowler–Noll–Vo hash function v. 1a - the good
  24698. [[nodiscard]] static constexpr id_type helper(const Char *curr) ENTT_NOEXCEPT {
  24699. auto value = traits_type::offset;
  24700. while(*curr != 0) {
  24701. value = (value ^ static_cast<traits_type::type>(*(curr++))) * traits_type::prime;
  24702. }
  24703. return value;
  24704. }
  24705. public:
  24706. /*! @brief Character type. */
  24707. using value_type = Char;
  24708. /*! @brief Unsigned integer type. */
  24709. using hash_type = id_type;
  24710. /**
  24711. * @brief Returns directly the numeric representation of a string view.
  24712. * @param str Human-readable identifer.
  24713. * @param size Length of the string to hash.
  24714. * @return The numeric representation of the string.
  24715. */
  24716. [[nodiscard]] static constexpr hash_type value(const value_type *str, std::size_t size) ENTT_NOEXCEPT {
  24717. id_type partial{traits_type::offset};
  24718. while(size--) { partial = (partial^(str++)[0])*traits_type::prime; }
  24719. return partial;
  24720. }
  24721. /**
  24722. * @brief Returns directly the numeric representation of a string.
  24723. *
  24724. * Forcing template resolution avoids implicit conversions. An
  24725. * human-readable identifier can be anything but a plain, old bunch of
  24726. * characters.<br/>
  24727. * Example of use:
  24728. * @code{.cpp}
  24729. * const auto value = basic_hashed_string<char>::to_value("my.png");
  24730. * @endcode
  24731. *
  24732. * @tparam N Number of characters of the identifier.
  24733. * @param str Human-readable identifer.
  24734. * @return The numeric representation of the string.
  24735. */
  24736. template<std::size_t N>
  24737. [[nodiscard]] static constexpr hash_type value(const value_type (&str)[N]) ENTT_NOEXCEPT {
  24738. return helper(str);
  24739. }
  24740. /**
  24741. * @brief Returns directly the numeric representation of a string.
  24742. * @param wrapper Helps achieving the purpose by relying on overloading.
  24743. * @return The numeric representation of the string.
  24744. */
  24745. [[nodiscard]] static hash_type value(const_wrapper wrapper) ENTT_NOEXCEPT {
  24746. return helper(wrapper.str);
  24747. }
  24748. /*! @brief Constructs an empty hashed string. */
  24749. constexpr basic_hashed_string() ENTT_NOEXCEPT
  24750. : str{nullptr}, hash{}
  24751. {}
  24752. /**
  24753. * @brief Constructs a hashed string from an array of const characters.
  24754. *
  24755. * Forcing template resolution avoids implicit conversions. An
  24756. * human-readable identifier can be anything but a plain, old bunch of
  24757. * characters.<br/>
  24758. * Example of use:
  24759. * @code{.cpp}
  24760. * basic_hashed_string<char> hs{"my.png"};
  24761. * @endcode
  24762. *
  24763. * @tparam N Number of characters of the identifier.
  24764. * @param curr Human-readable identifer.
  24765. */
  24766. template<std::size_t N>
  24767. constexpr basic_hashed_string(const value_type (&curr)[N]) ENTT_NOEXCEPT
  24768. : str{curr}, hash{helper(curr)}
  24769. {}
  24770. /**
  24771. * @brief Explicit constructor on purpose to avoid constructing a hashed
  24772. * string directly from a `const value_type *`.
  24773. * @param wrapper Helps achieving the purpose by relying on overloading.
  24774. */
  24775. explicit constexpr basic_hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
  24776. : str{wrapper.str}, hash{helper(wrapper.str)}
  24777. {}
  24778. /**
  24779. * @brief Returns the human-readable representation of a hashed string.
  24780. * @return The string used to initialize the instance.
  24781. */
  24782. [[nodiscard]] constexpr const value_type * data() const ENTT_NOEXCEPT {
  24783. return str;
  24784. }
  24785. /**
  24786. * @brief Returns the numeric representation of a hashed string.
  24787. * @return The numeric representation of the instance.
  24788. */
  24789. [[nodiscard]] constexpr hash_type value() const ENTT_NOEXCEPT {
  24790. return hash;
  24791. }
  24792. /*! @copydoc data */
  24793. [[nodiscard]] constexpr operator const value_type *() const ENTT_NOEXCEPT { return data(); }
  24794. /**
  24795. * @brief Returns the numeric representation of a hashed string.
  24796. * @return The numeric representation of the instance.
  24797. */
  24798. [[nodiscard]] constexpr operator hash_type() const ENTT_NOEXCEPT { return value(); }
  24799. /**
  24800. * @brief Compares two hashed strings.
  24801. * @param other Hashed string with which to compare.
  24802. * @return True if the two hashed strings are identical, false otherwise.
  24803. */
  24804. [[nodiscard]] constexpr bool operator==(const basic_hashed_string &other) const ENTT_NOEXCEPT {
  24805. return hash == other.hash;
  24806. }
  24807. private:
  24808. const value_type *str;
  24809. hash_type hash;
  24810. };
  24811. /**
  24812. * @brief Deduction guide.
  24813. *
  24814. * It allows to deduce the character type of the hashed string directly from a
  24815. * human-readable identifer provided to the constructor.
  24816. *
  24817. * @tparam Char Character type.
  24818. * @tparam N Number of characters of the identifier.
  24819. * @param str Human-readable identifer.
  24820. */
  24821. template<typename Char, std::size_t N>
  24822. basic_hashed_string(const Char (&str)[N])
  24823. -> basic_hashed_string<Char>;
  24824. /**
  24825. * @brief Compares two hashed strings.
  24826. * @tparam Char Character type.
  24827. * @param lhs A valid hashed string.
  24828. * @param rhs A valid hashed string.
  24829. * @return True if the two hashed strings are identical, false otherwise.
  24830. */
  24831. template<typename Char>
  24832. [[nodiscard]] constexpr bool operator!=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) ENTT_NOEXCEPT {
  24833. return !(lhs == rhs);
  24834. }
  24835. /*! @brief Aliases for common character types. */
  24836. using hashed_string = basic_hashed_string<char>;
  24837. /*! @brief Aliases for common character types. */
  24838. using hashed_wstring = basic_hashed_string<wchar_t>;
  24839. inline namespace literals {
  24840. /**
  24841. * @brief User defined literal for hashed strings.
  24842. * @param str The literal without its suffix.
  24843. * @return A properly initialized hashed string.
  24844. */
  24845. [[nodiscard]] constexpr entt::hashed_string operator"" _hs(const char *str, std::size_t) ENTT_NOEXCEPT {
  24846. return entt::hashed_string{str};
  24847. }
  24848. /**
  24849. * @brief User defined literal for hashed wstrings.
  24850. * @param str The literal without its suffix.
  24851. * @return A properly initialized hashed wstring.
  24852. */
  24853. [[nodiscard]] constexpr entt::hashed_wstring operator"" _hws(const wchar_t *str, std::size_t) ENTT_NOEXCEPT {
  24854. return entt::hashed_wstring{str};
  24855. }
  24856. }
  24857. }
  24858. #endif
  24859. // #include "fwd.hpp"
  24860. namespace entt {
  24861. /**
  24862. * @cond TURN_OFF_DOXYGEN
  24863. * Internal details not to be documented.
  24864. */
  24865. namespace internal {
  24866. struct ENTT_API type_seq final {
  24867. [[nodiscard]] static id_type next() ENTT_NOEXCEPT {
  24868. static ENTT_MAYBE_ATOMIC(id_type) value{};
  24869. return value++;
  24870. }
  24871. };
  24872. template<typename Type>
  24873. [[nodiscard]] constexpr auto stripped_type_name() ENTT_NOEXCEPT {
  24874. #if defined ENTT_PRETTY_FUNCTION
  24875. std::string_view pretty_function{ENTT_PRETTY_FUNCTION};
  24876. auto first = pretty_function.find_first_not_of(' ', pretty_function.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX)+1);
  24877. auto value = pretty_function.substr(first, pretty_function.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
  24878. return value;
  24879. #else
  24880. return std::string_view{""};
  24881. #endif
  24882. }
  24883. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  24884. [[nodiscard]] static constexpr std::string_view type_name(int) ENTT_NOEXCEPT {
  24885. constexpr auto value = stripped_type_name<Type>();
  24886. return value;
  24887. }
  24888. template<typename Type>
  24889. [[nodiscard]] static std::string_view type_name(char) ENTT_NOEXCEPT {
  24890. static const auto value = stripped_type_name<Type>();
  24891. return value;
  24892. }
  24893. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  24894. [[nodiscard]] static constexpr id_type type_hash(int) ENTT_NOEXCEPT {
  24895. constexpr auto stripped = stripped_type_name<Type>();
  24896. constexpr auto value = hashed_string::value(stripped.data(), stripped.size());
  24897. return value;
  24898. }
  24899. template<typename Type>
  24900. [[nodiscard]] static id_type type_hash(char) ENTT_NOEXCEPT {
  24901. static const auto value = [](const auto stripped) {
  24902. return hashed_string::value(stripped.data(), stripped.size());
  24903. }(stripped_type_name<Type>());
  24904. return value;
  24905. }
  24906. }
  24907. /**
  24908. * Internal details not to be documented.
  24909. * @endcond
  24910. */
  24911. /**
  24912. * @brief Type sequential identifier.
  24913. * @tparam Type Type for which to generate a sequential identifier.
  24914. */
  24915. template<typename Type, typename = void>
  24916. struct ENTT_API type_seq final {
  24917. /**
  24918. * @brief Returns the sequential identifier of a given type.
  24919. * @return The sequential identifier of a given type.
  24920. */
  24921. [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
  24922. static const id_type value = internal::type_seq::next();
  24923. return value;
  24924. }
  24925. /*! @copydoc value */
  24926. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  24927. };
  24928. /**
  24929. * @brief Type hash.
  24930. * @tparam Type Type for which to generate a hash value.
  24931. */
  24932. template<typename Type, typename = void>
  24933. struct type_hash final {
  24934. /**
  24935. * @brief Returns the numeric representation of a given type.
  24936. * @return The numeric representation of the given type.
  24937. */
  24938. #if defined ENTT_PRETTY_FUNCTION
  24939. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  24940. return internal::type_hash<Type>(0);
  24941. #else
  24942. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  24943. return type_seq<Type>::value();
  24944. #endif
  24945. }
  24946. /*! @copydoc value */
  24947. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  24948. };
  24949. /**
  24950. * @brief Type name.
  24951. * @tparam Type Type for which to generate a name.
  24952. */
  24953. template<typename Type, typename = void>
  24954. struct type_name final {
  24955. /**
  24956. * @brief Returns the name of a given type.
  24957. * @return The name of the given type.
  24958. */
  24959. [[nodiscard]] static constexpr std::string_view value() ENTT_NOEXCEPT {
  24960. return internal::type_name<Type>(0);
  24961. }
  24962. /*! @copydoc value */
  24963. [[nodiscard]] constexpr operator std::string_view() const ENTT_NOEXCEPT { return value(); }
  24964. };
  24965. /*! @brief Implementation specific information about a type. */
  24966. class type_info final {
  24967. template<typename>
  24968. friend type_info type_id() ENTT_NOEXCEPT;
  24969. type_info(id_type seq_v, id_type hash_v, std::string_view name_v) ENTT_NOEXCEPT
  24970. : seq_value{seq_v},
  24971. hash_value{hash_v},
  24972. name_value{name_v}
  24973. {}
  24974. public:
  24975. /*! @brief Default constructor. */
  24976. type_info() ENTT_NOEXCEPT
  24977. : type_info({}, {}, {})
  24978. {}
  24979. /*! @brief Default copy constructor. */
  24980. type_info(const type_info &) ENTT_NOEXCEPT = default;
  24981. /*! @brief Default move constructor. */
  24982. type_info(type_info &&) ENTT_NOEXCEPT = default;
  24983. /**
  24984. * @brief Default copy assignment operator.
  24985. * @return This type info object.
  24986. */
  24987. type_info & operator=(const type_info &) ENTT_NOEXCEPT = default;
  24988. /**
  24989. * @brief Default move assignment operator.
  24990. * @return This type info object.
  24991. */
  24992. type_info & operator=(type_info &&) ENTT_NOEXCEPT = default;
  24993. /**
  24994. * @brief Checks if a type info object is properly initialized.
  24995. * @return True if the object is properly initialized, false otherwise.
  24996. */
  24997. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  24998. return name_value.data() != nullptr;
  24999. }
  25000. /**
  25001. * @brief Type sequential identifier.
  25002. * @return Type sequential identifier.
  25003. */
  25004. [[nodiscard]] id_type seq() const ENTT_NOEXCEPT {
  25005. return seq_value;
  25006. }
  25007. /**
  25008. * @brief Type hash.
  25009. * @return Type hash.
  25010. */
  25011. [[nodiscard]] id_type hash() const ENTT_NOEXCEPT {
  25012. return hash_value;
  25013. }
  25014. /**
  25015. * @brief Type name.
  25016. * @return Type name.
  25017. */
  25018. [[nodiscard]] std::string_view name() const ENTT_NOEXCEPT {
  25019. return name_value;
  25020. }
  25021. /**
  25022. * @brief Compares the contents of two type info objects.
  25023. * @param other Object with which to compare.
  25024. * @return False if the two contents differ, true otherwise.
  25025. */
  25026. [[nodiscard]] bool operator==(const type_info &other) const ENTT_NOEXCEPT {
  25027. return hash_value == other.hash_value;
  25028. }
  25029. private:
  25030. id_type seq_value;
  25031. id_type hash_value;
  25032. std::string_view name_value;
  25033. };
  25034. /**
  25035. * @brief Compares the contents of two type info objects.
  25036. * @param lhs A type info object.
  25037. * @param rhs A type info object.
  25038. * @return True if the two contents differ, false otherwise.
  25039. */
  25040. [[nodiscard]] inline bool operator!=(const type_info &lhs, const type_info &rhs) ENTT_NOEXCEPT {
  25041. return !(lhs == rhs);
  25042. }
  25043. /**
  25044. * @brief Returns the type info object for a given type.
  25045. * @tparam Type Type for which to generate a type info object.
  25046. * @return The type info object for the given type.
  25047. */
  25048. template<typename Type>
  25049. [[nodiscard]] type_info type_id() ENTT_NOEXCEPT {
  25050. return type_info{
  25051. type_seq<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  25052. type_hash<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  25053. type_name<std::remove_cv_t<std::remove_reference_t<Type>>>::value()
  25054. };
  25055. }
  25056. }
  25057. #endif
  25058. // #include "../core/type_traits.hpp"
  25059. // #include "meta.hpp"
  25060. #ifndef ENTT_META_META_HPP
  25061. #define ENTT_META_META_HPP
  25062. #include <cstddef>
  25063. #include <functional>
  25064. #include <iterator>
  25065. #include <memory>
  25066. #include <type_traits>
  25067. #include <utility>
  25068. // #include "../config/config.h"
  25069. // #include "../core/any.hpp"
  25070. #ifndef ENTT_CORE_ANY_HPP
  25071. #define ENTT_CORE_ANY_HPP
  25072. #include <cstddef>
  25073. #include <functional>
  25074. #include <memory>
  25075. #include <type_traits>
  25076. #include <utility>
  25077. // #include "../config/config.h"
  25078. // #include "../core/utility.hpp"
  25079. #ifndef ENTT_CORE_UTILITY_HPP
  25080. #define ENTT_CORE_UTILITY_HPP
  25081. #include <utility>
  25082. // #include "../config/config.h"
  25083. #ifndef ENTT_CONFIG_CONFIG_H
  25084. #define ENTT_CONFIG_CONFIG_H
  25085. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  25086. # define ENTT_NOEXCEPT noexcept
  25087. # define ENTT_THROW throw
  25088. # define ENTT_TRY try
  25089. # define ENTT_CATCH catch(...)
  25090. #else
  25091. # define ENTT_NOEXCEPT
  25092. # define ENTT_THROW
  25093. # define ENTT_TRY if(true)
  25094. # define ENTT_CATCH if(false)
  25095. #endif
  25096. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  25097. # include <new>
  25098. # define ENTT_LAUNDER(expr) std::launder(expr)
  25099. #else
  25100. # define ENTT_LAUNDER(expr) expr
  25101. #endif
  25102. #ifndef ENTT_USE_ATOMIC
  25103. # define ENTT_MAYBE_ATOMIC(Type) Type
  25104. #else
  25105. # include <atomic>
  25106. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  25107. #endif
  25108. #ifndef ENTT_ID_TYPE
  25109. # include <cstdint>
  25110. # define ENTT_ID_TYPE std::uint32_t
  25111. #endif
  25112. #ifdef ENTT_SPARSE_PAGE
  25113. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  25114. #else
  25115. # define ENTT_SPARSE_PAGE 4096
  25116. #endif
  25117. #ifdef ENTT_PACKED_PAGE
  25118. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  25119. #else
  25120. # define ENTT_PACKED_PAGE 1024
  25121. #endif
  25122. #ifdef ENTT_DISABLE_ASSERT
  25123. # undef ENTT_ASSERT
  25124. # define ENTT_ASSERT(...) (void(0))
  25125. #elif !defined ENTT_ASSERT
  25126. # include <cassert>
  25127. # define ENTT_ASSERT(condition, ...) assert(condition)
  25128. #endif
  25129. #ifdef ENTT_NO_ETO
  25130. # include <type_traits>
  25131. # define ENTT_IGNORE_IF_EMPTY std::false_type
  25132. #else
  25133. # include <type_traits>
  25134. # define ENTT_IGNORE_IF_EMPTY std::true_type
  25135. #endif
  25136. #ifndef ENTT_STANDARD_CPP
  25137. # if defined __clang__ || defined __GNUC__
  25138. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  25139. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  25140. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  25141. # elif defined _MSC_VER
  25142. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  25143. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  25144. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  25145. # endif
  25146. #endif
  25147. #endif
  25148. namespace entt {
  25149. /*! @brief Identity function object (waiting for C++20). */
  25150. struct identity {
  25151. /**
  25152. * @brief Returns its argument unchanged.
  25153. * @tparam Type Type of the argument.
  25154. * @param value The actual argument.
  25155. * @return The submitted value as-is.
  25156. */
  25157. template<class Type>
  25158. [[nodiscard]] constexpr Type && operator()(Type &&value) const ENTT_NOEXCEPT {
  25159. return std::forward<Type>(value);
  25160. }
  25161. };
  25162. /**
  25163. * @brief Constant utility to disambiguate overloaded members of a class.
  25164. * @tparam Type Type of the desired overload.
  25165. * @tparam Class Type of class to which the member belongs.
  25166. * @param member A valid pointer to a member.
  25167. * @return Pointer to the member.
  25168. */
  25169. template<typename Type, typename Class>
  25170. [[nodiscard]] constexpr auto overload(Type Class:: *member) ENTT_NOEXCEPT { return member; }
  25171. /**
  25172. * @brief Constant utility to disambiguate overloaded functions.
  25173. * @tparam Func Function type of the desired overload.
  25174. * @param func A valid pointer to a function.
  25175. * @return Pointer to the function.
  25176. */
  25177. template<typename Func>
  25178. [[nodiscard]] constexpr auto overload(Func *func) ENTT_NOEXCEPT { return func; }
  25179. /**
  25180. * @brief Helper type for visitors.
  25181. * @tparam Func Types of function objects.
  25182. */
  25183. template<class... Func>
  25184. struct overloaded: Func... {
  25185. using Func::operator()...;
  25186. };
  25187. /**
  25188. * @brief Deduction guide.
  25189. * @tparam Func Types of function objects.
  25190. */
  25191. template<class... Func>
  25192. overloaded(Func...)
  25193. -> overloaded<Func...>;
  25194. /**
  25195. * @brief Basic implementation of a y-combinator.
  25196. * @tparam Func Type of a potentially recursive function.
  25197. */
  25198. template<class Func>
  25199. struct y_combinator {
  25200. /**
  25201. * @brief Constructs a y-combinator from a given function.
  25202. * @param recursive A potentially recursive function.
  25203. */
  25204. y_combinator(Func recursive):
  25205. func{std::move(recursive)}
  25206. {}
  25207. /**
  25208. * @brief Invokes a y-combinator and therefore its underlying function.
  25209. * @tparam Args Types of arguments to use to invoke the underlying function.
  25210. * @param args Parameters to use to invoke the underlying function.
  25211. * @return Return value of the underlying function, if any.
  25212. */
  25213. template <class... Args>
  25214. decltype(auto) operator()(Args &&... args) const {
  25215. return func(*this, std::forward<Args>(args)...);
  25216. }
  25217. /*! @copydoc operator()() */
  25218. template <class... Args>
  25219. decltype(auto) operator()(Args &&... args) {
  25220. return func(*this, std::forward<Args>(args)...);
  25221. }
  25222. private:
  25223. Func func;
  25224. };
  25225. }
  25226. #endif
  25227. // #include "fwd.hpp"
  25228. // #include "type_info.hpp"
  25229. #ifndef ENTT_CORE_TYPE_INFO_HPP
  25230. #define ENTT_CORE_TYPE_INFO_HPP
  25231. #include <string_view>
  25232. #include <type_traits>
  25233. // #include "../config/config.h"
  25234. // #include "../core/attribute.h"
  25235. // #include "hashed_string.hpp"
  25236. // #include "fwd.hpp"
  25237. namespace entt {
  25238. /**
  25239. * @cond TURN_OFF_DOXYGEN
  25240. * Internal details not to be documented.
  25241. */
  25242. namespace internal {
  25243. struct ENTT_API type_seq final {
  25244. [[nodiscard]] static id_type next() ENTT_NOEXCEPT {
  25245. static ENTT_MAYBE_ATOMIC(id_type) value{};
  25246. return value++;
  25247. }
  25248. };
  25249. template<typename Type>
  25250. [[nodiscard]] constexpr auto stripped_type_name() ENTT_NOEXCEPT {
  25251. #if defined ENTT_PRETTY_FUNCTION
  25252. std::string_view pretty_function{ENTT_PRETTY_FUNCTION};
  25253. auto first = pretty_function.find_first_not_of(' ', pretty_function.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX)+1);
  25254. auto value = pretty_function.substr(first, pretty_function.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
  25255. return value;
  25256. #else
  25257. return std::string_view{""};
  25258. #endif
  25259. }
  25260. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  25261. [[nodiscard]] static constexpr std::string_view type_name(int) ENTT_NOEXCEPT {
  25262. constexpr auto value = stripped_type_name<Type>();
  25263. return value;
  25264. }
  25265. template<typename Type>
  25266. [[nodiscard]] static std::string_view type_name(char) ENTT_NOEXCEPT {
  25267. static const auto value = stripped_type_name<Type>();
  25268. return value;
  25269. }
  25270. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  25271. [[nodiscard]] static constexpr id_type type_hash(int) ENTT_NOEXCEPT {
  25272. constexpr auto stripped = stripped_type_name<Type>();
  25273. constexpr auto value = hashed_string::value(stripped.data(), stripped.size());
  25274. return value;
  25275. }
  25276. template<typename Type>
  25277. [[nodiscard]] static id_type type_hash(char) ENTT_NOEXCEPT {
  25278. static const auto value = [](const auto stripped) {
  25279. return hashed_string::value(stripped.data(), stripped.size());
  25280. }(stripped_type_name<Type>());
  25281. return value;
  25282. }
  25283. }
  25284. /**
  25285. * Internal details not to be documented.
  25286. * @endcond
  25287. */
  25288. /**
  25289. * @brief Type sequential identifier.
  25290. * @tparam Type Type for which to generate a sequential identifier.
  25291. */
  25292. template<typename Type, typename = void>
  25293. struct ENTT_API type_seq final {
  25294. /**
  25295. * @brief Returns the sequential identifier of a given type.
  25296. * @return The sequential identifier of a given type.
  25297. */
  25298. [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
  25299. static const id_type value = internal::type_seq::next();
  25300. return value;
  25301. }
  25302. /*! @copydoc value */
  25303. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  25304. };
  25305. /**
  25306. * @brief Type hash.
  25307. * @tparam Type Type for which to generate a hash value.
  25308. */
  25309. template<typename Type, typename = void>
  25310. struct type_hash final {
  25311. /**
  25312. * @brief Returns the numeric representation of a given type.
  25313. * @return The numeric representation of the given type.
  25314. */
  25315. #if defined ENTT_PRETTY_FUNCTION
  25316. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  25317. return internal::type_hash<Type>(0);
  25318. #else
  25319. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  25320. return type_seq<Type>::value();
  25321. #endif
  25322. }
  25323. /*! @copydoc value */
  25324. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  25325. };
  25326. /**
  25327. * @brief Type name.
  25328. * @tparam Type Type for which to generate a name.
  25329. */
  25330. template<typename Type, typename = void>
  25331. struct type_name final {
  25332. /**
  25333. * @brief Returns the name of a given type.
  25334. * @return The name of the given type.
  25335. */
  25336. [[nodiscard]] static constexpr std::string_view value() ENTT_NOEXCEPT {
  25337. return internal::type_name<Type>(0);
  25338. }
  25339. /*! @copydoc value */
  25340. [[nodiscard]] constexpr operator std::string_view() const ENTT_NOEXCEPT { return value(); }
  25341. };
  25342. /*! @brief Implementation specific information about a type. */
  25343. class type_info final {
  25344. template<typename>
  25345. friend type_info type_id() ENTT_NOEXCEPT;
  25346. type_info(id_type seq_v, id_type hash_v, std::string_view name_v) ENTT_NOEXCEPT
  25347. : seq_value{seq_v},
  25348. hash_value{hash_v},
  25349. name_value{name_v}
  25350. {}
  25351. public:
  25352. /*! @brief Default constructor. */
  25353. type_info() ENTT_NOEXCEPT
  25354. : type_info({}, {}, {})
  25355. {}
  25356. /*! @brief Default copy constructor. */
  25357. type_info(const type_info &) ENTT_NOEXCEPT = default;
  25358. /*! @brief Default move constructor. */
  25359. type_info(type_info &&) ENTT_NOEXCEPT = default;
  25360. /**
  25361. * @brief Default copy assignment operator.
  25362. * @return This type info object.
  25363. */
  25364. type_info & operator=(const type_info &) ENTT_NOEXCEPT = default;
  25365. /**
  25366. * @brief Default move assignment operator.
  25367. * @return This type info object.
  25368. */
  25369. type_info & operator=(type_info &&) ENTT_NOEXCEPT = default;
  25370. /**
  25371. * @brief Checks if a type info object is properly initialized.
  25372. * @return True if the object is properly initialized, false otherwise.
  25373. */
  25374. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  25375. return name_value.data() != nullptr;
  25376. }
  25377. /**
  25378. * @brief Type sequential identifier.
  25379. * @return Type sequential identifier.
  25380. */
  25381. [[nodiscard]] id_type seq() const ENTT_NOEXCEPT {
  25382. return seq_value;
  25383. }
  25384. /**
  25385. * @brief Type hash.
  25386. * @return Type hash.
  25387. */
  25388. [[nodiscard]] id_type hash() const ENTT_NOEXCEPT {
  25389. return hash_value;
  25390. }
  25391. /**
  25392. * @brief Type name.
  25393. * @return Type name.
  25394. */
  25395. [[nodiscard]] std::string_view name() const ENTT_NOEXCEPT {
  25396. return name_value;
  25397. }
  25398. /**
  25399. * @brief Compares the contents of two type info objects.
  25400. * @param other Object with which to compare.
  25401. * @return False if the two contents differ, true otherwise.
  25402. */
  25403. [[nodiscard]] bool operator==(const type_info &other) const ENTT_NOEXCEPT {
  25404. return hash_value == other.hash_value;
  25405. }
  25406. private:
  25407. id_type seq_value;
  25408. id_type hash_value;
  25409. std::string_view name_value;
  25410. };
  25411. /**
  25412. * @brief Compares the contents of two type info objects.
  25413. * @param lhs A type info object.
  25414. * @param rhs A type info object.
  25415. * @return True if the two contents differ, false otherwise.
  25416. */
  25417. [[nodiscard]] inline bool operator!=(const type_info &lhs, const type_info &rhs) ENTT_NOEXCEPT {
  25418. return !(lhs == rhs);
  25419. }
  25420. /**
  25421. * @brief Returns the type info object for a given type.
  25422. * @tparam Type Type for which to generate a type info object.
  25423. * @return The type info object for the given type.
  25424. */
  25425. template<typename Type>
  25426. [[nodiscard]] type_info type_id() ENTT_NOEXCEPT {
  25427. return type_info{
  25428. type_seq<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  25429. type_hash<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  25430. type_name<std::remove_cv_t<std::remove_reference_t<Type>>>::value()
  25431. };
  25432. }
  25433. }
  25434. #endif
  25435. // #include "type_traits.hpp"
  25436. #ifndef ENTT_CORE_TYPE_TRAITS_HPP
  25437. #define ENTT_CORE_TYPE_TRAITS_HPP
  25438. #include <cstddef>
  25439. #include <iterator>
  25440. #include <type_traits>
  25441. #include <utility>
  25442. // #include "../config/config.h"
  25443. // #include "fwd.hpp"
  25444. namespace entt {
  25445. /**
  25446. * @brief Utility class to disambiguate overloaded functions.
  25447. * @tparam N Number of choices available.
  25448. */
  25449. template<std::size_t N>
  25450. struct choice_t
  25451. // Unfortunately, doxygen cannot parse such a construct.
  25452. /*! @cond TURN_OFF_DOXYGEN */
  25453. : choice_t<N-1>
  25454. /*! @endcond */
  25455. {};
  25456. /*! @copybrief choice_t */
  25457. template<>
  25458. struct choice_t<0> {};
  25459. /**
  25460. * @brief Variable template for the choice trick.
  25461. * @tparam N Number of choices available.
  25462. */
  25463. template<std::size_t N>
  25464. inline constexpr choice_t<N> choice{};
  25465. /**
  25466. * @brief Identity type trait.
  25467. *
  25468. * Useful to establish non-deduced contexts in template argument deduction
  25469. * (waiting for C++20) or to provide types through function arguments.
  25470. *
  25471. * @tparam Type A type.
  25472. */
  25473. template<typename Type>
  25474. struct type_identity {
  25475. /*! @brief Identity type. */
  25476. using type = Type;
  25477. };
  25478. /**
  25479. * @brief Helper type.
  25480. * @tparam Type A type.
  25481. */
  25482. template<typename Type>
  25483. using type_identity_t = typename type_identity<Type>::type;
  25484. /**
  25485. * @brief A type-only `sizeof` wrapper that returns 0 where `sizeof` complains.
  25486. * @tparam Type The type of which to return the size.
  25487. * @tparam The size of the type if `sizeof` accepts it, 0 otherwise.
  25488. */
  25489. template<typename Type, typename = void>
  25490. struct size_of: std::integral_constant<std::size_t, 0u> {};
  25491. /*! @copydoc size_of */
  25492. template<typename Type>
  25493. struct size_of<Type, std::void_t<decltype(sizeof(Type))>>
  25494. : std::integral_constant<std::size_t, sizeof(Type)>
  25495. {};
  25496. /**
  25497. * @brief Helper variable template.
  25498. * @tparam Type The type of which to return the size.
  25499. */
  25500. template<class Type>
  25501. inline constexpr std::size_t size_of_v = size_of<Type>::value;
  25502. /**
  25503. * @brief Using declaration to be used to _repeat_ the same type a number of
  25504. * times equal to the size of a given parameter pack.
  25505. * @tparam Type A type to repeat.
  25506. */
  25507. template<typename Type, typename>
  25508. using unpack_as_t = Type;
  25509. /**
  25510. * @brief Helper variable template to be used to _repeat_ the same value a
  25511. * number of times equal to the size of a given parameter pack.
  25512. * @tparam Value A value to repeat.
  25513. */
  25514. template<auto Value, typename>
  25515. inline constexpr auto unpack_as_v = Value;
  25516. /**
  25517. * @brief Wraps a static constant.
  25518. * @tparam Value A static constant.
  25519. */
  25520. template<auto Value>
  25521. using integral_constant = std::integral_constant<decltype(Value), Value>;
  25522. /**
  25523. * @brief Alias template to facilitate the creation of named values.
  25524. * @tparam Value A constant value at least convertible to `id_type`.
  25525. */
  25526. template<id_type Value>
  25527. using tag = integral_constant<Value>;
  25528. /**
  25529. * @brief A class to use to push around lists of types, nothing more.
  25530. * @tparam Type Types provided by the type list.
  25531. */
  25532. template<typename... Type>
  25533. struct type_list {
  25534. /*! @brief Type list type. */
  25535. using type = type_list;
  25536. /*! @brief Compile-time number of elements in the type list. */
  25537. static constexpr auto size = sizeof...(Type);
  25538. };
  25539. /*! @brief Primary template isn't defined on purpose. */
  25540. template<std::size_t, typename>
  25541. struct type_list_element;
  25542. /**
  25543. * @brief Provides compile-time indexed access to the types of a type list.
  25544. * @tparam Index Index of the type to return.
  25545. * @tparam Type First type provided by the type list.
  25546. * @tparam Other Other types provided by the type list.
  25547. */
  25548. template<std::size_t Index, typename Type, typename... Other>
  25549. struct type_list_element<Index, type_list<Type, Other...>>
  25550. : type_list_element<Index - 1u, type_list<Other...>>
  25551. {};
  25552. /**
  25553. * @brief Provides compile-time indexed access to the types of a type list.
  25554. * @tparam Type First type provided by the type list.
  25555. * @tparam Other Other types provided by the type list.
  25556. */
  25557. template<typename Type, typename... Other>
  25558. struct type_list_element<0u, type_list<Type, Other...>> {
  25559. /*! @brief Searched type. */
  25560. using type = Type;
  25561. };
  25562. /**
  25563. * @brief Helper type.
  25564. * @tparam Index Index of the type to return.
  25565. * @tparam List Type list to search into.
  25566. */
  25567. template<std::size_t Index, typename List>
  25568. using type_list_element_t = typename type_list_element<Index, List>::type;
  25569. /**
  25570. * @brief Concatenates multiple type lists.
  25571. * @tparam Type Types provided by the first type list.
  25572. * @tparam Other Types provided by the second type list.
  25573. * @return A type list composed by the types of both the type lists.
  25574. */
  25575. template<typename... Type, typename... Other>
  25576. constexpr type_list<Type..., Other...> operator+(type_list<Type...>, type_list<Other...>) { return {}; }
  25577. /*! @brief Primary template isn't defined on purpose. */
  25578. template<typename...>
  25579. struct type_list_cat;
  25580. /*! @brief Concatenates multiple type lists. */
  25581. template<>
  25582. struct type_list_cat<> {
  25583. /*! @brief A type list composed by the types of all the type lists. */
  25584. using type = type_list<>;
  25585. };
  25586. /**
  25587. * @brief Concatenates multiple type lists.
  25588. * @tparam Type Types provided by the first type list.
  25589. * @tparam Other Types provided by the second type list.
  25590. * @tparam List Other type lists, if any.
  25591. */
  25592. template<typename... Type, typename... Other, typename... List>
  25593. struct type_list_cat<type_list<Type...>, type_list<Other...>, List...> {
  25594. /*! @brief A type list composed by the types of all the type lists. */
  25595. using type = typename type_list_cat<type_list<Type..., Other...>, List...>::type;
  25596. };
  25597. /**
  25598. * @brief Concatenates multiple type lists.
  25599. * @tparam Type Types provided by the type list.
  25600. */
  25601. template<typename... Type>
  25602. struct type_list_cat<type_list<Type...>> {
  25603. /*! @brief A type list composed by the types of all the type lists. */
  25604. using type = type_list<Type...>;
  25605. };
  25606. /**
  25607. * @brief Helper type.
  25608. * @tparam List Type lists to concatenate.
  25609. */
  25610. template<typename... List>
  25611. using type_list_cat_t = typename type_list_cat<List...>::type;
  25612. /*! @brief Primary template isn't defined on purpose. */
  25613. template<typename>
  25614. struct type_list_unique;
  25615. /**
  25616. * @brief Removes duplicates types from a type list.
  25617. * @tparam Type One of the types provided by the given type list.
  25618. * @tparam Other The other types provided by the given type list.
  25619. */
  25620. template<typename Type, typename... Other>
  25621. struct type_list_unique<type_list<Type, Other...>> {
  25622. /*! @brief A type list without duplicate types. */
  25623. using type = std::conditional_t<
  25624. std::disjunction_v<std::is_same<Type, Other>...>,
  25625. typename type_list_unique<type_list<Other...>>::type,
  25626. type_list_cat_t<type_list<Type>, typename type_list_unique<type_list<Other...>>::type>
  25627. >;
  25628. };
  25629. /*! @brief Removes duplicates types from a type list. */
  25630. template<>
  25631. struct type_list_unique<type_list<>> {
  25632. /*! @brief A type list without duplicate types. */
  25633. using type = type_list<>;
  25634. };
  25635. /**
  25636. * @brief Helper type.
  25637. * @tparam Type A type list.
  25638. */
  25639. template<typename Type>
  25640. using type_list_unique_t = typename type_list_unique<Type>::type;
  25641. /**
  25642. * @brief Provides the member constant `value` to true if a type list contains a
  25643. * given type, false otherwise.
  25644. * @tparam List Type list.
  25645. * @tparam Type Type to look for.
  25646. */
  25647. template<typename List, typename Type>
  25648. struct type_list_contains;
  25649. /**
  25650. * @copybrief type_list_contains
  25651. * @tparam Type Types provided by the type list.
  25652. * @tparam Other Type to look for.
  25653. */
  25654. template<typename... Type, typename Other>
  25655. struct type_list_contains<type_list<Type...>, Other>: std::disjunction<std::is_same<Type, Other>...> {};
  25656. /**
  25657. * @brief Helper variable template.
  25658. * @tparam List Type list.
  25659. * @tparam Type Type to look for.
  25660. */
  25661. template<class List, typename Type>
  25662. inline constexpr bool type_list_contains_v = type_list_contains<List, Type>::value;
  25663. /*! @brief Primary template isn't defined on purpose. */
  25664. template<typename...>
  25665. struct type_list_diff;
  25666. /**
  25667. * @brief Computes the difference between two type lists.
  25668. * @tparam Type Types provided by the first type list.
  25669. * @tparam Other Types provided by the second type list.
  25670. */
  25671. template<typename... Type, typename... Other>
  25672. struct type_list_diff<type_list<Type...>, type_list<Other...>> {
  25673. /*! @brief A type list that is the difference between the two type lists. */
  25674. using type = type_list_cat_t<std::conditional_t<type_list_contains_v<type_list<Other...>, Type>, type_list<>, type_list<Type>>...>;
  25675. };
  25676. /**
  25677. * @brief Helper type.
  25678. * @tparam List Type lists between which to compute the difference.
  25679. */
  25680. template<typename... List>
  25681. using type_list_diff_t = typename type_list_diff<List...>::type;
  25682. /**
  25683. * @brief A class to use to push around lists of constant values, nothing more.
  25684. * @tparam Value Values provided by the value list.
  25685. */
  25686. template<auto... Value>
  25687. struct value_list {
  25688. /*! @brief Value list type. */
  25689. using type = value_list;
  25690. /*! @brief Compile-time number of elements in the value list. */
  25691. static constexpr auto size = sizeof...(Value);
  25692. };
  25693. /*! @brief Primary template isn't defined on purpose. */
  25694. template<std::size_t, typename>
  25695. struct value_list_element;
  25696. /**
  25697. * @brief Provides compile-time indexed access to the values of a value list.
  25698. * @tparam Index Index of the value to return.
  25699. * @tparam Value First value provided by the value list.
  25700. * @tparam Other Other values provided by the value list.
  25701. */
  25702. template<std::size_t Index, auto Value, auto... Other>
  25703. struct value_list_element<Index, value_list<Value, Other...>>
  25704. : value_list_element<Index - 1u, value_list<Other...>>
  25705. {};
  25706. /**
  25707. * @brief Provides compile-time indexed access to the types of a type list.
  25708. * @tparam Value First value provided by the value list.
  25709. * @tparam Other Other values provided by the value list.
  25710. */
  25711. template<auto Value, auto... Other>
  25712. struct value_list_element<0u, value_list<Value, Other...>> {
  25713. /*! @brief Searched value. */
  25714. static constexpr auto value = Value;
  25715. };
  25716. /**
  25717. * @brief Helper type.
  25718. * @tparam Index Index of the value to return.
  25719. * @tparam List Value list to search into.
  25720. */
  25721. template<std::size_t Index, typename List>
  25722. inline constexpr auto value_list_element_v = value_list_element<Index, List>::value;
  25723. /**
  25724. * @brief Concatenates multiple value lists.
  25725. * @tparam Value Values provided by the first value list.
  25726. * @tparam Other Values provided by the second value list.
  25727. * @return A value list composed by the values of both the value lists.
  25728. */
  25729. template<auto... Value, auto... Other>
  25730. constexpr value_list<Value..., Other...> operator+(value_list<Value...>, value_list<Other...>) { return {}; }
  25731. /*! @brief Primary template isn't defined on purpose. */
  25732. template<typename...>
  25733. struct value_list_cat;
  25734. /*! @brief Concatenates multiple value lists. */
  25735. template<>
  25736. struct value_list_cat<> {
  25737. /*! @brief A value list composed by the values of all the value lists. */
  25738. using type = value_list<>;
  25739. };
  25740. /**
  25741. * @brief Concatenates multiple value lists.
  25742. * @tparam Value Values provided by the first value list.
  25743. * @tparam Other Values provided by the second value list.
  25744. * @tparam List Other value lists, if any.
  25745. */
  25746. template<auto... Value, auto... Other, typename... List>
  25747. struct value_list_cat<value_list<Value...>, value_list<Other...>, List...> {
  25748. /*! @brief A value list composed by the values of all the value lists. */
  25749. using type = typename value_list_cat<value_list<Value..., Other...>, List...>::type;
  25750. };
  25751. /**
  25752. * @brief Concatenates multiple value lists.
  25753. * @tparam Value Values provided by the value list.
  25754. */
  25755. template<auto... Value>
  25756. struct value_list_cat<value_list<Value...>> {
  25757. /*! @brief A value list composed by the values of all the value lists. */
  25758. using type = value_list<Value...>;
  25759. };
  25760. /**
  25761. * @brief Helper type.
  25762. * @tparam List Value lists to concatenate.
  25763. */
  25764. template<typename... List>
  25765. using value_list_cat_t = typename value_list_cat<List...>::type;
  25766. /**
  25767. * @cond TURN_OFF_DOXYGEN
  25768. * Internal details not to be documented.
  25769. */
  25770. namespace internal {
  25771. template<typename>
  25772. [[nodiscard]] constexpr bool is_equality_comparable(...) { return false; }
  25773. template<typename Type>
  25774. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<0>)
  25775. -> decltype(std::declval<Type>() == std::declval<Type>()) { return true; }
  25776. template<typename Type>
  25777. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<1>)
  25778. -> decltype(std::declval<typename Type::value_type>(), std::declval<Type>() == std::declval<Type>()) {
  25779. if constexpr(std::is_same_v<typename Type::value_type, Type>) {
  25780. return is_equality_comparable<Type>(choice<0>);
  25781. } else {
  25782. return is_equality_comparable<typename Type::value_type>(choice<2>);
  25783. }
  25784. }
  25785. template<typename Type>
  25786. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<2>)
  25787. -> decltype(std::declval<typename Type::mapped_type>(), std::declval<Type>() == std::declval<Type>()) {
  25788. return is_equality_comparable<typename Type::key_type>(choice<2>) && is_equality_comparable<typename Type::mapped_type>(choice<2>);
  25789. }
  25790. }
  25791. /**
  25792. * Internal details not to be documented.
  25793. * @endcond
  25794. */
  25795. /**
  25796. * @brief Provides the member constant `value` to true if a given type is
  25797. * equality comparable, false otherwise.
  25798. * @tparam Type The type to test.
  25799. */
  25800. template<typename Type, typename = void>
  25801. struct is_equality_comparable: std::bool_constant<internal::is_equality_comparable<Type>(choice<2>)> {};
  25802. /**
  25803. * @brief Helper variable template.
  25804. * @tparam Type The type to test.
  25805. */
  25806. template<class Type>
  25807. inline constexpr bool is_equality_comparable_v = is_equality_comparable<Type>::value;
  25808. /*! @brief Same as std::is_invocable, but with tuples. */
  25809. template<typename, typename>
  25810. struct is_applicable: std::false_type {};
  25811. /**
  25812. * @copybrief is_applicable
  25813. * @tparam Func A valid function type.
  25814. * @tparam Tuple Tuple-like type.
  25815. * @tparam Args The list of arguments to use to probe the function type.
  25816. */
  25817. template<typename Func, template<typename...> class Tuple, typename... Args>
  25818. struct is_applicable<Func, Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  25819. /**
  25820. * @copybrief is_applicable
  25821. * @tparam Func A valid function type.
  25822. * @tparam Tuple Tuple-like type.
  25823. * @tparam Args The list of arguments to use to probe the function type.
  25824. */
  25825. template<typename Func, template<typename...> class Tuple, typename... Args>
  25826. struct is_applicable<Func, const Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  25827. /**
  25828. * @brief Helper variable template.
  25829. * @tparam Func A valid function type.
  25830. * @tparam Args The list of arguments to use to probe the function type.
  25831. */
  25832. template<typename Func, typename Args>
  25833. inline constexpr bool is_applicable_v = is_applicable<Func, Args>::value;
  25834. /*! @brief Same as std::is_invocable_r, but with tuples for arguments. */
  25835. template<typename, typename, typename>
  25836. struct is_applicable_r: std::false_type {};
  25837. /**
  25838. * @copybrief is_applicable_r
  25839. * @tparam Ret The type to which the return type of the function should be
  25840. * convertible.
  25841. * @tparam Func A valid function type.
  25842. * @tparam Args The list of arguments to use to probe the function type.
  25843. */
  25844. template<typename Ret, typename Func, typename... Args>
  25845. struct is_applicable_r<Ret, Func, std::tuple<Args...>>: std::is_invocable_r<Ret, Func, Args...> {};
  25846. /**
  25847. * @brief Helper variable template.
  25848. * @tparam Ret The type to which the return type of the function should be
  25849. * convertible.
  25850. * @tparam Func A valid function type.
  25851. * @tparam Args The list of arguments to use to probe the function type.
  25852. */
  25853. template<typename Ret, typename Func, typename Args>
  25854. inline constexpr bool is_applicable_r_v = is_applicable_r<Ret, Func, Args>::value;
  25855. /**
  25856. * @brief Provides the member constant `value` to true if a given type is
  25857. * complete, false otherwise.
  25858. * @tparam Type The type to test.
  25859. */
  25860. template<typename Type, typename = void>
  25861. struct is_complete: std::false_type {};
  25862. /*! @copydoc is_complete */
  25863. template<typename Type>
  25864. struct is_complete<Type, std::void_t<decltype(sizeof(Type))>>: std::true_type {};
  25865. /**
  25866. * @brief Helper variable template.
  25867. * @tparam Type The type to test.
  25868. */
  25869. template<typename Type>
  25870. inline constexpr bool is_complete_v = is_complete<Type>::value;
  25871. /**
  25872. * @brief Provides the member constant `value` to true if a given type is an
  25873. * iterator, false otherwise.
  25874. * @tparam Type The type to test.
  25875. */
  25876. template<typename Type, typename = void>
  25877. struct is_iterator: std::false_type {};
  25878. /*! @copydoc is_iterator */
  25879. template<typename Type>
  25880. struct is_iterator<Type, std::void_t<typename std::iterator_traits<Type>::iterator_category>>
  25881. : std::true_type
  25882. {};
  25883. /**
  25884. * @brief Helper variable template.
  25885. * @tparam Type The type to test.
  25886. */
  25887. template<typename Type>
  25888. inline constexpr bool is_iterator_v = is_iterator<Type>::value;
  25889. /**
  25890. * @brief Provides the member constant `value` to true if a given type is of the
  25891. * required iterator type, false otherwise.
  25892. * @tparam Type The type to test.
  25893. * @tparam It Required iterator type.
  25894. */
  25895. template<typename Type, typename It, typename = void>
  25896. struct is_iterator_type: std::false_type {};
  25897. /*! @copydoc is_iterator_type */
  25898. template<typename Type, typename It>
  25899. struct is_iterator_type<Type, It, std::enable_if_t<is_iterator_v<Type> && std::is_same_v<Type, It>>>
  25900. : std::true_type
  25901. {};
  25902. /*! @copydoc is_iterator_type */
  25903. template<typename Type, typename It>
  25904. struct is_iterator_type<Type, It, std::enable_if_t<!std::is_same_v<Type, It>, std::void_t<typename It::iterator_type>>>
  25905. : is_iterator_type<Type, typename It::iterator_type>
  25906. {};
  25907. /**
  25908. * @brief Helper variable template.
  25909. * @tparam Type The type to test.
  25910. * @tparam It Required iterator type.
  25911. */
  25912. template<typename Type, typename It>
  25913. inline constexpr bool is_iterator_type_v = is_iterator_type<Type, It>::value;
  25914. /**
  25915. * @brief Transcribes the constness of a type to another type.
  25916. * @tparam To The type to which to transcribe the constness.
  25917. * @tparam From The type from which to transcribe the constness.
  25918. */
  25919. template<typename To, typename From>
  25920. struct constness_as {
  25921. /*! @brief The type resulting from the transcription of the constness. */
  25922. using type = std::remove_const_t<To>;
  25923. };
  25924. /*! @copydoc constness_as */
  25925. template<typename To, typename From>
  25926. struct constness_as<To, const From> {
  25927. /*! @brief The type resulting from the transcription of the constness. */
  25928. using type = std::add_const_t<To>;
  25929. };
  25930. /**
  25931. * @brief Alias template to facilitate the transcription of the constness.
  25932. * @tparam To The type to which to transcribe the constness.
  25933. * @tparam From The type from which to transcribe the constness.
  25934. */
  25935. template<typename To, typename From>
  25936. using constness_as_t = typename constness_as<To, From>::type;
  25937. /**
  25938. * @brief Extracts the class of a non-static member object or function.
  25939. * @tparam Member A pointer to a non-static member object or function.
  25940. */
  25941. template<typename Member>
  25942. class member_class {
  25943. static_assert(std::is_member_pointer_v<Member>, "Invalid pointer type to non-static member object or function");
  25944. template<typename Class, typename Ret, typename... Args>
  25945. static Class * clazz(Ret(Class:: *)(Args...));
  25946. template<typename Class, typename Ret, typename... Args>
  25947. static Class * clazz(Ret(Class:: *)(Args...) const);
  25948. template<typename Class, typename Type>
  25949. static Class * clazz(Type Class:: *);
  25950. public:
  25951. /*! @brief The class of the given non-static member object or function. */
  25952. using type = std::remove_pointer_t<decltype(clazz(std::declval<Member>()))>;
  25953. };
  25954. /**
  25955. * @brief Helper type.
  25956. * @tparam Member A pointer to a non-static member object or function.
  25957. */
  25958. template<typename Member>
  25959. using member_class_t = typename member_class<Member>::type;
  25960. }
  25961. #endif
  25962. namespace entt {
  25963. /**
  25964. * @brief A SBO friendly, type-safe container for single values of any type.
  25965. * @tparam Len Size of the storage reserved for the small buffer optimization.
  25966. * @tparam Align Optional alignment requirement.
  25967. */
  25968. template<std::size_t Len, std::size_t Align>
  25969. class basic_any {
  25970. enum class operation: std::uint8_t { COPY, MOVE, DTOR, COMP, ADDR, CADDR, TYPE };
  25971. enum class policy: std::uint8_t { OWNER, REF, CREF };
  25972. using storage_type = std::aligned_storage_t<Len + !Len, Align>;
  25973. using vtable_type = const void *(const operation, const basic_any &, void *);
  25974. template<typename Type>
  25975. static constexpr bool in_situ = Len && alignof(Type) <= alignof(storage_type) && sizeof(Type) <= sizeof(storage_type) && std::is_nothrow_move_constructible_v<Type>;
  25976. template<typename Type>
  25977. [[nodiscard]] static constexpr policy type_to_policy() {
  25978. if constexpr(std::is_lvalue_reference_v<Type>) {
  25979. if constexpr(std::is_const_v<std::remove_reference_t<Type>>) {
  25980. return policy::CREF;
  25981. } else {
  25982. return policy::REF;
  25983. }
  25984. } else {
  25985. return policy::OWNER;
  25986. }
  25987. }
  25988. template<typename Type>
  25989. [[nodiscard]] static bool compare(const void *lhs, const void *rhs) {
  25990. if constexpr(!std::is_function_v<Type> && is_equality_comparable_v<Type>) {
  25991. return *static_cast<const Type *>(lhs) == *static_cast<const Type *>(rhs);
  25992. } else {
  25993. return lhs == rhs;
  25994. }
  25995. }
  25996. template<typename Type>
  25997. static const void * basic_vtable([[maybe_unused]] const operation op, [[maybe_unused]] const basic_any &from, [[maybe_unused]] void *to) {
  25998. static_assert(std::is_same_v<std::remove_reference_t<std::remove_const_t<Type>>, Type>, "Invalid type");
  25999. if constexpr(!std::is_void_v<Type>) {
  26000. const Type *instance = (in_situ<Type> && from.mode == policy::OWNER)
  26001. ? ENTT_LAUNDER(reinterpret_cast<const Type *>(&from.storage))
  26002. : static_cast<const Type *>(from.instance);
  26003. switch(op) {
  26004. case operation::COPY:
  26005. if constexpr(std::is_copy_constructible_v<Type>) {
  26006. static_cast<basic_any *>(to)->emplace<Type>(*instance);
  26007. }
  26008. break;
  26009. case operation::MOVE:
  26010. if constexpr(in_situ<Type>) {
  26011. if(from.mode == policy::OWNER) {
  26012. return new (&static_cast<basic_any *>(to)->storage) Type{std::move(*const_cast<Type *>(instance))};
  26013. }
  26014. }
  26015. return (static_cast<basic_any *>(to)->instance = std::exchange(const_cast<basic_any &>(from).instance, nullptr));
  26016. case operation::DTOR:
  26017. if(from.mode == policy::OWNER) {
  26018. if constexpr(in_situ<Type>) {
  26019. instance->~Type();
  26020. } else if constexpr(std::is_array_v<Type>) {
  26021. delete[] instance;
  26022. } else {
  26023. delete instance;
  26024. }
  26025. }
  26026. break;
  26027. case operation::COMP:
  26028. return compare<Type>(instance, (*static_cast<const basic_any **>(to))->data()) ? to : nullptr;
  26029. case operation::ADDR:
  26030. if(from.mode == policy::CREF) {
  26031. return nullptr;
  26032. }
  26033. [[fallthrough]];
  26034. case operation::CADDR:
  26035. return instance;
  26036. case operation::TYPE:
  26037. *static_cast<type_info *>(to) = type_id<Type>();
  26038. break;
  26039. }
  26040. }
  26041. return nullptr;
  26042. }
  26043. template<typename Type, typename... Args>
  26044. void initialize([[maybe_unused]] Args &&... args) {
  26045. if constexpr(!std::is_void_v<Type>) {
  26046. if constexpr(std::is_lvalue_reference_v<Type>) {
  26047. static_assert(sizeof...(Args) == 1u && (std::is_lvalue_reference_v<Args> && ...), "Invalid arguments");
  26048. instance = (std::addressof(args), ...);
  26049. } else if constexpr(in_situ<Type>) {
  26050. if constexpr(sizeof...(Args) != 0u && std::is_aggregate_v<Type>) {
  26051. new (&storage) Type{std::forward<Args>(args)...};
  26052. } else {
  26053. new (&storage) Type(std::forward<Args>(args)...);
  26054. }
  26055. } else {
  26056. if constexpr(sizeof...(Args) != 0u && std::is_aggregate_v<Type>) {
  26057. instance = new Type{std::forward<Args>(args)...};
  26058. } else {
  26059. instance = new Type(std::forward<Args>(args)...);
  26060. }
  26061. }
  26062. }
  26063. }
  26064. basic_any(const basic_any &other, const policy pol) ENTT_NOEXCEPT
  26065. : instance{other.data()},
  26066. vtable{other.vtable},
  26067. mode{pol}
  26068. {}
  26069. public:
  26070. /*! @brief Size of the internal storage. */
  26071. static constexpr auto length = Len;
  26072. /*! @brief Alignment requirement. */
  26073. static constexpr auto alignment = Align;
  26074. /*! @brief Default constructor. */
  26075. basic_any() ENTT_NOEXCEPT
  26076. : instance{},
  26077. vtable{&basic_vtable<void>},
  26078. mode{policy::OWNER}
  26079. {}
  26080. /**
  26081. * @brief Constructs a wrapper by directly initializing the new object.
  26082. * @tparam Type Type of object to use to initialize the wrapper.
  26083. * @tparam Args Types of arguments to use to construct the new instance.
  26084. * @param args Parameters to use to construct the instance.
  26085. */
  26086. template<typename Type, typename... Args>
  26087. explicit basic_any(std::in_place_type_t<Type>, Args &&... args)
  26088. : instance{},
  26089. vtable{&basic_vtable<std::remove_const_t<std::remove_reference_t<Type>>>},
  26090. mode{type_to_policy<Type>()}
  26091. {
  26092. initialize<Type>(std::forward<Args>(args)...);
  26093. }
  26094. /**
  26095. * @brief Constructs a wrapper that holds an unmanaged object.
  26096. * @tparam Type Type of object to use to initialize the wrapper.
  26097. * @param value An instance of an object to use to initialize the wrapper.
  26098. */
  26099. template<typename Type>
  26100. basic_any(std::reference_wrapper<Type> value) ENTT_NOEXCEPT
  26101. : basic_any{}
  26102. {
  26103. // invokes deprecated assignment operator (and avoids issues with vs2017)
  26104. *this = value;
  26105. }
  26106. /**
  26107. * @brief Constructs a wrapper from a given value.
  26108. * @tparam Type Type of object to use to initialize the wrapper.
  26109. * @param value An instance of an object to use to initialize the wrapper.
  26110. */
  26111. template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::decay_t<Type>, basic_any>>>
  26112. basic_any(Type &&value)
  26113. : instance{},
  26114. vtable{&basic_vtable<std::decay_t<Type>>},
  26115. mode{policy::OWNER}
  26116. {
  26117. initialize<std::decay_t<Type>>(std::forward<Type>(value));
  26118. }
  26119. /**
  26120. * @brief Copy constructor.
  26121. * @param other The instance to copy from.
  26122. */
  26123. basic_any(const basic_any &other)
  26124. : instance{},
  26125. vtable{&basic_vtable<void>},
  26126. mode{policy::OWNER}
  26127. {
  26128. other.vtable(operation::COPY, other, this);
  26129. }
  26130. /**
  26131. * @brief Move constructor.
  26132. * @param other The instance to move from.
  26133. */
  26134. basic_any(basic_any &&other) ENTT_NOEXCEPT
  26135. : instance{},
  26136. vtable{other.vtable},
  26137. mode{other.mode}
  26138. {
  26139. vtable(operation::MOVE, other, this);
  26140. }
  26141. /*! @brief Frees the internal storage, whatever it means. */
  26142. ~basic_any() {
  26143. vtable(operation::DTOR, *this, nullptr);
  26144. }
  26145. /**
  26146. * @brief Copy assignment operator.
  26147. * @param other The instance to copy from.
  26148. * @return This any object.
  26149. */
  26150. basic_any & operator=(const basic_any &other) {
  26151. reset();
  26152. other.vtable(operation::COPY, other, this);
  26153. return *this;
  26154. }
  26155. /**
  26156. * @brief Move assignment operator.
  26157. * @param other The instance to move from.
  26158. * @return This any object.
  26159. */
  26160. basic_any & operator=(basic_any &&other) ENTT_NOEXCEPT {
  26161. std::exchange(vtable, other.vtable)(operation::DTOR, *this, nullptr);
  26162. other.vtable(operation::MOVE, other, this);
  26163. mode = other.mode;
  26164. return *this;
  26165. }
  26166. /**
  26167. * @brief Value assignment operator.
  26168. * @tparam Type Type of object to use to initialize the wrapper.
  26169. * @param value An instance of an object to use to initialize the wrapper.
  26170. * @return This any object.
  26171. */
  26172. template<typename Type>
  26173. [[deprecated("Use std::in_place_type<T &>, entt::make_any<T &>, emplace<Type &> or forward_as_any instead")]]
  26174. basic_any & operator=(std::reference_wrapper<Type> value) ENTT_NOEXCEPT {
  26175. emplace<Type &>(value.get());
  26176. return *this;
  26177. }
  26178. /**
  26179. * @brief Value assignment operator.
  26180. * @tparam Type Type of object to use to initialize the wrapper.
  26181. * @param value An instance of an object to use to initialize the wrapper.
  26182. * @return This any object.
  26183. */
  26184. template<typename Type>
  26185. std::enable_if_t<!std::is_same_v<std::decay_t<Type>, basic_any>, basic_any &>
  26186. operator=(Type &&value) {
  26187. emplace<std::decay_t<Type>>(std::forward<Type>(value));
  26188. return *this;
  26189. }
  26190. /**
  26191. * @brief Returns the type of the contained object.
  26192. * @return The type of the contained object, if any.
  26193. */
  26194. [[nodiscard]] type_info type() const ENTT_NOEXCEPT {
  26195. type_info info{};
  26196. vtable(operation::TYPE, *this, &info);
  26197. return info;
  26198. }
  26199. /**
  26200. * @brief Returns an opaque pointer to the contained instance.
  26201. * @return An opaque pointer the contained instance, if any.
  26202. */
  26203. [[nodiscard]] const void * data() const ENTT_NOEXCEPT {
  26204. return vtable(operation::CADDR, *this, nullptr);
  26205. }
  26206. /*! @copydoc data */
  26207. [[nodiscard]] void * data() ENTT_NOEXCEPT {
  26208. return const_cast<void *>(vtable(operation::ADDR, *this, nullptr));
  26209. }
  26210. /**
  26211. * @brief Replaces the contained object by creating a new instance directly.
  26212. * @tparam Type Type of object to use to initialize the wrapper.
  26213. * @tparam Args Types of arguments to use to construct the new instance.
  26214. * @param args Parameters to use to construct the instance.
  26215. */
  26216. template<typename Type, typename... Args>
  26217. void emplace(Args &&... args) {
  26218. std::exchange(vtable, &basic_vtable<std::remove_const_t<std::remove_reference_t<Type>>>)(operation::DTOR, *this, nullptr);
  26219. mode = type_to_policy<Type>();
  26220. initialize<Type>(std::forward<Args>(args)...);
  26221. }
  26222. /*! @brief Destroys contained object */
  26223. void reset() {
  26224. std::exchange(vtable, &basic_vtable<void>)(operation::DTOR, *this, nullptr);
  26225. mode = policy::OWNER;
  26226. }
  26227. /**
  26228. * @brief Returns false if a wrapper is empty, true otherwise.
  26229. * @return False if the wrapper is empty, true otherwise.
  26230. */
  26231. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  26232. return !(vtable(operation::CADDR, *this, nullptr) == nullptr);
  26233. }
  26234. /**
  26235. * @brief Checks if two wrappers differ in their content.
  26236. * @param other Wrapper with which to compare.
  26237. * @return False if the two objects differ in their content, true otherwise.
  26238. */
  26239. bool operator==(const basic_any &other) const ENTT_NOEXCEPT {
  26240. const basic_any *trampoline = &other;
  26241. return type() == other.type() && (vtable(operation::COMP, *this, &trampoline) || !other.data());
  26242. }
  26243. /**
  26244. * @brief Aliasing constructor.
  26245. * @return A wrapper that shares a reference to an unmanaged object.
  26246. */
  26247. [[nodiscard]] basic_any as_ref() ENTT_NOEXCEPT {
  26248. return basic_any{*this, (mode == policy::CREF ? policy::CREF : policy::REF)};
  26249. }
  26250. /*! @copydoc as_ref */
  26251. [[nodiscard]] basic_any as_ref() const ENTT_NOEXCEPT {
  26252. return basic_any{*this, policy::CREF};
  26253. }
  26254. /**
  26255. * @brief Returns true if a wrapper owns its object, false otherwise.
  26256. * @return True if the wrapper owns its object, false otherwise.
  26257. */
  26258. [[nodiscard]] bool owner() const ENTT_NOEXCEPT {
  26259. return (mode == policy::OWNER);
  26260. }
  26261. private:
  26262. union { const void *instance; storage_type storage; };
  26263. vtable_type *vtable;
  26264. policy mode;
  26265. };
  26266. /**
  26267. * @brief Checks if two wrappers differ in their content.
  26268. * @tparam Len Size of the storage reserved for the small buffer optimization.
  26269. * @tparam Align Alignment requirement.
  26270. * @param lhs A wrapper, either empty or not.
  26271. * @param rhs A wrapper, either empty or not.
  26272. * @return True if the two wrappers differ in their content, false otherwise.
  26273. */
  26274. template<std::size_t Len, std::size_t Align>
  26275. [[nodiscard]] inline bool operator!=(const basic_any<Len, Align> &lhs, const basic_any<Len, Align> &rhs) ENTT_NOEXCEPT {
  26276. return !(lhs == rhs);
  26277. }
  26278. /**
  26279. * @brief Performs type-safe access to the contained object.
  26280. * @tparam Type Type to which conversion is required.
  26281. * @tparam Len Size of the storage reserved for the small buffer optimization.
  26282. * @tparam Align Alignment requirement.
  26283. * @param data Target any object.
  26284. * @return The element converted to the requested type.
  26285. */
  26286. template<typename Type, std::size_t Len, std::size_t Align>
  26287. Type any_cast(const basic_any<Len, Align> &data) ENTT_NOEXCEPT {
  26288. const auto * const instance = any_cast<std::remove_reference_t<Type>>(&data);
  26289. ENTT_ASSERT(instance, "Invalid instance");
  26290. return static_cast<Type>(*instance);
  26291. }
  26292. /*! @copydoc any_cast */
  26293. template<typename Type, std::size_t Len, std::size_t Align>
  26294. Type any_cast(basic_any<Len, Align> &data) ENTT_NOEXCEPT {
  26295. // forces const on non-reference types to make them work also with wrappers for const references
  26296. auto * const instance = any_cast<std::remove_reference_t<const Type>>(&data);
  26297. ENTT_ASSERT(instance, "Invalid instance");
  26298. return static_cast<Type>(*instance);
  26299. }
  26300. /*! @copydoc any_cast */
  26301. template<typename Type, std::size_t Len, std::size_t Align>
  26302. Type any_cast(basic_any<Len, Align> &&data) ENTT_NOEXCEPT {
  26303. // forces const on non-reference types to make them work also with wrappers for const references
  26304. auto * const instance = any_cast<std::remove_reference_t<const Type>>(&data);
  26305. ENTT_ASSERT(instance, "Invalid instance");
  26306. return static_cast<Type>(std::move(*instance));
  26307. }
  26308. /*! @copydoc any_cast */
  26309. template<typename Type, std::size_t Len, std::size_t Align>
  26310. const Type * any_cast(const basic_any<Len, Align> *data) ENTT_NOEXCEPT {
  26311. return (data->type() == type_id<Type>() ? static_cast<const Type *>(data->data()) : nullptr);
  26312. }
  26313. /*! @copydoc any_cast */
  26314. template<typename Type, std::size_t Len, std::size_t Align>
  26315. Type * any_cast(basic_any<Len, Align> *data) ENTT_NOEXCEPT {
  26316. // last attempt to make wrappers for const references return their values
  26317. return (data->type() == type_id<Type>() ? static_cast<Type *>(static_cast<constness_as_t<basic_any<Len, Align>, Type> *>(data)->data()) : nullptr);
  26318. }
  26319. /**
  26320. * @brief Constructs a wrapper from a given type, passing it all arguments.
  26321. * @tparam Type Type of object to use to initialize the wrapper.
  26322. * @tparam Len Size of the storage reserved for the small buffer optimization.
  26323. * @tparam Align Optional alignment requirement.
  26324. * @tparam Args Types of arguments to use to construct the new instance.
  26325. * @param args Parameters to use to construct the instance.
  26326. * @return A properly initialized wrapper for an object of the given type.
  26327. */
  26328. template<typename Type, std::size_t Len = basic_any<>::length, std::size_t Align = basic_any<Len>::alignment, typename... Args>
  26329. basic_any<Len, Align> make_any(Args &&... args) {
  26330. return basic_any<Len, Align>{std::in_place_type<Type>, std::forward<Args>(args)...};
  26331. }
  26332. /**
  26333. * @brief Forwards its argument and avoids copies for lvalue references.
  26334. * @tparam Len Size of the storage reserved for the small buffer optimization.
  26335. * @tparam Align Optional alignment requirement.
  26336. * @tparam Type Type of argument to use to construct the new instance.
  26337. * @param value Parameter to use to construct the instance.
  26338. * @return A properly initialized and not necessarily owning wrapper.
  26339. */
  26340. template<std::size_t Len = basic_any<>::length, std::size_t Align = basic_any<Len>::alignment, typename Type>
  26341. basic_any<Len, Align> forward_as_any(Type &&value) {
  26342. return basic_any<Len, Align>{std::in_place_type<std::conditional_t<std::is_rvalue_reference_v<Type>, std::decay_t<Type>, Type>>, std::forward<Type>(value)};
  26343. }
  26344. }
  26345. #endif
  26346. // #include "../core/fwd.hpp"
  26347. // #include "../core/utility.hpp"
  26348. #ifndef ENTT_CORE_UTILITY_HPP
  26349. #define ENTT_CORE_UTILITY_HPP
  26350. #include <utility>
  26351. // #include "../config/config.h"
  26352. namespace entt {
  26353. /*! @brief Identity function object (waiting for C++20). */
  26354. struct identity {
  26355. /**
  26356. * @brief Returns its argument unchanged.
  26357. * @tparam Type Type of the argument.
  26358. * @param value The actual argument.
  26359. * @return The submitted value as-is.
  26360. */
  26361. template<class Type>
  26362. [[nodiscard]] constexpr Type && operator()(Type &&value) const ENTT_NOEXCEPT {
  26363. return std::forward<Type>(value);
  26364. }
  26365. };
  26366. /**
  26367. * @brief Constant utility to disambiguate overloaded members of a class.
  26368. * @tparam Type Type of the desired overload.
  26369. * @tparam Class Type of class to which the member belongs.
  26370. * @param member A valid pointer to a member.
  26371. * @return Pointer to the member.
  26372. */
  26373. template<typename Type, typename Class>
  26374. [[nodiscard]] constexpr auto overload(Type Class:: *member) ENTT_NOEXCEPT { return member; }
  26375. /**
  26376. * @brief Constant utility to disambiguate overloaded functions.
  26377. * @tparam Func Function type of the desired overload.
  26378. * @param func A valid pointer to a function.
  26379. * @return Pointer to the function.
  26380. */
  26381. template<typename Func>
  26382. [[nodiscard]] constexpr auto overload(Func *func) ENTT_NOEXCEPT { return func; }
  26383. /**
  26384. * @brief Helper type for visitors.
  26385. * @tparam Func Types of function objects.
  26386. */
  26387. template<class... Func>
  26388. struct overloaded: Func... {
  26389. using Func::operator()...;
  26390. };
  26391. /**
  26392. * @brief Deduction guide.
  26393. * @tparam Func Types of function objects.
  26394. */
  26395. template<class... Func>
  26396. overloaded(Func...)
  26397. -> overloaded<Func...>;
  26398. /**
  26399. * @brief Basic implementation of a y-combinator.
  26400. * @tparam Func Type of a potentially recursive function.
  26401. */
  26402. template<class Func>
  26403. struct y_combinator {
  26404. /**
  26405. * @brief Constructs a y-combinator from a given function.
  26406. * @param recursive A potentially recursive function.
  26407. */
  26408. y_combinator(Func recursive):
  26409. func{std::move(recursive)}
  26410. {}
  26411. /**
  26412. * @brief Invokes a y-combinator and therefore its underlying function.
  26413. * @tparam Args Types of arguments to use to invoke the underlying function.
  26414. * @param args Parameters to use to invoke the underlying function.
  26415. * @return Return value of the underlying function, if any.
  26416. */
  26417. template <class... Args>
  26418. decltype(auto) operator()(Args &&... args) const {
  26419. return func(*this, std::forward<Args>(args)...);
  26420. }
  26421. /*! @copydoc operator()() */
  26422. template <class... Args>
  26423. decltype(auto) operator()(Args &&... args) {
  26424. return func(*this, std::forward<Args>(args)...);
  26425. }
  26426. private:
  26427. Func func;
  26428. };
  26429. }
  26430. #endif
  26431. // #include "../core/type_info.hpp"
  26432. // #include "../core/type_traits.hpp"
  26433. // #include "adl_pointer.hpp"
  26434. #ifndef ENTT_META_ADL_POINTER_HPP
  26435. #define ENTT_META_ADL_POINTER_HPP
  26436. namespace entt {
  26437. /**
  26438. * @brief ADL based lookup function for dereferencing meta pointer-like types.
  26439. * @tparam Type Element type.
  26440. * @param value A pointer-like object.
  26441. * @return The value returned from the dereferenced pointer.
  26442. */
  26443. template<typename Type>
  26444. decltype(auto) dereference_meta_pointer_like(const Type &value) {
  26445. return *value;
  26446. }
  26447. /**
  26448. * @brief Fake ADL based lookup function for meta pointer-like types.
  26449. * @tparam Type Element type.
  26450. */
  26451. template<typename Type>
  26452. struct adl_meta_pointer_like {
  26453. /**
  26454. * @brief Uses the default ADL based lookup method to resolve the call.
  26455. * @param value A pointer-like object.
  26456. * @return The value returned from the dereferenced pointer.
  26457. */
  26458. static decltype(auto) dereference(const Type &value) {
  26459. return dereference_meta_pointer_like(value);
  26460. }
  26461. };
  26462. }
  26463. #endif
  26464. // #include "ctx.hpp"
  26465. #ifndef ENTT_META_CTX_HPP
  26466. #define ENTT_META_CTX_HPP
  26467. // #include "../core/attribute.h"
  26468. // #include "../config/config.h"
  26469. namespace entt {
  26470. /**
  26471. * @cond TURN_OFF_DOXYGEN
  26472. * Internal details not to be documented.
  26473. */
  26474. namespace internal {
  26475. struct meta_type_node;
  26476. struct ENTT_API meta_context {
  26477. // we could use the lines below but VS2017 returns with an ICE if combined with ENTT_API despite the code being valid C++
  26478. // inline static meta_type_node *local = nullptr;
  26479. // inline static meta_type_node **global = &local;
  26480. [[nodiscard]] static meta_type_node * & local() ENTT_NOEXCEPT {
  26481. static meta_type_node *chain = nullptr;
  26482. return chain;
  26483. }
  26484. [[nodiscard]] static meta_type_node ** & global() ENTT_NOEXCEPT {
  26485. static meta_type_node **chain = &local();
  26486. return chain;
  26487. }
  26488. };
  26489. }
  26490. /**
  26491. * Internal details not to be documented.
  26492. * @endcond
  26493. */
  26494. /*! @brief Opaque container for a meta context. */
  26495. struct meta_ctx {
  26496. /**
  26497. * @brief Binds the meta system to a given context.
  26498. * @param other A valid context to which to bind.
  26499. */
  26500. static void bind(meta_ctx other) ENTT_NOEXCEPT {
  26501. internal::meta_context::global() = other.ctx;
  26502. }
  26503. private:
  26504. internal::meta_type_node **ctx{&internal::meta_context::local()};
  26505. };
  26506. }
  26507. #endif
  26508. // #include "node.hpp"
  26509. #ifndef ENTT_META_NODE_HPP
  26510. #define ENTT_META_NODE_HPP
  26511. #include <cstddef>
  26512. #include <type_traits>
  26513. #include <utility>
  26514. // #include "../config/config.h"
  26515. // #include "../core/attribute.h"
  26516. // #include "../core/fwd.hpp"
  26517. // #include "../core/type_info.hpp"
  26518. // #include "../core/type_traits.hpp"
  26519. // #include "type_traits.hpp"
  26520. namespace entt {
  26521. class meta_any;
  26522. class meta_type;
  26523. struct meta_handle;
  26524. /**
  26525. * @cond TURN_OFF_DOXYGEN
  26526. * Internal details not to be documented.
  26527. */
  26528. namespace internal {
  26529. struct meta_type_node;
  26530. struct meta_prop_node {
  26531. meta_prop_node * next;
  26532. const meta_any &id;
  26533. meta_any &value;
  26534. };
  26535. struct meta_base_node {
  26536. meta_type_node * const parent;
  26537. meta_base_node * next;
  26538. meta_type_node *(* const type)() ENTT_NOEXCEPT;
  26539. const void *(* const cast)(const void *) ENTT_NOEXCEPT;
  26540. };
  26541. struct meta_conv_node {
  26542. meta_type_node * const parent;
  26543. meta_conv_node * next;
  26544. meta_type_node *(* const type)() ENTT_NOEXCEPT;
  26545. meta_any(* const conv)(const void *);
  26546. };
  26547. struct meta_ctor_node {
  26548. using size_type = std::size_t;
  26549. meta_type_node * const parent;
  26550. meta_ctor_node * next;
  26551. meta_prop_node * prop;
  26552. const size_type arity;
  26553. meta_type(* const arg)(const size_type) ENTT_NOEXCEPT;
  26554. meta_any(* const invoke)(meta_any * const);
  26555. };
  26556. struct meta_data_node {
  26557. id_type id;
  26558. meta_type_node * const parent;
  26559. meta_data_node * next;
  26560. meta_prop_node * prop;
  26561. const bool is_const;
  26562. const bool is_static;
  26563. meta_type_node *(* const type)() ENTT_NOEXCEPT;
  26564. bool(* const set)(meta_handle, meta_any);
  26565. meta_any(* const get)(meta_handle);
  26566. };
  26567. struct meta_func_node {
  26568. using size_type = std::size_t;
  26569. id_type id;
  26570. meta_type_node * const parent;
  26571. meta_func_node * next;
  26572. meta_prop_node * prop;
  26573. const size_type arity;
  26574. const bool is_const;
  26575. const bool is_static;
  26576. meta_type_node *(* const ret)() ENTT_NOEXCEPT;
  26577. meta_type(* const arg)(const size_type) ENTT_NOEXCEPT;
  26578. meta_any(* const invoke)(meta_handle, meta_any *);
  26579. };
  26580. struct meta_template_info {
  26581. using size_type = std::size_t;
  26582. const bool is_template_specialization;
  26583. const size_type arity;
  26584. meta_type_node *(* const type)() ENTT_NOEXCEPT;
  26585. meta_type_node *(* const arg)(const size_type) ENTT_NOEXCEPT;
  26586. };
  26587. struct meta_type_node {
  26588. using size_type = std::size_t;
  26589. const type_info info;
  26590. id_type id;
  26591. meta_type_node * next;
  26592. meta_prop_node * prop;
  26593. const size_type size_of;
  26594. const bool is_void;
  26595. const bool is_integral;
  26596. const bool is_floating_point;
  26597. const bool is_array;
  26598. const bool is_enum;
  26599. const bool is_union;
  26600. const bool is_class;
  26601. const bool is_pointer;
  26602. const bool is_function_pointer;
  26603. const bool is_member_object_pointer;
  26604. const bool is_member_function_pointer;
  26605. const bool is_pointer_like;
  26606. const bool is_sequence_container;
  26607. const bool is_associative_container;
  26608. const meta_template_info template_info;
  26609. const size_type rank;
  26610. size_type(* const extent)(const size_type) ENTT_NOEXCEPT ;
  26611. meta_type_node *(* const remove_pointer)() ENTT_NOEXCEPT;
  26612. meta_type_node *(* const remove_extent)() ENTT_NOEXCEPT;
  26613. meta_ctor_node * const def_ctor;
  26614. meta_ctor_node *ctor{nullptr};
  26615. meta_base_node *base{nullptr};
  26616. meta_conv_node *conv{nullptr};
  26617. meta_data_node *data{nullptr};
  26618. meta_func_node *func{nullptr};
  26619. void(* dtor)(void *){nullptr};
  26620. };
  26621. template<auto Member, typename Op, typename Node>
  26622. auto meta_visit(const Op &op, const Node *node)
  26623. -> std::decay_t<decltype(node->*Member)> {
  26624. for(auto *curr = node->*Member; curr; curr = curr->next) {
  26625. if(op(curr)) {
  26626. return curr;
  26627. }
  26628. }
  26629. if constexpr(std::is_same_v<Node, meta_type_node>) {
  26630. for(auto *curr = node->base; curr; curr = curr->next) {
  26631. if(auto *ret = meta_visit<Member>(op, curr->type()); ret) {
  26632. return ret;
  26633. }
  26634. }
  26635. }
  26636. return nullptr;
  26637. }
  26638. template<typename... Args>
  26639. meta_type_node * meta_arg_node(type_list<Args...>, const std::size_t index) ENTT_NOEXCEPT;
  26640. template<typename Type>
  26641. class ENTT_API meta_node {
  26642. static_assert(std::is_same_v<Type, std::remove_cv_t<std::remove_reference_t<Type>>>, "Invalid type");
  26643. template<std::size_t... Index>
  26644. [[nodiscard]] static auto extent(const meta_type_node::size_type dim, std::index_sequence<Index...>) ENTT_NOEXCEPT {
  26645. meta_type_node::size_type ext{};
  26646. ((ext = (dim == Index ? std::extent_v<Type, Index> : ext)), ...);
  26647. return ext;
  26648. }
  26649. [[nodiscard]] static meta_ctor_node * meta_default_constructor([[maybe_unused]] meta_type_node *type) ENTT_NOEXCEPT {
  26650. if constexpr(std::is_default_constructible_v<Type>) {
  26651. static meta_ctor_node node{
  26652. type,
  26653. nullptr,
  26654. nullptr,
  26655. 0u,
  26656. nullptr,
  26657. [](meta_any * const) { return meta_any{std::in_place_type<Type>}; }
  26658. };
  26659. return &node;
  26660. } else {
  26661. return nullptr;
  26662. }
  26663. }
  26664. [[nodiscard]] static meta_template_info meta_template_descriptor() ENTT_NOEXCEPT {
  26665. if constexpr(is_complete_v<meta_template_traits<Type>>) {
  26666. return {
  26667. true,
  26668. meta_template_traits<Type>::args_type::size,
  26669. &meta_node<typename meta_template_traits<Type>::class_type>::resolve,
  26670. [](const std::size_t index) ENTT_NOEXCEPT {
  26671. return meta_arg_node(typename meta_template_traits<Type>::args_type{}, index);
  26672. }
  26673. };
  26674. } else {
  26675. return { false, 0u, nullptr, nullptr };
  26676. }
  26677. }
  26678. public:
  26679. [[nodiscard]] static meta_type_node * resolve() ENTT_NOEXCEPT {
  26680. static meta_type_node node{
  26681. type_id<Type>(),
  26682. {},
  26683. nullptr,
  26684. nullptr,
  26685. size_of_v<Type>,
  26686. std::is_void_v<Type>,
  26687. std::is_integral_v<Type>,
  26688. std::is_floating_point_v<Type>,
  26689. std::is_array_v<Type>,
  26690. std::is_enum_v<Type>,
  26691. std::is_union_v<Type>,
  26692. std::is_class_v<Type>,
  26693. std::is_pointer_v<Type>,
  26694. std::is_pointer_v<Type> && std::is_function_v<std::remove_pointer_t<Type>>,
  26695. std::is_member_object_pointer_v<Type>,
  26696. std::is_member_function_pointer_v<Type>,
  26697. is_meta_pointer_like_v<Type>,
  26698. is_complete_v<meta_sequence_container_traits<Type>>,
  26699. is_complete_v<meta_associative_container_traits<Type>>,
  26700. meta_template_descriptor(),
  26701. std::rank_v<Type>,
  26702. [](meta_type_node::size_type dim) ENTT_NOEXCEPT { return extent(dim, std::make_index_sequence<std::rank_v<Type>>{}); },
  26703. &meta_node<std::remove_cv_t<std::remove_reference_t<std::remove_pointer_t<Type>>>>::resolve,
  26704. &meta_node<std::remove_cv_t<std::remove_reference_t<std::remove_extent_t<Type>>>>::resolve,
  26705. meta_default_constructor(&node),
  26706. meta_default_constructor(&node)
  26707. };
  26708. return &node;
  26709. }
  26710. };
  26711. template<typename Type>
  26712. struct meta_info: meta_node<std::remove_cv_t<std::remove_reference_t<Type>>> {};
  26713. template<typename... Args>
  26714. meta_type_node * meta_arg_node(type_list<Args...>, const std::size_t index) ENTT_NOEXCEPT {
  26715. meta_type_node *args[sizeof...(Args) + 1u]{nullptr, internal::meta_info<Args>::resolve()...};
  26716. return args[index + 1u];
  26717. }
  26718. }
  26719. /**
  26720. * Internal details not to be documented.
  26721. * @endcond
  26722. */
  26723. }
  26724. #endif
  26725. // #include "range.hpp"
  26726. #ifndef ENTT_META_RANGE_HPP
  26727. #define ENTT_META_RANGE_HPP
  26728. #include <cstddef>
  26729. #include <iterator>
  26730. namespace entt {
  26731. /**
  26732. * @brief Iterable range to use to iterate all types of meta objects.
  26733. * @tparam Type Type of meta objects returned.
  26734. * @tparam Node Type of meta nodes iterated.
  26735. */
  26736. template<typename Type, typename Node = typename Type::node_type>
  26737. class meta_range {
  26738. struct range_iterator {
  26739. using difference_type = std::ptrdiff_t;
  26740. using value_type = Type;
  26741. using pointer = void;
  26742. using reference = value_type;
  26743. using iterator_category = std::input_iterator_tag;
  26744. using node_type = Node;
  26745. range_iterator() ENTT_NOEXCEPT = default;
  26746. range_iterator(node_type *head) ENTT_NOEXCEPT
  26747. : it{head}
  26748. {}
  26749. range_iterator & operator++() ENTT_NOEXCEPT {
  26750. return (it = it->next), *this;
  26751. }
  26752. range_iterator operator++(int) ENTT_NOEXCEPT {
  26753. range_iterator orig = *this;
  26754. return ++(*this), orig;
  26755. }
  26756. [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
  26757. return it;
  26758. }
  26759. [[nodiscard]] bool operator==(const range_iterator &other) const ENTT_NOEXCEPT {
  26760. return other.it == it;
  26761. }
  26762. [[nodiscard]] bool operator!=(const range_iterator &other) const ENTT_NOEXCEPT {
  26763. return !(*this == other);
  26764. }
  26765. private:
  26766. node_type *it{};
  26767. };
  26768. public:
  26769. /*! @brief Node type. */
  26770. using node_type = Node;
  26771. /*! @brief Input iterator type. */
  26772. using iterator = range_iterator;
  26773. /*! @brief Default constructor. */
  26774. meta_range() ENTT_NOEXCEPT = default;
  26775. /**
  26776. * @brief Constructs a meta range from a given node.
  26777. * @param head The underlying node with which to construct the range.
  26778. */
  26779. meta_range(node_type *head)
  26780. : node{head}
  26781. {}
  26782. /**
  26783. * @brief Returns an iterator to the beginning.
  26784. * @return An iterator to the first meta object of the range.
  26785. */
  26786. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  26787. return iterator{node};
  26788. }
  26789. /**
  26790. * @brief Returns an iterator to the end.
  26791. * @return An iterator to the element following the last meta object of the
  26792. * range.
  26793. */
  26794. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  26795. return iterator{};
  26796. }
  26797. private:
  26798. node_type *node{nullptr};
  26799. };
  26800. }
  26801. #endif
  26802. // #include "type_traits.hpp"
  26803. namespace entt {
  26804. class meta_any;
  26805. class meta_type;
  26806. /*! @brief Proxy object for sequence containers. */
  26807. class meta_sequence_container {
  26808. template<typename>
  26809. struct meta_sequence_container_proxy;
  26810. class meta_iterator;
  26811. public:
  26812. /*! @brief Unsigned integer type. */
  26813. using size_type = std::size_t;
  26814. /*! @brief Meta iterator type. */
  26815. using iterator = meta_iterator;
  26816. /*! @brief Default constructor. */
  26817. meta_sequence_container() ENTT_NOEXCEPT = default;
  26818. /**
  26819. * @brief Construct a proxy object for sequence containers.
  26820. * @tparam Type Type of container to wrap.
  26821. * @param instance The container to wrap.
  26822. */
  26823. template<typename Type>
  26824. meta_sequence_container(std::in_place_type_t<Type>, any instance) ENTT_NOEXCEPT
  26825. : value_type_fn{&meta_sequence_container_proxy<Type>::value_type},
  26826. size_fn{&meta_sequence_container_proxy<Type>::size},
  26827. resize_fn{&meta_sequence_container_proxy<Type>::resize},
  26828. clear_fn{&meta_sequence_container_proxy<Type>::clear},
  26829. begin_fn{&meta_sequence_container_proxy<Type>::begin},
  26830. end_fn{&meta_sequence_container_proxy<Type>::end},
  26831. insert_fn{&meta_sequence_container_proxy<Type>::insert},
  26832. erase_fn{&meta_sequence_container_proxy<Type>::erase},
  26833. get_fn{&meta_sequence_container_proxy<Type>::get},
  26834. storage{std::move(instance)}
  26835. {}
  26836. [[nodiscard]] inline meta_type value_type() const ENTT_NOEXCEPT;
  26837. [[nodiscard]] inline size_type size() const ENTT_NOEXCEPT;
  26838. inline bool resize(size_type);
  26839. inline bool clear();
  26840. [[nodiscard]] inline iterator begin();
  26841. [[nodiscard]] inline iterator end();
  26842. inline std::pair<iterator, bool> insert(iterator, meta_any);
  26843. inline std::pair<iterator, bool> erase(iterator);
  26844. [[nodiscard]] inline meta_any operator[](size_type);
  26845. [[nodiscard]] inline explicit operator bool() const ENTT_NOEXCEPT;
  26846. private:
  26847. meta_type(* value_type_fn)() ENTT_NOEXCEPT = nullptr;
  26848. size_type(* size_fn)(const any &) ENTT_NOEXCEPT = nullptr;
  26849. bool(* resize_fn)(any &, size_type) = nullptr;
  26850. bool(* clear_fn)(any &) = nullptr;
  26851. iterator(* begin_fn)(any &) = nullptr;
  26852. iterator(* end_fn)(any &) = nullptr;
  26853. std::pair<iterator, bool>(* insert_fn)(any &, iterator, meta_any &) = nullptr;
  26854. std::pair<iterator, bool>(* erase_fn)(any &, iterator) = nullptr;
  26855. meta_any(* get_fn)(any &, size_type) = nullptr;
  26856. any storage{};
  26857. };
  26858. /*! @brief Proxy object for associative containers. */
  26859. class meta_associative_container {
  26860. template<typename>
  26861. struct meta_associative_container_proxy;
  26862. class meta_iterator;
  26863. public:
  26864. /*! @brief Unsigned integer type. */
  26865. using size_type = std::size_t;
  26866. /*! @brief Meta iterator type. */
  26867. using iterator = meta_iterator;
  26868. /*! @brief Default constructor. */
  26869. meta_associative_container() ENTT_NOEXCEPT = default;
  26870. /**
  26871. * @brief Construct a proxy object for associative containers.
  26872. * @tparam Type Type of container to wrap.
  26873. * @param instance The container to wrap.
  26874. */
  26875. template<typename Type>
  26876. meta_associative_container(std::in_place_type_t<Type>, any instance) ENTT_NOEXCEPT
  26877. : key_only_container{is_key_only_meta_associative_container_v<Type>},
  26878. key_type_fn{&meta_associative_container_proxy<Type>::key_type},
  26879. mapped_type_fn{&meta_associative_container_proxy<Type>::mapped_type},
  26880. value_type_fn{&meta_associative_container_proxy<Type>::value_type},
  26881. size_fn{&meta_associative_container_proxy<Type>::size},
  26882. clear_fn{&meta_associative_container_proxy<Type>::clear},
  26883. begin_fn{&meta_associative_container_proxy<Type>::begin},
  26884. end_fn{&meta_associative_container_proxy<Type>::end},
  26885. insert_fn{&meta_associative_container_proxy<Type>::insert},
  26886. erase_fn{&meta_associative_container_proxy<Type>::erase},
  26887. find_fn{&meta_associative_container_proxy<Type>::find},
  26888. storage{std::move(instance)}
  26889. {}
  26890. [[nodiscard]] inline bool key_only() const ENTT_NOEXCEPT;
  26891. [[nodiscard]] inline meta_type key_type() const ENTT_NOEXCEPT;
  26892. [[nodiscard]] inline meta_type mapped_type() const ENTT_NOEXCEPT;
  26893. [[nodiscard]] inline meta_type value_type() const ENTT_NOEXCEPT;
  26894. [[nodiscard]] inline size_type size() const ENTT_NOEXCEPT;
  26895. inline bool clear();
  26896. [[nodiscard]] inline iterator begin();
  26897. [[nodiscard]] inline iterator end();
  26898. inline bool insert(meta_any, meta_any);
  26899. inline bool erase(meta_any);
  26900. [[nodiscard]] inline iterator find(meta_any);
  26901. [[nodiscard]] inline explicit operator bool() const ENTT_NOEXCEPT;
  26902. private:
  26903. bool key_only_container{};
  26904. meta_type(* key_type_fn)() ENTT_NOEXCEPT = nullptr;
  26905. meta_type(* mapped_type_fn)() ENTT_NOEXCEPT = nullptr;
  26906. meta_type(* value_type_fn)() ENTT_NOEXCEPT = nullptr;
  26907. size_type(* size_fn)(const any &) ENTT_NOEXCEPT = nullptr;
  26908. bool(* clear_fn)(any &) = nullptr;
  26909. iterator(* begin_fn)(any &) = nullptr;
  26910. iterator(* end_fn)(any &) = nullptr;
  26911. bool(* insert_fn)(any &, meta_any &, meta_any &) = nullptr;
  26912. bool(* erase_fn)(any &, meta_any &) = nullptr;
  26913. iterator(* find_fn)(any &, meta_any &) = nullptr;
  26914. any storage{};
  26915. };
  26916. /*! @brief Opaque wrapper for values of any type. */
  26917. class meta_any {
  26918. enum class operation { DTOR, DEREF, SEQ, ASSOC };
  26919. using vtable_type = void(const operation, const any &, void *);
  26920. template<typename Type>
  26921. static void basic_vtable([[maybe_unused]] const operation op, [[maybe_unused]] const any &from, [[maybe_unused]] void *to) {
  26922. static_assert(std::is_same_v<std::remove_reference_t<std::remove_const_t<Type>>, Type>, "Invalid type");
  26923. if constexpr(!std::is_void_v<Type>) {
  26924. switch(op) {
  26925. case operation::DTOR:
  26926. if(auto *curr = static_cast<internal::meta_type_node *>(to); curr->dtor && from.owner()) {
  26927. curr->dtor(const_cast<any &>(from).data());
  26928. }
  26929. break;
  26930. case operation::DEREF:
  26931. if constexpr(is_meta_pointer_like_v<Type>) {
  26932. using element_type = std::remove_const_t<typename std::pointer_traits<Type>::element_type>;
  26933. if constexpr(std::is_function_v<element_type>) {
  26934. *static_cast<meta_any *>(to) = any_cast<Type>(from);
  26935. } else if constexpr(!std::is_same_v<std::remove_const_t<typename std::pointer_traits<Type>::element_type>, void>) {
  26936. using in_place_type = decltype(adl_meta_pointer_like<Type>::dereference(any_cast<const Type &>(from)));
  26937. static_cast<meta_any *>(to)->emplace<in_place_type>(adl_meta_pointer_like<Type>::dereference(any_cast<const Type &>(from)));
  26938. }
  26939. }
  26940. case operation::SEQ:
  26941. if constexpr(is_complete_v<meta_sequence_container_traits<Type>>) {
  26942. *static_cast<meta_sequence_container *>(to) = { std::in_place_type<Type>, std::move(const_cast<any &>(from)) };
  26943. }
  26944. break;
  26945. case operation::ASSOC:
  26946. if constexpr(is_complete_v<meta_associative_container_traits<Type>>) {
  26947. *static_cast<meta_associative_container *>(to) = { std::in_place_type<Type>, std::move(const_cast<any &>(from)) };
  26948. }
  26949. break;
  26950. }
  26951. }
  26952. }
  26953. meta_any(const meta_any &other, any ref) ENTT_NOEXCEPT
  26954. : storage{std::move(ref)},
  26955. node{storage ? other.node : nullptr},
  26956. vtable{storage ? other.vtable : &basic_vtable<void>}
  26957. {}
  26958. public:
  26959. /*! @brief Default constructor. */
  26960. meta_any() ENTT_NOEXCEPT
  26961. : storage{},
  26962. node{},
  26963. vtable{&basic_vtable<void>}
  26964. {}
  26965. /**
  26966. * @brief Constructs a wrapper by directly initializing the new object.
  26967. * @tparam Type Type of object to use to initialize the wrapper.
  26968. * @tparam Args Types of arguments to use to construct the new instance.
  26969. * @param args Parameters to use to construct the instance.
  26970. */
  26971. template<typename Type, typename... Args>
  26972. explicit meta_any(std::in_place_type_t<Type>, Args &&... args)
  26973. : storage{std::in_place_type<Type>, std::forward<Args>(args)...},
  26974. node{internal::meta_info<Type>::resolve()},
  26975. vtable{&basic_vtable<std::remove_const_t<std::remove_reference_t<Type>>>}
  26976. {}
  26977. /**
  26978. * @brief Constructs a wrapper that holds an unmanaged object.
  26979. * @tparam Type Type of object to use to initialize the wrapper.
  26980. * @param value An instance of an object to use to initialize the wrapper.
  26981. */
  26982. template<typename Type>
  26983. meta_any(std::reference_wrapper<Type> value)
  26984. : meta_any{}
  26985. {
  26986. // invokes deprecated assignment operator (and avoids issues with vs2017)
  26987. *this = value;
  26988. }
  26989. /**
  26990. * @brief Constructs a wrapper from a given value.
  26991. * @tparam Type Type of object to use to initialize the wrapper.
  26992. * @param value An instance of an object to use to initialize the wrapper.
  26993. */
  26994. template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::decay_t<Type>, meta_any>>>
  26995. meta_any(Type &&value)
  26996. : storage{std::forward<Type>(value)},
  26997. node{internal::meta_info<std::decay_t<Type>>::resolve()},
  26998. vtable{&basic_vtable<std::decay_t<Type>>}
  26999. {}
  27000. /**
  27001. * @brief Copy constructor.
  27002. * @param other The instance to copy from.
  27003. */
  27004. meta_any(const meta_any &other) = default;
  27005. /**
  27006. * @brief Move constructor.
  27007. * @param other The instance to move from.
  27008. */
  27009. meta_any(meta_any &&other) ENTT_NOEXCEPT
  27010. : storage{std::move(other.storage)},
  27011. node{std::exchange(other.node, nullptr)},
  27012. vtable{std::exchange(other.vtable, &basic_vtable<void>)}
  27013. {}
  27014. /*! @brief Frees the internal storage, whatever it means. */
  27015. ~meta_any() {
  27016. vtable(operation::DTOR, storage, node);
  27017. }
  27018. /**
  27019. * @brief Copy assignment operator.
  27020. * @param other The instance to copy from.
  27021. * @return This meta any object.
  27022. */
  27023. meta_any & operator=(const meta_any &other) {
  27024. std::exchange(vtable, other.vtable)(operation::DTOR, storage, node);
  27025. storage = other.storage;
  27026. node = other.node;
  27027. return *this;
  27028. }
  27029. /**
  27030. * @brief Move assignment operator.
  27031. * @param other The instance to move from.
  27032. * @return This meta any object.
  27033. */
  27034. meta_any & operator=(meta_any &&other) ENTT_NOEXCEPT {
  27035. std::exchange(vtable, std::exchange(other.vtable, &basic_vtable<void>))(operation::DTOR, storage, node);
  27036. storage = std::move(other.storage);
  27037. node = std::exchange(other.node, nullptr);
  27038. return *this;
  27039. }
  27040. /**
  27041. * @brief Value assignment operator.
  27042. * @tparam Type Type of object to use to initialize the wrapper.
  27043. * @param value An instance of an object to use to initialize the wrapper.
  27044. * @return This meta any object.
  27045. */
  27046. template<typename Type>
  27047. [[deprecated("Use std::in_place_type<T &>, entt::make_meta<T &>, emplace<Type &> or forward_as_meta instead")]]
  27048. meta_any & operator=(std::reference_wrapper<Type> value) {
  27049. emplace<Type &>(value.get());
  27050. return *this;
  27051. }
  27052. /**
  27053. * @brief Value assignment operator.
  27054. * @tparam Type Type of object to use to initialize the wrapper.
  27055. * @param value An instance of an object to use to initialize the wrapper.
  27056. * @return This meta any object.
  27057. */
  27058. template<typename Type>
  27059. std::enable_if_t<!std::is_same_v<std::decay_t<Type>, meta_any>, meta_any &>
  27060. operator=(Type &&value) {
  27061. emplace<std::decay_t<Type>>(std::forward<Type>(value));
  27062. return *this;
  27063. }
  27064. /**
  27065. * @brief Returns the type of the underlying object.
  27066. * @return The type of the underlying object, if any.
  27067. */
  27068. [[nodiscard]] inline meta_type type() const ENTT_NOEXCEPT;
  27069. /**
  27070. * @brief Returns an opaque pointer to the contained instance.
  27071. * @return An opaque pointer the contained instance, if any.
  27072. */
  27073. [[nodiscard]] const void * data() const ENTT_NOEXCEPT {
  27074. return storage.data();
  27075. }
  27076. /*! @copydoc data */
  27077. [[nodiscard]] void * data() ENTT_NOEXCEPT {
  27078. return storage.data();
  27079. }
  27080. /**
  27081. * @brief Invokes the underlying function, if possible.
  27082. *
  27083. * @sa meta_func::invoke
  27084. *
  27085. * @tparam Args Types of arguments to use to invoke the function.
  27086. * @param id Unique identifier.
  27087. * @param args Parameters to use to invoke the function.
  27088. * @return A wrapper containing the returned value, if any.
  27089. */
  27090. template<typename... Args>
  27091. meta_any invoke(const id_type id, Args &&... args) const;
  27092. /*! @copydoc invoke */
  27093. template<typename... Args>
  27094. meta_any invoke(const id_type id, Args &&... args);
  27095. /**
  27096. * @brief Sets the value of a given variable.
  27097. *
  27098. * The type of the value must be such that a cast or conversion to the type
  27099. * of the variable is possible. Otherwise, invoking the setter does nothing.
  27100. *
  27101. * @tparam Type Type of value to assign.
  27102. * @param id Unique identifier.
  27103. * @param value Parameter to use to set the underlying variable.
  27104. * @return True in case of success, false otherwise.
  27105. */
  27106. template<typename Type>
  27107. bool set(const id_type id, Type &&value);
  27108. /**
  27109. * @brief Gets the value of a given variable.
  27110. * @param id Unique identifier.
  27111. * @return A wrapper containing the value of the underlying variable.
  27112. */
  27113. [[nodiscard]] meta_any get(const id_type id) const;
  27114. /*! @copydoc get */
  27115. [[nodiscard]] meta_any get(const id_type id);
  27116. /**
  27117. * @brief Tries to cast an instance to a given type.
  27118. * @tparam Type Type to which to cast the instance.
  27119. * @return A (possibly null) pointer to the contained instance.
  27120. */
  27121. template<typename Type>
  27122. [[nodiscard]] const Type * try_cast() const {
  27123. if(node) {
  27124. if(const auto info = type_id<Type>(); node->info == info) {
  27125. return any_cast<Type>(&storage);
  27126. } else if(const auto *base = internal::meta_visit<&internal::meta_type_node::base>([info](const auto *curr) { return curr->type()->info == info; }, node); base) {
  27127. return static_cast<const Type *>(base->cast(storage.data()));
  27128. }
  27129. }
  27130. return nullptr;
  27131. }
  27132. /*! @copydoc try_cast */
  27133. template<typename Type>
  27134. [[nodiscard]] Type * try_cast() {
  27135. if(node) {
  27136. if(const auto info = type_id<Type>(); node->info == info) {
  27137. return any_cast<Type>(&storage);
  27138. } else if(const auto *base = internal::meta_visit<&internal::meta_type_node::base>([info](const auto *curr) { return curr->type()->info == info; }, node); base) {
  27139. return static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(base->cast(static_cast<constness_as_t<any, Type> &>(storage).data())));
  27140. }
  27141. }
  27142. return nullptr;
  27143. }
  27144. /**
  27145. * @brief Tries to cast an instance to a given type.
  27146. *
  27147. * The type of the instance must be such that the cast is possible.
  27148. *
  27149. * @warning
  27150. * Attempting to perform an invalid cast results in undefined behavior.
  27151. *
  27152. * @tparam Type Type to which to cast the instance.
  27153. * @return A reference to the contained instance.
  27154. */
  27155. template<typename Type>
  27156. [[nodiscard]] Type cast() const {
  27157. auto * const instance = try_cast<std::remove_reference_t<Type>>();
  27158. ENTT_ASSERT(instance, "Invalid instance");
  27159. return static_cast<Type>(*instance);
  27160. }
  27161. /*! @copydoc cast */
  27162. template<typename Type>
  27163. [[nodiscard]] Type cast() {
  27164. // forces const on non-reference types to make them work also with wrappers for const references
  27165. auto * const instance = try_cast<std::remove_reference_t<const Type>>();
  27166. ENTT_ASSERT(instance, "Invalid instance");
  27167. return static_cast<Type>(*instance);
  27168. }
  27169. /**
  27170. * @brief Converts an object in such a way that a given cast becomes viable.
  27171. * @tparam Type Type to which the cast is requested.
  27172. * @return A valid meta any object if there exists a viable conversion, an
  27173. * invalid one otherwise.
  27174. */
  27175. template<typename Type>
  27176. [[nodiscard]] meta_any allow_cast() const {
  27177. if(try_cast<std::remove_reference_t<Type>>() != nullptr) {
  27178. return as_ref();
  27179. } else if(node) {
  27180. if(const auto * const conv = internal::meta_visit<&internal::meta_type_node::conv>([info = type_id<Type>()](const auto *curr) { return curr->type()->info == info; }, node); conv) {
  27181. return conv->conv(storage.data());
  27182. }
  27183. }
  27184. return {};
  27185. }
  27186. /**
  27187. * @brief Converts an object in such a way that a given cast becomes viable.
  27188. * @tparam Type Type to which the cast is requested.
  27189. * @return True if there exists a viable conversion, false otherwise.
  27190. */
  27191. template<typename Type>
  27192. bool allow_cast() {
  27193. // forces const on non-reference types to make them work also with wrappers for const references
  27194. if(try_cast<std::remove_reference_t<const Type>>() != nullptr) {
  27195. return true;
  27196. } else if(node) {
  27197. if(const auto * const conv = internal::meta_visit<&internal::meta_type_node::conv>([info = type_id<Type>()](const auto *curr) { return curr->type()->info == info; }, node); conv) {
  27198. *this = conv->conv(std::as_const(storage).data());
  27199. return true;
  27200. }
  27201. }
  27202. return false;
  27203. }
  27204. /**
  27205. * @brief Replaces the contained object by creating a new instance directly.
  27206. * @tparam Type Type of object to use to initialize the wrapper.
  27207. * @tparam Args Types of arguments to use to construct the new instance.
  27208. * @param args Parameters to use to construct the instance.
  27209. */
  27210. template<typename Type, typename... Args>
  27211. void emplace(Args &&... args) {
  27212. std::exchange(vtable, &basic_vtable<std::remove_const_t<std::remove_reference_t<Type>>>)(operation::DTOR, storage, node);
  27213. storage.emplace<Type>(std::forward<Args>(args)...);
  27214. node = internal::meta_info<Type>::resolve();
  27215. }
  27216. /*! @brief Destroys contained object */
  27217. void reset() {
  27218. std::exchange(vtable, &basic_vtable<void>)(operation::DTOR, storage, node);
  27219. storage.reset();
  27220. node = nullptr;
  27221. }
  27222. /**
  27223. * @brief Returns a sequence container proxy.
  27224. * @return A sequence container proxy for the underlying object.
  27225. */
  27226. [[nodiscard]] meta_sequence_container as_sequence_container() ENTT_NOEXCEPT {
  27227. meta_sequence_container proxy;
  27228. vtable(operation::SEQ, storage.as_ref(), &proxy);
  27229. return proxy;
  27230. }
  27231. /*! @copydoc as_sequence_container */
  27232. [[nodiscard]] meta_sequence_container as_sequence_container() const ENTT_NOEXCEPT {
  27233. meta_sequence_container proxy;
  27234. vtable(operation::SEQ, storage.as_ref(), &proxy);
  27235. return proxy;
  27236. }
  27237. /**
  27238. * @brief Returns an associative container proxy.
  27239. * @return An associative container proxy for the underlying object.
  27240. */
  27241. [[nodiscard]] meta_associative_container as_associative_container() ENTT_NOEXCEPT {
  27242. meta_associative_container proxy;
  27243. vtable(operation::ASSOC, storage.as_ref(), &proxy);
  27244. return proxy;
  27245. }
  27246. /*! @copydoc as_associative_container */
  27247. [[nodiscard]] meta_associative_container as_associative_container() const ENTT_NOEXCEPT {
  27248. meta_associative_container proxy;
  27249. vtable(operation::ASSOC, storage.as_ref(), &proxy);
  27250. return proxy;
  27251. }
  27252. /**
  27253. * @brief Indirection operator for dereferencing opaque objects.
  27254. * @return A wrapper that shares a reference to an unmanaged object if the
  27255. * wrapped element is dereferenceable, an invalid meta any otherwise.
  27256. */
  27257. [[nodiscard]] meta_any operator*() const ENTT_NOEXCEPT {
  27258. meta_any ret{};
  27259. vtable(operation::DEREF, storage, &ret);
  27260. return ret;
  27261. }
  27262. /**
  27263. * @brief Returns false if a wrapper is invalid, true otherwise.
  27264. * @return False if the wrapper is invalid, true otherwise.
  27265. */
  27266. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  27267. return !(node == nullptr);
  27268. }
  27269. /**
  27270. * @brief Checks if two wrappers differ in their content.
  27271. * @param other Wrapper with which to compare.
  27272. * @return False if the two objects differ in their content, true otherwise.
  27273. */
  27274. [[nodiscard]] bool operator==(const meta_any &other) const {
  27275. return (!node && !other.node) || (node && other.node && node->info == other.node->info && storage == other.storage);
  27276. }
  27277. /**
  27278. * @brief Aliasing constructor.
  27279. * @return A wrapper that shares a reference to an unmanaged object.
  27280. */
  27281. [[nodiscard]] meta_any as_ref() ENTT_NOEXCEPT {
  27282. return meta_any{*this, storage.as_ref()};
  27283. }
  27284. /*! @copydoc as_ref */
  27285. [[nodiscard]] meta_any as_ref() const ENTT_NOEXCEPT {
  27286. return meta_any{*this, storage.as_ref()};
  27287. }
  27288. private:
  27289. any storage;
  27290. internal::meta_type_node *node;
  27291. vtable_type *vtable;
  27292. };
  27293. /**
  27294. * @brief Checks if two wrappers differ in their content.
  27295. * @param lhs A wrapper, either empty or not.
  27296. * @param rhs A wrapper, either empty or not.
  27297. * @return True if the two wrappers differ in their content, false otherwise.
  27298. */
  27299. [[nodiscard]] inline bool operator!=(const meta_any &lhs, const meta_any &rhs) ENTT_NOEXCEPT {
  27300. return !(lhs == rhs);
  27301. }
  27302. /**
  27303. * @brief Constructs a wrapper from a given type, passing it all arguments.
  27304. * @tparam Type Type of object to use to initialize the wrapper.
  27305. * @tparam Args Types of arguments to use to construct the new instance.
  27306. * @param args Parameters to use to construct the instance.
  27307. * @return A properly initialized wrapper for an object of the given type.
  27308. */
  27309. template<typename Type, typename... Args>
  27310. meta_any make_meta(Args &&... args) {
  27311. return meta_any{std::in_place_type<Type>, std::forward<Args>(args)...};
  27312. }
  27313. /**
  27314. * @brief Forwards its argument and avoids copies for lvalue references.
  27315. * @tparam Type Type of argument to use to construct the new instance.
  27316. * @param value Parameter to use to construct the instance.
  27317. * @return A properly initialized and not necessarily owning wrapper.
  27318. */
  27319. template<typename Type>
  27320. meta_any forward_as_meta(Type &&value) {
  27321. return meta_any{std::in_place_type<std::conditional_t<std::is_rvalue_reference_v<Type>, std::decay_t<Type>, Type>>, std::forward<Type>(value)};
  27322. }
  27323. /**
  27324. * @brief Opaque pointers to instances of any type.
  27325. *
  27326. * A handle doesn't perform copies and isn't responsible for the contained
  27327. * object. It doesn't prolong the lifetime of the pointed instance.<br/>
  27328. * Handles are used to generate references to actual objects when needed.
  27329. */
  27330. struct meta_handle {
  27331. /*! @brief Default constructor. */
  27332. meta_handle() = default;
  27333. /*! @brief Default copy constructor, deleted on purpose. */
  27334. meta_handle(const meta_handle &) = delete;
  27335. /*! @brief Default move constructor. */
  27336. meta_handle(meta_handle &&) = default;
  27337. /**
  27338. * @brief Default copy assignment operator, deleted on purpose.
  27339. * @return This meta handle.
  27340. */
  27341. meta_handle & operator=(const meta_handle &) = delete;
  27342. /**
  27343. * @brief Default move assignment operator.
  27344. * @return This meta handle.
  27345. */
  27346. meta_handle & operator=(meta_handle &&) = default;
  27347. /**
  27348. * @brief Creates a handle that points to an unmanaged object.
  27349. * @tparam Type Type of object to use to initialize the handle.
  27350. * @param value An instance of an object to use to initialize the handle.
  27351. */
  27352. template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::decay_t<Type>, meta_handle>>>
  27353. meta_handle(Type &value) ENTT_NOEXCEPT
  27354. : meta_handle{}
  27355. {
  27356. if constexpr(std::is_same_v<std::decay_t<Type>, meta_any>) {
  27357. any = value.as_ref();
  27358. } else {
  27359. any.emplace<Type &>(value);
  27360. }
  27361. }
  27362. /**
  27363. * @brief Returns false if a handle is invalid, true otherwise.
  27364. * @return False if the handle is invalid, true otherwise.
  27365. */
  27366. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  27367. return static_cast<bool>(any);
  27368. }
  27369. /**
  27370. * @brief Access operator for accessing the contained opaque object.
  27371. * @return A wrapper that shares a reference to an unmanaged object.
  27372. */
  27373. [[nodiscard]] meta_any * operator->() {
  27374. return &any;
  27375. }
  27376. /*! @copydoc operator-> */
  27377. [[nodiscard]] const meta_any * operator->() const {
  27378. return &any;
  27379. }
  27380. private:
  27381. meta_any any;
  27382. };
  27383. /*! @brief Opaque wrapper for properties of any type. */
  27384. struct meta_prop {
  27385. /*! @brief Node type. */
  27386. using node_type = internal::meta_prop_node;
  27387. /**
  27388. * @brief Constructs an instance from a given node.
  27389. * @param curr The underlying node with which to construct the instance.
  27390. */
  27391. meta_prop(const node_type *curr = nullptr) ENTT_NOEXCEPT
  27392. : node{curr}
  27393. {}
  27394. /**
  27395. * @brief Returns the stored key as a const reference.
  27396. * @return A wrapper containing the key stored with the property.
  27397. */
  27398. [[nodiscard]] meta_any key() const {
  27399. return node->id.as_ref();
  27400. }
  27401. /**
  27402. * @brief Returns the stored value by copy.
  27403. * @return A wrapper containing the value stored with the property.
  27404. */
  27405. [[nodiscard]] meta_any value() const {
  27406. return node->value;
  27407. }
  27408. /**
  27409. * @brief Returns true if an object is valid, false otherwise.
  27410. * @return True if the object is valid, false otherwise.
  27411. */
  27412. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  27413. return !(node == nullptr);
  27414. }
  27415. private:
  27416. const node_type *node;
  27417. };
  27418. /*! @brief Opaque wrapper for constructors. */
  27419. struct meta_ctor {
  27420. /*! @brief Node type. */
  27421. using node_type = internal::meta_ctor_node;
  27422. /*! @brief Unsigned integer type. */
  27423. using size_type = typename node_type::size_type;
  27424. /*! @copydoc meta_prop::meta_prop */
  27425. meta_ctor(const node_type *curr = nullptr) ENTT_NOEXCEPT
  27426. : node{curr}
  27427. {}
  27428. /**
  27429. * @brief Returns the type to which an object belongs.
  27430. * @return The type to which the object belongs.
  27431. */
  27432. [[nodiscard]] inline meta_type parent() const ENTT_NOEXCEPT;
  27433. /**
  27434. * @brief Returns the number of arguments accepted by a constructor.
  27435. * @return The number of arguments accepted by the constructor.
  27436. */
  27437. [[nodiscard]] size_type arity() const ENTT_NOEXCEPT {
  27438. return node->arity;
  27439. }
  27440. /**
  27441. * @brief Returns the type of the i-th argument of a constructor.
  27442. * @param index Index of the argument of which to return the type.
  27443. * @return The type of the i-th argument of a constructor.
  27444. */
  27445. [[nodiscard]] meta_type arg(size_type index) const ENTT_NOEXCEPT;
  27446. /**
  27447. * @brief Creates an instance of the underlying type, if possible.
  27448. *
  27449. * Parameters must be such that a cast or conversion to the required types
  27450. * is possible. Otherwise, an empty and thus invalid wrapper is returned.
  27451. *
  27452. * @param args Parameters to use to construct the instance.
  27453. * @param sz Number of parameters to use to construct the instance.
  27454. * @return A wrapper containing the new instance, if any.
  27455. */
  27456. [[nodiscard]] meta_any invoke(meta_any * const args, const size_type sz) const {
  27457. return sz == arity() ? node->invoke(args) : meta_any{};
  27458. }
  27459. /**
  27460. * @copybrief invoke
  27461. *
  27462. * @sa invoke
  27463. *
  27464. * @tparam Args Types of arguments to use to construct the instance.
  27465. * @param args Parameters to use to construct the instance.
  27466. * @return A wrapper containing the new instance, if any.
  27467. */
  27468. template<typename... Args>
  27469. [[nodiscard]] meta_any invoke([[maybe_unused]] Args &&... args) const {
  27470. meta_any arguments[sizeof...(Args) + 1u]{std::forward<Args>(args)...};
  27471. return invoke(arguments, sizeof...(Args));
  27472. }
  27473. /**
  27474. * @brief Returns a range to use to visit all properties.
  27475. * @return An iterable range to use to visit all properties.
  27476. */
  27477. [[nodiscard]] meta_range<meta_prop> prop() const ENTT_NOEXCEPT {
  27478. return node->prop;
  27479. }
  27480. /**
  27481. * @brief Returns the property associated with a given key.
  27482. * @param key The key to use to search for a property.
  27483. * @return The property associated with the given key, if any.
  27484. */
  27485. [[nodiscard]] meta_prop prop(meta_any key) const {
  27486. return internal::meta_visit<&node_type::prop>([&key](const auto *curr) { return curr->id == key; }, node);
  27487. }
  27488. /**
  27489. * @brief Returns true if an object is valid, false otherwise.
  27490. * @return True if the object is valid, false otherwise.
  27491. */
  27492. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  27493. return !(node == nullptr);
  27494. }
  27495. private:
  27496. const node_type *node;
  27497. };
  27498. /*! @brief Opaque wrapper for data members. */
  27499. struct meta_data {
  27500. /*! @brief Node type. */
  27501. using node_type = internal::meta_data_node;
  27502. /*! @copydoc meta_prop::meta_prop */
  27503. meta_data(const node_type *curr = nullptr) ENTT_NOEXCEPT
  27504. : node{curr}
  27505. {}
  27506. /*! @copydoc meta_type::id */
  27507. [[nodiscard]] id_type id() const ENTT_NOEXCEPT {
  27508. return node->id;
  27509. }
  27510. /*! @copydoc meta_ctor::parent */
  27511. [[nodiscard]] inline meta_type parent() const ENTT_NOEXCEPT;
  27512. /**
  27513. * @brief Indicates whether a data member is constant or not.
  27514. * @return True if the data member is constant, false otherwise.
  27515. */
  27516. [[nodiscard]] bool is_const() const ENTT_NOEXCEPT {
  27517. return node->is_const;
  27518. }
  27519. /**
  27520. * @brief Indicates whether a data member is static or not.
  27521. * @return True if the data member is static, false otherwise.
  27522. */
  27523. [[nodiscard]] bool is_static() const ENTT_NOEXCEPT {
  27524. return node->is_static;
  27525. }
  27526. /*! @copydoc meta_any::type */
  27527. [[nodiscard]] inline meta_type type() const ENTT_NOEXCEPT;
  27528. /**
  27529. * @brief Sets the value of a given variable.
  27530. *
  27531. * It must be possible to cast the instance to the parent type of the data
  27532. * member. Otherwise, invoking the setter results in an undefined
  27533. * behavior.<br/>
  27534. * The type of the value must be such that a cast or conversion to the type
  27535. * of the variable is possible. Otherwise, invoking the setter does nothing.
  27536. *
  27537. * @tparam Type Type of value to assign.
  27538. * @param instance An opaque instance of the underlying type.
  27539. * @param value Parameter to use to set the underlying variable.
  27540. * @return True in case of success, false otherwise.
  27541. */
  27542. template<typename Type>
  27543. bool set(meta_handle instance, Type &&value) const {
  27544. return node->set && node->set(std::move(instance), std::forward<Type>(value));
  27545. }
  27546. /**
  27547. * @brief Gets the value of a given variable.
  27548. *
  27549. * It must be possible to cast the instance to the parent type of the data
  27550. * member. Otherwise, invoking the getter results in an undefined behavior.
  27551. *
  27552. * @param instance An opaque instance of the underlying type.
  27553. * @return A wrapper containing the value of the underlying variable.
  27554. */
  27555. [[nodiscard]] meta_any get(meta_handle instance) const {
  27556. return node->get(std::move(instance));
  27557. }
  27558. /*! @copydoc meta_ctor::prop */
  27559. [[nodiscard]] meta_range<meta_prop> prop() const ENTT_NOEXCEPT {
  27560. return node->prop;
  27561. }
  27562. /**
  27563. * @brief Returns the property associated with a given key.
  27564. * @param key The key to use to search for a property.
  27565. * @return The property associated with the given key, if any.
  27566. */
  27567. [[nodiscard]] meta_prop prop(meta_any key) const {
  27568. return internal::meta_visit<&node_type::prop>([&key](const auto *curr) { return curr->id == key; }, node);
  27569. }
  27570. /**
  27571. * @brief Returns true if an object is valid, false otherwise.
  27572. * @return True if the object is valid, false otherwise.
  27573. */
  27574. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  27575. return !(node == nullptr);
  27576. }
  27577. private:
  27578. const node_type *node;
  27579. };
  27580. /*! @brief Opaque wrapper for member functions. */
  27581. struct meta_func {
  27582. /*! @brief Node type. */
  27583. using node_type = internal::meta_func_node;
  27584. /*! @brief Unsigned integer type. */
  27585. using size_type = typename node_type::size_type;
  27586. /*! @copydoc meta_prop::meta_prop */
  27587. meta_func(const node_type *curr = nullptr) ENTT_NOEXCEPT
  27588. : node{curr}
  27589. {}
  27590. /*! @copydoc meta_type::id */
  27591. [[nodiscard]] id_type id() const ENTT_NOEXCEPT {
  27592. return node->id;
  27593. }
  27594. /*! @copydoc meta_ctor::parent */
  27595. [[nodiscard]] inline meta_type parent() const ENTT_NOEXCEPT;
  27596. /**
  27597. * @brief Returns the number of arguments accepted by a member function.
  27598. * @return The number of arguments accepted by the member function.
  27599. */
  27600. [[nodiscard]] size_type arity() const ENTT_NOEXCEPT {
  27601. return node->arity;
  27602. }
  27603. /**
  27604. * @brief Indicates whether a member function is constant or not.
  27605. * @return True if the member function is constant, false otherwise.
  27606. */
  27607. [[nodiscard]] bool is_const() const ENTT_NOEXCEPT {
  27608. return node->is_const;
  27609. }
  27610. /**
  27611. * @brief Indicates whether a member function is static or not.
  27612. * @return True if the member function is static, false otherwise.
  27613. */
  27614. [[nodiscard]] bool is_static() const ENTT_NOEXCEPT {
  27615. return node->is_static;
  27616. }
  27617. /**
  27618. * @brief Returns the return type of a member function.
  27619. * @return The return type of the member function.
  27620. */
  27621. [[nodiscard]] inline meta_type ret() const ENTT_NOEXCEPT;
  27622. /**
  27623. * @brief Returns the type of the i-th argument of a member function.
  27624. * @param index Index of the argument of which to return the type.
  27625. * @return The type of the i-th argument of a member function.
  27626. */
  27627. [[nodiscard]] inline meta_type arg(size_type index) const ENTT_NOEXCEPT;
  27628. /**
  27629. * @brief Invokes the underlying function, if possible.
  27630. *
  27631. * To invoke a member function, the parameters must be such that a cast or
  27632. * conversion to the required types is possible. Otherwise, an empty and
  27633. * thus invalid wrapper is returned.<br/>
  27634. * It must be possible to cast the instance to the parent type of the member
  27635. * function. Otherwise, invoking the underlying function results in an
  27636. * undefined behavior.
  27637. *
  27638. * @param instance An opaque instance of the underlying type.
  27639. * @param args Parameters to use to invoke the function.
  27640. * @param sz Number of parameters to use to invoke the function.
  27641. * @return A wrapper containing the returned value, if any.
  27642. */
  27643. meta_any invoke(meta_handle instance, meta_any * const args, const size_type sz) const {
  27644. return sz == arity() ? node->invoke(std::move(instance), args) : meta_any{};
  27645. }
  27646. /**
  27647. * @copybrief invoke
  27648. *
  27649. * @sa invoke
  27650. *
  27651. * @tparam Args Types of arguments to use to invoke the function.
  27652. * @param instance An opaque instance of the underlying type.
  27653. * @param args Parameters to use to invoke the function.
  27654. * @return A wrapper containing the new instance, if any.
  27655. */
  27656. template<typename... Args>
  27657. meta_any invoke(meta_handle instance, Args &&... args) const {
  27658. meta_any arguments[sizeof...(Args) + 1u]{std::forward<Args>(args)...};
  27659. return invoke(std::move(instance), arguments, sizeof...(Args));
  27660. }
  27661. /*! @copydoc meta_ctor::prop */
  27662. [[nodiscard]] meta_range<meta_prop> prop() const ENTT_NOEXCEPT {
  27663. return node->prop;
  27664. }
  27665. /**
  27666. * @brief Returns the property associated with a given key.
  27667. * @param key The key to use to search for a property.
  27668. * @return The property associated with the given key, if any.
  27669. */
  27670. [[nodiscard]] meta_prop prop(meta_any key) const {
  27671. return internal::meta_visit<&node_type::prop>([&key](const auto *curr) { return curr->id == key; }, node);
  27672. }
  27673. /**
  27674. * @brief Returns true if an object is valid, false otherwise.
  27675. * @return True if the object is valid, false otherwise.
  27676. */
  27677. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  27678. return !(node == nullptr);
  27679. }
  27680. private:
  27681. const node_type *node;
  27682. };
  27683. /*! @brief Opaque wrapper for types. */
  27684. class meta_type {
  27685. static bool can_cast_or_convert(const internal::meta_type_node *type, const type_info info) ENTT_NOEXCEPT {
  27686. if(type->info == info) {
  27687. return true;
  27688. }
  27689. for(const auto *curr = type->conv; curr; curr = curr->next) {
  27690. if(curr->type()->info == info) {
  27691. return true;
  27692. }
  27693. }
  27694. for(const auto *curr = type->base; curr; curr = curr->next) {
  27695. if(auto *target = curr->type(); can_cast_or_convert(target, info)) {
  27696. return true;
  27697. }
  27698. }
  27699. return false;
  27700. }
  27701. template<typename... Args, auto... Index>
  27702. [[nodiscard]] static const internal::meta_ctor_node * ctor(const internal::meta_ctor_node *curr, std::index_sequence<Index...>) {
  27703. for(; curr; curr = curr->next) {
  27704. if(curr->arity == sizeof...(Args) && (can_cast_or_convert(internal::meta_info<Args>::resolve(), curr->arg(Index).info()) && ...)) {
  27705. return curr;
  27706. }
  27707. }
  27708. return nullptr;
  27709. }
  27710. template<auto... Member, typename Node>
  27711. void unregister_all(Node **curr) {
  27712. while(*curr) {
  27713. (unregister_all(&((*curr)->*Member)), ...);
  27714. *curr = std::exchange((*curr)->next, nullptr);
  27715. }
  27716. }
  27717. public:
  27718. /*! @brief Node type. */
  27719. using node_type = internal::meta_type_node;
  27720. /*! @brief Node type. */
  27721. using base_node_type = internal::meta_base_node;
  27722. /*! @brief Unsigned integer type. */
  27723. using size_type = typename node_type::size_type;
  27724. /*! @copydoc meta_prop::meta_prop */
  27725. meta_type(node_type *curr = nullptr) ENTT_NOEXCEPT
  27726. : node{curr}
  27727. {}
  27728. /**
  27729. * @brief Constructs an instance from a given base node.
  27730. * @param curr The base node with which to construct the instance.
  27731. */
  27732. meta_type(base_node_type *curr) ENTT_NOEXCEPT
  27733. : node{curr ? curr->type() : nullptr}
  27734. {}
  27735. /**
  27736. * @brief Returns the type info object of the underlying type.
  27737. * @return The type info object of the underlying type.
  27738. */
  27739. [[nodiscard]] type_info info() const ENTT_NOEXCEPT {
  27740. return node->info;
  27741. }
  27742. /**
  27743. * @brief Returns the identifier assigned to a type.
  27744. * @return The identifier assigned to the type.
  27745. */
  27746. [[nodiscard]] id_type id() const ENTT_NOEXCEPT {
  27747. return node->id;
  27748. }
  27749. /**
  27750. * @brief Returns the size of the underlying type if known.
  27751. * @return The size of the underlying type if known, 0 otherwise.
  27752. */
  27753. [[nodiscard]] size_type size_of() const ENTT_NOEXCEPT {
  27754. return node->size_of;
  27755. }
  27756. /**
  27757. * @brief Checks whether a type refers to void or not.
  27758. * @return True if the underlying type is void, false otherwise.
  27759. */
  27760. [[nodiscard]] bool is_void() const ENTT_NOEXCEPT {
  27761. return node->is_void;
  27762. }
  27763. /**
  27764. * @brief Checks whether a type refers to an integral type or not.
  27765. * @return True if the underlying type is an integral type, false otherwise.
  27766. */
  27767. [[nodiscard]] bool is_integral() const ENTT_NOEXCEPT {
  27768. return node->is_integral;
  27769. }
  27770. /**
  27771. * @brief Checks whether a type refers to a floating-point type or not.
  27772. * @return True if the underlying type is a floating-point type, false
  27773. * otherwise.
  27774. */
  27775. [[nodiscard]] bool is_floating_point() const ENTT_NOEXCEPT {
  27776. return node->is_floating_point;
  27777. }
  27778. /**
  27779. * @brief Checks whether a type refers to an array type or not.
  27780. * @return True if the underlying type is an array type, false otherwise.
  27781. */
  27782. [[nodiscard]] bool is_array() const ENTT_NOEXCEPT {
  27783. return node->is_array;
  27784. }
  27785. /**
  27786. * @brief Checks whether a type refers to an enum or not.
  27787. * @return True if the underlying type is an enum, false otherwise.
  27788. */
  27789. [[nodiscard]] bool is_enum() const ENTT_NOEXCEPT {
  27790. return node->is_enum;
  27791. }
  27792. /**
  27793. * @brief Checks whether a type refers to an union or not.
  27794. * @return True if the underlying type is an union, false otherwise.
  27795. */
  27796. [[nodiscard]] bool is_union() const ENTT_NOEXCEPT {
  27797. return node->is_union;
  27798. }
  27799. /**
  27800. * @brief Checks whether a type refers to a class or not.
  27801. * @return True if the underlying type is a class, false otherwise.
  27802. */
  27803. [[nodiscard]] bool is_class() const ENTT_NOEXCEPT {
  27804. return node->is_class;
  27805. }
  27806. /**
  27807. * @brief Checks whether a type refers to a pointer or not.
  27808. * @return True if the underlying type is a pointer, false otherwise.
  27809. */
  27810. [[nodiscard]] bool is_pointer() const ENTT_NOEXCEPT {
  27811. return node->is_pointer;
  27812. }
  27813. /**
  27814. * @brief Checks whether a type refers to a function pointer or not.
  27815. * @return True if the underlying type is a function pointer, false
  27816. * otherwise.
  27817. */
  27818. [[nodiscard]] bool is_function_pointer() const ENTT_NOEXCEPT {
  27819. return node->is_function_pointer;
  27820. }
  27821. /**
  27822. * @brief Checks whether a type refers to a pointer to data member or not.
  27823. * @return True if the underlying type is a pointer to data member, false
  27824. * otherwise.
  27825. */
  27826. [[nodiscard]] bool is_member_object_pointer() const ENTT_NOEXCEPT {
  27827. return node->is_member_object_pointer;
  27828. }
  27829. /**
  27830. * @brief Checks whether a type refers to a pointer to member function or
  27831. * not.
  27832. * @return True if the underlying type is a pointer to member function,
  27833. * false otherwise.
  27834. */
  27835. [[nodiscard]] bool is_member_function_pointer() const ENTT_NOEXCEPT {
  27836. return node->is_member_function_pointer;
  27837. }
  27838. /**
  27839. * @brief Checks whether a type is a pointer-like type or not.
  27840. * @return True if the underlying type is a pointer-like one, false
  27841. * otherwise.
  27842. */
  27843. [[nodiscard]] bool is_pointer_like() const ENTT_NOEXCEPT {
  27844. return node->is_pointer_like;
  27845. }
  27846. /**
  27847. * @brief Checks whether a type refers to a sequence container or not.
  27848. * @return True if the type is a sequence container, false otherwise.
  27849. */
  27850. [[nodiscard]] bool is_sequence_container() const ENTT_NOEXCEPT {
  27851. return node->is_sequence_container;
  27852. }
  27853. /**
  27854. * @brief Checks whether a type refers to an associative container or not.
  27855. * @return True if the type is an associative container, false otherwise.
  27856. */
  27857. [[nodiscard]] bool is_associative_container() const ENTT_NOEXCEPT {
  27858. return node->is_associative_container;
  27859. }
  27860. /**
  27861. * @brief Checks whether a type refers to a recognized class template
  27862. * specialization or not.
  27863. * @return True if the type is a recognized class template specialization,
  27864. * false otherwise.
  27865. */
  27866. [[nodiscard]] bool is_template_specialization() const ENTT_NOEXCEPT {
  27867. return node->template_info.is_template_specialization;
  27868. }
  27869. /**
  27870. * @brief Returns the number of template arguments, if any.
  27871. * @return The number of template arguments, if any.
  27872. */
  27873. [[nodiscard]] size_type template_arity() const ENTT_NOEXCEPT {
  27874. return node->template_info.arity;
  27875. }
  27876. /**
  27877. * @brief Returns a tag for the class template of the underlying type.
  27878. *
  27879. * @sa meta_class_template_tag
  27880. *
  27881. * @return The tag for the class template of the underlying type.
  27882. */
  27883. [[nodiscard]] inline meta_type template_type() const ENTT_NOEXCEPT {
  27884. return is_template_specialization() ? node->template_info.type() : meta_type{};
  27885. }
  27886. /**
  27887. * @brief Returns the type of the i-th template argument of a type.
  27888. * @param index Index of the template argument of which to return the type.
  27889. * @return The type of the i-th template argument of a type.
  27890. */
  27891. [[nodiscard]] inline meta_type template_arg(size_type index) const ENTT_NOEXCEPT {
  27892. return index < template_arity() ? node->template_info.arg(index) : meta_type{};
  27893. }
  27894. /**
  27895. * @brief Provides the number of dimensions of an array type.
  27896. * @return The number of dimensions in case of array types, 0 otherwise.
  27897. */
  27898. [[nodiscard]] size_type rank() const ENTT_NOEXCEPT {
  27899. return node->rank;
  27900. }
  27901. /**
  27902. * @brief The number of elements along the given dimension of an array type.
  27903. * @param dim The dimension of which to return the number of elements.
  27904. * @return The number of elements along the given dimension in case of array
  27905. * types, 0 otherwise.
  27906. */
  27907. [[nodiscard]] size_type extent(size_type dim = {}) const ENTT_NOEXCEPT {
  27908. return node->extent(dim);
  27909. }
  27910. /**
  27911. * @brief Provides the type for which the pointer is defined.
  27912. * @return The type for which the pointer is defined or this type if it
  27913. * doesn't refer to a pointer type.
  27914. */
  27915. [[nodiscard]] meta_type remove_pointer() const ENTT_NOEXCEPT {
  27916. return node->remove_pointer();
  27917. }
  27918. /**
  27919. * @brief Provides the type for which the array is defined.
  27920. * @return The type for which the array is defined or this type if it
  27921. * doesn't refer to an array type.
  27922. */
  27923. [[nodiscard]] meta_type remove_extent() const ENTT_NOEXCEPT {
  27924. return node->remove_extent();
  27925. }
  27926. /**
  27927. * @brief Returns a range to use to visit top-level base meta types.
  27928. * @return An iterable range to use to visit top-level base meta types.
  27929. */
  27930. [[nodiscard]] meta_range<meta_type, internal::meta_base_node> base() const ENTT_NOEXCEPT {
  27931. return node->base;
  27932. }
  27933. /**
  27934. * @brief Returns the base meta type associated with a given identifier.
  27935. * @param id Unique identifier.
  27936. * @return The base meta type associated with the given identifier, if any.
  27937. */
  27938. [[nodiscard]] meta_type base(const id_type id) const {
  27939. return internal::meta_visit<&node_type::base>([id](const auto *curr) { return curr->type()->id == id; }, node);
  27940. }
  27941. /**
  27942. * @brief Returns a range to use to visit top-level constructors.
  27943. * @return An iterable range to use to visit top-level constructors.
  27944. */
  27945. [[nodiscard]] meta_range<meta_ctor> ctor() const ENTT_NOEXCEPT {
  27946. return node->ctor;
  27947. }
  27948. /**
  27949. * @brief Returns a constructor for a given list of types of arguments.
  27950. * @tparam Args Constructor arguments.
  27951. * @return The requested constructor, if any.
  27952. */
  27953. template<typename... Args>
  27954. [[nodiscard]] meta_ctor ctor() const {
  27955. return ctor<Args...>(node->ctor, std::make_index_sequence<sizeof...(Args)>{});
  27956. }
  27957. /**
  27958. * @brief Returns a range to use to visit top-level data.
  27959. * @return An iterable range to use to visit top-level data.
  27960. */
  27961. [[nodiscard]] meta_range<meta_data> data() const ENTT_NOEXCEPT {
  27962. return node->data;
  27963. }
  27964. /**
  27965. * @brief Returns the data associated with a given identifier.
  27966. *
  27967. * The data of the base classes will also be visited, if any.
  27968. *
  27969. * @param id Unique identifier.
  27970. * @return The data associated with the given identifier, if any.
  27971. */
  27972. [[nodiscard]] meta_data data(const id_type id) const {
  27973. return internal::meta_visit<&node_type::data>([id](const auto *curr) { return curr->id == id; }, node);
  27974. }
  27975. /**
  27976. * @brief Returns a range to use to visit top-level functions.
  27977. * @return An iterable range to use to visit top-level functions.
  27978. */
  27979. [[nodiscard]] meta_range<meta_func> func() const ENTT_NOEXCEPT {
  27980. return node->func;
  27981. }
  27982. /**
  27983. * @brief Returns the function associated with a given identifier.
  27984. *
  27985. * The functions of the base classes will also be visited, if any.<br/>
  27986. * In the case of overloaded functions, the first one with the required
  27987. * identifier will be returned.
  27988. *
  27989. * @param id Unique identifier.
  27990. * @return The function associated with the given identifier, if any.
  27991. */
  27992. [[nodiscard]] meta_func func(const id_type id) const {
  27993. return internal::meta_visit<&node_type::func>([id](const auto *curr) { return curr->id == id; }, node);
  27994. }
  27995. /**
  27996. * @brief Creates an instance of the underlying type, if possible.
  27997. *
  27998. * Parameters must be such that a cast or conversion to the required types
  27999. * is possible. Otherwise, an empty and thus invalid wrapper is returned.
  28000. *
  28001. * @param args Parameters to use to construct the instance.
  28002. * @param sz Number of parameters to use to construct the instance.
  28003. * @return A wrapper containing the new instance, if any.
  28004. */
  28005. [[nodiscard]] meta_any construct(meta_any * const args, const size_type sz) const {
  28006. meta_any ret{};
  28007. internal::meta_visit<&node_type::ctor>([args, sz, &ret](const auto *curr) { return (curr->arity == sz) && (ret = curr->invoke(args)); }, node);
  28008. return ret;
  28009. }
  28010. /**
  28011. * @copybrief construct
  28012. *
  28013. * @sa construct
  28014. *
  28015. * @tparam Args Types of arguments to use to construct the instance.
  28016. * @param args Parameters to use to construct the instance.
  28017. * @return A wrapper containing the new instance, if any.
  28018. */
  28019. template<typename... Args>
  28020. [[nodiscard]] meta_any construct(Args &&... args) const {
  28021. meta_any arguments[sizeof...(Args) + 1u]{std::forward<Args>(args)...};
  28022. return construct(arguments, sizeof...(Args));
  28023. }
  28024. /**
  28025. * @brief Invokes a function given an identifier, if possible.
  28026. *
  28027. * It must be possible to cast the instance to the parent type of the member
  28028. * function. Otherwise, invoking the underlying function results in an
  28029. * undefined behavior.
  28030. *
  28031. * @sa meta_func::invoke
  28032. *
  28033. * @param id Unique identifier.
  28034. * @param instance An opaque instance of the underlying type.
  28035. * @param args Parameters to use to invoke the function.
  28036. * @param sz Number of parameters to use to invoke the function.
  28037. * @return A wrapper containing the returned value, if any.
  28038. */
  28039. meta_any invoke(const id_type id, meta_handle instance, meta_any * const args, const size_type sz) const {
  28040. const internal::meta_func_node* candidate{};
  28041. size_type extent{sz + 1u};
  28042. bool ambiguous{};
  28043. for(auto *it = internal::meta_visit<&node_type::func>([id, sz](const auto *curr) { return curr->id == id && curr->arity == sz; }, node); it && it->id == id && it->arity == sz; it = it->next) {
  28044. size_type direct{};
  28045. size_type ext{};
  28046. for(size_type next{}; next < sz && next == (direct + ext); ++next) {
  28047. const auto type = args[next].type();
  28048. const auto req = it->arg(next).info();
  28049. type.info() == req ? ++direct : (ext += can_cast_or_convert(type.node, req));
  28050. }
  28051. if((direct + ext) == sz) {
  28052. if(ext < extent) {
  28053. candidate = it;
  28054. extent = ext;
  28055. ambiguous = false;
  28056. } else if(ext == extent) {
  28057. ambiguous = true;
  28058. }
  28059. }
  28060. }
  28061. return (candidate && !ambiguous) ? candidate->invoke(std::move(instance), args) : meta_any{};
  28062. }
  28063. /**
  28064. * @copybrief invoke
  28065. *
  28066. * @sa invoke
  28067. *
  28068. * @param id Unique identifier.
  28069. * @tparam Args Types of arguments to use to invoke the function.
  28070. * @param instance An opaque instance of the underlying type.
  28071. * @param args Parameters to use to invoke the function.
  28072. * @return A wrapper containing the new instance, if any.
  28073. */
  28074. template<typename... Args>
  28075. meta_any invoke(const id_type id, meta_handle instance, Args &&... args) const {
  28076. meta_any arguments[sizeof...(Args) + 1u]{std::forward<Args>(args)...};
  28077. return invoke(id, std::move(instance), arguments, sizeof...(Args));
  28078. }
  28079. /**
  28080. * @brief Sets the value of a given variable.
  28081. *
  28082. * It must be possible to cast the instance to the parent type of the data
  28083. * member. Otherwise, invoking the setter results in an undefined
  28084. * behavior.<br/>
  28085. * The type of the value must be such that a cast or conversion to the type
  28086. * of the variable is possible. Otherwise, invoking the setter does nothing.
  28087. *
  28088. * @tparam Type Type of value to assign.
  28089. * @param id Unique identifier.
  28090. * @param instance An opaque instance of the underlying type.
  28091. * @param value Parameter to use to set the underlying variable.
  28092. * @return True in case of success, false otherwise.
  28093. */
  28094. template<typename Type>
  28095. bool set(const id_type id, meta_handle instance, Type &&value) const {
  28096. const auto candidate = data(id);
  28097. return candidate && candidate.set(std::move(instance), std::forward<Type>(value));
  28098. }
  28099. /**
  28100. * @brief Gets the value of a given variable.
  28101. *
  28102. * It must be possible to cast the instance to the parent type of the data
  28103. * member. Otherwise, invoking the getter results in an undefined behavior.
  28104. *
  28105. * @param id Unique identifier.
  28106. * @param instance An opaque instance of the underlying type.
  28107. * @return A wrapper containing the value of the underlying variable.
  28108. */
  28109. [[nodiscard]] meta_any get(const id_type id, meta_handle instance) const {
  28110. const auto candidate = data(id);
  28111. return candidate ? candidate.get(std::move(instance)) : meta_any{};
  28112. }
  28113. /**
  28114. * @brief Returns a range to use to visit top-level properties.
  28115. * @return An iterable range to use to visit top-level properties.
  28116. */
  28117. [[nodiscard]] meta_range<meta_prop> prop() const ENTT_NOEXCEPT {
  28118. return node->prop;
  28119. }
  28120. /**
  28121. * @brief Returns the property associated with a given key.
  28122. *
  28123. * Properties of the base classes will also be visited, if any.
  28124. *
  28125. * @param key The key to use to search for a property.
  28126. * @return The property associated with the given key, if any.
  28127. */
  28128. [[nodiscard]] meta_prop prop(meta_any key) const {
  28129. return internal::meta_visit<&node_type::prop>([&key](const auto *curr) { return curr->id == key; }, node);
  28130. }
  28131. /**
  28132. * @brief Returns true if an object is valid, false otherwise.
  28133. * @return True if the object is valid, false otherwise.
  28134. */
  28135. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  28136. return !(node == nullptr);
  28137. }
  28138. /**
  28139. * @brief Checks if two objects refer to the same type.
  28140. * @param other The object with which to compare.
  28141. * @return True if the objects refer to the same type, false otherwise.
  28142. */
  28143. [[nodiscard]] bool operator==(const meta_type &other) const ENTT_NOEXCEPT {
  28144. return (!node && !other.node) || (node && other.node && node->info == other.node->info);
  28145. }
  28146. /**
  28147. * @brief Resets a type and all its parts.
  28148. *
  28149. * This function resets a type and all its data members, member functions
  28150. * and properties, as well as its constructors, destructors and conversion
  28151. * functions if any.<br/>
  28152. * Base classes aren't reset but the link between the two types is removed.
  28153. *
  28154. * The type is also removed from the list of searchable types.
  28155. */
  28156. void reset() ENTT_NOEXCEPT {
  28157. for(auto** it = internal::meta_context::global(); *it; it = &(*it)->next) {
  28158. if(*it == node) {
  28159. *it = (*it)->next;
  28160. break;
  28161. }
  28162. }
  28163. unregister_all(&node->prop);
  28164. unregister_all(&node->base);
  28165. unregister_all(&node->conv);
  28166. unregister_all<&internal::meta_ctor_node::prop>(&node->ctor);
  28167. unregister_all<&internal::meta_data_node::prop>(&node->data);
  28168. unregister_all<&internal::meta_func_node::prop>(&node->func);
  28169. node->id = {};
  28170. node->ctor = node->def_ctor;
  28171. node->dtor = nullptr;
  28172. }
  28173. private:
  28174. node_type *node;
  28175. };
  28176. /**
  28177. * @brief Checks if two objects refer to the same type.
  28178. * @param lhs An object, either valid or not.
  28179. * @param rhs An object, either valid or not.
  28180. * @return False if the objects refer to the same node, true otherwise.
  28181. */
  28182. [[nodiscard]] inline bool operator!=(const meta_type &lhs, const meta_type &rhs) ENTT_NOEXCEPT {
  28183. return !(lhs == rhs);
  28184. }
  28185. [[nodiscard]] inline meta_type meta_any::type() const ENTT_NOEXCEPT {
  28186. return node;
  28187. }
  28188. template<typename... Args>
  28189. meta_any meta_any::invoke(const id_type id, Args &&... args) const {
  28190. return type().invoke(id, *this, std::forward<Args>(args)...);
  28191. }
  28192. template<typename... Args>
  28193. meta_any meta_any::invoke(const id_type id, Args &&... args) {
  28194. return type().invoke(id, *this, std::forward<Args>(args)...);
  28195. }
  28196. template<typename Type>
  28197. bool meta_any::set(const id_type id, Type &&value) {
  28198. return type().set(id, *this, std::forward<Type>(value));
  28199. }
  28200. [[nodiscard]] inline meta_any meta_any::get(const id_type id) const {
  28201. return type().get(id, *this);
  28202. }
  28203. [[nodiscard]] inline meta_any meta_any::get(const id_type id) {
  28204. return type().get(id, *this);
  28205. }
  28206. [[nodiscard]] inline meta_type meta_ctor::parent() const ENTT_NOEXCEPT {
  28207. return node->parent;
  28208. }
  28209. [[nodiscard]] inline meta_type meta_ctor::arg(size_type index) const ENTT_NOEXCEPT {
  28210. return index < arity() ? node->arg(index) : meta_type{};
  28211. }
  28212. [[nodiscard]] inline meta_type meta_data::parent() const ENTT_NOEXCEPT {
  28213. return node->parent;
  28214. }
  28215. [[nodiscard]] inline meta_type meta_data::type() const ENTT_NOEXCEPT {
  28216. return node->type();
  28217. }
  28218. [[nodiscard]] inline meta_type meta_func::parent() const ENTT_NOEXCEPT {
  28219. return node->parent;
  28220. }
  28221. [[nodiscard]] inline meta_type meta_func::ret() const ENTT_NOEXCEPT {
  28222. return node->ret();
  28223. }
  28224. [[nodiscard]] inline meta_type meta_func::arg(size_type index) const ENTT_NOEXCEPT {
  28225. return index < arity() ? node->arg(index) : meta_type{};
  28226. }
  28227. /*! @brief Opaque iterator for sequence containers. */
  28228. class meta_sequence_container::meta_iterator {
  28229. /*! @brief A sequence container can access the underlying iterator. */
  28230. friend class meta_sequence_container;
  28231. enum class operation { INCR, DEREF };
  28232. using vtable_type = void(const operation, const any &, void *);
  28233. template<typename It>
  28234. static void basic_vtable(const operation op, const any &from, void *to) {
  28235. switch(op) {
  28236. case operation::INCR:
  28237. ++any_cast<It &>(const_cast<any &>(from));
  28238. break;
  28239. case operation::DEREF:
  28240. static_cast<meta_any *>(to)->emplace<typename std::iterator_traits<It>::reference>(*any_cast<const It &>(from));
  28241. break;
  28242. }
  28243. }
  28244. public:
  28245. /*! @brief Signed integer type. */
  28246. using difference_type = std::ptrdiff_t;
  28247. /*! @brief Type of elements returned by the iterator. */
  28248. using value_type = meta_any;
  28249. /*! @brief Pointer type, `void` on purpose. */
  28250. using pointer = void;
  28251. /*! @brief Reference type, it is **not** an actual reference. */
  28252. using reference = value_type;
  28253. /*! @brief Iterator category. */
  28254. using iterator_category = std::input_iterator_tag;
  28255. /*! @brief Default constructor. */
  28256. meta_iterator() ENTT_NOEXCEPT = default;
  28257. /**
  28258. * @brief Constructs a meta iterator from a given iterator.
  28259. * @tparam It Type of actual iterator with which to build the meta iterator.
  28260. * @param iter The actual iterator with which to build the meta iterator.
  28261. */
  28262. template<typename It>
  28263. meta_iterator(It iter)
  28264. : vtable{&basic_vtable<It>},
  28265. handle{std::move(iter)}
  28266. {}
  28267. /*! @brief Pre-increment operator. @return This iterator. */
  28268. meta_iterator & operator++() ENTT_NOEXCEPT {
  28269. return vtable(operation::INCR, handle, nullptr), *this;
  28270. }
  28271. /*! @brief Post-increment operator. @return This iterator. */
  28272. meta_iterator operator++(int) ENTT_NOEXCEPT {
  28273. meta_iterator orig = *this;
  28274. return ++(*this), orig;
  28275. }
  28276. /**
  28277. * @brief Checks if two iterators refer to the same element.
  28278. * @param other The iterator with which to compare.
  28279. * @return True if the iterators refer to the same element, false otherwise.
  28280. */
  28281. [[nodiscard]] bool operator==(const meta_iterator &other) const ENTT_NOEXCEPT {
  28282. return handle == other.handle;
  28283. }
  28284. /**
  28285. * @brief Checks if two iterators refer to the same element.
  28286. * @param other The iterator with which to compare.
  28287. * @return False if the iterators refer to the same element, true otherwise.
  28288. */
  28289. [[nodiscard]] bool operator!=(const meta_iterator &other) const ENTT_NOEXCEPT {
  28290. return !(*this == other);
  28291. }
  28292. /**
  28293. * @brief Indirection operator.
  28294. * @return The element to which the iterator points.
  28295. */
  28296. [[nodiscard]] reference operator*() const {
  28297. meta_any other;
  28298. vtable(operation::DEREF, handle, &other);
  28299. return other;
  28300. }
  28301. /**
  28302. * @brief Returns false if an iterator is invalid, true otherwise.
  28303. * @return False if the iterator is invalid, true otherwise.
  28304. */
  28305. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  28306. return static_cast<bool>(handle);
  28307. }
  28308. private:
  28309. vtable_type *vtable{};
  28310. any handle{};
  28311. };
  28312. template<typename Type>
  28313. struct meta_sequence_container::meta_sequence_container_proxy {
  28314. using traits_type = meta_sequence_container_traits<Type>;
  28315. [[nodiscard]] static meta_type value_type() ENTT_NOEXCEPT {
  28316. return internal::meta_info<typename Type::value_type>::resolve();
  28317. }
  28318. [[nodiscard]] static size_type size(const any &container) ENTT_NOEXCEPT {
  28319. return traits_type::size(any_cast<const Type &>(container));
  28320. }
  28321. [[nodiscard]] static bool resize(any &container, size_type sz) {
  28322. auto * const cont = any_cast<Type>(&container);
  28323. return cont && traits_type::resize(*cont, sz);
  28324. }
  28325. [[nodiscard]] static bool clear(any &container) {
  28326. auto * const cont = any_cast<Type>(&container);
  28327. return cont && traits_type::clear(*cont);
  28328. }
  28329. [[nodiscard]] static iterator begin(any &container) {
  28330. if(auto * const cont = any_cast<Type>(&container); cont) {
  28331. return iterator{traits_type::begin(*cont)};
  28332. }
  28333. return iterator{traits_type::cbegin(any_cast<const Type &>(container))};
  28334. }
  28335. [[nodiscard]] static iterator end(any &container) {
  28336. if(auto * const cont = any_cast<Type>(&container); cont) {
  28337. return iterator{traits_type::end(*cont)};
  28338. }
  28339. return iterator{traits_type::cend(any_cast<const Type &>(container))};
  28340. }
  28341. [[nodiscard]] static std::pair<iterator, bool> insert(any &container, iterator it, meta_any &value) {
  28342. if(auto * const cont = any_cast<Type>(&container); cont) {
  28343. // this abomination is necessary because only on macos value_type and const_reference are different types for std::vector<bool>
  28344. if(value.allow_cast<typename Type::const_reference>() || value.allow_cast<typename Type::value_type>()) {
  28345. const auto *element = value.try_cast<std::remove_reference_t<typename Type::const_reference>>();
  28346. auto ret = traits_type::insert(*cont, any_cast<const typename Type::iterator &>(it.handle), element ? *element : value.cast<typename Type::value_type>());
  28347. return { iterator{std::move(ret.first)}, ret.second };
  28348. }
  28349. }
  28350. return {};
  28351. }
  28352. [[nodiscard]] static std::pair<iterator, bool> erase(any &container, iterator it) {
  28353. if(auto * const cont = any_cast<Type>(&container); cont) {
  28354. auto ret = traits_type::erase(*cont, any_cast<const typename Type::iterator &>(it.handle));
  28355. return { iterator{std::move(ret.first)}, ret.second };
  28356. }
  28357. return {};
  28358. }
  28359. [[nodiscard]] static meta_any get(any &container, size_type pos) {
  28360. if(auto * const cont = any_cast<Type>(&container); cont) {
  28361. return meta_any{std::in_place_type<typename Type::reference>, traits_type::get(*cont, pos)};
  28362. }
  28363. return meta_any{std::in_place_type<typename Type::const_reference>, traits_type::cget(any_cast<const Type &>(container), pos)};
  28364. }
  28365. };
  28366. /**
  28367. * @brief Returns the meta value type of a container.
  28368. * @return The meta value type of the container.
  28369. */
  28370. [[nodiscard]] inline meta_type meta_sequence_container::value_type() const ENTT_NOEXCEPT {
  28371. return value_type_fn();
  28372. }
  28373. /**
  28374. * @brief Returns the size of a container.
  28375. * @return The size of the container.
  28376. */
  28377. [[nodiscard]] inline meta_sequence_container::size_type meta_sequence_container::size() const ENTT_NOEXCEPT {
  28378. return size_fn(storage);
  28379. }
  28380. /**
  28381. * @brief Resizes a container to contain a given number of elements.
  28382. * @param sz The new size of the container.
  28383. * @return True in case of success, false otherwise.
  28384. */
  28385. inline bool meta_sequence_container::resize(size_type sz) {
  28386. return resize_fn(storage, sz);
  28387. }
  28388. /**
  28389. * @brief Clears the content of a container.
  28390. * @return True in case of success, false otherwise.
  28391. */
  28392. inline bool meta_sequence_container::clear() {
  28393. return clear_fn(storage);
  28394. }
  28395. /**
  28396. * @brief Returns an iterator to the first element of a container.
  28397. * @return An iterator to the first element of the container.
  28398. */
  28399. [[nodiscard]] inline meta_sequence_container::iterator meta_sequence_container::begin() {
  28400. return begin_fn(storage);
  28401. }
  28402. /**
  28403. * @brief Returns an iterator that is past the last element of a container.
  28404. * @return An iterator that is past the last element of the container.
  28405. */
  28406. [[nodiscard]] inline meta_sequence_container::iterator meta_sequence_container::end() {
  28407. return end_fn(storage);
  28408. }
  28409. /**
  28410. * @brief Inserts an element at a specified location of a container.
  28411. * @param it Iterator before which the element will be inserted.
  28412. * @param value Element value to insert.
  28413. * @return A pair consisting of an iterator to the inserted element (in case of
  28414. * success) and a bool denoting whether the insertion took place.
  28415. */
  28416. inline std::pair<meta_sequence_container::iterator, bool> meta_sequence_container::insert(iterator it, meta_any value) {
  28417. return insert_fn(storage, it, value);
  28418. }
  28419. /**
  28420. * @brief Removes a given element from a container.
  28421. * @param it Iterator to the element to remove.
  28422. * @return A pair consisting of an iterator following the last removed element
  28423. * (in case of success) and a bool denoting whether the insertion took place.
  28424. */
  28425. inline std::pair<meta_sequence_container::iterator, bool> meta_sequence_container::erase(iterator it) {
  28426. return erase_fn(storage, it);
  28427. }
  28428. /**
  28429. * @brief Returns a reference to the element at a given location of a container
  28430. * (no bounds checking is performed).
  28431. * @param pos The position of the element to return.
  28432. * @return A reference to the requested element properly wrapped.
  28433. */
  28434. [[nodiscard]] inline meta_any meta_sequence_container::operator[](size_type pos) {
  28435. return get_fn(storage, pos);
  28436. }
  28437. /**
  28438. * @brief Returns false if a proxy is invalid, true otherwise.
  28439. * @return False if the proxy is invalid, true otherwise.
  28440. */
  28441. [[nodiscard]] inline meta_sequence_container::operator bool() const ENTT_NOEXCEPT {
  28442. return static_cast<bool>(storage);
  28443. }
  28444. /*! @brief Opaque iterator for associative containers. */
  28445. class meta_associative_container::meta_iterator {
  28446. enum operation { INCR, DEREF };
  28447. using vtable_type = void(const operation, const any &, void *);
  28448. template<bool KeyOnly, typename It>
  28449. static void basic_vtable(const operation op, const any &from, void *to) {
  28450. switch(op) {
  28451. case operation::INCR:
  28452. ++any_cast<It &>(const_cast<any &>(from));
  28453. break;
  28454. case operation::DEREF:
  28455. const auto &it = any_cast<const It &>(from);
  28456. if constexpr(KeyOnly) {
  28457. static_cast<std::pair<meta_any, meta_any> *>(to)->first.emplace<decltype(*it)>(*it);
  28458. } else {
  28459. static_cast<std::pair<meta_any, meta_any> *>(to)->first.emplace<decltype((it->first))>(it->first);
  28460. static_cast<std::pair<meta_any, meta_any> *>(to)->second.emplace<decltype((it->second))>(it->second);
  28461. }
  28462. break;
  28463. }
  28464. }
  28465. public:
  28466. /*! @brief Signed integer type. */
  28467. using difference_type = std::ptrdiff_t;
  28468. /*! @brief Type of elements returned by the iterator. */
  28469. using value_type = std::pair<meta_any, meta_any>;
  28470. /*! @brief Pointer type, `void` on purpose. */
  28471. using pointer = void;
  28472. /*! @brief Reference type, it is **not** an actual reference. */
  28473. using reference = value_type;
  28474. /*! @brief Iterator category. */
  28475. using iterator_category = std::input_iterator_tag;
  28476. /*! @brief Default constructor. */
  28477. meta_iterator() ENTT_NOEXCEPT = default;
  28478. /**
  28479. * @brief Constructs an meta iterator from a given iterator.
  28480. * @tparam KeyOnly True if the container is also key-only, false otherwise.
  28481. * @tparam It Type of actual iterator with which to build the meta iterator.
  28482. * @param iter The actual iterator with which to build the meta iterator.
  28483. */
  28484. template<bool KeyOnly, typename It>
  28485. meta_iterator(std::integral_constant<bool, KeyOnly>, It iter)
  28486. : vtable{&basic_vtable<KeyOnly, It>},
  28487. handle{std::move(iter)}
  28488. {}
  28489. /*! @brief Pre-increment operator. @return This iterator. */
  28490. meta_iterator & operator++() ENTT_NOEXCEPT {
  28491. return vtable(operation::INCR, handle, nullptr), *this;
  28492. }
  28493. /*! @brief Post-increment operator. @return This iterator. */
  28494. meta_iterator operator++(int) ENTT_NOEXCEPT {
  28495. meta_iterator orig = *this;
  28496. return ++(*this), orig;
  28497. }
  28498. /**
  28499. * @brief Checks if two iterators refer to the same element.
  28500. * @param other The iterator with which to compare.
  28501. * @return True if the iterators refer to the same element, false otherwise.
  28502. */
  28503. [[nodiscard]] bool operator==(const meta_iterator &other) const ENTT_NOEXCEPT {
  28504. return handle == other.handle;
  28505. }
  28506. /**
  28507. * @brief Checks if two iterators refer to the same element.
  28508. * @param other The iterator with which to compare.
  28509. * @return False if the iterators refer to the same element, true otherwise.
  28510. */
  28511. [[nodiscard]] bool operator!=(const meta_iterator &other) const ENTT_NOEXCEPT {
  28512. return !(*this == other);
  28513. }
  28514. /**
  28515. * @brief Indirection operator.
  28516. * @return The element to which the iterator points.
  28517. */
  28518. [[nodiscard]] reference operator*() const {
  28519. reference other;
  28520. vtable(operation::DEREF, handle, &other);
  28521. return other;
  28522. }
  28523. /**
  28524. * @brief Returns false if an iterator is invalid, true otherwise.
  28525. * @return False if the iterator is invalid, true otherwise.
  28526. */
  28527. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  28528. return static_cast<bool>(handle);
  28529. }
  28530. private:
  28531. vtable_type *vtable{};
  28532. any handle{};
  28533. };
  28534. template<typename Type>
  28535. struct meta_associative_container::meta_associative_container_proxy {
  28536. using traits_type = meta_associative_container_traits<Type>;
  28537. [[nodiscard]] static meta_type key_type() ENTT_NOEXCEPT {
  28538. return internal::meta_info<typename Type::key_type>::resolve();
  28539. }
  28540. [[nodiscard]] static meta_type mapped_type() ENTT_NOEXCEPT {
  28541. if constexpr(is_key_only_meta_associative_container_v<Type>) {
  28542. return meta_type{};
  28543. } else {
  28544. return internal::meta_info<typename Type::mapped_type>::resolve();
  28545. }
  28546. }
  28547. [[nodiscard]] static meta_type value_type() ENTT_NOEXCEPT {
  28548. return internal::meta_info<typename Type::value_type>::resolve();
  28549. }
  28550. [[nodiscard]] static size_type size(const any &container) ENTT_NOEXCEPT {
  28551. return traits_type::size(any_cast<const Type &>(container));
  28552. }
  28553. [[nodiscard]] static bool clear(any &container) {
  28554. auto * const cont = any_cast<Type>(&container);
  28555. return cont && traits_type::clear(*cont);
  28556. }
  28557. [[nodiscard]] static iterator begin(any &container) {
  28558. if(auto * const cont = any_cast<Type>(&container); cont) {
  28559. return iterator{is_key_only_meta_associative_container<Type>{}, traits_type::begin(*cont)};
  28560. }
  28561. return iterator{is_key_only_meta_associative_container<Type>{}, traits_type::cbegin(any_cast<const Type &>(container))};
  28562. }
  28563. [[nodiscard]] static iterator end(any &container) {
  28564. if(auto * const cont = any_cast<Type>(&container); cont) {
  28565. return iterator{is_key_only_meta_associative_container<Type>{}, traits_type::end(*cont)};
  28566. }
  28567. return iterator{is_key_only_meta_associative_container<Type>{}, traits_type::cend(any_cast<const Type &>(container))};
  28568. }
  28569. [[nodiscard]] static bool insert(any &container, meta_any &key, meta_any &value) {
  28570. if(auto * const cont = any_cast<Type>(&container); cont && key.allow_cast<const typename Type::key_type &>()) {
  28571. if constexpr(is_key_only_meta_associative_container_v<Type>) {
  28572. return traits_type::insert(*cont, key.cast<const typename Type::key_type &>());
  28573. } else {
  28574. return value.allow_cast<const typename Type::mapped_type &>()
  28575. && traits_type::insert(*cont, key.cast<const typename Type::key_type &>(), value.cast<const typename Type::mapped_type &>());
  28576. }
  28577. }
  28578. return false;
  28579. }
  28580. [[nodiscard]] static bool erase(any &container, meta_any &key) {
  28581. if(auto * const cont = any_cast<Type>(&container); cont && key.allow_cast<const typename Type::key_type &>()) {
  28582. return traits_type::erase(*cont, key.cast<const typename Type::key_type &>());
  28583. }
  28584. return false;
  28585. }
  28586. [[nodiscard]] static iterator find(any &container, meta_any &key) {
  28587. if(key.allow_cast<const typename Type::key_type &>()) {
  28588. if(auto * const cont = any_cast<Type>(&container); cont) {
  28589. return iterator{is_key_only_meta_associative_container<Type>{}, traits_type::find(*cont, key.cast<const typename Type::key_type &>())};
  28590. }
  28591. return iterator{is_key_only_meta_associative_container<Type>{}, traits_type::cfind(any_cast<const Type &>(container), key.cast<const typename Type::key_type &>())};
  28592. }
  28593. return {};
  28594. }
  28595. };
  28596. /**
  28597. * @brief Returns true if a container is also key-only, false otherwise.
  28598. * @return True if the associative container is also key-only, false otherwise.
  28599. */
  28600. [[nodiscard]] inline bool meta_associative_container::key_only() const ENTT_NOEXCEPT {
  28601. return key_only_container;
  28602. }
  28603. /**
  28604. * @brief Returns the meta key type of a container.
  28605. * @return The meta key type of the a container.
  28606. */
  28607. [[nodiscard]] inline meta_type meta_associative_container::key_type() const ENTT_NOEXCEPT {
  28608. return key_type_fn();
  28609. }
  28610. /**
  28611. * @brief Returns the meta mapped type of a container.
  28612. * @return The meta mapped type of the a container.
  28613. */
  28614. [[nodiscard]] inline meta_type meta_associative_container::mapped_type() const ENTT_NOEXCEPT {
  28615. return mapped_type_fn();
  28616. }
  28617. /*! @copydoc meta_sequence_container::value_type */
  28618. [[nodiscard]] inline meta_type meta_associative_container::value_type() const ENTT_NOEXCEPT {
  28619. return value_type_fn();
  28620. }
  28621. /*! @copydoc meta_sequence_container::size */
  28622. [[nodiscard]] inline meta_associative_container::size_type meta_associative_container::size() const ENTT_NOEXCEPT {
  28623. return size_fn(storage);
  28624. }
  28625. /*! @copydoc meta_sequence_container::clear */
  28626. inline bool meta_associative_container::clear() {
  28627. return clear_fn(storage);
  28628. }
  28629. /*! @copydoc meta_sequence_container::begin */
  28630. [[nodiscard]] inline meta_associative_container::iterator meta_associative_container::begin() {
  28631. return begin_fn(storage);
  28632. }
  28633. /*! @copydoc meta_sequence_container::end */
  28634. [[nodiscard]] inline meta_associative_container::iterator meta_associative_container::end() {
  28635. return end_fn(storage);
  28636. }
  28637. /**
  28638. * @brief Inserts an element (a key/value pair) into a container.
  28639. * @param key The key of the element to insert.
  28640. * @param value The value of the element to insert.
  28641. * @return A bool denoting whether the insertion took place.
  28642. */
  28643. inline bool meta_associative_container::insert(meta_any key, meta_any value = {}) {
  28644. return insert_fn(storage, key, value);
  28645. }
  28646. /**
  28647. * @brief Removes the specified element from a container.
  28648. * @param key The key of the element to remove.
  28649. * @return A bool denoting whether the removal took place.
  28650. */
  28651. inline bool meta_associative_container::erase(meta_any key) {
  28652. return erase_fn(storage, key);
  28653. }
  28654. /**
  28655. * @brief Returns an iterator to the element with a given key, if any.
  28656. * @param key The key of the element to search.
  28657. * @return An iterator to the element with the given key, if any.
  28658. */
  28659. [[nodiscard]] inline meta_associative_container::iterator meta_associative_container::find(meta_any key) {
  28660. return find_fn(storage, key);
  28661. }
  28662. /**
  28663. * @brief Returns false if a proxy is invalid, true otherwise.
  28664. * @return False if the proxy is invalid, true otherwise.
  28665. */
  28666. [[nodiscard]] inline meta_associative_container::operator bool() const ENTT_NOEXCEPT {
  28667. return static_cast<bool>(storage);
  28668. }
  28669. }
  28670. #endif
  28671. // #include "node.hpp"
  28672. // #include "policy.hpp"
  28673. #ifndef ENTT_META_POLICY_HPP
  28674. #define ENTT_META_POLICY_HPP
  28675. namespace entt {
  28676. /*! @brief Empty class type used to request the _as ref_ policy. */
  28677. struct as_ref_t {};
  28678. /*! @brief Empty class type used to request the _as cref_ policy. */
  28679. struct as_cref_t {};
  28680. /*! @brief Empty class type used to request the _as-is_ policy. */
  28681. struct as_is_t {};
  28682. /*! @brief Empty class type used to request the _as void_ policy. */
  28683. struct as_void_t {};
  28684. }
  28685. #endif
  28686. // #include "utility.hpp"
  28687. #ifndef ENTT_META_UTILITY_HPP
  28688. #define ENTT_META_UTILITY_HPP
  28689. #include <cstddef>
  28690. #include <functional>
  28691. #include <type_traits>
  28692. #include <utility>
  28693. // #include "../config/config.h"
  28694. // #include "../core/type_traits.hpp"
  28695. // #include "meta.hpp"
  28696. // #include "node.hpp"
  28697. // #include "policy.hpp"
  28698. namespace entt {
  28699. /*! @brief Primary template isn't defined on purpose. */
  28700. template<typename, typename>
  28701. struct meta_function_descriptor;
  28702. /**
  28703. * @brief Meta function descriptor.
  28704. * @tparam Type Reflected type to which the meta function is associated.
  28705. * @tparam Ret Function return type.
  28706. * @tparam Class Actual owner of the member function.
  28707. * @tparam Args Function arguments.
  28708. */
  28709. template<typename Type, typename Ret, typename Class, typename... Args>
  28710. struct meta_function_descriptor<Type, Ret(Class:: *)(Args...) const> {
  28711. /*! @brief Meta function return type. */
  28712. using return_type = Ret;
  28713. /*! @brief Meta function arguments. */
  28714. using args_type = std::conditional_t<std::is_same_v<Type, Class>, type_list<Args...>, type_list<const Class &, Args...>>;
  28715. /*! @brief True if the meta function is const, false otherwise. */
  28716. static constexpr auto is_const = true;
  28717. /*! @brief True if the meta function is static, false otherwise. */
  28718. static constexpr auto is_static = !std::is_same_v<Type, Class>;
  28719. };
  28720. /**
  28721. * @brief Meta function descriptor.
  28722. * @tparam Type Reflected type to which the meta function is associated.
  28723. * @tparam Ret Function return type.
  28724. * @tparam Class Actual owner of the member function.
  28725. * @tparam Args Function arguments.
  28726. */
  28727. template<typename Type, typename Ret, typename Class, typename... Args>
  28728. struct meta_function_descriptor<Type, Ret(Class:: *)(Args...)> {
  28729. /*! @brief Meta function return type. */
  28730. using return_type = Ret;
  28731. /*! @brief Meta function arguments. */
  28732. using args_type = std::conditional_t<std::is_same_v<Type, Class>, type_list<Args...>, type_list<Class &, Args...>>;
  28733. /*! @brief True if the meta function is const, false otherwise. */
  28734. static constexpr auto is_const = false;
  28735. /*! @brief True if the meta function is static, false otherwise. */
  28736. static constexpr auto is_static = !std::is_same_v<Type, Class>;
  28737. };
  28738. /**
  28739. * @brief Meta function descriptor.
  28740. * @tparam Type Reflected type to which the meta function is associated.
  28741. * @tparam Ret Function return type.
  28742. * @tparam Args Function arguments.
  28743. */
  28744. template<typename Type, typename Ret, typename... Args>
  28745. struct meta_function_descriptor<Type, Ret(*)(Args...)> {
  28746. /*! @brief Meta function return type. */
  28747. using return_type = Ret;
  28748. /*! @brief Meta function arguments. */
  28749. using args_type = type_list<Args...>;
  28750. /*! @brief True if the meta function is const, false otherwise. */
  28751. static constexpr auto is_const = false;
  28752. /*! @brief True if the meta function is static, false otherwise. */
  28753. static constexpr auto is_static = true;
  28754. };
  28755. /**
  28756. * @brief Meta function helper.
  28757. *
  28758. * Converts a function type to be associated with a reflected type into its meta
  28759. * function descriptor.
  28760. *
  28761. * @tparam Type Reflected type to which the meta function is associated.
  28762. * @tparam Candidate The actual function to associate with the reflected type.
  28763. */
  28764. template<typename Type, typename Candidate>
  28765. class meta_function_helper {
  28766. template<typename Ret, typename... Args, typename Class>
  28767. static constexpr meta_function_descriptor<Type, Ret(Class:: *)(Args...) const> get_rid_of_noexcept(Ret(Class:: *)(Args...) const);
  28768. template<typename Ret, typename... Args, typename Class>
  28769. static constexpr meta_function_descriptor<Type, Ret(Class:: *)(Args...)> get_rid_of_noexcept(Ret(Class:: *)(Args...));
  28770. template<typename Ret, typename... Args>
  28771. static constexpr meta_function_descriptor<Type, Ret(*)(Args...)> get_rid_of_noexcept(Ret(*)(Args...));
  28772. public:
  28773. /*! @brief The meta function descriptor of the given function. */
  28774. using type = decltype(get_rid_of_noexcept(std::declval<Candidate>()));
  28775. };
  28776. /**
  28777. * @brief Helper type.
  28778. * @tparam Type Reflected type to which the meta function is associated.
  28779. * @tparam Candidate The actual function to associate with the reflected type.
  28780. */
  28781. template<typename Type, typename Candidate>
  28782. using meta_function_helper_t = typename meta_function_helper<Type, Candidate>::type;
  28783. /**
  28784. * @brief Returns the meta type of the i-th element of a list of arguments.
  28785. * @tparam Args Actual types of arguments.
  28786. * @return The meta type of the i-th element of the list of arguments.
  28787. */
  28788. template<typename... Args>
  28789. [[nodiscard]] static meta_type meta_arg(type_list<Args...>, const std::size_t index) ENTT_NOEXCEPT {
  28790. return internal::meta_arg_node(type_list<Args...>{}, index);
  28791. }
  28792. /**
  28793. * @brief Constructs an instance given a list of erased parameters, if possible.
  28794. * @tparam Type Actual type of the instance to construct.
  28795. * @tparam Args Types of arguments expected.
  28796. * @tparam Index Indexes to use to extract erased arguments from their list.
  28797. * @param args Parameters to use to construct the instance.
  28798. * @return A meta any containing the new instance, if any.
  28799. */
  28800. template<typename Type, typename... Args, std::size_t... Index>
  28801. [[nodiscard]] meta_any meta_construct(meta_any * const args, std::index_sequence<Index...>) {
  28802. if(((args+Index)->allow_cast<Args>() && ...)) {
  28803. return Type{(args+Index)->cast<Args>()...};
  28804. }
  28805. return {};
  28806. }
  28807. /**
  28808. * @brief Sets the value of a given variable.
  28809. * @tparam Type Reflected type to which the variable is associated.
  28810. * @tparam Data The actual variable to set.
  28811. * @param instance An opaque instance of the underlying type, if required.
  28812. * @param value Parameter to use to set the variable.
  28813. * @return True in case of success, false otherwise.
  28814. */
  28815. template<typename Type, auto Data>
  28816. [[nodiscard]] bool meta_setter([[maybe_unused]] meta_handle instance, [[maybe_unused]] meta_any value) {
  28817. if constexpr(!std::is_same_v<decltype(Data), Type> && !std::is_same_v<decltype(Data), std::nullptr_t>) {
  28818. if constexpr(std::is_function_v<std::remove_reference_t<std::remove_pointer_t<decltype(Data)>>>) {
  28819. using data_type = type_list_element_t<1u, typename meta_function_helper_t<Type, decltype(Data)>::args_type>;
  28820. if(auto * const clazz = instance->try_cast<Type>(); clazz && value.allow_cast<data_type>()) {
  28821. Data(*clazz, value.cast<data_type>());
  28822. return true;
  28823. }
  28824. } else if constexpr(std::is_member_function_pointer_v<decltype(Data)>) {
  28825. using data_type = type_list_element_t<0u, typename meta_function_helper_t<Type, decltype(Data)>::args_type>;
  28826. if(auto * const clazz = instance->try_cast<Type>(); clazz && value.allow_cast<data_type>()) {
  28827. (clazz->*Data)(value.cast<data_type>());
  28828. return true;
  28829. }
  28830. } else if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
  28831. using data_type = std::remove_reference_t<decltype(std::declval<Type>().*Data)>;
  28832. if constexpr(!std::is_array_v<data_type> && !std::is_const_v<data_type>) {
  28833. if(auto * const clazz = instance->try_cast<Type>(); clazz && value.allow_cast<data_type>()) {
  28834. clazz->*Data = value.cast<data_type>();
  28835. return true;
  28836. }
  28837. }
  28838. } else {
  28839. using data_type = std::remove_reference_t<decltype(*Data)>;
  28840. if constexpr(!std::is_array_v<data_type> && !std::is_const_v<data_type>) {
  28841. if(value.allow_cast<data_type>()) {
  28842. *Data = value.cast<data_type>();
  28843. return true;
  28844. }
  28845. }
  28846. }
  28847. }
  28848. return false;
  28849. }
  28850. /**
  28851. * @brief Wraps a value depending on the given policy.
  28852. * @tparam Policy Optional policy (no policy set by default).
  28853. * @tparam Type Type of value to wrap.
  28854. * @param value Value to wrap.
  28855. * @return A meta any containing the returned value.
  28856. */
  28857. template<typename Policy = as_is_t, typename Type>
  28858. meta_any meta_dispatch(Type &&value) {
  28859. if constexpr(std::is_same_v<Policy, as_void_t>) {
  28860. return meta_any{std::in_place_type<void>, std::forward<Type>(value)};
  28861. } else if constexpr(std::is_same_v<Policy, as_ref_t>) {
  28862. return meta_any{std::in_place_type<Type>, std::forward<Type>(value)};
  28863. } else if constexpr(std::is_same_v<Policy, as_cref_t>) {
  28864. static_assert(std::is_lvalue_reference_v<Type>, "Invalid type");
  28865. return meta_any{std::in_place_type<const std::remove_reference_t<Type> &>, std::as_const(value)};
  28866. } else {
  28867. static_assert(std::is_same_v<Policy, as_is_t>, "Policy not supported");
  28868. return meta_any{std::forward<Type>(value)};
  28869. }
  28870. }
  28871. /**
  28872. * @brief Gets the value of a given variable.
  28873. * @tparam Type Reflected type to which the variable is associated.
  28874. * @tparam Data The actual variable to get.
  28875. * @tparam Policy Optional policy (no policy set by default).
  28876. * @param instance An opaque instance of the underlying type, if required.
  28877. * @return A meta any containing the value of the underlying variable.
  28878. */
  28879. template<typename Type, auto Data, typename Policy = as_is_t>
  28880. [[nodiscard]] meta_any meta_getter([[maybe_unused]] meta_handle instance) {
  28881. if constexpr(std::is_function_v<std::remove_reference_t<std::remove_pointer_t<decltype(Data)>>>) {
  28882. auto * const clazz = instance->try_cast<std::conditional_t<std::is_invocable_v<decltype(Data), const Type &>, const Type, Type>>();
  28883. return clazz ? meta_dispatch<Policy>(Data(*clazz)) : meta_any{};
  28884. } else if constexpr(std::is_member_function_pointer_v<decltype(Data)>) {
  28885. auto * const clazz = instance->try_cast<std::conditional_t<std::is_invocable_v<decltype(Data), const Type &>, const Type, Type>>();
  28886. return clazz ? meta_dispatch<Policy>((clazz->*Data)()) : meta_any{};
  28887. } else if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
  28888. if constexpr(!std::is_array_v<std::remove_cv_t<std::remove_reference_t<decltype(std::declval<Type>().*Data)>>>) {
  28889. if(auto * clazz = instance->try_cast<Type>(); clazz) {
  28890. return meta_dispatch<Policy>(clazz->*Data);
  28891. } else if(auto * fallback = instance->try_cast<const Type>(); fallback) {
  28892. return meta_dispatch<Policy>(fallback->*Data);
  28893. }
  28894. }
  28895. return meta_any{};
  28896. } else if constexpr(std::is_pointer_v<decltype(Data)>) {
  28897. if constexpr(std::is_array_v<std::remove_pointer_t<decltype(Data)>>) {
  28898. return meta_any{};
  28899. } else {
  28900. return meta_dispatch<Policy>(*Data);
  28901. }
  28902. } else {
  28903. return meta_dispatch<Policy>(Data);
  28904. }
  28905. }
  28906. /**
  28907. * @brief Invokes a function given a list of erased parameters, if possible.
  28908. * @tparam Type Reflected type to which the function is associated.
  28909. * @tparam Candidate The actual function to invoke.
  28910. * @tparam Policy Optional policy (no policy set by default).
  28911. * @tparam Index Indexes to use to extract erased arguments from their list.
  28912. * @param instance An opaque instance of the underlying type, if required.
  28913. * @param args Parameters to use to invoke the function.
  28914. * @return A meta any containing the returned value, if any.
  28915. */
  28916. template<typename Type, auto Candidate, typename Policy = as_is_t, std::size_t... Index>
  28917. [[nodiscard]] std::enable_if_t<!std::is_invocable_v<decltype(Candidate)>, meta_any> meta_invoke([[maybe_unused]] meta_handle instance, meta_any *args, std::index_sequence<Index...>) {
  28918. using descriptor = meta_function_helper_t<Type, decltype(Candidate)>;
  28919. const auto invoke = [](auto &&maybe_clazz, auto &&... other) {
  28920. if constexpr(std::is_member_function_pointer_v<decltype(Candidate)>) {
  28921. if constexpr(std::is_void_v<typename descriptor::return_type>) {
  28922. (std::forward<decltype(maybe_clazz)>(maybe_clazz).*Candidate)(std::forward<decltype(other)>(other)...);
  28923. return meta_any{std::in_place_type<void>};
  28924. } else {
  28925. return meta_dispatch<Policy>((std::forward<decltype(maybe_clazz)>(maybe_clazz).*Candidate)(std::forward<decltype(other)>(other)...));
  28926. }
  28927. } else {
  28928. if constexpr(std::is_void_v<typename descriptor::return_type>) {
  28929. Candidate(std::forward<decltype(maybe_clazz)>(maybe_clazz), std::forward<decltype(other)>(other)...);
  28930. return meta_any{std::in_place_type<void>};
  28931. } else {
  28932. return meta_dispatch<Policy>(Candidate(std::forward<decltype(maybe_clazz)>(maybe_clazz), std::forward<decltype(other)>(other)...));
  28933. }
  28934. }
  28935. };
  28936. if constexpr(std::is_invocable_v<decltype(Candidate), const Type &, type_list_element_t<Index, typename descriptor::args_type>...>) {
  28937. if(const auto * const clazz = instance->try_cast<const Type>(); clazz && ((args+Index)->allow_cast<type_list_element_t<Index, typename descriptor::args_type>>() && ...)) {
  28938. return invoke(*clazz, (args+Index)->cast<type_list_element_t<Index, typename descriptor::args_type>>()...);
  28939. }
  28940. } else if constexpr(std::is_invocable_v<decltype(Candidate), Type &, type_list_element_t<Index, typename descriptor::args_type>...>) {
  28941. if(auto * const clazz = instance->try_cast<Type>(); clazz && ((args+Index)->allow_cast<type_list_element_t<Index, typename descriptor::args_type>>() && ...)) {
  28942. return invoke(*clazz, (args+Index)->cast<type_list_element_t<Index, typename descriptor::args_type>>()...);
  28943. }
  28944. } else {
  28945. if(((args+Index)->allow_cast<type_list_element_t<Index, typename descriptor::args_type>>() && ...)) {
  28946. return invoke((args+Index)->cast<type_list_element_t<Index, typename descriptor::args_type>>()...);
  28947. }
  28948. }
  28949. return meta_any{};
  28950. }
  28951. /**
  28952. * @brief Invokes a function given a list of erased parameters, if possible.
  28953. * @tparam Type Reflected type to which the function is associated.
  28954. * @tparam Candidate The actual function to invoke.
  28955. * @tparam Policy Optional policy (no policy set by default).
  28956. * @tparam Index Indexes to use to extract erased arguments from their list.
  28957. * @return A meta any containing the returned value, if any.
  28958. */
  28959. template<typename Type, auto Candidate, typename Policy = as_is_t, std::size_t... Index>
  28960. [[nodiscard]] std::enable_if_t<std::is_invocable_v<decltype(Candidate)>, meta_any> meta_invoke(meta_handle, meta_any *, std::index_sequence<Index...>) {
  28961. if constexpr(std::is_void_v<decltype(Candidate())>) {
  28962. Candidate();
  28963. return meta_any{std::in_place_type<void>};
  28964. } else {
  28965. return meta_dispatch<Policy>(Candidate());
  28966. }
  28967. }
  28968. }
  28969. #endif
  28970. namespace entt {
  28971. /**
  28972. * @cond TURN_OFF_DOXYGEN
  28973. * Internal details not to be documented.
  28974. */
  28975. namespace internal {
  28976. template<typename Node>
  28977. [[nodiscard]] bool find_if(const Node *candidate, const Node *node) ENTT_NOEXCEPT {
  28978. return node && (node == candidate || find_if(candidate, node->next));
  28979. }
  28980. template<typename Id, typename Node>
  28981. [[nodiscard]] bool find_if_not(const Id id, Node *node, const Node *owner) ENTT_NOEXCEPT {
  28982. if constexpr(std::is_pointer_v<Id>) {
  28983. return node && ((*node->id == *id && node != owner) || find_if_not(id, node->next, owner));
  28984. } else {
  28985. return node && ((node->id == id && node != owner) || find_if_not(id, node->next, owner));
  28986. }
  28987. }
  28988. }
  28989. /**
  28990. * Internal details not to be documented.
  28991. * @endcond
  28992. */
  28993. /**
  28994. * @brief Meta factory to be used for reflection purposes.
  28995. *
  28996. * The meta factory is an utility class used to reflect types, data members and
  28997. * functions of all sorts. This class ensures that the underlying web of types
  28998. * is built correctly and performs some checks in debug mode to ensure that
  28999. * there are no subtle errors at runtime.
  29000. */
  29001. template<typename...>
  29002. struct meta_factory;
  29003. /**
  29004. * @brief Extended meta factory to be used for reflection purposes.
  29005. * @tparam Type Reflected type for which the factory was created.
  29006. * @tparam Spec Property specialization pack used to disambiguate overloads.
  29007. */
  29008. template<typename Type, typename... Spec>
  29009. struct meta_factory<Type, Spec...>: public meta_factory<Type> {
  29010. private:
  29011. template<std::size_t Step = 0, std::size_t... Index, typename... Property, typename... Other>
  29012. void unpack(std::index_sequence<Index...>, std::tuple<Property...> property, Other &&... other) {
  29013. unroll<Step>(choice<3>, std::move(std::get<Index>(property))..., std::forward<Other>(other)...);
  29014. }
  29015. template<std::size_t Step = 0, typename... Property, typename... Other>
  29016. void unroll(choice_t<3>, std::tuple<Property...> property, Other &&... other) {
  29017. unpack<Step>(std::index_sequence_for<Property...>{}, std::move(property), std::forward<Other>(other)...);
  29018. }
  29019. template<std::size_t Step = 0, typename... Property, typename... Other>
  29020. void unroll(choice_t<2>, std::pair<Property...> property, Other &&... other) {
  29021. assign<Step>(std::move(property.first), std::move(property.second));
  29022. unroll<Step+1>(choice<3>, std::forward<Other>(other)...);
  29023. }
  29024. template<std::size_t Step = 0, typename Property, typename... Other>
  29025. std::enable_if_t<!std::is_invocable_v<Property>>
  29026. unroll(choice_t<1>, Property &&property, Other &&... other) {
  29027. assign<Step>(std::forward<Property>(property));
  29028. unroll<Step+1>(choice<3>, std::forward<Other>(other)...);
  29029. }
  29030. template<std::size_t Step = 0, typename Func, typename... Other>
  29031. void unroll(choice_t<0>, Func &&invocable, Other &&... other) {
  29032. unroll<Step>(choice<3>, std::forward<Func>(invocable)(), std::forward<Other>(other)...);
  29033. }
  29034. template<std::size_t>
  29035. void unroll(choice_t<0>) {}
  29036. template<std::size_t = 0, typename Key>
  29037. void assign(Key &&key, meta_any value = {}) {
  29038. static meta_any property[2u]{};
  29039. static internal::meta_prop_node node{
  29040. nullptr,
  29041. property[0u],
  29042. property[1u]
  29043. };
  29044. entt::meta_any instance{std::forward<Key>(key)};
  29045. ENTT_ASSERT(!internal::find_if_not(&instance, *curr, &node), "Duplicate key");
  29046. property[0u] = std::move(instance);
  29047. property[1u] = std::move(value);
  29048. if(!internal::find_if(&node, *curr)) {
  29049. node.next = *curr;
  29050. *curr = &node;
  29051. }
  29052. }
  29053. public:
  29054. /**
  29055. * @brief Constructs an extended factory from a given node.
  29056. * @param target The underlying node to which to assign the properties.
  29057. */
  29058. meta_factory(internal::meta_prop_node **target) ENTT_NOEXCEPT
  29059. : curr{target}
  29060. {}
  29061. /**
  29062. * @brief Assigns a property to the last meta object created.
  29063. *
  29064. * Both the key and the value (if any) must be at least copy constructible.
  29065. *
  29066. * @tparam PropertyOrKey Type of the property or property key.
  29067. * @tparam Value Optional type of the property value.
  29068. * @param property_or_key Property or property key.
  29069. * @param value Optional property value.
  29070. * @return A meta factory for the parent type.
  29071. */
  29072. template<typename PropertyOrKey, typename... Value>
  29073. auto prop(PropertyOrKey &&property_or_key, Value &&... value) && {
  29074. if constexpr(sizeof...(Value) == 0) {
  29075. unroll(choice<3>, std::forward<PropertyOrKey>(property_or_key));
  29076. } else {
  29077. assign(std::forward<PropertyOrKey>(property_or_key), std::forward<Value>(value)...);
  29078. }
  29079. return meta_factory<Type, Spec..., PropertyOrKey, Value...>{curr};
  29080. }
  29081. /**
  29082. * @brief Assigns properties to the last meta object created.
  29083. *
  29084. * Both the keys and the values (if any) must be at least copy
  29085. * constructible.
  29086. *
  29087. * @tparam Property Types of the properties.
  29088. * @param property Properties to assign to the last meta object created.
  29089. * @return A meta factory for the parent type.
  29090. */
  29091. template <typename... Property>
  29092. auto props(Property... property) && {
  29093. unroll(choice<3>, std::forward<Property>(property)...);
  29094. return meta_factory<Type, Spec..., Property...>{curr};
  29095. }
  29096. private:
  29097. internal::meta_prop_node **curr;
  29098. };
  29099. /**
  29100. * @brief Basic meta factory to be used for reflection purposes.
  29101. * @tparam Type Reflected type for which the factory was created.
  29102. */
  29103. template<typename Type>
  29104. struct meta_factory<Type> {
  29105. /**
  29106. * @brief Makes a meta type _searchable_.
  29107. * @param id Optional unique identifier.
  29108. * @return An extended meta factory for the given type.
  29109. */
  29110. auto type(const id_type id = type_hash<Type>::value()) {
  29111. auto * const node = internal::meta_info<Type>::resolve();
  29112. ENTT_ASSERT(!internal::find_if_not(id, *internal::meta_context::global(), node), "Duplicate identifier");
  29113. node->id = id;
  29114. if(!internal::find_if(node, *internal::meta_context::global())) {
  29115. node->next = *internal::meta_context::global();
  29116. *internal::meta_context::global() = node;
  29117. }
  29118. return meta_factory<Type, Type>{&node->prop};
  29119. }
  29120. /**
  29121. * @brief Assigns a meta base to a meta type.
  29122. *
  29123. * A reflected base class must be a real base class of the reflected type.
  29124. *
  29125. * @tparam Base Type of the base class to assign to the meta type.
  29126. * @return A meta factory for the parent type.
  29127. */
  29128. template<typename Base>
  29129. auto base() ENTT_NOEXCEPT {
  29130. static_assert(std::is_base_of_v<Base, Type>, "Invalid base type");
  29131. auto * const type = internal::meta_info<Type>::resolve();
  29132. static internal::meta_base_node node{
  29133. type,
  29134. nullptr,
  29135. &internal::meta_info<Base>::resolve,
  29136. [](const void *instance) ENTT_NOEXCEPT -> const void * {
  29137. return static_cast<const Base *>(static_cast<const Type *>(instance));
  29138. }
  29139. };
  29140. if(!internal::find_if(&node, type->base)) {
  29141. node.next = type->base;
  29142. type->base = &node;
  29143. }
  29144. return meta_factory<Type>{};
  29145. }
  29146. /**
  29147. * @brief Assigns a meta conversion function to a meta type.
  29148. *
  29149. * Conversion functions can be either free functions or member
  29150. * functions.<br/>
  29151. * In case of free functions, they must accept a const reference to an
  29152. * instance of the parent type as an argument. In case of member functions,
  29153. * they should have no arguments at all.
  29154. *
  29155. * @tparam Candidate The actual function to use for the conversion.
  29156. * @return A meta factory for the parent type.
  29157. */
  29158. template<auto Candidate>
  29159. std::enable_if_t<std::is_member_function_pointer_v<decltype(Candidate)>, meta_factory<Type>> conv() ENTT_NOEXCEPT {
  29160. using conv_type = std::invoke_result_t<decltype(Candidate), Type &>;
  29161. auto * const type = internal::meta_info<Type>::resolve();
  29162. static internal::meta_conv_node node{
  29163. type,
  29164. nullptr,
  29165. &internal::meta_info<conv_type>::resolve,
  29166. [](const void *instance) -> meta_any {
  29167. return (static_cast<const Type *>(instance)->*Candidate)();
  29168. }
  29169. };
  29170. if(!internal::find_if(&node, type->conv)) {
  29171. node.next = type->conv;
  29172. type->conv = &node;
  29173. }
  29174. return meta_factory<Type>{};
  29175. }
  29176. /*! @copydoc conv */
  29177. template<auto Candidate>
  29178. std::enable_if_t<!std::is_member_function_pointer_v<decltype(Candidate)>, meta_factory<Type>> conv() ENTT_NOEXCEPT {
  29179. using conv_type = std::invoke_result_t<decltype(Candidate), Type &>;
  29180. auto * const type = internal::meta_info<Type>::resolve();
  29181. static internal::meta_conv_node node{
  29182. type,
  29183. nullptr,
  29184. &internal::meta_info<conv_type>::resolve,
  29185. [](const void *instance) -> meta_any {
  29186. return Candidate(*static_cast<const Type *>(instance));
  29187. }
  29188. };
  29189. if(!internal::find_if(&node, type->conv)) {
  29190. node.next = type->conv;
  29191. type->conv = &node;
  29192. }
  29193. return meta_factory<Type>{};
  29194. }
  29195. /**
  29196. * @brief Assigns a meta conversion function to a meta type.
  29197. *
  29198. * The given type must be such that an instance of the reflected type can be
  29199. * converted to it.
  29200. *
  29201. * @tparam To Type of the conversion function to assign to the meta type.
  29202. * @return A meta factory for the parent type.
  29203. */
  29204. template<typename To>
  29205. auto conv() ENTT_NOEXCEPT {
  29206. static_assert(std::is_convertible_v<Type, To>, "Could not convert to the required type");
  29207. auto * const type = internal::meta_info<Type>::resolve();
  29208. static internal::meta_conv_node node{
  29209. type,
  29210. nullptr,
  29211. &internal::meta_info<To>::resolve,
  29212. [](const void *instance) -> meta_any {
  29213. return static_cast<To>(*static_cast<const Type *>(instance));
  29214. }
  29215. };
  29216. if(!internal::find_if(&node, type->conv)) {
  29217. node.next = type->conv;
  29218. type->conv = &node;
  29219. }
  29220. return meta_factory<Type>{};
  29221. }
  29222. /**
  29223. * @brief Assigns a meta constructor to a meta type.
  29224. *
  29225. * Both member functions and free function can be assigned to meta types in
  29226. * the role of constructors. All that is required is that they return an
  29227. * instance of the underlying type.<br/>
  29228. * From a client's point of view, nothing changes if a constructor of a meta
  29229. * type is a built-in one or not.
  29230. *
  29231. * @tparam Candidate The actual function to use as a constructor.
  29232. * @tparam Policy Optional policy (no policy set by default).
  29233. * @return An extended meta factory for the parent type.
  29234. */
  29235. template<auto Candidate, typename Policy = as_is_t>
  29236. auto ctor() ENTT_NOEXCEPT {
  29237. using descriptor = meta_function_helper_t<Type, decltype(Candidate)>;
  29238. static_assert(std::is_same_v<std::decay_t<typename descriptor::return_type>, Type>, "The function doesn't return an object of the required type");
  29239. auto * const type = internal::meta_info<Type>::resolve();
  29240. static internal::meta_ctor_node node{
  29241. type,
  29242. nullptr,
  29243. nullptr,
  29244. descriptor::args_type::size,
  29245. [](const typename internal::meta_ctor_node::size_type index) ENTT_NOEXCEPT {
  29246. return meta_arg(typename descriptor::args_type{}, index);
  29247. },
  29248. [](meta_any * const args) {
  29249. return meta_invoke<Type, Candidate, Policy>({}, args, std::make_index_sequence<descriptor::args_type::size>{});
  29250. }
  29251. };
  29252. if(!internal::find_if(&node, type->ctor)) {
  29253. node.next = type->ctor;
  29254. type->ctor = &node;
  29255. }
  29256. return meta_factory<Type, std::integral_constant<decltype(Candidate), Candidate>>{&node.prop};
  29257. }
  29258. /**
  29259. * @brief Assigns a meta constructor to a meta type.
  29260. *
  29261. * A meta constructor is uniquely identified by the types of its arguments
  29262. * and is such that there exists an actual constructor of the underlying
  29263. * type that can be invoked with parameters whose types are those given.
  29264. *
  29265. * @tparam Args Types of arguments to use to construct an instance.
  29266. * @return An extended meta factory for the parent type.
  29267. */
  29268. template<typename... Args>
  29269. auto ctor() ENTT_NOEXCEPT {
  29270. using descriptor = meta_function_helper_t<Type, Type(*)(Args...)>;
  29271. auto * const type = internal::meta_info<Type>::resolve();
  29272. static internal::meta_ctor_node node{
  29273. type,
  29274. nullptr,
  29275. nullptr,
  29276. descriptor::args_type::size,
  29277. [](const typename internal::meta_ctor_node::size_type index) ENTT_NOEXCEPT {
  29278. return meta_arg(typename descriptor::args_type{}, index);
  29279. },
  29280. [](meta_any * const args) {
  29281. return meta_construct<Type, Args...>(args, std::make_index_sequence<descriptor::args_type::size>{});
  29282. }
  29283. };
  29284. if(!internal::find_if(&node, type->ctor)) {
  29285. node.next = type->ctor;
  29286. type->ctor = &node;
  29287. }
  29288. return meta_factory<Type, Type(Args...)>{&node.prop};
  29289. }
  29290. /**
  29291. * @brief Assigns a meta destructor to a meta type.
  29292. *
  29293. * Free functions can be assigned to meta types in the role of destructors.
  29294. * The signature of the function should identical to the following:
  29295. *
  29296. * @code{.cpp}
  29297. * void(Type &);
  29298. * @endcode
  29299. *
  29300. * The purpose is to give users the ability to free up resources that
  29301. * require special treatment before an object is actually destroyed.
  29302. *
  29303. * @tparam Func The actual function to use as a destructor.
  29304. * @return A meta factory for the parent type.
  29305. */
  29306. template<auto Func>
  29307. auto dtor() ENTT_NOEXCEPT {
  29308. static_assert(std::is_invocable_v<decltype(Func), Type &>, "The function doesn't accept an object of the type provided");
  29309. auto * const type = internal::meta_info<Type>::resolve();
  29310. type->dtor = [](void *instance) {
  29311. Func(*static_cast<Type *>(instance));
  29312. };
  29313. return meta_factory<Type>{};
  29314. }
  29315. /**
  29316. * @brief Assigns a meta data to a meta type.
  29317. *
  29318. * Both data members and static and global variables, as well as constants
  29319. * of any kind, can be assigned to a meta type.<br/>
  29320. * From a client's point of view, all the variables associated with the
  29321. * reflected object will appear as if they were part of the type itself.
  29322. *
  29323. * @tparam Data The actual variable to attach to the meta type.
  29324. * @tparam Policy Optional policy (no policy set by default).
  29325. * @param id Unique identifier.
  29326. * @return An extended meta factory for the parent type.
  29327. */
  29328. template<auto Data, typename Policy = as_is_t>
  29329. auto data(const id_type id) ENTT_NOEXCEPT {
  29330. if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
  29331. return data<Data, Data, Policy>(id);
  29332. } else {
  29333. using data_type = std::remove_pointer_t<decltype(Data)>;
  29334. auto * const type = internal::meta_info<Type>::resolve();
  29335. static internal::meta_data_node node{
  29336. {},
  29337. type,
  29338. nullptr,
  29339. nullptr,
  29340. std::is_same_v<Type, data_type> || std::is_const_v<data_type>,
  29341. true,
  29342. &internal::meta_info<data_type>::resolve,
  29343. &meta_setter<Type, Data>,
  29344. &meta_getter<Type, Data, Policy>
  29345. };
  29346. ENTT_ASSERT(!internal::find_if_not(id, type->data, &node), "Duplicate identifier");
  29347. node.id = id;
  29348. if(!internal::find_if(&node, type->data)) {
  29349. node.next = type->data;
  29350. type->data = &node;
  29351. }
  29352. return meta_factory<Type, std::integral_constant<decltype(Data), Data>>{&node.prop};
  29353. }
  29354. }
  29355. /**
  29356. * @brief Assigns a meta data to a meta type by means of its setter and
  29357. * getter.
  29358. *
  29359. * Setters and getters can be either free functions, member functions or a
  29360. * mix of them.<br/>
  29361. * In case of free functions, setters and getters must accept a reference to
  29362. * an instance of the parent type as their first argument. A setter has then
  29363. * an extra argument of a type convertible to that of the parameter to
  29364. * set.<br/>
  29365. * In case of member functions, getters have no arguments at all, while
  29366. * setters has an argument of a type convertible to that of the parameter to
  29367. * set.
  29368. *
  29369. * @tparam Setter The actual function to use as a setter.
  29370. * @tparam Getter The actual function to use as a getter.
  29371. * @tparam Policy Optional policy (no policy set by default).
  29372. * @param id Unique identifier.
  29373. * @return An extended meta factory for the parent type.
  29374. */
  29375. template<auto Setter, auto Getter, typename Policy = as_is_t>
  29376. auto data(const id_type id) ENTT_NOEXCEPT {
  29377. using underlying_type = std::remove_reference_t<std::invoke_result_t<decltype(Getter), Type &>>;
  29378. auto * const type = internal::meta_info<Type>::resolve();
  29379. static internal::meta_data_node node{
  29380. {},
  29381. type,
  29382. nullptr,
  29383. nullptr,
  29384. std::is_same_v<decltype(Setter), std::nullptr_t> || (std::is_member_object_pointer_v<decltype(Setter)> && std::is_const_v<underlying_type>),
  29385. false,
  29386. &internal::meta_info<underlying_type>::resolve,
  29387. &meta_setter<Type, Setter>,
  29388. &meta_getter<Type, Getter, Policy>
  29389. };
  29390. ENTT_ASSERT(!internal::find_if_not(id, type->data, &node), "Duplicate identifier");
  29391. node.id = id;
  29392. if(!internal::find_if(&node, type->data)) {
  29393. node.next = type->data;
  29394. type->data = &node;
  29395. }
  29396. return meta_factory<Type, std::integral_constant<decltype(Setter), Setter>, std::integral_constant<decltype(Getter), Getter>>{&node.prop};
  29397. }
  29398. /**
  29399. * @brief Assigns a meta funcion to a meta type.
  29400. *
  29401. * Both member functions and free functions can be assigned to a meta
  29402. * type.<br/>
  29403. * From a client's point of view, all the functions associated with the
  29404. * reflected object will appear as if they were part of the type itself.
  29405. *
  29406. * @tparam Candidate The actual function to attach to the meta type.
  29407. * @tparam Policy Optional policy (no policy set by default).
  29408. * @param id Unique identifier.
  29409. * @return An extended meta factory for the parent type.
  29410. */
  29411. template<auto Candidate, typename Policy = as_is_t>
  29412. auto func(const id_type id) ENTT_NOEXCEPT {
  29413. using descriptor = meta_function_helper_t<Type, decltype(Candidate)>;
  29414. auto * const type = internal::meta_info<Type>::resolve();
  29415. static internal::meta_func_node node{
  29416. {},
  29417. type,
  29418. nullptr,
  29419. nullptr,
  29420. descriptor::args_type::size,
  29421. descriptor::is_const,
  29422. descriptor::is_static,
  29423. &internal::meta_info<std::conditional_t<std::is_same_v<Policy, as_void_t>, void, typename descriptor::return_type>>::resolve,
  29424. [](const typename internal::meta_func_node::size_type index) ENTT_NOEXCEPT {
  29425. return meta_arg(typename descriptor::args_type{}, index);
  29426. },
  29427. [](meta_handle instance, meta_any *args) {
  29428. return meta_invoke<Type, Candidate, Policy>(std::move(instance), args, std::make_index_sequence<descriptor::args_type::size>{});
  29429. }
  29430. };
  29431. for(auto *it = &type->func; *it; it = &(*it)->next) {
  29432. if(*it == &node) {
  29433. *it = node.next;
  29434. break;
  29435. }
  29436. }
  29437. internal::meta_func_node **it = &type->func;
  29438. for(; *it && (*it)->id != id; it = &(*it)->next);
  29439. for(; *it && (*it)->id == id && (*it)->arity < node.arity; it = &(*it)->next);
  29440. node.id = id;
  29441. node.next = *it;
  29442. *it = &node;
  29443. return meta_factory<Type, std::integral_constant<decltype(Candidate), Candidate>>{&node.prop};
  29444. }
  29445. };
  29446. /**
  29447. * @brief Utility function to use for reflection.
  29448. *
  29449. * This is the point from which everything starts.<br/>
  29450. * By invoking this function with a type that is not yet reflected, a meta type
  29451. * is created to which it will be possible to attach meta objects through a
  29452. * dedicated factory.
  29453. *
  29454. * @tparam Type Type to reflect.
  29455. * @return A meta factory for the given type.
  29456. */
  29457. template<typename Type>
  29458. [[nodiscard]] auto meta() ENTT_NOEXCEPT {
  29459. auto * const node = internal::meta_info<Type>::resolve();
  29460. // extended meta factory to allow assigning properties to opaque meta types
  29461. return meta_factory<Type, Type>{&node->prop};
  29462. }
  29463. }
  29464. #endif
  29465. // #include "meta/meta.hpp"
  29466. #ifndef ENTT_META_META_HPP
  29467. #define ENTT_META_META_HPP
  29468. #include <cstddef>
  29469. #include <functional>
  29470. #include <iterator>
  29471. #include <memory>
  29472. #include <type_traits>
  29473. #include <utility>
  29474. // #include "../config/config.h"
  29475. // #include "../core/any.hpp"
  29476. // #include "../core/fwd.hpp"
  29477. // #include "../core/utility.hpp"
  29478. // #include "../core/type_info.hpp"
  29479. // #include "../core/type_traits.hpp"
  29480. // #include "adl_pointer.hpp"
  29481. // #include "ctx.hpp"
  29482. // #include "node.hpp"
  29483. // #include "range.hpp"
  29484. // #include "type_traits.hpp"
  29485. namespace entt {
  29486. class meta_any;
  29487. class meta_type;
  29488. /*! @brief Proxy object for sequence containers. */
  29489. class meta_sequence_container {
  29490. template<typename>
  29491. struct meta_sequence_container_proxy;
  29492. class meta_iterator;
  29493. public:
  29494. /*! @brief Unsigned integer type. */
  29495. using size_type = std::size_t;
  29496. /*! @brief Meta iterator type. */
  29497. using iterator = meta_iterator;
  29498. /*! @brief Default constructor. */
  29499. meta_sequence_container() ENTT_NOEXCEPT = default;
  29500. /**
  29501. * @brief Construct a proxy object for sequence containers.
  29502. * @tparam Type Type of container to wrap.
  29503. * @param instance The container to wrap.
  29504. */
  29505. template<typename Type>
  29506. meta_sequence_container(std::in_place_type_t<Type>, any instance) ENTT_NOEXCEPT
  29507. : value_type_fn{&meta_sequence_container_proxy<Type>::value_type},
  29508. size_fn{&meta_sequence_container_proxy<Type>::size},
  29509. resize_fn{&meta_sequence_container_proxy<Type>::resize},
  29510. clear_fn{&meta_sequence_container_proxy<Type>::clear},
  29511. begin_fn{&meta_sequence_container_proxy<Type>::begin},
  29512. end_fn{&meta_sequence_container_proxy<Type>::end},
  29513. insert_fn{&meta_sequence_container_proxy<Type>::insert},
  29514. erase_fn{&meta_sequence_container_proxy<Type>::erase},
  29515. get_fn{&meta_sequence_container_proxy<Type>::get},
  29516. storage{std::move(instance)}
  29517. {}
  29518. [[nodiscard]] inline meta_type value_type() const ENTT_NOEXCEPT;
  29519. [[nodiscard]] inline size_type size() const ENTT_NOEXCEPT;
  29520. inline bool resize(size_type);
  29521. inline bool clear();
  29522. [[nodiscard]] inline iterator begin();
  29523. [[nodiscard]] inline iterator end();
  29524. inline std::pair<iterator, bool> insert(iterator, meta_any);
  29525. inline std::pair<iterator, bool> erase(iterator);
  29526. [[nodiscard]] inline meta_any operator[](size_type);
  29527. [[nodiscard]] inline explicit operator bool() const ENTT_NOEXCEPT;
  29528. private:
  29529. meta_type(* value_type_fn)() ENTT_NOEXCEPT = nullptr;
  29530. size_type(* size_fn)(const any &) ENTT_NOEXCEPT = nullptr;
  29531. bool(* resize_fn)(any &, size_type) = nullptr;
  29532. bool(* clear_fn)(any &) = nullptr;
  29533. iterator(* begin_fn)(any &) = nullptr;
  29534. iterator(* end_fn)(any &) = nullptr;
  29535. std::pair<iterator, bool>(* insert_fn)(any &, iterator, meta_any &) = nullptr;
  29536. std::pair<iterator, bool>(* erase_fn)(any &, iterator) = nullptr;
  29537. meta_any(* get_fn)(any &, size_type) = nullptr;
  29538. any storage{};
  29539. };
  29540. /*! @brief Proxy object for associative containers. */
  29541. class meta_associative_container {
  29542. template<typename>
  29543. struct meta_associative_container_proxy;
  29544. class meta_iterator;
  29545. public:
  29546. /*! @brief Unsigned integer type. */
  29547. using size_type = std::size_t;
  29548. /*! @brief Meta iterator type. */
  29549. using iterator = meta_iterator;
  29550. /*! @brief Default constructor. */
  29551. meta_associative_container() ENTT_NOEXCEPT = default;
  29552. /**
  29553. * @brief Construct a proxy object for associative containers.
  29554. * @tparam Type Type of container to wrap.
  29555. * @param instance The container to wrap.
  29556. */
  29557. template<typename Type>
  29558. meta_associative_container(std::in_place_type_t<Type>, any instance) ENTT_NOEXCEPT
  29559. : key_only_container{is_key_only_meta_associative_container_v<Type>},
  29560. key_type_fn{&meta_associative_container_proxy<Type>::key_type},
  29561. mapped_type_fn{&meta_associative_container_proxy<Type>::mapped_type},
  29562. value_type_fn{&meta_associative_container_proxy<Type>::value_type},
  29563. size_fn{&meta_associative_container_proxy<Type>::size},
  29564. clear_fn{&meta_associative_container_proxy<Type>::clear},
  29565. begin_fn{&meta_associative_container_proxy<Type>::begin},
  29566. end_fn{&meta_associative_container_proxy<Type>::end},
  29567. insert_fn{&meta_associative_container_proxy<Type>::insert},
  29568. erase_fn{&meta_associative_container_proxy<Type>::erase},
  29569. find_fn{&meta_associative_container_proxy<Type>::find},
  29570. storage{std::move(instance)}
  29571. {}
  29572. [[nodiscard]] inline bool key_only() const ENTT_NOEXCEPT;
  29573. [[nodiscard]] inline meta_type key_type() const ENTT_NOEXCEPT;
  29574. [[nodiscard]] inline meta_type mapped_type() const ENTT_NOEXCEPT;
  29575. [[nodiscard]] inline meta_type value_type() const ENTT_NOEXCEPT;
  29576. [[nodiscard]] inline size_type size() const ENTT_NOEXCEPT;
  29577. inline bool clear();
  29578. [[nodiscard]] inline iterator begin();
  29579. [[nodiscard]] inline iterator end();
  29580. inline bool insert(meta_any, meta_any);
  29581. inline bool erase(meta_any);
  29582. [[nodiscard]] inline iterator find(meta_any);
  29583. [[nodiscard]] inline explicit operator bool() const ENTT_NOEXCEPT;
  29584. private:
  29585. bool key_only_container{};
  29586. meta_type(* key_type_fn)() ENTT_NOEXCEPT = nullptr;
  29587. meta_type(* mapped_type_fn)() ENTT_NOEXCEPT = nullptr;
  29588. meta_type(* value_type_fn)() ENTT_NOEXCEPT = nullptr;
  29589. size_type(* size_fn)(const any &) ENTT_NOEXCEPT = nullptr;
  29590. bool(* clear_fn)(any &) = nullptr;
  29591. iterator(* begin_fn)(any &) = nullptr;
  29592. iterator(* end_fn)(any &) = nullptr;
  29593. bool(* insert_fn)(any &, meta_any &, meta_any &) = nullptr;
  29594. bool(* erase_fn)(any &, meta_any &) = nullptr;
  29595. iterator(* find_fn)(any &, meta_any &) = nullptr;
  29596. any storage{};
  29597. };
  29598. /*! @brief Opaque wrapper for values of any type. */
  29599. class meta_any {
  29600. enum class operation { DTOR, DEREF, SEQ, ASSOC };
  29601. using vtable_type = void(const operation, const any &, void *);
  29602. template<typename Type>
  29603. static void basic_vtable([[maybe_unused]] const operation op, [[maybe_unused]] const any &from, [[maybe_unused]] void *to) {
  29604. static_assert(std::is_same_v<std::remove_reference_t<std::remove_const_t<Type>>, Type>, "Invalid type");
  29605. if constexpr(!std::is_void_v<Type>) {
  29606. switch(op) {
  29607. case operation::DTOR:
  29608. if(auto *curr = static_cast<internal::meta_type_node *>(to); curr->dtor && from.owner()) {
  29609. curr->dtor(const_cast<any &>(from).data());
  29610. }
  29611. break;
  29612. case operation::DEREF:
  29613. if constexpr(is_meta_pointer_like_v<Type>) {
  29614. using element_type = std::remove_const_t<typename std::pointer_traits<Type>::element_type>;
  29615. if constexpr(std::is_function_v<element_type>) {
  29616. *static_cast<meta_any *>(to) = any_cast<Type>(from);
  29617. } else if constexpr(!std::is_same_v<std::remove_const_t<typename std::pointer_traits<Type>::element_type>, void>) {
  29618. using in_place_type = decltype(adl_meta_pointer_like<Type>::dereference(any_cast<const Type &>(from)));
  29619. static_cast<meta_any *>(to)->emplace<in_place_type>(adl_meta_pointer_like<Type>::dereference(any_cast<const Type &>(from)));
  29620. }
  29621. }
  29622. case operation::SEQ:
  29623. if constexpr(is_complete_v<meta_sequence_container_traits<Type>>) {
  29624. *static_cast<meta_sequence_container *>(to) = { std::in_place_type<Type>, std::move(const_cast<any &>(from)) };
  29625. }
  29626. break;
  29627. case operation::ASSOC:
  29628. if constexpr(is_complete_v<meta_associative_container_traits<Type>>) {
  29629. *static_cast<meta_associative_container *>(to) = { std::in_place_type<Type>, std::move(const_cast<any &>(from)) };
  29630. }
  29631. break;
  29632. }
  29633. }
  29634. }
  29635. meta_any(const meta_any &other, any ref) ENTT_NOEXCEPT
  29636. : storage{std::move(ref)},
  29637. node{storage ? other.node : nullptr},
  29638. vtable{storage ? other.vtable : &basic_vtable<void>}
  29639. {}
  29640. public:
  29641. /*! @brief Default constructor. */
  29642. meta_any() ENTT_NOEXCEPT
  29643. : storage{},
  29644. node{},
  29645. vtable{&basic_vtable<void>}
  29646. {}
  29647. /**
  29648. * @brief Constructs a wrapper by directly initializing the new object.
  29649. * @tparam Type Type of object to use to initialize the wrapper.
  29650. * @tparam Args Types of arguments to use to construct the new instance.
  29651. * @param args Parameters to use to construct the instance.
  29652. */
  29653. template<typename Type, typename... Args>
  29654. explicit meta_any(std::in_place_type_t<Type>, Args &&... args)
  29655. : storage{std::in_place_type<Type>, std::forward<Args>(args)...},
  29656. node{internal::meta_info<Type>::resolve()},
  29657. vtable{&basic_vtable<std::remove_const_t<std::remove_reference_t<Type>>>}
  29658. {}
  29659. /**
  29660. * @brief Constructs a wrapper that holds an unmanaged object.
  29661. * @tparam Type Type of object to use to initialize the wrapper.
  29662. * @param value An instance of an object to use to initialize the wrapper.
  29663. */
  29664. template<typename Type>
  29665. meta_any(std::reference_wrapper<Type> value)
  29666. : meta_any{}
  29667. {
  29668. // invokes deprecated assignment operator (and avoids issues with vs2017)
  29669. *this = value;
  29670. }
  29671. /**
  29672. * @brief Constructs a wrapper from a given value.
  29673. * @tparam Type Type of object to use to initialize the wrapper.
  29674. * @param value An instance of an object to use to initialize the wrapper.
  29675. */
  29676. template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::decay_t<Type>, meta_any>>>
  29677. meta_any(Type &&value)
  29678. : storage{std::forward<Type>(value)},
  29679. node{internal::meta_info<std::decay_t<Type>>::resolve()},
  29680. vtable{&basic_vtable<std::decay_t<Type>>}
  29681. {}
  29682. /**
  29683. * @brief Copy constructor.
  29684. * @param other The instance to copy from.
  29685. */
  29686. meta_any(const meta_any &other) = default;
  29687. /**
  29688. * @brief Move constructor.
  29689. * @param other The instance to move from.
  29690. */
  29691. meta_any(meta_any &&other) ENTT_NOEXCEPT
  29692. : storage{std::move(other.storage)},
  29693. node{std::exchange(other.node, nullptr)},
  29694. vtable{std::exchange(other.vtable, &basic_vtable<void>)}
  29695. {}
  29696. /*! @brief Frees the internal storage, whatever it means. */
  29697. ~meta_any() {
  29698. vtable(operation::DTOR, storage, node);
  29699. }
  29700. /**
  29701. * @brief Copy assignment operator.
  29702. * @param other The instance to copy from.
  29703. * @return This meta any object.
  29704. */
  29705. meta_any & operator=(const meta_any &other) {
  29706. std::exchange(vtable, other.vtable)(operation::DTOR, storage, node);
  29707. storage = other.storage;
  29708. node = other.node;
  29709. return *this;
  29710. }
  29711. /**
  29712. * @brief Move assignment operator.
  29713. * @param other The instance to move from.
  29714. * @return This meta any object.
  29715. */
  29716. meta_any & operator=(meta_any &&other) ENTT_NOEXCEPT {
  29717. std::exchange(vtable, std::exchange(other.vtable, &basic_vtable<void>))(operation::DTOR, storage, node);
  29718. storage = std::move(other.storage);
  29719. node = std::exchange(other.node, nullptr);
  29720. return *this;
  29721. }
  29722. /**
  29723. * @brief Value assignment operator.
  29724. * @tparam Type Type of object to use to initialize the wrapper.
  29725. * @param value An instance of an object to use to initialize the wrapper.
  29726. * @return This meta any object.
  29727. */
  29728. template<typename Type>
  29729. [[deprecated("Use std::in_place_type<T &>, entt::make_meta<T &>, emplace<Type &> or forward_as_meta instead")]]
  29730. meta_any & operator=(std::reference_wrapper<Type> value) {
  29731. emplace<Type &>(value.get());
  29732. return *this;
  29733. }
  29734. /**
  29735. * @brief Value assignment operator.
  29736. * @tparam Type Type of object to use to initialize the wrapper.
  29737. * @param value An instance of an object to use to initialize the wrapper.
  29738. * @return This meta any object.
  29739. */
  29740. template<typename Type>
  29741. std::enable_if_t<!std::is_same_v<std::decay_t<Type>, meta_any>, meta_any &>
  29742. operator=(Type &&value) {
  29743. emplace<std::decay_t<Type>>(std::forward<Type>(value));
  29744. return *this;
  29745. }
  29746. /**
  29747. * @brief Returns the type of the underlying object.
  29748. * @return The type of the underlying object, if any.
  29749. */
  29750. [[nodiscard]] inline meta_type type() const ENTT_NOEXCEPT;
  29751. /**
  29752. * @brief Returns an opaque pointer to the contained instance.
  29753. * @return An opaque pointer the contained instance, if any.
  29754. */
  29755. [[nodiscard]] const void * data() const ENTT_NOEXCEPT {
  29756. return storage.data();
  29757. }
  29758. /*! @copydoc data */
  29759. [[nodiscard]] void * data() ENTT_NOEXCEPT {
  29760. return storage.data();
  29761. }
  29762. /**
  29763. * @brief Invokes the underlying function, if possible.
  29764. *
  29765. * @sa meta_func::invoke
  29766. *
  29767. * @tparam Args Types of arguments to use to invoke the function.
  29768. * @param id Unique identifier.
  29769. * @param args Parameters to use to invoke the function.
  29770. * @return A wrapper containing the returned value, if any.
  29771. */
  29772. template<typename... Args>
  29773. meta_any invoke(const id_type id, Args &&... args) const;
  29774. /*! @copydoc invoke */
  29775. template<typename... Args>
  29776. meta_any invoke(const id_type id, Args &&... args);
  29777. /**
  29778. * @brief Sets the value of a given variable.
  29779. *
  29780. * The type of the value must be such that a cast or conversion to the type
  29781. * of the variable is possible. Otherwise, invoking the setter does nothing.
  29782. *
  29783. * @tparam Type Type of value to assign.
  29784. * @param id Unique identifier.
  29785. * @param value Parameter to use to set the underlying variable.
  29786. * @return True in case of success, false otherwise.
  29787. */
  29788. template<typename Type>
  29789. bool set(const id_type id, Type &&value);
  29790. /**
  29791. * @brief Gets the value of a given variable.
  29792. * @param id Unique identifier.
  29793. * @return A wrapper containing the value of the underlying variable.
  29794. */
  29795. [[nodiscard]] meta_any get(const id_type id) const;
  29796. /*! @copydoc get */
  29797. [[nodiscard]] meta_any get(const id_type id);
  29798. /**
  29799. * @brief Tries to cast an instance to a given type.
  29800. * @tparam Type Type to which to cast the instance.
  29801. * @return A (possibly null) pointer to the contained instance.
  29802. */
  29803. template<typename Type>
  29804. [[nodiscard]] const Type * try_cast() const {
  29805. if(node) {
  29806. if(const auto info = type_id<Type>(); node->info == info) {
  29807. return any_cast<Type>(&storage);
  29808. } else if(const auto *base = internal::meta_visit<&internal::meta_type_node::base>([info](const auto *curr) { return curr->type()->info == info; }, node); base) {
  29809. return static_cast<const Type *>(base->cast(storage.data()));
  29810. }
  29811. }
  29812. return nullptr;
  29813. }
  29814. /*! @copydoc try_cast */
  29815. template<typename Type>
  29816. [[nodiscard]] Type * try_cast() {
  29817. if(node) {
  29818. if(const auto info = type_id<Type>(); node->info == info) {
  29819. return any_cast<Type>(&storage);
  29820. } else if(const auto *base = internal::meta_visit<&internal::meta_type_node::base>([info](const auto *curr) { return curr->type()->info == info; }, node); base) {
  29821. return static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(base->cast(static_cast<constness_as_t<any, Type> &>(storage).data())));
  29822. }
  29823. }
  29824. return nullptr;
  29825. }
  29826. /**
  29827. * @brief Tries to cast an instance to a given type.
  29828. *
  29829. * The type of the instance must be such that the cast is possible.
  29830. *
  29831. * @warning
  29832. * Attempting to perform an invalid cast results in undefined behavior.
  29833. *
  29834. * @tparam Type Type to which to cast the instance.
  29835. * @return A reference to the contained instance.
  29836. */
  29837. template<typename Type>
  29838. [[nodiscard]] Type cast() const {
  29839. auto * const instance = try_cast<std::remove_reference_t<Type>>();
  29840. ENTT_ASSERT(instance, "Invalid instance");
  29841. return static_cast<Type>(*instance);
  29842. }
  29843. /*! @copydoc cast */
  29844. template<typename Type>
  29845. [[nodiscard]] Type cast() {
  29846. // forces const on non-reference types to make them work also with wrappers for const references
  29847. auto * const instance = try_cast<std::remove_reference_t<const Type>>();
  29848. ENTT_ASSERT(instance, "Invalid instance");
  29849. return static_cast<Type>(*instance);
  29850. }
  29851. /**
  29852. * @brief Converts an object in such a way that a given cast becomes viable.
  29853. * @tparam Type Type to which the cast is requested.
  29854. * @return A valid meta any object if there exists a viable conversion, an
  29855. * invalid one otherwise.
  29856. */
  29857. template<typename Type>
  29858. [[nodiscard]] meta_any allow_cast() const {
  29859. if(try_cast<std::remove_reference_t<Type>>() != nullptr) {
  29860. return as_ref();
  29861. } else if(node) {
  29862. if(const auto * const conv = internal::meta_visit<&internal::meta_type_node::conv>([info = type_id<Type>()](const auto *curr) { return curr->type()->info == info; }, node); conv) {
  29863. return conv->conv(storage.data());
  29864. }
  29865. }
  29866. return {};
  29867. }
  29868. /**
  29869. * @brief Converts an object in such a way that a given cast becomes viable.
  29870. * @tparam Type Type to which the cast is requested.
  29871. * @return True if there exists a viable conversion, false otherwise.
  29872. */
  29873. template<typename Type>
  29874. bool allow_cast() {
  29875. // forces const on non-reference types to make them work also with wrappers for const references
  29876. if(try_cast<std::remove_reference_t<const Type>>() != nullptr) {
  29877. return true;
  29878. } else if(node) {
  29879. if(const auto * const conv = internal::meta_visit<&internal::meta_type_node::conv>([info = type_id<Type>()](const auto *curr) { return curr->type()->info == info; }, node); conv) {
  29880. *this = conv->conv(std::as_const(storage).data());
  29881. return true;
  29882. }
  29883. }
  29884. return false;
  29885. }
  29886. /**
  29887. * @brief Replaces the contained object by creating a new instance directly.
  29888. * @tparam Type Type of object to use to initialize the wrapper.
  29889. * @tparam Args Types of arguments to use to construct the new instance.
  29890. * @param args Parameters to use to construct the instance.
  29891. */
  29892. template<typename Type, typename... Args>
  29893. void emplace(Args &&... args) {
  29894. std::exchange(vtable, &basic_vtable<std::remove_const_t<std::remove_reference_t<Type>>>)(operation::DTOR, storage, node);
  29895. storage.emplace<Type>(std::forward<Args>(args)...);
  29896. node = internal::meta_info<Type>::resolve();
  29897. }
  29898. /*! @brief Destroys contained object */
  29899. void reset() {
  29900. std::exchange(vtable, &basic_vtable<void>)(operation::DTOR, storage, node);
  29901. storage.reset();
  29902. node = nullptr;
  29903. }
  29904. /**
  29905. * @brief Returns a sequence container proxy.
  29906. * @return A sequence container proxy for the underlying object.
  29907. */
  29908. [[nodiscard]] meta_sequence_container as_sequence_container() ENTT_NOEXCEPT {
  29909. meta_sequence_container proxy;
  29910. vtable(operation::SEQ, storage.as_ref(), &proxy);
  29911. return proxy;
  29912. }
  29913. /*! @copydoc as_sequence_container */
  29914. [[nodiscard]] meta_sequence_container as_sequence_container() const ENTT_NOEXCEPT {
  29915. meta_sequence_container proxy;
  29916. vtable(operation::SEQ, storage.as_ref(), &proxy);
  29917. return proxy;
  29918. }
  29919. /**
  29920. * @brief Returns an associative container proxy.
  29921. * @return An associative container proxy for the underlying object.
  29922. */
  29923. [[nodiscard]] meta_associative_container as_associative_container() ENTT_NOEXCEPT {
  29924. meta_associative_container proxy;
  29925. vtable(operation::ASSOC, storage.as_ref(), &proxy);
  29926. return proxy;
  29927. }
  29928. /*! @copydoc as_associative_container */
  29929. [[nodiscard]] meta_associative_container as_associative_container() const ENTT_NOEXCEPT {
  29930. meta_associative_container proxy;
  29931. vtable(operation::ASSOC, storage.as_ref(), &proxy);
  29932. return proxy;
  29933. }
  29934. /**
  29935. * @brief Indirection operator for dereferencing opaque objects.
  29936. * @return A wrapper that shares a reference to an unmanaged object if the
  29937. * wrapped element is dereferenceable, an invalid meta any otherwise.
  29938. */
  29939. [[nodiscard]] meta_any operator*() const ENTT_NOEXCEPT {
  29940. meta_any ret{};
  29941. vtable(operation::DEREF, storage, &ret);
  29942. return ret;
  29943. }
  29944. /**
  29945. * @brief Returns false if a wrapper is invalid, true otherwise.
  29946. * @return False if the wrapper is invalid, true otherwise.
  29947. */
  29948. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  29949. return !(node == nullptr);
  29950. }
  29951. /**
  29952. * @brief Checks if two wrappers differ in their content.
  29953. * @param other Wrapper with which to compare.
  29954. * @return False if the two objects differ in their content, true otherwise.
  29955. */
  29956. [[nodiscard]] bool operator==(const meta_any &other) const {
  29957. return (!node && !other.node) || (node && other.node && node->info == other.node->info && storage == other.storage);
  29958. }
  29959. /**
  29960. * @brief Aliasing constructor.
  29961. * @return A wrapper that shares a reference to an unmanaged object.
  29962. */
  29963. [[nodiscard]] meta_any as_ref() ENTT_NOEXCEPT {
  29964. return meta_any{*this, storage.as_ref()};
  29965. }
  29966. /*! @copydoc as_ref */
  29967. [[nodiscard]] meta_any as_ref() const ENTT_NOEXCEPT {
  29968. return meta_any{*this, storage.as_ref()};
  29969. }
  29970. private:
  29971. any storage;
  29972. internal::meta_type_node *node;
  29973. vtable_type *vtable;
  29974. };
  29975. /**
  29976. * @brief Checks if two wrappers differ in their content.
  29977. * @param lhs A wrapper, either empty or not.
  29978. * @param rhs A wrapper, either empty or not.
  29979. * @return True if the two wrappers differ in their content, false otherwise.
  29980. */
  29981. [[nodiscard]] inline bool operator!=(const meta_any &lhs, const meta_any &rhs) ENTT_NOEXCEPT {
  29982. return !(lhs == rhs);
  29983. }
  29984. /**
  29985. * @brief Constructs a wrapper from a given type, passing it all arguments.
  29986. * @tparam Type Type of object to use to initialize the wrapper.
  29987. * @tparam Args Types of arguments to use to construct the new instance.
  29988. * @param args Parameters to use to construct the instance.
  29989. * @return A properly initialized wrapper for an object of the given type.
  29990. */
  29991. template<typename Type, typename... Args>
  29992. meta_any make_meta(Args &&... args) {
  29993. return meta_any{std::in_place_type<Type>, std::forward<Args>(args)...};
  29994. }
  29995. /**
  29996. * @brief Forwards its argument and avoids copies for lvalue references.
  29997. * @tparam Type Type of argument to use to construct the new instance.
  29998. * @param value Parameter to use to construct the instance.
  29999. * @return A properly initialized and not necessarily owning wrapper.
  30000. */
  30001. template<typename Type>
  30002. meta_any forward_as_meta(Type &&value) {
  30003. return meta_any{std::in_place_type<std::conditional_t<std::is_rvalue_reference_v<Type>, std::decay_t<Type>, Type>>, std::forward<Type>(value)};
  30004. }
  30005. /**
  30006. * @brief Opaque pointers to instances of any type.
  30007. *
  30008. * A handle doesn't perform copies and isn't responsible for the contained
  30009. * object. It doesn't prolong the lifetime of the pointed instance.<br/>
  30010. * Handles are used to generate references to actual objects when needed.
  30011. */
  30012. struct meta_handle {
  30013. /*! @brief Default constructor. */
  30014. meta_handle() = default;
  30015. /*! @brief Default copy constructor, deleted on purpose. */
  30016. meta_handle(const meta_handle &) = delete;
  30017. /*! @brief Default move constructor. */
  30018. meta_handle(meta_handle &&) = default;
  30019. /**
  30020. * @brief Default copy assignment operator, deleted on purpose.
  30021. * @return This meta handle.
  30022. */
  30023. meta_handle & operator=(const meta_handle &) = delete;
  30024. /**
  30025. * @brief Default move assignment operator.
  30026. * @return This meta handle.
  30027. */
  30028. meta_handle & operator=(meta_handle &&) = default;
  30029. /**
  30030. * @brief Creates a handle that points to an unmanaged object.
  30031. * @tparam Type Type of object to use to initialize the handle.
  30032. * @param value An instance of an object to use to initialize the handle.
  30033. */
  30034. template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::decay_t<Type>, meta_handle>>>
  30035. meta_handle(Type &value) ENTT_NOEXCEPT
  30036. : meta_handle{}
  30037. {
  30038. if constexpr(std::is_same_v<std::decay_t<Type>, meta_any>) {
  30039. any = value.as_ref();
  30040. } else {
  30041. any.emplace<Type &>(value);
  30042. }
  30043. }
  30044. /**
  30045. * @brief Returns false if a handle is invalid, true otherwise.
  30046. * @return False if the handle is invalid, true otherwise.
  30047. */
  30048. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  30049. return static_cast<bool>(any);
  30050. }
  30051. /**
  30052. * @brief Access operator for accessing the contained opaque object.
  30053. * @return A wrapper that shares a reference to an unmanaged object.
  30054. */
  30055. [[nodiscard]] meta_any * operator->() {
  30056. return &any;
  30057. }
  30058. /*! @copydoc operator-> */
  30059. [[nodiscard]] const meta_any * operator->() const {
  30060. return &any;
  30061. }
  30062. private:
  30063. meta_any any;
  30064. };
  30065. /*! @brief Opaque wrapper for properties of any type. */
  30066. struct meta_prop {
  30067. /*! @brief Node type. */
  30068. using node_type = internal::meta_prop_node;
  30069. /**
  30070. * @brief Constructs an instance from a given node.
  30071. * @param curr The underlying node with which to construct the instance.
  30072. */
  30073. meta_prop(const node_type *curr = nullptr) ENTT_NOEXCEPT
  30074. : node{curr}
  30075. {}
  30076. /**
  30077. * @brief Returns the stored key as a const reference.
  30078. * @return A wrapper containing the key stored with the property.
  30079. */
  30080. [[nodiscard]] meta_any key() const {
  30081. return node->id.as_ref();
  30082. }
  30083. /**
  30084. * @brief Returns the stored value by copy.
  30085. * @return A wrapper containing the value stored with the property.
  30086. */
  30087. [[nodiscard]] meta_any value() const {
  30088. return node->value;
  30089. }
  30090. /**
  30091. * @brief Returns true if an object is valid, false otherwise.
  30092. * @return True if the object is valid, false otherwise.
  30093. */
  30094. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  30095. return !(node == nullptr);
  30096. }
  30097. private:
  30098. const node_type *node;
  30099. };
  30100. /*! @brief Opaque wrapper for constructors. */
  30101. struct meta_ctor {
  30102. /*! @brief Node type. */
  30103. using node_type = internal::meta_ctor_node;
  30104. /*! @brief Unsigned integer type. */
  30105. using size_type = typename node_type::size_type;
  30106. /*! @copydoc meta_prop::meta_prop */
  30107. meta_ctor(const node_type *curr = nullptr) ENTT_NOEXCEPT
  30108. : node{curr}
  30109. {}
  30110. /**
  30111. * @brief Returns the type to which an object belongs.
  30112. * @return The type to which the object belongs.
  30113. */
  30114. [[nodiscard]] inline meta_type parent() const ENTT_NOEXCEPT;
  30115. /**
  30116. * @brief Returns the number of arguments accepted by a constructor.
  30117. * @return The number of arguments accepted by the constructor.
  30118. */
  30119. [[nodiscard]] size_type arity() const ENTT_NOEXCEPT {
  30120. return node->arity;
  30121. }
  30122. /**
  30123. * @brief Returns the type of the i-th argument of a constructor.
  30124. * @param index Index of the argument of which to return the type.
  30125. * @return The type of the i-th argument of a constructor.
  30126. */
  30127. [[nodiscard]] meta_type arg(size_type index) const ENTT_NOEXCEPT;
  30128. /**
  30129. * @brief Creates an instance of the underlying type, if possible.
  30130. *
  30131. * Parameters must be such that a cast or conversion to the required types
  30132. * is possible. Otherwise, an empty and thus invalid wrapper is returned.
  30133. *
  30134. * @param args Parameters to use to construct the instance.
  30135. * @param sz Number of parameters to use to construct the instance.
  30136. * @return A wrapper containing the new instance, if any.
  30137. */
  30138. [[nodiscard]] meta_any invoke(meta_any * const args, const size_type sz) const {
  30139. return sz == arity() ? node->invoke(args) : meta_any{};
  30140. }
  30141. /**
  30142. * @copybrief invoke
  30143. *
  30144. * @sa invoke
  30145. *
  30146. * @tparam Args Types of arguments to use to construct the instance.
  30147. * @param args Parameters to use to construct the instance.
  30148. * @return A wrapper containing the new instance, if any.
  30149. */
  30150. template<typename... Args>
  30151. [[nodiscard]] meta_any invoke([[maybe_unused]] Args &&... args) const {
  30152. meta_any arguments[sizeof...(Args) + 1u]{std::forward<Args>(args)...};
  30153. return invoke(arguments, sizeof...(Args));
  30154. }
  30155. /**
  30156. * @brief Returns a range to use to visit all properties.
  30157. * @return An iterable range to use to visit all properties.
  30158. */
  30159. [[nodiscard]] meta_range<meta_prop> prop() const ENTT_NOEXCEPT {
  30160. return node->prop;
  30161. }
  30162. /**
  30163. * @brief Returns the property associated with a given key.
  30164. * @param key The key to use to search for a property.
  30165. * @return The property associated with the given key, if any.
  30166. */
  30167. [[nodiscard]] meta_prop prop(meta_any key) const {
  30168. return internal::meta_visit<&node_type::prop>([&key](const auto *curr) { return curr->id == key; }, node);
  30169. }
  30170. /**
  30171. * @brief Returns true if an object is valid, false otherwise.
  30172. * @return True if the object is valid, false otherwise.
  30173. */
  30174. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  30175. return !(node == nullptr);
  30176. }
  30177. private:
  30178. const node_type *node;
  30179. };
  30180. /*! @brief Opaque wrapper for data members. */
  30181. struct meta_data {
  30182. /*! @brief Node type. */
  30183. using node_type = internal::meta_data_node;
  30184. /*! @copydoc meta_prop::meta_prop */
  30185. meta_data(const node_type *curr = nullptr) ENTT_NOEXCEPT
  30186. : node{curr}
  30187. {}
  30188. /*! @copydoc meta_type::id */
  30189. [[nodiscard]] id_type id() const ENTT_NOEXCEPT {
  30190. return node->id;
  30191. }
  30192. /*! @copydoc meta_ctor::parent */
  30193. [[nodiscard]] inline meta_type parent() const ENTT_NOEXCEPT;
  30194. /**
  30195. * @brief Indicates whether a data member is constant or not.
  30196. * @return True if the data member is constant, false otherwise.
  30197. */
  30198. [[nodiscard]] bool is_const() const ENTT_NOEXCEPT {
  30199. return node->is_const;
  30200. }
  30201. /**
  30202. * @brief Indicates whether a data member is static or not.
  30203. * @return True if the data member is static, false otherwise.
  30204. */
  30205. [[nodiscard]] bool is_static() const ENTT_NOEXCEPT {
  30206. return node->is_static;
  30207. }
  30208. /*! @copydoc meta_any::type */
  30209. [[nodiscard]] inline meta_type type() const ENTT_NOEXCEPT;
  30210. /**
  30211. * @brief Sets the value of a given variable.
  30212. *
  30213. * It must be possible to cast the instance to the parent type of the data
  30214. * member. Otherwise, invoking the setter results in an undefined
  30215. * behavior.<br/>
  30216. * The type of the value must be such that a cast or conversion to the type
  30217. * of the variable is possible. Otherwise, invoking the setter does nothing.
  30218. *
  30219. * @tparam Type Type of value to assign.
  30220. * @param instance An opaque instance of the underlying type.
  30221. * @param value Parameter to use to set the underlying variable.
  30222. * @return True in case of success, false otherwise.
  30223. */
  30224. template<typename Type>
  30225. bool set(meta_handle instance, Type &&value) const {
  30226. return node->set && node->set(std::move(instance), std::forward<Type>(value));
  30227. }
  30228. /**
  30229. * @brief Gets the value of a given variable.
  30230. *
  30231. * It must be possible to cast the instance to the parent type of the data
  30232. * member. Otherwise, invoking the getter results in an undefined behavior.
  30233. *
  30234. * @param instance An opaque instance of the underlying type.
  30235. * @return A wrapper containing the value of the underlying variable.
  30236. */
  30237. [[nodiscard]] meta_any get(meta_handle instance) const {
  30238. return node->get(std::move(instance));
  30239. }
  30240. /*! @copydoc meta_ctor::prop */
  30241. [[nodiscard]] meta_range<meta_prop> prop() const ENTT_NOEXCEPT {
  30242. return node->prop;
  30243. }
  30244. /**
  30245. * @brief Returns the property associated with a given key.
  30246. * @param key The key to use to search for a property.
  30247. * @return The property associated with the given key, if any.
  30248. */
  30249. [[nodiscard]] meta_prop prop(meta_any key) const {
  30250. return internal::meta_visit<&node_type::prop>([&key](const auto *curr) { return curr->id == key; }, node);
  30251. }
  30252. /**
  30253. * @brief Returns true if an object is valid, false otherwise.
  30254. * @return True if the object is valid, false otherwise.
  30255. */
  30256. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  30257. return !(node == nullptr);
  30258. }
  30259. private:
  30260. const node_type *node;
  30261. };
  30262. /*! @brief Opaque wrapper for member functions. */
  30263. struct meta_func {
  30264. /*! @brief Node type. */
  30265. using node_type = internal::meta_func_node;
  30266. /*! @brief Unsigned integer type. */
  30267. using size_type = typename node_type::size_type;
  30268. /*! @copydoc meta_prop::meta_prop */
  30269. meta_func(const node_type *curr = nullptr) ENTT_NOEXCEPT
  30270. : node{curr}
  30271. {}
  30272. /*! @copydoc meta_type::id */
  30273. [[nodiscard]] id_type id() const ENTT_NOEXCEPT {
  30274. return node->id;
  30275. }
  30276. /*! @copydoc meta_ctor::parent */
  30277. [[nodiscard]] inline meta_type parent() const ENTT_NOEXCEPT;
  30278. /**
  30279. * @brief Returns the number of arguments accepted by a member function.
  30280. * @return The number of arguments accepted by the member function.
  30281. */
  30282. [[nodiscard]] size_type arity() const ENTT_NOEXCEPT {
  30283. return node->arity;
  30284. }
  30285. /**
  30286. * @brief Indicates whether a member function is constant or not.
  30287. * @return True if the member function is constant, false otherwise.
  30288. */
  30289. [[nodiscard]] bool is_const() const ENTT_NOEXCEPT {
  30290. return node->is_const;
  30291. }
  30292. /**
  30293. * @brief Indicates whether a member function is static or not.
  30294. * @return True if the member function is static, false otherwise.
  30295. */
  30296. [[nodiscard]] bool is_static() const ENTT_NOEXCEPT {
  30297. return node->is_static;
  30298. }
  30299. /**
  30300. * @brief Returns the return type of a member function.
  30301. * @return The return type of the member function.
  30302. */
  30303. [[nodiscard]] inline meta_type ret() const ENTT_NOEXCEPT;
  30304. /**
  30305. * @brief Returns the type of the i-th argument of a member function.
  30306. * @param index Index of the argument of which to return the type.
  30307. * @return The type of the i-th argument of a member function.
  30308. */
  30309. [[nodiscard]] inline meta_type arg(size_type index) const ENTT_NOEXCEPT;
  30310. /**
  30311. * @brief Invokes the underlying function, if possible.
  30312. *
  30313. * To invoke a member function, the parameters must be such that a cast or
  30314. * conversion to the required types is possible. Otherwise, an empty and
  30315. * thus invalid wrapper is returned.<br/>
  30316. * It must be possible to cast the instance to the parent type of the member
  30317. * function. Otherwise, invoking the underlying function results in an
  30318. * undefined behavior.
  30319. *
  30320. * @param instance An opaque instance of the underlying type.
  30321. * @param args Parameters to use to invoke the function.
  30322. * @param sz Number of parameters to use to invoke the function.
  30323. * @return A wrapper containing the returned value, if any.
  30324. */
  30325. meta_any invoke(meta_handle instance, meta_any * const args, const size_type sz) const {
  30326. return sz == arity() ? node->invoke(std::move(instance), args) : meta_any{};
  30327. }
  30328. /**
  30329. * @copybrief invoke
  30330. *
  30331. * @sa invoke
  30332. *
  30333. * @tparam Args Types of arguments to use to invoke the function.
  30334. * @param instance An opaque instance of the underlying type.
  30335. * @param args Parameters to use to invoke the function.
  30336. * @return A wrapper containing the new instance, if any.
  30337. */
  30338. template<typename... Args>
  30339. meta_any invoke(meta_handle instance, Args &&... args) const {
  30340. meta_any arguments[sizeof...(Args) + 1u]{std::forward<Args>(args)...};
  30341. return invoke(std::move(instance), arguments, sizeof...(Args));
  30342. }
  30343. /*! @copydoc meta_ctor::prop */
  30344. [[nodiscard]] meta_range<meta_prop> prop() const ENTT_NOEXCEPT {
  30345. return node->prop;
  30346. }
  30347. /**
  30348. * @brief Returns the property associated with a given key.
  30349. * @param key The key to use to search for a property.
  30350. * @return The property associated with the given key, if any.
  30351. */
  30352. [[nodiscard]] meta_prop prop(meta_any key) const {
  30353. return internal::meta_visit<&node_type::prop>([&key](const auto *curr) { return curr->id == key; }, node);
  30354. }
  30355. /**
  30356. * @brief Returns true if an object is valid, false otherwise.
  30357. * @return True if the object is valid, false otherwise.
  30358. */
  30359. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  30360. return !(node == nullptr);
  30361. }
  30362. private:
  30363. const node_type *node;
  30364. };
  30365. /*! @brief Opaque wrapper for types. */
  30366. class meta_type {
  30367. static bool can_cast_or_convert(const internal::meta_type_node *type, const type_info info) ENTT_NOEXCEPT {
  30368. if(type->info == info) {
  30369. return true;
  30370. }
  30371. for(const auto *curr = type->conv; curr; curr = curr->next) {
  30372. if(curr->type()->info == info) {
  30373. return true;
  30374. }
  30375. }
  30376. for(const auto *curr = type->base; curr; curr = curr->next) {
  30377. if(auto *target = curr->type(); can_cast_or_convert(target, info)) {
  30378. return true;
  30379. }
  30380. }
  30381. return false;
  30382. }
  30383. template<typename... Args, auto... Index>
  30384. [[nodiscard]] static const internal::meta_ctor_node * ctor(const internal::meta_ctor_node *curr, std::index_sequence<Index...>) {
  30385. for(; curr; curr = curr->next) {
  30386. if(curr->arity == sizeof...(Args) && (can_cast_or_convert(internal::meta_info<Args>::resolve(), curr->arg(Index).info()) && ...)) {
  30387. return curr;
  30388. }
  30389. }
  30390. return nullptr;
  30391. }
  30392. template<auto... Member, typename Node>
  30393. void unregister_all(Node **curr) {
  30394. while(*curr) {
  30395. (unregister_all(&((*curr)->*Member)), ...);
  30396. *curr = std::exchange((*curr)->next, nullptr);
  30397. }
  30398. }
  30399. public:
  30400. /*! @brief Node type. */
  30401. using node_type = internal::meta_type_node;
  30402. /*! @brief Node type. */
  30403. using base_node_type = internal::meta_base_node;
  30404. /*! @brief Unsigned integer type. */
  30405. using size_type = typename node_type::size_type;
  30406. /*! @copydoc meta_prop::meta_prop */
  30407. meta_type(node_type *curr = nullptr) ENTT_NOEXCEPT
  30408. : node{curr}
  30409. {}
  30410. /**
  30411. * @brief Constructs an instance from a given base node.
  30412. * @param curr The base node with which to construct the instance.
  30413. */
  30414. meta_type(base_node_type *curr) ENTT_NOEXCEPT
  30415. : node{curr ? curr->type() : nullptr}
  30416. {}
  30417. /**
  30418. * @brief Returns the type info object of the underlying type.
  30419. * @return The type info object of the underlying type.
  30420. */
  30421. [[nodiscard]] type_info info() const ENTT_NOEXCEPT {
  30422. return node->info;
  30423. }
  30424. /**
  30425. * @brief Returns the identifier assigned to a type.
  30426. * @return The identifier assigned to the type.
  30427. */
  30428. [[nodiscard]] id_type id() const ENTT_NOEXCEPT {
  30429. return node->id;
  30430. }
  30431. /**
  30432. * @brief Returns the size of the underlying type if known.
  30433. * @return The size of the underlying type if known, 0 otherwise.
  30434. */
  30435. [[nodiscard]] size_type size_of() const ENTT_NOEXCEPT {
  30436. return node->size_of;
  30437. }
  30438. /**
  30439. * @brief Checks whether a type refers to void or not.
  30440. * @return True if the underlying type is void, false otherwise.
  30441. */
  30442. [[nodiscard]] bool is_void() const ENTT_NOEXCEPT {
  30443. return node->is_void;
  30444. }
  30445. /**
  30446. * @brief Checks whether a type refers to an integral type or not.
  30447. * @return True if the underlying type is an integral type, false otherwise.
  30448. */
  30449. [[nodiscard]] bool is_integral() const ENTT_NOEXCEPT {
  30450. return node->is_integral;
  30451. }
  30452. /**
  30453. * @brief Checks whether a type refers to a floating-point type or not.
  30454. * @return True if the underlying type is a floating-point type, false
  30455. * otherwise.
  30456. */
  30457. [[nodiscard]] bool is_floating_point() const ENTT_NOEXCEPT {
  30458. return node->is_floating_point;
  30459. }
  30460. /**
  30461. * @brief Checks whether a type refers to an array type or not.
  30462. * @return True if the underlying type is an array type, false otherwise.
  30463. */
  30464. [[nodiscard]] bool is_array() const ENTT_NOEXCEPT {
  30465. return node->is_array;
  30466. }
  30467. /**
  30468. * @brief Checks whether a type refers to an enum or not.
  30469. * @return True if the underlying type is an enum, false otherwise.
  30470. */
  30471. [[nodiscard]] bool is_enum() const ENTT_NOEXCEPT {
  30472. return node->is_enum;
  30473. }
  30474. /**
  30475. * @brief Checks whether a type refers to an union or not.
  30476. * @return True if the underlying type is an union, false otherwise.
  30477. */
  30478. [[nodiscard]] bool is_union() const ENTT_NOEXCEPT {
  30479. return node->is_union;
  30480. }
  30481. /**
  30482. * @brief Checks whether a type refers to a class or not.
  30483. * @return True if the underlying type is a class, false otherwise.
  30484. */
  30485. [[nodiscard]] bool is_class() const ENTT_NOEXCEPT {
  30486. return node->is_class;
  30487. }
  30488. /**
  30489. * @brief Checks whether a type refers to a pointer or not.
  30490. * @return True if the underlying type is a pointer, false otherwise.
  30491. */
  30492. [[nodiscard]] bool is_pointer() const ENTT_NOEXCEPT {
  30493. return node->is_pointer;
  30494. }
  30495. /**
  30496. * @brief Checks whether a type refers to a function pointer or not.
  30497. * @return True if the underlying type is a function pointer, false
  30498. * otherwise.
  30499. */
  30500. [[nodiscard]] bool is_function_pointer() const ENTT_NOEXCEPT {
  30501. return node->is_function_pointer;
  30502. }
  30503. /**
  30504. * @brief Checks whether a type refers to a pointer to data member or not.
  30505. * @return True if the underlying type is a pointer to data member, false
  30506. * otherwise.
  30507. */
  30508. [[nodiscard]] bool is_member_object_pointer() const ENTT_NOEXCEPT {
  30509. return node->is_member_object_pointer;
  30510. }
  30511. /**
  30512. * @brief Checks whether a type refers to a pointer to member function or
  30513. * not.
  30514. * @return True if the underlying type is a pointer to member function,
  30515. * false otherwise.
  30516. */
  30517. [[nodiscard]] bool is_member_function_pointer() const ENTT_NOEXCEPT {
  30518. return node->is_member_function_pointer;
  30519. }
  30520. /**
  30521. * @brief Checks whether a type is a pointer-like type or not.
  30522. * @return True if the underlying type is a pointer-like one, false
  30523. * otherwise.
  30524. */
  30525. [[nodiscard]] bool is_pointer_like() const ENTT_NOEXCEPT {
  30526. return node->is_pointer_like;
  30527. }
  30528. /**
  30529. * @brief Checks whether a type refers to a sequence container or not.
  30530. * @return True if the type is a sequence container, false otherwise.
  30531. */
  30532. [[nodiscard]] bool is_sequence_container() const ENTT_NOEXCEPT {
  30533. return node->is_sequence_container;
  30534. }
  30535. /**
  30536. * @brief Checks whether a type refers to an associative container or not.
  30537. * @return True if the type is an associative container, false otherwise.
  30538. */
  30539. [[nodiscard]] bool is_associative_container() const ENTT_NOEXCEPT {
  30540. return node->is_associative_container;
  30541. }
  30542. /**
  30543. * @brief Checks whether a type refers to a recognized class template
  30544. * specialization or not.
  30545. * @return True if the type is a recognized class template specialization,
  30546. * false otherwise.
  30547. */
  30548. [[nodiscard]] bool is_template_specialization() const ENTT_NOEXCEPT {
  30549. return node->template_info.is_template_specialization;
  30550. }
  30551. /**
  30552. * @brief Returns the number of template arguments, if any.
  30553. * @return The number of template arguments, if any.
  30554. */
  30555. [[nodiscard]] size_type template_arity() const ENTT_NOEXCEPT {
  30556. return node->template_info.arity;
  30557. }
  30558. /**
  30559. * @brief Returns a tag for the class template of the underlying type.
  30560. *
  30561. * @sa meta_class_template_tag
  30562. *
  30563. * @return The tag for the class template of the underlying type.
  30564. */
  30565. [[nodiscard]] inline meta_type template_type() const ENTT_NOEXCEPT {
  30566. return is_template_specialization() ? node->template_info.type() : meta_type{};
  30567. }
  30568. /**
  30569. * @brief Returns the type of the i-th template argument of a type.
  30570. * @param index Index of the template argument of which to return the type.
  30571. * @return The type of the i-th template argument of a type.
  30572. */
  30573. [[nodiscard]] inline meta_type template_arg(size_type index) const ENTT_NOEXCEPT {
  30574. return index < template_arity() ? node->template_info.arg(index) : meta_type{};
  30575. }
  30576. /**
  30577. * @brief Provides the number of dimensions of an array type.
  30578. * @return The number of dimensions in case of array types, 0 otherwise.
  30579. */
  30580. [[nodiscard]] size_type rank() const ENTT_NOEXCEPT {
  30581. return node->rank;
  30582. }
  30583. /**
  30584. * @brief The number of elements along the given dimension of an array type.
  30585. * @param dim The dimension of which to return the number of elements.
  30586. * @return The number of elements along the given dimension in case of array
  30587. * types, 0 otherwise.
  30588. */
  30589. [[nodiscard]] size_type extent(size_type dim = {}) const ENTT_NOEXCEPT {
  30590. return node->extent(dim);
  30591. }
  30592. /**
  30593. * @brief Provides the type for which the pointer is defined.
  30594. * @return The type for which the pointer is defined or this type if it
  30595. * doesn't refer to a pointer type.
  30596. */
  30597. [[nodiscard]] meta_type remove_pointer() const ENTT_NOEXCEPT {
  30598. return node->remove_pointer();
  30599. }
  30600. /**
  30601. * @brief Provides the type for which the array is defined.
  30602. * @return The type for which the array is defined or this type if it
  30603. * doesn't refer to an array type.
  30604. */
  30605. [[nodiscard]] meta_type remove_extent() const ENTT_NOEXCEPT {
  30606. return node->remove_extent();
  30607. }
  30608. /**
  30609. * @brief Returns a range to use to visit top-level base meta types.
  30610. * @return An iterable range to use to visit top-level base meta types.
  30611. */
  30612. [[nodiscard]] meta_range<meta_type, internal::meta_base_node> base() const ENTT_NOEXCEPT {
  30613. return node->base;
  30614. }
  30615. /**
  30616. * @brief Returns the base meta type associated with a given identifier.
  30617. * @param id Unique identifier.
  30618. * @return The base meta type associated with the given identifier, if any.
  30619. */
  30620. [[nodiscard]] meta_type base(const id_type id) const {
  30621. return internal::meta_visit<&node_type::base>([id](const auto *curr) { return curr->type()->id == id; }, node);
  30622. }
  30623. /**
  30624. * @brief Returns a range to use to visit top-level constructors.
  30625. * @return An iterable range to use to visit top-level constructors.
  30626. */
  30627. [[nodiscard]] meta_range<meta_ctor> ctor() const ENTT_NOEXCEPT {
  30628. return node->ctor;
  30629. }
  30630. /**
  30631. * @brief Returns a constructor for a given list of types of arguments.
  30632. * @tparam Args Constructor arguments.
  30633. * @return The requested constructor, if any.
  30634. */
  30635. template<typename... Args>
  30636. [[nodiscard]] meta_ctor ctor() const {
  30637. return ctor<Args...>(node->ctor, std::make_index_sequence<sizeof...(Args)>{});
  30638. }
  30639. /**
  30640. * @brief Returns a range to use to visit top-level data.
  30641. * @return An iterable range to use to visit top-level data.
  30642. */
  30643. [[nodiscard]] meta_range<meta_data> data() const ENTT_NOEXCEPT {
  30644. return node->data;
  30645. }
  30646. /**
  30647. * @brief Returns the data associated with a given identifier.
  30648. *
  30649. * The data of the base classes will also be visited, if any.
  30650. *
  30651. * @param id Unique identifier.
  30652. * @return The data associated with the given identifier, if any.
  30653. */
  30654. [[nodiscard]] meta_data data(const id_type id) const {
  30655. return internal::meta_visit<&node_type::data>([id](const auto *curr) { return curr->id == id; }, node);
  30656. }
  30657. /**
  30658. * @brief Returns a range to use to visit top-level functions.
  30659. * @return An iterable range to use to visit top-level functions.
  30660. */
  30661. [[nodiscard]] meta_range<meta_func> func() const ENTT_NOEXCEPT {
  30662. return node->func;
  30663. }
  30664. /**
  30665. * @brief Returns the function associated with a given identifier.
  30666. *
  30667. * The functions of the base classes will also be visited, if any.<br/>
  30668. * In the case of overloaded functions, the first one with the required
  30669. * identifier will be returned.
  30670. *
  30671. * @param id Unique identifier.
  30672. * @return The function associated with the given identifier, if any.
  30673. */
  30674. [[nodiscard]] meta_func func(const id_type id) const {
  30675. return internal::meta_visit<&node_type::func>([id](const auto *curr) { return curr->id == id; }, node);
  30676. }
  30677. /**
  30678. * @brief Creates an instance of the underlying type, if possible.
  30679. *
  30680. * Parameters must be such that a cast or conversion to the required types
  30681. * is possible. Otherwise, an empty and thus invalid wrapper is returned.
  30682. *
  30683. * @param args Parameters to use to construct the instance.
  30684. * @param sz Number of parameters to use to construct the instance.
  30685. * @return A wrapper containing the new instance, if any.
  30686. */
  30687. [[nodiscard]] meta_any construct(meta_any * const args, const size_type sz) const {
  30688. meta_any ret{};
  30689. internal::meta_visit<&node_type::ctor>([args, sz, &ret](const auto *curr) { return (curr->arity == sz) && (ret = curr->invoke(args)); }, node);
  30690. return ret;
  30691. }
  30692. /**
  30693. * @copybrief construct
  30694. *
  30695. * @sa construct
  30696. *
  30697. * @tparam Args Types of arguments to use to construct the instance.
  30698. * @param args Parameters to use to construct the instance.
  30699. * @return A wrapper containing the new instance, if any.
  30700. */
  30701. template<typename... Args>
  30702. [[nodiscard]] meta_any construct(Args &&... args) const {
  30703. meta_any arguments[sizeof...(Args) + 1u]{std::forward<Args>(args)...};
  30704. return construct(arguments, sizeof...(Args));
  30705. }
  30706. /**
  30707. * @brief Invokes a function given an identifier, if possible.
  30708. *
  30709. * It must be possible to cast the instance to the parent type of the member
  30710. * function. Otherwise, invoking the underlying function results in an
  30711. * undefined behavior.
  30712. *
  30713. * @sa meta_func::invoke
  30714. *
  30715. * @param id Unique identifier.
  30716. * @param instance An opaque instance of the underlying type.
  30717. * @param args Parameters to use to invoke the function.
  30718. * @param sz Number of parameters to use to invoke the function.
  30719. * @return A wrapper containing the returned value, if any.
  30720. */
  30721. meta_any invoke(const id_type id, meta_handle instance, meta_any * const args, const size_type sz) const {
  30722. const internal::meta_func_node* candidate{};
  30723. size_type extent{sz + 1u};
  30724. bool ambiguous{};
  30725. for(auto *it = internal::meta_visit<&node_type::func>([id, sz](const auto *curr) { return curr->id == id && curr->arity == sz; }, node); it && it->id == id && it->arity == sz; it = it->next) {
  30726. size_type direct{};
  30727. size_type ext{};
  30728. for(size_type next{}; next < sz && next == (direct + ext); ++next) {
  30729. const auto type = args[next].type();
  30730. const auto req = it->arg(next).info();
  30731. type.info() == req ? ++direct : (ext += can_cast_or_convert(type.node, req));
  30732. }
  30733. if((direct + ext) == sz) {
  30734. if(ext < extent) {
  30735. candidate = it;
  30736. extent = ext;
  30737. ambiguous = false;
  30738. } else if(ext == extent) {
  30739. ambiguous = true;
  30740. }
  30741. }
  30742. }
  30743. return (candidate && !ambiguous) ? candidate->invoke(std::move(instance), args) : meta_any{};
  30744. }
  30745. /**
  30746. * @copybrief invoke
  30747. *
  30748. * @sa invoke
  30749. *
  30750. * @param id Unique identifier.
  30751. * @tparam Args Types of arguments to use to invoke the function.
  30752. * @param instance An opaque instance of the underlying type.
  30753. * @param args Parameters to use to invoke the function.
  30754. * @return A wrapper containing the new instance, if any.
  30755. */
  30756. template<typename... Args>
  30757. meta_any invoke(const id_type id, meta_handle instance, Args &&... args) const {
  30758. meta_any arguments[sizeof...(Args) + 1u]{std::forward<Args>(args)...};
  30759. return invoke(id, std::move(instance), arguments, sizeof...(Args));
  30760. }
  30761. /**
  30762. * @brief Sets the value of a given variable.
  30763. *
  30764. * It must be possible to cast the instance to the parent type of the data
  30765. * member. Otherwise, invoking the setter results in an undefined
  30766. * behavior.<br/>
  30767. * The type of the value must be such that a cast or conversion to the type
  30768. * of the variable is possible. Otherwise, invoking the setter does nothing.
  30769. *
  30770. * @tparam Type Type of value to assign.
  30771. * @param id Unique identifier.
  30772. * @param instance An opaque instance of the underlying type.
  30773. * @param value Parameter to use to set the underlying variable.
  30774. * @return True in case of success, false otherwise.
  30775. */
  30776. template<typename Type>
  30777. bool set(const id_type id, meta_handle instance, Type &&value) const {
  30778. const auto candidate = data(id);
  30779. return candidate && candidate.set(std::move(instance), std::forward<Type>(value));
  30780. }
  30781. /**
  30782. * @brief Gets the value of a given variable.
  30783. *
  30784. * It must be possible to cast the instance to the parent type of the data
  30785. * member. Otherwise, invoking the getter results in an undefined behavior.
  30786. *
  30787. * @param id Unique identifier.
  30788. * @param instance An opaque instance of the underlying type.
  30789. * @return A wrapper containing the value of the underlying variable.
  30790. */
  30791. [[nodiscard]] meta_any get(const id_type id, meta_handle instance) const {
  30792. const auto candidate = data(id);
  30793. return candidate ? candidate.get(std::move(instance)) : meta_any{};
  30794. }
  30795. /**
  30796. * @brief Returns a range to use to visit top-level properties.
  30797. * @return An iterable range to use to visit top-level properties.
  30798. */
  30799. [[nodiscard]] meta_range<meta_prop> prop() const ENTT_NOEXCEPT {
  30800. return node->prop;
  30801. }
  30802. /**
  30803. * @brief Returns the property associated with a given key.
  30804. *
  30805. * Properties of the base classes will also be visited, if any.
  30806. *
  30807. * @param key The key to use to search for a property.
  30808. * @return The property associated with the given key, if any.
  30809. */
  30810. [[nodiscard]] meta_prop prop(meta_any key) const {
  30811. return internal::meta_visit<&node_type::prop>([&key](const auto *curr) { return curr->id == key; }, node);
  30812. }
  30813. /**
  30814. * @brief Returns true if an object is valid, false otherwise.
  30815. * @return True if the object is valid, false otherwise.
  30816. */
  30817. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  30818. return !(node == nullptr);
  30819. }
  30820. /**
  30821. * @brief Checks if two objects refer to the same type.
  30822. * @param other The object with which to compare.
  30823. * @return True if the objects refer to the same type, false otherwise.
  30824. */
  30825. [[nodiscard]] bool operator==(const meta_type &other) const ENTT_NOEXCEPT {
  30826. return (!node && !other.node) || (node && other.node && node->info == other.node->info);
  30827. }
  30828. /**
  30829. * @brief Resets a type and all its parts.
  30830. *
  30831. * This function resets a type and all its data members, member functions
  30832. * and properties, as well as its constructors, destructors and conversion
  30833. * functions if any.<br/>
  30834. * Base classes aren't reset but the link between the two types is removed.
  30835. *
  30836. * The type is also removed from the list of searchable types.
  30837. */
  30838. void reset() ENTT_NOEXCEPT {
  30839. for(auto** it = internal::meta_context::global(); *it; it = &(*it)->next) {
  30840. if(*it == node) {
  30841. *it = (*it)->next;
  30842. break;
  30843. }
  30844. }
  30845. unregister_all(&node->prop);
  30846. unregister_all(&node->base);
  30847. unregister_all(&node->conv);
  30848. unregister_all<&internal::meta_ctor_node::prop>(&node->ctor);
  30849. unregister_all<&internal::meta_data_node::prop>(&node->data);
  30850. unregister_all<&internal::meta_func_node::prop>(&node->func);
  30851. node->id = {};
  30852. node->ctor = node->def_ctor;
  30853. node->dtor = nullptr;
  30854. }
  30855. private:
  30856. node_type *node;
  30857. };
  30858. /**
  30859. * @brief Checks if two objects refer to the same type.
  30860. * @param lhs An object, either valid or not.
  30861. * @param rhs An object, either valid or not.
  30862. * @return False if the objects refer to the same node, true otherwise.
  30863. */
  30864. [[nodiscard]] inline bool operator!=(const meta_type &lhs, const meta_type &rhs) ENTT_NOEXCEPT {
  30865. return !(lhs == rhs);
  30866. }
  30867. [[nodiscard]] inline meta_type meta_any::type() const ENTT_NOEXCEPT {
  30868. return node;
  30869. }
  30870. template<typename... Args>
  30871. meta_any meta_any::invoke(const id_type id, Args &&... args) const {
  30872. return type().invoke(id, *this, std::forward<Args>(args)...);
  30873. }
  30874. template<typename... Args>
  30875. meta_any meta_any::invoke(const id_type id, Args &&... args) {
  30876. return type().invoke(id, *this, std::forward<Args>(args)...);
  30877. }
  30878. template<typename Type>
  30879. bool meta_any::set(const id_type id, Type &&value) {
  30880. return type().set(id, *this, std::forward<Type>(value));
  30881. }
  30882. [[nodiscard]] inline meta_any meta_any::get(const id_type id) const {
  30883. return type().get(id, *this);
  30884. }
  30885. [[nodiscard]] inline meta_any meta_any::get(const id_type id) {
  30886. return type().get(id, *this);
  30887. }
  30888. [[nodiscard]] inline meta_type meta_ctor::parent() const ENTT_NOEXCEPT {
  30889. return node->parent;
  30890. }
  30891. [[nodiscard]] inline meta_type meta_ctor::arg(size_type index) const ENTT_NOEXCEPT {
  30892. return index < arity() ? node->arg(index) : meta_type{};
  30893. }
  30894. [[nodiscard]] inline meta_type meta_data::parent() const ENTT_NOEXCEPT {
  30895. return node->parent;
  30896. }
  30897. [[nodiscard]] inline meta_type meta_data::type() const ENTT_NOEXCEPT {
  30898. return node->type();
  30899. }
  30900. [[nodiscard]] inline meta_type meta_func::parent() const ENTT_NOEXCEPT {
  30901. return node->parent;
  30902. }
  30903. [[nodiscard]] inline meta_type meta_func::ret() const ENTT_NOEXCEPT {
  30904. return node->ret();
  30905. }
  30906. [[nodiscard]] inline meta_type meta_func::arg(size_type index) const ENTT_NOEXCEPT {
  30907. return index < arity() ? node->arg(index) : meta_type{};
  30908. }
  30909. /*! @brief Opaque iterator for sequence containers. */
  30910. class meta_sequence_container::meta_iterator {
  30911. /*! @brief A sequence container can access the underlying iterator. */
  30912. friend class meta_sequence_container;
  30913. enum class operation { INCR, DEREF };
  30914. using vtable_type = void(const operation, const any &, void *);
  30915. template<typename It>
  30916. static void basic_vtable(const operation op, const any &from, void *to) {
  30917. switch(op) {
  30918. case operation::INCR:
  30919. ++any_cast<It &>(const_cast<any &>(from));
  30920. break;
  30921. case operation::DEREF:
  30922. static_cast<meta_any *>(to)->emplace<typename std::iterator_traits<It>::reference>(*any_cast<const It &>(from));
  30923. break;
  30924. }
  30925. }
  30926. public:
  30927. /*! @brief Signed integer type. */
  30928. using difference_type = std::ptrdiff_t;
  30929. /*! @brief Type of elements returned by the iterator. */
  30930. using value_type = meta_any;
  30931. /*! @brief Pointer type, `void` on purpose. */
  30932. using pointer = void;
  30933. /*! @brief Reference type, it is **not** an actual reference. */
  30934. using reference = value_type;
  30935. /*! @brief Iterator category. */
  30936. using iterator_category = std::input_iterator_tag;
  30937. /*! @brief Default constructor. */
  30938. meta_iterator() ENTT_NOEXCEPT = default;
  30939. /**
  30940. * @brief Constructs a meta iterator from a given iterator.
  30941. * @tparam It Type of actual iterator with which to build the meta iterator.
  30942. * @param iter The actual iterator with which to build the meta iterator.
  30943. */
  30944. template<typename It>
  30945. meta_iterator(It iter)
  30946. : vtable{&basic_vtable<It>},
  30947. handle{std::move(iter)}
  30948. {}
  30949. /*! @brief Pre-increment operator. @return This iterator. */
  30950. meta_iterator & operator++() ENTT_NOEXCEPT {
  30951. return vtable(operation::INCR, handle, nullptr), *this;
  30952. }
  30953. /*! @brief Post-increment operator. @return This iterator. */
  30954. meta_iterator operator++(int) ENTT_NOEXCEPT {
  30955. meta_iterator orig = *this;
  30956. return ++(*this), orig;
  30957. }
  30958. /**
  30959. * @brief Checks if two iterators refer to the same element.
  30960. * @param other The iterator with which to compare.
  30961. * @return True if the iterators refer to the same element, false otherwise.
  30962. */
  30963. [[nodiscard]] bool operator==(const meta_iterator &other) const ENTT_NOEXCEPT {
  30964. return handle == other.handle;
  30965. }
  30966. /**
  30967. * @brief Checks if two iterators refer to the same element.
  30968. * @param other The iterator with which to compare.
  30969. * @return False if the iterators refer to the same element, true otherwise.
  30970. */
  30971. [[nodiscard]] bool operator!=(const meta_iterator &other) const ENTT_NOEXCEPT {
  30972. return !(*this == other);
  30973. }
  30974. /**
  30975. * @brief Indirection operator.
  30976. * @return The element to which the iterator points.
  30977. */
  30978. [[nodiscard]] reference operator*() const {
  30979. meta_any other;
  30980. vtable(operation::DEREF, handle, &other);
  30981. return other;
  30982. }
  30983. /**
  30984. * @brief Returns false if an iterator is invalid, true otherwise.
  30985. * @return False if the iterator is invalid, true otherwise.
  30986. */
  30987. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  30988. return static_cast<bool>(handle);
  30989. }
  30990. private:
  30991. vtable_type *vtable{};
  30992. any handle{};
  30993. };
  30994. template<typename Type>
  30995. struct meta_sequence_container::meta_sequence_container_proxy {
  30996. using traits_type = meta_sequence_container_traits<Type>;
  30997. [[nodiscard]] static meta_type value_type() ENTT_NOEXCEPT {
  30998. return internal::meta_info<typename Type::value_type>::resolve();
  30999. }
  31000. [[nodiscard]] static size_type size(const any &container) ENTT_NOEXCEPT {
  31001. return traits_type::size(any_cast<const Type &>(container));
  31002. }
  31003. [[nodiscard]] static bool resize(any &container, size_type sz) {
  31004. auto * const cont = any_cast<Type>(&container);
  31005. return cont && traits_type::resize(*cont, sz);
  31006. }
  31007. [[nodiscard]] static bool clear(any &container) {
  31008. auto * const cont = any_cast<Type>(&container);
  31009. return cont && traits_type::clear(*cont);
  31010. }
  31011. [[nodiscard]] static iterator begin(any &container) {
  31012. if(auto * const cont = any_cast<Type>(&container); cont) {
  31013. return iterator{traits_type::begin(*cont)};
  31014. }
  31015. return iterator{traits_type::cbegin(any_cast<const Type &>(container))};
  31016. }
  31017. [[nodiscard]] static iterator end(any &container) {
  31018. if(auto * const cont = any_cast<Type>(&container); cont) {
  31019. return iterator{traits_type::end(*cont)};
  31020. }
  31021. return iterator{traits_type::cend(any_cast<const Type &>(container))};
  31022. }
  31023. [[nodiscard]] static std::pair<iterator, bool> insert(any &container, iterator it, meta_any &value) {
  31024. if(auto * const cont = any_cast<Type>(&container); cont) {
  31025. // this abomination is necessary because only on macos value_type and const_reference are different types for std::vector<bool>
  31026. if(value.allow_cast<typename Type::const_reference>() || value.allow_cast<typename Type::value_type>()) {
  31027. const auto *element = value.try_cast<std::remove_reference_t<typename Type::const_reference>>();
  31028. auto ret = traits_type::insert(*cont, any_cast<const typename Type::iterator &>(it.handle), element ? *element : value.cast<typename Type::value_type>());
  31029. return { iterator{std::move(ret.first)}, ret.second };
  31030. }
  31031. }
  31032. return {};
  31033. }
  31034. [[nodiscard]] static std::pair<iterator, bool> erase(any &container, iterator it) {
  31035. if(auto * const cont = any_cast<Type>(&container); cont) {
  31036. auto ret = traits_type::erase(*cont, any_cast<const typename Type::iterator &>(it.handle));
  31037. return { iterator{std::move(ret.first)}, ret.second };
  31038. }
  31039. return {};
  31040. }
  31041. [[nodiscard]] static meta_any get(any &container, size_type pos) {
  31042. if(auto * const cont = any_cast<Type>(&container); cont) {
  31043. return meta_any{std::in_place_type<typename Type::reference>, traits_type::get(*cont, pos)};
  31044. }
  31045. return meta_any{std::in_place_type<typename Type::const_reference>, traits_type::cget(any_cast<const Type &>(container), pos)};
  31046. }
  31047. };
  31048. /**
  31049. * @brief Returns the meta value type of a container.
  31050. * @return The meta value type of the container.
  31051. */
  31052. [[nodiscard]] inline meta_type meta_sequence_container::value_type() const ENTT_NOEXCEPT {
  31053. return value_type_fn();
  31054. }
  31055. /**
  31056. * @brief Returns the size of a container.
  31057. * @return The size of the container.
  31058. */
  31059. [[nodiscard]] inline meta_sequence_container::size_type meta_sequence_container::size() const ENTT_NOEXCEPT {
  31060. return size_fn(storage);
  31061. }
  31062. /**
  31063. * @brief Resizes a container to contain a given number of elements.
  31064. * @param sz The new size of the container.
  31065. * @return True in case of success, false otherwise.
  31066. */
  31067. inline bool meta_sequence_container::resize(size_type sz) {
  31068. return resize_fn(storage, sz);
  31069. }
  31070. /**
  31071. * @brief Clears the content of a container.
  31072. * @return True in case of success, false otherwise.
  31073. */
  31074. inline bool meta_sequence_container::clear() {
  31075. return clear_fn(storage);
  31076. }
  31077. /**
  31078. * @brief Returns an iterator to the first element of a container.
  31079. * @return An iterator to the first element of the container.
  31080. */
  31081. [[nodiscard]] inline meta_sequence_container::iterator meta_sequence_container::begin() {
  31082. return begin_fn(storage);
  31083. }
  31084. /**
  31085. * @brief Returns an iterator that is past the last element of a container.
  31086. * @return An iterator that is past the last element of the container.
  31087. */
  31088. [[nodiscard]] inline meta_sequence_container::iterator meta_sequence_container::end() {
  31089. return end_fn(storage);
  31090. }
  31091. /**
  31092. * @brief Inserts an element at a specified location of a container.
  31093. * @param it Iterator before which the element will be inserted.
  31094. * @param value Element value to insert.
  31095. * @return A pair consisting of an iterator to the inserted element (in case of
  31096. * success) and a bool denoting whether the insertion took place.
  31097. */
  31098. inline std::pair<meta_sequence_container::iterator, bool> meta_sequence_container::insert(iterator it, meta_any value) {
  31099. return insert_fn(storage, it, value);
  31100. }
  31101. /**
  31102. * @brief Removes a given element from a container.
  31103. * @param it Iterator to the element to remove.
  31104. * @return A pair consisting of an iterator following the last removed element
  31105. * (in case of success) and a bool denoting whether the insertion took place.
  31106. */
  31107. inline std::pair<meta_sequence_container::iterator, bool> meta_sequence_container::erase(iterator it) {
  31108. return erase_fn(storage, it);
  31109. }
  31110. /**
  31111. * @brief Returns a reference to the element at a given location of a container
  31112. * (no bounds checking is performed).
  31113. * @param pos The position of the element to return.
  31114. * @return A reference to the requested element properly wrapped.
  31115. */
  31116. [[nodiscard]] inline meta_any meta_sequence_container::operator[](size_type pos) {
  31117. return get_fn(storage, pos);
  31118. }
  31119. /**
  31120. * @brief Returns false if a proxy is invalid, true otherwise.
  31121. * @return False if the proxy is invalid, true otherwise.
  31122. */
  31123. [[nodiscard]] inline meta_sequence_container::operator bool() const ENTT_NOEXCEPT {
  31124. return static_cast<bool>(storage);
  31125. }
  31126. /*! @brief Opaque iterator for associative containers. */
  31127. class meta_associative_container::meta_iterator {
  31128. enum operation { INCR, DEREF };
  31129. using vtable_type = void(const operation, const any &, void *);
  31130. template<bool KeyOnly, typename It>
  31131. static void basic_vtable(const operation op, const any &from, void *to) {
  31132. switch(op) {
  31133. case operation::INCR:
  31134. ++any_cast<It &>(const_cast<any &>(from));
  31135. break;
  31136. case operation::DEREF:
  31137. const auto &it = any_cast<const It &>(from);
  31138. if constexpr(KeyOnly) {
  31139. static_cast<std::pair<meta_any, meta_any> *>(to)->first.emplace<decltype(*it)>(*it);
  31140. } else {
  31141. static_cast<std::pair<meta_any, meta_any> *>(to)->first.emplace<decltype((it->first))>(it->first);
  31142. static_cast<std::pair<meta_any, meta_any> *>(to)->second.emplace<decltype((it->second))>(it->second);
  31143. }
  31144. break;
  31145. }
  31146. }
  31147. public:
  31148. /*! @brief Signed integer type. */
  31149. using difference_type = std::ptrdiff_t;
  31150. /*! @brief Type of elements returned by the iterator. */
  31151. using value_type = std::pair<meta_any, meta_any>;
  31152. /*! @brief Pointer type, `void` on purpose. */
  31153. using pointer = void;
  31154. /*! @brief Reference type, it is **not** an actual reference. */
  31155. using reference = value_type;
  31156. /*! @brief Iterator category. */
  31157. using iterator_category = std::input_iterator_tag;
  31158. /*! @brief Default constructor. */
  31159. meta_iterator() ENTT_NOEXCEPT = default;
  31160. /**
  31161. * @brief Constructs an meta iterator from a given iterator.
  31162. * @tparam KeyOnly True if the container is also key-only, false otherwise.
  31163. * @tparam It Type of actual iterator with which to build the meta iterator.
  31164. * @param iter The actual iterator with which to build the meta iterator.
  31165. */
  31166. template<bool KeyOnly, typename It>
  31167. meta_iterator(std::integral_constant<bool, KeyOnly>, It iter)
  31168. : vtable{&basic_vtable<KeyOnly, It>},
  31169. handle{std::move(iter)}
  31170. {}
  31171. /*! @brief Pre-increment operator. @return This iterator. */
  31172. meta_iterator & operator++() ENTT_NOEXCEPT {
  31173. return vtable(operation::INCR, handle, nullptr), *this;
  31174. }
  31175. /*! @brief Post-increment operator. @return This iterator. */
  31176. meta_iterator operator++(int) ENTT_NOEXCEPT {
  31177. meta_iterator orig = *this;
  31178. return ++(*this), orig;
  31179. }
  31180. /**
  31181. * @brief Checks if two iterators refer to the same element.
  31182. * @param other The iterator with which to compare.
  31183. * @return True if the iterators refer to the same element, false otherwise.
  31184. */
  31185. [[nodiscard]] bool operator==(const meta_iterator &other) const ENTT_NOEXCEPT {
  31186. return handle == other.handle;
  31187. }
  31188. /**
  31189. * @brief Checks if two iterators refer to the same element.
  31190. * @param other The iterator with which to compare.
  31191. * @return False if the iterators refer to the same element, true otherwise.
  31192. */
  31193. [[nodiscard]] bool operator!=(const meta_iterator &other) const ENTT_NOEXCEPT {
  31194. return !(*this == other);
  31195. }
  31196. /**
  31197. * @brief Indirection operator.
  31198. * @return The element to which the iterator points.
  31199. */
  31200. [[nodiscard]] reference operator*() const {
  31201. reference other;
  31202. vtable(operation::DEREF, handle, &other);
  31203. return other;
  31204. }
  31205. /**
  31206. * @brief Returns false if an iterator is invalid, true otherwise.
  31207. * @return False if the iterator is invalid, true otherwise.
  31208. */
  31209. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  31210. return static_cast<bool>(handle);
  31211. }
  31212. private:
  31213. vtable_type *vtable{};
  31214. any handle{};
  31215. };
  31216. template<typename Type>
  31217. struct meta_associative_container::meta_associative_container_proxy {
  31218. using traits_type = meta_associative_container_traits<Type>;
  31219. [[nodiscard]] static meta_type key_type() ENTT_NOEXCEPT {
  31220. return internal::meta_info<typename Type::key_type>::resolve();
  31221. }
  31222. [[nodiscard]] static meta_type mapped_type() ENTT_NOEXCEPT {
  31223. if constexpr(is_key_only_meta_associative_container_v<Type>) {
  31224. return meta_type{};
  31225. } else {
  31226. return internal::meta_info<typename Type::mapped_type>::resolve();
  31227. }
  31228. }
  31229. [[nodiscard]] static meta_type value_type() ENTT_NOEXCEPT {
  31230. return internal::meta_info<typename Type::value_type>::resolve();
  31231. }
  31232. [[nodiscard]] static size_type size(const any &container) ENTT_NOEXCEPT {
  31233. return traits_type::size(any_cast<const Type &>(container));
  31234. }
  31235. [[nodiscard]] static bool clear(any &container) {
  31236. auto * const cont = any_cast<Type>(&container);
  31237. return cont && traits_type::clear(*cont);
  31238. }
  31239. [[nodiscard]] static iterator begin(any &container) {
  31240. if(auto * const cont = any_cast<Type>(&container); cont) {
  31241. return iterator{is_key_only_meta_associative_container<Type>{}, traits_type::begin(*cont)};
  31242. }
  31243. return iterator{is_key_only_meta_associative_container<Type>{}, traits_type::cbegin(any_cast<const Type &>(container))};
  31244. }
  31245. [[nodiscard]] static iterator end(any &container) {
  31246. if(auto * const cont = any_cast<Type>(&container); cont) {
  31247. return iterator{is_key_only_meta_associative_container<Type>{}, traits_type::end(*cont)};
  31248. }
  31249. return iterator{is_key_only_meta_associative_container<Type>{}, traits_type::cend(any_cast<const Type &>(container))};
  31250. }
  31251. [[nodiscard]] static bool insert(any &container, meta_any &key, meta_any &value) {
  31252. if(auto * const cont = any_cast<Type>(&container); cont && key.allow_cast<const typename Type::key_type &>()) {
  31253. if constexpr(is_key_only_meta_associative_container_v<Type>) {
  31254. return traits_type::insert(*cont, key.cast<const typename Type::key_type &>());
  31255. } else {
  31256. return value.allow_cast<const typename Type::mapped_type &>()
  31257. && traits_type::insert(*cont, key.cast<const typename Type::key_type &>(), value.cast<const typename Type::mapped_type &>());
  31258. }
  31259. }
  31260. return false;
  31261. }
  31262. [[nodiscard]] static bool erase(any &container, meta_any &key) {
  31263. if(auto * const cont = any_cast<Type>(&container); cont && key.allow_cast<const typename Type::key_type &>()) {
  31264. return traits_type::erase(*cont, key.cast<const typename Type::key_type &>());
  31265. }
  31266. return false;
  31267. }
  31268. [[nodiscard]] static iterator find(any &container, meta_any &key) {
  31269. if(key.allow_cast<const typename Type::key_type &>()) {
  31270. if(auto * const cont = any_cast<Type>(&container); cont) {
  31271. return iterator{is_key_only_meta_associative_container<Type>{}, traits_type::find(*cont, key.cast<const typename Type::key_type &>())};
  31272. }
  31273. return iterator{is_key_only_meta_associative_container<Type>{}, traits_type::cfind(any_cast<const Type &>(container), key.cast<const typename Type::key_type &>())};
  31274. }
  31275. return {};
  31276. }
  31277. };
  31278. /**
  31279. * @brief Returns true if a container is also key-only, false otherwise.
  31280. * @return True if the associative container is also key-only, false otherwise.
  31281. */
  31282. [[nodiscard]] inline bool meta_associative_container::key_only() const ENTT_NOEXCEPT {
  31283. return key_only_container;
  31284. }
  31285. /**
  31286. * @brief Returns the meta key type of a container.
  31287. * @return The meta key type of the a container.
  31288. */
  31289. [[nodiscard]] inline meta_type meta_associative_container::key_type() const ENTT_NOEXCEPT {
  31290. return key_type_fn();
  31291. }
  31292. /**
  31293. * @brief Returns the meta mapped type of a container.
  31294. * @return The meta mapped type of the a container.
  31295. */
  31296. [[nodiscard]] inline meta_type meta_associative_container::mapped_type() const ENTT_NOEXCEPT {
  31297. return mapped_type_fn();
  31298. }
  31299. /*! @copydoc meta_sequence_container::value_type */
  31300. [[nodiscard]] inline meta_type meta_associative_container::value_type() const ENTT_NOEXCEPT {
  31301. return value_type_fn();
  31302. }
  31303. /*! @copydoc meta_sequence_container::size */
  31304. [[nodiscard]] inline meta_associative_container::size_type meta_associative_container::size() const ENTT_NOEXCEPT {
  31305. return size_fn(storage);
  31306. }
  31307. /*! @copydoc meta_sequence_container::clear */
  31308. inline bool meta_associative_container::clear() {
  31309. return clear_fn(storage);
  31310. }
  31311. /*! @copydoc meta_sequence_container::begin */
  31312. [[nodiscard]] inline meta_associative_container::iterator meta_associative_container::begin() {
  31313. return begin_fn(storage);
  31314. }
  31315. /*! @copydoc meta_sequence_container::end */
  31316. [[nodiscard]] inline meta_associative_container::iterator meta_associative_container::end() {
  31317. return end_fn(storage);
  31318. }
  31319. /**
  31320. * @brief Inserts an element (a key/value pair) into a container.
  31321. * @param key The key of the element to insert.
  31322. * @param value The value of the element to insert.
  31323. * @return A bool denoting whether the insertion took place.
  31324. */
  31325. inline bool meta_associative_container::insert(meta_any key, meta_any value = {}) {
  31326. return insert_fn(storage, key, value);
  31327. }
  31328. /**
  31329. * @brief Removes the specified element from a container.
  31330. * @param key The key of the element to remove.
  31331. * @return A bool denoting whether the removal took place.
  31332. */
  31333. inline bool meta_associative_container::erase(meta_any key) {
  31334. return erase_fn(storage, key);
  31335. }
  31336. /**
  31337. * @brief Returns an iterator to the element with a given key, if any.
  31338. * @param key The key of the element to search.
  31339. * @return An iterator to the element with the given key, if any.
  31340. */
  31341. [[nodiscard]] inline meta_associative_container::iterator meta_associative_container::find(meta_any key) {
  31342. return find_fn(storage, key);
  31343. }
  31344. /**
  31345. * @brief Returns false if a proxy is invalid, true otherwise.
  31346. * @return False if the proxy is invalid, true otherwise.
  31347. */
  31348. [[nodiscard]] inline meta_associative_container::operator bool() const ENTT_NOEXCEPT {
  31349. return static_cast<bool>(storage);
  31350. }
  31351. }
  31352. #endif
  31353. // #include "meta/node.hpp"
  31354. #ifndef ENTT_META_NODE_HPP
  31355. #define ENTT_META_NODE_HPP
  31356. #include <cstddef>
  31357. #include <type_traits>
  31358. #include <utility>
  31359. // #include "../config/config.h"
  31360. // #include "../core/attribute.h"
  31361. // #include "../core/fwd.hpp"
  31362. // #include "../core/type_info.hpp"
  31363. // #include "../core/type_traits.hpp"
  31364. // #include "type_traits.hpp"
  31365. namespace entt {
  31366. class meta_any;
  31367. class meta_type;
  31368. struct meta_handle;
  31369. /**
  31370. * @cond TURN_OFF_DOXYGEN
  31371. * Internal details not to be documented.
  31372. */
  31373. namespace internal {
  31374. struct meta_type_node;
  31375. struct meta_prop_node {
  31376. meta_prop_node * next;
  31377. const meta_any &id;
  31378. meta_any &value;
  31379. };
  31380. struct meta_base_node {
  31381. meta_type_node * const parent;
  31382. meta_base_node * next;
  31383. meta_type_node *(* const type)() ENTT_NOEXCEPT;
  31384. const void *(* const cast)(const void *) ENTT_NOEXCEPT;
  31385. };
  31386. struct meta_conv_node {
  31387. meta_type_node * const parent;
  31388. meta_conv_node * next;
  31389. meta_type_node *(* const type)() ENTT_NOEXCEPT;
  31390. meta_any(* const conv)(const void *);
  31391. };
  31392. struct meta_ctor_node {
  31393. using size_type = std::size_t;
  31394. meta_type_node * const parent;
  31395. meta_ctor_node * next;
  31396. meta_prop_node * prop;
  31397. const size_type arity;
  31398. meta_type(* const arg)(const size_type) ENTT_NOEXCEPT;
  31399. meta_any(* const invoke)(meta_any * const);
  31400. };
  31401. struct meta_data_node {
  31402. id_type id;
  31403. meta_type_node * const parent;
  31404. meta_data_node * next;
  31405. meta_prop_node * prop;
  31406. const bool is_const;
  31407. const bool is_static;
  31408. meta_type_node *(* const type)() ENTT_NOEXCEPT;
  31409. bool(* const set)(meta_handle, meta_any);
  31410. meta_any(* const get)(meta_handle);
  31411. };
  31412. struct meta_func_node {
  31413. using size_type = std::size_t;
  31414. id_type id;
  31415. meta_type_node * const parent;
  31416. meta_func_node * next;
  31417. meta_prop_node * prop;
  31418. const size_type arity;
  31419. const bool is_const;
  31420. const bool is_static;
  31421. meta_type_node *(* const ret)() ENTT_NOEXCEPT;
  31422. meta_type(* const arg)(const size_type) ENTT_NOEXCEPT;
  31423. meta_any(* const invoke)(meta_handle, meta_any *);
  31424. };
  31425. struct meta_template_info {
  31426. using size_type = std::size_t;
  31427. const bool is_template_specialization;
  31428. const size_type arity;
  31429. meta_type_node *(* const type)() ENTT_NOEXCEPT;
  31430. meta_type_node *(* const arg)(const size_type) ENTT_NOEXCEPT;
  31431. };
  31432. struct meta_type_node {
  31433. using size_type = std::size_t;
  31434. const type_info info;
  31435. id_type id;
  31436. meta_type_node * next;
  31437. meta_prop_node * prop;
  31438. const size_type size_of;
  31439. const bool is_void;
  31440. const bool is_integral;
  31441. const bool is_floating_point;
  31442. const bool is_array;
  31443. const bool is_enum;
  31444. const bool is_union;
  31445. const bool is_class;
  31446. const bool is_pointer;
  31447. const bool is_function_pointer;
  31448. const bool is_member_object_pointer;
  31449. const bool is_member_function_pointer;
  31450. const bool is_pointer_like;
  31451. const bool is_sequence_container;
  31452. const bool is_associative_container;
  31453. const meta_template_info template_info;
  31454. const size_type rank;
  31455. size_type(* const extent)(const size_type) ENTT_NOEXCEPT ;
  31456. meta_type_node *(* const remove_pointer)() ENTT_NOEXCEPT;
  31457. meta_type_node *(* const remove_extent)() ENTT_NOEXCEPT;
  31458. meta_ctor_node * const def_ctor;
  31459. meta_ctor_node *ctor{nullptr};
  31460. meta_base_node *base{nullptr};
  31461. meta_conv_node *conv{nullptr};
  31462. meta_data_node *data{nullptr};
  31463. meta_func_node *func{nullptr};
  31464. void(* dtor)(void *){nullptr};
  31465. };
  31466. template<auto Member, typename Op, typename Node>
  31467. auto meta_visit(const Op &op, const Node *node)
  31468. -> std::decay_t<decltype(node->*Member)> {
  31469. for(auto *curr = node->*Member; curr; curr = curr->next) {
  31470. if(op(curr)) {
  31471. return curr;
  31472. }
  31473. }
  31474. if constexpr(std::is_same_v<Node, meta_type_node>) {
  31475. for(auto *curr = node->base; curr; curr = curr->next) {
  31476. if(auto *ret = meta_visit<Member>(op, curr->type()); ret) {
  31477. return ret;
  31478. }
  31479. }
  31480. }
  31481. return nullptr;
  31482. }
  31483. template<typename... Args>
  31484. meta_type_node * meta_arg_node(type_list<Args...>, const std::size_t index) ENTT_NOEXCEPT;
  31485. template<typename Type>
  31486. class ENTT_API meta_node {
  31487. static_assert(std::is_same_v<Type, std::remove_cv_t<std::remove_reference_t<Type>>>, "Invalid type");
  31488. template<std::size_t... Index>
  31489. [[nodiscard]] static auto extent(const meta_type_node::size_type dim, std::index_sequence<Index...>) ENTT_NOEXCEPT {
  31490. meta_type_node::size_type ext{};
  31491. ((ext = (dim == Index ? std::extent_v<Type, Index> : ext)), ...);
  31492. return ext;
  31493. }
  31494. [[nodiscard]] static meta_ctor_node * meta_default_constructor([[maybe_unused]] meta_type_node *type) ENTT_NOEXCEPT {
  31495. if constexpr(std::is_default_constructible_v<Type>) {
  31496. static meta_ctor_node node{
  31497. type,
  31498. nullptr,
  31499. nullptr,
  31500. 0u,
  31501. nullptr,
  31502. [](meta_any * const) { return meta_any{std::in_place_type<Type>}; }
  31503. };
  31504. return &node;
  31505. } else {
  31506. return nullptr;
  31507. }
  31508. }
  31509. [[nodiscard]] static meta_template_info meta_template_descriptor() ENTT_NOEXCEPT {
  31510. if constexpr(is_complete_v<meta_template_traits<Type>>) {
  31511. return {
  31512. true,
  31513. meta_template_traits<Type>::args_type::size,
  31514. &meta_node<typename meta_template_traits<Type>::class_type>::resolve,
  31515. [](const std::size_t index) ENTT_NOEXCEPT {
  31516. return meta_arg_node(typename meta_template_traits<Type>::args_type{}, index);
  31517. }
  31518. };
  31519. } else {
  31520. return { false, 0u, nullptr, nullptr };
  31521. }
  31522. }
  31523. public:
  31524. [[nodiscard]] static meta_type_node * resolve() ENTT_NOEXCEPT {
  31525. static meta_type_node node{
  31526. type_id<Type>(),
  31527. {},
  31528. nullptr,
  31529. nullptr,
  31530. size_of_v<Type>,
  31531. std::is_void_v<Type>,
  31532. std::is_integral_v<Type>,
  31533. std::is_floating_point_v<Type>,
  31534. std::is_array_v<Type>,
  31535. std::is_enum_v<Type>,
  31536. std::is_union_v<Type>,
  31537. std::is_class_v<Type>,
  31538. std::is_pointer_v<Type>,
  31539. std::is_pointer_v<Type> && std::is_function_v<std::remove_pointer_t<Type>>,
  31540. std::is_member_object_pointer_v<Type>,
  31541. std::is_member_function_pointer_v<Type>,
  31542. is_meta_pointer_like_v<Type>,
  31543. is_complete_v<meta_sequence_container_traits<Type>>,
  31544. is_complete_v<meta_associative_container_traits<Type>>,
  31545. meta_template_descriptor(),
  31546. std::rank_v<Type>,
  31547. [](meta_type_node::size_type dim) ENTT_NOEXCEPT { return extent(dim, std::make_index_sequence<std::rank_v<Type>>{}); },
  31548. &meta_node<std::remove_cv_t<std::remove_reference_t<std::remove_pointer_t<Type>>>>::resolve,
  31549. &meta_node<std::remove_cv_t<std::remove_reference_t<std::remove_extent_t<Type>>>>::resolve,
  31550. meta_default_constructor(&node),
  31551. meta_default_constructor(&node)
  31552. };
  31553. return &node;
  31554. }
  31555. };
  31556. template<typename Type>
  31557. struct meta_info: meta_node<std::remove_cv_t<std::remove_reference_t<Type>>> {};
  31558. template<typename... Args>
  31559. meta_type_node * meta_arg_node(type_list<Args...>, const std::size_t index) ENTT_NOEXCEPT {
  31560. meta_type_node *args[sizeof...(Args) + 1u]{nullptr, internal::meta_info<Args>::resolve()...};
  31561. return args[index + 1u];
  31562. }
  31563. }
  31564. /**
  31565. * Internal details not to be documented.
  31566. * @endcond
  31567. */
  31568. }
  31569. #endif
  31570. // #include "meta/pointer.hpp"
  31571. #ifndef ENTT_META_POINTER_HPP
  31572. #define ENTT_META_POINTER_HPP
  31573. #include <memory>
  31574. #include <type_traits>
  31575. // #include "type_traits.hpp"
  31576. namespace entt {
  31577. /**
  31578. * @brief Makes plain pointers pointer-like types for the meta system.
  31579. * @tparam Type Element type.
  31580. */
  31581. template<typename Type>
  31582. struct is_meta_pointer_like<Type *>
  31583. : std::true_type
  31584. {};
  31585. /**
  31586. * @brief Partial specialization used to reject pointers to arrays.
  31587. * @tparam Type Type of elements of the array.
  31588. * @tparam N Number of elements of the array.
  31589. */
  31590. template<typename Type, std::size_t N>
  31591. struct is_meta_pointer_like<Type(*)[N]>
  31592. : std::false_type
  31593. {};
  31594. /**
  31595. * @brief Makes `std::shared_ptr`s of any type pointer-like types for the meta
  31596. * system.
  31597. * @tparam Type Element type.
  31598. */
  31599. template<typename Type>
  31600. struct is_meta_pointer_like<std::shared_ptr<Type>>
  31601. : std::true_type
  31602. {};
  31603. /**
  31604. * @brief Makes `std::unique_ptr`s of any type pointer-like types for the meta
  31605. * system.
  31606. * @tparam Type Element type.
  31607. * @tparam Args Other arguments.
  31608. */
  31609. template<typename Type, typename... Args>
  31610. struct is_meta_pointer_like<std::unique_ptr<Type, Args...>>
  31611. : std::true_type
  31612. {};
  31613. }
  31614. #endif
  31615. // #include "meta/policy.hpp"
  31616. #ifndef ENTT_META_POLICY_HPP
  31617. #define ENTT_META_POLICY_HPP
  31618. namespace entt {
  31619. /*! @brief Empty class type used to request the _as ref_ policy. */
  31620. struct as_ref_t {};
  31621. /*! @brief Empty class type used to request the _as cref_ policy. */
  31622. struct as_cref_t {};
  31623. /*! @brief Empty class type used to request the _as-is_ policy. */
  31624. struct as_is_t {};
  31625. /*! @brief Empty class type used to request the _as void_ policy. */
  31626. struct as_void_t {};
  31627. }
  31628. #endif
  31629. // #include "meta/range.hpp"
  31630. #ifndef ENTT_META_RANGE_HPP
  31631. #define ENTT_META_RANGE_HPP
  31632. #include <cstddef>
  31633. #include <iterator>
  31634. namespace entt {
  31635. /**
  31636. * @brief Iterable range to use to iterate all types of meta objects.
  31637. * @tparam Type Type of meta objects returned.
  31638. * @tparam Node Type of meta nodes iterated.
  31639. */
  31640. template<typename Type, typename Node = typename Type::node_type>
  31641. class meta_range {
  31642. struct range_iterator {
  31643. using difference_type = std::ptrdiff_t;
  31644. using value_type = Type;
  31645. using pointer = void;
  31646. using reference = value_type;
  31647. using iterator_category = std::input_iterator_tag;
  31648. using node_type = Node;
  31649. range_iterator() ENTT_NOEXCEPT = default;
  31650. range_iterator(node_type *head) ENTT_NOEXCEPT
  31651. : it{head}
  31652. {}
  31653. range_iterator & operator++() ENTT_NOEXCEPT {
  31654. return (it = it->next), *this;
  31655. }
  31656. range_iterator operator++(int) ENTT_NOEXCEPT {
  31657. range_iterator orig = *this;
  31658. return ++(*this), orig;
  31659. }
  31660. [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
  31661. return it;
  31662. }
  31663. [[nodiscard]] bool operator==(const range_iterator &other) const ENTT_NOEXCEPT {
  31664. return other.it == it;
  31665. }
  31666. [[nodiscard]] bool operator!=(const range_iterator &other) const ENTT_NOEXCEPT {
  31667. return !(*this == other);
  31668. }
  31669. private:
  31670. node_type *it{};
  31671. };
  31672. public:
  31673. /*! @brief Node type. */
  31674. using node_type = Node;
  31675. /*! @brief Input iterator type. */
  31676. using iterator = range_iterator;
  31677. /*! @brief Default constructor. */
  31678. meta_range() ENTT_NOEXCEPT = default;
  31679. /**
  31680. * @brief Constructs a meta range from a given node.
  31681. * @param head The underlying node with which to construct the range.
  31682. */
  31683. meta_range(node_type *head)
  31684. : node{head}
  31685. {}
  31686. /**
  31687. * @brief Returns an iterator to the beginning.
  31688. * @return An iterator to the first meta object of the range.
  31689. */
  31690. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  31691. return iterator{node};
  31692. }
  31693. /**
  31694. * @brief Returns an iterator to the end.
  31695. * @return An iterator to the element following the last meta object of the
  31696. * range.
  31697. */
  31698. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  31699. return iterator{};
  31700. }
  31701. private:
  31702. node_type *node{nullptr};
  31703. };
  31704. }
  31705. #endif
  31706. // #include "meta/resolve.hpp"
  31707. #ifndef ENTT_META_RESOLVE_HPP
  31708. #define ENTT_META_RESOLVE_HPP
  31709. #include <algorithm>
  31710. // #include "../core/type_info.hpp"
  31711. // #include "ctx.hpp"
  31712. // #include "meta.hpp"
  31713. // #include "node.hpp"
  31714. // #include "range.hpp"
  31715. namespace entt {
  31716. /**
  31717. * @brief Returns the meta type associated with a given type.
  31718. * @tparam Type Type to use to search for a meta type.
  31719. * @return The meta type associated with the given type, if any.
  31720. */
  31721. template<typename Type>
  31722. [[nodiscard]] meta_type resolve() ENTT_NOEXCEPT {
  31723. return internal::meta_info<Type>::resolve();
  31724. }
  31725. /**
  31726. * @brief Returns a range to use to visit all meta types.
  31727. * @return An iterable range to use to visit all meta types.
  31728. */
  31729. [[nodiscard]] inline meta_range<meta_type> resolve() {
  31730. return *internal::meta_context::global();
  31731. }
  31732. /**
  31733. * @brief Returns the meta type associated with a given identifier, if any.
  31734. * @param id Unique identifier.
  31735. * @return The meta type associated with the given identifier, if any.
  31736. */
  31737. [[nodiscard]] inline meta_type resolve(const id_type id) ENTT_NOEXCEPT {
  31738. for(auto *curr = *internal::meta_context::global(); curr; curr = curr->next) {
  31739. if(curr->id == id) {
  31740. return curr;
  31741. }
  31742. }
  31743. return {};
  31744. }
  31745. /**
  31746. * @brief Returns the meta type associated with a given type info object, if
  31747. * any.
  31748. * @param info The type info object of the requested type.
  31749. * @return The meta type associated with the given type info object, if any.
  31750. */
  31751. [[nodiscard]] inline meta_type resolve(const type_info info) ENTT_NOEXCEPT {
  31752. for(auto *curr = *internal::meta_context::global(); curr; curr = curr->next) {
  31753. if(curr->info == info) {
  31754. return curr;
  31755. }
  31756. }
  31757. return {};
  31758. }
  31759. }
  31760. #endif
  31761. // #include "meta/template.hpp"
  31762. #ifndef ENTT_META_TEMPLATE_HPP
  31763. #define ENTT_META_TEMPLATE_HPP
  31764. // #include "../core/type_traits.hpp"
  31765. namespace entt {
  31766. /*! @brief Utility class to disambiguate class templates. */
  31767. template<template<typename...> typename>
  31768. struct meta_class_template_tag {};
  31769. /**
  31770. * @brief General purpose traits class for generating meta template information.
  31771. * @tparam Clazz Type of class template.
  31772. * @tparam Args Types of template arguments.
  31773. */
  31774. template<template<typename...> typename Clazz, typename... Args>
  31775. struct meta_template_traits<Clazz<Args...>> {
  31776. /*! @brief Wrapped class template. */
  31777. using class_type = meta_class_template_tag<Clazz>;
  31778. /*! @brief List of template arguments. */
  31779. using args_type = type_list<Args...>;
  31780. };
  31781. }
  31782. #endif
  31783. // #include "meta/type_traits.hpp"
  31784. #ifndef ENTT_META_TYPE_TRAITS_HPP
  31785. #define ENTT_META_TYPE_TRAITS_HPP
  31786. #include <type_traits>
  31787. namespace entt {
  31788. /**
  31789. * @brief Traits class template to be specialized to enable support for meta
  31790. * template information.
  31791. */
  31792. template<typename>
  31793. struct meta_template_traits;
  31794. /**
  31795. * @brief Traits class template to be specialized to enable support for meta
  31796. * sequence containers.
  31797. */
  31798. template<typename>
  31799. struct meta_sequence_container_traits;
  31800. /**
  31801. * @brief Traits class template to be specialized to enable support for meta
  31802. * associative containers.
  31803. */
  31804. template<typename>
  31805. struct meta_associative_container_traits;
  31806. /**
  31807. * @brief Provides the member constant `value` to true if a meta associative
  31808. * container claims to wrap a key-only type, false otherwise.
  31809. * @tparam Type Potentially key-only meta associative container type.
  31810. */
  31811. template<typename, typename = void>
  31812. struct is_key_only_meta_associative_container: std::true_type {};
  31813. /*! @copydoc is_key_only_meta_associative_container */
  31814. template<typename Type>
  31815. struct is_key_only_meta_associative_container<Type, std::void_t<typename meta_associative_container_traits<Type>::type::mapped_type>>
  31816. : std::false_type
  31817. {};
  31818. /**
  31819. * @brief Helper variable template.
  31820. * @tparam Type Potentially key-only meta associative container type.
  31821. */
  31822. template<typename Type>
  31823. inline constexpr auto is_key_only_meta_associative_container_v = is_key_only_meta_associative_container<Type>::value;
  31824. /**
  31825. * @brief Provides the member constant `value` to true if a given type is a
  31826. * pointer-like type from the point of view of the meta system, false otherwise.
  31827. * @tparam Type Potentially pointer-like type.
  31828. */
  31829. template<typename>
  31830. struct is_meta_pointer_like: std::false_type {};
  31831. /**
  31832. * @brief Partial specialization to ensure that const pointer-like types are
  31833. * also accepted.
  31834. * @tparam Type Potentially pointer-like type.
  31835. */
  31836. template<typename Type>
  31837. struct is_meta_pointer_like<const Type>: is_meta_pointer_like<Type> {};
  31838. /**
  31839. * @brief Helper variable template.
  31840. * @tparam Type Potentially pointer-like type.
  31841. */
  31842. template<typename Type>
  31843. inline constexpr auto is_meta_pointer_like_v = is_meta_pointer_like<Type>::value;
  31844. }
  31845. #endif
  31846. // #include "meta/utility.hpp"
  31847. #ifndef ENTT_META_UTILITY_HPP
  31848. #define ENTT_META_UTILITY_HPP
  31849. #include <cstddef>
  31850. #include <functional>
  31851. #include <type_traits>
  31852. #include <utility>
  31853. // #include "../config/config.h"
  31854. // #include "../core/type_traits.hpp"
  31855. // #include "meta.hpp"
  31856. // #include "node.hpp"
  31857. // #include "policy.hpp"
  31858. namespace entt {
  31859. /*! @brief Primary template isn't defined on purpose. */
  31860. template<typename, typename>
  31861. struct meta_function_descriptor;
  31862. /**
  31863. * @brief Meta function descriptor.
  31864. * @tparam Type Reflected type to which the meta function is associated.
  31865. * @tparam Ret Function return type.
  31866. * @tparam Class Actual owner of the member function.
  31867. * @tparam Args Function arguments.
  31868. */
  31869. template<typename Type, typename Ret, typename Class, typename... Args>
  31870. struct meta_function_descriptor<Type, Ret(Class:: *)(Args...) const> {
  31871. /*! @brief Meta function return type. */
  31872. using return_type = Ret;
  31873. /*! @brief Meta function arguments. */
  31874. using args_type = std::conditional_t<std::is_same_v<Type, Class>, type_list<Args...>, type_list<const Class &, Args...>>;
  31875. /*! @brief True if the meta function is const, false otherwise. */
  31876. static constexpr auto is_const = true;
  31877. /*! @brief True if the meta function is static, false otherwise. */
  31878. static constexpr auto is_static = !std::is_same_v<Type, Class>;
  31879. };
  31880. /**
  31881. * @brief Meta function descriptor.
  31882. * @tparam Type Reflected type to which the meta function is associated.
  31883. * @tparam Ret Function return type.
  31884. * @tparam Class Actual owner of the member function.
  31885. * @tparam Args Function arguments.
  31886. */
  31887. template<typename Type, typename Ret, typename Class, typename... Args>
  31888. struct meta_function_descriptor<Type, Ret(Class:: *)(Args...)> {
  31889. /*! @brief Meta function return type. */
  31890. using return_type = Ret;
  31891. /*! @brief Meta function arguments. */
  31892. using args_type = std::conditional_t<std::is_same_v<Type, Class>, type_list<Args...>, type_list<Class &, Args...>>;
  31893. /*! @brief True if the meta function is const, false otherwise. */
  31894. static constexpr auto is_const = false;
  31895. /*! @brief True if the meta function is static, false otherwise. */
  31896. static constexpr auto is_static = !std::is_same_v<Type, Class>;
  31897. };
  31898. /**
  31899. * @brief Meta function descriptor.
  31900. * @tparam Type Reflected type to which the meta function is associated.
  31901. * @tparam Ret Function return type.
  31902. * @tparam Args Function arguments.
  31903. */
  31904. template<typename Type, typename Ret, typename... Args>
  31905. struct meta_function_descriptor<Type, Ret(*)(Args...)> {
  31906. /*! @brief Meta function return type. */
  31907. using return_type = Ret;
  31908. /*! @brief Meta function arguments. */
  31909. using args_type = type_list<Args...>;
  31910. /*! @brief True if the meta function is const, false otherwise. */
  31911. static constexpr auto is_const = false;
  31912. /*! @brief True if the meta function is static, false otherwise. */
  31913. static constexpr auto is_static = true;
  31914. };
  31915. /**
  31916. * @brief Meta function helper.
  31917. *
  31918. * Converts a function type to be associated with a reflected type into its meta
  31919. * function descriptor.
  31920. *
  31921. * @tparam Type Reflected type to which the meta function is associated.
  31922. * @tparam Candidate The actual function to associate with the reflected type.
  31923. */
  31924. template<typename Type, typename Candidate>
  31925. class meta_function_helper {
  31926. template<typename Ret, typename... Args, typename Class>
  31927. static constexpr meta_function_descriptor<Type, Ret(Class:: *)(Args...) const> get_rid_of_noexcept(Ret(Class:: *)(Args...) const);
  31928. template<typename Ret, typename... Args, typename Class>
  31929. static constexpr meta_function_descriptor<Type, Ret(Class:: *)(Args...)> get_rid_of_noexcept(Ret(Class:: *)(Args...));
  31930. template<typename Ret, typename... Args>
  31931. static constexpr meta_function_descriptor<Type, Ret(*)(Args...)> get_rid_of_noexcept(Ret(*)(Args...));
  31932. public:
  31933. /*! @brief The meta function descriptor of the given function. */
  31934. using type = decltype(get_rid_of_noexcept(std::declval<Candidate>()));
  31935. };
  31936. /**
  31937. * @brief Helper type.
  31938. * @tparam Type Reflected type to which the meta function is associated.
  31939. * @tparam Candidate The actual function to associate with the reflected type.
  31940. */
  31941. template<typename Type, typename Candidate>
  31942. using meta_function_helper_t = typename meta_function_helper<Type, Candidate>::type;
  31943. /**
  31944. * @brief Returns the meta type of the i-th element of a list of arguments.
  31945. * @tparam Args Actual types of arguments.
  31946. * @return The meta type of the i-th element of the list of arguments.
  31947. */
  31948. template<typename... Args>
  31949. [[nodiscard]] static meta_type meta_arg(type_list<Args...>, const std::size_t index) ENTT_NOEXCEPT {
  31950. return internal::meta_arg_node(type_list<Args...>{}, index);
  31951. }
  31952. /**
  31953. * @brief Constructs an instance given a list of erased parameters, if possible.
  31954. * @tparam Type Actual type of the instance to construct.
  31955. * @tparam Args Types of arguments expected.
  31956. * @tparam Index Indexes to use to extract erased arguments from their list.
  31957. * @param args Parameters to use to construct the instance.
  31958. * @return A meta any containing the new instance, if any.
  31959. */
  31960. template<typename Type, typename... Args, std::size_t... Index>
  31961. [[nodiscard]] meta_any meta_construct(meta_any * const args, std::index_sequence<Index...>) {
  31962. if(((args+Index)->allow_cast<Args>() && ...)) {
  31963. return Type{(args+Index)->cast<Args>()...};
  31964. }
  31965. return {};
  31966. }
  31967. /**
  31968. * @brief Sets the value of a given variable.
  31969. * @tparam Type Reflected type to which the variable is associated.
  31970. * @tparam Data The actual variable to set.
  31971. * @param instance An opaque instance of the underlying type, if required.
  31972. * @param value Parameter to use to set the variable.
  31973. * @return True in case of success, false otherwise.
  31974. */
  31975. template<typename Type, auto Data>
  31976. [[nodiscard]] bool meta_setter([[maybe_unused]] meta_handle instance, [[maybe_unused]] meta_any value) {
  31977. if constexpr(!std::is_same_v<decltype(Data), Type> && !std::is_same_v<decltype(Data), std::nullptr_t>) {
  31978. if constexpr(std::is_function_v<std::remove_reference_t<std::remove_pointer_t<decltype(Data)>>>) {
  31979. using data_type = type_list_element_t<1u, typename meta_function_helper_t<Type, decltype(Data)>::args_type>;
  31980. if(auto * const clazz = instance->try_cast<Type>(); clazz && value.allow_cast<data_type>()) {
  31981. Data(*clazz, value.cast<data_type>());
  31982. return true;
  31983. }
  31984. } else if constexpr(std::is_member_function_pointer_v<decltype(Data)>) {
  31985. using data_type = type_list_element_t<0u, typename meta_function_helper_t<Type, decltype(Data)>::args_type>;
  31986. if(auto * const clazz = instance->try_cast<Type>(); clazz && value.allow_cast<data_type>()) {
  31987. (clazz->*Data)(value.cast<data_type>());
  31988. return true;
  31989. }
  31990. } else if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
  31991. using data_type = std::remove_reference_t<decltype(std::declval<Type>().*Data)>;
  31992. if constexpr(!std::is_array_v<data_type> && !std::is_const_v<data_type>) {
  31993. if(auto * const clazz = instance->try_cast<Type>(); clazz && value.allow_cast<data_type>()) {
  31994. clazz->*Data = value.cast<data_type>();
  31995. return true;
  31996. }
  31997. }
  31998. } else {
  31999. using data_type = std::remove_reference_t<decltype(*Data)>;
  32000. if constexpr(!std::is_array_v<data_type> && !std::is_const_v<data_type>) {
  32001. if(value.allow_cast<data_type>()) {
  32002. *Data = value.cast<data_type>();
  32003. return true;
  32004. }
  32005. }
  32006. }
  32007. }
  32008. return false;
  32009. }
  32010. /**
  32011. * @brief Wraps a value depending on the given policy.
  32012. * @tparam Policy Optional policy (no policy set by default).
  32013. * @tparam Type Type of value to wrap.
  32014. * @param value Value to wrap.
  32015. * @return A meta any containing the returned value.
  32016. */
  32017. template<typename Policy = as_is_t, typename Type>
  32018. meta_any meta_dispatch(Type &&value) {
  32019. if constexpr(std::is_same_v<Policy, as_void_t>) {
  32020. return meta_any{std::in_place_type<void>, std::forward<Type>(value)};
  32021. } else if constexpr(std::is_same_v<Policy, as_ref_t>) {
  32022. return meta_any{std::in_place_type<Type>, std::forward<Type>(value)};
  32023. } else if constexpr(std::is_same_v<Policy, as_cref_t>) {
  32024. static_assert(std::is_lvalue_reference_v<Type>, "Invalid type");
  32025. return meta_any{std::in_place_type<const std::remove_reference_t<Type> &>, std::as_const(value)};
  32026. } else {
  32027. static_assert(std::is_same_v<Policy, as_is_t>, "Policy not supported");
  32028. return meta_any{std::forward<Type>(value)};
  32029. }
  32030. }
  32031. /**
  32032. * @brief Gets the value of a given variable.
  32033. * @tparam Type Reflected type to which the variable is associated.
  32034. * @tparam Data The actual variable to get.
  32035. * @tparam Policy Optional policy (no policy set by default).
  32036. * @param instance An opaque instance of the underlying type, if required.
  32037. * @return A meta any containing the value of the underlying variable.
  32038. */
  32039. template<typename Type, auto Data, typename Policy = as_is_t>
  32040. [[nodiscard]] meta_any meta_getter([[maybe_unused]] meta_handle instance) {
  32041. if constexpr(std::is_function_v<std::remove_reference_t<std::remove_pointer_t<decltype(Data)>>>) {
  32042. auto * const clazz = instance->try_cast<std::conditional_t<std::is_invocable_v<decltype(Data), const Type &>, const Type, Type>>();
  32043. return clazz ? meta_dispatch<Policy>(Data(*clazz)) : meta_any{};
  32044. } else if constexpr(std::is_member_function_pointer_v<decltype(Data)>) {
  32045. auto * const clazz = instance->try_cast<std::conditional_t<std::is_invocable_v<decltype(Data), const Type &>, const Type, Type>>();
  32046. return clazz ? meta_dispatch<Policy>((clazz->*Data)()) : meta_any{};
  32047. } else if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
  32048. if constexpr(!std::is_array_v<std::remove_cv_t<std::remove_reference_t<decltype(std::declval<Type>().*Data)>>>) {
  32049. if(auto * clazz = instance->try_cast<Type>(); clazz) {
  32050. return meta_dispatch<Policy>(clazz->*Data);
  32051. } else if(auto * fallback = instance->try_cast<const Type>(); fallback) {
  32052. return meta_dispatch<Policy>(fallback->*Data);
  32053. }
  32054. }
  32055. return meta_any{};
  32056. } else if constexpr(std::is_pointer_v<decltype(Data)>) {
  32057. if constexpr(std::is_array_v<std::remove_pointer_t<decltype(Data)>>) {
  32058. return meta_any{};
  32059. } else {
  32060. return meta_dispatch<Policy>(*Data);
  32061. }
  32062. } else {
  32063. return meta_dispatch<Policy>(Data);
  32064. }
  32065. }
  32066. /**
  32067. * @brief Invokes a function given a list of erased parameters, if possible.
  32068. * @tparam Type Reflected type to which the function is associated.
  32069. * @tparam Candidate The actual function to invoke.
  32070. * @tparam Policy Optional policy (no policy set by default).
  32071. * @tparam Index Indexes to use to extract erased arguments from their list.
  32072. * @param instance An opaque instance of the underlying type, if required.
  32073. * @param args Parameters to use to invoke the function.
  32074. * @return A meta any containing the returned value, if any.
  32075. */
  32076. template<typename Type, auto Candidate, typename Policy = as_is_t, std::size_t... Index>
  32077. [[nodiscard]] std::enable_if_t<!std::is_invocable_v<decltype(Candidate)>, meta_any> meta_invoke([[maybe_unused]] meta_handle instance, meta_any *args, std::index_sequence<Index...>) {
  32078. using descriptor = meta_function_helper_t<Type, decltype(Candidate)>;
  32079. const auto invoke = [](auto &&maybe_clazz, auto &&... other) {
  32080. if constexpr(std::is_member_function_pointer_v<decltype(Candidate)>) {
  32081. if constexpr(std::is_void_v<typename descriptor::return_type>) {
  32082. (std::forward<decltype(maybe_clazz)>(maybe_clazz).*Candidate)(std::forward<decltype(other)>(other)...);
  32083. return meta_any{std::in_place_type<void>};
  32084. } else {
  32085. return meta_dispatch<Policy>((std::forward<decltype(maybe_clazz)>(maybe_clazz).*Candidate)(std::forward<decltype(other)>(other)...));
  32086. }
  32087. } else {
  32088. if constexpr(std::is_void_v<typename descriptor::return_type>) {
  32089. Candidate(std::forward<decltype(maybe_clazz)>(maybe_clazz), std::forward<decltype(other)>(other)...);
  32090. return meta_any{std::in_place_type<void>};
  32091. } else {
  32092. return meta_dispatch<Policy>(Candidate(std::forward<decltype(maybe_clazz)>(maybe_clazz), std::forward<decltype(other)>(other)...));
  32093. }
  32094. }
  32095. };
  32096. if constexpr(std::is_invocable_v<decltype(Candidate), const Type &, type_list_element_t<Index, typename descriptor::args_type>...>) {
  32097. if(const auto * const clazz = instance->try_cast<const Type>(); clazz && ((args+Index)->allow_cast<type_list_element_t<Index, typename descriptor::args_type>>() && ...)) {
  32098. return invoke(*clazz, (args+Index)->cast<type_list_element_t<Index, typename descriptor::args_type>>()...);
  32099. }
  32100. } else if constexpr(std::is_invocable_v<decltype(Candidate), Type &, type_list_element_t<Index, typename descriptor::args_type>...>) {
  32101. if(auto * const clazz = instance->try_cast<Type>(); clazz && ((args+Index)->allow_cast<type_list_element_t<Index, typename descriptor::args_type>>() && ...)) {
  32102. return invoke(*clazz, (args+Index)->cast<type_list_element_t<Index, typename descriptor::args_type>>()...);
  32103. }
  32104. } else {
  32105. if(((args+Index)->allow_cast<type_list_element_t<Index, typename descriptor::args_type>>() && ...)) {
  32106. return invoke((args+Index)->cast<type_list_element_t<Index, typename descriptor::args_type>>()...);
  32107. }
  32108. }
  32109. return meta_any{};
  32110. }
  32111. /**
  32112. * @brief Invokes a function given a list of erased parameters, if possible.
  32113. * @tparam Type Reflected type to which the function is associated.
  32114. * @tparam Candidate The actual function to invoke.
  32115. * @tparam Policy Optional policy (no policy set by default).
  32116. * @tparam Index Indexes to use to extract erased arguments from their list.
  32117. * @return A meta any containing the returned value, if any.
  32118. */
  32119. template<typename Type, auto Candidate, typename Policy = as_is_t, std::size_t... Index>
  32120. [[nodiscard]] std::enable_if_t<std::is_invocable_v<decltype(Candidate)>, meta_any> meta_invoke(meta_handle, meta_any *, std::index_sequence<Index...>) {
  32121. if constexpr(std::is_void_v<decltype(Candidate())>) {
  32122. Candidate();
  32123. return meta_any{std::in_place_type<void>};
  32124. } else {
  32125. return meta_dispatch<Policy>(Candidate());
  32126. }
  32127. }
  32128. }
  32129. #endif
  32130. // #include "platform/android-ndk-r17.hpp"
  32131. #ifndef ENTT_PLATFORM_ANDROID_NDK_R17_HPP
  32132. #define ENTT_PLATFORM_ANDROID_NDK_R17_HPP
  32133. /**
  32134. * @cond TURN_OFF_DOXYGEN
  32135. * Internal details not to be documented.
  32136. */
  32137. #ifdef __ANDROID__
  32138. #include <android/ndk-version.h>
  32139. #if __NDK_MAJOR__ == 17
  32140. #include <functional>
  32141. #include <type_traits>
  32142. #include <utility>
  32143. namespace std {
  32144. namespace internal {
  32145. template<typename Func, typename... Args>
  32146. constexpr auto is_invocable(int) -> decltype(std::invoke(std::declval<Func>(), std::declval<Args>()...), std::true_type{});
  32147. template<typename, typename...>
  32148. constexpr std::false_type is_invocable(...);
  32149. template<typename Ret, typename Func, typename... Args>
  32150. constexpr auto is_invocable_r(int)
  32151. -> std::enable_if_t<decltype(std::is_convertible_v<decltype(std::invoke(std::declval<Func>(), std::declval<Args>()...)), Ret>, std::true_type>;
  32152. template<typename, typename, typename...>
  32153. constexpr std::false_type is_invocable_r(...);
  32154. }
  32155. template<typename Func, typename... Args>
  32156. struct is_invocable: decltype(internal::is_invocable<Func, Args...>(0)) {};
  32157. template<typename Func, typename... Argsv>
  32158. inline constexpr bool is_invocable_v = std::is_invocable<Func, Args...>::value;
  32159. template<typename Ret, typename Func, typename... Args>
  32160. struct is_invocable_r: decltype(internal::is_invocable_r<Ret, Func, Args...>(0)) {};
  32161. template<typename Ret, typename Func, typename... Args>
  32162. inline constexpr bool is_invocable_r_v = std::is_invocable_r<Ret, Func, Args...>::value;
  32163. template<typename Func, typename...Args>
  32164. struct invoke_result {
  32165. using type = decltype(std::invoke(std::declval<Func>(), std::declval<Args>()...));
  32166. };
  32167. template<typename Func, typename... Args>
  32168. using invoke_result_t = typename std::invoke_result<Func, Args...>::type;
  32169. }
  32170. #endif
  32171. #endif
  32172. /**
  32173. * Internal details not to be documented.
  32174. * @endcond
  32175. */
  32176. #endif
  32177. // #include "poly/poly.hpp"
  32178. #ifndef ENTT_POLY_POLY_HPP
  32179. #define ENTT_POLY_POLY_HPP
  32180. #include <cstddef>
  32181. #include <functional>
  32182. #include <tuple>
  32183. #include <type_traits>
  32184. #include <utility>
  32185. // #include "../config/config.h"
  32186. #ifndef ENTT_CONFIG_CONFIG_H
  32187. #define ENTT_CONFIG_CONFIG_H
  32188. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  32189. # define ENTT_NOEXCEPT noexcept
  32190. # define ENTT_THROW throw
  32191. # define ENTT_TRY try
  32192. # define ENTT_CATCH catch(...)
  32193. #else
  32194. # define ENTT_NOEXCEPT
  32195. # define ENTT_THROW
  32196. # define ENTT_TRY if(true)
  32197. # define ENTT_CATCH if(false)
  32198. #endif
  32199. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  32200. # include <new>
  32201. # define ENTT_LAUNDER(expr) std::launder(expr)
  32202. #else
  32203. # define ENTT_LAUNDER(expr) expr
  32204. #endif
  32205. #ifndef ENTT_USE_ATOMIC
  32206. # define ENTT_MAYBE_ATOMIC(Type) Type
  32207. #else
  32208. # include <atomic>
  32209. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  32210. #endif
  32211. #ifndef ENTT_ID_TYPE
  32212. # include <cstdint>
  32213. # define ENTT_ID_TYPE std::uint32_t
  32214. #endif
  32215. #ifdef ENTT_SPARSE_PAGE
  32216. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  32217. #else
  32218. # define ENTT_SPARSE_PAGE 4096
  32219. #endif
  32220. #ifdef ENTT_PACKED_PAGE
  32221. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  32222. #else
  32223. # define ENTT_PACKED_PAGE 1024
  32224. #endif
  32225. #ifdef ENTT_DISABLE_ASSERT
  32226. # undef ENTT_ASSERT
  32227. # define ENTT_ASSERT(...) (void(0))
  32228. #elif !defined ENTT_ASSERT
  32229. # include <cassert>
  32230. # define ENTT_ASSERT(condition, ...) assert(condition)
  32231. #endif
  32232. #ifdef ENTT_NO_ETO
  32233. # include <type_traits>
  32234. # define ENTT_IGNORE_IF_EMPTY std::false_type
  32235. #else
  32236. # include <type_traits>
  32237. # define ENTT_IGNORE_IF_EMPTY std::true_type
  32238. #endif
  32239. #ifndef ENTT_STANDARD_CPP
  32240. # if defined __clang__ || defined __GNUC__
  32241. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  32242. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  32243. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  32244. # elif defined _MSC_VER
  32245. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  32246. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  32247. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  32248. # endif
  32249. #endif
  32250. #endif
  32251. // #include "../core/any.hpp"
  32252. #ifndef ENTT_CORE_ANY_HPP
  32253. #define ENTT_CORE_ANY_HPP
  32254. #include <cstddef>
  32255. #include <functional>
  32256. #include <memory>
  32257. #include <type_traits>
  32258. #include <utility>
  32259. // #include "../config/config.h"
  32260. #ifndef ENTT_CONFIG_CONFIG_H
  32261. #define ENTT_CONFIG_CONFIG_H
  32262. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  32263. # define ENTT_NOEXCEPT noexcept
  32264. # define ENTT_THROW throw
  32265. # define ENTT_TRY try
  32266. # define ENTT_CATCH catch(...)
  32267. #else
  32268. # define ENTT_NOEXCEPT
  32269. # define ENTT_THROW
  32270. # define ENTT_TRY if(true)
  32271. # define ENTT_CATCH if(false)
  32272. #endif
  32273. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  32274. # include <new>
  32275. # define ENTT_LAUNDER(expr) std::launder(expr)
  32276. #else
  32277. # define ENTT_LAUNDER(expr) expr
  32278. #endif
  32279. #ifndef ENTT_USE_ATOMIC
  32280. # define ENTT_MAYBE_ATOMIC(Type) Type
  32281. #else
  32282. # include <atomic>
  32283. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  32284. #endif
  32285. #ifndef ENTT_ID_TYPE
  32286. # include <cstdint>
  32287. # define ENTT_ID_TYPE std::uint32_t
  32288. #endif
  32289. #ifdef ENTT_SPARSE_PAGE
  32290. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  32291. #else
  32292. # define ENTT_SPARSE_PAGE 4096
  32293. #endif
  32294. #ifdef ENTT_PACKED_PAGE
  32295. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  32296. #else
  32297. # define ENTT_PACKED_PAGE 1024
  32298. #endif
  32299. #ifdef ENTT_DISABLE_ASSERT
  32300. # undef ENTT_ASSERT
  32301. # define ENTT_ASSERT(...) (void(0))
  32302. #elif !defined ENTT_ASSERT
  32303. # include <cassert>
  32304. # define ENTT_ASSERT(condition, ...) assert(condition)
  32305. #endif
  32306. #ifdef ENTT_NO_ETO
  32307. # include <type_traits>
  32308. # define ENTT_IGNORE_IF_EMPTY std::false_type
  32309. #else
  32310. # include <type_traits>
  32311. # define ENTT_IGNORE_IF_EMPTY std::true_type
  32312. #endif
  32313. #ifndef ENTT_STANDARD_CPP
  32314. # if defined __clang__ || defined __GNUC__
  32315. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  32316. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  32317. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  32318. # elif defined _MSC_VER
  32319. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  32320. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  32321. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  32322. # endif
  32323. #endif
  32324. #endif
  32325. // #include "../core/utility.hpp"
  32326. #ifndef ENTT_CORE_UTILITY_HPP
  32327. #define ENTT_CORE_UTILITY_HPP
  32328. #include <utility>
  32329. // #include "../config/config.h"
  32330. #ifndef ENTT_CONFIG_CONFIG_H
  32331. #define ENTT_CONFIG_CONFIG_H
  32332. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  32333. # define ENTT_NOEXCEPT noexcept
  32334. # define ENTT_THROW throw
  32335. # define ENTT_TRY try
  32336. # define ENTT_CATCH catch(...)
  32337. #else
  32338. # define ENTT_NOEXCEPT
  32339. # define ENTT_THROW
  32340. # define ENTT_TRY if(true)
  32341. # define ENTT_CATCH if(false)
  32342. #endif
  32343. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  32344. # include <new>
  32345. # define ENTT_LAUNDER(expr) std::launder(expr)
  32346. #else
  32347. # define ENTT_LAUNDER(expr) expr
  32348. #endif
  32349. #ifndef ENTT_USE_ATOMIC
  32350. # define ENTT_MAYBE_ATOMIC(Type) Type
  32351. #else
  32352. # include <atomic>
  32353. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  32354. #endif
  32355. #ifndef ENTT_ID_TYPE
  32356. # include <cstdint>
  32357. # define ENTT_ID_TYPE std::uint32_t
  32358. #endif
  32359. #ifdef ENTT_SPARSE_PAGE
  32360. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  32361. #else
  32362. # define ENTT_SPARSE_PAGE 4096
  32363. #endif
  32364. #ifdef ENTT_PACKED_PAGE
  32365. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  32366. #else
  32367. # define ENTT_PACKED_PAGE 1024
  32368. #endif
  32369. #ifdef ENTT_DISABLE_ASSERT
  32370. # undef ENTT_ASSERT
  32371. # define ENTT_ASSERT(...) (void(0))
  32372. #elif !defined ENTT_ASSERT
  32373. # include <cassert>
  32374. # define ENTT_ASSERT(condition, ...) assert(condition)
  32375. #endif
  32376. #ifdef ENTT_NO_ETO
  32377. # include <type_traits>
  32378. # define ENTT_IGNORE_IF_EMPTY std::false_type
  32379. #else
  32380. # include <type_traits>
  32381. # define ENTT_IGNORE_IF_EMPTY std::true_type
  32382. #endif
  32383. #ifndef ENTT_STANDARD_CPP
  32384. # if defined __clang__ || defined __GNUC__
  32385. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  32386. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  32387. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  32388. # elif defined _MSC_VER
  32389. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  32390. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  32391. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  32392. # endif
  32393. #endif
  32394. #endif
  32395. namespace entt {
  32396. /*! @brief Identity function object (waiting for C++20). */
  32397. struct identity {
  32398. /**
  32399. * @brief Returns its argument unchanged.
  32400. * @tparam Type Type of the argument.
  32401. * @param value The actual argument.
  32402. * @return The submitted value as-is.
  32403. */
  32404. template<class Type>
  32405. [[nodiscard]] constexpr Type && operator()(Type &&value) const ENTT_NOEXCEPT {
  32406. return std::forward<Type>(value);
  32407. }
  32408. };
  32409. /**
  32410. * @brief Constant utility to disambiguate overloaded members of a class.
  32411. * @tparam Type Type of the desired overload.
  32412. * @tparam Class Type of class to which the member belongs.
  32413. * @param member A valid pointer to a member.
  32414. * @return Pointer to the member.
  32415. */
  32416. template<typename Type, typename Class>
  32417. [[nodiscard]] constexpr auto overload(Type Class:: *member) ENTT_NOEXCEPT { return member; }
  32418. /**
  32419. * @brief Constant utility to disambiguate overloaded functions.
  32420. * @tparam Func Function type of the desired overload.
  32421. * @param func A valid pointer to a function.
  32422. * @return Pointer to the function.
  32423. */
  32424. template<typename Func>
  32425. [[nodiscard]] constexpr auto overload(Func *func) ENTT_NOEXCEPT { return func; }
  32426. /**
  32427. * @brief Helper type for visitors.
  32428. * @tparam Func Types of function objects.
  32429. */
  32430. template<class... Func>
  32431. struct overloaded: Func... {
  32432. using Func::operator()...;
  32433. };
  32434. /**
  32435. * @brief Deduction guide.
  32436. * @tparam Func Types of function objects.
  32437. */
  32438. template<class... Func>
  32439. overloaded(Func...)
  32440. -> overloaded<Func...>;
  32441. /**
  32442. * @brief Basic implementation of a y-combinator.
  32443. * @tparam Func Type of a potentially recursive function.
  32444. */
  32445. template<class Func>
  32446. struct y_combinator {
  32447. /**
  32448. * @brief Constructs a y-combinator from a given function.
  32449. * @param recursive A potentially recursive function.
  32450. */
  32451. y_combinator(Func recursive):
  32452. func{std::move(recursive)}
  32453. {}
  32454. /**
  32455. * @brief Invokes a y-combinator and therefore its underlying function.
  32456. * @tparam Args Types of arguments to use to invoke the underlying function.
  32457. * @param args Parameters to use to invoke the underlying function.
  32458. * @return Return value of the underlying function, if any.
  32459. */
  32460. template <class... Args>
  32461. decltype(auto) operator()(Args &&... args) const {
  32462. return func(*this, std::forward<Args>(args)...);
  32463. }
  32464. /*! @copydoc operator()() */
  32465. template <class... Args>
  32466. decltype(auto) operator()(Args &&... args) {
  32467. return func(*this, std::forward<Args>(args)...);
  32468. }
  32469. private:
  32470. Func func;
  32471. };
  32472. }
  32473. #endif
  32474. // #include "fwd.hpp"
  32475. #ifndef ENTT_CORE_FWD_HPP
  32476. #define ENTT_CORE_FWD_HPP
  32477. #include <type_traits>
  32478. // #include "../config/config.h"
  32479. namespace entt {
  32480. template<std::size_t Len = sizeof(double[2]), std::size_t = alignof(typename std::aligned_storage_t<Len + !Len>)>
  32481. class basic_any;
  32482. /*! @brief Alias declaration for type identifiers. */
  32483. using id_type = ENTT_ID_TYPE;
  32484. /*! @brief Alias declaration for the most common use case. */
  32485. using any = basic_any<>;
  32486. }
  32487. #endif
  32488. // #include "type_info.hpp"
  32489. #ifndef ENTT_CORE_TYPE_INFO_HPP
  32490. #define ENTT_CORE_TYPE_INFO_HPP
  32491. #include <string_view>
  32492. #include <type_traits>
  32493. // #include "../config/config.h"
  32494. // #include "../core/attribute.h"
  32495. #ifndef ENTT_CORE_ATTRIBUTE_H
  32496. #define ENTT_CORE_ATTRIBUTE_H
  32497. #ifndef ENTT_EXPORT
  32498. # if defined _WIN32 || defined __CYGWIN__ || defined _MSC_VER
  32499. # define ENTT_EXPORT __declspec(dllexport)
  32500. # define ENTT_IMPORT __declspec(dllimport)
  32501. # define ENTT_HIDDEN
  32502. # elif defined __GNUC__ && __GNUC__ >= 4
  32503. # define ENTT_EXPORT __attribute__((visibility("default")))
  32504. # define ENTT_IMPORT __attribute__((visibility("default")))
  32505. # define ENTT_HIDDEN __attribute__((visibility("hidden")))
  32506. # else /* Unsupported compiler */
  32507. # define ENTT_EXPORT
  32508. # define ENTT_IMPORT
  32509. # define ENTT_HIDDEN
  32510. # endif
  32511. #endif
  32512. #ifndef ENTT_API
  32513. # if defined ENTT_API_EXPORT
  32514. # define ENTT_API ENTT_EXPORT
  32515. # elif defined ENTT_API_IMPORT
  32516. # define ENTT_API ENTT_IMPORT
  32517. # else /* No API */
  32518. # define ENTT_API
  32519. # endif
  32520. #endif
  32521. #endif
  32522. // #include "hashed_string.hpp"
  32523. #ifndef ENTT_CORE_HASHED_STRING_HPP
  32524. #define ENTT_CORE_HASHED_STRING_HPP
  32525. #include <cstddef>
  32526. #include <cstdint>
  32527. // #include "../config/config.h"
  32528. // #include "fwd.hpp"
  32529. namespace entt {
  32530. /**
  32531. * @cond TURN_OFF_DOXYGEN
  32532. * Internal details not to be documented.
  32533. */
  32534. namespace internal {
  32535. template<typename>
  32536. struct fnv1a_traits;
  32537. template<>
  32538. struct fnv1a_traits<std::uint32_t> {
  32539. using type = std::uint32_t;
  32540. static constexpr std::uint32_t offset = 2166136261;
  32541. static constexpr std::uint32_t prime = 16777619;
  32542. };
  32543. template<>
  32544. struct fnv1a_traits<std::uint64_t> {
  32545. using type = std::uint64_t;
  32546. static constexpr std::uint64_t offset = 14695981039346656037ull;
  32547. static constexpr std::uint64_t prime = 1099511628211ull;
  32548. };
  32549. }
  32550. /**
  32551. * Internal details not to be documented.
  32552. * @endcond
  32553. */
  32554. /**
  32555. * @brief Zero overhead unique identifier.
  32556. *
  32557. * A hashed string is a compile-time tool that allows users to use
  32558. * human-readable identifers in the codebase while using their numeric
  32559. * counterparts at runtime.<br/>
  32560. * Because of that, a hashed string can also be used in constant expressions if
  32561. * required.
  32562. *
  32563. * @tparam Char Character type.
  32564. */
  32565. template<typename Char>
  32566. class basic_hashed_string {
  32567. using traits_type = internal::fnv1a_traits<id_type>;
  32568. struct const_wrapper {
  32569. // non-explicit constructor on purpose
  32570. constexpr const_wrapper(const Char *curr) ENTT_NOEXCEPT: str{curr} {}
  32571. const Char *str;
  32572. };
  32573. // Fowler–Noll–Vo hash function v. 1a - the good
  32574. [[nodiscard]] static constexpr id_type helper(const Char *curr) ENTT_NOEXCEPT {
  32575. auto value = traits_type::offset;
  32576. while(*curr != 0) {
  32577. value = (value ^ static_cast<traits_type::type>(*(curr++))) * traits_type::prime;
  32578. }
  32579. return value;
  32580. }
  32581. public:
  32582. /*! @brief Character type. */
  32583. using value_type = Char;
  32584. /*! @brief Unsigned integer type. */
  32585. using hash_type = id_type;
  32586. /**
  32587. * @brief Returns directly the numeric representation of a string view.
  32588. * @param str Human-readable identifer.
  32589. * @param size Length of the string to hash.
  32590. * @return The numeric representation of the string.
  32591. */
  32592. [[nodiscard]] static constexpr hash_type value(const value_type *str, std::size_t size) ENTT_NOEXCEPT {
  32593. id_type partial{traits_type::offset};
  32594. while(size--) { partial = (partial^(str++)[0])*traits_type::prime; }
  32595. return partial;
  32596. }
  32597. /**
  32598. * @brief Returns directly the numeric representation of a string.
  32599. *
  32600. * Forcing template resolution avoids implicit conversions. An
  32601. * human-readable identifier can be anything but a plain, old bunch of
  32602. * characters.<br/>
  32603. * Example of use:
  32604. * @code{.cpp}
  32605. * const auto value = basic_hashed_string<char>::to_value("my.png");
  32606. * @endcode
  32607. *
  32608. * @tparam N Number of characters of the identifier.
  32609. * @param str Human-readable identifer.
  32610. * @return The numeric representation of the string.
  32611. */
  32612. template<std::size_t N>
  32613. [[nodiscard]] static constexpr hash_type value(const value_type (&str)[N]) ENTT_NOEXCEPT {
  32614. return helper(str);
  32615. }
  32616. /**
  32617. * @brief Returns directly the numeric representation of a string.
  32618. * @param wrapper Helps achieving the purpose by relying on overloading.
  32619. * @return The numeric representation of the string.
  32620. */
  32621. [[nodiscard]] static hash_type value(const_wrapper wrapper) ENTT_NOEXCEPT {
  32622. return helper(wrapper.str);
  32623. }
  32624. /*! @brief Constructs an empty hashed string. */
  32625. constexpr basic_hashed_string() ENTT_NOEXCEPT
  32626. : str{nullptr}, hash{}
  32627. {}
  32628. /**
  32629. * @brief Constructs a hashed string from an array of const characters.
  32630. *
  32631. * Forcing template resolution avoids implicit conversions. An
  32632. * human-readable identifier can be anything but a plain, old bunch of
  32633. * characters.<br/>
  32634. * Example of use:
  32635. * @code{.cpp}
  32636. * basic_hashed_string<char> hs{"my.png"};
  32637. * @endcode
  32638. *
  32639. * @tparam N Number of characters of the identifier.
  32640. * @param curr Human-readable identifer.
  32641. */
  32642. template<std::size_t N>
  32643. constexpr basic_hashed_string(const value_type (&curr)[N]) ENTT_NOEXCEPT
  32644. : str{curr}, hash{helper(curr)}
  32645. {}
  32646. /**
  32647. * @brief Explicit constructor on purpose to avoid constructing a hashed
  32648. * string directly from a `const value_type *`.
  32649. * @param wrapper Helps achieving the purpose by relying on overloading.
  32650. */
  32651. explicit constexpr basic_hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
  32652. : str{wrapper.str}, hash{helper(wrapper.str)}
  32653. {}
  32654. /**
  32655. * @brief Returns the human-readable representation of a hashed string.
  32656. * @return The string used to initialize the instance.
  32657. */
  32658. [[nodiscard]] constexpr const value_type * data() const ENTT_NOEXCEPT {
  32659. return str;
  32660. }
  32661. /**
  32662. * @brief Returns the numeric representation of a hashed string.
  32663. * @return The numeric representation of the instance.
  32664. */
  32665. [[nodiscard]] constexpr hash_type value() const ENTT_NOEXCEPT {
  32666. return hash;
  32667. }
  32668. /*! @copydoc data */
  32669. [[nodiscard]] constexpr operator const value_type *() const ENTT_NOEXCEPT { return data(); }
  32670. /**
  32671. * @brief Returns the numeric representation of a hashed string.
  32672. * @return The numeric representation of the instance.
  32673. */
  32674. [[nodiscard]] constexpr operator hash_type() const ENTT_NOEXCEPT { return value(); }
  32675. /**
  32676. * @brief Compares two hashed strings.
  32677. * @param other Hashed string with which to compare.
  32678. * @return True if the two hashed strings are identical, false otherwise.
  32679. */
  32680. [[nodiscard]] constexpr bool operator==(const basic_hashed_string &other) const ENTT_NOEXCEPT {
  32681. return hash == other.hash;
  32682. }
  32683. private:
  32684. const value_type *str;
  32685. hash_type hash;
  32686. };
  32687. /**
  32688. * @brief Deduction guide.
  32689. *
  32690. * It allows to deduce the character type of the hashed string directly from a
  32691. * human-readable identifer provided to the constructor.
  32692. *
  32693. * @tparam Char Character type.
  32694. * @tparam N Number of characters of the identifier.
  32695. * @param str Human-readable identifer.
  32696. */
  32697. template<typename Char, std::size_t N>
  32698. basic_hashed_string(const Char (&str)[N])
  32699. -> basic_hashed_string<Char>;
  32700. /**
  32701. * @brief Compares two hashed strings.
  32702. * @tparam Char Character type.
  32703. * @param lhs A valid hashed string.
  32704. * @param rhs A valid hashed string.
  32705. * @return True if the two hashed strings are identical, false otherwise.
  32706. */
  32707. template<typename Char>
  32708. [[nodiscard]] constexpr bool operator!=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) ENTT_NOEXCEPT {
  32709. return !(lhs == rhs);
  32710. }
  32711. /*! @brief Aliases for common character types. */
  32712. using hashed_string = basic_hashed_string<char>;
  32713. /*! @brief Aliases for common character types. */
  32714. using hashed_wstring = basic_hashed_string<wchar_t>;
  32715. inline namespace literals {
  32716. /**
  32717. * @brief User defined literal for hashed strings.
  32718. * @param str The literal without its suffix.
  32719. * @return A properly initialized hashed string.
  32720. */
  32721. [[nodiscard]] constexpr entt::hashed_string operator"" _hs(const char *str, std::size_t) ENTT_NOEXCEPT {
  32722. return entt::hashed_string{str};
  32723. }
  32724. /**
  32725. * @brief User defined literal for hashed wstrings.
  32726. * @param str The literal without its suffix.
  32727. * @return A properly initialized hashed wstring.
  32728. */
  32729. [[nodiscard]] constexpr entt::hashed_wstring operator"" _hws(const wchar_t *str, std::size_t) ENTT_NOEXCEPT {
  32730. return entt::hashed_wstring{str};
  32731. }
  32732. }
  32733. }
  32734. #endif
  32735. // #include "fwd.hpp"
  32736. namespace entt {
  32737. /**
  32738. * @cond TURN_OFF_DOXYGEN
  32739. * Internal details not to be documented.
  32740. */
  32741. namespace internal {
  32742. struct ENTT_API type_seq final {
  32743. [[nodiscard]] static id_type next() ENTT_NOEXCEPT {
  32744. static ENTT_MAYBE_ATOMIC(id_type) value{};
  32745. return value++;
  32746. }
  32747. };
  32748. template<typename Type>
  32749. [[nodiscard]] constexpr auto stripped_type_name() ENTT_NOEXCEPT {
  32750. #if defined ENTT_PRETTY_FUNCTION
  32751. std::string_view pretty_function{ENTT_PRETTY_FUNCTION};
  32752. auto first = pretty_function.find_first_not_of(' ', pretty_function.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX)+1);
  32753. auto value = pretty_function.substr(first, pretty_function.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
  32754. return value;
  32755. #else
  32756. return std::string_view{""};
  32757. #endif
  32758. }
  32759. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  32760. [[nodiscard]] static constexpr std::string_view type_name(int) ENTT_NOEXCEPT {
  32761. constexpr auto value = stripped_type_name<Type>();
  32762. return value;
  32763. }
  32764. template<typename Type>
  32765. [[nodiscard]] static std::string_view type_name(char) ENTT_NOEXCEPT {
  32766. static const auto value = stripped_type_name<Type>();
  32767. return value;
  32768. }
  32769. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  32770. [[nodiscard]] static constexpr id_type type_hash(int) ENTT_NOEXCEPT {
  32771. constexpr auto stripped = stripped_type_name<Type>();
  32772. constexpr auto value = hashed_string::value(stripped.data(), stripped.size());
  32773. return value;
  32774. }
  32775. template<typename Type>
  32776. [[nodiscard]] static id_type type_hash(char) ENTT_NOEXCEPT {
  32777. static const auto value = [](const auto stripped) {
  32778. return hashed_string::value(stripped.data(), stripped.size());
  32779. }(stripped_type_name<Type>());
  32780. return value;
  32781. }
  32782. }
  32783. /**
  32784. * Internal details not to be documented.
  32785. * @endcond
  32786. */
  32787. /**
  32788. * @brief Type sequential identifier.
  32789. * @tparam Type Type for which to generate a sequential identifier.
  32790. */
  32791. template<typename Type, typename = void>
  32792. struct ENTT_API type_seq final {
  32793. /**
  32794. * @brief Returns the sequential identifier of a given type.
  32795. * @return The sequential identifier of a given type.
  32796. */
  32797. [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
  32798. static const id_type value = internal::type_seq::next();
  32799. return value;
  32800. }
  32801. /*! @copydoc value */
  32802. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  32803. };
  32804. /**
  32805. * @brief Type hash.
  32806. * @tparam Type Type for which to generate a hash value.
  32807. */
  32808. template<typename Type, typename = void>
  32809. struct type_hash final {
  32810. /**
  32811. * @brief Returns the numeric representation of a given type.
  32812. * @return The numeric representation of the given type.
  32813. */
  32814. #if defined ENTT_PRETTY_FUNCTION
  32815. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  32816. return internal::type_hash<Type>(0);
  32817. #else
  32818. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  32819. return type_seq<Type>::value();
  32820. #endif
  32821. }
  32822. /*! @copydoc value */
  32823. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  32824. };
  32825. /**
  32826. * @brief Type name.
  32827. * @tparam Type Type for which to generate a name.
  32828. */
  32829. template<typename Type, typename = void>
  32830. struct type_name final {
  32831. /**
  32832. * @brief Returns the name of a given type.
  32833. * @return The name of the given type.
  32834. */
  32835. [[nodiscard]] static constexpr std::string_view value() ENTT_NOEXCEPT {
  32836. return internal::type_name<Type>(0);
  32837. }
  32838. /*! @copydoc value */
  32839. [[nodiscard]] constexpr operator std::string_view() const ENTT_NOEXCEPT { return value(); }
  32840. };
  32841. /*! @brief Implementation specific information about a type. */
  32842. class type_info final {
  32843. template<typename>
  32844. friend type_info type_id() ENTT_NOEXCEPT;
  32845. type_info(id_type seq_v, id_type hash_v, std::string_view name_v) ENTT_NOEXCEPT
  32846. : seq_value{seq_v},
  32847. hash_value{hash_v},
  32848. name_value{name_v}
  32849. {}
  32850. public:
  32851. /*! @brief Default constructor. */
  32852. type_info() ENTT_NOEXCEPT
  32853. : type_info({}, {}, {})
  32854. {}
  32855. /*! @brief Default copy constructor. */
  32856. type_info(const type_info &) ENTT_NOEXCEPT = default;
  32857. /*! @brief Default move constructor. */
  32858. type_info(type_info &&) ENTT_NOEXCEPT = default;
  32859. /**
  32860. * @brief Default copy assignment operator.
  32861. * @return This type info object.
  32862. */
  32863. type_info & operator=(const type_info &) ENTT_NOEXCEPT = default;
  32864. /**
  32865. * @brief Default move assignment operator.
  32866. * @return This type info object.
  32867. */
  32868. type_info & operator=(type_info &&) ENTT_NOEXCEPT = default;
  32869. /**
  32870. * @brief Checks if a type info object is properly initialized.
  32871. * @return True if the object is properly initialized, false otherwise.
  32872. */
  32873. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  32874. return name_value.data() != nullptr;
  32875. }
  32876. /**
  32877. * @brief Type sequential identifier.
  32878. * @return Type sequential identifier.
  32879. */
  32880. [[nodiscard]] id_type seq() const ENTT_NOEXCEPT {
  32881. return seq_value;
  32882. }
  32883. /**
  32884. * @brief Type hash.
  32885. * @return Type hash.
  32886. */
  32887. [[nodiscard]] id_type hash() const ENTT_NOEXCEPT {
  32888. return hash_value;
  32889. }
  32890. /**
  32891. * @brief Type name.
  32892. * @return Type name.
  32893. */
  32894. [[nodiscard]] std::string_view name() const ENTT_NOEXCEPT {
  32895. return name_value;
  32896. }
  32897. /**
  32898. * @brief Compares the contents of two type info objects.
  32899. * @param other Object with which to compare.
  32900. * @return False if the two contents differ, true otherwise.
  32901. */
  32902. [[nodiscard]] bool operator==(const type_info &other) const ENTT_NOEXCEPT {
  32903. return hash_value == other.hash_value;
  32904. }
  32905. private:
  32906. id_type seq_value;
  32907. id_type hash_value;
  32908. std::string_view name_value;
  32909. };
  32910. /**
  32911. * @brief Compares the contents of two type info objects.
  32912. * @param lhs A type info object.
  32913. * @param rhs A type info object.
  32914. * @return True if the two contents differ, false otherwise.
  32915. */
  32916. [[nodiscard]] inline bool operator!=(const type_info &lhs, const type_info &rhs) ENTT_NOEXCEPT {
  32917. return !(lhs == rhs);
  32918. }
  32919. /**
  32920. * @brief Returns the type info object for a given type.
  32921. * @tparam Type Type for which to generate a type info object.
  32922. * @return The type info object for the given type.
  32923. */
  32924. template<typename Type>
  32925. [[nodiscard]] type_info type_id() ENTT_NOEXCEPT {
  32926. return type_info{
  32927. type_seq<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  32928. type_hash<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  32929. type_name<std::remove_cv_t<std::remove_reference_t<Type>>>::value()
  32930. };
  32931. }
  32932. }
  32933. #endif
  32934. // #include "type_traits.hpp"
  32935. #ifndef ENTT_CORE_TYPE_TRAITS_HPP
  32936. #define ENTT_CORE_TYPE_TRAITS_HPP
  32937. #include <cstddef>
  32938. #include <iterator>
  32939. #include <type_traits>
  32940. #include <utility>
  32941. // #include "../config/config.h"
  32942. // #include "fwd.hpp"
  32943. namespace entt {
  32944. /**
  32945. * @brief Utility class to disambiguate overloaded functions.
  32946. * @tparam N Number of choices available.
  32947. */
  32948. template<std::size_t N>
  32949. struct choice_t
  32950. // Unfortunately, doxygen cannot parse such a construct.
  32951. /*! @cond TURN_OFF_DOXYGEN */
  32952. : choice_t<N-1>
  32953. /*! @endcond */
  32954. {};
  32955. /*! @copybrief choice_t */
  32956. template<>
  32957. struct choice_t<0> {};
  32958. /**
  32959. * @brief Variable template for the choice trick.
  32960. * @tparam N Number of choices available.
  32961. */
  32962. template<std::size_t N>
  32963. inline constexpr choice_t<N> choice{};
  32964. /**
  32965. * @brief Identity type trait.
  32966. *
  32967. * Useful to establish non-deduced contexts in template argument deduction
  32968. * (waiting for C++20) or to provide types through function arguments.
  32969. *
  32970. * @tparam Type A type.
  32971. */
  32972. template<typename Type>
  32973. struct type_identity {
  32974. /*! @brief Identity type. */
  32975. using type = Type;
  32976. };
  32977. /**
  32978. * @brief Helper type.
  32979. * @tparam Type A type.
  32980. */
  32981. template<typename Type>
  32982. using type_identity_t = typename type_identity<Type>::type;
  32983. /**
  32984. * @brief A type-only `sizeof` wrapper that returns 0 where `sizeof` complains.
  32985. * @tparam Type The type of which to return the size.
  32986. * @tparam The size of the type if `sizeof` accepts it, 0 otherwise.
  32987. */
  32988. template<typename Type, typename = void>
  32989. struct size_of: std::integral_constant<std::size_t, 0u> {};
  32990. /*! @copydoc size_of */
  32991. template<typename Type>
  32992. struct size_of<Type, std::void_t<decltype(sizeof(Type))>>
  32993. : std::integral_constant<std::size_t, sizeof(Type)>
  32994. {};
  32995. /**
  32996. * @brief Helper variable template.
  32997. * @tparam Type The type of which to return the size.
  32998. */
  32999. template<class Type>
  33000. inline constexpr std::size_t size_of_v = size_of<Type>::value;
  33001. /**
  33002. * @brief Using declaration to be used to _repeat_ the same type a number of
  33003. * times equal to the size of a given parameter pack.
  33004. * @tparam Type A type to repeat.
  33005. */
  33006. template<typename Type, typename>
  33007. using unpack_as_t = Type;
  33008. /**
  33009. * @brief Helper variable template to be used to _repeat_ the same value a
  33010. * number of times equal to the size of a given parameter pack.
  33011. * @tparam Value A value to repeat.
  33012. */
  33013. template<auto Value, typename>
  33014. inline constexpr auto unpack_as_v = Value;
  33015. /**
  33016. * @brief Wraps a static constant.
  33017. * @tparam Value A static constant.
  33018. */
  33019. template<auto Value>
  33020. using integral_constant = std::integral_constant<decltype(Value), Value>;
  33021. /**
  33022. * @brief Alias template to facilitate the creation of named values.
  33023. * @tparam Value A constant value at least convertible to `id_type`.
  33024. */
  33025. template<id_type Value>
  33026. using tag = integral_constant<Value>;
  33027. /**
  33028. * @brief A class to use to push around lists of types, nothing more.
  33029. * @tparam Type Types provided by the type list.
  33030. */
  33031. template<typename... Type>
  33032. struct type_list {
  33033. /*! @brief Type list type. */
  33034. using type = type_list;
  33035. /*! @brief Compile-time number of elements in the type list. */
  33036. static constexpr auto size = sizeof...(Type);
  33037. };
  33038. /*! @brief Primary template isn't defined on purpose. */
  33039. template<std::size_t, typename>
  33040. struct type_list_element;
  33041. /**
  33042. * @brief Provides compile-time indexed access to the types of a type list.
  33043. * @tparam Index Index of the type to return.
  33044. * @tparam Type First type provided by the type list.
  33045. * @tparam Other Other types provided by the type list.
  33046. */
  33047. template<std::size_t Index, typename Type, typename... Other>
  33048. struct type_list_element<Index, type_list<Type, Other...>>
  33049. : type_list_element<Index - 1u, type_list<Other...>>
  33050. {};
  33051. /**
  33052. * @brief Provides compile-time indexed access to the types of a type list.
  33053. * @tparam Type First type provided by the type list.
  33054. * @tparam Other Other types provided by the type list.
  33055. */
  33056. template<typename Type, typename... Other>
  33057. struct type_list_element<0u, type_list<Type, Other...>> {
  33058. /*! @brief Searched type. */
  33059. using type = Type;
  33060. };
  33061. /**
  33062. * @brief Helper type.
  33063. * @tparam Index Index of the type to return.
  33064. * @tparam List Type list to search into.
  33065. */
  33066. template<std::size_t Index, typename List>
  33067. using type_list_element_t = typename type_list_element<Index, List>::type;
  33068. /**
  33069. * @brief Concatenates multiple type lists.
  33070. * @tparam Type Types provided by the first type list.
  33071. * @tparam Other Types provided by the second type list.
  33072. * @return A type list composed by the types of both the type lists.
  33073. */
  33074. template<typename... Type, typename... Other>
  33075. constexpr type_list<Type..., Other...> operator+(type_list<Type...>, type_list<Other...>) { return {}; }
  33076. /*! @brief Primary template isn't defined on purpose. */
  33077. template<typename...>
  33078. struct type_list_cat;
  33079. /*! @brief Concatenates multiple type lists. */
  33080. template<>
  33081. struct type_list_cat<> {
  33082. /*! @brief A type list composed by the types of all the type lists. */
  33083. using type = type_list<>;
  33084. };
  33085. /**
  33086. * @brief Concatenates multiple type lists.
  33087. * @tparam Type Types provided by the first type list.
  33088. * @tparam Other Types provided by the second type list.
  33089. * @tparam List Other type lists, if any.
  33090. */
  33091. template<typename... Type, typename... Other, typename... List>
  33092. struct type_list_cat<type_list<Type...>, type_list<Other...>, List...> {
  33093. /*! @brief A type list composed by the types of all the type lists. */
  33094. using type = typename type_list_cat<type_list<Type..., Other...>, List...>::type;
  33095. };
  33096. /**
  33097. * @brief Concatenates multiple type lists.
  33098. * @tparam Type Types provided by the type list.
  33099. */
  33100. template<typename... Type>
  33101. struct type_list_cat<type_list<Type...>> {
  33102. /*! @brief A type list composed by the types of all the type lists. */
  33103. using type = type_list<Type...>;
  33104. };
  33105. /**
  33106. * @brief Helper type.
  33107. * @tparam List Type lists to concatenate.
  33108. */
  33109. template<typename... List>
  33110. using type_list_cat_t = typename type_list_cat<List...>::type;
  33111. /*! @brief Primary template isn't defined on purpose. */
  33112. template<typename>
  33113. struct type_list_unique;
  33114. /**
  33115. * @brief Removes duplicates types from a type list.
  33116. * @tparam Type One of the types provided by the given type list.
  33117. * @tparam Other The other types provided by the given type list.
  33118. */
  33119. template<typename Type, typename... Other>
  33120. struct type_list_unique<type_list<Type, Other...>> {
  33121. /*! @brief A type list without duplicate types. */
  33122. using type = std::conditional_t<
  33123. std::disjunction_v<std::is_same<Type, Other>...>,
  33124. typename type_list_unique<type_list<Other...>>::type,
  33125. type_list_cat_t<type_list<Type>, typename type_list_unique<type_list<Other...>>::type>
  33126. >;
  33127. };
  33128. /*! @brief Removes duplicates types from a type list. */
  33129. template<>
  33130. struct type_list_unique<type_list<>> {
  33131. /*! @brief A type list without duplicate types. */
  33132. using type = type_list<>;
  33133. };
  33134. /**
  33135. * @brief Helper type.
  33136. * @tparam Type A type list.
  33137. */
  33138. template<typename Type>
  33139. using type_list_unique_t = typename type_list_unique<Type>::type;
  33140. /**
  33141. * @brief Provides the member constant `value` to true if a type list contains a
  33142. * given type, false otherwise.
  33143. * @tparam List Type list.
  33144. * @tparam Type Type to look for.
  33145. */
  33146. template<typename List, typename Type>
  33147. struct type_list_contains;
  33148. /**
  33149. * @copybrief type_list_contains
  33150. * @tparam Type Types provided by the type list.
  33151. * @tparam Other Type to look for.
  33152. */
  33153. template<typename... Type, typename Other>
  33154. struct type_list_contains<type_list<Type...>, Other>: std::disjunction<std::is_same<Type, Other>...> {};
  33155. /**
  33156. * @brief Helper variable template.
  33157. * @tparam List Type list.
  33158. * @tparam Type Type to look for.
  33159. */
  33160. template<class List, typename Type>
  33161. inline constexpr bool type_list_contains_v = type_list_contains<List, Type>::value;
  33162. /*! @brief Primary template isn't defined on purpose. */
  33163. template<typename...>
  33164. struct type_list_diff;
  33165. /**
  33166. * @brief Computes the difference between two type lists.
  33167. * @tparam Type Types provided by the first type list.
  33168. * @tparam Other Types provided by the second type list.
  33169. */
  33170. template<typename... Type, typename... Other>
  33171. struct type_list_diff<type_list<Type...>, type_list<Other...>> {
  33172. /*! @brief A type list that is the difference between the two type lists. */
  33173. using type = type_list_cat_t<std::conditional_t<type_list_contains_v<type_list<Other...>, Type>, type_list<>, type_list<Type>>...>;
  33174. };
  33175. /**
  33176. * @brief Helper type.
  33177. * @tparam List Type lists between which to compute the difference.
  33178. */
  33179. template<typename... List>
  33180. using type_list_diff_t = typename type_list_diff<List...>::type;
  33181. /**
  33182. * @brief A class to use to push around lists of constant values, nothing more.
  33183. * @tparam Value Values provided by the value list.
  33184. */
  33185. template<auto... Value>
  33186. struct value_list {
  33187. /*! @brief Value list type. */
  33188. using type = value_list;
  33189. /*! @brief Compile-time number of elements in the value list. */
  33190. static constexpr auto size = sizeof...(Value);
  33191. };
  33192. /*! @brief Primary template isn't defined on purpose. */
  33193. template<std::size_t, typename>
  33194. struct value_list_element;
  33195. /**
  33196. * @brief Provides compile-time indexed access to the values of a value list.
  33197. * @tparam Index Index of the value to return.
  33198. * @tparam Value First value provided by the value list.
  33199. * @tparam Other Other values provided by the value list.
  33200. */
  33201. template<std::size_t Index, auto Value, auto... Other>
  33202. struct value_list_element<Index, value_list<Value, Other...>>
  33203. : value_list_element<Index - 1u, value_list<Other...>>
  33204. {};
  33205. /**
  33206. * @brief Provides compile-time indexed access to the types of a type list.
  33207. * @tparam Value First value provided by the value list.
  33208. * @tparam Other Other values provided by the value list.
  33209. */
  33210. template<auto Value, auto... Other>
  33211. struct value_list_element<0u, value_list<Value, Other...>> {
  33212. /*! @brief Searched value. */
  33213. static constexpr auto value = Value;
  33214. };
  33215. /**
  33216. * @brief Helper type.
  33217. * @tparam Index Index of the value to return.
  33218. * @tparam List Value list to search into.
  33219. */
  33220. template<std::size_t Index, typename List>
  33221. inline constexpr auto value_list_element_v = value_list_element<Index, List>::value;
  33222. /**
  33223. * @brief Concatenates multiple value lists.
  33224. * @tparam Value Values provided by the first value list.
  33225. * @tparam Other Values provided by the second value list.
  33226. * @return A value list composed by the values of both the value lists.
  33227. */
  33228. template<auto... Value, auto... Other>
  33229. constexpr value_list<Value..., Other...> operator+(value_list<Value...>, value_list<Other...>) { return {}; }
  33230. /*! @brief Primary template isn't defined on purpose. */
  33231. template<typename...>
  33232. struct value_list_cat;
  33233. /*! @brief Concatenates multiple value lists. */
  33234. template<>
  33235. struct value_list_cat<> {
  33236. /*! @brief A value list composed by the values of all the value lists. */
  33237. using type = value_list<>;
  33238. };
  33239. /**
  33240. * @brief Concatenates multiple value lists.
  33241. * @tparam Value Values provided by the first value list.
  33242. * @tparam Other Values provided by the second value list.
  33243. * @tparam List Other value lists, if any.
  33244. */
  33245. template<auto... Value, auto... Other, typename... List>
  33246. struct value_list_cat<value_list<Value...>, value_list<Other...>, List...> {
  33247. /*! @brief A value list composed by the values of all the value lists. */
  33248. using type = typename value_list_cat<value_list<Value..., Other...>, List...>::type;
  33249. };
  33250. /**
  33251. * @brief Concatenates multiple value lists.
  33252. * @tparam Value Values provided by the value list.
  33253. */
  33254. template<auto... Value>
  33255. struct value_list_cat<value_list<Value...>> {
  33256. /*! @brief A value list composed by the values of all the value lists. */
  33257. using type = value_list<Value...>;
  33258. };
  33259. /**
  33260. * @brief Helper type.
  33261. * @tparam List Value lists to concatenate.
  33262. */
  33263. template<typename... List>
  33264. using value_list_cat_t = typename value_list_cat<List...>::type;
  33265. /**
  33266. * @cond TURN_OFF_DOXYGEN
  33267. * Internal details not to be documented.
  33268. */
  33269. namespace internal {
  33270. template<typename>
  33271. [[nodiscard]] constexpr bool is_equality_comparable(...) { return false; }
  33272. template<typename Type>
  33273. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<0>)
  33274. -> decltype(std::declval<Type>() == std::declval<Type>()) { return true; }
  33275. template<typename Type>
  33276. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<1>)
  33277. -> decltype(std::declval<typename Type::value_type>(), std::declval<Type>() == std::declval<Type>()) {
  33278. if constexpr(std::is_same_v<typename Type::value_type, Type>) {
  33279. return is_equality_comparable<Type>(choice<0>);
  33280. } else {
  33281. return is_equality_comparable<typename Type::value_type>(choice<2>);
  33282. }
  33283. }
  33284. template<typename Type>
  33285. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<2>)
  33286. -> decltype(std::declval<typename Type::mapped_type>(), std::declval<Type>() == std::declval<Type>()) {
  33287. return is_equality_comparable<typename Type::key_type>(choice<2>) && is_equality_comparable<typename Type::mapped_type>(choice<2>);
  33288. }
  33289. }
  33290. /**
  33291. * Internal details not to be documented.
  33292. * @endcond
  33293. */
  33294. /**
  33295. * @brief Provides the member constant `value` to true if a given type is
  33296. * equality comparable, false otherwise.
  33297. * @tparam Type The type to test.
  33298. */
  33299. template<typename Type, typename = void>
  33300. struct is_equality_comparable: std::bool_constant<internal::is_equality_comparable<Type>(choice<2>)> {};
  33301. /**
  33302. * @brief Helper variable template.
  33303. * @tparam Type The type to test.
  33304. */
  33305. template<class Type>
  33306. inline constexpr bool is_equality_comparable_v = is_equality_comparable<Type>::value;
  33307. /*! @brief Same as std::is_invocable, but with tuples. */
  33308. template<typename, typename>
  33309. struct is_applicable: std::false_type {};
  33310. /**
  33311. * @copybrief is_applicable
  33312. * @tparam Func A valid function type.
  33313. * @tparam Tuple Tuple-like type.
  33314. * @tparam Args The list of arguments to use to probe the function type.
  33315. */
  33316. template<typename Func, template<typename...> class Tuple, typename... Args>
  33317. struct is_applicable<Func, Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  33318. /**
  33319. * @copybrief is_applicable
  33320. * @tparam Func A valid function type.
  33321. * @tparam Tuple Tuple-like type.
  33322. * @tparam Args The list of arguments to use to probe the function type.
  33323. */
  33324. template<typename Func, template<typename...> class Tuple, typename... Args>
  33325. struct is_applicable<Func, const Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  33326. /**
  33327. * @brief Helper variable template.
  33328. * @tparam Func A valid function type.
  33329. * @tparam Args The list of arguments to use to probe the function type.
  33330. */
  33331. template<typename Func, typename Args>
  33332. inline constexpr bool is_applicable_v = is_applicable<Func, Args>::value;
  33333. /*! @brief Same as std::is_invocable_r, but with tuples for arguments. */
  33334. template<typename, typename, typename>
  33335. struct is_applicable_r: std::false_type {};
  33336. /**
  33337. * @copybrief is_applicable_r
  33338. * @tparam Ret The type to which the return type of the function should be
  33339. * convertible.
  33340. * @tparam Func A valid function type.
  33341. * @tparam Args The list of arguments to use to probe the function type.
  33342. */
  33343. template<typename Ret, typename Func, typename... Args>
  33344. struct is_applicable_r<Ret, Func, std::tuple<Args...>>: std::is_invocable_r<Ret, Func, Args...> {};
  33345. /**
  33346. * @brief Helper variable template.
  33347. * @tparam Ret The type to which the return type of the function should be
  33348. * convertible.
  33349. * @tparam Func A valid function type.
  33350. * @tparam Args The list of arguments to use to probe the function type.
  33351. */
  33352. template<typename Ret, typename Func, typename Args>
  33353. inline constexpr bool is_applicable_r_v = is_applicable_r<Ret, Func, Args>::value;
  33354. /**
  33355. * @brief Provides the member constant `value` to true if a given type is
  33356. * complete, false otherwise.
  33357. * @tparam Type The type to test.
  33358. */
  33359. template<typename Type, typename = void>
  33360. struct is_complete: std::false_type {};
  33361. /*! @copydoc is_complete */
  33362. template<typename Type>
  33363. struct is_complete<Type, std::void_t<decltype(sizeof(Type))>>: std::true_type {};
  33364. /**
  33365. * @brief Helper variable template.
  33366. * @tparam Type The type to test.
  33367. */
  33368. template<typename Type>
  33369. inline constexpr bool is_complete_v = is_complete<Type>::value;
  33370. /**
  33371. * @brief Provides the member constant `value` to true if a given type is an
  33372. * iterator, false otherwise.
  33373. * @tparam Type The type to test.
  33374. */
  33375. template<typename Type, typename = void>
  33376. struct is_iterator: std::false_type {};
  33377. /*! @copydoc is_iterator */
  33378. template<typename Type>
  33379. struct is_iterator<Type, std::void_t<typename std::iterator_traits<Type>::iterator_category>>
  33380. : std::true_type
  33381. {};
  33382. /**
  33383. * @brief Helper variable template.
  33384. * @tparam Type The type to test.
  33385. */
  33386. template<typename Type>
  33387. inline constexpr bool is_iterator_v = is_iterator<Type>::value;
  33388. /**
  33389. * @brief Provides the member constant `value` to true if a given type is of the
  33390. * required iterator type, false otherwise.
  33391. * @tparam Type The type to test.
  33392. * @tparam It Required iterator type.
  33393. */
  33394. template<typename Type, typename It, typename = void>
  33395. struct is_iterator_type: std::false_type {};
  33396. /*! @copydoc is_iterator_type */
  33397. template<typename Type, typename It>
  33398. struct is_iterator_type<Type, It, std::enable_if_t<is_iterator_v<Type> && std::is_same_v<Type, It>>>
  33399. : std::true_type
  33400. {};
  33401. /*! @copydoc is_iterator_type */
  33402. template<typename Type, typename It>
  33403. struct is_iterator_type<Type, It, std::enable_if_t<!std::is_same_v<Type, It>, std::void_t<typename It::iterator_type>>>
  33404. : is_iterator_type<Type, typename It::iterator_type>
  33405. {};
  33406. /**
  33407. * @brief Helper variable template.
  33408. * @tparam Type The type to test.
  33409. * @tparam It Required iterator type.
  33410. */
  33411. template<typename Type, typename It>
  33412. inline constexpr bool is_iterator_type_v = is_iterator_type<Type, It>::value;
  33413. /**
  33414. * @brief Transcribes the constness of a type to another type.
  33415. * @tparam To The type to which to transcribe the constness.
  33416. * @tparam From The type from which to transcribe the constness.
  33417. */
  33418. template<typename To, typename From>
  33419. struct constness_as {
  33420. /*! @brief The type resulting from the transcription of the constness. */
  33421. using type = std::remove_const_t<To>;
  33422. };
  33423. /*! @copydoc constness_as */
  33424. template<typename To, typename From>
  33425. struct constness_as<To, const From> {
  33426. /*! @brief The type resulting from the transcription of the constness. */
  33427. using type = std::add_const_t<To>;
  33428. };
  33429. /**
  33430. * @brief Alias template to facilitate the transcription of the constness.
  33431. * @tparam To The type to which to transcribe the constness.
  33432. * @tparam From The type from which to transcribe the constness.
  33433. */
  33434. template<typename To, typename From>
  33435. using constness_as_t = typename constness_as<To, From>::type;
  33436. /**
  33437. * @brief Extracts the class of a non-static member object or function.
  33438. * @tparam Member A pointer to a non-static member object or function.
  33439. */
  33440. template<typename Member>
  33441. class member_class {
  33442. static_assert(std::is_member_pointer_v<Member>, "Invalid pointer type to non-static member object or function");
  33443. template<typename Class, typename Ret, typename... Args>
  33444. static Class * clazz(Ret(Class:: *)(Args...));
  33445. template<typename Class, typename Ret, typename... Args>
  33446. static Class * clazz(Ret(Class:: *)(Args...) const);
  33447. template<typename Class, typename Type>
  33448. static Class * clazz(Type Class:: *);
  33449. public:
  33450. /*! @brief The class of the given non-static member object or function. */
  33451. using type = std::remove_pointer_t<decltype(clazz(std::declval<Member>()))>;
  33452. };
  33453. /**
  33454. * @brief Helper type.
  33455. * @tparam Member A pointer to a non-static member object or function.
  33456. */
  33457. template<typename Member>
  33458. using member_class_t = typename member_class<Member>::type;
  33459. }
  33460. #endif
  33461. namespace entt {
  33462. /**
  33463. * @brief A SBO friendly, type-safe container for single values of any type.
  33464. * @tparam Len Size of the storage reserved for the small buffer optimization.
  33465. * @tparam Align Optional alignment requirement.
  33466. */
  33467. template<std::size_t Len, std::size_t Align>
  33468. class basic_any {
  33469. enum class operation: std::uint8_t { COPY, MOVE, DTOR, COMP, ADDR, CADDR, TYPE };
  33470. enum class policy: std::uint8_t { OWNER, REF, CREF };
  33471. using storage_type = std::aligned_storage_t<Len + !Len, Align>;
  33472. using vtable_type = const void *(const operation, const basic_any &, void *);
  33473. template<typename Type>
  33474. static constexpr bool in_situ = Len && alignof(Type) <= alignof(storage_type) && sizeof(Type) <= sizeof(storage_type) && std::is_nothrow_move_constructible_v<Type>;
  33475. template<typename Type>
  33476. [[nodiscard]] static constexpr policy type_to_policy() {
  33477. if constexpr(std::is_lvalue_reference_v<Type>) {
  33478. if constexpr(std::is_const_v<std::remove_reference_t<Type>>) {
  33479. return policy::CREF;
  33480. } else {
  33481. return policy::REF;
  33482. }
  33483. } else {
  33484. return policy::OWNER;
  33485. }
  33486. }
  33487. template<typename Type>
  33488. [[nodiscard]] static bool compare(const void *lhs, const void *rhs) {
  33489. if constexpr(!std::is_function_v<Type> && is_equality_comparable_v<Type>) {
  33490. return *static_cast<const Type *>(lhs) == *static_cast<const Type *>(rhs);
  33491. } else {
  33492. return lhs == rhs;
  33493. }
  33494. }
  33495. template<typename Type>
  33496. static const void * basic_vtable([[maybe_unused]] const operation op, [[maybe_unused]] const basic_any &from, [[maybe_unused]] void *to) {
  33497. static_assert(std::is_same_v<std::remove_reference_t<std::remove_const_t<Type>>, Type>, "Invalid type");
  33498. if constexpr(!std::is_void_v<Type>) {
  33499. const Type *instance = (in_situ<Type> && from.mode == policy::OWNER)
  33500. ? ENTT_LAUNDER(reinterpret_cast<const Type *>(&from.storage))
  33501. : static_cast<const Type *>(from.instance);
  33502. switch(op) {
  33503. case operation::COPY:
  33504. if constexpr(std::is_copy_constructible_v<Type>) {
  33505. static_cast<basic_any *>(to)->emplace<Type>(*instance);
  33506. }
  33507. break;
  33508. case operation::MOVE:
  33509. if constexpr(in_situ<Type>) {
  33510. if(from.mode == policy::OWNER) {
  33511. return new (&static_cast<basic_any *>(to)->storage) Type{std::move(*const_cast<Type *>(instance))};
  33512. }
  33513. }
  33514. return (static_cast<basic_any *>(to)->instance = std::exchange(const_cast<basic_any &>(from).instance, nullptr));
  33515. case operation::DTOR:
  33516. if(from.mode == policy::OWNER) {
  33517. if constexpr(in_situ<Type>) {
  33518. instance->~Type();
  33519. } else if constexpr(std::is_array_v<Type>) {
  33520. delete[] instance;
  33521. } else {
  33522. delete instance;
  33523. }
  33524. }
  33525. break;
  33526. case operation::COMP:
  33527. return compare<Type>(instance, (*static_cast<const basic_any **>(to))->data()) ? to : nullptr;
  33528. case operation::ADDR:
  33529. if(from.mode == policy::CREF) {
  33530. return nullptr;
  33531. }
  33532. [[fallthrough]];
  33533. case operation::CADDR:
  33534. return instance;
  33535. case operation::TYPE:
  33536. *static_cast<type_info *>(to) = type_id<Type>();
  33537. break;
  33538. }
  33539. }
  33540. return nullptr;
  33541. }
  33542. template<typename Type, typename... Args>
  33543. void initialize([[maybe_unused]] Args &&... args) {
  33544. if constexpr(!std::is_void_v<Type>) {
  33545. if constexpr(std::is_lvalue_reference_v<Type>) {
  33546. static_assert(sizeof...(Args) == 1u && (std::is_lvalue_reference_v<Args> && ...), "Invalid arguments");
  33547. instance = (std::addressof(args), ...);
  33548. } else if constexpr(in_situ<Type>) {
  33549. if constexpr(sizeof...(Args) != 0u && std::is_aggregate_v<Type>) {
  33550. new (&storage) Type{std::forward<Args>(args)...};
  33551. } else {
  33552. new (&storage) Type(std::forward<Args>(args)...);
  33553. }
  33554. } else {
  33555. if constexpr(sizeof...(Args) != 0u && std::is_aggregate_v<Type>) {
  33556. instance = new Type{std::forward<Args>(args)...};
  33557. } else {
  33558. instance = new Type(std::forward<Args>(args)...);
  33559. }
  33560. }
  33561. }
  33562. }
  33563. basic_any(const basic_any &other, const policy pol) ENTT_NOEXCEPT
  33564. : instance{other.data()},
  33565. vtable{other.vtable},
  33566. mode{pol}
  33567. {}
  33568. public:
  33569. /*! @brief Size of the internal storage. */
  33570. static constexpr auto length = Len;
  33571. /*! @brief Alignment requirement. */
  33572. static constexpr auto alignment = Align;
  33573. /*! @brief Default constructor. */
  33574. basic_any() ENTT_NOEXCEPT
  33575. : instance{},
  33576. vtable{&basic_vtable<void>},
  33577. mode{policy::OWNER}
  33578. {}
  33579. /**
  33580. * @brief Constructs a wrapper by directly initializing the new object.
  33581. * @tparam Type Type of object to use to initialize the wrapper.
  33582. * @tparam Args Types of arguments to use to construct the new instance.
  33583. * @param args Parameters to use to construct the instance.
  33584. */
  33585. template<typename Type, typename... Args>
  33586. explicit basic_any(std::in_place_type_t<Type>, Args &&... args)
  33587. : instance{},
  33588. vtable{&basic_vtable<std::remove_const_t<std::remove_reference_t<Type>>>},
  33589. mode{type_to_policy<Type>()}
  33590. {
  33591. initialize<Type>(std::forward<Args>(args)...);
  33592. }
  33593. /**
  33594. * @brief Constructs a wrapper that holds an unmanaged object.
  33595. * @tparam Type Type of object to use to initialize the wrapper.
  33596. * @param value An instance of an object to use to initialize the wrapper.
  33597. */
  33598. template<typename Type>
  33599. basic_any(std::reference_wrapper<Type> value) ENTT_NOEXCEPT
  33600. : basic_any{}
  33601. {
  33602. // invokes deprecated assignment operator (and avoids issues with vs2017)
  33603. *this = value;
  33604. }
  33605. /**
  33606. * @brief Constructs a wrapper from a given value.
  33607. * @tparam Type Type of object to use to initialize the wrapper.
  33608. * @param value An instance of an object to use to initialize the wrapper.
  33609. */
  33610. template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::decay_t<Type>, basic_any>>>
  33611. basic_any(Type &&value)
  33612. : instance{},
  33613. vtable{&basic_vtable<std::decay_t<Type>>},
  33614. mode{policy::OWNER}
  33615. {
  33616. initialize<std::decay_t<Type>>(std::forward<Type>(value));
  33617. }
  33618. /**
  33619. * @brief Copy constructor.
  33620. * @param other The instance to copy from.
  33621. */
  33622. basic_any(const basic_any &other)
  33623. : instance{},
  33624. vtable{&basic_vtable<void>},
  33625. mode{policy::OWNER}
  33626. {
  33627. other.vtable(operation::COPY, other, this);
  33628. }
  33629. /**
  33630. * @brief Move constructor.
  33631. * @param other The instance to move from.
  33632. */
  33633. basic_any(basic_any &&other) ENTT_NOEXCEPT
  33634. : instance{},
  33635. vtable{other.vtable},
  33636. mode{other.mode}
  33637. {
  33638. vtable(operation::MOVE, other, this);
  33639. }
  33640. /*! @brief Frees the internal storage, whatever it means. */
  33641. ~basic_any() {
  33642. vtable(operation::DTOR, *this, nullptr);
  33643. }
  33644. /**
  33645. * @brief Copy assignment operator.
  33646. * @param other The instance to copy from.
  33647. * @return This any object.
  33648. */
  33649. basic_any & operator=(const basic_any &other) {
  33650. reset();
  33651. other.vtable(operation::COPY, other, this);
  33652. return *this;
  33653. }
  33654. /**
  33655. * @brief Move assignment operator.
  33656. * @param other The instance to move from.
  33657. * @return This any object.
  33658. */
  33659. basic_any & operator=(basic_any &&other) ENTT_NOEXCEPT {
  33660. std::exchange(vtable, other.vtable)(operation::DTOR, *this, nullptr);
  33661. other.vtable(operation::MOVE, other, this);
  33662. mode = other.mode;
  33663. return *this;
  33664. }
  33665. /**
  33666. * @brief Value assignment operator.
  33667. * @tparam Type Type of object to use to initialize the wrapper.
  33668. * @param value An instance of an object to use to initialize the wrapper.
  33669. * @return This any object.
  33670. */
  33671. template<typename Type>
  33672. [[deprecated("Use std::in_place_type<T &>, entt::make_any<T &>, emplace<Type &> or forward_as_any instead")]]
  33673. basic_any & operator=(std::reference_wrapper<Type> value) ENTT_NOEXCEPT {
  33674. emplace<Type &>(value.get());
  33675. return *this;
  33676. }
  33677. /**
  33678. * @brief Value assignment operator.
  33679. * @tparam Type Type of object to use to initialize the wrapper.
  33680. * @param value An instance of an object to use to initialize the wrapper.
  33681. * @return This any object.
  33682. */
  33683. template<typename Type>
  33684. std::enable_if_t<!std::is_same_v<std::decay_t<Type>, basic_any>, basic_any &>
  33685. operator=(Type &&value) {
  33686. emplace<std::decay_t<Type>>(std::forward<Type>(value));
  33687. return *this;
  33688. }
  33689. /**
  33690. * @brief Returns the type of the contained object.
  33691. * @return The type of the contained object, if any.
  33692. */
  33693. [[nodiscard]] type_info type() const ENTT_NOEXCEPT {
  33694. type_info info{};
  33695. vtable(operation::TYPE, *this, &info);
  33696. return info;
  33697. }
  33698. /**
  33699. * @brief Returns an opaque pointer to the contained instance.
  33700. * @return An opaque pointer the contained instance, if any.
  33701. */
  33702. [[nodiscard]] const void * data() const ENTT_NOEXCEPT {
  33703. return vtable(operation::CADDR, *this, nullptr);
  33704. }
  33705. /*! @copydoc data */
  33706. [[nodiscard]] void * data() ENTT_NOEXCEPT {
  33707. return const_cast<void *>(vtable(operation::ADDR, *this, nullptr));
  33708. }
  33709. /**
  33710. * @brief Replaces the contained object by creating a new instance directly.
  33711. * @tparam Type Type of object to use to initialize the wrapper.
  33712. * @tparam Args Types of arguments to use to construct the new instance.
  33713. * @param args Parameters to use to construct the instance.
  33714. */
  33715. template<typename Type, typename... Args>
  33716. void emplace(Args &&... args) {
  33717. std::exchange(vtable, &basic_vtable<std::remove_const_t<std::remove_reference_t<Type>>>)(operation::DTOR, *this, nullptr);
  33718. mode = type_to_policy<Type>();
  33719. initialize<Type>(std::forward<Args>(args)...);
  33720. }
  33721. /*! @brief Destroys contained object */
  33722. void reset() {
  33723. std::exchange(vtable, &basic_vtable<void>)(operation::DTOR, *this, nullptr);
  33724. mode = policy::OWNER;
  33725. }
  33726. /**
  33727. * @brief Returns false if a wrapper is empty, true otherwise.
  33728. * @return False if the wrapper is empty, true otherwise.
  33729. */
  33730. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  33731. return !(vtable(operation::CADDR, *this, nullptr) == nullptr);
  33732. }
  33733. /**
  33734. * @brief Checks if two wrappers differ in their content.
  33735. * @param other Wrapper with which to compare.
  33736. * @return False if the two objects differ in their content, true otherwise.
  33737. */
  33738. bool operator==(const basic_any &other) const ENTT_NOEXCEPT {
  33739. const basic_any *trampoline = &other;
  33740. return type() == other.type() && (vtable(operation::COMP, *this, &trampoline) || !other.data());
  33741. }
  33742. /**
  33743. * @brief Aliasing constructor.
  33744. * @return A wrapper that shares a reference to an unmanaged object.
  33745. */
  33746. [[nodiscard]] basic_any as_ref() ENTT_NOEXCEPT {
  33747. return basic_any{*this, (mode == policy::CREF ? policy::CREF : policy::REF)};
  33748. }
  33749. /*! @copydoc as_ref */
  33750. [[nodiscard]] basic_any as_ref() const ENTT_NOEXCEPT {
  33751. return basic_any{*this, policy::CREF};
  33752. }
  33753. /**
  33754. * @brief Returns true if a wrapper owns its object, false otherwise.
  33755. * @return True if the wrapper owns its object, false otherwise.
  33756. */
  33757. [[nodiscard]] bool owner() const ENTT_NOEXCEPT {
  33758. return (mode == policy::OWNER);
  33759. }
  33760. private:
  33761. union { const void *instance; storage_type storage; };
  33762. vtable_type *vtable;
  33763. policy mode;
  33764. };
  33765. /**
  33766. * @brief Checks if two wrappers differ in their content.
  33767. * @tparam Len Size of the storage reserved for the small buffer optimization.
  33768. * @tparam Align Alignment requirement.
  33769. * @param lhs A wrapper, either empty or not.
  33770. * @param rhs A wrapper, either empty or not.
  33771. * @return True if the two wrappers differ in their content, false otherwise.
  33772. */
  33773. template<std::size_t Len, std::size_t Align>
  33774. [[nodiscard]] inline bool operator!=(const basic_any<Len, Align> &lhs, const basic_any<Len, Align> &rhs) ENTT_NOEXCEPT {
  33775. return !(lhs == rhs);
  33776. }
  33777. /**
  33778. * @brief Performs type-safe access to the contained object.
  33779. * @tparam Type Type to which conversion is required.
  33780. * @tparam Len Size of the storage reserved for the small buffer optimization.
  33781. * @tparam Align Alignment requirement.
  33782. * @param data Target any object.
  33783. * @return The element converted to the requested type.
  33784. */
  33785. template<typename Type, std::size_t Len, std::size_t Align>
  33786. Type any_cast(const basic_any<Len, Align> &data) ENTT_NOEXCEPT {
  33787. const auto * const instance = any_cast<std::remove_reference_t<Type>>(&data);
  33788. ENTT_ASSERT(instance, "Invalid instance");
  33789. return static_cast<Type>(*instance);
  33790. }
  33791. /*! @copydoc any_cast */
  33792. template<typename Type, std::size_t Len, std::size_t Align>
  33793. Type any_cast(basic_any<Len, Align> &data) ENTT_NOEXCEPT {
  33794. // forces const on non-reference types to make them work also with wrappers for const references
  33795. auto * const instance = any_cast<std::remove_reference_t<const Type>>(&data);
  33796. ENTT_ASSERT(instance, "Invalid instance");
  33797. return static_cast<Type>(*instance);
  33798. }
  33799. /*! @copydoc any_cast */
  33800. template<typename Type, std::size_t Len, std::size_t Align>
  33801. Type any_cast(basic_any<Len, Align> &&data) ENTT_NOEXCEPT {
  33802. // forces const on non-reference types to make them work also with wrappers for const references
  33803. auto * const instance = any_cast<std::remove_reference_t<const Type>>(&data);
  33804. ENTT_ASSERT(instance, "Invalid instance");
  33805. return static_cast<Type>(std::move(*instance));
  33806. }
  33807. /*! @copydoc any_cast */
  33808. template<typename Type, std::size_t Len, std::size_t Align>
  33809. const Type * any_cast(const basic_any<Len, Align> *data) ENTT_NOEXCEPT {
  33810. return (data->type() == type_id<Type>() ? static_cast<const Type *>(data->data()) : nullptr);
  33811. }
  33812. /*! @copydoc any_cast */
  33813. template<typename Type, std::size_t Len, std::size_t Align>
  33814. Type * any_cast(basic_any<Len, Align> *data) ENTT_NOEXCEPT {
  33815. // last attempt to make wrappers for const references return their values
  33816. return (data->type() == type_id<Type>() ? static_cast<Type *>(static_cast<constness_as_t<basic_any<Len, Align>, Type> *>(data)->data()) : nullptr);
  33817. }
  33818. /**
  33819. * @brief Constructs a wrapper from a given type, passing it all arguments.
  33820. * @tparam Type Type of object to use to initialize the wrapper.
  33821. * @tparam Len Size of the storage reserved for the small buffer optimization.
  33822. * @tparam Align Optional alignment requirement.
  33823. * @tparam Args Types of arguments to use to construct the new instance.
  33824. * @param args Parameters to use to construct the instance.
  33825. * @return A properly initialized wrapper for an object of the given type.
  33826. */
  33827. template<typename Type, std::size_t Len = basic_any<>::length, std::size_t Align = basic_any<Len>::alignment, typename... Args>
  33828. basic_any<Len, Align> make_any(Args &&... args) {
  33829. return basic_any<Len, Align>{std::in_place_type<Type>, std::forward<Args>(args)...};
  33830. }
  33831. /**
  33832. * @brief Forwards its argument and avoids copies for lvalue references.
  33833. * @tparam Len Size of the storage reserved for the small buffer optimization.
  33834. * @tparam Align Optional alignment requirement.
  33835. * @tparam Type Type of argument to use to construct the new instance.
  33836. * @param value Parameter to use to construct the instance.
  33837. * @return A properly initialized and not necessarily owning wrapper.
  33838. */
  33839. template<std::size_t Len = basic_any<>::length, std::size_t Align = basic_any<Len>::alignment, typename Type>
  33840. basic_any<Len, Align> forward_as_any(Type &&value) {
  33841. return basic_any<Len, Align>{std::in_place_type<std::conditional_t<std::is_rvalue_reference_v<Type>, std::decay_t<Type>, Type>>, std::forward<Type>(value)};
  33842. }
  33843. }
  33844. #endif
  33845. // #include "../core/type_info.hpp"
  33846. #ifndef ENTT_CORE_TYPE_INFO_HPP
  33847. #define ENTT_CORE_TYPE_INFO_HPP
  33848. #include <string_view>
  33849. #include <type_traits>
  33850. // #include "../config/config.h"
  33851. // #include "../core/attribute.h"
  33852. // #include "hashed_string.hpp"
  33853. // #include "fwd.hpp"
  33854. namespace entt {
  33855. /**
  33856. * @cond TURN_OFF_DOXYGEN
  33857. * Internal details not to be documented.
  33858. */
  33859. namespace internal {
  33860. struct ENTT_API type_seq final {
  33861. [[nodiscard]] static id_type next() ENTT_NOEXCEPT {
  33862. static ENTT_MAYBE_ATOMIC(id_type) value{};
  33863. return value++;
  33864. }
  33865. };
  33866. template<typename Type>
  33867. [[nodiscard]] constexpr auto stripped_type_name() ENTT_NOEXCEPT {
  33868. #if defined ENTT_PRETTY_FUNCTION
  33869. std::string_view pretty_function{ENTT_PRETTY_FUNCTION};
  33870. auto first = pretty_function.find_first_not_of(' ', pretty_function.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX)+1);
  33871. auto value = pretty_function.substr(first, pretty_function.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
  33872. return value;
  33873. #else
  33874. return std::string_view{""};
  33875. #endif
  33876. }
  33877. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  33878. [[nodiscard]] static constexpr std::string_view type_name(int) ENTT_NOEXCEPT {
  33879. constexpr auto value = stripped_type_name<Type>();
  33880. return value;
  33881. }
  33882. template<typename Type>
  33883. [[nodiscard]] static std::string_view type_name(char) ENTT_NOEXCEPT {
  33884. static const auto value = stripped_type_name<Type>();
  33885. return value;
  33886. }
  33887. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  33888. [[nodiscard]] static constexpr id_type type_hash(int) ENTT_NOEXCEPT {
  33889. constexpr auto stripped = stripped_type_name<Type>();
  33890. constexpr auto value = hashed_string::value(stripped.data(), stripped.size());
  33891. return value;
  33892. }
  33893. template<typename Type>
  33894. [[nodiscard]] static id_type type_hash(char) ENTT_NOEXCEPT {
  33895. static const auto value = [](const auto stripped) {
  33896. return hashed_string::value(stripped.data(), stripped.size());
  33897. }(stripped_type_name<Type>());
  33898. return value;
  33899. }
  33900. }
  33901. /**
  33902. * Internal details not to be documented.
  33903. * @endcond
  33904. */
  33905. /**
  33906. * @brief Type sequential identifier.
  33907. * @tparam Type Type for which to generate a sequential identifier.
  33908. */
  33909. template<typename Type, typename = void>
  33910. struct ENTT_API type_seq final {
  33911. /**
  33912. * @brief Returns the sequential identifier of a given type.
  33913. * @return The sequential identifier of a given type.
  33914. */
  33915. [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
  33916. static const id_type value = internal::type_seq::next();
  33917. return value;
  33918. }
  33919. /*! @copydoc value */
  33920. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  33921. };
  33922. /**
  33923. * @brief Type hash.
  33924. * @tparam Type Type for which to generate a hash value.
  33925. */
  33926. template<typename Type, typename = void>
  33927. struct type_hash final {
  33928. /**
  33929. * @brief Returns the numeric representation of a given type.
  33930. * @return The numeric representation of the given type.
  33931. */
  33932. #if defined ENTT_PRETTY_FUNCTION
  33933. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  33934. return internal::type_hash<Type>(0);
  33935. #else
  33936. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  33937. return type_seq<Type>::value();
  33938. #endif
  33939. }
  33940. /*! @copydoc value */
  33941. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  33942. };
  33943. /**
  33944. * @brief Type name.
  33945. * @tparam Type Type for which to generate a name.
  33946. */
  33947. template<typename Type, typename = void>
  33948. struct type_name final {
  33949. /**
  33950. * @brief Returns the name of a given type.
  33951. * @return The name of the given type.
  33952. */
  33953. [[nodiscard]] static constexpr std::string_view value() ENTT_NOEXCEPT {
  33954. return internal::type_name<Type>(0);
  33955. }
  33956. /*! @copydoc value */
  33957. [[nodiscard]] constexpr operator std::string_view() const ENTT_NOEXCEPT { return value(); }
  33958. };
  33959. /*! @brief Implementation specific information about a type. */
  33960. class type_info final {
  33961. template<typename>
  33962. friend type_info type_id() ENTT_NOEXCEPT;
  33963. type_info(id_type seq_v, id_type hash_v, std::string_view name_v) ENTT_NOEXCEPT
  33964. : seq_value{seq_v},
  33965. hash_value{hash_v},
  33966. name_value{name_v}
  33967. {}
  33968. public:
  33969. /*! @brief Default constructor. */
  33970. type_info() ENTT_NOEXCEPT
  33971. : type_info({}, {}, {})
  33972. {}
  33973. /*! @brief Default copy constructor. */
  33974. type_info(const type_info &) ENTT_NOEXCEPT = default;
  33975. /*! @brief Default move constructor. */
  33976. type_info(type_info &&) ENTT_NOEXCEPT = default;
  33977. /**
  33978. * @brief Default copy assignment operator.
  33979. * @return This type info object.
  33980. */
  33981. type_info & operator=(const type_info &) ENTT_NOEXCEPT = default;
  33982. /**
  33983. * @brief Default move assignment operator.
  33984. * @return This type info object.
  33985. */
  33986. type_info & operator=(type_info &&) ENTT_NOEXCEPT = default;
  33987. /**
  33988. * @brief Checks if a type info object is properly initialized.
  33989. * @return True if the object is properly initialized, false otherwise.
  33990. */
  33991. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  33992. return name_value.data() != nullptr;
  33993. }
  33994. /**
  33995. * @brief Type sequential identifier.
  33996. * @return Type sequential identifier.
  33997. */
  33998. [[nodiscard]] id_type seq() const ENTT_NOEXCEPT {
  33999. return seq_value;
  34000. }
  34001. /**
  34002. * @brief Type hash.
  34003. * @return Type hash.
  34004. */
  34005. [[nodiscard]] id_type hash() const ENTT_NOEXCEPT {
  34006. return hash_value;
  34007. }
  34008. /**
  34009. * @brief Type name.
  34010. * @return Type name.
  34011. */
  34012. [[nodiscard]] std::string_view name() const ENTT_NOEXCEPT {
  34013. return name_value;
  34014. }
  34015. /**
  34016. * @brief Compares the contents of two type info objects.
  34017. * @param other Object with which to compare.
  34018. * @return False if the two contents differ, true otherwise.
  34019. */
  34020. [[nodiscard]] bool operator==(const type_info &other) const ENTT_NOEXCEPT {
  34021. return hash_value == other.hash_value;
  34022. }
  34023. private:
  34024. id_type seq_value;
  34025. id_type hash_value;
  34026. std::string_view name_value;
  34027. };
  34028. /**
  34029. * @brief Compares the contents of two type info objects.
  34030. * @param lhs A type info object.
  34031. * @param rhs A type info object.
  34032. * @return True if the two contents differ, false otherwise.
  34033. */
  34034. [[nodiscard]] inline bool operator!=(const type_info &lhs, const type_info &rhs) ENTT_NOEXCEPT {
  34035. return !(lhs == rhs);
  34036. }
  34037. /**
  34038. * @brief Returns the type info object for a given type.
  34039. * @tparam Type Type for which to generate a type info object.
  34040. * @return The type info object for the given type.
  34041. */
  34042. template<typename Type>
  34043. [[nodiscard]] type_info type_id() ENTT_NOEXCEPT {
  34044. return type_info{
  34045. type_seq<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  34046. type_hash<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  34047. type_name<std::remove_cv_t<std::remove_reference_t<Type>>>::value()
  34048. };
  34049. }
  34050. }
  34051. #endif
  34052. // #include "../core/type_traits.hpp"
  34053. #ifndef ENTT_CORE_TYPE_TRAITS_HPP
  34054. #define ENTT_CORE_TYPE_TRAITS_HPP
  34055. #include <cstddef>
  34056. #include <iterator>
  34057. #include <type_traits>
  34058. #include <utility>
  34059. // #include "../config/config.h"
  34060. // #include "fwd.hpp"
  34061. namespace entt {
  34062. /**
  34063. * @brief Utility class to disambiguate overloaded functions.
  34064. * @tparam N Number of choices available.
  34065. */
  34066. template<std::size_t N>
  34067. struct choice_t
  34068. // Unfortunately, doxygen cannot parse such a construct.
  34069. /*! @cond TURN_OFF_DOXYGEN */
  34070. : choice_t<N-1>
  34071. /*! @endcond */
  34072. {};
  34073. /*! @copybrief choice_t */
  34074. template<>
  34075. struct choice_t<0> {};
  34076. /**
  34077. * @brief Variable template for the choice trick.
  34078. * @tparam N Number of choices available.
  34079. */
  34080. template<std::size_t N>
  34081. inline constexpr choice_t<N> choice{};
  34082. /**
  34083. * @brief Identity type trait.
  34084. *
  34085. * Useful to establish non-deduced contexts in template argument deduction
  34086. * (waiting for C++20) or to provide types through function arguments.
  34087. *
  34088. * @tparam Type A type.
  34089. */
  34090. template<typename Type>
  34091. struct type_identity {
  34092. /*! @brief Identity type. */
  34093. using type = Type;
  34094. };
  34095. /**
  34096. * @brief Helper type.
  34097. * @tparam Type A type.
  34098. */
  34099. template<typename Type>
  34100. using type_identity_t = typename type_identity<Type>::type;
  34101. /**
  34102. * @brief A type-only `sizeof` wrapper that returns 0 where `sizeof` complains.
  34103. * @tparam Type The type of which to return the size.
  34104. * @tparam The size of the type if `sizeof` accepts it, 0 otherwise.
  34105. */
  34106. template<typename Type, typename = void>
  34107. struct size_of: std::integral_constant<std::size_t, 0u> {};
  34108. /*! @copydoc size_of */
  34109. template<typename Type>
  34110. struct size_of<Type, std::void_t<decltype(sizeof(Type))>>
  34111. : std::integral_constant<std::size_t, sizeof(Type)>
  34112. {};
  34113. /**
  34114. * @brief Helper variable template.
  34115. * @tparam Type The type of which to return the size.
  34116. */
  34117. template<class Type>
  34118. inline constexpr std::size_t size_of_v = size_of<Type>::value;
  34119. /**
  34120. * @brief Using declaration to be used to _repeat_ the same type a number of
  34121. * times equal to the size of a given parameter pack.
  34122. * @tparam Type A type to repeat.
  34123. */
  34124. template<typename Type, typename>
  34125. using unpack_as_t = Type;
  34126. /**
  34127. * @brief Helper variable template to be used to _repeat_ the same value a
  34128. * number of times equal to the size of a given parameter pack.
  34129. * @tparam Value A value to repeat.
  34130. */
  34131. template<auto Value, typename>
  34132. inline constexpr auto unpack_as_v = Value;
  34133. /**
  34134. * @brief Wraps a static constant.
  34135. * @tparam Value A static constant.
  34136. */
  34137. template<auto Value>
  34138. using integral_constant = std::integral_constant<decltype(Value), Value>;
  34139. /**
  34140. * @brief Alias template to facilitate the creation of named values.
  34141. * @tparam Value A constant value at least convertible to `id_type`.
  34142. */
  34143. template<id_type Value>
  34144. using tag = integral_constant<Value>;
  34145. /**
  34146. * @brief A class to use to push around lists of types, nothing more.
  34147. * @tparam Type Types provided by the type list.
  34148. */
  34149. template<typename... Type>
  34150. struct type_list {
  34151. /*! @brief Type list type. */
  34152. using type = type_list;
  34153. /*! @brief Compile-time number of elements in the type list. */
  34154. static constexpr auto size = sizeof...(Type);
  34155. };
  34156. /*! @brief Primary template isn't defined on purpose. */
  34157. template<std::size_t, typename>
  34158. struct type_list_element;
  34159. /**
  34160. * @brief Provides compile-time indexed access to the types of a type list.
  34161. * @tparam Index Index of the type to return.
  34162. * @tparam Type First type provided by the type list.
  34163. * @tparam Other Other types provided by the type list.
  34164. */
  34165. template<std::size_t Index, typename Type, typename... Other>
  34166. struct type_list_element<Index, type_list<Type, Other...>>
  34167. : type_list_element<Index - 1u, type_list<Other...>>
  34168. {};
  34169. /**
  34170. * @brief Provides compile-time indexed access to the types of a type list.
  34171. * @tparam Type First type provided by the type list.
  34172. * @tparam Other Other types provided by the type list.
  34173. */
  34174. template<typename Type, typename... Other>
  34175. struct type_list_element<0u, type_list<Type, Other...>> {
  34176. /*! @brief Searched type. */
  34177. using type = Type;
  34178. };
  34179. /**
  34180. * @brief Helper type.
  34181. * @tparam Index Index of the type to return.
  34182. * @tparam List Type list to search into.
  34183. */
  34184. template<std::size_t Index, typename List>
  34185. using type_list_element_t = typename type_list_element<Index, List>::type;
  34186. /**
  34187. * @brief Concatenates multiple type lists.
  34188. * @tparam Type Types provided by the first type list.
  34189. * @tparam Other Types provided by the second type list.
  34190. * @return A type list composed by the types of both the type lists.
  34191. */
  34192. template<typename... Type, typename... Other>
  34193. constexpr type_list<Type..., Other...> operator+(type_list<Type...>, type_list<Other...>) { return {}; }
  34194. /*! @brief Primary template isn't defined on purpose. */
  34195. template<typename...>
  34196. struct type_list_cat;
  34197. /*! @brief Concatenates multiple type lists. */
  34198. template<>
  34199. struct type_list_cat<> {
  34200. /*! @brief A type list composed by the types of all the type lists. */
  34201. using type = type_list<>;
  34202. };
  34203. /**
  34204. * @brief Concatenates multiple type lists.
  34205. * @tparam Type Types provided by the first type list.
  34206. * @tparam Other Types provided by the second type list.
  34207. * @tparam List Other type lists, if any.
  34208. */
  34209. template<typename... Type, typename... Other, typename... List>
  34210. struct type_list_cat<type_list<Type...>, type_list<Other...>, List...> {
  34211. /*! @brief A type list composed by the types of all the type lists. */
  34212. using type = typename type_list_cat<type_list<Type..., Other...>, List...>::type;
  34213. };
  34214. /**
  34215. * @brief Concatenates multiple type lists.
  34216. * @tparam Type Types provided by the type list.
  34217. */
  34218. template<typename... Type>
  34219. struct type_list_cat<type_list<Type...>> {
  34220. /*! @brief A type list composed by the types of all the type lists. */
  34221. using type = type_list<Type...>;
  34222. };
  34223. /**
  34224. * @brief Helper type.
  34225. * @tparam List Type lists to concatenate.
  34226. */
  34227. template<typename... List>
  34228. using type_list_cat_t = typename type_list_cat<List...>::type;
  34229. /*! @brief Primary template isn't defined on purpose. */
  34230. template<typename>
  34231. struct type_list_unique;
  34232. /**
  34233. * @brief Removes duplicates types from a type list.
  34234. * @tparam Type One of the types provided by the given type list.
  34235. * @tparam Other The other types provided by the given type list.
  34236. */
  34237. template<typename Type, typename... Other>
  34238. struct type_list_unique<type_list<Type, Other...>> {
  34239. /*! @brief A type list without duplicate types. */
  34240. using type = std::conditional_t<
  34241. std::disjunction_v<std::is_same<Type, Other>...>,
  34242. typename type_list_unique<type_list<Other...>>::type,
  34243. type_list_cat_t<type_list<Type>, typename type_list_unique<type_list<Other...>>::type>
  34244. >;
  34245. };
  34246. /*! @brief Removes duplicates types from a type list. */
  34247. template<>
  34248. struct type_list_unique<type_list<>> {
  34249. /*! @brief A type list without duplicate types. */
  34250. using type = type_list<>;
  34251. };
  34252. /**
  34253. * @brief Helper type.
  34254. * @tparam Type A type list.
  34255. */
  34256. template<typename Type>
  34257. using type_list_unique_t = typename type_list_unique<Type>::type;
  34258. /**
  34259. * @brief Provides the member constant `value` to true if a type list contains a
  34260. * given type, false otherwise.
  34261. * @tparam List Type list.
  34262. * @tparam Type Type to look for.
  34263. */
  34264. template<typename List, typename Type>
  34265. struct type_list_contains;
  34266. /**
  34267. * @copybrief type_list_contains
  34268. * @tparam Type Types provided by the type list.
  34269. * @tparam Other Type to look for.
  34270. */
  34271. template<typename... Type, typename Other>
  34272. struct type_list_contains<type_list<Type...>, Other>: std::disjunction<std::is_same<Type, Other>...> {};
  34273. /**
  34274. * @brief Helper variable template.
  34275. * @tparam List Type list.
  34276. * @tparam Type Type to look for.
  34277. */
  34278. template<class List, typename Type>
  34279. inline constexpr bool type_list_contains_v = type_list_contains<List, Type>::value;
  34280. /*! @brief Primary template isn't defined on purpose. */
  34281. template<typename...>
  34282. struct type_list_diff;
  34283. /**
  34284. * @brief Computes the difference between two type lists.
  34285. * @tparam Type Types provided by the first type list.
  34286. * @tparam Other Types provided by the second type list.
  34287. */
  34288. template<typename... Type, typename... Other>
  34289. struct type_list_diff<type_list<Type...>, type_list<Other...>> {
  34290. /*! @brief A type list that is the difference between the two type lists. */
  34291. using type = type_list_cat_t<std::conditional_t<type_list_contains_v<type_list<Other...>, Type>, type_list<>, type_list<Type>>...>;
  34292. };
  34293. /**
  34294. * @brief Helper type.
  34295. * @tparam List Type lists between which to compute the difference.
  34296. */
  34297. template<typename... List>
  34298. using type_list_diff_t = typename type_list_diff<List...>::type;
  34299. /**
  34300. * @brief A class to use to push around lists of constant values, nothing more.
  34301. * @tparam Value Values provided by the value list.
  34302. */
  34303. template<auto... Value>
  34304. struct value_list {
  34305. /*! @brief Value list type. */
  34306. using type = value_list;
  34307. /*! @brief Compile-time number of elements in the value list. */
  34308. static constexpr auto size = sizeof...(Value);
  34309. };
  34310. /*! @brief Primary template isn't defined on purpose. */
  34311. template<std::size_t, typename>
  34312. struct value_list_element;
  34313. /**
  34314. * @brief Provides compile-time indexed access to the values of a value list.
  34315. * @tparam Index Index of the value to return.
  34316. * @tparam Value First value provided by the value list.
  34317. * @tparam Other Other values provided by the value list.
  34318. */
  34319. template<std::size_t Index, auto Value, auto... Other>
  34320. struct value_list_element<Index, value_list<Value, Other...>>
  34321. : value_list_element<Index - 1u, value_list<Other...>>
  34322. {};
  34323. /**
  34324. * @brief Provides compile-time indexed access to the types of a type list.
  34325. * @tparam Value First value provided by the value list.
  34326. * @tparam Other Other values provided by the value list.
  34327. */
  34328. template<auto Value, auto... Other>
  34329. struct value_list_element<0u, value_list<Value, Other...>> {
  34330. /*! @brief Searched value. */
  34331. static constexpr auto value = Value;
  34332. };
  34333. /**
  34334. * @brief Helper type.
  34335. * @tparam Index Index of the value to return.
  34336. * @tparam List Value list to search into.
  34337. */
  34338. template<std::size_t Index, typename List>
  34339. inline constexpr auto value_list_element_v = value_list_element<Index, List>::value;
  34340. /**
  34341. * @brief Concatenates multiple value lists.
  34342. * @tparam Value Values provided by the first value list.
  34343. * @tparam Other Values provided by the second value list.
  34344. * @return A value list composed by the values of both the value lists.
  34345. */
  34346. template<auto... Value, auto... Other>
  34347. constexpr value_list<Value..., Other...> operator+(value_list<Value...>, value_list<Other...>) { return {}; }
  34348. /*! @brief Primary template isn't defined on purpose. */
  34349. template<typename...>
  34350. struct value_list_cat;
  34351. /*! @brief Concatenates multiple value lists. */
  34352. template<>
  34353. struct value_list_cat<> {
  34354. /*! @brief A value list composed by the values of all the value lists. */
  34355. using type = value_list<>;
  34356. };
  34357. /**
  34358. * @brief Concatenates multiple value lists.
  34359. * @tparam Value Values provided by the first value list.
  34360. * @tparam Other Values provided by the second value list.
  34361. * @tparam List Other value lists, if any.
  34362. */
  34363. template<auto... Value, auto... Other, typename... List>
  34364. struct value_list_cat<value_list<Value...>, value_list<Other...>, List...> {
  34365. /*! @brief A value list composed by the values of all the value lists. */
  34366. using type = typename value_list_cat<value_list<Value..., Other...>, List...>::type;
  34367. };
  34368. /**
  34369. * @brief Concatenates multiple value lists.
  34370. * @tparam Value Values provided by the value list.
  34371. */
  34372. template<auto... Value>
  34373. struct value_list_cat<value_list<Value...>> {
  34374. /*! @brief A value list composed by the values of all the value lists. */
  34375. using type = value_list<Value...>;
  34376. };
  34377. /**
  34378. * @brief Helper type.
  34379. * @tparam List Value lists to concatenate.
  34380. */
  34381. template<typename... List>
  34382. using value_list_cat_t = typename value_list_cat<List...>::type;
  34383. /**
  34384. * @cond TURN_OFF_DOXYGEN
  34385. * Internal details not to be documented.
  34386. */
  34387. namespace internal {
  34388. template<typename>
  34389. [[nodiscard]] constexpr bool is_equality_comparable(...) { return false; }
  34390. template<typename Type>
  34391. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<0>)
  34392. -> decltype(std::declval<Type>() == std::declval<Type>()) { return true; }
  34393. template<typename Type>
  34394. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<1>)
  34395. -> decltype(std::declval<typename Type::value_type>(), std::declval<Type>() == std::declval<Type>()) {
  34396. if constexpr(std::is_same_v<typename Type::value_type, Type>) {
  34397. return is_equality_comparable<Type>(choice<0>);
  34398. } else {
  34399. return is_equality_comparable<typename Type::value_type>(choice<2>);
  34400. }
  34401. }
  34402. template<typename Type>
  34403. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<2>)
  34404. -> decltype(std::declval<typename Type::mapped_type>(), std::declval<Type>() == std::declval<Type>()) {
  34405. return is_equality_comparable<typename Type::key_type>(choice<2>) && is_equality_comparable<typename Type::mapped_type>(choice<2>);
  34406. }
  34407. }
  34408. /**
  34409. * Internal details not to be documented.
  34410. * @endcond
  34411. */
  34412. /**
  34413. * @brief Provides the member constant `value` to true if a given type is
  34414. * equality comparable, false otherwise.
  34415. * @tparam Type The type to test.
  34416. */
  34417. template<typename Type, typename = void>
  34418. struct is_equality_comparable: std::bool_constant<internal::is_equality_comparable<Type>(choice<2>)> {};
  34419. /**
  34420. * @brief Helper variable template.
  34421. * @tparam Type The type to test.
  34422. */
  34423. template<class Type>
  34424. inline constexpr bool is_equality_comparable_v = is_equality_comparable<Type>::value;
  34425. /*! @brief Same as std::is_invocable, but with tuples. */
  34426. template<typename, typename>
  34427. struct is_applicable: std::false_type {};
  34428. /**
  34429. * @copybrief is_applicable
  34430. * @tparam Func A valid function type.
  34431. * @tparam Tuple Tuple-like type.
  34432. * @tparam Args The list of arguments to use to probe the function type.
  34433. */
  34434. template<typename Func, template<typename...> class Tuple, typename... Args>
  34435. struct is_applicable<Func, Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  34436. /**
  34437. * @copybrief is_applicable
  34438. * @tparam Func A valid function type.
  34439. * @tparam Tuple Tuple-like type.
  34440. * @tparam Args The list of arguments to use to probe the function type.
  34441. */
  34442. template<typename Func, template<typename...> class Tuple, typename... Args>
  34443. struct is_applicable<Func, const Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  34444. /**
  34445. * @brief Helper variable template.
  34446. * @tparam Func A valid function type.
  34447. * @tparam Args The list of arguments to use to probe the function type.
  34448. */
  34449. template<typename Func, typename Args>
  34450. inline constexpr bool is_applicable_v = is_applicable<Func, Args>::value;
  34451. /*! @brief Same as std::is_invocable_r, but with tuples for arguments. */
  34452. template<typename, typename, typename>
  34453. struct is_applicable_r: std::false_type {};
  34454. /**
  34455. * @copybrief is_applicable_r
  34456. * @tparam Ret The type to which the return type of the function should be
  34457. * convertible.
  34458. * @tparam Func A valid function type.
  34459. * @tparam Args The list of arguments to use to probe the function type.
  34460. */
  34461. template<typename Ret, typename Func, typename... Args>
  34462. struct is_applicable_r<Ret, Func, std::tuple<Args...>>: std::is_invocable_r<Ret, Func, Args...> {};
  34463. /**
  34464. * @brief Helper variable template.
  34465. * @tparam Ret The type to which the return type of the function should be
  34466. * convertible.
  34467. * @tparam Func A valid function type.
  34468. * @tparam Args The list of arguments to use to probe the function type.
  34469. */
  34470. template<typename Ret, typename Func, typename Args>
  34471. inline constexpr bool is_applicable_r_v = is_applicable_r<Ret, Func, Args>::value;
  34472. /**
  34473. * @brief Provides the member constant `value` to true if a given type is
  34474. * complete, false otherwise.
  34475. * @tparam Type The type to test.
  34476. */
  34477. template<typename Type, typename = void>
  34478. struct is_complete: std::false_type {};
  34479. /*! @copydoc is_complete */
  34480. template<typename Type>
  34481. struct is_complete<Type, std::void_t<decltype(sizeof(Type))>>: std::true_type {};
  34482. /**
  34483. * @brief Helper variable template.
  34484. * @tparam Type The type to test.
  34485. */
  34486. template<typename Type>
  34487. inline constexpr bool is_complete_v = is_complete<Type>::value;
  34488. /**
  34489. * @brief Provides the member constant `value` to true if a given type is an
  34490. * iterator, false otherwise.
  34491. * @tparam Type The type to test.
  34492. */
  34493. template<typename Type, typename = void>
  34494. struct is_iterator: std::false_type {};
  34495. /*! @copydoc is_iterator */
  34496. template<typename Type>
  34497. struct is_iterator<Type, std::void_t<typename std::iterator_traits<Type>::iterator_category>>
  34498. : std::true_type
  34499. {};
  34500. /**
  34501. * @brief Helper variable template.
  34502. * @tparam Type The type to test.
  34503. */
  34504. template<typename Type>
  34505. inline constexpr bool is_iterator_v = is_iterator<Type>::value;
  34506. /**
  34507. * @brief Provides the member constant `value` to true if a given type is of the
  34508. * required iterator type, false otherwise.
  34509. * @tparam Type The type to test.
  34510. * @tparam It Required iterator type.
  34511. */
  34512. template<typename Type, typename It, typename = void>
  34513. struct is_iterator_type: std::false_type {};
  34514. /*! @copydoc is_iterator_type */
  34515. template<typename Type, typename It>
  34516. struct is_iterator_type<Type, It, std::enable_if_t<is_iterator_v<Type> && std::is_same_v<Type, It>>>
  34517. : std::true_type
  34518. {};
  34519. /*! @copydoc is_iterator_type */
  34520. template<typename Type, typename It>
  34521. struct is_iterator_type<Type, It, std::enable_if_t<!std::is_same_v<Type, It>, std::void_t<typename It::iterator_type>>>
  34522. : is_iterator_type<Type, typename It::iterator_type>
  34523. {};
  34524. /**
  34525. * @brief Helper variable template.
  34526. * @tparam Type The type to test.
  34527. * @tparam It Required iterator type.
  34528. */
  34529. template<typename Type, typename It>
  34530. inline constexpr bool is_iterator_type_v = is_iterator_type<Type, It>::value;
  34531. /**
  34532. * @brief Transcribes the constness of a type to another type.
  34533. * @tparam To The type to which to transcribe the constness.
  34534. * @tparam From The type from which to transcribe the constness.
  34535. */
  34536. template<typename To, typename From>
  34537. struct constness_as {
  34538. /*! @brief The type resulting from the transcription of the constness. */
  34539. using type = std::remove_const_t<To>;
  34540. };
  34541. /*! @copydoc constness_as */
  34542. template<typename To, typename From>
  34543. struct constness_as<To, const From> {
  34544. /*! @brief The type resulting from the transcription of the constness. */
  34545. using type = std::add_const_t<To>;
  34546. };
  34547. /**
  34548. * @brief Alias template to facilitate the transcription of the constness.
  34549. * @tparam To The type to which to transcribe the constness.
  34550. * @tparam From The type from which to transcribe the constness.
  34551. */
  34552. template<typename To, typename From>
  34553. using constness_as_t = typename constness_as<To, From>::type;
  34554. /**
  34555. * @brief Extracts the class of a non-static member object or function.
  34556. * @tparam Member A pointer to a non-static member object or function.
  34557. */
  34558. template<typename Member>
  34559. class member_class {
  34560. static_assert(std::is_member_pointer_v<Member>, "Invalid pointer type to non-static member object or function");
  34561. template<typename Class, typename Ret, typename... Args>
  34562. static Class * clazz(Ret(Class:: *)(Args...));
  34563. template<typename Class, typename Ret, typename... Args>
  34564. static Class * clazz(Ret(Class:: *)(Args...) const);
  34565. template<typename Class, typename Type>
  34566. static Class * clazz(Type Class:: *);
  34567. public:
  34568. /*! @brief The class of the given non-static member object or function. */
  34569. using type = std::remove_pointer_t<decltype(clazz(std::declval<Member>()))>;
  34570. };
  34571. /**
  34572. * @brief Helper type.
  34573. * @tparam Member A pointer to a non-static member object or function.
  34574. */
  34575. template<typename Member>
  34576. using member_class_t = typename member_class<Member>::type;
  34577. }
  34578. #endif
  34579. // #include "fwd.hpp"
  34580. #ifndef ENTT_POLY_FWD_HPP
  34581. #define ENTT_POLY_FWD_HPP
  34582. #include <type_traits>
  34583. namespace entt {
  34584. template<typename, std::size_t Len, std::size_t = alignof(typename std::aligned_storage_t<Len + !Len>)>
  34585. class basic_poly;
  34586. /**
  34587. * @brief Alias declaration for the most common use case.
  34588. * @tparam Concept Concept descriptor.
  34589. */
  34590. template<typename Concept>
  34591. using poly = basic_poly<Concept, sizeof(double[2])>;
  34592. }
  34593. #endif
  34594. namespace entt {
  34595. /*! @brief Inspector class used to infer the type of the virtual table. */
  34596. struct poly_inspector {
  34597. /**
  34598. * @brief Generic conversion operator (definition only).
  34599. * @tparam Type Type to which conversion is requested.
  34600. */
  34601. template <class Type>
  34602. operator Type &&() const;
  34603. /**
  34604. * @brief Dummy invocation function (definition only).
  34605. * @tparam Member Index of the function to invoke.
  34606. * @tparam Args Types of arguments to pass to the function.
  34607. * @param args The arguments to pass to the function.
  34608. * @return A poly inspector convertible to any type.
  34609. */
  34610. template<auto Member, typename... Args>
  34611. poly_inspector invoke(Args &&... args) const;
  34612. /*! @copydoc invoke */
  34613. template<auto Member, typename... Args>
  34614. poly_inspector invoke(Args &&... args);
  34615. };
  34616. /**
  34617. * @brief Static virtual table factory.
  34618. * @tparam Concept Concept descriptor.
  34619. * @tparam Len Size of the storage reserved for the small buffer optimization.
  34620. * @tparam Align Alignment requirement.
  34621. */
  34622. template<typename Concept, std::size_t Len, std::size_t Align>
  34623. class poly_vtable {
  34624. using inspector = typename Concept::template type<poly_inspector>;
  34625. template<typename Ret, typename... Args>
  34626. static auto vtable_entry(Ret(*)(inspector &, Args...)) -> Ret(*)(basic_any<Len, Align> &, Args...);
  34627. template<typename Ret, typename... Args>
  34628. static auto vtable_entry(Ret(*)(const inspector &, Args...)) -> Ret(*)(const basic_any<Len, Align> &, Args...);
  34629. template<typename Ret, typename... Args>
  34630. static auto vtable_entry(Ret(*)(Args...)) -> Ret(*)(const basic_any<Len, Align> &, Args...);
  34631. template<typename Ret, typename... Args>
  34632. static auto vtable_entry(Ret(inspector:: *)(Args...)) -> Ret(*)(basic_any<Len, Align> &, Args...);
  34633. template<typename Ret, typename... Args>
  34634. static auto vtable_entry(Ret(inspector:: *)(Args...) const) -> Ret(*)(const basic_any<Len, Align> &, Args...);
  34635. template<auto... Candidate>
  34636. static auto make_vtable(value_list<Candidate...>)
  34637. -> decltype(std::make_tuple(vtable_entry(Candidate)...));
  34638. template<typename... Func>
  34639. [[nodiscard]] static constexpr auto make_vtable(type_list<Func...>) {
  34640. if constexpr(sizeof...(Func) == 0) {
  34641. return decltype(make_vtable(typename Concept::template impl<inspector>{})){};
  34642. } else if constexpr((std::is_function_v<Func> && ...)) {
  34643. return decltype(std::make_tuple(vtable_entry(std::declval<Func inspector:: *>())...)){};
  34644. }
  34645. }
  34646. template<typename Type, auto Candidate, typename Ret, typename Any, typename... Args>
  34647. static void fill_vtable_entry(Ret(* &entry)(Any &, Args...)) {
  34648. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Args...>) {
  34649. entry = +[](Any &, Args... args) -> Ret {
  34650. return std::invoke(Candidate, std::forward<Args>(args)...);
  34651. };
  34652. } else {
  34653. entry = +[](Any &instance, Args... args) -> Ret {
  34654. return static_cast<Ret>(std::invoke(Candidate, any_cast<constness_as_t<Type, Any> &>(instance), std::forward<Args>(args)...));
  34655. };
  34656. }
  34657. }
  34658. template<typename Type, auto... Index>
  34659. [[nodiscard]] static auto fill_vtable(std::index_sequence<Index...>) {
  34660. type impl{};
  34661. (fill_vtable_entry<Type, value_list_element_v<Index, typename Concept::template impl<Type>>>(std::get<Index>(impl)), ...);
  34662. return impl;
  34663. }
  34664. public:
  34665. /*! @brief Virtual table type. */
  34666. using type = decltype(make_vtable(Concept{}));
  34667. /**
  34668. * @brief Returns a static virtual table for a specific concept and type.
  34669. * @tparam Type The type for which to generate the virtual table.
  34670. * @return A static virtual table for the given concept and type.
  34671. */
  34672. template<typename Type>
  34673. [[nodiscard]] static const auto * instance() {
  34674. static_assert(std::is_same_v<Type, std::decay_t<Type>>, "Type differs from its decayed form");
  34675. static const auto vtable = fill_vtable<Type>(std::make_index_sequence<Concept::template impl<Type>::size>{});
  34676. return &vtable;
  34677. }
  34678. };
  34679. /**
  34680. * @brief Poly base class used to inject functionalities into concepts.
  34681. * @tparam Poly The outermost poly class.
  34682. */
  34683. template<typename Poly>
  34684. struct poly_base {
  34685. /**
  34686. * @brief Invokes a function from the static virtual table.
  34687. * @tparam Member Index of the function to invoke.
  34688. * @tparam Args Types of arguments to pass to the function.
  34689. * @param self A reference to the poly object that made the call.
  34690. * @param args The arguments to pass to the function.
  34691. * @return The return value of the invoked function, if any.
  34692. */
  34693. template<auto Member, typename... Args>
  34694. [[nodiscard]] decltype(auto) invoke(const poly_base &self, Args &&... args) const {
  34695. const auto &poly = static_cast<const Poly &>(self);
  34696. return std::get<Member>(*poly.vtable)(poly.storage, std::forward<Args>(args)...);
  34697. }
  34698. /*! @copydoc invoke */
  34699. template<auto Member, typename... Args>
  34700. [[nodiscard]] decltype(auto) invoke(poly_base &self, Args &&... args) {
  34701. auto &poly = static_cast<Poly &>(self);
  34702. return std::get<Member>(*poly.vtable)(poly.storage, std::forward<Args>(args)...);
  34703. }
  34704. };
  34705. /**
  34706. * @brief Shortcut for calling `poly_base<Type>::invoke`.
  34707. * @tparam Member Index of the function to invoke.
  34708. * @tparam Poly A fully defined poly object.
  34709. * @tparam Args Types of arguments to pass to the function.
  34710. * @param self A reference to the poly object that made the call.
  34711. * @param args The arguments to pass to the function.
  34712. * @return The return value of the invoked function, if any.
  34713. */
  34714. template<auto Member, typename Poly, typename... Args>
  34715. decltype(auto) poly_call(Poly &&self, Args &&... args) {
  34716. return std::forward<Poly>(self).template invoke<Member>(self, std::forward<Args>(args)...);
  34717. }
  34718. /**
  34719. * @brief Static polymorphism made simple and within everyone's reach.
  34720. *
  34721. * Static polymorphism is a very powerful tool in C++, albeit sometimes
  34722. * cumbersome to obtain.<br/>
  34723. * This class aims to make it simple and easy to use.
  34724. *
  34725. * @note
  34726. * Both deduced and defined static virtual tables are supported.<br/>
  34727. * Moreover, the `poly` class template also works with unmanaged objects.
  34728. *
  34729. * @tparam Concept Concept descriptor.
  34730. * @tparam Len Size of the storage reserved for the small buffer optimization.
  34731. * @tparam Align Optional alignment requirement.
  34732. */
  34733. template<typename Concept, std::size_t Len, std::size_t Align>
  34734. class basic_poly: private Concept::template type<poly_base<basic_poly<Concept, Len, Align>>> {
  34735. /*! @brief A poly base is allowed to snoop into a poly object. */
  34736. friend struct poly_base<basic_poly>;
  34737. using vtable_type = typename poly_vtable<Concept, Len, Align>::type;
  34738. public:
  34739. /*! @brief Concept type. */
  34740. using concept_type = typename Concept::template type<poly_base<basic_poly>>;
  34741. /*! @brief Default constructor. */
  34742. basic_poly() ENTT_NOEXCEPT
  34743. : storage{},
  34744. vtable{}
  34745. {}
  34746. /**
  34747. * @brief Constructs a poly by directly initializing the new object.
  34748. * @tparam Type Type of object to use to initialize the poly.
  34749. * @tparam Args Types of arguments to use to construct the new instance.
  34750. * @param args Parameters to use to construct the instance.
  34751. */
  34752. template<typename Type, typename... Args>
  34753. explicit basic_poly(std::in_place_type_t<Type>, Args &&... args)
  34754. : storage{std::in_place_type<Type>, std::forward<Args>(args)...},
  34755. vtable{poly_vtable<Concept, Len, Align>::template instance<std::remove_const_t<std::remove_reference_t<Type>>>()}
  34756. {}
  34757. /**
  34758. * @brief Constructs a poly from a given value.
  34759. * @tparam Type Type of object to use to initialize the poly.
  34760. * @param value An instance of an object to use to initialize the poly.
  34761. */
  34762. template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::remove_cv_t<std::remove_reference_t<Type>>, basic_poly>>>
  34763. basic_poly(Type &&value) ENTT_NOEXCEPT
  34764. : basic_poly{std::in_place_type<std::remove_cv_t<std::remove_reference_t<Type>>>, std::forward<Type>(value)}
  34765. {}
  34766. /**
  34767. * @brief Copy constructor.
  34768. * @param other The instance to copy from.
  34769. */
  34770. basic_poly(const basic_poly &other) = default;
  34771. /**
  34772. * @brief Move constructor.
  34773. * @param other The instance to move from.
  34774. */
  34775. basic_poly(basic_poly &&other) ENTT_NOEXCEPT
  34776. : basic_poly{}
  34777. {
  34778. swap(*this, other);
  34779. }
  34780. /**
  34781. * @brief Assignment operator.
  34782. * @param other The instance to assign from.
  34783. * @return This poly object.
  34784. */
  34785. basic_poly & operator=(basic_poly other) {
  34786. swap(other, *this);
  34787. return *this;
  34788. }
  34789. /**
  34790. * @brief Returns the type of the contained object.
  34791. * @return The type of the contained object, if any.
  34792. */
  34793. [[nodiscard]] type_info type() const ENTT_NOEXCEPT {
  34794. return storage.type();
  34795. }
  34796. /**
  34797. * @brief Returns an opaque pointer to the contained instance.
  34798. * @return An opaque pointer the contained instance, if any.
  34799. */
  34800. [[nodiscard]] const void * data() const ENTT_NOEXCEPT {
  34801. return storage.data();
  34802. }
  34803. /*! @copydoc data */
  34804. [[nodiscard]] void * data() ENTT_NOEXCEPT {
  34805. return storage.data();
  34806. }
  34807. /**
  34808. * @brief Replaces the contained object by creating a new instance directly.
  34809. * @tparam Type Type of object to use to initialize the poly.
  34810. * @tparam Args Types of arguments to use to construct the new instance.
  34811. * @param args Parameters to use to construct the instance.
  34812. */
  34813. template<typename Type, typename... Args>
  34814. void emplace(Args &&... args) {
  34815. *this = basic_poly{std::in_place_type<Type>, std::forward<Args>(args)...};
  34816. }
  34817. /*! @brief Destroys contained object */
  34818. void reset() {
  34819. *this = basic_poly{};
  34820. }
  34821. /**
  34822. * @brief Returns false if a poly is empty, true otherwise.
  34823. * @return False if the poly is empty, true otherwise.
  34824. */
  34825. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  34826. return !(vtable == nullptr);
  34827. }
  34828. /**
  34829. * @brief Returns a pointer to the underlying concept.
  34830. * @return A pointer to the underlying concept.
  34831. */
  34832. [[nodiscard]] concept_type * operator->() ENTT_NOEXCEPT {
  34833. return this;
  34834. }
  34835. /*! @copydoc operator-> */
  34836. [[nodiscard]] const concept_type * operator->() const ENTT_NOEXCEPT {
  34837. return this;
  34838. }
  34839. /**
  34840. * @brief Swaps two poly objects.
  34841. * @param lhs A valid poly object.
  34842. * @param rhs A valid poly object.
  34843. */
  34844. friend void swap(basic_poly &lhs, basic_poly &rhs) {
  34845. using std::swap;
  34846. swap(lhs.storage, rhs.storage);
  34847. swap(lhs.vtable, rhs.vtable);
  34848. }
  34849. /**
  34850. * @brief Aliasing constructor.
  34851. * @return A poly that shares a reference to an unmanaged object.
  34852. */
  34853. [[nodiscard]] basic_poly as_ref() ENTT_NOEXCEPT {
  34854. basic_poly ref = std::as_const(*this).as_ref();
  34855. ref.storage = storage.as_ref();
  34856. return ref;
  34857. }
  34858. /*! @copydoc as_ref */
  34859. [[nodiscard]] basic_poly as_ref() const ENTT_NOEXCEPT {
  34860. basic_poly ref{};
  34861. ref.storage = storage.as_ref();
  34862. ref.vtable = vtable;
  34863. return ref;
  34864. }
  34865. private:
  34866. basic_any<Len, Align> storage;
  34867. const vtable_type *vtable;
  34868. };
  34869. }
  34870. #endif
  34871. // #include "process/process.hpp"
  34872. #ifndef ENTT_PROCESS_PROCESS_HPP
  34873. #define ENTT_PROCESS_PROCESS_HPP
  34874. #include <utility>
  34875. #include <type_traits>
  34876. // #include "../config/config.h"
  34877. #ifndef ENTT_CONFIG_CONFIG_H
  34878. #define ENTT_CONFIG_CONFIG_H
  34879. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  34880. # define ENTT_NOEXCEPT noexcept
  34881. # define ENTT_THROW throw
  34882. # define ENTT_TRY try
  34883. # define ENTT_CATCH catch(...)
  34884. #else
  34885. # define ENTT_NOEXCEPT
  34886. # define ENTT_THROW
  34887. # define ENTT_TRY if(true)
  34888. # define ENTT_CATCH if(false)
  34889. #endif
  34890. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  34891. # include <new>
  34892. # define ENTT_LAUNDER(expr) std::launder(expr)
  34893. #else
  34894. # define ENTT_LAUNDER(expr) expr
  34895. #endif
  34896. #ifndef ENTT_USE_ATOMIC
  34897. # define ENTT_MAYBE_ATOMIC(Type) Type
  34898. #else
  34899. # include <atomic>
  34900. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  34901. #endif
  34902. #ifndef ENTT_ID_TYPE
  34903. # include <cstdint>
  34904. # define ENTT_ID_TYPE std::uint32_t
  34905. #endif
  34906. #ifdef ENTT_SPARSE_PAGE
  34907. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  34908. #else
  34909. # define ENTT_SPARSE_PAGE 4096
  34910. #endif
  34911. #ifdef ENTT_PACKED_PAGE
  34912. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  34913. #else
  34914. # define ENTT_PACKED_PAGE 1024
  34915. #endif
  34916. #ifdef ENTT_DISABLE_ASSERT
  34917. # undef ENTT_ASSERT
  34918. # define ENTT_ASSERT(...) (void(0))
  34919. #elif !defined ENTT_ASSERT
  34920. # include <cassert>
  34921. # define ENTT_ASSERT(condition, ...) assert(condition)
  34922. #endif
  34923. #ifdef ENTT_NO_ETO
  34924. # include <type_traits>
  34925. # define ENTT_IGNORE_IF_EMPTY std::false_type
  34926. #else
  34927. # include <type_traits>
  34928. # define ENTT_IGNORE_IF_EMPTY std::true_type
  34929. #endif
  34930. #ifndef ENTT_STANDARD_CPP
  34931. # if defined __clang__ || defined __GNUC__
  34932. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  34933. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  34934. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  34935. # elif defined _MSC_VER
  34936. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  34937. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  34938. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  34939. # endif
  34940. #endif
  34941. #endif
  34942. namespace entt {
  34943. /**
  34944. * @brief Base class for processes.
  34945. *
  34946. * This class stays true to the CRTP idiom. Derived classes must specify what's
  34947. * the intended type for elapsed times.<br/>
  34948. * A process should expose publicly the following member functions whether
  34949. * required:
  34950. *
  34951. * * @code{.cpp}
  34952. * void update(Delta, void *);
  34953. * @endcode
  34954. *
  34955. * It's invoked once per tick until a process is explicitly aborted or it
  34956. * terminates either with or without errors. Even though it's not mandatory to
  34957. * declare this member function, as a rule of thumb each process should at
  34958. * least define it to work properly. The `void *` parameter is an opaque
  34959. * pointer to user data (if any) forwarded directly to the process during an
  34960. * update.
  34961. *
  34962. * * @code{.cpp}
  34963. * void init();
  34964. * @endcode
  34965. *
  34966. * It's invoked when the process joins the running queue of a scheduler. This
  34967. * happens as soon as it's attached to the scheduler if the process is a top
  34968. * level one, otherwise when it replaces its parent if the process is a
  34969. * continuation.
  34970. *
  34971. * * @code{.cpp}
  34972. * void succeeded();
  34973. * @endcode
  34974. *
  34975. * It's invoked in case of success, immediately after an update and during the
  34976. * same tick.
  34977. *
  34978. * * @code{.cpp}
  34979. * void failed();
  34980. * @endcode
  34981. *
  34982. * It's invoked in case of errors, immediately after an update and during the
  34983. * same tick.
  34984. *
  34985. * * @code{.cpp}
  34986. * void aborted();
  34987. * @endcode
  34988. *
  34989. * It's invoked only if a process is explicitly aborted. There is no guarantee
  34990. * that it executes in the same tick, this depends solely on whether the
  34991. * process is aborted immediately or not.
  34992. *
  34993. * Derived classes can change the internal state of a process by invoking the
  34994. * `succeed` and `fail` protected member functions and even pause or unpause the
  34995. * process itself.
  34996. *
  34997. * @sa scheduler
  34998. *
  34999. * @tparam Derived Actual type of process that extends the class template.
  35000. * @tparam Delta Type to use to provide elapsed time.
  35001. */
  35002. template<typename Derived, typename Delta>
  35003. class process {
  35004. enum class state: unsigned int {
  35005. UNINITIALIZED = 0,
  35006. RUNNING,
  35007. PAUSED,
  35008. SUCCEEDED,
  35009. FAILED,
  35010. ABORTED,
  35011. FINISHED,
  35012. REJECTED
  35013. };
  35014. template<typename Target = Derived>
  35015. auto next(std::integral_constant<state, state::UNINITIALIZED>)
  35016. -> decltype(std::declval<Target>().init(), void()) {
  35017. static_cast<Target *>(this)->init();
  35018. }
  35019. template<typename Target = Derived>
  35020. auto next(std::integral_constant<state, state::RUNNING>, Delta delta, void *data)
  35021. -> decltype(std::declval<Target>().update(delta, data), void()) {
  35022. static_cast<Target *>(this)->update(delta, data);
  35023. }
  35024. template<typename Target = Derived>
  35025. auto next(std::integral_constant<state, state::SUCCEEDED>)
  35026. -> decltype(std::declval<Target>().succeeded(), void()) {
  35027. static_cast<Target *>(this)->succeeded();
  35028. }
  35029. template<typename Target = Derived>
  35030. auto next(std::integral_constant<state, state::FAILED>)
  35031. -> decltype(std::declval<Target>().failed(), void()) {
  35032. static_cast<Target *>(this)->failed();
  35033. }
  35034. template<typename Target = Derived>
  35035. auto next(std::integral_constant<state, state::ABORTED>)
  35036. -> decltype(std::declval<Target>().aborted(), void()) {
  35037. static_cast<Target *>(this)->aborted();
  35038. }
  35039. void next(...) const ENTT_NOEXCEPT {}
  35040. protected:
  35041. /**
  35042. * @brief Terminates a process with success if it's still alive.
  35043. *
  35044. * The function is idempotent and it does nothing if the process isn't
  35045. * alive.
  35046. */
  35047. void succeed() ENTT_NOEXCEPT {
  35048. if(alive()) {
  35049. current = state::SUCCEEDED;
  35050. }
  35051. }
  35052. /**
  35053. * @brief Terminates a process with errors if it's still alive.
  35054. *
  35055. * The function is idempotent and it does nothing if the process isn't
  35056. * alive.
  35057. */
  35058. void fail() ENTT_NOEXCEPT {
  35059. if(alive()) {
  35060. current = state::FAILED;
  35061. }
  35062. }
  35063. /**
  35064. * @brief Stops a process if it's in a running state.
  35065. *
  35066. * The function is idempotent and it does nothing if the process isn't
  35067. * running.
  35068. */
  35069. void pause() ENTT_NOEXCEPT {
  35070. if(current == state::RUNNING) {
  35071. current = state::PAUSED;
  35072. }
  35073. }
  35074. /**
  35075. * @brief Restarts a process if it's paused.
  35076. *
  35077. * The function is idempotent and it does nothing if the process isn't
  35078. * paused.
  35079. */
  35080. void unpause() ENTT_NOEXCEPT {
  35081. if(current == state::PAUSED) {
  35082. current = state::RUNNING;
  35083. }
  35084. }
  35085. public:
  35086. /*! @brief Type used to provide elapsed time. */
  35087. using delta_type = Delta;
  35088. /*! @brief Default destructor. */
  35089. virtual ~process() {
  35090. static_assert(std::is_base_of_v<process, Derived>, "Incorrect use of the class template");
  35091. }
  35092. /**
  35093. * @brief Aborts a process if it's still alive.
  35094. *
  35095. * The function is idempotent and it does nothing if the process isn't
  35096. * alive.
  35097. *
  35098. * @param immediately Requests an immediate operation.
  35099. */
  35100. void abort(const bool immediately = false) {
  35101. if(alive()) {
  35102. current = state::ABORTED;
  35103. if(immediately) {
  35104. tick({});
  35105. }
  35106. }
  35107. }
  35108. /**
  35109. * @brief Returns true if a process is either running or paused.
  35110. * @return True if the process is still alive, false otherwise.
  35111. */
  35112. [[nodiscard]] bool alive() const ENTT_NOEXCEPT {
  35113. return current == state::RUNNING || current == state::PAUSED;
  35114. }
  35115. /**
  35116. * @brief Returns true if a process is already terminated.
  35117. * @return True if the process is terminated, false otherwise.
  35118. */
  35119. [[nodiscard]] bool finished() const ENTT_NOEXCEPT {
  35120. return current == state::FINISHED;
  35121. }
  35122. /**
  35123. * @brief Returns true if a process is currently paused.
  35124. * @return True if the process is paused, false otherwise.
  35125. */
  35126. [[nodiscard]] bool paused() const ENTT_NOEXCEPT {
  35127. return current == state::PAUSED;
  35128. }
  35129. /**
  35130. * @brief Returns true if a process terminated with errors.
  35131. * @return True if the process terminated with errors, false otherwise.
  35132. */
  35133. [[nodiscard]] bool rejected() const ENTT_NOEXCEPT {
  35134. return current == state::REJECTED;
  35135. }
  35136. /**
  35137. * @brief Updates a process and its internal state if required.
  35138. * @param delta Elapsed time.
  35139. * @param data Optional data.
  35140. */
  35141. void tick(const Delta delta, void *data = nullptr) {
  35142. switch (current) {
  35143. case state::UNINITIALIZED:
  35144. next(std::integral_constant<state, state::UNINITIALIZED>{});
  35145. current = state::RUNNING;
  35146. break;
  35147. case state::RUNNING:
  35148. next(std::integral_constant<state, state::RUNNING>{}, delta, data);
  35149. break;
  35150. default:
  35151. // suppress warnings
  35152. break;
  35153. }
  35154. // if it's dead, it must be notified and removed immediately
  35155. switch(current) {
  35156. case state::SUCCEEDED:
  35157. next(std::integral_constant<state, state::SUCCEEDED>{});
  35158. current = state::FINISHED;
  35159. break;
  35160. case state::FAILED:
  35161. next(std::integral_constant<state, state::FAILED>{});
  35162. current = state::REJECTED;
  35163. break;
  35164. case state::ABORTED:
  35165. next(std::integral_constant<state, state::ABORTED>{});
  35166. current = state::REJECTED;
  35167. break;
  35168. default:
  35169. // suppress warnings
  35170. break;
  35171. }
  35172. }
  35173. private:
  35174. state current{state::UNINITIALIZED};
  35175. };
  35176. /**
  35177. * @brief Adaptor for lambdas and functors to turn them into processes.
  35178. *
  35179. * Lambdas and functors can't be used directly with a scheduler for they are not
  35180. * properly defined processes with managed life cycles.<br/>
  35181. * This class helps in filling the gap and turning lambdas and functors into
  35182. * full featured processes usable by a scheduler.
  35183. *
  35184. * The signature of the function call operator should be equivalent to the
  35185. * following:
  35186. *
  35187. * @code{.cpp}
  35188. * void(Delta delta, void *data, auto succeed, auto fail);
  35189. * @endcode
  35190. *
  35191. * Where:
  35192. *
  35193. * * `delta` is the elapsed time.
  35194. * * `data` is an opaque pointer to user data if any, `nullptr` otherwise.
  35195. * * `succeed` is a function to call when a process terminates with success.
  35196. * * `fail` is a function to call when a process terminates with errors.
  35197. *
  35198. * The signature of the function call operator of both `succeed` and `fail`
  35199. * is equivalent to the following:
  35200. *
  35201. * @code{.cpp}
  35202. * void();
  35203. * @endcode
  35204. *
  35205. * Usually users shouldn't worry about creating adaptors. A scheduler will
  35206. * create them internally each and avery time a lambda or a functor is used as
  35207. * a process.
  35208. *
  35209. * @sa process
  35210. * @sa scheduler
  35211. *
  35212. * @tparam Func Actual type of process.
  35213. * @tparam Delta Type to use to provide elapsed time.
  35214. */
  35215. template<typename Func, typename Delta>
  35216. struct process_adaptor: process<process_adaptor<Func, Delta>, Delta>, private Func {
  35217. /**
  35218. * @brief Constructs a process adaptor from a lambda or a functor.
  35219. * @tparam Args Types of arguments to use to initialize the actual process.
  35220. * @param args Parameters to use to initialize the actual process.
  35221. */
  35222. template<typename... Args>
  35223. process_adaptor(Args &&... args)
  35224. : Func{std::forward<Args>(args)...}
  35225. {}
  35226. /**
  35227. * @brief Updates a process and its internal state if required.
  35228. * @param delta Elapsed time.
  35229. * @param data Optional data.
  35230. */
  35231. void update(const Delta delta, void *data) {
  35232. Func::operator()(delta, data, [this]() { this->succeed(); }, [this]() { this->fail(); });
  35233. }
  35234. };
  35235. }
  35236. #endif
  35237. // #include "process/scheduler.hpp"
  35238. #ifndef ENTT_PROCESS_SCHEDULER_HPP
  35239. #define ENTT_PROCESS_SCHEDULER_HPP
  35240. #include <vector>
  35241. #include <memory>
  35242. #include <utility>
  35243. #include <algorithm>
  35244. #include <type_traits>
  35245. // #include "../config/config.h"
  35246. // #include "process.hpp"
  35247. #ifndef ENTT_PROCESS_PROCESS_HPP
  35248. #define ENTT_PROCESS_PROCESS_HPP
  35249. #include <utility>
  35250. #include <type_traits>
  35251. // #include "../config/config.h"
  35252. namespace entt {
  35253. /**
  35254. * @brief Base class for processes.
  35255. *
  35256. * This class stays true to the CRTP idiom. Derived classes must specify what's
  35257. * the intended type for elapsed times.<br/>
  35258. * A process should expose publicly the following member functions whether
  35259. * required:
  35260. *
  35261. * * @code{.cpp}
  35262. * void update(Delta, void *);
  35263. * @endcode
  35264. *
  35265. * It's invoked once per tick until a process is explicitly aborted or it
  35266. * terminates either with or without errors. Even though it's not mandatory to
  35267. * declare this member function, as a rule of thumb each process should at
  35268. * least define it to work properly. The `void *` parameter is an opaque
  35269. * pointer to user data (if any) forwarded directly to the process during an
  35270. * update.
  35271. *
  35272. * * @code{.cpp}
  35273. * void init();
  35274. * @endcode
  35275. *
  35276. * It's invoked when the process joins the running queue of a scheduler. This
  35277. * happens as soon as it's attached to the scheduler if the process is a top
  35278. * level one, otherwise when it replaces its parent if the process is a
  35279. * continuation.
  35280. *
  35281. * * @code{.cpp}
  35282. * void succeeded();
  35283. * @endcode
  35284. *
  35285. * It's invoked in case of success, immediately after an update and during the
  35286. * same tick.
  35287. *
  35288. * * @code{.cpp}
  35289. * void failed();
  35290. * @endcode
  35291. *
  35292. * It's invoked in case of errors, immediately after an update and during the
  35293. * same tick.
  35294. *
  35295. * * @code{.cpp}
  35296. * void aborted();
  35297. * @endcode
  35298. *
  35299. * It's invoked only if a process is explicitly aborted. There is no guarantee
  35300. * that it executes in the same tick, this depends solely on whether the
  35301. * process is aborted immediately or not.
  35302. *
  35303. * Derived classes can change the internal state of a process by invoking the
  35304. * `succeed` and `fail` protected member functions and even pause or unpause the
  35305. * process itself.
  35306. *
  35307. * @sa scheduler
  35308. *
  35309. * @tparam Derived Actual type of process that extends the class template.
  35310. * @tparam Delta Type to use to provide elapsed time.
  35311. */
  35312. template<typename Derived, typename Delta>
  35313. class process {
  35314. enum class state: unsigned int {
  35315. UNINITIALIZED = 0,
  35316. RUNNING,
  35317. PAUSED,
  35318. SUCCEEDED,
  35319. FAILED,
  35320. ABORTED,
  35321. FINISHED,
  35322. REJECTED
  35323. };
  35324. template<typename Target = Derived>
  35325. auto next(std::integral_constant<state, state::UNINITIALIZED>)
  35326. -> decltype(std::declval<Target>().init(), void()) {
  35327. static_cast<Target *>(this)->init();
  35328. }
  35329. template<typename Target = Derived>
  35330. auto next(std::integral_constant<state, state::RUNNING>, Delta delta, void *data)
  35331. -> decltype(std::declval<Target>().update(delta, data), void()) {
  35332. static_cast<Target *>(this)->update(delta, data);
  35333. }
  35334. template<typename Target = Derived>
  35335. auto next(std::integral_constant<state, state::SUCCEEDED>)
  35336. -> decltype(std::declval<Target>().succeeded(), void()) {
  35337. static_cast<Target *>(this)->succeeded();
  35338. }
  35339. template<typename Target = Derived>
  35340. auto next(std::integral_constant<state, state::FAILED>)
  35341. -> decltype(std::declval<Target>().failed(), void()) {
  35342. static_cast<Target *>(this)->failed();
  35343. }
  35344. template<typename Target = Derived>
  35345. auto next(std::integral_constant<state, state::ABORTED>)
  35346. -> decltype(std::declval<Target>().aborted(), void()) {
  35347. static_cast<Target *>(this)->aborted();
  35348. }
  35349. void next(...) const ENTT_NOEXCEPT {}
  35350. protected:
  35351. /**
  35352. * @brief Terminates a process with success if it's still alive.
  35353. *
  35354. * The function is idempotent and it does nothing if the process isn't
  35355. * alive.
  35356. */
  35357. void succeed() ENTT_NOEXCEPT {
  35358. if(alive()) {
  35359. current = state::SUCCEEDED;
  35360. }
  35361. }
  35362. /**
  35363. * @brief Terminates a process with errors if it's still alive.
  35364. *
  35365. * The function is idempotent and it does nothing if the process isn't
  35366. * alive.
  35367. */
  35368. void fail() ENTT_NOEXCEPT {
  35369. if(alive()) {
  35370. current = state::FAILED;
  35371. }
  35372. }
  35373. /**
  35374. * @brief Stops a process if it's in a running state.
  35375. *
  35376. * The function is idempotent and it does nothing if the process isn't
  35377. * running.
  35378. */
  35379. void pause() ENTT_NOEXCEPT {
  35380. if(current == state::RUNNING) {
  35381. current = state::PAUSED;
  35382. }
  35383. }
  35384. /**
  35385. * @brief Restarts a process if it's paused.
  35386. *
  35387. * The function is idempotent and it does nothing if the process isn't
  35388. * paused.
  35389. */
  35390. void unpause() ENTT_NOEXCEPT {
  35391. if(current == state::PAUSED) {
  35392. current = state::RUNNING;
  35393. }
  35394. }
  35395. public:
  35396. /*! @brief Type used to provide elapsed time. */
  35397. using delta_type = Delta;
  35398. /*! @brief Default destructor. */
  35399. virtual ~process() {
  35400. static_assert(std::is_base_of_v<process, Derived>, "Incorrect use of the class template");
  35401. }
  35402. /**
  35403. * @brief Aborts a process if it's still alive.
  35404. *
  35405. * The function is idempotent and it does nothing if the process isn't
  35406. * alive.
  35407. *
  35408. * @param immediately Requests an immediate operation.
  35409. */
  35410. void abort(const bool immediately = false) {
  35411. if(alive()) {
  35412. current = state::ABORTED;
  35413. if(immediately) {
  35414. tick({});
  35415. }
  35416. }
  35417. }
  35418. /**
  35419. * @brief Returns true if a process is either running or paused.
  35420. * @return True if the process is still alive, false otherwise.
  35421. */
  35422. [[nodiscard]] bool alive() const ENTT_NOEXCEPT {
  35423. return current == state::RUNNING || current == state::PAUSED;
  35424. }
  35425. /**
  35426. * @brief Returns true if a process is already terminated.
  35427. * @return True if the process is terminated, false otherwise.
  35428. */
  35429. [[nodiscard]] bool finished() const ENTT_NOEXCEPT {
  35430. return current == state::FINISHED;
  35431. }
  35432. /**
  35433. * @brief Returns true if a process is currently paused.
  35434. * @return True if the process is paused, false otherwise.
  35435. */
  35436. [[nodiscard]] bool paused() const ENTT_NOEXCEPT {
  35437. return current == state::PAUSED;
  35438. }
  35439. /**
  35440. * @brief Returns true if a process terminated with errors.
  35441. * @return True if the process terminated with errors, false otherwise.
  35442. */
  35443. [[nodiscard]] bool rejected() const ENTT_NOEXCEPT {
  35444. return current == state::REJECTED;
  35445. }
  35446. /**
  35447. * @brief Updates a process and its internal state if required.
  35448. * @param delta Elapsed time.
  35449. * @param data Optional data.
  35450. */
  35451. void tick(const Delta delta, void *data = nullptr) {
  35452. switch (current) {
  35453. case state::UNINITIALIZED:
  35454. next(std::integral_constant<state, state::UNINITIALIZED>{});
  35455. current = state::RUNNING;
  35456. break;
  35457. case state::RUNNING:
  35458. next(std::integral_constant<state, state::RUNNING>{}, delta, data);
  35459. break;
  35460. default:
  35461. // suppress warnings
  35462. break;
  35463. }
  35464. // if it's dead, it must be notified and removed immediately
  35465. switch(current) {
  35466. case state::SUCCEEDED:
  35467. next(std::integral_constant<state, state::SUCCEEDED>{});
  35468. current = state::FINISHED;
  35469. break;
  35470. case state::FAILED:
  35471. next(std::integral_constant<state, state::FAILED>{});
  35472. current = state::REJECTED;
  35473. break;
  35474. case state::ABORTED:
  35475. next(std::integral_constant<state, state::ABORTED>{});
  35476. current = state::REJECTED;
  35477. break;
  35478. default:
  35479. // suppress warnings
  35480. break;
  35481. }
  35482. }
  35483. private:
  35484. state current{state::UNINITIALIZED};
  35485. };
  35486. /**
  35487. * @brief Adaptor for lambdas and functors to turn them into processes.
  35488. *
  35489. * Lambdas and functors can't be used directly with a scheduler for they are not
  35490. * properly defined processes with managed life cycles.<br/>
  35491. * This class helps in filling the gap and turning lambdas and functors into
  35492. * full featured processes usable by a scheduler.
  35493. *
  35494. * The signature of the function call operator should be equivalent to the
  35495. * following:
  35496. *
  35497. * @code{.cpp}
  35498. * void(Delta delta, void *data, auto succeed, auto fail);
  35499. * @endcode
  35500. *
  35501. * Where:
  35502. *
  35503. * * `delta` is the elapsed time.
  35504. * * `data` is an opaque pointer to user data if any, `nullptr` otherwise.
  35505. * * `succeed` is a function to call when a process terminates with success.
  35506. * * `fail` is a function to call when a process terminates with errors.
  35507. *
  35508. * The signature of the function call operator of both `succeed` and `fail`
  35509. * is equivalent to the following:
  35510. *
  35511. * @code{.cpp}
  35512. * void();
  35513. * @endcode
  35514. *
  35515. * Usually users shouldn't worry about creating adaptors. A scheduler will
  35516. * create them internally each and avery time a lambda or a functor is used as
  35517. * a process.
  35518. *
  35519. * @sa process
  35520. * @sa scheduler
  35521. *
  35522. * @tparam Func Actual type of process.
  35523. * @tparam Delta Type to use to provide elapsed time.
  35524. */
  35525. template<typename Func, typename Delta>
  35526. struct process_adaptor: process<process_adaptor<Func, Delta>, Delta>, private Func {
  35527. /**
  35528. * @brief Constructs a process adaptor from a lambda or a functor.
  35529. * @tparam Args Types of arguments to use to initialize the actual process.
  35530. * @param args Parameters to use to initialize the actual process.
  35531. */
  35532. template<typename... Args>
  35533. process_adaptor(Args &&... args)
  35534. : Func{std::forward<Args>(args)...}
  35535. {}
  35536. /**
  35537. * @brief Updates a process and its internal state if required.
  35538. * @param delta Elapsed time.
  35539. * @param data Optional data.
  35540. */
  35541. void update(const Delta delta, void *data) {
  35542. Func::operator()(delta, data, [this]() { this->succeed(); }, [this]() { this->fail(); });
  35543. }
  35544. };
  35545. }
  35546. #endif
  35547. namespace entt {
  35548. /**
  35549. * @brief Cooperative scheduler for processes.
  35550. *
  35551. * A cooperative scheduler runs processes and helps managing their life cycles.
  35552. *
  35553. * Each process is invoked once per tick. If a process terminates, it's
  35554. * removed automatically from the scheduler and it's never invoked again.<br/>
  35555. * A process can also have a child. In this case, the process is replaced with
  35556. * its child when it terminates if it returns with success. In case of errors,
  35557. * both the process and its child are discarded.
  35558. *
  35559. * Example of use (pseudocode):
  35560. *
  35561. * @code{.cpp}
  35562. * scheduler.attach([](auto delta, void *, auto succeed, auto fail) {
  35563. * // code
  35564. * }).then<my_process>(arguments...);
  35565. * @endcode
  35566. *
  35567. * In order to invoke all scheduled processes, call the `update` member function
  35568. * passing it the elapsed time to forward to the tasks.
  35569. *
  35570. * @sa process
  35571. *
  35572. * @tparam Delta Type to use to provide elapsed time.
  35573. */
  35574. template<typename Delta>
  35575. class scheduler {
  35576. struct process_handler {
  35577. using instance_type = std::unique_ptr<void, void(*)(void *)>;
  35578. using update_fn_type = bool(process_handler &, Delta, void *);
  35579. using abort_fn_type = void(process_handler &, bool);
  35580. using next_type = std::unique_ptr<process_handler>;
  35581. instance_type instance;
  35582. update_fn_type *update;
  35583. abort_fn_type *abort;
  35584. next_type next;
  35585. };
  35586. struct continuation {
  35587. continuation(process_handler *ref)
  35588. : handler{ref}
  35589. {}
  35590. template<typename Proc, typename... Args>
  35591. continuation then(Args &&... args) {
  35592. static_assert(std::is_base_of_v<process<Proc, Delta>, Proc>, "Invalid process type");
  35593. auto proc = typename process_handler::instance_type{new Proc{std::forward<Args>(args)...}, &scheduler::deleter<Proc>};
  35594. handler->next.reset(new process_handler{std::move(proc), &scheduler::update<Proc>, &scheduler::abort<Proc>, nullptr});
  35595. handler = handler->next.get();
  35596. return *this;
  35597. }
  35598. template<typename Func>
  35599. continuation then(Func &&func) {
  35600. return then<process_adaptor<std::decay_t<Func>, Delta>>(std::forward<Func>(func));
  35601. }
  35602. private:
  35603. process_handler *handler;
  35604. };
  35605. template<typename Proc>
  35606. [[nodiscard]] static bool update(process_handler &handler, const Delta delta, void *data) {
  35607. auto *process = static_cast<Proc *>(handler.instance.get());
  35608. process->tick(delta, data);
  35609. if(process->rejected()) {
  35610. return true;
  35611. } else if(process->finished()) {
  35612. if(handler.next) {
  35613. handler = std::move(*handler.next);
  35614. // forces the process to exit the uninitialized state
  35615. return handler.update(handler, {}, nullptr);
  35616. }
  35617. return true;
  35618. }
  35619. return false;
  35620. }
  35621. template<typename Proc>
  35622. static void abort(process_handler &handler, const bool immediately) {
  35623. static_cast<Proc *>(handler.instance.get())->abort(immediately);
  35624. }
  35625. template<typename Proc>
  35626. static void deleter(void *proc) {
  35627. delete static_cast<Proc *>(proc);
  35628. }
  35629. public:
  35630. /*! @brief Unsigned integer type. */
  35631. using size_type = std::size_t;
  35632. /*! @brief Default constructor. */
  35633. scheduler() = default;
  35634. /*! @brief Default move constructor. */
  35635. scheduler(scheduler &&) = default;
  35636. /*! @brief Default move assignment operator. @return This scheduler. */
  35637. scheduler & operator=(scheduler &&) = default;
  35638. /**
  35639. * @brief Number of processes currently scheduled.
  35640. * @return Number of processes currently scheduled.
  35641. */
  35642. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  35643. return handlers.size();
  35644. }
  35645. /**
  35646. * @brief Returns true if at least a process is currently scheduled.
  35647. * @return True if there are scheduled processes, false otherwise.
  35648. */
  35649. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  35650. return handlers.empty();
  35651. }
  35652. /**
  35653. * @brief Discards all scheduled processes.
  35654. *
  35655. * Processes aren't aborted. They are discarded along with their children
  35656. * and never executed again.
  35657. */
  35658. void clear() {
  35659. handlers.clear();
  35660. }
  35661. /**
  35662. * @brief Schedules a process for the next tick.
  35663. *
  35664. * Returned value is an opaque object that can be used to attach a child to
  35665. * the given process. The child is automatically scheduled when the process
  35666. * terminates and only if the process returns with success.
  35667. *
  35668. * Example of use (pseudocode):
  35669. *
  35670. * @code{.cpp}
  35671. * // schedules a task in the form of a process class
  35672. * scheduler.attach<my_process>(arguments...)
  35673. * // appends a child in the form of a lambda function
  35674. * .then([](auto delta, void *, auto succeed, auto fail) {
  35675. * // code
  35676. * })
  35677. * // appends a child in the form of another process class
  35678. * .then<my_other_process>();
  35679. * @endcode
  35680. *
  35681. * @tparam Proc Type of process to schedule.
  35682. * @tparam Args Types of arguments to use to initialize the process.
  35683. * @param args Parameters to use to initialize the process.
  35684. * @return An opaque object to use to concatenate processes.
  35685. */
  35686. template<typename Proc, typename... Args>
  35687. auto attach(Args &&... args) {
  35688. static_assert(std::is_base_of_v<process<Proc, Delta>, Proc>, "Invalid process type");
  35689. auto proc = typename process_handler::instance_type{new Proc{std::forward<Args>(args)...}, &scheduler::deleter<Proc>};
  35690. process_handler handler{std::move(proc), &scheduler::update<Proc>, &scheduler::abort<Proc>, nullptr};
  35691. // forces the process to exit the uninitialized state
  35692. handler.update(handler, {}, nullptr);
  35693. return continuation{&handlers.emplace_back(std::move(handler))};
  35694. }
  35695. /**
  35696. * @brief Schedules a process for the next tick.
  35697. *
  35698. * A process can be either a lambda or a functor. The scheduler wraps both
  35699. * of them in a process adaptor internally.<br/>
  35700. * The signature of the function call operator should be equivalent to the
  35701. * following:
  35702. *
  35703. * @code{.cpp}
  35704. * void(Delta delta, void *data, auto succeed, auto fail);
  35705. * @endcode
  35706. *
  35707. * Where:
  35708. *
  35709. * * `delta` is the elapsed time.
  35710. * * `data` is an opaque pointer to user data if any, `nullptr` otherwise.
  35711. * * `succeed` is a function to call when a process terminates with success.
  35712. * * `fail` is a function to call when a process terminates with errors.
  35713. *
  35714. * The signature of the function call operator of both `succeed` and `fail`
  35715. * is equivalent to the following:
  35716. *
  35717. * @code{.cpp}
  35718. * void();
  35719. * @endcode
  35720. *
  35721. * Returned value is an opaque object that can be used to attach a child to
  35722. * the given process. The child is automatically scheduled when the process
  35723. * terminates and only if the process returns with success.
  35724. *
  35725. * Example of use (pseudocode):
  35726. *
  35727. * @code{.cpp}
  35728. * // schedules a task in the form of a lambda function
  35729. * scheduler.attach([](auto delta, void *, auto succeed, auto fail) {
  35730. * // code
  35731. * })
  35732. * // appends a child in the form of another lambda function
  35733. * .then([](auto delta, void *, auto succeed, auto fail) {
  35734. * // code
  35735. * })
  35736. * // appends a child in the form of a process class
  35737. * .then<my_process>(arguments...);
  35738. * @endcode
  35739. *
  35740. * @sa process_adaptor
  35741. *
  35742. * @tparam Func Type of process to schedule.
  35743. * @param func Either a lambda or a functor to use as a process.
  35744. * @return An opaque object to use to concatenate processes.
  35745. */
  35746. template<typename Func>
  35747. auto attach(Func &&func) {
  35748. using Proc = process_adaptor<std::decay_t<Func>, Delta>;
  35749. return attach<Proc>(std::forward<Func>(func));
  35750. }
  35751. /**
  35752. * @brief Updates all scheduled processes.
  35753. *
  35754. * All scheduled processes are executed in no specific order.<br/>
  35755. * If a process terminates with success, it's replaced with its child, if
  35756. * any. Otherwise, if a process terminates with an error, it's removed along
  35757. * with its child.
  35758. *
  35759. * @param delta Elapsed time.
  35760. * @param data Optional data.
  35761. */
  35762. void update(const Delta delta, void *data = nullptr) {
  35763. auto sz = handlers.size();
  35764. for(auto pos = handlers.size(); pos; --pos) {
  35765. auto &handler = handlers[pos-1];
  35766. if(const auto dead = handler.update(handler, delta, data); dead) {
  35767. std::swap(handler, handlers[--sz]);
  35768. }
  35769. }
  35770. handlers.erase(handlers.begin() + sz, handlers.end());
  35771. }
  35772. /**
  35773. * @brief Aborts all scheduled processes.
  35774. *
  35775. * Unless an immediate operation is requested, the abort is scheduled for
  35776. * the next tick. Processes won't be executed anymore in any case.<br/>
  35777. * Once a process is fully aborted and thus finished, it's discarded along
  35778. * with its child, if any.
  35779. *
  35780. * @param immediately Requests an immediate operation.
  35781. */
  35782. void abort(const bool immediately = false) {
  35783. decltype(handlers) exec;
  35784. exec.swap(handlers);
  35785. for(auto &&handler: exec) {
  35786. handler.abort(handler, immediately);
  35787. }
  35788. std::move(handlers.begin(), handlers.end(), std::back_inserter(exec));
  35789. handlers.swap(exec);
  35790. }
  35791. private:
  35792. std::vector<process_handler> handlers{};
  35793. };
  35794. }
  35795. #endif
  35796. // #include "resource/cache.hpp"
  35797. #ifndef ENTT_RESOURCE_CACHE_HPP
  35798. #define ENTT_RESOURCE_CACHE_HPP
  35799. #include <type_traits>
  35800. #include <unordered_map>
  35801. #include <utility>
  35802. // #include "../config/config.h"
  35803. #ifndef ENTT_CONFIG_CONFIG_H
  35804. #define ENTT_CONFIG_CONFIG_H
  35805. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  35806. # define ENTT_NOEXCEPT noexcept
  35807. # define ENTT_THROW throw
  35808. # define ENTT_TRY try
  35809. # define ENTT_CATCH catch(...)
  35810. #else
  35811. # define ENTT_NOEXCEPT
  35812. # define ENTT_THROW
  35813. # define ENTT_TRY if(true)
  35814. # define ENTT_CATCH if(false)
  35815. #endif
  35816. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  35817. # include <new>
  35818. # define ENTT_LAUNDER(expr) std::launder(expr)
  35819. #else
  35820. # define ENTT_LAUNDER(expr) expr
  35821. #endif
  35822. #ifndef ENTT_USE_ATOMIC
  35823. # define ENTT_MAYBE_ATOMIC(Type) Type
  35824. #else
  35825. # include <atomic>
  35826. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  35827. #endif
  35828. #ifndef ENTT_ID_TYPE
  35829. # include <cstdint>
  35830. # define ENTT_ID_TYPE std::uint32_t
  35831. #endif
  35832. #ifdef ENTT_SPARSE_PAGE
  35833. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  35834. #else
  35835. # define ENTT_SPARSE_PAGE 4096
  35836. #endif
  35837. #ifdef ENTT_PACKED_PAGE
  35838. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  35839. #else
  35840. # define ENTT_PACKED_PAGE 1024
  35841. #endif
  35842. #ifdef ENTT_DISABLE_ASSERT
  35843. # undef ENTT_ASSERT
  35844. # define ENTT_ASSERT(...) (void(0))
  35845. #elif !defined ENTT_ASSERT
  35846. # include <cassert>
  35847. # define ENTT_ASSERT(condition, ...) assert(condition)
  35848. #endif
  35849. #ifdef ENTT_NO_ETO
  35850. # include <type_traits>
  35851. # define ENTT_IGNORE_IF_EMPTY std::false_type
  35852. #else
  35853. # include <type_traits>
  35854. # define ENTT_IGNORE_IF_EMPTY std::true_type
  35855. #endif
  35856. #ifndef ENTT_STANDARD_CPP
  35857. # if defined __clang__ || defined __GNUC__
  35858. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  35859. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  35860. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  35861. # elif defined _MSC_VER
  35862. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  35863. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  35864. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  35865. # endif
  35866. #endif
  35867. #endif
  35868. // #include "../core/fwd.hpp"
  35869. #ifndef ENTT_CORE_FWD_HPP
  35870. #define ENTT_CORE_FWD_HPP
  35871. #include <type_traits>
  35872. // #include "../config/config.h"
  35873. #ifndef ENTT_CONFIG_CONFIG_H
  35874. #define ENTT_CONFIG_CONFIG_H
  35875. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  35876. # define ENTT_NOEXCEPT noexcept
  35877. # define ENTT_THROW throw
  35878. # define ENTT_TRY try
  35879. # define ENTT_CATCH catch(...)
  35880. #else
  35881. # define ENTT_NOEXCEPT
  35882. # define ENTT_THROW
  35883. # define ENTT_TRY if(true)
  35884. # define ENTT_CATCH if(false)
  35885. #endif
  35886. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  35887. # include <new>
  35888. # define ENTT_LAUNDER(expr) std::launder(expr)
  35889. #else
  35890. # define ENTT_LAUNDER(expr) expr
  35891. #endif
  35892. #ifndef ENTT_USE_ATOMIC
  35893. # define ENTT_MAYBE_ATOMIC(Type) Type
  35894. #else
  35895. # include <atomic>
  35896. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  35897. #endif
  35898. #ifndef ENTT_ID_TYPE
  35899. # include <cstdint>
  35900. # define ENTT_ID_TYPE std::uint32_t
  35901. #endif
  35902. #ifdef ENTT_SPARSE_PAGE
  35903. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  35904. #else
  35905. # define ENTT_SPARSE_PAGE 4096
  35906. #endif
  35907. #ifdef ENTT_PACKED_PAGE
  35908. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  35909. #else
  35910. # define ENTT_PACKED_PAGE 1024
  35911. #endif
  35912. #ifdef ENTT_DISABLE_ASSERT
  35913. # undef ENTT_ASSERT
  35914. # define ENTT_ASSERT(...) (void(0))
  35915. #elif !defined ENTT_ASSERT
  35916. # include <cassert>
  35917. # define ENTT_ASSERT(condition, ...) assert(condition)
  35918. #endif
  35919. #ifdef ENTT_NO_ETO
  35920. # include <type_traits>
  35921. # define ENTT_IGNORE_IF_EMPTY std::false_type
  35922. #else
  35923. # include <type_traits>
  35924. # define ENTT_IGNORE_IF_EMPTY std::true_type
  35925. #endif
  35926. #ifndef ENTT_STANDARD_CPP
  35927. # if defined __clang__ || defined __GNUC__
  35928. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  35929. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  35930. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  35931. # elif defined _MSC_VER
  35932. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  35933. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  35934. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  35935. # endif
  35936. #endif
  35937. #endif
  35938. namespace entt {
  35939. template<std::size_t Len = sizeof(double[2]), std::size_t = alignof(typename std::aligned_storage_t<Len + !Len>)>
  35940. class basic_any;
  35941. /*! @brief Alias declaration for type identifiers. */
  35942. using id_type = ENTT_ID_TYPE;
  35943. /*! @brief Alias declaration for the most common use case. */
  35944. using any = basic_any<>;
  35945. }
  35946. #endif
  35947. // #include "handle.hpp"
  35948. #ifndef ENTT_RESOURCE_HANDLE_HPP
  35949. #define ENTT_RESOURCE_HANDLE_HPP
  35950. #include <memory>
  35951. #include <type_traits>
  35952. #include <utility>
  35953. // #include "../config/config.h"
  35954. // #include "fwd.hpp"
  35955. #ifndef ENTT_RESOURCE_FWD_HPP
  35956. #define ENTT_RESOURCE_FWD_HPP
  35957. namespace entt {
  35958. template<typename>
  35959. struct resource_cache;
  35960. template<typename>
  35961. class resource_handle;
  35962. template<typename, typename>
  35963. class resource_loader;
  35964. }
  35965. #endif
  35966. namespace entt {
  35967. /**
  35968. * @brief Shared resource handle.
  35969. *
  35970. * A shared resource handle is a small class that wraps a resource and keeps it
  35971. * alive even if it's deleted from the cache. It can be either copied or
  35972. * moved. A handle shares a reference to the same resource with all the other
  35973. * handles constructed for the same identifier.<br/>
  35974. * As a rule of thumb, resources should never be copied nor moved. Handles are
  35975. * the way to go to keep references to them.
  35976. *
  35977. * @tparam Resource Type of resource managed by a handle.
  35978. */
  35979. template<typename Resource>
  35980. class resource_handle {
  35981. /*! @brief Resource handles are friends with each other. */
  35982. template<typename>
  35983. friend class resource_handle;
  35984. public:
  35985. /*! @brief Default constructor. */
  35986. resource_handle() ENTT_NOEXCEPT = default;
  35987. /**
  35988. * @brief Creates a handle from a shared pointer, namely a resource.
  35989. * @param res A pointer to a properly initialized resource.
  35990. */
  35991. resource_handle(std::shared_ptr<Resource> res) ENTT_NOEXCEPT
  35992. : resource{std::move(res)}
  35993. {}
  35994. /**
  35995. * @brief Copy constructor.
  35996. * @param other The instance to copy from.
  35997. */
  35998. resource_handle(const resource_handle<Resource> &other) ENTT_NOEXCEPT = default;
  35999. /**
  36000. * @brief Move constructor.
  36001. * @param other The instance to move from.
  36002. */
  36003. resource_handle(resource_handle<Resource> &&other) ENTT_NOEXCEPT = default;
  36004. /**
  36005. * @brief Copy constructs a handle which shares ownership of the resource.
  36006. * @tparam Other Type of resource managed by the received handle.
  36007. * @param other The handle to copy from.
  36008. */
  36009. template<typename Other, typename = std::enable_if_t<!std::is_same_v<Other, Resource> && std::is_base_of_v<Resource, Other>>>
  36010. resource_handle(const resource_handle<Other> &other) ENTT_NOEXCEPT
  36011. : resource{other.resource}
  36012. {}
  36013. /**
  36014. * @brief Move constructs a handle which takes ownership of the resource.
  36015. * @tparam Other Type of resource managed by the received handle.
  36016. * @param other The handle to move from.
  36017. */
  36018. template<typename Other, typename = std::enable_if_t<!std::is_same_v<Other, Resource> && std::is_base_of_v<Resource, Other>>>
  36019. resource_handle(resource_handle<Other> &&other) ENTT_NOEXCEPT
  36020. : resource{std::move(other.resource)}
  36021. {}
  36022. /**
  36023. * @brief Copy assignment operator.
  36024. * @param other The instance to copy from.
  36025. * @return This resource handle.
  36026. */
  36027. resource_handle & operator=(const resource_handle<Resource> &other) ENTT_NOEXCEPT = default;
  36028. /**
  36029. * @brief Move assignment operator.
  36030. * @param other The instance to move from.
  36031. * @return This resource handle.
  36032. */
  36033. resource_handle & operator=(resource_handle<Resource> &&other) ENTT_NOEXCEPT = default;
  36034. /**
  36035. * @brief Copy assignment operator from foreign handle.
  36036. * @tparam Other Type of resource managed by the received handle.
  36037. * @param other The handle to copy from.
  36038. * @return This resource handle.
  36039. */
  36040. template<typename Other>
  36041. std::enable_if_t<!std::is_same_v<Other, Resource> && std::is_base_of_v<Resource, Other>, resource_handle &>
  36042. operator=(const resource_handle<Other> &other) ENTT_NOEXCEPT {
  36043. resource = other.resource;
  36044. return *this;
  36045. }
  36046. /**
  36047. * @brief Move assignment operator from foreign handle.
  36048. * @tparam Other Type of resource managed by the received handle.
  36049. * @param other The handle to move from.
  36050. * @return This resource handle.
  36051. */
  36052. template<typename Other>
  36053. std::enable_if_t<!std::is_same_v<Other, Resource> && std::is_base_of_v<Resource, Other>, resource_handle &>
  36054. operator=(resource_handle<Other> &&other) ENTT_NOEXCEPT {
  36055. resource = std::move(other.resource);
  36056. return *this;
  36057. }
  36058. /**
  36059. * @brief Gets a reference to the managed resource.
  36060. *
  36061. * @warning
  36062. * The behavior is undefined if the handle doesn't contain a resource.
  36063. *
  36064. * @return A reference to the managed resource.
  36065. */
  36066. [[nodiscard]] const Resource & get() const ENTT_NOEXCEPT {
  36067. ENTT_ASSERT(static_cast<bool>(resource), "Invalid resource");
  36068. return *resource;
  36069. }
  36070. /*! @copydoc get */
  36071. [[nodiscard]] Resource & get() ENTT_NOEXCEPT {
  36072. return const_cast<Resource &>(std::as_const(*this).get());
  36073. }
  36074. /*! @copydoc get */
  36075. [[nodiscard]] operator const Resource & () const ENTT_NOEXCEPT {
  36076. return get();
  36077. }
  36078. /*! @copydoc get */
  36079. [[nodiscard]] operator Resource & () ENTT_NOEXCEPT {
  36080. return get();
  36081. }
  36082. /*! @copydoc get */
  36083. [[nodiscard]] const Resource & operator *() const ENTT_NOEXCEPT {
  36084. return get();
  36085. }
  36086. /*! @copydoc get */
  36087. [[nodiscard]] Resource & operator *() ENTT_NOEXCEPT {
  36088. return get();
  36089. }
  36090. /**
  36091. * @brief Gets a pointer to the managed resource.
  36092. *
  36093. * @warning
  36094. * The behavior is undefined if the handle doesn't contain a resource.
  36095. *
  36096. * @return A pointer to the managed resource or `nullptr` if the handle
  36097. * contains no resource at all.
  36098. */
  36099. [[nodiscard]] const Resource * operator->() const ENTT_NOEXCEPT {
  36100. return resource.get();
  36101. }
  36102. /*! @copydoc operator-> */
  36103. [[nodiscard]] Resource * operator->() ENTT_NOEXCEPT {
  36104. return const_cast<Resource *>(std::as_const(*this).operator->());
  36105. }
  36106. /**
  36107. * @brief Returns true if a handle contains a resource, false otherwise.
  36108. * @return True if the handle contains a resource, false otherwise.
  36109. */
  36110. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  36111. return static_cast<bool>(resource);
  36112. }
  36113. private:
  36114. std::shared_ptr<Resource> resource;
  36115. };
  36116. }
  36117. #endif
  36118. // #include "loader.hpp"
  36119. #ifndef ENTT_RESOURCE_LOADER_HPP
  36120. #define ENTT_RESOURCE_LOADER_HPP
  36121. // #include "fwd.hpp"
  36122. // #include "handle.hpp"
  36123. namespace entt {
  36124. /**
  36125. * @brief Base class for resource loaders.
  36126. *
  36127. * Resource loaders must inherit from this class and stay true to the CRTP
  36128. * idiom. Moreover, a resource loader must expose a public, const member
  36129. * function named `load` that accepts a variable number of arguments and returns
  36130. * a handle to the resource just created.<br/>
  36131. * As an example:
  36132. *
  36133. * @code{.cpp}
  36134. * struct my_resource {};
  36135. *
  36136. * struct my_loader: entt::resource_loader<my_loader, my_resource> {
  36137. * resource_handle<my_resource> load(int value) const {
  36138. * // use the integer value somehow
  36139. * return std::make_shared<my_resource>();
  36140. * }
  36141. * };
  36142. * @endcode
  36143. *
  36144. * In general, resource loaders should not have a state or retain data of any
  36145. * type. They should let the cache manage their resources instead.
  36146. *
  36147. * @note
  36148. * Base class and CRTP idiom aren't strictly required with the current
  36149. * implementation. One could argue that a cache can easily work with loaders of
  36150. * any type. However, future changes won't be breaking ones by forcing the use
  36151. * of a base class today and that's why the model is already in its place.
  36152. *
  36153. * @tparam Loader Type of the derived class.
  36154. * @tparam Resource Type of resource for which to use the loader.
  36155. */
  36156. template<typename Loader, typename Resource>
  36157. class resource_loader {
  36158. /*! @brief Resource loaders are friends of their caches. */
  36159. template<typename Other>
  36160. friend struct resource_cache;
  36161. /**
  36162. * @brief Loads the resource and returns it.
  36163. * @tparam Args Types of arguments for the loader.
  36164. * @param args Arguments for the loader.
  36165. * @return The resource just loaded or an empty pointer in case of errors.
  36166. */
  36167. template<typename... Args>
  36168. [[nodiscard]] resource_handle<Resource> get(Args &&... args) const {
  36169. return static_cast<const Loader *>(this)->load(std::forward<Args>(args)...);
  36170. }
  36171. };
  36172. }
  36173. #endif
  36174. // #include "fwd.hpp"
  36175. namespace entt {
  36176. /**
  36177. * @brief Simple cache for resources of a given type.
  36178. *
  36179. * Minimal implementation of a cache for resources of a given type. It doesn't
  36180. * offer much functionalities but it's suitable for small or medium sized
  36181. * applications and can be freely inherited to add targeted functionalities for
  36182. * large sized applications.
  36183. *
  36184. * @tparam Resource Type of resources managed by a cache.
  36185. */
  36186. template<typename Resource>
  36187. struct resource_cache {
  36188. /*! @brief Unsigned integer type. */
  36189. using size_type = std::size_t;
  36190. /*! @brief Type of resources managed by a cache. */
  36191. using resource_type = Resource;
  36192. /*! @brief Default constructor. */
  36193. resource_cache() = default;
  36194. /*! @brief Default move constructor. */
  36195. resource_cache(resource_cache &&) = default;
  36196. /*! @brief Default move assignment operator. @return This cache. */
  36197. resource_cache & operator=(resource_cache &&) = default;
  36198. /**
  36199. * @brief Number of resources managed by a cache.
  36200. * @return Number of resources currently stored.
  36201. */
  36202. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  36203. return resources.size();
  36204. }
  36205. /**
  36206. * @brief Returns true if a cache contains no resources, false otherwise.
  36207. * @return True if the cache contains no resources, false otherwise.
  36208. */
  36209. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  36210. return resources.empty();
  36211. }
  36212. /**
  36213. * @brief Clears a cache and discards all its resources.
  36214. *
  36215. * Handles are not invalidated and the memory used by a resource isn't
  36216. * freed as long as at least a handle keeps the resource itself alive.
  36217. */
  36218. void clear() ENTT_NOEXCEPT {
  36219. resources.clear();
  36220. }
  36221. /**
  36222. * @brief Loads the resource that corresponds to a given identifier.
  36223. *
  36224. * In case an identifier isn't already present in the cache, it loads its
  36225. * resource and stores it aside for future uses. Arguments are forwarded
  36226. * directly to the loader in order to construct properly the requested
  36227. * resource.
  36228. *
  36229. * @note
  36230. * If the identifier is already present in the cache, this function does
  36231. * nothing and the arguments are simply discarded.
  36232. *
  36233. * @warning
  36234. * If the resource cannot be loaded correctly, the returned handle will be
  36235. * invalid and any use of it will result in undefined behavior.
  36236. *
  36237. * @tparam Loader Type of loader to use to load the resource if required.
  36238. * @tparam Args Types of arguments to use to load the resource if required.
  36239. * @param id Unique resource identifier.
  36240. * @param args Arguments to use to load the resource if required.
  36241. * @return A handle for the given resource.
  36242. */
  36243. template<typename Loader, typename... Args>
  36244. resource_handle<Resource> load(const id_type id, Args &&... args) {
  36245. if(auto it = resources.find(id); it == resources.cend()) {
  36246. if(auto handle = temp<Loader>(std::forward<Args>(args)...); handle) {
  36247. return (resources[id] = std::move(handle));
  36248. }
  36249. } else {
  36250. return it->second;
  36251. }
  36252. return {};
  36253. }
  36254. /**
  36255. * @brief Reloads a resource or loads it for the first time if not present.
  36256. *
  36257. * Equivalent to the following snippet (pseudocode):
  36258. *
  36259. * @code{.cpp}
  36260. * cache.discard(id);
  36261. * cache.load(id, args...);
  36262. * @endcode
  36263. *
  36264. * Arguments are forwarded directly to the loader in order to construct
  36265. * properly the requested resource.
  36266. *
  36267. * @warning
  36268. * If the resource cannot be loaded correctly, the returned handle will be
  36269. * invalid and any use of it will result in undefined behavior.
  36270. *
  36271. * @tparam Loader Type of loader to use to load the resource.
  36272. * @tparam Args Types of arguments to use to load the resource.
  36273. * @param id Unique resource identifier.
  36274. * @param args Arguments to use to load the resource.
  36275. * @return A handle for the given resource.
  36276. */
  36277. template<typename Loader, typename... Args>
  36278. resource_handle<Resource> reload(const id_type id, Args &&... args) {
  36279. return (discard(id), load<Loader>(id, std::forward<Args>(args)...));
  36280. }
  36281. /**
  36282. * @brief Creates a temporary handle for a resource.
  36283. *
  36284. * Arguments are forwarded directly to the loader in order to construct
  36285. * properly the requested resource. The handle isn't stored aside and the
  36286. * cache isn't in charge of the lifetime of the resource itself.
  36287. *
  36288. * @tparam Loader Type of loader to use to load the resource.
  36289. * @tparam Args Types of arguments to use to load the resource.
  36290. * @param args Arguments to use to load the resource.
  36291. * @return A handle for the given resource.
  36292. */
  36293. template<typename Loader, typename... Args>
  36294. [[nodiscard]] resource_handle<Resource> temp(Args &&... args) const {
  36295. return Loader{}.get(std::forward<Args>(args)...);
  36296. }
  36297. /**
  36298. * @brief Creates a handle for a given resource identifier.
  36299. *
  36300. * A resource handle can be in a either valid or invalid state. In other
  36301. * terms, a resource handle is properly initialized with a resource if the
  36302. * cache contains the resource itself. Otherwise the returned handle is
  36303. * uninitialized and accessing it results in undefined behavior.
  36304. *
  36305. * @sa resource_handle
  36306. *
  36307. * @param id Unique resource identifier.
  36308. * @return A handle for the given resource.
  36309. */
  36310. [[nodiscard]] resource_handle<Resource> handle(const id_type id) const {
  36311. if(auto it = resources.find(id); it != resources.cend()) {
  36312. return it->second;
  36313. }
  36314. return {};
  36315. }
  36316. /**
  36317. * @brief Checks if a cache contains a given identifier.
  36318. * @param id Unique resource identifier.
  36319. * @return True if the cache contains the resource, false otherwise.
  36320. */
  36321. [[nodiscard]] bool contains(const id_type id) const {
  36322. return (resources.find(id) != resources.cend());
  36323. }
  36324. /**
  36325. * @brief Discards the resource that corresponds to a given identifier.
  36326. *
  36327. * Handles are not invalidated and the memory used by the resource isn't
  36328. * freed as long as at least a handle keeps the resource itself alive.
  36329. *
  36330. * @param id Unique resource identifier.
  36331. */
  36332. void discard(const id_type id) {
  36333. if(auto it = resources.find(id); it != resources.end()) {
  36334. resources.erase(it);
  36335. }
  36336. }
  36337. /**
  36338. * @brief Iterates all resources.
  36339. *
  36340. * The function object is invoked for each element. It is provided with
  36341. * either the resource identifier, the resource handle or both of them.<br/>
  36342. * The signature of the function must be equivalent to one of the following
  36343. * forms:
  36344. *
  36345. * @code{.cpp}
  36346. * void(const entt::id_type);
  36347. * void(entt::resource_handle<Resource>);
  36348. * void(const entt::id_type, entt::resource_handle<Resource>);
  36349. * @endcode
  36350. *
  36351. * @tparam Func Type of the function object to invoke.
  36352. * @param func A valid function object.
  36353. */
  36354. template <typename Func>
  36355. void each(Func func) const {
  36356. auto begin = resources.begin();
  36357. auto end = resources.end();
  36358. while(begin != end) {
  36359. auto curr = begin++;
  36360. if constexpr(std::is_invocable_v<Func, id_type>) {
  36361. func(curr->first);
  36362. } else if constexpr(std::is_invocable_v<Func, resource_handle<Resource>>) {
  36363. func(curr->second);
  36364. } else {
  36365. func(curr->first, curr->second);
  36366. }
  36367. }
  36368. }
  36369. private:
  36370. std::unordered_map<id_type, resource_handle<Resource>> resources;
  36371. };
  36372. }
  36373. #endif
  36374. // #include "resource/handle.hpp"
  36375. #ifndef ENTT_RESOURCE_HANDLE_HPP
  36376. #define ENTT_RESOURCE_HANDLE_HPP
  36377. #include <memory>
  36378. #include <type_traits>
  36379. #include <utility>
  36380. // #include "../config/config.h"
  36381. // #include "fwd.hpp"
  36382. namespace entt {
  36383. /**
  36384. * @brief Shared resource handle.
  36385. *
  36386. * A shared resource handle is a small class that wraps a resource and keeps it
  36387. * alive even if it's deleted from the cache. It can be either copied or
  36388. * moved. A handle shares a reference to the same resource with all the other
  36389. * handles constructed for the same identifier.<br/>
  36390. * As a rule of thumb, resources should never be copied nor moved. Handles are
  36391. * the way to go to keep references to them.
  36392. *
  36393. * @tparam Resource Type of resource managed by a handle.
  36394. */
  36395. template<typename Resource>
  36396. class resource_handle {
  36397. /*! @brief Resource handles are friends with each other. */
  36398. template<typename>
  36399. friend class resource_handle;
  36400. public:
  36401. /*! @brief Default constructor. */
  36402. resource_handle() ENTT_NOEXCEPT = default;
  36403. /**
  36404. * @brief Creates a handle from a shared pointer, namely a resource.
  36405. * @param res A pointer to a properly initialized resource.
  36406. */
  36407. resource_handle(std::shared_ptr<Resource> res) ENTT_NOEXCEPT
  36408. : resource{std::move(res)}
  36409. {}
  36410. /**
  36411. * @brief Copy constructor.
  36412. * @param other The instance to copy from.
  36413. */
  36414. resource_handle(const resource_handle<Resource> &other) ENTT_NOEXCEPT = default;
  36415. /**
  36416. * @brief Move constructor.
  36417. * @param other The instance to move from.
  36418. */
  36419. resource_handle(resource_handle<Resource> &&other) ENTT_NOEXCEPT = default;
  36420. /**
  36421. * @brief Copy constructs a handle which shares ownership of the resource.
  36422. * @tparam Other Type of resource managed by the received handle.
  36423. * @param other The handle to copy from.
  36424. */
  36425. template<typename Other, typename = std::enable_if_t<!std::is_same_v<Other, Resource> && std::is_base_of_v<Resource, Other>>>
  36426. resource_handle(const resource_handle<Other> &other) ENTT_NOEXCEPT
  36427. : resource{other.resource}
  36428. {}
  36429. /**
  36430. * @brief Move constructs a handle which takes ownership of the resource.
  36431. * @tparam Other Type of resource managed by the received handle.
  36432. * @param other The handle to move from.
  36433. */
  36434. template<typename Other, typename = std::enable_if_t<!std::is_same_v<Other, Resource> && std::is_base_of_v<Resource, Other>>>
  36435. resource_handle(resource_handle<Other> &&other) ENTT_NOEXCEPT
  36436. : resource{std::move(other.resource)}
  36437. {}
  36438. /**
  36439. * @brief Copy assignment operator.
  36440. * @param other The instance to copy from.
  36441. * @return This resource handle.
  36442. */
  36443. resource_handle & operator=(const resource_handle<Resource> &other) ENTT_NOEXCEPT = default;
  36444. /**
  36445. * @brief Move assignment operator.
  36446. * @param other The instance to move from.
  36447. * @return This resource handle.
  36448. */
  36449. resource_handle & operator=(resource_handle<Resource> &&other) ENTT_NOEXCEPT = default;
  36450. /**
  36451. * @brief Copy assignment operator from foreign handle.
  36452. * @tparam Other Type of resource managed by the received handle.
  36453. * @param other The handle to copy from.
  36454. * @return This resource handle.
  36455. */
  36456. template<typename Other>
  36457. std::enable_if_t<!std::is_same_v<Other, Resource> && std::is_base_of_v<Resource, Other>, resource_handle &>
  36458. operator=(const resource_handle<Other> &other) ENTT_NOEXCEPT {
  36459. resource = other.resource;
  36460. return *this;
  36461. }
  36462. /**
  36463. * @brief Move assignment operator from foreign handle.
  36464. * @tparam Other Type of resource managed by the received handle.
  36465. * @param other The handle to move from.
  36466. * @return This resource handle.
  36467. */
  36468. template<typename Other>
  36469. std::enable_if_t<!std::is_same_v<Other, Resource> && std::is_base_of_v<Resource, Other>, resource_handle &>
  36470. operator=(resource_handle<Other> &&other) ENTT_NOEXCEPT {
  36471. resource = std::move(other.resource);
  36472. return *this;
  36473. }
  36474. /**
  36475. * @brief Gets a reference to the managed resource.
  36476. *
  36477. * @warning
  36478. * The behavior is undefined if the handle doesn't contain a resource.
  36479. *
  36480. * @return A reference to the managed resource.
  36481. */
  36482. [[nodiscard]] const Resource & get() const ENTT_NOEXCEPT {
  36483. ENTT_ASSERT(static_cast<bool>(resource), "Invalid resource");
  36484. return *resource;
  36485. }
  36486. /*! @copydoc get */
  36487. [[nodiscard]] Resource & get() ENTT_NOEXCEPT {
  36488. return const_cast<Resource &>(std::as_const(*this).get());
  36489. }
  36490. /*! @copydoc get */
  36491. [[nodiscard]] operator const Resource & () const ENTT_NOEXCEPT {
  36492. return get();
  36493. }
  36494. /*! @copydoc get */
  36495. [[nodiscard]] operator Resource & () ENTT_NOEXCEPT {
  36496. return get();
  36497. }
  36498. /*! @copydoc get */
  36499. [[nodiscard]] const Resource & operator *() const ENTT_NOEXCEPT {
  36500. return get();
  36501. }
  36502. /*! @copydoc get */
  36503. [[nodiscard]] Resource & operator *() ENTT_NOEXCEPT {
  36504. return get();
  36505. }
  36506. /**
  36507. * @brief Gets a pointer to the managed resource.
  36508. *
  36509. * @warning
  36510. * The behavior is undefined if the handle doesn't contain a resource.
  36511. *
  36512. * @return A pointer to the managed resource or `nullptr` if the handle
  36513. * contains no resource at all.
  36514. */
  36515. [[nodiscard]] const Resource * operator->() const ENTT_NOEXCEPT {
  36516. return resource.get();
  36517. }
  36518. /*! @copydoc operator-> */
  36519. [[nodiscard]] Resource * operator->() ENTT_NOEXCEPT {
  36520. return const_cast<Resource *>(std::as_const(*this).operator->());
  36521. }
  36522. /**
  36523. * @brief Returns true if a handle contains a resource, false otherwise.
  36524. * @return True if the handle contains a resource, false otherwise.
  36525. */
  36526. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  36527. return static_cast<bool>(resource);
  36528. }
  36529. private:
  36530. std::shared_ptr<Resource> resource;
  36531. };
  36532. }
  36533. #endif
  36534. // #include "resource/loader.hpp"
  36535. #ifndef ENTT_RESOURCE_LOADER_HPP
  36536. #define ENTT_RESOURCE_LOADER_HPP
  36537. // #include "fwd.hpp"
  36538. // #include "handle.hpp"
  36539. namespace entt {
  36540. /**
  36541. * @brief Base class for resource loaders.
  36542. *
  36543. * Resource loaders must inherit from this class and stay true to the CRTP
  36544. * idiom. Moreover, a resource loader must expose a public, const member
  36545. * function named `load` that accepts a variable number of arguments and returns
  36546. * a handle to the resource just created.<br/>
  36547. * As an example:
  36548. *
  36549. * @code{.cpp}
  36550. * struct my_resource {};
  36551. *
  36552. * struct my_loader: entt::resource_loader<my_loader, my_resource> {
  36553. * resource_handle<my_resource> load(int value) const {
  36554. * // use the integer value somehow
  36555. * return std::make_shared<my_resource>();
  36556. * }
  36557. * };
  36558. * @endcode
  36559. *
  36560. * In general, resource loaders should not have a state or retain data of any
  36561. * type. They should let the cache manage their resources instead.
  36562. *
  36563. * @note
  36564. * Base class and CRTP idiom aren't strictly required with the current
  36565. * implementation. One could argue that a cache can easily work with loaders of
  36566. * any type. However, future changes won't be breaking ones by forcing the use
  36567. * of a base class today and that's why the model is already in its place.
  36568. *
  36569. * @tparam Loader Type of the derived class.
  36570. * @tparam Resource Type of resource for which to use the loader.
  36571. */
  36572. template<typename Loader, typename Resource>
  36573. class resource_loader {
  36574. /*! @brief Resource loaders are friends of their caches. */
  36575. template<typename Other>
  36576. friend struct resource_cache;
  36577. /**
  36578. * @brief Loads the resource and returns it.
  36579. * @tparam Args Types of arguments for the loader.
  36580. * @param args Arguments for the loader.
  36581. * @return The resource just loaded or an empty pointer in case of errors.
  36582. */
  36583. template<typename... Args>
  36584. [[nodiscard]] resource_handle<Resource> get(Args &&... args) const {
  36585. return static_cast<const Loader *>(this)->load(std::forward<Args>(args)...);
  36586. }
  36587. };
  36588. }
  36589. #endif
  36590. // #include "signal/delegate.hpp"
  36591. #ifndef ENTT_SIGNAL_DELEGATE_HPP
  36592. #define ENTT_SIGNAL_DELEGATE_HPP
  36593. #include <tuple>
  36594. #include <cstddef>
  36595. #include <utility>
  36596. #include <functional>
  36597. #include <type_traits>
  36598. // #include "../core/type_traits.hpp"
  36599. #ifndef ENTT_CORE_TYPE_TRAITS_HPP
  36600. #define ENTT_CORE_TYPE_TRAITS_HPP
  36601. #include <cstddef>
  36602. #include <iterator>
  36603. #include <type_traits>
  36604. #include <utility>
  36605. // #include "../config/config.h"
  36606. #ifndef ENTT_CONFIG_CONFIG_H
  36607. #define ENTT_CONFIG_CONFIG_H
  36608. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  36609. # define ENTT_NOEXCEPT noexcept
  36610. # define ENTT_THROW throw
  36611. # define ENTT_TRY try
  36612. # define ENTT_CATCH catch(...)
  36613. #else
  36614. # define ENTT_NOEXCEPT
  36615. # define ENTT_THROW
  36616. # define ENTT_TRY if(true)
  36617. # define ENTT_CATCH if(false)
  36618. #endif
  36619. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  36620. # include <new>
  36621. # define ENTT_LAUNDER(expr) std::launder(expr)
  36622. #else
  36623. # define ENTT_LAUNDER(expr) expr
  36624. #endif
  36625. #ifndef ENTT_USE_ATOMIC
  36626. # define ENTT_MAYBE_ATOMIC(Type) Type
  36627. #else
  36628. # include <atomic>
  36629. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  36630. #endif
  36631. #ifndef ENTT_ID_TYPE
  36632. # include <cstdint>
  36633. # define ENTT_ID_TYPE std::uint32_t
  36634. #endif
  36635. #ifdef ENTT_SPARSE_PAGE
  36636. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  36637. #else
  36638. # define ENTT_SPARSE_PAGE 4096
  36639. #endif
  36640. #ifdef ENTT_PACKED_PAGE
  36641. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  36642. #else
  36643. # define ENTT_PACKED_PAGE 1024
  36644. #endif
  36645. #ifdef ENTT_DISABLE_ASSERT
  36646. # undef ENTT_ASSERT
  36647. # define ENTT_ASSERT(...) (void(0))
  36648. #elif !defined ENTT_ASSERT
  36649. # include <cassert>
  36650. # define ENTT_ASSERT(condition, ...) assert(condition)
  36651. #endif
  36652. #ifdef ENTT_NO_ETO
  36653. # include <type_traits>
  36654. # define ENTT_IGNORE_IF_EMPTY std::false_type
  36655. #else
  36656. # include <type_traits>
  36657. # define ENTT_IGNORE_IF_EMPTY std::true_type
  36658. #endif
  36659. #ifndef ENTT_STANDARD_CPP
  36660. # if defined __clang__ || defined __GNUC__
  36661. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  36662. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  36663. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  36664. # elif defined _MSC_VER
  36665. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  36666. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  36667. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  36668. # endif
  36669. #endif
  36670. #endif
  36671. // #include "fwd.hpp"
  36672. #ifndef ENTT_CORE_FWD_HPP
  36673. #define ENTT_CORE_FWD_HPP
  36674. #include <type_traits>
  36675. // #include "../config/config.h"
  36676. namespace entt {
  36677. template<std::size_t Len = sizeof(double[2]), std::size_t = alignof(typename std::aligned_storage_t<Len + !Len>)>
  36678. class basic_any;
  36679. /*! @brief Alias declaration for type identifiers. */
  36680. using id_type = ENTT_ID_TYPE;
  36681. /*! @brief Alias declaration for the most common use case. */
  36682. using any = basic_any<>;
  36683. }
  36684. #endif
  36685. namespace entt {
  36686. /**
  36687. * @brief Utility class to disambiguate overloaded functions.
  36688. * @tparam N Number of choices available.
  36689. */
  36690. template<std::size_t N>
  36691. struct choice_t
  36692. // Unfortunately, doxygen cannot parse such a construct.
  36693. /*! @cond TURN_OFF_DOXYGEN */
  36694. : choice_t<N-1>
  36695. /*! @endcond */
  36696. {};
  36697. /*! @copybrief choice_t */
  36698. template<>
  36699. struct choice_t<0> {};
  36700. /**
  36701. * @brief Variable template for the choice trick.
  36702. * @tparam N Number of choices available.
  36703. */
  36704. template<std::size_t N>
  36705. inline constexpr choice_t<N> choice{};
  36706. /**
  36707. * @brief Identity type trait.
  36708. *
  36709. * Useful to establish non-deduced contexts in template argument deduction
  36710. * (waiting for C++20) or to provide types through function arguments.
  36711. *
  36712. * @tparam Type A type.
  36713. */
  36714. template<typename Type>
  36715. struct type_identity {
  36716. /*! @brief Identity type. */
  36717. using type = Type;
  36718. };
  36719. /**
  36720. * @brief Helper type.
  36721. * @tparam Type A type.
  36722. */
  36723. template<typename Type>
  36724. using type_identity_t = typename type_identity<Type>::type;
  36725. /**
  36726. * @brief A type-only `sizeof` wrapper that returns 0 where `sizeof` complains.
  36727. * @tparam Type The type of which to return the size.
  36728. * @tparam The size of the type if `sizeof` accepts it, 0 otherwise.
  36729. */
  36730. template<typename Type, typename = void>
  36731. struct size_of: std::integral_constant<std::size_t, 0u> {};
  36732. /*! @copydoc size_of */
  36733. template<typename Type>
  36734. struct size_of<Type, std::void_t<decltype(sizeof(Type))>>
  36735. : std::integral_constant<std::size_t, sizeof(Type)>
  36736. {};
  36737. /**
  36738. * @brief Helper variable template.
  36739. * @tparam Type The type of which to return the size.
  36740. */
  36741. template<class Type>
  36742. inline constexpr std::size_t size_of_v = size_of<Type>::value;
  36743. /**
  36744. * @brief Using declaration to be used to _repeat_ the same type a number of
  36745. * times equal to the size of a given parameter pack.
  36746. * @tparam Type A type to repeat.
  36747. */
  36748. template<typename Type, typename>
  36749. using unpack_as_t = Type;
  36750. /**
  36751. * @brief Helper variable template to be used to _repeat_ the same value a
  36752. * number of times equal to the size of a given parameter pack.
  36753. * @tparam Value A value to repeat.
  36754. */
  36755. template<auto Value, typename>
  36756. inline constexpr auto unpack_as_v = Value;
  36757. /**
  36758. * @brief Wraps a static constant.
  36759. * @tparam Value A static constant.
  36760. */
  36761. template<auto Value>
  36762. using integral_constant = std::integral_constant<decltype(Value), Value>;
  36763. /**
  36764. * @brief Alias template to facilitate the creation of named values.
  36765. * @tparam Value A constant value at least convertible to `id_type`.
  36766. */
  36767. template<id_type Value>
  36768. using tag = integral_constant<Value>;
  36769. /**
  36770. * @brief A class to use to push around lists of types, nothing more.
  36771. * @tparam Type Types provided by the type list.
  36772. */
  36773. template<typename... Type>
  36774. struct type_list {
  36775. /*! @brief Type list type. */
  36776. using type = type_list;
  36777. /*! @brief Compile-time number of elements in the type list. */
  36778. static constexpr auto size = sizeof...(Type);
  36779. };
  36780. /*! @brief Primary template isn't defined on purpose. */
  36781. template<std::size_t, typename>
  36782. struct type_list_element;
  36783. /**
  36784. * @brief Provides compile-time indexed access to the types of a type list.
  36785. * @tparam Index Index of the type to return.
  36786. * @tparam Type First type provided by the type list.
  36787. * @tparam Other Other types provided by the type list.
  36788. */
  36789. template<std::size_t Index, typename Type, typename... Other>
  36790. struct type_list_element<Index, type_list<Type, Other...>>
  36791. : type_list_element<Index - 1u, type_list<Other...>>
  36792. {};
  36793. /**
  36794. * @brief Provides compile-time indexed access to the types of a type list.
  36795. * @tparam Type First type provided by the type list.
  36796. * @tparam Other Other types provided by the type list.
  36797. */
  36798. template<typename Type, typename... Other>
  36799. struct type_list_element<0u, type_list<Type, Other...>> {
  36800. /*! @brief Searched type. */
  36801. using type = Type;
  36802. };
  36803. /**
  36804. * @brief Helper type.
  36805. * @tparam Index Index of the type to return.
  36806. * @tparam List Type list to search into.
  36807. */
  36808. template<std::size_t Index, typename List>
  36809. using type_list_element_t = typename type_list_element<Index, List>::type;
  36810. /**
  36811. * @brief Concatenates multiple type lists.
  36812. * @tparam Type Types provided by the first type list.
  36813. * @tparam Other Types provided by the second type list.
  36814. * @return A type list composed by the types of both the type lists.
  36815. */
  36816. template<typename... Type, typename... Other>
  36817. constexpr type_list<Type..., Other...> operator+(type_list<Type...>, type_list<Other...>) { return {}; }
  36818. /*! @brief Primary template isn't defined on purpose. */
  36819. template<typename...>
  36820. struct type_list_cat;
  36821. /*! @brief Concatenates multiple type lists. */
  36822. template<>
  36823. struct type_list_cat<> {
  36824. /*! @brief A type list composed by the types of all the type lists. */
  36825. using type = type_list<>;
  36826. };
  36827. /**
  36828. * @brief Concatenates multiple type lists.
  36829. * @tparam Type Types provided by the first type list.
  36830. * @tparam Other Types provided by the second type list.
  36831. * @tparam List Other type lists, if any.
  36832. */
  36833. template<typename... Type, typename... Other, typename... List>
  36834. struct type_list_cat<type_list<Type...>, type_list<Other...>, List...> {
  36835. /*! @brief A type list composed by the types of all the type lists. */
  36836. using type = typename type_list_cat<type_list<Type..., Other...>, List...>::type;
  36837. };
  36838. /**
  36839. * @brief Concatenates multiple type lists.
  36840. * @tparam Type Types provided by the type list.
  36841. */
  36842. template<typename... Type>
  36843. struct type_list_cat<type_list<Type...>> {
  36844. /*! @brief A type list composed by the types of all the type lists. */
  36845. using type = type_list<Type...>;
  36846. };
  36847. /**
  36848. * @brief Helper type.
  36849. * @tparam List Type lists to concatenate.
  36850. */
  36851. template<typename... List>
  36852. using type_list_cat_t = typename type_list_cat<List...>::type;
  36853. /*! @brief Primary template isn't defined on purpose. */
  36854. template<typename>
  36855. struct type_list_unique;
  36856. /**
  36857. * @brief Removes duplicates types from a type list.
  36858. * @tparam Type One of the types provided by the given type list.
  36859. * @tparam Other The other types provided by the given type list.
  36860. */
  36861. template<typename Type, typename... Other>
  36862. struct type_list_unique<type_list<Type, Other...>> {
  36863. /*! @brief A type list without duplicate types. */
  36864. using type = std::conditional_t<
  36865. std::disjunction_v<std::is_same<Type, Other>...>,
  36866. typename type_list_unique<type_list<Other...>>::type,
  36867. type_list_cat_t<type_list<Type>, typename type_list_unique<type_list<Other...>>::type>
  36868. >;
  36869. };
  36870. /*! @brief Removes duplicates types from a type list. */
  36871. template<>
  36872. struct type_list_unique<type_list<>> {
  36873. /*! @brief A type list without duplicate types. */
  36874. using type = type_list<>;
  36875. };
  36876. /**
  36877. * @brief Helper type.
  36878. * @tparam Type A type list.
  36879. */
  36880. template<typename Type>
  36881. using type_list_unique_t = typename type_list_unique<Type>::type;
  36882. /**
  36883. * @brief Provides the member constant `value` to true if a type list contains a
  36884. * given type, false otherwise.
  36885. * @tparam List Type list.
  36886. * @tparam Type Type to look for.
  36887. */
  36888. template<typename List, typename Type>
  36889. struct type_list_contains;
  36890. /**
  36891. * @copybrief type_list_contains
  36892. * @tparam Type Types provided by the type list.
  36893. * @tparam Other Type to look for.
  36894. */
  36895. template<typename... Type, typename Other>
  36896. struct type_list_contains<type_list<Type...>, Other>: std::disjunction<std::is_same<Type, Other>...> {};
  36897. /**
  36898. * @brief Helper variable template.
  36899. * @tparam List Type list.
  36900. * @tparam Type Type to look for.
  36901. */
  36902. template<class List, typename Type>
  36903. inline constexpr bool type_list_contains_v = type_list_contains<List, Type>::value;
  36904. /*! @brief Primary template isn't defined on purpose. */
  36905. template<typename...>
  36906. struct type_list_diff;
  36907. /**
  36908. * @brief Computes the difference between two type lists.
  36909. * @tparam Type Types provided by the first type list.
  36910. * @tparam Other Types provided by the second type list.
  36911. */
  36912. template<typename... Type, typename... Other>
  36913. struct type_list_diff<type_list<Type...>, type_list<Other...>> {
  36914. /*! @brief A type list that is the difference between the two type lists. */
  36915. using type = type_list_cat_t<std::conditional_t<type_list_contains_v<type_list<Other...>, Type>, type_list<>, type_list<Type>>...>;
  36916. };
  36917. /**
  36918. * @brief Helper type.
  36919. * @tparam List Type lists between which to compute the difference.
  36920. */
  36921. template<typename... List>
  36922. using type_list_diff_t = typename type_list_diff<List...>::type;
  36923. /**
  36924. * @brief A class to use to push around lists of constant values, nothing more.
  36925. * @tparam Value Values provided by the value list.
  36926. */
  36927. template<auto... Value>
  36928. struct value_list {
  36929. /*! @brief Value list type. */
  36930. using type = value_list;
  36931. /*! @brief Compile-time number of elements in the value list. */
  36932. static constexpr auto size = sizeof...(Value);
  36933. };
  36934. /*! @brief Primary template isn't defined on purpose. */
  36935. template<std::size_t, typename>
  36936. struct value_list_element;
  36937. /**
  36938. * @brief Provides compile-time indexed access to the values of a value list.
  36939. * @tparam Index Index of the value to return.
  36940. * @tparam Value First value provided by the value list.
  36941. * @tparam Other Other values provided by the value list.
  36942. */
  36943. template<std::size_t Index, auto Value, auto... Other>
  36944. struct value_list_element<Index, value_list<Value, Other...>>
  36945. : value_list_element<Index - 1u, value_list<Other...>>
  36946. {};
  36947. /**
  36948. * @brief Provides compile-time indexed access to the types of a type list.
  36949. * @tparam Value First value provided by the value list.
  36950. * @tparam Other Other values provided by the value list.
  36951. */
  36952. template<auto Value, auto... Other>
  36953. struct value_list_element<0u, value_list<Value, Other...>> {
  36954. /*! @brief Searched value. */
  36955. static constexpr auto value = Value;
  36956. };
  36957. /**
  36958. * @brief Helper type.
  36959. * @tparam Index Index of the value to return.
  36960. * @tparam List Value list to search into.
  36961. */
  36962. template<std::size_t Index, typename List>
  36963. inline constexpr auto value_list_element_v = value_list_element<Index, List>::value;
  36964. /**
  36965. * @brief Concatenates multiple value lists.
  36966. * @tparam Value Values provided by the first value list.
  36967. * @tparam Other Values provided by the second value list.
  36968. * @return A value list composed by the values of both the value lists.
  36969. */
  36970. template<auto... Value, auto... Other>
  36971. constexpr value_list<Value..., Other...> operator+(value_list<Value...>, value_list<Other...>) { return {}; }
  36972. /*! @brief Primary template isn't defined on purpose. */
  36973. template<typename...>
  36974. struct value_list_cat;
  36975. /*! @brief Concatenates multiple value lists. */
  36976. template<>
  36977. struct value_list_cat<> {
  36978. /*! @brief A value list composed by the values of all the value lists. */
  36979. using type = value_list<>;
  36980. };
  36981. /**
  36982. * @brief Concatenates multiple value lists.
  36983. * @tparam Value Values provided by the first value list.
  36984. * @tparam Other Values provided by the second value list.
  36985. * @tparam List Other value lists, if any.
  36986. */
  36987. template<auto... Value, auto... Other, typename... List>
  36988. struct value_list_cat<value_list<Value...>, value_list<Other...>, List...> {
  36989. /*! @brief A value list composed by the values of all the value lists. */
  36990. using type = typename value_list_cat<value_list<Value..., Other...>, List...>::type;
  36991. };
  36992. /**
  36993. * @brief Concatenates multiple value lists.
  36994. * @tparam Value Values provided by the value list.
  36995. */
  36996. template<auto... Value>
  36997. struct value_list_cat<value_list<Value...>> {
  36998. /*! @brief A value list composed by the values of all the value lists. */
  36999. using type = value_list<Value...>;
  37000. };
  37001. /**
  37002. * @brief Helper type.
  37003. * @tparam List Value lists to concatenate.
  37004. */
  37005. template<typename... List>
  37006. using value_list_cat_t = typename value_list_cat<List...>::type;
  37007. /**
  37008. * @cond TURN_OFF_DOXYGEN
  37009. * Internal details not to be documented.
  37010. */
  37011. namespace internal {
  37012. template<typename>
  37013. [[nodiscard]] constexpr bool is_equality_comparable(...) { return false; }
  37014. template<typename Type>
  37015. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<0>)
  37016. -> decltype(std::declval<Type>() == std::declval<Type>()) { return true; }
  37017. template<typename Type>
  37018. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<1>)
  37019. -> decltype(std::declval<typename Type::value_type>(), std::declval<Type>() == std::declval<Type>()) {
  37020. if constexpr(std::is_same_v<typename Type::value_type, Type>) {
  37021. return is_equality_comparable<Type>(choice<0>);
  37022. } else {
  37023. return is_equality_comparable<typename Type::value_type>(choice<2>);
  37024. }
  37025. }
  37026. template<typename Type>
  37027. [[nodiscard]] constexpr auto is_equality_comparable(choice_t<2>)
  37028. -> decltype(std::declval<typename Type::mapped_type>(), std::declval<Type>() == std::declval<Type>()) {
  37029. return is_equality_comparable<typename Type::key_type>(choice<2>) && is_equality_comparable<typename Type::mapped_type>(choice<2>);
  37030. }
  37031. }
  37032. /**
  37033. * Internal details not to be documented.
  37034. * @endcond
  37035. */
  37036. /**
  37037. * @brief Provides the member constant `value` to true if a given type is
  37038. * equality comparable, false otherwise.
  37039. * @tparam Type The type to test.
  37040. */
  37041. template<typename Type, typename = void>
  37042. struct is_equality_comparable: std::bool_constant<internal::is_equality_comparable<Type>(choice<2>)> {};
  37043. /**
  37044. * @brief Helper variable template.
  37045. * @tparam Type The type to test.
  37046. */
  37047. template<class Type>
  37048. inline constexpr bool is_equality_comparable_v = is_equality_comparable<Type>::value;
  37049. /*! @brief Same as std::is_invocable, but with tuples. */
  37050. template<typename, typename>
  37051. struct is_applicable: std::false_type {};
  37052. /**
  37053. * @copybrief is_applicable
  37054. * @tparam Func A valid function type.
  37055. * @tparam Tuple Tuple-like type.
  37056. * @tparam Args The list of arguments to use to probe the function type.
  37057. */
  37058. template<typename Func, template<typename...> class Tuple, typename... Args>
  37059. struct is_applicable<Func, Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  37060. /**
  37061. * @copybrief is_applicable
  37062. * @tparam Func A valid function type.
  37063. * @tparam Tuple Tuple-like type.
  37064. * @tparam Args The list of arguments to use to probe the function type.
  37065. */
  37066. template<typename Func, template<typename...> class Tuple, typename... Args>
  37067. struct is_applicable<Func, const Tuple<Args...>>: std::is_invocable<Func, Args...> {};
  37068. /**
  37069. * @brief Helper variable template.
  37070. * @tparam Func A valid function type.
  37071. * @tparam Args The list of arguments to use to probe the function type.
  37072. */
  37073. template<typename Func, typename Args>
  37074. inline constexpr bool is_applicable_v = is_applicable<Func, Args>::value;
  37075. /*! @brief Same as std::is_invocable_r, but with tuples for arguments. */
  37076. template<typename, typename, typename>
  37077. struct is_applicable_r: std::false_type {};
  37078. /**
  37079. * @copybrief is_applicable_r
  37080. * @tparam Ret The type to which the return type of the function should be
  37081. * convertible.
  37082. * @tparam Func A valid function type.
  37083. * @tparam Args The list of arguments to use to probe the function type.
  37084. */
  37085. template<typename Ret, typename Func, typename... Args>
  37086. struct is_applicable_r<Ret, Func, std::tuple<Args...>>: std::is_invocable_r<Ret, Func, Args...> {};
  37087. /**
  37088. * @brief Helper variable template.
  37089. * @tparam Ret The type to which the return type of the function should be
  37090. * convertible.
  37091. * @tparam Func A valid function type.
  37092. * @tparam Args The list of arguments to use to probe the function type.
  37093. */
  37094. template<typename Ret, typename Func, typename Args>
  37095. inline constexpr bool is_applicable_r_v = is_applicable_r<Ret, Func, Args>::value;
  37096. /**
  37097. * @brief Provides the member constant `value` to true if a given type is
  37098. * complete, false otherwise.
  37099. * @tparam Type The type to test.
  37100. */
  37101. template<typename Type, typename = void>
  37102. struct is_complete: std::false_type {};
  37103. /*! @copydoc is_complete */
  37104. template<typename Type>
  37105. struct is_complete<Type, std::void_t<decltype(sizeof(Type))>>: std::true_type {};
  37106. /**
  37107. * @brief Helper variable template.
  37108. * @tparam Type The type to test.
  37109. */
  37110. template<typename Type>
  37111. inline constexpr bool is_complete_v = is_complete<Type>::value;
  37112. /**
  37113. * @brief Provides the member constant `value` to true if a given type is an
  37114. * iterator, false otherwise.
  37115. * @tparam Type The type to test.
  37116. */
  37117. template<typename Type, typename = void>
  37118. struct is_iterator: std::false_type {};
  37119. /*! @copydoc is_iterator */
  37120. template<typename Type>
  37121. struct is_iterator<Type, std::void_t<typename std::iterator_traits<Type>::iterator_category>>
  37122. : std::true_type
  37123. {};
  37124. /**
  37125. * @brief Helper variable template.
  37126. * @tparam Type The type to test.
  37127. */
  37128. template<typename Type>
  37129. inline constexpr bool is_iterator_v = is_iterator<Type>::value;
  37130. /**
  37131. * @brief Provides the member constant `value` to true if a given type is of the
  37132. * required iterator type, false otherwise.
  37133. * @tparam Type The type to test.
  37134. * @tparam It Required iterator type.
  37135. */
  37136. template<typename Type, typename It, typename = void>
  37137. struct is_iterator_type: std::false_type {};
  37138. /*! @copydoc is_iterator_type */
  37139. template<typename Type, typename It>
  37140. struct is_iterator_type<Type, It, std::enable_if_t<is_iterator_v<Type> && std::is_same_v<Type, It>>>
  37141. : std::true_type
  37142. {};
  37143. /*! @copydoc is_iterator_type */
  37144. template<typename Type, typename It>
  37145. struct is_iterator_type<Type, It, std::enable_if_t<!std::is_same_v<Type, It>, std::void_t<typename It::iterator_type>>>
  37146. : is_iterator_type<Type, typename It::iterator_type>
  37147. {};
  37148. /**
  37149. * @brief Helper variable template.
  37150. * @tparam Type The type to test.
  37151. * @tparam It Required iterator type.
  37152. */
  37153. template<typename Type, typename It>
  37154. inline constexpr bool is_iterator_type_v = is_iterator_type<Type, It>::value;
  37155. /**
  37156. * @brief Transcribes the constness of a type to another type.
  37157. * @tparam To The type to which to transcribe the constness.
  37158. * @tparam From The type from which to transcribe the constness.
  37159. */
  37160. template<typename To, typename From>
  37161. struct constness_as {
  37162. /*! @brief The type resulting from the transcription of the constness. */
  37163. using type = std::remove_const_t<To>;
  37164. };
  37165. /*! @copydoc constness_as */
  37166. template<typename To, typename From>
  37167. struct constness_as<To, const From> {
  37168. /*! @brief The type resulting from the transcription of the constness. */
  37169. using type = std::add_const_t<To>;
  37170. };
  37171. /**
  37172. * @brief Alias template to facilitate the transcription of the constness.
  37173. * @tparam To The type to which to transcribe the constness.
  37174. * @tparam From The type from which to transcribe the constness.
  37175. */
  37176. template<typename To, typename From>
  37177. using constness_as_t = typename constness_as<To, From>::type;
  37178. /**
  37179. * @brief Extracts the class of a non-static member object or function.
  37180. * @tparam Member A pointer to a non-static member object or function.
  37181. */
  37182. template<typename Member>
  37183. class member_class {
  37184. static_assert(std::is_member_pointer_v<Member>, "Invalid pointer type to non-static member object or function");
  37185. template<typename Class, typename Ret, typename... Args>
  37186. static Class * clazz(Ret(Class:: *)(Args...));
  37187. template<typename Class, typename Ret, typename... Args>
  37188. static Class * clazz(Ret(Class:: *)(Args...) const);
  37189. template<typename Class, typename Type>
  37190. static Class * clazz(Type Class:: *);
  37191. public:
  37192. /*! @brief The class of the given non-static member object or function. */
  37193. using type = std::remove_pointer_t<decltype(clazz(std::declval<Member>()))>;
  37194. };
  37195. /**
  37196. * @brief Helper type.
  37197. * @tparam Member A pointer to a non-static member object or function.
  37198. */
  37199. template<typename Member>
  37200. using member_class_t = typename member_class<Member>::type;
  37201. }
  37202. #endif
  37203. // #include "../config/config.h"
  37204. #ifndef ENTT_CONFIG_CONFIG_H
  37205. #define ENTT_CONFIG_CONFIG_H
  37206. #if defined(__cpp_exceptions) && !defined(ENTT_NOEXCEPTION)
  37207. # define ENTT_NOEXCEPT noexcept
  37208. # define ENTT_THROW throw
  37209. # define ENTT_TRY try
  37210. # define ENTT_CATCH catch(...)
  37211. #else
  37212. # define ENTT_NOEXCEPT
  37213. # define ENTT_THROW
  37214. # define ENTT_TRY if(true)
  37215. # define ENTT_CATCH if(false)
  37216. #endif
  37217. #if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606L
  37218. # include <new>
  37219. # define ENTT_LAUNDER(expr) std::launder(expr)
  37220. #else
  37221. # define ENTT_LAUNDER(expr) expr
  37222. #endif
  37223. #ifndef ENTT_USE_ATOMIC
  37224. # define ENTT_MAYBE_ATOMIC(Type) Type
  37225. #else
  37226. # include <atomic>
  37227. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  37228. #endif
  37229. #ifndef ENTT_ID_TYPE
  37230. # include <cstdint>
  37231. # define ENTT_ID_TYPE std::uint32_t
  37232. #endif
  37233. #ifdef ENTT_SPARSE_PAGE
  37234. static_assert(ENTT_SPARSE_PAGE && ((ENTT_SPARSE_PAGE & (ENTT_SPARSE_PAGE - 1)) == 0), "ENTT_SPARSE_PAGE must be a power of two");
  37235. #else
  37236. # define ENTT_SPARSE_PAGE 4096
  37237. #endif
  37238. #ifdef ENTT_PACKED_PAGE
  37239. static_assert(ENTT_PACKED_PAGE && ((ENTT_PACKED_PAGE & (ENTT_PACKED_PAGE - 1)) == 0), "ENTT_PACKED_PAGE must be a power of two");
  37240. #else
  37241. # define ENTT_PACKED_PAGE 1024
  37242. #endif
  37243. #ifdef ENTT_DISABLE_ASSERT
  37244. # undef ENTT_ASSERT
  37245. # define ENTT_ASSERT(...) (void(0))
  37246. #elif !defined ENTT_ASSERT
  37247. # include <cassert>
  37248. # define ENTT_ASSERT(condition, ...) assert(condition)
  37249. #endif
  37250. #ifdef ENTT_NO_ETO
  37251. # include <type_traits>
  37252. # define ENTT_IGNORE_IF_EMPTY std::false_type
  37253. #else
  37254. # include <type_traits>
  37255. # define ENTT_IGNORE_IF_EMPTY std::true_type
  37256. #endif
  37257. #ifndef ENTT_STANDARD_CPP
  37258. # if defined __clang__ || defined __GNUC__
  37259. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  37260. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  37261. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  37262. # elif defined _MSC_VER
  37263. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  37264. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  37265. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  37266. # endif
  37267. #endif
  37268. #endif
  37269. namespace entt {
  37270. /**
  37271. * @cond TURN_OFF_DOXYGEN
  37272. * Internal details not to be documented.
  37273. */
  37274. namespace internal {
  37275. template<typename Ret, typename... Args>
  37276. auto function_pointer(Ret(*)(Args...)) -> Ret(*)(Args...);
  37277. template<typename Ret, typename Type, typename... Args, typename Other>
  37278. auto function_pointer(Ret(*)(Type, Args...), Other &&) -> Ret(*)(Args...);
  37279. template<typename Class, typename Ret, typename... Args, typename... Other>
  37280. auto function_pointer(Ret(Class:: *)(Args...), Other &&...) -> Ret(*)(Args...);
  37281. template<typename Class, typename Ret, typename... Args, typename... Other>
  37282. auto function_pointer(Ret(Class:: *)(Args...) const, Other &&...) -> Ret(*)(Args...);
  37283. template<typename Class, typename Type, typename... Other>
  37284. auto function_pointer(Type Class:: *, Other &&...) -> Type(*)();
  37285. template<typename... Type>
  37286. using function_pointer_t = decltype(internal::function_pointer(std::declval<Type>()...));
  37287. template<typename... Class, typename Ret, typename... Args>
  37288. [[nodiscard]] constexpr auto index_sequence_for(Ret(*)(Args...)) {
  37289. return std::index_sequence_for<Class..., Args...>{};
  37290. }
  37291. }
  37292. /**
  37293. * Internal details not to be documented.
  37294. * @endcond
  37295. */
  37296. /*! @brief Used to wrap a function or a member of a specified type. */
  37297. template<auto>
  37298. struct connect_arg_t {};
  37299. /*! @brief Constant of type connect_arg_t used to disambiguate calls. */
  37300. template<auto Func>
  37301. inline constexpr connect_arg_t<Func> connect_arg{};
  37302. /**
  37303. * @brief Basic delegate implementation.
  37304. *
  37305. * Primary template isn't defined on purpose. All the specializations give a
  37306. * compile-time error unless the template parameter is a function type.
  37307. */
  37308. template<typename>
  37309. class delegate;
  37310. /**
  37311. * @brief Utility class to use to send around functions and members.
  37312. *
  37313. * Unmanaged delegate for function pointers and members. Users of this class are
  37314. * in charge of disconnecting instances before deleting them.
  37315. *
  37316. * A delegate can be used as a general purpose invoker without memory overhead
  37317. * for free functions possibly with payloads and bound or unbound members.
  37318. *
  37319. * @tparam Ret Return type of a function type.
  37320. * @tparam Args Types of arguments of a function type.
  37321. */
  37322. template<typename Ret, typename... Args>
  37323. class delegate<Ret(Args...)> {
  37324. template<auto Candidate, std::size_t... Index>
  37325. [[nodiscard]] auto wrap(std::index_sequence<Index...>) ENTT_NOEXCEPT {
  37326. return [](const void *, Args... args) -> Ret {
  37327. [[maybe_unused]] const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
  37328. return static_cast<Ret>(std::invoke(Candidate, std::forward<type_list_element_t<Index, type_list<Args...>>>(std::get<Index>(arguments))...));
  37329. };
  37330. }
  37331. template<auto Candidate, typename Type, std::size_t... Index>
  37332. [[nodiscard]] auto wrap(Type &, std::index_sequence<Index...>) ENTT_NOEXCEPT {
  37333. return [](const void *payload, Args... args) -> Ret {
  37334. [[maybe_unused]] const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
  37335. Type *curr = static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(payload));
  37336. return static_cast<Ret>(std::invoke(Candidate, *curr, std::forward<type_list_element_t<Index, type_list<Args...>>>(std::get<Index>(arguments))...));
  37337. };
  37338. }
  37339. template<auto Candidate, typename Type, std::size_t... Index>
  37340. [[nodiscard]] auto wrap(Type *, std::index_sequence<Index...>) ENTT_NOEXCEPT {
  37341. return [](const void *payload, Args... args) -> Ret {
  37342. [[maybe_unused]] const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
  37343. Type *curr = static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(payload));
  37344. return static_cast<Ret>(std::invoke(Candidate, curr, std::forward<type_list_element_t<Index, type_list<Args...>>>(std::get<Index>(arguments))...));
  37345. };
  37346. }
  37347. public:
  37348. /*! @brief Function type of the contained target. */
  37349. using function_type = Ret(const void *, Args...);
  37350. /*! @brief Function type of the delegate. */
  37351. using type = Ret(Args...);
  37352. /*! @brief Return type of the delegate. */
  37353. using result_type = Ret;
  37354. /*! @brief Default constructor. */
  37355. delegate() ENTT_NOEXCEPT
  37356. : fn{nullptr}, data{nullptr}
  37357. {}
  37358. /**
  37359. * @brief Constructs a delegate and connects a free function or an unbound
  37360. * member.
  37361. * @tparam Candidate Function or member to connect to the delegate.
  37362. */
  37363. template<auto Candidate>
  37364. delegate(connect_arg_t<Candidate>) ENTT_NOEXCEPT {
  37365. connect<Candidate>();
  37366. }
  37367. /**
  37368. * @brief Constructs a delegate and connects a free function with payload or
  37369. * a bound member.
  37370. * @tparam Candidate Function or member to connect to the delegate.
  37371. * @tparam Type Type of class or type of payload.
  37372. * @param value_or_instance A valid object that fits the purpose.
  37373. */
  37374. template<auto Candidate, typename Type>
  37375. delegate(connect_arg_t<Candidate>, Type &&value_or_instance) ENTT_NOEXCEPT {
  37376. connect<Candidate>(std::forward<Type>(value_or_instance));
  37377. }
  37378. /**
  37379. * @brief Constructs a delegate and connects an user defined function with
  37380. * optional payload.
  37381. * @param function Function to connect to the delegate.
  37382. * @param payload User defined arbitrary data.
  37383. */
  37384. delegate(function_type *function, const void *payload = nullptr) ENTT_NOEXCEPT {
  37385. connect(function, payload);
  37386. }
  37387. /**
  37388. * @brief Connects a free function or an unbound member to a delegate.
  37389. * @tparam Candidate Function or member to connect to the delegate.
  37390. */
  37391. template<auto Candidate>
  37392. void connect() ENTT_NOEXCEPT {
  37393. data = nullptr;
  37394. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Args...>) {
  37395. fn = [](const void *, Args... args) -> Ret {
  37396. return Ret(std::invoke(Candidate, std::forward<Args>(args)...));
  37397. };
  37398. } else if constexpr(std::is_member_pointer_v<decltype(Candidate)>) {
  37399. fn = wrap<Candidate>(internal::index_sequence_for<type_list_element_t<0, type_list<Args...>>>(internal::function_pointer_t<decltype(Candidate)>{}));
  37400. } else {
  37401. fn = wrap<Candidate>(internal::index_sequence_for(internal::function_pointer_t<decltype(Candidate)>{}));
  37402. }
  37403. }
  37404. /**
  37405. * @brief Connects a free function with payload or a bound member to a
  37406. * delegate.
  37407. *
  37408. * The delegate isn't responsible for the connected object or the payload.
  37409. * Users must always guarantee that the lifetime of the instance overcomes
  37410. * the one of the delegate.<br/>
  37411. * When used to connect a free function with payload, its signature must be
  37412. * such that the instance is the first argument before the ones used to
  37413. * define the delegate itself.
  37414. *
  37415. * @tparam Candidate Function or member to connect to the delegate.
  37416. * @tparam Type Type of class or type of payload.
  37417. * @param value_or_instance A valid reference that fits the purpose.
  37418. */
  37419. template<auto Candidate, typename Type>
  37420. void connect(Type &value_or_instance) ENTT_NOEXCEPT {
  37421. data = &value_or_instance;
  37422. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Type &, Args...>) {
  37423. fn = [](const void *payload, Args... args) -> Ret {
  37424. Type *curr = static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(payload));
  37425. return Ret(std::invoke(Candidate, *curr, std::forward<Args>(args)...));
  37426. };
  37427. } else {
  37428. fn = wrap<Candidate>(value_or_instance, internal::index_sequence_for(internal::function_pointer_t<decltype(Candidate), Type>{}));
  37429. }
  37430. }
  37431. /**
  37432. * @brief Connects a free function with payload or a bound member to a
  37433. * delegate.
  37434. *
  37435. * @sa connect(Type &)
  37436. *
  37437. * @tparam Candidate Function or member to connect to the delegate.
  37438. * @tparam Type Type of class or type of payload.
  37439. * @param value_or_instance A valid pointer that fits the purpose.
  37440. */
  37441. template<auto Candidate, typename Type>
  37442. void connect(Type *value_or_instance) ENTT_NOEXCEPT {
  37443. data = value_or_instance;
  37444. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Type *, Args...>) {
  37445. fn = [](const void *payload, Args... args) -> Ret {
  37446. Type *curr = static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(payload));
  37447. return Ret(std::invoke(Candidate, curr, std::forward<Args>(args)...));
  37448. };
  37449. } else {
  37450. fn = wrap<Candidate>(value_or_instance, internal::index_sequence_for(internal::function_pointer_t<decltype(Candidate), Type>{}));
  37451. }
  37452. }
  37453. /**
  37454. * @brief Connects an user defined function with optional payload to a
  37455. * delegate.
  37456. *
  37457. * The delegate isn't responsible for the connected object or the payload.
  37458. * Users must always guarantee that the lifetime of an instance overcomes
  37459. * the one of the delegate.<br/>
  37460. * The payload is returned as the first argument to the target function in
  37461. * all cases.
  37462. *
  37463. * @param function Function to connect to the delegate.
  37464. * @param payload User defined arbitrary data.
  37465. */
  37466. void connect(function_type *function, const void *payload = nullptr) ENTT_NOEXCEPT {
  37467. fn = function;
  37468. data = payload;
  37469. }
  37470. /**
  37471. * @brief Resets a delegate.
  37472. *
  37473. * After a reset, a delegate cannot be invoked anymore.
  37474. */
  37475. void reset() ENTT_NOEXCEPT {
  37476. fn = nullptr;
  37477. data = nullptr;
  37478. }
  37479. /**
  37480. * @brief Returns the instance or the payload linked to a delegate, if any.
  37481. * @return An opaque pointer to the underlying data.
  37482. */
  37483. [[nodiscard]] const void * instance() const ENTT_NOEXCEPT {
  37484. return data;
  37485. }
  37486. /**
  37487. * @brief Triggers a delegate.
  37488. *
  37489. * The delegate invokes the underlying function and returns the result.
  37490. *
  37491. * @warning
  37492. * Attempting to trigger an invalid delegate results in undefined
  37493. * behavior.
  37494. *
  37495. * @param args Arguments to use to invoke the underlying function.
  37496. * @return The value returned by the underlying function.
  37497. */
  37498. Ret operator()(Args... args) const {
  37499. ENTT_ASSERT(static_cast<bool>(*this), "Uninitialized delegate");
  37500. return fn(data, std::forward<Args>(args)...);
  37501. }
  37502. /**
  37503. * @brief Checks whether a delegate actually stores a listener.
  37504. * @return False if the delegate is empty, true otherwise.
  37505. */
  37506. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  37507. // no need to test also data
  37508. return !(fn == nullptr);
  37509. }
  37510. /**
  37511. * @brief Compares the contents of two delegates.
  37512. * @param other Delegate with which to compare.
  37513. * @return False if the two contents differ, true otherwise.
  37514. */
  37515. [[nodiscard]] bool operator==(const delegate<Ret(Args...)> &other) const ENTT_NOEXCEPT {
  37516. return fn == other.fn && data == other.data;
  37517. }
  37518. private:
  37519. function_type *fn;
  37520. const void *data;
  37521. };
  37522. /**
  37523. * @brief Compares the contents of two delegates.
  37524. * @tparam Ret Return type of a function type.
  37525. * @tparam Args Types of arguments of a function type.
  37526. * @param lhs A valid delegate object.
  37527. * @param rhs A valid delegate object.
  37528. * @return True if the two contents differ, false otherwise.
  37529. */
  37530. template<typename Ret, typename... Args>
  37531. [[nodiscard]] bool operator!=(const delegate<Ret(Args...)> &lhs, const delegate<Ret(Args...)> &rhs) ENTT_NOEXCEPT {
  37532. return !(lhs == rhs);
  37533. }
  37534. /**
  37535. * @brief Deduction guide.
  37536. * @tparam Candidate Function or member to connect to the delegate.
  37537. */
  37538. template<auto Candidate>
  37539. delegate(connect_arg_t<Candidate>)
  37540. -> delegate<std::remove_pointer_t<internal::function_pointer_t<decltype(Candidate)>>>;
  37541. /**
  37542. * @brief Deduction guide.
  37543. * @tparam Candidate Function or member to connect to the delegate.
  37544. * @tparam Type Type of class or type of payload.
  37545. */
  37546. template<auto Candidate, typename Type>
  37547. delegate(connect_arg_t<Candidate>, Type &&)
  37548. -> delegate<std::remove_pointer_t<internal::function_pointer_t<decltype(Candidate), Type>>>;
  37549. /**
  37550. * @brief Deduction guide.
  37551. * @tparam Ret Return type of a function type.
  37552. * @tparam Args Types of arguments of a function type.
  37553. */
  37554. template<typename Ret, typename... Args>
  37555. delegate(Ret(*)(const void *, Args...), const void * = nullptr)
  37556. -> delegate<Ret(Args...)>;
  37557. }
  37558. #endif
  37559. // #include "signal/dispatcher.hpp"
  37560. #ifndef ENTT_SIGNAL_DISPATCHER_HPP
  37561. #define ENTT_SIGNAL_DISPATCHER_HPP
  37562. #include <cstddef>
  37563. #include <memory>
  37564. #include <type_traits>
  37565. #include <utility>
  37566. #include <vector>
  37567. // #include "../config/config.h"
  37568. // #include "../core/fwd.hpp"
  37569. #ifndef ENTT_CORE_FWD_HPP
  37570. #define ENTT_CORE_FWD_HPP
  37571. #include <type_traits>
  37572. // #include "../config/config.h"
  37573. namespace entt {
  37574. template<std::size_t Len = sizeof(double[2]), std::size_t = alignof(typename std::aligned_storage_t<Len + !Len>)>
  37575. class basic_any;
  37576. /*! @brief Alias declaration for type identifiers. */
  37577. using id_type = ENTT_ID_TYPE;
  37578. /*! @brief Alias declaration for the most common use case. */
  37579. using any = basic_any<>;
  37580. }
  37581. #endif
  37582. // #include "../core/type_info.hpp"
  37583. #ifndef ENTT_CORE_TYPE_INFO_HPP
  37584. #define ENTT_CORE_TYPE_INFO_HPP
  37585. #include <string_view>
  37586. #include <type_traits>
  37587. // #include "../config/config.h"
  37588. // #include "../core/attribute.h"
  37589. #ifndef ENTT_CORE_ATTRIBUTE_H
  37590. #define ENTT_CORE_ATTRIBUTE_H
  37591. #ifndef ENTT_EXPORT
  37592. # if defined _WIN32 || defined __CYGWIN__ || defined _MSC_VER
  37593. # define ENTT_EXPORT __declspec(dllexport)
  37594. # define ENTT_IMPORT __declspec(dllimport)
  37595. # define ENTT_HIDDEN
  37596. # elif defined __GNUC__ && __GNUC__ >= 4
  37597. # define ENTT_EXPORT __attribute__((visibility("default")))
  37598. # define ENTT_IMPORT __attribute__((visibility("default")))
  37599. # define ENTT_HIDDEN __attribute__((visibility("hidden")))
  37600. # else /* Unsupported compiler */
  37601. # define ENTT_EXPORT
  37602. # define ENTT_IMPORT
  37603. # define ENTT_HIDDEN
  37604. # endif
  37605. #endif
  37606. #ifndef ENTT_API
  37607. # if defined ENTT_API_EXPORT
  37608. # define ENTT_API ENTT_EXPORT
  37609. # elif defined ENTT_API_IMPORT
  37610. # define ENTT_API ENTT_IMPORT
  37611. # else /* No API */
  37612. # define ENTT_API
  37613. # endif
  37614. #endif
  37615. #endif
  37616. // #include "hashed_string.hpp"
  37617. #ifndef ENTT_CORE_HASHED_STRING_HPP
  37618. #define ENTT_CORE_HASHED_STRING_HPP
  37619. #include <cstddef>
  37620. #include <cstdint>
  37621. // #include "../config/config.h"
  37622. // #include "fwd.hpp"
  37623. namespace entt {
  37624. /**
  37625. * @cond TURN_OFF_DOXYGEN
  37626. * Internal details not to be documented.
  37627. */
  37628. namespace internal {
  37629. template<typename>
  37630. struct fnv1a_traits;
  37631. template<>
  37632. struct fnv1a_traits<std::uint32_t> {
  37633. using type = std::uint32_t;
  37634. static constexpr std::uint32_t offset = 2166136261;
  37635. static constexpr std::uint32_t prime = 16777619;
  37636. };
  37637. template<>
  37638. struct fnv1a_traits<std::uint64_t> {
  37639. using type = std::uint64_t;
  37640. static constexpr std::uint64_t offset = 14695981039346656037ull;
  37641. static constexpr std::uint64_t prime = 1099511628211ull;
  37642. };
  37643. }
  37644. /**
  37645. * Internal details not to be documented.
  37646. * @endcond
  37647. */
  37648. /**
  37649. * @brief Zero overhead unique identifier.
  37650. *
  37651. * A hashed string is a compile-time tool that allows users to use
  37652. * human-readable identifers in the codebase while using their numeric
  37653. * counterparts at runtime.<br/>
  37654. * Because of that, a hashed string can also be used in constant expressions if
  37655. * required.
  37656. *
  37657. * @tparam Char Character type.
  37658. */
  37659. template<typename Char>
  37660. class basic_hashed_string {
  37661. using traits_type = internal::fnv1a_traits<id_type>;
  37662. struct const_wrapper {
  37663. // non-explicit constructor on purpose
  37664. constexpr const_wrapper(const Char *curr) ENTT_NOEXCEPT: str{curr} {}
  37665. const Char *str;
  37666. };
  37667. // Fowler–Noll–Vo hash function v. 1a - the good
  37668. [[nodiscard]] static constexpr id_type helper(const Char *curr) ENTT_NOEXCEPT {
  37669. auto value = traits_type::offset;
  37670. while(*curr != 0) {
  37671. value = (value ^ static_cast<traits_type::type>(*(curr++))) * traits_type::prime;
  37672. }
  37673. return value;
  37674. }
  37675. public:
  37676. /*! @brief Character type. */
  37677. using value_type = Char;
  37678. /*! @brief Unsigned integer type. */
  37679. using hash_type = id_type;
  37680. /**
  37681. * @brief Returns directly the numeric representation of a string view.
  37682. * @param str Human-readable identifer.
  37683. * @param size Length of the string to hash.
  37684. * @return The numeric representation of the string.
  37685. */
  37686. [[nodiscard]] static constexpr hash_type value(const value_type *str, std::size_t size) ENTT_NOEXCEPT {
  37687. id_type partial{traits_type::offset};
  37688. while(size--) { partial = (partial^(str++)[0])*traits_type::prime; }
  37689. return partial;
  37690. }
  37691. /**
  37692. * @brief Returns directly the numeric representation of a string.
  37693. *
  37694. * Forcing template resolution avoids implicit conversions. An
  37695. * human-readable identifier can be anything but a plain, old bunch of
  37696. * characters.<br/>
  37697. * Example of use:
  37698. * @code{.cpp}
  37699. * const auto value = basic_hashed_string<char>::to_value("my.png");
  37700. * @endcode
  37701. *
  37702. * @tparam N Number of characters of the identifier.
  37703. * @param str Human-readable identifer.
  37704. * @return The numeric representation of the string.
  37705. */
  37706. template<std::size_t N>
  37707. [[nodiscard]] static constexpr hash_type value(const value_type (&str)[N]) ENTT_NOEXCEPT {
  37708. return helper(str);
  37709. }
  37710. /**
  37711. * @brief Returns directly the numeric representation of a string.
  37712. * @param wrapper Helps achieving the purpose by relying on overloading.
  37713. * @return The numeric representation of the string.
  37714. */
  37715. [[nodiscard]] static hash_type value(const_wrapper wrapper) ENTT_NOEXCEPT {
  37716. return helper(wrapper.str);
  37717. }
  37718. /*! @brief Constructs an empty hashed string. */
  37719. constexpr basic_hashed_string() ENTT_NOEXCEPT
  37720. : str{nullptr}, hash{}
  37721. {}
  37722. /**
  37723. * @brief Constructs a hashed string from an array of const characters.
  37724. *
  37725. * Forcing template resolution avoids implicit conversions. An
  37726. * human-readable identifier can be anything but a plain, old bunch of
  37727. * characters.<br/>
  37728. * Example of use:
  37729. * @code{.cpp}
  37730. * basic_hashed_string<char> hs{"my.png"};
  37731. * @endcode
  37732. *
  37733. * @tparam N Number of characters of the identifier.
  37734. * @param curr Human-readable identifer.
  37735. */
  37736. template<std::size_t N>
  37737. constexpr basic_hashed_string(const value_type (&curr)[N]) ENTT_NOEXCEPT
  37738. : str{curr}, hash{helper(curr)}
  37739. {}
  37740. /**
  37741. * @brief Explicit constructor on purpose to avoid constructing a hashed
  37742. * string directly from a `const value_type *`.
  37743. * @param wrapper Helps achieving the purpose by relying on overloading.
  37744. */
  37745. explicit constexpr basic_hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
  37746. : str{wrapper.str}, hash{helper(wrapper.str)}
  37747. {}
  37748. /**
  37749. * @brief Returns the human-readable representation of a hashed string.
  37750. * @return The string used to initialize the instance.
  37751. */
  37752. [[nodiscard]] constexpr const value_type * data() const ENTT_NOEXCEPT {
  37753. return str;
  37754. }
  37755. /**
  37756. * @brief Returns the numeric representation of a hashed string.
  37757. * @return The numeric representation of the instance.
  37758. */
  37759. [[nodiscard]] constexpr hash_type value() const ENTT_NOEXCEPT {
  37760. return hash;
  37761. }
  37762. /*! @copydoc data */
  37763. [[nodiscard]] constexpr operator const value_type *() const ENTT_NOEXCEPT { return data(); }
  37764. /**
  37765. * @brief Returns the numeric representation of a hashed string.
  37766. * @return The numeric representation of the instance.
  37767. */
  37768. [[nodiscard]] constexpr operator hash_type() const ENTT_NOEXCEPT { return value(); }
  37769. /**
  37770. * @brief Compares two hashed strings.
  37771. * @param other Hashed string with which to compare.
  37772. * @return True if the two hashed strings are identical, false otherwise.
  37773. */
  37774. [[nodiscard]] constexpr bool operator==(const basic_hashed_string &other) const ENTT_NOEXCEPT {
  37775. return hash == other.hash;
  37776. }
  37777. private:
  37778. const value_type *str;
  37779. hash_type hash;
  37780. };
  37781. /**
  37782. * @brief Deduction guide.
  37783. *
  37784. * It allows to deduce the character type of the hashed string directly from a
  37785. * human-readable identifer provided to the constructor.
  37786. *
  37787. * @tparam Char Character type.
  37788. * @tparam N Number of characters of the identifier.
  37789. * @param str Human-readable identifer.
  37790. */
  37791. template<typename Char, std::size_t N>
  37792. basic_hashed_string(const Char (&str)[N])
  37793. -> basic_hashed_string<Char>;
  37794. /**
  37795. * @brief Compares two hashed strings.
  37796. * @tparam Char Character type.
  37797. * @param lhs A valid hashed string.
  37798. * @param rhs A valid hashed string.
  37799. * @return True if the two hashed strings are identical, false otherwise.
  37800. */
  37801. template<typename Char>
  37802. [[nodiscard]] constexpr bool operator!=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) ENTT_NOEXCEPT {
  37803. return !(lhs == rhs);
  37804. }
  37805. /*! @brief Aliases for common character types. */
  37806. using hashed_string = basic_hashed_string<char>;
  37807. /*! @brief Aliases for common character types. */
  37808. using hashed_wstring = basic_hashed_string<wchar_t>;
  37809. inline namespace literals {
  37810. /**
  37811. * @brief User defined literal for hashed strings.
  37812. * @param str The literal without its suffix.
  37813. * @return A properly initialized hashed string.
  37814. */
  37815. [[nodiscard]] constexpr entt::hashed_string operator"" _hs(const char *str, std::size_t) ENTT_NOEXCEPT {
  37816. return entt::hashed_string{str};
  37817. }
  37818. /**
  37819. * @brief User defined literal for hashed wstrings.
  37820. * @param str The literal without its suffix.
  37821. * @return A properly initialized hashed wstring.
  37822. */
  37823. [[nodiscard]] constexpr entt::hashed_wstring operator"" _hws(const wchar_t *str, std::size_t) ENTT_NOEXCEPT {
  37824. return entt::hashed_wstring{str};
  37825. }
  37826. }
  37827. }
  37828. #endif
  37829. // #include "fwd.hpp"
  37830. namespace entt {
  37831. /**
  37832. * @cond TURN_OFF_DOXYGEN
  37833. * Internal details not to be documented.
  37834. */
  37835. namespace internal {
  37836. struct ENTT_API type_seq final {
  37837. [[nodiscard]] static id_type next() ENTT_NOEXCEPT {
  37838. static ENTT_MAYBE_ATOMIC(id_type) value{};
  37839. return value++;
  37840. }
  37841. };
  37842. template<typename Type>
  37843. [[nodiscard]] constexpr auto stripped_type_name() ENTT_NOEXCEPT {
  37844. #if defined ENTT_PRETTY_FUNCTION
  37845. std::string_view pretty_function{ENTT_PRETTY_FUNCTION};
  37846. auto first = pretty_function.find_first_not_of(' ', pretty_function.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX)+1);
  37847. auto value = pretty_function.substr(first, pretty_function.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
  37848. return value;
  37849. #else
  37850. return std::string_view{""};
  37851. #endif
  37852. }
  37853. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  37854. [[nodiscard]] static constexpr std::string_view type_name(int) ENTT_NOEXCEPT {
  37855. constexpr auto value = stripped_type_name<Type>();
  37856. return value;
  37857. }
  37858. template<typename Type>
  37859. [[nodiscard]] static std::string_view type_name(char) ENTT_NOEXCEPT {
  37860. static const auto value = stripped_type_name<Type>();
  37861. return value;
  37862. }
  37863. template<typename Type, auto = stripped_type_name<Type>().find_first_of('.')>
  37864. [[nodiscard]] static constexpr id_type type_hash(int) ENTT_NOEXCEPT {
  37865. constexpr auto stripped = stripped_type_name<Type>();
  37866. constexpr auto value = hashed_string::value(stripped.data(), stripped.size());
  37867. return value;
  37868. }
  37869. template<typename Type>
  37870. [[nodiscard]] static id_type type_hash(char) ENTT_NOEXCEPT {
  37871. static const auto value = [](const auto stripped) {
  37872. return hashed_string::value(stripped.data(), stripped.size());
  37873. }(stripped_type_name<Type>());
  37874. return value;
  37875. }
  37876. }
  37877. /**
  37878. * Internal details not to be documented.
  37879. * @endcond
  37880. */
  37881. /**
  37882. * @brief Type sequential identifier.
  37883. * @tparam Type Type for which to generate a sequential identifier.
  37884. */
  37885. template<typename Type, typename = void>
  37886. struct ENTT_API type_seq final {
  37887. /**
  37888. * @brief Returns the sequential identifier of a given type.
  37889. * @return The sequential identifier of a given type.
  37890. */
  37891. [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
  37892. static const id_type value = internal::type_seq::next();
  37893. return value;
  37894. }
  37895. /*! @copydoc value */
  37896. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  37897. };
  37898. /**
  37899. * @brief Type hash.
  37900. * @tparam Type Type for which to generate a hash value.
  37901. */
  37902. template<typename Type, typename = void>
  37903. struct type_hash final {
  37904. /**
  37905. * @brief Returns the numeric representation of a given type.
  37906. * @return The numeric representation of the given type.
  37907. */
  37908. #if defined ENTT_PRETTY_FUNCTION
  37909. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  37910. return internal::type_hash<Type>(0);
  37911. #else
  37912. [[nodiscard]] static constexpr id_type value() ENTT_NOEXCEPT {
  37913. return type_seq<Type>::value();
  37914. #endif
  37915. }
  37916. /*! @copydoc value */
  37917. [[nodiscard]] constexpr operator id_type() const ENTT_NOEXCEPT { return value(); }
  37918. };
  37919. /**
  37920. * @brief Type name.
  37921. * @tparam Type Type for which to generate a name.
  37922. */
  37923. template<typename Type, typename = void>
  37924. struct type_name final {
  37925. /**
  37926. * @brief Returns the name of a given type.
  37927. * @return The name of the given type.
  37928. */
  37929. [[nodiscard]] static constexpr std::string_view value() ENTT_NOEXCEPT {
  37930. return internal::type_name<Type>(0);
  37931. }
  37932. /*! @copydoc value */
  37933. [[nodiscard]] constexpr operator std::string_view() const ENTT_NOEXCEPT { return value(); }
  37934. };
  37935. /*! @brief Implementation specific information about a type. */
  37936. class type_info final {
  37937. template<typename>
  37938. friend type_info type_id() ENTT_NOEXCEPT;
  37939. type_info(id_type seq_v, id_type hash_v, std::string_view name_v) ENTT_NOEXCEPT
  37940. : seq_value{seq_v},
  37941. hash_value{hash_v},
  37942. name_value{name_v}
  37943. {}
  37944. public:
  37945. /*! @brief Default constructor. */
  37946. type_info() ENTT_NOEXCEPT
  37947. : type_info({}, {}, {})
  37948. {}
  37949. /*! @brief Default copy constructor. */
  37950. type_info(const type_info &) ENTT_NOEXCEPT = default;
  37951. /*! @brief Default move constructor. */
  37952. type_info(type_info &&) ENTT_NOEXCEPT = default;
  37953. /**
  37954. * @brief Default copy assignment operator.
  37955. * @return This type info object.
  37956. */
  37957. type_info & operator=(const type_info &) ENTT_NOEXCEPT = default;
  37958. /**
  37959. * @brief Default move assignment operator.
  37960. * @return This type info object.
  37961. */
  37962. type_info & operator=(type_info &&) ENTT_NOEXCEPT = default;
  37963. /**
  37964. * @brief Checks if a type info object is properly initialized.
  37965. * @return True if the object is properly initialized, false otherwise.
  37966. */
  37967. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  37968. return name_value.data() != nullptr;
  37969. }
  37970. /**
  37971. * @brief Type sequential identifier.
  37972. * @return Type sequential identifier.
  37973. */
  37974. [[nodiscard]] id_type seq() const ENTT_NOEXCEPT {
  37975. return seq_value;
  37976. }
  37977. /**
  37978. * @brief Type hash.
  37979. * @return Type hash.
  37980. */
  37981. [[nodiscard]] id_type hash() const ENTT_NOEXCEPT {
  37982. return hash_value;
  37983. }
  37984. /**
  37985. * @brief Type name.
  37986. * @return Type name.
  37987. */
  37988. [[nodiscard]] std::string_view name() const ENTT_NOEXCEPT {
  37989. return name_value;
  37990. }
  37991. /**
  37992. * @brief Compares the contents of two type info objects.
  37993. * @param other Object with which to compare.
  37994. * @return False if the two contents differ, true otherwise.
  37995. */
  37996. [[nodiscard]] bool operator==(const type_info &other) const ENTT_NOEXCEPT {
  37997. return hash_value == other.hash_value;
  37998. }
  37999. private:
  38000. id_type seq_value;
  38001. id_type hash_value;
  38002. std::string_view name_value;
  38003. };
  38004. /**
  38005. * @brief Compares the contents of two type info objects.
  38006. * @param lhs A type info object.
  38007. * @param rhs A type info object.
  38008. * @return True if the two contents differ, false otherwise.
  38009. */
  38010. [[nodiscard]] inline bool operator!=(const type_info &lhs, const type_info &rhs) ENTT_NOEXCEPT {
  38011. return !(lhs == rhs);
  38012. }
  38013. /**
  38014. * @brief Returns the type info object for a given type.
  38015. * @tparam Type Type for which to generate a type info object.
  38016. * @return The type info object for the given type.
  38017. */
  38018. template<typename Type>
  38019. [[nodiscard]] type_info type_id() ENTT_NOEXCEPT {
  38020. return type_info{
  38021. type_seq<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  38022. type_hash<std::remove_cv_t<std::remove_reference_t<Type>>>::value(),
  38023. type_name<std::remove_cv_t<std::remove_reference_t<Type>>>::value()
  38024. };
  38025. }
  38026. }
  38027. #endif
  38028. // #include "sigh.hpp"
  38029. #ifndef ENTT_SIGNAL_SIGH_HPP
  38030. #define ENTT_SIGNAL_SIGH_HPP
  38031. #include <vector>
  38032. #include <utility>
  38033. #include <iterator>
  38034. #include <algorithm>
  38035. #include <functional>
  38036. #include <type_traits>
  38037. // #include "../config/config.h"
  38038. // #include "delegate.hpp"
  38039. #ifndef ENTT_SIGNAL_DELEGATE_HPP
  38040. #define ENTT_SIGNAL_DELEGATE_HPP
  38041. #include <tuple>
  38042. #include <cstddef>
  38043. #include <utility>
  38044. #include <functional>
  38045. #include <type_traits>
  38046. // #include "../core/type_traits.hpp"
  38047. // #include "../config/config.h"
  38048. namespace entt {
  38049. /**
  38050. * @cond TURN_OFF_DOXYGEN
  38051. * Internal details not to be documented.
  38052. */
  38053. namespace internal {
  38054. template<typename Ret, typename... Args>
  38055. auto function_pointer(Ret(*)(Args...)) -> Ret(*)(Args...);
  38056. template<typename Ret, typename Type, typename... Args, typename Other>
  38057. auto function_pointer(Ret(*)(Type, Args...), Other &&) -> Ret(*)(Args...);
  38058. template<typename Class, typename Ret, typename... Args, typename... Other>
  38059. auto function_pointer(Ret(Class:: *)(Args...), Other &&...) -> Ret(*)(Args...);
  38060. template<typename Class, typename Ret, typename... Args, typename... Other>
  38061. auto function_pointer(Ret(Class:: *)(Args...) const, Other &&...) -> Ret(*)(Args...);
  38062. template<typename Class, typename Type, typename... Other>
  38063. auto function_pointer(Type Class:: *, Other &&...) -> Type(*)();
  38064. template<typename... Type>
  38065. using function_pointer_t = decltype(internal::function_pointer(std::declval<Type>()...));
  38066. template<typename... Class, typename Ret, typename... Args>
  38067. [[nodiscard]] constexpr auto index_sequence_for(Ret(*)(Args...)) {
  38068. return std::index_sequence_for<Class..., Args...>{};
  38069. }
  38070. }
  38071. /**
  38072. * Internal details not to be documented.
  38073. * @endcond
  38074. */
  38075. /*! @brief Used to wrap a function or a member of a specified type. */
  38076. template<auto>
  38077. struct connect_arg_t {};
  38078. /*! @brief Constant of type connect_arg_t used to disambiguate calls. */
  38079. template<auto Func>
  38080. inline constexpr connect_arg_t<Func> connect_arg{};
  38081. /**
  38082. * @brief Basic delegate implementation.
  38083. *
  38084. * Primary template isn't defined on purpose. All the specializations give a
  38085. * compile-time error unless the template parameter is a function type.
  38086. */
  38087. template<typename>
  38088. class delegate;
  38089. /**
  38090. * @brief Utility class to use to send around functions and members.
  38091. *
  38092. * Unmanaged delegate for function pointers and members. Users of this class are
  38093. * in charge of disconnecting instances before deleting them.
  38094. *
  38095. * A delegate can be used as a general purpose invoker without memory overhead
  38096. * for free functions possibly with payloads and bound or unbound members.
  38097. *
  38098. * @tparam Ret Return type of a function type.
  38099. * @tparam Args Types of arguments of a function type.
  38100. */
  38101. template<typename Ret, typename... Args>
  38102. class delegate<Ret(Args...)> {
  38103. template<auto Candidate, std::size_t... Index>
  38104. [[nodiscard]] auto wrap(std::index_sequence<Index...>) ENTT_NOEXCEPT {
  38105. return [](const void *, Args... args) -> Ret {
  38106. [[maybe_unused]] const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
  38107. return static_cast<Ret>(std::invoke(Candidate, std::forward<type_list_element_t<Index, type_list<Args...>>>(std::get<Index>(arguments))...));
  38108. };
  38109. }
  38110. template<auto Candidate, typename Type, std::size_t... Index>
  38111. [[nodiscard]] auto wrap(Type &, std::index_sequence<Index...>) ENTT_NOEXCEPT {
  38112. return [](const void *payload, Args... args) -> Ret {
  38113. [[maybe_unused]] const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
  38114. Type *curr = static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(payload));
  38115. return static_cast<Ret>(std::invoke(Candidate, *curr, std::forward<type_list_element_t<Index, type_list<Args...>>>(std::get<Index>(arguments))...));
  38116. };
  38117. }
  38118. template<auto Candidate, typename Type, std::size_t... Index>
  38119. [[nodiscard]] auto wrap(Type *, std::index_sequence<Index...>) ENTT_NOEXCEPT {
  38120. return [](const void *payload, Args... args) -> Ret {
  38121. [[maybe_unused]] const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
  38122. Type *curr = static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(payload));
  38123. return static_cast<Ret>(std::invoke(Candidate, curr, std::forward<type_list_element_t<Index, type_list<Args...>>>(std::get<Index>(arguments))...));
  38124. };
  38125. }
  38126. public:
  38127. /*! @brief Function type of the contained target. */
  38128. using function_type = Ret(const void *, Args...);
  38129. /*! @brief Function type of the delegate. */
  38130. using type = Ret(Args...);
  38131. /*! @brief Return type of the delegate. */
  38132. using result_type = Ret;
  38133. /*! @brief Default constructor. */
  38134. delegate() ENTT_NOEXCEPT
  38135. : fn{nullptr}, data{nullptr}
  38136. {}
  38137. /**
  38138. * @brief Constructs a delegate and connects a free function or an unbound
  38139. * member.
  38140. * @tparam Candidate Function or member to connect to the delegate.
  38141. */
  38142. template<auto Candidate>
  38143. delegate(connect_arg_t<Candidate>) ENTT_NOEXCEPT {
  38144. connect<Candidate>();
  38145. }
  38146. /**
  38147. * @brief Constructs a delegate and connects a free function with payload or
  38148. * a bound member.
  38149. * @tparam Candidate Function or member to connect to the delegate.
  38150. * @tparam Type Type of class or type of payload.
  38151. * @param value_or_instance A valid object that fits the purpose.
  38152. */
  38153. template<auto Candidate, typename Type>
  38154. delegate(connect_arg_t<Candidate>, Type &&value_or_instance) ENTT_NOEXCEPT {
  38155. connect<Candidate>(std::forward<Type>(value_or_instance));
  38156. }
  38157. /**
  38158. * @brief Constructs a delegate and connects an user defined function with
  38159. * optional payload.
  38160. * @param function Function to connect to the delegate.
  38161. * @param payload User defined arbitrary data.
  38162. */
  38163. delegate(function_type *function, const void *payload = nullptr) ENTT_NOEXCEPT {
  38164. connect(function, payload);
  38165. }
  38166. /**
  38167. * @brief Connects a free function or an unbound member to a delegate.
  38168. * @tparam Candidate Function or member to connect to the delegate.
  38169. */
  38170. template<auto Candidate>
  38171. void connect() ENTT_NOEXCEPT {
  38172. data = nullptr;
  38173. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Args...>) {
  38174. fn = [](const void *, Args... args) -> Ret {
  38175. return Ret(std::invoke(Candidate, std::forward<Args>(args)...));
  38176. };
  38177. } else if constexpr(std::is_member_pointer_v<decltype(Candidate)>) {
  38178. fn = wrap<Candidate>(internal::index_sequence_for<type_list_element_t<0, type_list<Args...>>>(internal::function_pointer_t<decltype(Candidate)>{}));
  38179. } else {
  38180. fn = wrap<Candidate>(internal::index_sequence_for(internal::function_pointer_t<decltype(Candidate)>{}));
  38181. }
  38182. }
  38183. /**
  38184. * @brief Connects a free function with payload or a bound member to a
  38185. * delegate.
  38186. *
  38187. * The delegate isn't responsible for the connected object or the payload.
  38188. * Users must always guarantee that the lifetime of the instance overcomes
  38189. * the one of the delegate.<br/>
  38190. * When used to connect a free function with payload, its signature must be
  38191. * such that the instance is the first argument before the ones used to
  38192. * define the delegate itself.
  38193. *
  38194. * @tparam Candidate Function or member to connect to the delegate.
  38195. * @tparam Type Type of class or type of payload.
  38196. * @param value_or_instance A valid reference that fits the purpose.
  38197. */
  38198. template<auto Candidate, typename Type>
  38199. void connect(Type &value_or_instance) ENTT_NOEXCEPT {
  38200. data = &value_or_instance;
  38201. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Type &, Args...>) {
  38202. fn = [](const void *payload, Args... args) -> Ret {
  38203. Type *curr = static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(payload));
  38204. return Ret(std::invoke(Candidate, *curr, std::forward<Args>(args)...));
  38205. };
  38206. } else {
  38207. fn = wrap<Candidate>(value_or_instance, internal::index_sequence_for(internal::function_pointer_t<decltype(Candidate), Type>{}));
  38208. }
  38209. }
  38210. /**
  38211. * @brief Connects a free function with payload or a bound member to a
  38212. * delegate.
  38213. *
  38214. * @sa connect(Type &)
  38215. *
  38216. * @tparam Candidate Function or member to connect to the delegate.
  38217. * @tparam Type Type of class or type of payload.
  38218. * @param value_or_instance A valid pointer that fits the purpose.
  38219. */
  38220. template<auto Candidate, typename Type>
  38221. void connect(Type *value_or_instance) ENTT_NOEXCEPT {
  38222. data = value_or_instance;
  38223. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Type *, Args...>) {
  38224. fn = [](const void *payload, Args... args) -> Ret {
  38225. Type *curr = static_cast<Type *>(const_cast<constness_as_t<void, Type> *>(payload));
  38226. return Ret(std::invoke(Candidate, curr, std::forward<Args>(args)...));
  38227. };
  38228. } else {
  38229. fn = wrap<Candidate>(value_or_instance, internal::index_sequence_for(internal::function_pointer_t<decltype(Candidate), Type>{}));
  38230. }
  38231. }
  38232. /**
  38233. * @brief Connects an user defined function with optional payload to a
  38234. * delegate.
  38235. *
  38236. * The delegate isn't responsible for the connected object or the payload.
  38237. * Users must always guarantee that the lifetime of an instance overcomes
  38238. * the one of the delegate.<br/>
  38239. * The payload is returned as the first argument to the target function in
  38240. * all cases.
  38241. *
  38242. * @param function Function to connect to the delegate.
  38243. * @param payload User defined arbitrary data.
  38244. */
  38245. void connect(function_type *function, const void *payload = nullptr) ENTT_NOEXCEPT {
  38246. fn = function;
  38247. data = payload;
  38248. }
  38249. /**
  38250. * @brief Resets a delegate.
  38251. *
  38252. * After a reset, a delegate cannot be invoked anymore.
  38253. */
  38254. void reset() ENTT_NOEXCEPT {
  38255. fn = nullptr;
  38256. data = nullptr;
  38257. }
  38258. /**
  38259. * @brief Returns the instance or the payload linked to a delegate, if any.
  38260. * @return An opaque pointer to the underlying data.
  38261. */
  38262. [[nodiscard]] const void * instance() const ENTT_NOEXCEPT {
  38263. return data;
  38264. }
  38265. /**
  38266. * @brief Triggers a delegate.
  38267. *
  38268. * The delegate invokes the underlying function and returns the result.
  38269. *
  38270. * @warning
  38271. * Attempting to trigger an invalid delegate results in undefined
  38272. * behavior.
  38273. *
  38274. * @param args Arguments to use to invoke the underlying function.
  38275. * @return The value returned by the underlying function.
  38276. */
  38277. Ret operator()(Args... args) const {
  38278. ENTT_ASSERT(static_cast<bool>(*this), "Uninitialized delegate");
  38279. return fn(data, std::forward<Args>(args)...);
  38280. }
  38281. /**
  38282. * @brief Checks whether a delegate actually stores a listener.
  38283. * @return False if the delegate is empty, true otherwise.
  38284. */
  38285. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  38286. // no need to test also data
  38287. return !(fn == nullptr);
  38288. }
  38289. /**
  38290. * @brief Compares the contents of two delegates.
  38291. * @param other Delegate with which to compare.
  38292. * @return False if the two contents differ, true otherwise.
  38293. */
  38294. [[nodiscard]] bool operator==(const delegate<Ret(Args...)> &other) const ENTT_NOEXCEPT {
  38295. return fn == other.fn && data == other.data;
  38296. }
  38297. private:
  38298. function_type *fn;
  38299. const void *data;
  38300. };
  38301. /**
  38302. * @brief Compares the contents of two delegates.
  38303. * @tparam Ret Return type of a function type.
  38304. * @tparam Args Types of arguments of a function type.
  38305. * @param lhs A valid delegate object.
  38306. * @param rhs A valid delegate object.
  38307. * @return True if the two contents differ, false otherwise.
  38308. */
  38309. template<typename Ret, typename... Args>
  38310. [[nodiscard]] bool operator!=(const delegate<Ret(Args...)> &lhs, const delegate<Ret(Args...)> &rhs) ENTT_NOEXCEPT {
  38311. return !(lhs == rhs);
  38312. }
  38313. /**
  38314. * @brief Deduction guide.
  38315. * @tparam Candidate Function or member to connect to the delegate.
  38316. */
  38317. template<auto Candidate>
  38318. delegate(connect_arg_t<Candidate>)
  38319. -> delegate<std::remove_pointer_t<internal::function_pointer_t<decltype(Candidate)>>>;
  38320. /**
  38321. * @brief Deduction guide.
  38322. * @tparam Candidate Function or member to connect to the delegate.
  38323. * @tparam Type Type of class or type of payload.
  38324. */
  38325. template<auto Candidate, typename Type>
  38326. delegate(connect_arg_t<Candidate>, Type &&)
  38327. -> delegate<std::remove_pointer_t<internal::function_pointer_t<decltype(Candidate), Type>>>;
  38328. /**
  38329. * @brief Deduction guide.
  38330. * @tparam Ret Return type of a function type.
  38331. * @tparam Args Types of arguments of a function type.
  38332. */
  38333. template<typename Ret, typename... Args>
  38334. delegate(Ret(*)(const void *, Args...), const void * = nullptr)
  38335. -> delegate<Ret(Args...)>;
  38336. }
  38337. #endif
  38338. // #include "fwd.hpp"
  38339. #ifndef ENTT_SIGNAL_FWD_HPP
  38340. #define ENTT_SIGNAL_FWD_HPP
  38341. namespace entt {
  38342. template<typename>
  38343. class delegate;
  38344. class dispatcher;
  38345. template<typename>
  38346. class emitter;
  38347. class connection;
  38348. struct scoped_connection;
  38349. template<typename>
  38350. class sink;
  38351. template<typename>
  38352. class sigh;
  38353. }
  38354. #endif
  38355. namespace entt {
  38356. /**
  38357. * @brief Sink class.
  38358. *
  38359. * Primary template isn't defined on purpose. All the specializations give a
  38360. * compile-time error unless the template parameter is a function type.
  38361. *
  38362. * @tparam Function A valid function type.
  38363. */
  38364. template<typename Function>
  38365. class sink;
  38366. /**
  38367. * @brief Unmanaged signal handler.
  38368. *
  38369. * Primary template isn't defined on purpose. All the specializations give a
  38370. * compile-time error unless the template parameter is a function type.
  38371. *
  38372. * @tparam Function A valid function type.
  38373. */
  38374. template<typename Function>
  38375. class sigh;
  38376. /**
  38377. * @brief Unmanaged signal handler.
  38378. *
  38379. * It works directly with references to classes and pointers to member functions
  38380. * as well as pointers to free functions. Users of this class are in charge of
  38381. * disconnecting instances before deleting them.
  38382. *
  38383. * This class serves mainly two purposes:
  38384. *
  38385. * * Creating signals to use later to notify a bunch of listeners.
  38386. * * Collecting results from a set of functions like in a voting system.
  38387. *
  38388. * @tparam Ret Return type of a function type.
  38389. * @tparam Args Types of arguments of a function type.
  38390. */
  38391. template<typename Ret, typename... Args>
  38392. class sigh<Ret(Args...)> {
  38393. /*! @brief A sink is allowed to modify a signal. */
  38394. friend class sink<Ret(Args...)>;
  38395. public:
  38396. /*! @brief Unsigned integer type. */
  38397. using size_type = std::size_t;
  38398. /*! @brief Sink type. */
  38399. using sink_type = sink<Ret(Args...)>;
  38400. /**
  38401. * @brief Instance type when it comes to connecting member functions.
  38402. * @tparam Class Type of class to which the member function belongs.
  38403. */
  38404. template<typename Class>
  38405. using instance_type = Class *;
  38406. /**
  38407. * @brief Number of listeners connected to the signal.
  38408. * @return Number of listeners currently connected.
  38409. */
  38410. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  38411. return calls.size();
  38412. }
  38413. /**
  38414. * @brief Returns false if at least a listener is connected to the signal.
  38415. * @return True if the signal has no listeners connected, false otherwise.
  38416. */
  38417. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  38418. return calls.empty();
  38419. }
  38420. /**
  38421. * @brief Triggers a signal.
  38422. *
  38423. * All the listeners are notified. Order isn't guaranteed.
  38424. *
  38425. * @param args Arguments to use to invoke listeners.
  38426. */
  38427. void publish(Args... args) const {
  38428. for(auto &&call: std::as_const(calls)) {
  38429. call(args...);
  38430. }
  38431. }
  38432. /**
  38433. * @brief Collects return values from the listeners.
  38434. *
  38435. * The collector must expose a call operator with the following properties:
  38436. *
  38437. * * The return type is either `void` or such that it's convertible to
  38438. * `bool`. In the second case, a true value will stop the iteration.
  38439. * * The list of parameters is empty if `Ret` is `void`, otherwise it
  38440. * contains a single element such that `Ret` is convertible to it.
  38441. *
  38442. * @tparam Func Type of collector to use, if any.
  38443. * @param func A valid function object.
  38444. * @param args Arguments to use to invoke listeners.
  38445. */
  38446. template<typename Func>
  38447. void collect(Func func, Args... args) const {
  38448. for(auto &&call: calls) {
  38449. if constexpr(std::is_void_v<Ret>) {
  38450. if constexpr(std::is_invocable_r_v<bool, Func>) {
  38451. call(args...);
  38452. if(func()) { break; }
  38453. } else {
  38454. call(args...);
  38455. func();
  38456. }
  38457. } else {
  38458. if constexpr(std::is_invocable_r_v<bool, Func, Ret>) {
  38459. if(func(call(args...))) { break; }
  38460. } else {
  38461. func(call(args...));
  38462. }
  38463. }
  38464. }
  38465. }
  38466. private:
  38467. std::vector<delegate<Ret(Args...)>> calls;
  38468. };
  38469. /**
  38470. * @brief Connection class.
  38471. *
  38472. * Opaque object the aim of which is to allow users to release an already
  38473. * estabilished connection without having to keep a reference to the signal or
  38474. * the sink that generated it.
  38475. */
  38476. class connection {
  38477. /*! @brief A sink is allowed to create connection objects. */
  38478. template<typename>
  38479. friend class sink;
  38480. connection(delegate<void(void *)> fn, void *ref)
  38481. : disconnect{fn}, signal{ref}
  38482. {}
  38483. public:
  38484. /*! @brief Default constructor. */
  38485. connection() = default;
  38486. /**
  38487. * @brief Checks whether a connection is properly initialized.
  38488. * @return True if the connection is properly initialized, false otherwise.
  38489. */
  38490. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  38491. return static_cast<bool>(disconnect);
  38492. }
  38493. /*! @brief Breaks the connection. */
  38494. void release() {
  38495. if(disconnect) {
  38496. disconnect(signal);
  38497. disconnect.reset();
  38498. }
  38499. }
  38500. private:
  38501. delegate<void(void *)> disconnect;
  38502. void *signal{};
  38503. };
  38504. /**
  38505. * @brief Scoped connection class.
  38506. *
  38507. * Opaque object the aim of which is to allow users to release an already
  38508. * estabilished connection without having to keep a reference to the signal or
  38509. * the sink that generated it.<br/>
  38510. * A scoped connection automatically breaks the link between the two objects
  38511. * when it goes out of scope.
  38512. */
  38513. struct scoped_connection {
  38514. /*! @brief Default constructor. */
  38515. scoped_connection() = default;
  38516. /**
  38517. * @brief Constructs a scoped connection from a basic connection.
  38518. * @param other A valid connection object.
  38519. */
  38520. scoped_connection(const connection &other)
  38521. : conn{other}
  38522. {}
  38523. /*! @brief Default copy constructor, deleted on purpose. */
  38524. scoped_connection(const scoped_connection &) = delete;
  38525. /*! @brief Automatically breaks the link on destruction. */
  38526. ~scoped_connection() {
  38527. conn.release();
  38528. }
  38529. /**
  38530. * @brief Default copy assignment operator, deleted on purpose.
  38531. * @return This scoped connection.
  38532. */
  38533. scoped_connection & operator=(const scoped_connection &) = delete;
  38534. /**
  38535. * @brief Acquires a connection.
  38536. * @param other The connection object to acquire.
  38537. * @return This scoped connection.
  38538. */
  38539. scoped_connection & operator=(connection other) {
  38540. conn = std::move(other);
  38541. return *this;
  38542. }
  38543. /**
  38544. * @brief Checks whether a scoped connection is properly initialized.
  38545. * @return True if the connection is properly initialized, false otherwise.
  38546. */
  38547. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  38548. return static_cast<bool>(conn);
  38549. }
  38550. /*! @brief Breaks the connection. */
  38551. void release() {
  38552. conn.release();
  38553. }
  38554. private:
  38555. connection conn;
  38556. };
  38557. /**
  38558. * @brief Sink class.
  38559. *
  38560. * A sink is used to connect listeners to signals and to disconnect them.<br/>
  38561. * The function type for a listener is the one of the signal to which it
  38562. * belongs.
  38563. *
  38564. * The clear separation between a signal and a sink permits to store the former
  38565. * as private data member without exposing the publish functionality to the
  38566. * users of the class.
  38567. *
  38568. * @warning
  38569. * Lifetime of a sink must not overcome that of the signal to which it refers.
  38570. * In any other case, attempting to use a sink results in undefined behavior.
  38571. *
  38572. * @tparam Ret Return type of a function type.
  38573. * @tparam Args Types of arguments of a function type.
  38574. */
  38575. template<typename Ret, typename... Args>
  38576. class sink<Ret(Args...)> {
  38577. using signal_type = sigh<Ret(Args...)>;
  38578. using difference_type = typename std::iterator_traits<typename decltype(signal_type::calls)::iterator>::difference_type;
  38579. template<auto Candidate, typename Type>
  38580. static void release(Type value_or_instance, void *signal) {
  38581. sink{*static_cast<signal_type *>(signal)}.disconnect<Candidate>(value_or_instance);
  38582. }
  38583. template<auto Candidate>
  38584. static void release(void *signal) {
  38585. sink{*static_cast<signal_type *>(signal)}.disconnect<Candidate>();
  38586. }
  38587. public:
  38588. /**
  38589. * @brief Constructs a sink that is allowed to modify a given signal.
  38590. * @param ref A valid reference to a signal object.
  38591. */
  38592. sink(sigh<Ret(Args...)> &ref) ENTT_NOEXCEPT
  38593. : offset{},
  38594. signal{&ref}
  38595. {}
  38596. /**
  38597. * @brief Returns false if at least a listener is connected to the sink.
  38598. * @return True if the sink has no listeners connected, false otherwise.
  38599. */
  38600. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  38601. return signal->calls.empty();
  38602. }
  38603. /**
  38604. * @brief Returns a sink that connects before a given free function or an
  38605. * unbound member.
  38606. * @tparam Function A valid free function pointer.
  38607. * @return A properly initialized sink object.
  38608. */
  38609. template<auto Function>
  38610. [[nodiscard]] sink before() {
  38611. delegate<Ret(Args...)> call{};
  38612. call.template connect<Function>();
  38613. const auto &calls = signal->calls;
  38614. const auto it = std::find(calls.cbegin(), calls.cend(), std::move(call));
  38615. sink other{*this};
  38616. other.offset = std::distance(it, calls.cend());
  38617. return other;
  38618. }
  38619. /**
  38620. * @brief Returns a sink that connects before a free function with payload
  38621. * or a bound member.
  38622. * @tparam Candidate Member or free function to look for.
  38623. * @tparam Type Type of class or type of payload.
  38624. * @param value_or_instance A valid object that fits the purpose.
  38625. * @return A properly initialized sink object.
  38626. */
  38627. template<auto Candidate, typename Type>
  38628. [[nodiscard]] sink before(Type &&value_or_instance) {
  38629. delegate<Ret(Args...)> call{};
  38630. call.template connect<Candidate>(value_or_instance);
  38631. const auto &calls = signal->calls;
  38632. const auto it = std::find(calls.cbegin(), calls.cend(), std::move(call));
  38633. sink other{*this};
  38634. other.offset = std::distance(it, calls.cend());
  38635. return other;
  38636. }
  38637. /**
  38638. * @brief Returns a sink that connects before a given instance or specific
  38639. * payload.
  38640. * @tparam Type Type of class or type of payload.
  38641. * @param value_or_instance A valid object that fits the purpose.
  38642. * @return A properly initialized sink object.
  38643. */
  38644. template<typename Type>
  38645. [[nodiscard]] sink before(Type &value_or_instance) {
  38646. return before(&value_or_instance);
  38647. }
  38648. /**
  38649. * @brief Returns a sink that connects before a given instance or specific
  38650. * payload.
  38651. * @tparam Type Type of class or type of payload.
  38652. * @param value_or_instance A valid pointer that fits the purpose.
  38653. * @return A properly initialized sink object.
  38654. */
  38655. template<typename Type>
  38656. [[nodiscard]] sink before(Type *value_or_instance) {
  38657. sink other{*this};
  38658. if(value_or_instance) {
  38659. const auto &calls = signal->calls;
  38660. const auto it = std::find_if(calls.cbegin(), calls.cend(), [value_or_instance](const auto &delegate) {
  38661. return delegate.instance() == value_or_instance;
  38662. });
  38663. other.offset = std::distance(it, calls.cend());
  38664. }
  38665. return other;
  38666. }
  38667. /**
  38668. * @brief Returns a sink that connects before anything else.
  38669. * @return A properly initialized sink object.
  38670. */
  38671. [[nodiscard]] sink before() {
  38672. sink other{*this};
  38673. other.offset = signal->calls.size();
  38674. return other;
  38675. }
  38676. /**
  38677. * @brief Connects a free function or an unbound member to a signal.
  38678. *
  38679. * The signal handler performs checks to avoid multiple connections for the
  38680. * same function.
  38681. *
  38682. * @tparam Candidate Function or member to connect to the signal.
  38683. * @return A properly initialized connection object.
  38684. */
  38685. template<auto Candidate>
  38686. connection connect() {
  38687. disconnect<Candidate>();
  38688. delegate<Ret(Args...)> call{};
  38689. call.template connect<Candidate>();
  38690. signal->calls.insert(signal->calls.end() - offset, std::move(call));
  38691. delegate<void(void *)> conn{};
  38692. conn.template connect<&release<Candidate>>();
  38693. return { std::move(conn), signal };
  38694. }
  38695. /**
  38696. * @brief Connects a free function with payload or a bound member to a
  38697. * signal.
  38698. *
  38699. * The signal isn't responsible for the connected object or the payload.
  38700. * Users must always guarantee that the lifetime of the instance overcomes
  38701. * the one of the signal. On the other side, the signal handler performs
  38702. * checks to avoid multiple connections for the same function.<br/>
  38703. * When used to connect a free function with payload, its signature must be
  38704. * such that the instance is the first argument before the ones used to
  38705. * define the signal itself.
  38706. *
  38707. * @tparam Candidate Function or member to connect to the signal.
  38708. * @tparam Type Type of class or type of payload.
  38709. * @param value_or_instance A valid object that fits the purpose.
  38710. * @return A properly initialized connection object.
  38711. */
  38712. template<auto Candidate, typename Type>
  38713. connection connect(Type &&value_or_instance) {
  38714. disconnect<Candidate>(value_or_instance);
  38715. delegate<Ret(Args...)> call{};
  38716. call.template connect<Candidate>(value_or_instance);
  38717. signal->calls.insert(signal->calls.end() - offset, std::move(call));
  38718. delegate<void(void *)> conn{};
  38719. conn.template connect<&release<Candidate, Type>>(value_or_instance);
  38720. return { std::move(conn), signal };
  38721. }
  38722. /**
  38723. * @brief Disconnects a free function or an unbound member from a signal.
  38724. * @tparam Candidate Function or member to disconnect from the signal.
  38725. */
  38726. template<auto Candidate>
  38727. void disconnect() {
  38728. auto &calls = signal->calls;
  38729. delegate<Ret(Args...)> call{};
  38730. call.template connect<Candidate>();
  38731. calls.erase(std::remove(calls.begin(), calls.end(), std::move(call)), calls.end());
  38732. }
  38733. /**
  38734. * @brief Disconnects a free function with payload or a bound member from a
  38735. * signal.
  38736. * @tparam Candidate Function or member to disconnect from the signal.
  38737. * @tparam Type Type of class or type of payload.
  38738. * @param value_or_instance A valid object that fits the purpose.
  38739. */
  38740. template<auto Candidate, typename Type>
  38741. void disconnect(Type &&value_or_instance) {
  38742. auto &calls = signal->calls;
  38743. delegate<Ret(Args...)> call{};
  38744. call.template connect<Candidate>(value_or_instance);
  38745. calls.erase(std::remove(calls.begin(), calls.end(), std::move(call)), calls.end());
  38746. }
  38747. /**
  38748. * @brief Disconnects free functions with payload or bound members from a
  38749. * signal.
  38750. * @tparam Type Type of class or type of payload.
  38751. * @param value_or_instance A valid object that fits the purpose.
  38752. */
  38753. template<typename Type>
  38754. void disconnect(Type &value_or_instance) {
  38755. disconnect(&value_or_instance);
  38756. }
  38757. /**
  38758. * @brief Disconnects free functions with payload or bound members from a
  38759. * signal.
  38760. * @tparam Type Type of class or type of payload.
  38761. * @param value_or_instance A valid object that fits the purpose.
  38762. */
  38763. template<typename Type>
  38764. void disconnect(Type *value_or_instance) {
  38765. if(value_or_instance) {
  38766. auto &calls = signal->calls;
  38767. calls.erase(std::remove_if(calls.begin(), calls.end(), [value_or_instance](const auto &delegate) {
  38768. return delegate.instance() == value_or_instance;
  38769. }), calls.end());
  38770. }
  38771. }
  38772. /*! @brief Disconnects all the listeners from a signal. */
  38773. void disconnect() {
  38774. signal->calls.clear();
  38775. }
  38776. private:
  38777. difference_type offset;
  38778. signal_type *signal;
  38779. };
  38780. /**
  38781. * @brief Deduction guide.
  38782. *
  38783. * It allows to deduce the function type of a sink directly from the signal it
  38784. * refers to.
  38785. *
  38786. * @tparam Ret Return type of a function type.
  38787. * @tparam Args Types of arguments of a function type.
  38788. */
  38789. template<typename Ret, typename... Args>
  38790. sink(sigh<Ret(Args...)> &)
  38791. -> sink<Ret(Args...)>;
  38792. }
  38793. #endif
  38794. namespace entt {
  38795. /**
  38796. * @brief Basic dispatcher implementation.
  38797. *
  38798. * A dispatcher can be used either to trigger an immediate event or to enqueue
  38799. * events to be published all together once per tick.<br/>
  38800. * Listeners are provided in the form of member functions. For each event of
  38801. * type `Event`, listeners are such that they can be invoked with an argument of
  38802. * type `Event &`, no matter what the return type is.
  38803. *
  38804. * The dispatcher creates instances of the `sigh` class internally. Refer to the
  38805. * documentation of the latter for more details.
  38806. */
  38807. class dispatcher {
  38808. struct basic_pool {
  38809. virtual ~basic_pool() = default;
  38810. virtual void publish() = 0;
  38811. virtual void disconnect(void *) = 0;
  38812. virtual void clear() ENTT_NOEXCEPT = 0;
  38813. };
  38814. template<typename Event>
  38815. struct pool_handler final: basic_pool {
  38816. static_assert(std::is_same_v<Event, std::decay_t<Event>>, "Invalid event type");
  38817. using signal_type = sigh<void(Event &)>;
  38818. using sink_type = typename signal_type::sink_type;
  38819. void publish() override {
  38820. const auto length = events.size();
  38821. for(std::size_t pos{}; pos < length; ++pos) {
  38822. signal.publish(events[pos]);
  38823. }
  38824. events.erase(events.cbegin(), events.cbegin()+length);
  38825. }
  38826. void disconnect(void *instance) override {
  38827. sink().disconnect(instance);
  38828. }
  38829. void clear() ENTT_NOEXCEPT override {
  38830. events.clear();
  38831. }
  38832. [[nodiscard]] sink_type sink() ENTT_NOEXCEPT {
  38833. return entt::sink{signal};
  38834. }
  38835. template<typename... Args>
  38836. void trigger(Args &&... args) {
  38837. Event instance{std::forward<Args>(args)...};
  38838. signal.publish(instance);
  38839. }
  38840. template<typename... Args>
  38841. void enqueue(Args &&... args) {
  38842. if constexpr(std::is_aggregate_v<Event>) {
  38843. events.push_back(Event{std::forward<Args>(args)...});
  38844. } else {
  38845. events.emplace_back(std::forward<Args>(args)...);
  38846. }
  38847. }
  38848. private:
  38849. signal_type signal{};
  38850. std::vector<Event> events;
  38851. };
  38852. template<typename Event>
  38853. [[nodiscard]] pool_handler<Event> & assure() {
  38854. const auto index = type_seq<Event>::value();
  38855. if(!(index < pools.size())) {
  38856. pools.resize(std::size_t(index)+1u);
  38857. }
  38858. if(!pools[index]) {
  38859. pools[index].reset(new pool_handler<Event>{});
  38860. }
  38861. return static_cast<pool_handler<Event> &>(*pools[index]);
  38862. }
  38863. public:
  38864. /*! @brief Default constructor. */
  38865. dispatcher() = default;
  38866. /*! @brief Default move constructor. */
  38867. dispatcher(dispatcher &&) = default;
  38868. /*! @brief Default move assignment operator. @return This dispatcher. */
  38869. dispatcher & operator=(dispatcher &&) = default;
  38870. /**
  38871. * @brief Returns a sink object for the given event.
  38872. *
  38873. * A sink is an opaque object used to connect listeners to events.
  38874. *
  38875. * The function type for a listener is _compatible_ with:
  38876. * @code{.cpp}
  38877. * void(Event &);
  38878. * @endcode
  38879. *
  38880. * The order of invocation of the listeners isn't guaranteed.
  38881. *
  38882. * @sa sink
  38883. *
  38884. * @tparam Event Type of event of which to get the sink.
  38885. * @return A temporary sink object.
  38886. */
  38887. template<typename Event>
  38888. [[nodiscard]] auto sink() {
  38889. return assure<Event>().sink();
  38890. }
  38891. /**
  38892. * @brief Triggers an immediate event of the given type.
  38893. *
  38894. * All the listeners registered for the given type are immediately notified.
  38895. * The event is discarded after the execution.
  38896. *
  38897. * @tparam Event Type of event to trigger.
  38898. * @tparam Args Types of arguments to use to construct the event.
  38899. * @param args Arguments to use to construct the event.
  38900. */
  38901. template<typename Event, typename... Args>
  38902. void trigger(Args &&... args) {
  38903. assure<Event>().trigger(std::forward<Args>(args)...);
  38904. }
  38905. /**
  38906. * @brief Triggers an immediate event of the given type.
  38907. *
  38908. * All the listeners registered for the given type are immediately notified.
  38909. * The event is discarded after the execution.
  38910. *
  38911. * @tparam Event Type of event to trigger.
  38912. * @param event An instance of the given type of event.
  38913. */
  38914. template<typename Event>
  38915. void trigger(Event &&event) {
  38916. assure<std::decay_t<Event>>().trigger(std::forward<Event>(event));
  38917. }
  38918. /**
  38919. * @brief Enqueues an event of the given type.
  38920. *
  38921. * An event of the given type is queued. No listener is invoked. Use the
  38922. * `update` member function to notify listeners when ready.
  38923. *
  38924. * @tparam Event Type of event to enqueue.
  38925. * @tparam Args Types of arguments to use to construct the event.
  38926. * @param args Arguments to use to construct the event.
  38927. */
  38928. template<typename Event, typename... Args>
  38929. void enqueue(Args &&... args) {
  38930. assure<Event>().enqueue(std::forward<Args>(args)...);
  38931. }
  38932. /**
  38933. * @brief Enqueues an event of the given type.
  38934. *
  38935. * An event of the given type is queued. No listener is invoked. Use the
  38936. * `update` member function to notify listeners when ready.
  38937. *
  38938. * @tparam Event Type of event to enqueue.
  38939. * @param event An instance of the given type of event.
  38940. */
  38941. template<typename Event>
  38942. void enqueue(Event &&event) {
  38943. assure<std::decay_t<Event>>().enqueue(std::forward<Event>(event));
  38944. }
  38945. /**
  38946. * @brief Utility function to disconnect everything related to a given value
  38947. * or instance from a dispatcher.
  38948. * @tparam Type Type of class or type of payload.
  38949. * @param value_or_instance A valid object that fits the purpose.
  38950. */
  38951. template<typename Type>
  38952. void disconnect(Type &value_or_instance) {
  38953. disconnect(&value_or_instance);
  38954. }
  38955. /**
  38956. * @brief Utility function to disconnect everything related to a given value
  38957. * or instance from a dispatcher.
  38958. * @tparam Type Type of class or type of payload.
  38959. * @param value_or_instance A valid object that fits the purpose.
  38960. */
  38961. template<typename Type>
  38962. void disconnect(Type *value_or_instance) {
  38963. for(auto &&cpool: pools) {
  38964. if(cpool) {
  38965. cpool->disconnect(value_or_instance);
  38966. }
  38967. }
  38968. }
  38969. /**
  38970. * @brief Discards all the events queued so far.
  38971. *
  38972. * If no types are provided, the dispatcher will clear all the existing
  38973. * pools.
  38974. *
  38975. * @tparam Event Type of events to discard.
  38976. */
  38977. template<typename... Event>
  38978. void clear() {
  38979. if constexpr(sizeof...(Event) == 0) {
  38980. for(auto &&cpool: pools) {
  38981. if(cpool) {
  38982. cpool->clear();
  38983. }
  38984. }
  38985. } else {
  38986. (assure<Event>().clear(), ...);
  38987. }
  38988. }
  38989. /**
  38990. * @brief Delivers all the pending events of the given type.
  38991. *
  38992. * This method is blocking and it doesn't return until all the events are
  38993. * delivered to the registered listeners. It's responsibility of the users
  38994. * to reduce at a minimum the time spent in the bodies of the listeners.
  38995. *
  38996. * @tparam Event Type of events to send.
  38997. */
  38998. template<typename Event>
  38999. void update() {
  39000. assure<Event>().publish();
  39001. }
  39002. /**
  39003. * @brief Delivers all the pending events.
  39004. *
  39005. * This method is blocking and it doesn't return until all the events are
  39006. * delivered to the registered listeners. It's responsibility of the users
  39007. * to reduce at a minimum the time spent in the bodies of the listeners.
  39008. */
  39009. void update() const {
  39010. for(auto pos = pools.size(); pos; --pos) {
  39011. if(auto &&cpool = pools[pos-1]; cpool) {
  39012. cpool->publish();
  39013. }
  39014. }
  39015. }
  39016. private:
  39017. std::vector<std::unique_ptr<basic_pool>> pools;
  39018. };
  39019. }
  39020. #endif
  39021. // #include "signal/emitter.hpp"
  39022. #ifndef ENTT_SIGNAL_EMITTER_HPP
  39023. #define ENTT_SIGNAL_EMITTER_HPP
  39024. #include <algorithm>
  39025. #include <functional>
  39026. #include <iterator>
  39027. #include <list>
  39028. #include <memory>
  39029. #include <type_traits>
  39030. #include <utility>
  39031. #include <vector>
  39032. // #include "../config/config.h"
  39033. // #include "../core/fwd.hpp"
  39034. // #include "../core/type_info.hpp"
  39035. namespace entt {
  39036. /**
  39037. * @brief General purpose event emitter.
  39038. *
  39039. * The emitter class template follows the CRTP idiom. To create a custom emitter
  39040. * type, derived classes must inherit directly from the base class as:
  39041. *
  39042. * @code{.cpp}
  39043. * struct my_emitter: emitter<my_emitter> {
  39044. * // ...
  39045. * }
  39046. * @endcode
  39047. *
  39048. * Pools for the type of events are created internally on the fly. It's not
  39049. * required to specify in advance the full list of accepted types.<br/>
  39050. * Moreover, whenever an event is published, an emitter provides the listeners
  39051. * with a reference to itself along with a reference to the event. Therefore
  39052. * listeners have an handy way to work with it without incurring in the need of
  39053. * capturing a reference to the emitter.
  39054. *
  39055. * @tparam Derived Actual type of emitter that extends the class template.
  39056. */
  39057. template<typename Derived>
  39058. class emitter {
  39059. struct basic_pool {
  39060. virtual ~basic_pool() = default;
  39061. virtual bool empty() const ENTT_NOEXCEPT = 0;
  39062. virtual void clear() ENTT_NOEXCEPT = 0;
  39063. };
  39064. template<typename Event>
  39065. struct pool_handler final: basic_pool {
  39066. static_assert(std::is_same_v<Event, std::decay_t<Event>>, "Invalid event type");
  39067. using listener_type = std::function<void(Event &, Derived &)>;
  39068. using element_type = std::pair<bool, listener_type>;
  39069. using container_type = std::list<element_type>;
  39070. using connection_type = typename container_type::iterator;
  39071. [[nodiscard]] bool empty() const ENTT_NOEXCEPT override {
  39072. auto pred = [](auto &&element) { return element.first; };
  39073. return std::all_of(once_list.cbegin(), once_list.cend(), pred) &&
  39074. std::all_of(on_list.cbegin(), on_list.cend(), pred);
  39075. }
  39076. void clear() ENTT_NOEXCEPT override {
  39077. if(publishing) {
  39078. for(auto &&element: once_list) {
  39079. element.first = true;
  39080. }
  39081. for(auto &&element: on_list) {
  39082. element.first = true;
  39083. }
  39084. } else {
  39085. once_list.clear();
  39086. on_list.clear();
  39087. }
  39088. }
  39089. connection_type once(listener_type listener) {
  39090. return once_list.emplace(once_list.cend(), false, std::move(listener));
  39091. }
  39092. connection_type on(listener_type listener) {
  39093. return on_list.emplace(on_list.cend(), false, std::move(listener));
  39094. }
  39095. void erase(connection_type conn) {
  39096. conn->first = true;
  39097. if(!publishing) {
  39098. auto pred = [](auto &&element) { return element.first; };
  39099. once_list.remove_if(pred);
  39100. on_list.remove_if(pred);
  39101. }
  39102. }
  39103. void publish(Event &event, Derived &ref) {
  39104. container_type swap_list;
  39105. once_list.swap(swap_list);
  39106. publishing = true;
  39107. for(auto &&element: on_list) {
  39108. element.first ? void() : element.second(event, ref);
  39109. }
  39110. for(auto &&element: swap_list) {
  39111. element.first ? void() : element.second(event, ref);
  39112. }
  39113. publishing = false;
  39114. on_list.remove_if([](auto &&element) { return element.first; });
  39115. }
  39116. private:
  39117. bool publishing{false};
  39118. container_type once_list{};
  39119. container_type on_list{};
  39120. };
  39121. template<typename Event>
  39122. [[nodiscard]] pool_handler<Event> * assure() {
  39123. const auto index = type_seq<Event>::value();
  39124. if(!(index < pools.size())) {
  39125. pools.resize(std::size_t(index)+1u);
  39126. }
  39127. if(!pools[index]) {
  39128. pools[index].reset(new pool_handler<Event>{});
  39129. }
  39130. return static_cast<pool_handler<Event> *>(pools[index].get());
  39131. }
  39132. template<typename Event>
  39133. [[nodiscard]] const pool_handler<Event> * assure() const {
  39134. const auto index = type_seq<Event>::value();
  39135. return (!(index < pools.size()) || !pools[index]) ? nullptr : static_cast<const pool_handler<Event> *>(pools[index].get());
  39136. }
  39137. public:
  39138. /** @brief Type of listeners accepted for the given event. */
  39139. template<typename Event>
  39140. using listener = typename pool_handler<Event>::listener_type;
  39141. /**
  39142. * @brief Generic connection type for events.
  39143. *
  39144. * Type of the connection object returned by the event emitter whenever a
  39145. * listener for the given type is registered.<br/>
  39146. * It can be used to break connections still in use.
  39147. *
  39148. * @tparam Event Type of event for which the connection is created.
  39149. */
  39150. template<typename Event>
  39151. struct connection: private pool_handler<Event>::connection_type {
  39152. /** @brief Event emitters are friend classes of connections. */
  39153. friend class emitter;
  39154. /*! @brief Default constructor. */
  39155. connection() = default;
  39156. /**
  39157. * @brief Creates a connection that wraps its underlying instance.
  39158. * @param conn A connection object to wrap.
  39159. */
  39160. connection(typename pool_handler<Event>::connection_type conn)
  39161. : pool_handler<Event>::connection_type{std::move(conn)}
  39162. {}
  39163. };
  39164. /*! @brief Default constructor. */
  39165. emitter() = default;
  39166. /*! @brief Default destructor. */
  39167. virtual ~emitter() {
  39168. static_assert(std::is_base_of_v<emitter<Derived>, Derived>, "Incorrect use of the class template");
  39169. }
  39170. /*! @brief Default move constructor. */
  39171. emitter(emitter &&) = default;
  39172. /*! @brief Default move assignment operator. @return This emitter. */
  39173. emitter & operator=(emitter &&) = default;
  39174. /**
  39175. * @brief Emits the given event.
  39176. *
  39177. * All the listeners registered for the specific event type are invoked with
  39178. * the given event. The event type must either have a proper constructor for
  39179. * the arguments provided or be an aggregate type.
  39180. *
  39181. * @tparam Event Type of event to publish.
  39182. * @tparam Args Types of arguments to use to construct the event.
  39183. * @param args Parameters to use to initialize the event.
  39184. */
  39185. template<typename Event, typename... Args>
  39186. void publish(Args &&... args) {
  39187. Event instance{std::forward<Args>(args)...};
  39188. assure<Event>()->publish(instance, *static_cast<Derived *>(this));
  39189. }
  39190. /**
  39191. * @brief Registers a long-lived listener with the event emitter.
  39192. *
  39193. * This method can be used to register a listener designed to be invoked
  39194. * more than once for the given event type.<br/>
  39195. * The connection returned by the method can be freely discarded. It's meant
  39196. * to be used later to disconnect the listener if required.
  39197. *
  39198. * The listener is as a callable object that can be moved and the type of
  39199. * which is _compatible_ with `void(Event &, Derived &)`.
  39200. *
  39201. * @note
  39202. * Whenever an event is emitted, the emitter provides the listener with a
  39203. * reference to the derived class. Listeners don't have to capture those
  39204. * instances for later uses.
  39205. *
  39206. * @tparam Event Type of event to which to connect the listener.
  39207. * @param instance The listener to register.
  39208. * @return Connection object that can be used to disconnect the listener.
  39209. */
  39210. template<typename Event>
  39211. connection<Event> on(listener<Event> instance) {
  39212. return assure<Event>()->on(std::move(instance));
  39213. }
  39214. /**
  39215. * @brief Registers a short-lived listener with the event emitter.
  39216. *
  39217. * This method can be used to register a listener designed to be invoked
  39218. * only once for the given event type.<br/>
  39219. * The connection returned by the method can be freely discarded. It's meant
  39220. * to be used later to disconnect the listener if required.
  39221. *
  39222. * The listener is as a callable object that can be moved and the type of
  39223. * which is _compatible_ with `void(Event &, Derived &)`.
  39224. *
  39225. * @note
  39226. * Whenever an event is emitted, the emitter provides the listener with a
  39227. * reference to the derived class. Listeners don't have to capture those
  39228. * instances for later uses.
  39229. *
  39230. * @tparam Event Type of event to which to connect the listener.
  39231. * @param instance The listener to register.
  39232. * @return Connection object that can be used to disconnect the listener.
  39233. */
  39234. template<typename Event>
  39235. connection<Event> once(listener<Event> instance) {
  39236. return assure<Event>()->once(std::move(instance));
  39237. }
  39238. /**
  39239. * @brief Disconnects a listener from the event emitter.
  39240. *
  39241. * Do not use twice the same connection to disconnect a listener, it results
  39242. * in undefined behavior. Once used, discard the connection object.
  39243. *
  39244. * @tparam Event Type of event of the connection.
  39245. * @param conn A valid connection.
  39246. */
  39247. template<typename Event>
  39248. void erase(connection<Event> conn) {
  39249. assure<Event>()->erase(std::move(conn));
  39250. }
  39251. /**
  39252. * @brief Disconnects all the listeners for the given event type.
  39253. *
  39254. * All the connections previously returned for the given event are
  39255. * invalidated. Using them results in undefined behavior.
  39256. *
  39257. * @tparam Event Type of event to reset.
  39258. */
  39259. template<typename Event>
  39260. void clear() {
  39261. assure<Event>()->clear();
  39262. }
  39263. /**
  39264. * @brief Disconnects all the listeners.
  39265. *
  39266. * All the connections previously returned are invalidated. Using them
  39267. * results in undefined behavior.
  39268. */
  39269. void clear() ENTT_NOEXCEPT {
  39270. for(auto &&cpool: pools) {
  39271. if(cpool) {
  39272. cpool->clear();
  39273. }
  39274. }
  39275. }
  39276. /**
  39277. * @brief Checks if there are listeners registered for the specific event.
  39278. * @tparam Event Type of event to test.
  39279. * @return True if there are no listeners registered, false otherwise.
  39280. */
  39281. template<typename Event>
  39282. [[nodiscard]] bool empty() const {
  39283. const auto *cpool = assure<Event>();
  39284. return !cpool || cpool->empty();
  39285. }
  39286. /**
  39287. * @brief Checks if there are listeners registered with the event emitter.
  39288. * @return True if there are no listeners registered, false otherwise.
  39289. */
  39290. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  39291. return std::all_of(pools.cbegin(), pools.cend(), [](auto &&cpool) {
  39292. return !cpool || cpool->empty();
  39293. });
  39294. }
  39295. private:
  39296. std::vector<std::unique_ptr<basic_pool>> pools{};
  39297. };
  39298. }
  39299. #endif
  39300. // #include "signal/sigh.hpp"
  39301. #ifndef ENTT_SIGNAL_SIGH_HPP
  39302. #define ENTT_SIGNAL_SIGH_HPP
  39303. #include <vector>
  39304. #include <utility>
  39305. #include <iterator>
  39306. #include <algorithm>
  39307. #include <functional>
  39308. #include <type_traits>
  39309. // #include "../config/config.h"
  39310. // #include "delegate.hpp"
  39311. // #include "fwd.hpp"
  39312. namespace entt {
  39313. /**
  39314. * @brief Sink class.
  39315. *
  39316. * Primary template isn't defined on purpose. All the specializations give a
  39317. * compile-time error unless the template parameter is a function type.
  39318. *
  39319. * @tparam Function A valid function type.
  39320. */
  39321. template<typename Function>
  39322. class sink;
  39323. /**
  39324. * @brief Unmanaged signal handler.
  39325. *
  39326. * Primary template isn't defined on purpose. All the specializations give a
  39327. * compile-time error unless the template parameter is a function type.
  39328. *
  39329. * @tparam Function A valid function type.
  39330. */
  39331. template<typename Function>
  39332. class sigh;
  39333. /**
  39334. * @brief Unmanaged signal handler.
  39335. *
  39336. * It works directly with references to classes and pointers to member functions
  39337. * as well as pointers to free functions. Users of this class are in charge of
  39338. * disconnecting instances before deleting them.
  39339. *
  39340. * This class serves mainly two purposes:
  39341. *
  39342. * * Creating signals to use later to notify a bunch of listeners.
  39343. * * Collecting results from a set of functions like in a voting system.
  39344. *
  39345. * @tparam Ret Return type of a function type.
  39346. * @tparam Args Types of arguments of a function type.
  39347. */
  39348. template<typename Ret, typename... Args>
  39349. class sigh<Ret(Args...)> {
  39350. /*! @brief A sink is allowed to modify a signal. */
  39351. friend class sink<Ret(Args...)>;
  39352. public:
  39353. /*! @brief Unsigned integer type. */
  39354. using size_type = std::size_t;
  39355. /*! @brief Sink type. */
  39356. using sink_type = sink<Ret(Args...)>;
  39357. /**
  39358. * @brief Instance type when it comes to connecting member functions.
  39359. * @tparam Class Type of class to which the member function belongs.
  39360. */
  39361. template<typename Class>
  39362. using instance_type = Class *;
  39363. /**
  39364. * @brief Number of listeners connected to the signal.
  39365. * @return Number of listeners currently connected.
  39366. */
  39367. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  39368. return calls.size();
  39369. }
  39370. /**
  39371. * @brief Returns false if at least a listener is connected to the signal.
  39372. * @return True if the signal has no listeners connected, false otherwise.
  39373. */
  39374. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  39375. return calls.empty();
  39376. }
  39377. /**
  39378. * @brief Triggers a signal.
  39379. *
  39380. * All the listeners are notified. Order isn't guaranteed.
  39381. *
  39382. * @param args Arguments to use to invoke listeners.
  39383. */
  39384. void publish(Args... args) const {
  39385. for(auto &&call: std::as_const(calls)) {
  39386. call(args...);
  39387. }
  39388. }
  39389. /**
  39390. * @brief Collects return values from the listeners.
  39391. *
  39392. * The collector must expose a call operator with the following properties:
  39393. *
  39394. * * The return type is either `void` or such that it's convertible to
  39395. * `bool`. In the second case, a true value will stop the iteration.
  39396. * * The list of parameters is empty if `Ret` is `void`, otherwise it
  39397. * contains a single element such that `Ret` is convertible to it.
  39398. *
  39399. * @tparam Func Type of collector to use, if any.
  39400. * @param func A valid function object.
  39401. * @param args Arguments to use to invoke listeners.
  39402. */
  39403. template<typename Func>
  39404. void collect(Func func, Args... args) const {
  39405. for(auto &&call: calls) {
  39406. if constexpr(std::is_void_v<Ret>) {
  39407. if constexpr(std::is_invocable_r_v<bool, Func>) {
  39408. call(args...);
  39409. if(func()) { break; }
  39410. } else {
  39411. call(args...);
  39412. func();
  39413. }
  39414. } else {
  39415. if constexpr(std::is_invocable_r_v<bool, Func, Ret>) {
  39416. if(func(call(args...))) { break; }
  39417. } else {
  39418. func(call(args...));
  39419. }
  39420. }
  39421. }
  39422. }
  39423. private:
  39424. std::vector<delegate<Ret(Args...)>> calls;
  39425. };
  39426. /**
  39427. * @brief Connection class.
  39428. *
  39429. * Opaque object the aim of which is to allow users to release an already
  39430. * estabilished connection without having to keep a reference to the signal or
  39431. * the sink that generated it.
  39432. */
  39433. class connection {
  39434. /*! @brief A sink is allowed to create connection objects. */
  39435. template<typename>
  39436. friend class sink;
  39437. connection(delegate<void(void *)> fn, void *ref)
  39438. : disconnect{fn}, signal{ref}
  39439. {}
  39440. public:
  39441. /*! @brief Default constructor. */
  39442. connection() = default;
  39443. /**
  39444. * @brief Checks whether a connection is properly initialized.
  39445. * @return True if the connection is properly initialized, false otherwise.
  39446. */
  39447. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  39448. return static_cast<bool>(disconnect);
  39449. }
  39450. /*! @brief Breaks the connection. */
  39451. void release() {
  39452. if(disconnect) {
  39453. disconnect(signal);
  39454. disconnect.reset();
  39455. }
  39456. }
  39457. private:
  39458. delegate<void(void *)> disconnect;
  39459. void *signal{};
  39460. };
  39461. /**
  39462. * @brief Scoped connection class.
  39463. *
  39464. * Opaque object the aim of which is to allow users to release an already
  39465. * estabilished connection without having to keep a reference to the signal or
  39466. * the sink that generated it.<br/>
  39467. * A scoped connection automatically breaks the link between the two objects
  39468. * when it goes out of scope.
  39469. */
  39470. struct scoped_connection {
  39471. /*! @brief Default constructor. */
  39472. scoped_connection() = default;
  39473. /**
  39474. * @brief Constructs a scoped connection from a basic connection.
  39475. * @param other A valid connection object.
  39476. */
  39477. scoped_connection(const connection &other)
  39478. : conn{other}
  39479. {}
  39480. /*! @brief Default copy constructor, deleted on purpose. */
  39481. scoped_connection(const scoped_connection &) = delete;
  39482. /*! @brief Automatically breaks the link on destruction. */
  39483. ~scoped_connection() {
  39484. conn.release();
  39485. }
  39486. /**
  39487. * @brief Default copy assignment operator, deleted on purpose.
  39488. * @return This scoped connection.
  39489. */
  39490. scoped_connection & operator=(const scoped_connection &) = delete;
  39491. /**
  39492. * @brief Acquires a connection.
  39493. * @param other The connection object to acquire.
  39494. * @return This scoped connection.
  39495. */
  39496. scoped_connection & operator=(connection other) {
  39497. conn = std::move(other);
  39498. return *this;
  39499. }
  39500. /**
  39501. * @brief Checks whether a scoped connection is properly initialized.
  39502. * @return True if the connection is properly initialized, false otherwise.
  39503. */
  39504. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  39505. return static_cast<bool>(conn);
  39506. }
  39507. /*! @brief Breaks the connection. */
  39508. void release() {
  39509. conn.release();
  39510. }
  39511. private:
  39512. connection conn;
  39513. };
  39514. /**
  39515. * @brief Sink class.
  39516. *
  39517. * A sink is used to connect listeners to signals and to disconnect them.<br/>
  39518. * The function type for a listener is the one of the signal to which it
  39519. * belongs.
  39520. *
  39521. * The clear separation between a signal and a sink permits to store the former
  39522. * as private data member without exposing the publish functionality to the
  39523. * users of the class.
  39524. *
  39525. * @warning
  39526. * Lifetime of a sink must not overcome that of the signal to which it refers.
  39527. * In any other case, attempting to use a sink results in undefined behavior.
  39528. *
  39529. * @tparam Ret Return type of a function type.
  39530. * @tparam Args Types of arguments of a function type.
  39531. */
  39532. template<typename Ret, typename... Args>
  39533. class sink<Ret(Args...)> {
  39534. using signal_type = sigh<Ret(Args...)>;
  39535. using difference_type = typename std::iterator_traits<typename decltype(signal_type::calls)::iterator>::difference_type;
  39536. template<auto Candidate, typename Type>
  39537. static void release(Type value_or_instance, void *signal) {
  39538. sink{*static_cast<signal_type *>(signal)}.disconnect<Candidate>(value_or_instance);
  39539. }
  39540. template<auto Candidate>
  39541. static void release(void *signal) {
  39542. sink{*static_cast<signal_type *>(signal)}.disconnect<Candidate>();
  39543. }
  39544. public:
  39545. /**
  39546. * @brief Constructs a sink that is allowed to modify a given signal.
  39547. * @param ref A valid reference to a signal object.
  39548. */
  39549. sink(sigh<Ret(Args...)> &ref) ENTT_NOEXCEPT
  39550. : offset{},
  39551. signal{&ref}
  39552. {}
  39553. /**
  39554. * @brief Returns false if at least a listener is connected to the sink.
  39555. * @return True if the sink has no listeners connected, false otherwise.
  39556. */
  39557. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  39558. return signal->calls.empty();
  39559. }
  39560. /**
  39561. * @brief Returns a sink that connects before a given free function or an
  39562. * unbound member.
  39563. * @tparam Function A valid free function pointer.
  39564. * @return A properly initialized sink object.
  39565. */
  39566. template<auto Function>
  39567. [[nodiscard]] sink before() {
  39568. delegate<Ret(Args...)> call{};
  39569. call.template connect<Function>();
  39570. const auto &calls = signal->calls;
  39571. const auto it = std::find(calls.cbegin(), calls.cend(), std::move(call));
  39572. sink other{*this};
  39573. other.offset = std::distance(it, calls.cend());
  39574. return other;
  39575. }
  39576. /**
  39577. * @brief Returns a sink that connects before a free function with payload
  39578. * or a bound member.
  39579. * @tparam Candidate Member or free function to look for.
  39580. * @tparam Type Type of class or type of payload.
  39581. * @param value_or_instance A valid object that fits the purpose.
  39582. * @return A properly initialized sink object.
  39583. */
  39584. template<auto Candidate, typename Type>
  39585. [[nodiscard]] sink before(Type &&value_or_instance) {
  39586. delegate<Ret(Args...)> call{};
  39587. call.template connect<Candidate>(value_or_instance);
  39588. const auto &calls = signal->calls;
  39589. const auto it = std::find(calls.cbegin(), calls.cend(), std::move(call));
  39590. sink other{*this};
  39591. other.offset = std::distance(it, calls.cend());
  39592. return other;
  39593. }
  39594. /**
  39595. * @brief Returns a sink that connects before a given instance or specific
  39596. * payload.
  39597. * @tparam Type Type of class or type of payload.
  39598. * @param value_or_instance A valid object that fits the purpose.
  39599. * @return A properly initialized sink object.
  39600. */
  39601. template<typename Type>
  39602. [[nodiscard]] sink before(Type &value_or_instance) {
  39603. return before(&value_or_instance);
  39604. }
  39605. /**
  39606. * @brief Returns a sink that connects before a given instance or specific
  39607. * payload.
  39608. * @tparam Type Type of class or type of payload.
  39609. * @param value_or_instance A valid pointer that fits the purpose.
  39610. * @return A properly initialized sink object.
  39611. */
  39612. template<typename Type>
  39613. [[nodiscard]] sink before(Type *value_or_instance) {
  39614. sink other{*this};
  39615. if(value_or_instance) {
  39616. const auto &calls = signal->calls;
  39617. const auto it = std::find_if(calls.cbegin(), calls.cend(), [value_or_instance](const auto &delegate) {
  39618. return delegate.instance() == value_or_instance;
  39619. });
  39620. other.offset = std::distance(it, calls.cend());
  39621. }
  39622. return other;
  39623. }
  39624. /**
  39625. * @brief Returns a sink that connects before anything else.
  39626. * @return A properly initialized sink object.
  39627. */
  39628. [[nodiscard]] sink before() {
  39629. sink other{*this};
  39630. other.offset = signal->calls.size();
  39631. return other;
  39632. }
  39633. /**
  39634. * @brief Connects a free function or an unbound member to a signal.
  39635. *
  39636. * The signal handler performs checks to avoid multiple connections for the
  39637. * same function.
  39638. *
  39639. * @tparam Candidate Function or member to connect to the signal.
  39640. * @return A properly initialized connection object.
  39641. */
  39642. template<auto Candidate>
  39643. connection connect() {
  39644. disconnect<Candidate>();
  39645. delegate<Ret(Args...)> call{};
  39646. call.template connect<Candidate>();
  39647. signal->calls.insert(signal->calls.end() - offset, std::move(call));
  39648. delegate<void(void *)> conn{};
  39649. conn.template connect<&release<Candidate>>();
  39650. return { std::move(conn), signal };
  39651. }
  39652. /**
  39653. * @brief Connects a free function with payload or a bound member to a
  39654. * signal.
  39655. *
  39656. * The signal isn't responsible for the connected object or the payload.
  39657. * Users must always guarantee that the lifetime of the instance overcomes
  39658. * the one of the signal. On the other side, the signal handler performs
  39659. * checks to avoid multiple connections for the same function.<br/>
  39660. * When used to connect a free function with payload, its signature must be
  39661. * such that the instance is the first argument before the ones used to
  39662. * define the signal itself.
  39663. *
  39664. * @tparam Candidate Function or member to connect to the signal.
  39665. * @tparam Type Type of class or type of payload.
  39666. * @param value_or_instance A valid object that fits the purpose.
  39667. * @return A properly initialized connection object.
  39668. */
  39669. template<auto Candidate, typename Type>
  39670. connection connect(Type &&value_or_instance) {
  39671. disconnect<Candidate>(value_or_instance);
  39672. delegate<Ret(Args...)> call{};
  39673. call.template connect<Candidate>(value_or_instance);
  39674. signal->calls.insert(signal->calls.end() - offset, std::move(call));
  39675. delegate<void(void *)> conn{};
  39676. conn.template connect<&release<Candidate, Type>>(value_or_instance);
  39677. return { std::move(conn), signal };
  39678. }
  39679. /**
  39680. * @brief Disconnects a free function or an unbound member from a signal.
  39681. * @tparam Candidate Function or member to disconnect from the signal.
  39682. */
  39683. template<auto Candidate>
  39684. void disconnect() {
  39685. auto &calls = signal->calls;
  39686. delegate<Ret(Args...)> call{};
  39687. call.template connect<Candidate>();
  39688. calls.erase(std::remove(calls.begin(), calls.end(), std::move(call)), calls.end());
  39689. }
  39690. /**
  39691. * @brief Disconnects a free function with payload or a bound member from a
  39692. * signal.
  39693. * @tparam Candidate Function or member to disconnect from the signal.
  39694. * @tparam Type Type of class or type of payload.
  39695. * @param value_or_instance A valid object that fits the purpose.
  39696. */
  39697. template<auto Candidate, typename Type>
  39698. void disconnect(Type &&value_or_instance) {
  39699. auto &calls = signal->calls;
  39700. delegate<Ret(Args...)> call{};
  39701. call.template connect<Candidate>(value_or_instance);
  39702. calls.erase(std::remove(calls.begin(), calls.end(), std::move(call)), calls.end());
  39703. }
  39704. /**
  39705. * @brief Disconnects free functions with payload or bound members from a
  39706. * signal.
  39707. * @tparam Type Type of class or type of payload.
  39708. * @param value_or_instance A valid object that fits the purpose.
  39709. */
  39710. template<typename Type>
  39711. void disconnect(Type &value_or_instance) {
  39712. disconnect(&value_or_instance);
  39713. }
  39714. /**
  39715. * @brief Disconnects free functions with payload or bound members from a
  39716. * signal.
  39717. * @tparam Type Type of class or type of payload.
  39718. * @param value_or_instance A valid object that fits the purpose.
  39719. */
  39720. template<typename Type>
  39721. void disconnect(Type *value_or_instance) {
  39722. if(value_or_instance) {
  39723. auto &calls = signal->calls;
  39724. calls.erase(std::remove_if(calls.begin(), calls.end(), [value_or_instance](const auto &delegate) {
  39725. return delegate.instance() == value_or_instance;
  39726. }), calls.end());
  39727. }
  39728. }
  39729. /*! @brief Disconnects all the listeners from a signal. */
  39730. void disconnect() {
  39731. signal->calls.clear();
  39732. }
  39733. private:
  39734. difference_type offset;
  39735. signal_type *signal;
  39736. };
  39737. /**
  39738. * @brief Deduction guide.
  39739. *
  39740. * It allows to deduce the function type of a sink directly from the signal it
  39741. * refers to.
  39742. *
  39743. * @tparam Ret Return type of a function type.
  39744. * @tparam Args Types of arguments of a function type.
  39745. */
  39746. template<typename Ret, typename... Args>
  39747. sink(sigh<Ret(Args...)> &)
  39748. -> sink<Ret(Args...)>;
  39749. }
  39750. #endif