entt.hpp 626 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185
  1. // #include "core/algorithm.hpp"
  2. #ifndef ENTT_CORE_ALGORITHM_HPP
  3. #define ENTT_CORE_ALGORITHM_HPP
  4. #include <vector>
  5. #include <utility>
  6. #include <iterator>
  7. #include <algorithm>
  8. #include <functional>
  9. // #include "utility.hpp"
  10. #ifndef ENTT_CORE_UTILITY_HPP
  11. #define ENTT_CORE_UTILITY_HPP
  12. #include <utility>
  13. // #include "../config/config.h"
  14. #ifndef ENTT_CONFIG_CONFIG_H
  15. #define ENTT_CONFIG_CONFIG_H
  16. #ifndef ENTT_NOEXCEPT
  17. # define ENTT_NOEXCEPT noexcept
  18. #endif
  19. #ifndef ENTT_HS_SUFFIX
  20. # define ENTT_HS_SUFFIX _hs
  21. #endif
  22. #ifndef ENTT_HWS_SUFFIX
  23. # define ENTT_HWS_SUFFIX _hws
  24. #endif
  25. #ifndef ENTT_USE_ATOMIC
  26. # define ENTT_MAYBE_ATOMIC(Type) Type
  27. #else
  28. # include <atomic>
  29. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  30. #endif
  31. #ifndef ENTT_ID_TYPE
  32. # include <cstdint>
  33. # define ENTT_ID_TYPE std::uint32_t
  34. #endif
  35. #ifndef ENTT_PAGE_SIZE
  36. # define ENTT_PAGE_SIZE 32768
  37. #endif
  38. #ifndef ENTT_ASSERT
  39. # include <cassert>
  40. # define ENTT_ASSERT(condition) assert(condition)
  41. #endif
  42. #ifndef ENTT_NO_ETO
  43. # include <type_traits>
  44. # define ENTT_IS_EMPTY(Type) std::is_empty<Type>
  45. #else
  46. # include <type_traits>
  47. # define ENTT_IS_EMPTY(Type) std::false_type
  48. #endif
  49. #ifndef ENTT_STANDARD_CPP
  50. # if defined __clang__ || (defined __GNUC__ && __GNUC__ > 8)
  51. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  52. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  53. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  54. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  55. # elif defined __GNUC__
  56. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  57. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  58. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  59. # elif defined _MSC_VER
  60. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  61. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  62. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  63. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  64. # endif
  65. #endif
  66. #ifndef ENTT_STANDALONE
  67. # define ENTT_FAST_PATH(...) false
  68. #else
  69. # define ENTT_FAST_PATH(Cond) Cond
  70. #endif
  71. #endif
  72. namespace entt {
  73. /*! @brief Identity function object (waiting for C++20). */
  74. struct identity {
  75. /**
  76. * @brief Returns its argument unchanged.
  77. * @tparam Type Type of the argument.
  78. * @param value The actual argument.
  79. * @return The submitted value as-is.
  80. */
  81. template<class Type>
  82. [[nodiscard]] constexpr Type && operator()(Type &&value) const ENTT_NOEXCEPT {
  83. return std::forward<Type>(value);
  84. }
  85. };
  86. /**
  87. * @brief Constant utility to disambiguate overloaded members of a class.
  88. * @tparam Type Type of the desired overload.
  89. * @tparam Class Type of class to which the member belongs.
  90. * @param member A valid pointer to a member.
  91. * @return Pointer to the member.
  92. */
  93. template<typename Type, typename Class>
  94. [[nodiscard]] constexpr auto overload(Type Class:: *member) ENTT_NOEXCEPT { return member; }
  95. /**
  96. * @brief Constant utility to disambiguate overloaded functions.
  97. * @tparam Func Function type of the desired overload.
  98. * @param func A valid pointer to a function.
  99. * @return Pointer to the function.
  100. */
  101. template<typename Func>
  102. [[nodiscard]] constexpr auto overload(Func *func) ENTT_NOEXCEPT { return func; }
  103. /**
  104. * @brief Helper type for visitors.
  105. * @tparam Func Types of function objects.
  106. */
  107. template<class... Func>
  108. struct overloaded: Func... {
  109. using Func::operator()...;
  110. };
  111. /**
  112. * @brief Deduction guide.
  113. * @tparam Func Types of function objects.
  114. */
  115. template<class... Func>
  116. overloaded(Func...) -> overloaded<Func...>;
  117. /**
  118. * @brief Basic implementation of a y-combinator.
  119. * @tparam Func Type of a potentially recursive function.
  120. */
  121. template<class Func>
  122. struct y_combinator {
  123. /**
  124. * @brief Constructs a y-combinator from a given function.
  125. * @param recursive A potentially recursive function.
  126. */
  127. y_combinator(Func recursive):
  128. func{std::move(recursive)}
  129. {}
  130. /**
  131. * @brief Invokes a y-combinator and therefore its underlying function.
  132. * @tparam Args Types of arguments to use to invoke the underlying function.
  133. * @param args Parameters to use to invoke the underlying function.
  134. * @return Return value of the underlying function, if any.
  135. */
  136. template <class... Args>
  137. decltype(auto) operator()(Args &&... args) const {
  138. return func(*this, std::forward<Args>(args)...);
  139. }
  140. /*! @copydoc operator()() */
  141. template <class... Args>
  142. decltype(auto) operator()(Args &&... args) {
  143. return func(*this, std::forward<Args>(args)...);
  144. }
  145. private:
  146. Func func;
  147. };
  148. }
  149. #endif
  150. namespace entt {
  151. /**
  152. * @brief Function object to wrap `std::sort` in a class type.
  153. *
  154. * Unfortunately, `std::sort` cannot be passed as template argument to a class
  155. * template or a function template.<br/>
  156. * This class fills the gap by wrapping some flavors of `std::sort` in a
  157. * function object.
  158. */
  159. struct std_sort {
  160. /**
  161. * @brief Sorts the elements in a range.
  162. *
  163. * Sorts the elements in a range using the given binary comparison function.
  164. *
  165. * @tparam It Type of random access iterator.
  166. * @tparam Compare Type of comparison function object.
  167. * @tparam Args Types of arguments to forward to the sort function.
  168. * @param first An iterator to the first element of the range to sort.
  169. * @param last An iterator past the last element of the range to sort.
  170. * @param compare A valid comparison function object.
  171. * @param args Arguments to forward to the sort function, if any.
  172. */
  173. template<typename It, typename Compare = std::less<>, typename... Args>
  174. void operator()(It first, It last, Compare compare = Compare{}, Args &&... args) const {
  175. std::sort(std::forward<Args>(args)..., std::move(first), std::move(last), std::move(compare));
  176. }
  177. };
  178. /*! @brief Function object for performing insertion sort. */
  179. struct insertion_sort {
  180. /**
  181. * @brief Sorts the elements in a range.
  182. *
  183. * Sorts the elements in a range using the given binary comparison function.
  184. *
  185. * @tparam It Type of random access iterator.
  186. * @tparam Compare Type of comparison function object.
  187. * @param first An iterator to the first element of the range to sort.
  188. * @param last An iterator past the last element of the range to sort.
  189. * @param compare A valid comparison function object.
  190. */
  191. template<typename It, typename Compare = std::less<>>
  192. void operator()(It first, It last, Compare compare = Compare{}) const {
  193. if(first < last) {
  194. for(auto it = first+1; it < last; ++it) {
  195. auto value = std::move(*it);
  196. auto pre = it;
  197. for(; pre > first && compare(value, *(pre-1)); --pre) {
  198. *pre = std::move(*(pre-1));
  199. }
  200. *pre = std::move(value);
  201. }
  202. }
  203. }
  204. };
  205. /**
  206. * @brief Function object for performing LSD radix sort.
  207. * @tparam Bit Number of bits processed per pass.
  208. * @tparam N Maximum number of bits to sort.
  209. */
  210. template<std::size_t Bit, std::size_t N>
  211. struct radix_sort {
  212. static_assert((N % Bit) == 0, "The maximum number of bits to sort must be a multiple of the number of bits processed per pass");
  213. /**
  214. * @brief Sorts the elements in a range.
  215. *
  216. * Sorts the elements in a range using the given _getter_ to access the
  217. * actual data to be sorted.
  218. *
  219. * This implementation is inspired by the online book
  220. * [Physically Based Rendering](http://www.pbr-book.org/3ed-2018/Primitives_and_Intersection_Acceleration/Bounding_Volume_Hierarchies.html#RadixSort).
  221. *
  222. * @tparam It Type of random access iterator.
  223. * @tparam Getter Type of _getter_ function object.
  224. * @param first An iterator to the first element of the range to sort.
  225. * @param last An iterator past the last element of the range to sort.
  226. * @param getter A valid _getter_ function object.
  227. */
  228. template<typename It, typename Getter = identity>
  229. void operator()(It first, It last, Getter getter = Getter{}) const {
  230. if(first < last) {
  231. static constexpr auto mask = (1 << Bit) - 1;
  232. static constexpr auto buckets = 1 << Bit;
  233. static constexpr auto passes = N / Bit;
  234. using value_type = typename std::iterator_traits<It>::value_type;
  235. std::vector<value_type> aux(std::distance(first, last));
  236. auto part = [getter = std::move(getter)](auto from, auto to, auto out, auto start) {
  237. std::size_t index[buckets]{};
  238. std::size_t count[buckets]{};
  239. std::for_each(from, to, [&getter, &count, start](const value_type &item) {
  240. ++count[(getter(item) >> start) & mask];
  241. });
  242. std::for_each(std::next(std::begin(index)), std::end(index), [index = std::begin(index), count = std::begin(count)](auto &item) mutable {
  243. item = *(index++) + *(count++);
  244. });
  245. std::for_each(from, to, [&getter, &out, &index, start](value_type &item) {
  246. out[index[(getter(item) >> start) & mask]++] = std::move(item);
  247. });
  248. };
  249. for(std::size_t pass = 0; pass < (passes & ~1); pass += 2) {
  250. part(first, last, aux.begin(), pass * Bit);
  251. part(aux.begin(), aux.end(), first, (pass + 1) * Bit);
  252. }
  253. if constexpr(passes & 1) {
  254. part(first, last, aux.begin(), (passes - 1) * Bit);
  255. std::move(aux.begin(), aux.end(), first);
  256. }
  257. }
  258. }
  259. };
  260. }
  261. #endif
  262. // #include "core/attribute.h"
  263. #ifndef ENTT_CORE_ATTRIBUTE_H
  264. #define ENTT_CORE_ATTRIBUTE_H
  265. #ifndef ENTT_EXPORT
  266. # if defined _WIN32 || defined __CYGWIN__ || defined _MSC_VER
  267. # define ENTT_EXPORT __declspec(dllexport)
  268. # define ENTT_IMPORT __declspec(dllimport)
  269. # define ENTT_HIDDEN
  270. # elif defined __GNUC__ && __GNUC__ >= 4
  271. # define ENTT_EXPORT __attribute__((visibility("default")))
  272. # define ENTT_IMPORT __attribute__((visibility("default")))
  273. # define ENTT_HIDDEN __attribute__((visibility("hidden")))
  274. # else /* Unsupported compiler */
  275. # define ENTT_EXPORT
  276. # define ENTT_IMPORT
  277. # define ENTT_HIDDEN
  278. # endif
  279. #endif
  280. #ifndef ENTT_API
  281. # if defined ENTT_API_EXPORT
  282. # define ENTT_API ENTT_EXPORT
  283. # elif defined ENTT_API_IMPORT
  284. # define ENTT_API ENTT_IMPORT
  285. # else /* No API */
  286. # define ENTT_API
  287. # endif
  288. #endif
  289. #endif
  290. // #include "core/family.hpp"
  291. #ifndef ENTT_CORE_FAMILY_HPP
  292. #define ENTT_CORE_FAMILY_HPP
  293. // #include "../config/config.h"
  294. // #include "fwd.hpp"
  295. #ifndef ENTT_CORE_FWD_HPP
  296. #define ENTT_CORE_FWD_HPP
  297. // #include "../config/config.h"
  298. namespace entt {
  299. /*! @brief Alias declaration for type identifiers. */
  300. using id_type = ENTT_ID_TYPE;
  301. }
  302. #endif
  303. namespace entt {
  304. /**
  305. * @brief Dynamic identifier generator.
  306. *
  307. * Utility class template that can be used to assign unique identifiers to types
  308. * at runtime. Use different specializations to create separate sets of
  309. * identifiers.
  310. */
  311. template<typename...>
  312. class family {
  313. inline static ENTT_MAYBE_ATOMIC(id_type) identifier{};
  314. public:
  315. /*! @brief Unsigned integer type. */
  316. using family_type = id_type;
  317. /*! @brief Statically generated unique identifier for the given type. */
  318. template<typename... Type>
  319. // at the time I'm writing, clang crashes during compilation if auto is used instead of family_type
  320. inline static const family_type type = identifier++;
  321. };
  322. }
  323. #endif
  324. // #include "core/hashed_string.hpp"
  325. #ifndef ENTT_CORE_HASHED_STRING_HPP
  326. #define ENTT_CORE_HASHED_STRING_HPP
  327. #include <cstddef>
  328. #include <cstdint>
  329. // #include "../config/config.h"
  330. // #include "fwd.hpp"
  331. namespace entt {
  332. /**
  333. * @cond TURN_OFF_DOXYGEN
  334. * Internal details not to be documented.
  335. */
  336. namespace internal {
  337. template<typename>
  338. struct fnv1a_traits;
  339. template<>
  340. struct fnv1a_traits<std::uint32_t> {
  341. using type = std::uint32_t;
  342. static constexpr std::uint32_t offset = 2166136261;
  343. static constexpr std::uint32_t prime = 16777619;
  344. };
  345. template<>
  346. struct fnv1a_traits<std::uint64_t> {
  347. using type = std::uint64_t;
  348. static constexpr std::uint64_t offset = 14695981039346656037ull;
  349. static constexpr std::uint64_t prime = 1099511628211ull;
  350. };
  351. }
  352. /**
  353. * Internal details not to be documented.
  354. * @endcond
  355. */
  356. /**
  357. * @brief Zero overhead unique identifier.
  358. *
  359. * A hashed string is a compile-time tool that allows users to use
  360. * human-readable identifers in the codebase while using their numeric
  361. * counterparts at runtime.<br/>
  362. * Because of that, a hashed string can also be used in constant expressions if
  363. * required.
  364. *
  365. * @tparam Char Character type.
  366. */
  367. template<typename Char>
  368. class basic_hashed_string {
  369. using traits_type = internal::fnv1a_traits<id_type>;
  370. struct const_wrapper {
  371. // non-explicit constructor on purpose
  372. constexpr const_wrapper(const Char *curr) ENTT_NOEXCEPT: str{curr} {}
  373. const Char *str;
  374. };
  375. // Fowler–Noll–Vo hash function v. 1a - the good
  376. [[nodiscard]] static constexpr id_type helper(const Char *curr) ENTT_NOEXCEPT {
  377. auto value = traits_type::offset;
  378. while(*curr != 0) {
  379. value = (value ^ static_cast<traits_type::type>(*(curr++))) * traits_type::prime;
  380. }
  381. return value;
  382. }
  383. public:
  384. /*! @brief Character type. */
  385. using value_type = Char;
  386. /*! @brief Unsigned integer type. */
  387. using hash_type = id_type;
  388. /**
  389. * @brief Returns directly the numeric representation of a string.
  390. *
  391. * Forcing template resolution avoids implicit conversions. An
  392. * human-readable identifier can be anything but a plain, old bunch of
  393. * characters.<br/>
  394. * Example of use:
  395. * @code{.cpp}
  396. * const auto value = basic_hashed_string<char>::to_value("my.png");
  397. * @endcode
  398. *
  399. * @tparam N Number of characters of the identifier.
  400. * @param str Human-readable identifer.
  401. * @return The numeric representation of the string.
  402. */
  403. template<std::size_t N>
  404. [[nodiscard]] static constexpr hash_type value(const value_type (&str)[N]) ENTT_NOEXCEPT {
  405. return helper(str);
  406. }
  407. /**
  408. * @brief Returns directly the numeric representation of a string.
  409. * @param wrapper Helps achieving the purpose by relying on overloading.
  410. * @return The numeric representation of the string.
  411. */
  412. [[nodiscard]] static hash_type value(const_wrapper wrapper) ENTT_NOEXCEPT {
  413. return helper(wrapper.str);
  414. }
  415. /**
  416. * @brief Returns directly the numeric representation of a string view.
  417. * @param str Human-readable identifer.
  418. * @param size Length of the string to hash.
  419. * @return The numeric representation of the string.
  420. */
  421. [[nodiscard]] static hash_type value(const value_type *str, std::size_t size) ENTT_NOEXCEPT {
  422. id_type partial{traits_type::offset};
  423. while(size--) { partial = (partial^(str++)[0])*traits_type::prime; }
  424. return partial;
  425. }
  426. /*! @brief Constructs an empty hashed string. */
  427. constexpr basic_hashed_string() ENTT_NOEXCEPT
  428. : str{nullptr}, hash{}
  429. {}
  430. /**
  431. * @brief Constructs a hashed string from an array of const characters.
  432. *
  433. * Forcing template resolution avoids implicit conversions. An
  434. * human-readable identifier can be anything but a plain, old bunch of
  435. * characters.<br/>
  436. * Example of use:
  437. * @code{.cpp}
  438. * basic_hashed_string<char> hs{"my.png"};
  439. * @endcode
  440. *
  441. * @tparam N Number of characters of the identifier.
  442. * @param curr Human-readable identifer.
  443. */
  444. template<std::size_t N>
  445. constexpr basic_hashed_string(const value_type (&curr)[N]) ENTT_NOEXCEPT
  446. : str{curr}, hash{helper(curr)}
  447. {}
  448. /**
  449. * @brief Explicit constructor on purpose to avoid constructing a hashed
  450. * string directly from a `const value_type *`.
  451. * @param wrapper Helps achieving the purpose by relying on overloading.
  452. */
  453. explicit constexpr basic_hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
  454. : str{wrapper.str}, hash{helper(wrapper.str)}
  455. {}
  456. /**
  457. * @brief Returns the human-readable representation of a hashed string.
  458. * @return The string used to initialize the instance.
  459. */
  460. [[nodiscard]] constexpr const value_type * data() const ENTT_NOEXCEPT {
  461. return str;
  462. }
  463. /**
  464. * @brief Returns the numeric representation of a hashed string.
  465. * @return The numeric representation of the instance.
  466. */
  467. [[nodiscard]] constexpr hash_type value() const ENTT_NOEXCEPT {
  468. return hash;
  469. }
  470. /*! @copydoc data */
  471. [[nodiscard]] constexpr operator const value_type *() const ENTT_NOEXCEPT { return data(); }
  472. /**
  473. * @brief Returns the numeric representation of a hashed string.
  474. * @return The numeric representation of the instance.
  475. */
  476. [[nodiscard]] constexpr operator hash_type() const ENTT_NOEXCEPT { return value(); }
  477. /**
  478. * @brief Compares two hashed strings.
  479. * @param other Hashed string with which to compare.
  480. * @return True if the two hashed strings are identical, false otherwise.
  481. */
  482. [[nodiscard]] constexpr bool operator==(const basic_hashed_string &other) const ENTT_NOEXCEPT {
  483. return hash == other.hash;
  484. }
  485. private:
  486. const value_type *str;
  487. hash_type hash;
  488. };
  489. /**
  490. * @brief Deduction guide.
  491. *
  492. * It allows to deduce the character type of the hashed string directly from a
  493. * human-readable identifer provided to the constructor.
  494. *
  495. * @tparam Char Character type.
  496. * @tparam N Number of characters of the identifier.
  497. * @param str Human-readable identifer.
  498. */
  499. template<typename Char, std::size_t N>
  500. basic_hashed_string(const Char (&str)[N]) ENTT_NOEXCEPT
  501. -> basic_hashed_string<Char>;
  502. /**
  503. * @brief Compares two hashed strings.
  504. * @tparam Char Character type.
  505. * @param lhs A valid hashed string.
  506. * @param rhs A valid hashed string.
  507. * @return True if the two hashed strings are identical, false otherwise.
  508. */
  509. template<typename Char>
  510. [[nodiscard]] constexpr bool operator!=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) ENTT_NOEXCEPT {
  511. return !(lhs == rhs);
  512. }
  513. /*! @brief Aliases for common character types. */
  514. using hashed_string = basic_hashed_string<char>;
  515. /*! @brief Aliases for common character types. */
  516. using hashed_wstring = basic_hashed_string<wchar_t>;
  517. }
  518. /**
  519. * @brief User defined literal for hashed strings.
  520. * @param str The literal without its suffix.
  521. * @return A properly initialized hashed string.
  522. */
  523. [[nodiscard]] constexpr entt::hashed_string operator"" ENTT_HS_SUFFIX(const char *str, std::size_t) ENTT_NOEXCEPT {
  524. return entt::hashed_string{str};
  525. }
  526. /**
  527. * @brief User defined literal for hashed wstrings.
  528. * @param str The literal without its suffix.
  529. * @return A properly initialized hashed wstring.
  530. */
  531. [[nodiscard]] constexpr entt::hashed_wstring operator"" ENTT_HWS_SUFFIX(const wchar_t *str, std::size_t) ENTT_NOEXCEPT {
  532. return entt::hashed_wstring{str};
  533. }
  534. #endif
  535. // #include "core/ident.hpp"
  536. #ifndef ENTT_CORE_IDENT_HPP
  537. #define ENTT_CORE_IDENT_HPP
  538. #include <tuple>
  539. #include <cstddef>
  540. #include <utility>
  541. #include <type_traits>
  542. // #include "../config/config.h"
  543. // #include "fwd.hpp"
  544. namespace entt {
  545. /**
  546. * @brief Types identifiers.
  547. *
  548. * Variable template used to generate identifiers at compile-time for the given
  549. * types. Use the `get` member function to know what's the identifier associated
  550. * to the specific type.
  551. *
  552. * @note
  553. * Identifiers are constant expression and can be used in any context where such
  554. * an expression is required. As an example:
  555. * @code{.cpp}
  556. * using id = entt::identifier<a_type, another_type>;
  557. *
  558. * switch(a_type_identifier) {
  559. * case id::type<a_type>:
  560. * // ...
  561. * break;
  562. * case id::type<another_type>:
  563. * // ...
  564. * break;
  565. * default:
  566. * // ...
  567. * }
  568. * @endcode
  569. *
  570. * @tparam Types List of types for which to generate identifiers.
  571. */
  572. template<typename... Types>
  573. class identifier {
  574. using tuple_type = std::tuple<std::decay_t<Types>...>;
  575. template<typename Type, std::size_t... Indexes>
  576. [[nodiscard]] static constexpr id_type get(std::index_sequence<Indexes...>) {
  577. static_assert(std::disjunction_v<std::is_same<Type, Types>...>, "Invalid type");
  578. return (0 + ... + (std::is_same_v<Type, std::tuple_element_t<Indexes, tuple_type>> ? id_type(Indexes) : id_type{}));
  579. }
  580. public:
  581. /*! @brief Unsigned integer type. */
  582. using identifier_type = id_type;
  583. /*! @brief Statically generated unique identifier for the given type. */
  584. template<typename Type>
  585. static constexpr identifier_type type = get<std::decay_t<Type>>(std::index_sequence_for<Types...>{});
  586. };
  587. }
  588. #endif
  589. // #include "core/monostate.hpp"
  590. #ifndef ENTT_CORE_MONOSTATE_HPP
  591. #define ENTT_CORE_MONOSTATE_HPP
  592. // #include "../config/config.h"
  593. // #include "fwd.hpp"
  594. namespace entt {
  595. /**
  596. * @brief Minimal implementation of the monostate pattern.
  597. *
  598. * A minimal, yet complete configuration system built on top of the monostate
  599. * pattern. Thread safe by design, it works only with basic types like `int`s or
  600. * `bool`s.<br/>
  601. * Multiple types and therefore more than one value can be associated with a
  602. * single key. Because of this, users must pay attention to use the same type
  603. * both during an assignment and when they try to read back their data.
  604. * Otherwise, they can incur in unexpected results.
  605. */
  606. template<id_type>
  607. struct monostate {
  608. /**
  609. * @brief Assigns a value of a specific type to a given key.
  610. * @tparam Type Type of the value to assign.
  611. * @param val User data to assign to the given key.
  612. */
  613. template<typename Type>
  614. void operator=(Type val) const ENTT_NOEXCEPT {
  615. value<Type> = val;
  616. }
  617. /**
  618. * @brief Gets a value of a specific type for a given key.
  619. * @tparam Type Type of the value to get.
  620. * @return Stored value, if any.
  621. */
  622. template<typename Type>
  623. operator Type() const ENTT_NOEXCEPT {
  624. return value<Type>;
  625. }
  626. private:
  627. template<typename Type>
  628. inline static ENTT_MAYBE_ATOMIC(Type) value{};
  629. };
  630. /**
  631. * @brief Helper variable template.
  632. * @tparam Value Value used to differentiate between different variables.
  633. */
  634. template<id_type Value>
  635. inline monostate<Value> monostate_v = {};
  636. }
  637. #endif
  638. // #include "core/type_info.hpp"
  639. #ifndef ENTT_CORE_TYPE_INFO_HPP
  640. #define ENTT_CORE_TYPE_INFO_HPP
  641. #include <string_view>
  642. // #include "../config/config.h"
  643. // #include "../core/attribute.h"
  644. #ifndef ENTT_CORE_ATTRIBUTE_H
  645. #define ENTT_CORE_ATTRIBUTE_H
  646. #ifndef ENTT_EXPORT
  647. # if defined _WIN32 || defined __CYGWIN__ || defined _MSC_VER
  648. # define ENTT_EXPORT __declspec(dllexport)
  649. # define ENTT_IMPORT __declspec(dllimport)
  650. # define ENTT_HIDDEN
  651. # elif defined __GNUC__ && __GNUC__ >= 4
  652. # define ENTT_EXPORT __attribute__((visibility("default")))
  653. # define ENTT_IMPORT __attribute__((visibility("default")))
  654. # define ENTT_HIDDEN __attribute__((visibility("hidden")))
  655. # else /* Unsupported compiler */
  656. # define ENTT_EXPORT
  657. # define ENTT_IMPORT
  658. # define ENTT_HIDDEN
  659. # endif
  660. #endif
  661. #ifndef ENTT_API
  662. # if defined ENTT_API_EXPORT
  663. # define ENTT_API ENTT_EXPORT
  664. # elif defined ENTT_API_IMPORT
  665. # define ENTT_API ENTT_IMPORT
  666. # else /* No API */
  667. # define ENTT_API
  668. # endif
  669. #endif
  670. #endif
  671. // #include "hashed_string.hpp"
  672. // #include "fwd.hpp"
  673. namespace entt {
  674. /**
  675. * @cond TURN_OFF_DOXYGEN
  676. * Internal details not to be documented.
  677. */
  678. namespace internal {
  679. struct ENTT_API type_index {
  680. [[nodiscard]] static id_type next() ENTT_NOEXCEPT {
  681. static ENTT_MAYBE_ATOMIC(id_type) value{};
  682. return value++;
  683. }
  684. };
  685. template<typename Type>
  686. [[nodiscard]] constexpr auto type_name() ENTT_NOEXCEPT {
  687. #if defined ENTT_PRETTY_FUNCTION
  688. std::string_view pretty_function{ENTT_PRETTY_FUNCTION};
  689. auto first = pretty_function.find_first_not_of(' ', pretty_function.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX)+1);
  690. auto value = pretty_function.substr(first, pretty_function.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
  691. return value;
  692. #else
  693. return std::string_view{};
  694. #endif
  695. }
  696. }
  697. /**
  698. * Internal details not to be documented.
  699. * @endcond
  700. */
  701. /**
  702. * @brief Type index.
  703. * @tparam Type Type for which to generate a sequential identifier.
  704. */
  705. template<typename Type, typename = void>
  706. struct ENTT_API type_index {
  707. /**
  708. * @brief Returns the sequential identifier of a given type.
  709. * @return The sequential identifier of a given type.
  710. */
  711. [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
  712. static const id_type value = internal::type_index::next();
  713. return value;
  714. }
  715. };
  716. /**
  717. * @brief Provides the member constant `value` to true if a given type is
  718. * indexable, false otherwise.
  719. * @tparam Type Potentially indexable type.
  720. */
  721. template<typename, typename = void>
  722. struct has_type_index: std::false_type {};
  723. /*! @brief has_type_index */
  724. template<typename Type>
  725. struct has_type_index<Type, std::void_t<decltype(type_index<Type>::value())>>: std::true_type {};
  726. /**
  727. * @brief Helper variable template.
  728. * @tparam Type Potentially indexable type.
  729. */
  730. template<typename Type>
  731. inline constexpr bool has_type_index_v = has_type_index<Type>::value;
  732. /**
  733. * @brief Type info.
  734. * @tparam Type Type for which to generate information.
  735. */
  736. template<typename Type, typename = void>
  737. struct type_info {
  738. /**
  739. * @brief Returns the numeric representation of a given type.
  740. * @return The numeric representation of the given type.
  741. */
  742. #if defined ENTT_PRETTY_FUNCTION_CONSTEXPR
  743. [[nodiscard]] static constexpr id_type id() ENTT_NOEXCEPT {
  744. constexpr auto value = hashed_string::value(ENTT_PRETTY_FUNCTION);
  745. return value;
  746. }
  747. #elif defined ENTT_PRETTY_FUNCTION
  748. [[nodiscard]] static id_type id() ENTT_NOEXCEPT {
  749. static const auto value = hashed_string::value(ENTT_PRETTY_FUNCTION);
  750. return value;
  751. }
  752. #else
  753. [[nodiscard]] static id_type id() ENTT_NOEXCEPT {
  754. return type_index<Type>::value();
  755. }
  756. #endif
  757. /**
  758. * @brief Returns the name of a given type.
  759. * @return The name of the given type.
  760. */
  761. #if defined ENTT_PRETTY_FUNCTION_CONSTEXPR
  762. [[nodiscard]] static constexpr std::string_view name() ENTT_NOEXCEPT {
  763. constexpr auto value = internal::type_name<Type>();
  764. return value;
  765. }
  766. #elif defined ENTT_PRETTY_FUNCTION
  767. [[nodiscard]] static std::string_view name() ENTT_NOEXCEPT {
  768. static const auto value = internal::type_name<Type>();
  769. return value;
  770. }
  771. #else
  772. [[nodiscard]] static constexpr std::string_view name() ENTT_NOEXCEPT {
  773. return internal::type_name<Type>();
  774. }
  775. #endif
  776. };
  777. }
  778. #endif
  779. // #include "core/type_traits.hpp"
  780. #ifndef ENTT_CORE_TYPE_TRAITS_HPP
  781. #define ENTT_CORE_TYPE_TRAITS_HPP
  782. #include <cstddef>
  783. #include <utility>
  784. #include <type_traits>
  785. // #include "../config/config.h"
  786. // #include "fwd.hpp"
  787. namespace entt {
  788. /**
  789. * @brief Using declaration to be used to _repeat_ the same type a number of
  790. * times equal to the size of a given parameter pack.
  791. * @tparam Type A type to repeat.
  792. */
  793. template<typename Type, typename>
  794. using unpack_as_t = Type;
  795. /**
  796. * @brief Helper variable template to be used to _repeat_ the same value a
  797. * number of times equal to the size of a given parameter pack.
  798. * @tparam Value A value to repeat.
  799. */
  800. template<auto Value, typename>
  801. inline constexpr auto unpack_as_v = Value;
  802. /**
  803. * @brief Wraps a static constant.
  804. * @tparam Value A static constant.
  805. */
  806. template<auto Value>
  807. using integral_constant = std::integral_constant<decltype(Value), Value>;
  808. /**
  809. * @brief Alias template to ease the creation of named values.
  810. * @tparam Value A constant value at least convertible to `id_type`.
  811. */
  812. template<id_type Value>
  813. using tag = integral_constant<Value>;
  814. /**
  815. * @brief Utility class to disambiguate overloaded functions.
  816. * @tparam N Number of choices available.
  817. */
  818. template<std::size_t N>
  819. struct choice_t
  820. // Unfortunately, doxygen cannot parse such a construct.
  821. /*! @cond TURN_OFF_DOXYGEN */
  822. : choice_t<N-1>
  823. /*! @endcond */
  824. {};
  825. /*! @copybrief choice_t */
  826. template<>
  827. struct choice_t<0> {};
  828. /**
  829. * @brief Variable template for the choice trick.
  830. * @tparam N Number of choices available.
  831. */
  832. template<std::size_t N>
  833. inline constexpr choice_t<N> choice{};
  834. /*! @brief A class to use to push around lists of types, nothing more. */
  835. template<typename...>
  836. struct type_list {};
  837. /*! @brief Primary template isn't defined on purpose. */
  838. template<typename>
  839. struct type_list_size;
  840. /**
  841. * @brief Compile-time number of elements in a type list.
  842. * @tparam Type Types provided by the type list.
  843. */
  844. template<typename... Type>
  845. struct type_list_size<type_list<Type...>>
  846. : std::integral_constant<std::size_t, sizeof...(Type)>
  847. {};
  848. /**
  849. * @brief Helper variable template.
  850. * @tparam List Type list.
  851. */
  852. template<class List>
  853. inline constexpr auto type_list_size_v = type_list_size<List>::value;
  854. /*! @brief Primary template isn't defined on purpose. */
  855. template<typename...>
  856. struct type_list_cat;
  857. /*! @brief Concatenates multiple type lists. */
  858. template<>
  859. struct type_list_cat<> {
  860. /*! @brief A type list composed by the types of all the type lists. */
  861. using type = type_list<>;
  862. };
  863. /**
  864. * @brief Concatenates multiple type lists.
  865. * @tparam Type Types provided by the first type list.
  866. * @tparam Other Types provided by the second type list.
  867. * @tparam List Other type lists, if any.
  868. */
  869. template<typename... Type, typename... Other, typename... List>
  870. struct type_list_cat<type_list<Type...>, type_list<Other...>, List...> {
  871. /*! @brief A type list composed by the types of all the type lists. */
  872. using type = typename type_list_cat<type_list<Type..., Other...>, List...>::type;
  873. };
  874. /**
  875. * @brief Concatenates multiple type lists.
  876. * @tparam Type Types provided by the type list.
  877. */
  878. template<typename... Type>
  879. struct type_list_cat<type_list<Type...>> {
  880. /*! @brief A type list composed by the types of all the type lists. */
  881. using type = type_list<Type...>;
  882. };
  883. /**
  884. * @brief Helper type.
  885. * @tparam List Type lists to concatenate.
  886. */
  887. template<typename... List>
  888. using type_list_cat_t = typename type_list_cat<List...>::type;
  889. /*! @brief Primary template isn't defined on purpose. */
  890. template<typename>
  891. struct type_list_unique;
  892. /**
  893. * @brief Removes duplicates types from a type list.
  894. * @tparam Type One of the types provided by the given type list.
  895. * @tparam Other The other types provided by the given type list.
  896. */
  897. template<typename Type, typename... Other>
  898. struct type_list_unique<type_list<Type, Other...>> {
  899. /*! @brief A type list without duplicate types. */
  900. using type = std::conditional_t<
  901. std::disjunction_v<std::is_same<Type, Other>...>,
  902. typename type_list_unique<type_list<Other...>>::type,
  903. type_list_cat_t<type_list<Type>, typename type_list_unique<type_list<Other...>>::type>
  904. >;
  905. };
  906. /*! @brief Removes duplicates types from a type list. */
  907. template<>
  908. struct type_list_unique<type_list<>> {
  909. /*! @brief A type list without duplicate types. */
  910. using type = type_list<>;
  911. };
  912. /**
  913. * @brief Helper type.
  914. * @tparam Type A type list.
  915. */
  916. template<typename Type>
  917. using type_list_unique_t = typename type_list_unique<Type>::type;
  918. /**
  919. * @brief Provides the member constant `value` to true if a given type is
  920. * equality comparable, false otherwise.
  921. * @tparam Type Potentially equality comparable type.
  922. */
  923. template<typename Type, typename = std::void_t<>>
  924. struct is_equality_comparable: std::false_type {};
  925. /*! @copydoc is_equality_comparable */
  926. template<typename Type>
  927. struct is_equality_comparable<Type, std::void_t<decltype(std::declval<Type>() == std::declval<Type>())>>
  928. : std::true_type
  929. {};
  930. /**
  931. * @brief Helper variable template.
  932. * @tparam Type Potentially equality comparable type.
  933. */
  934. template<class Type>
  935. inline constexpr auto is_equality_comparable_v = is_equality_comparable<Type>::value;
  936. /**
  937. * @brief Provides the member constant `value` to true if a given type is empty
  938. * and the empty type optimization is enabled, false otherwise.
  939. * @tparam Type Potential empty type.
  940. */
  941. template<typename Type, typename = void>
  942. struct is_eto_eligible
  943. : ENTT_IS_EMPTY(Type)
  944. {};
  945. /**
  946. * @brief Helper variable template.
  947. * @tparam Type Potential empty type.
  948. */
  949. template<typename Type>
  950. inline constexpr auto is_eto_eligible_v = is_eto_eligible<Type>::value;
  951. /**
  952. * @brief Extracts the class of a non-static member object or function.
  953. * @tparam Member A pointer to a non-static member object or function.
  954. */
  955. template<typename Member>
  956. class member_class {
  957. static_assert(std::is_member_pointer_v<Member>, "Invalid pointer type to non-static member object or function");
  958. template<typename Class, typename Ret, typename... Args>
  959. static Class * clazz(Ret(Class:: *)(Args...));
  960. template<typename Class, typename Ret, typename... Args>
  961. static Class * clazz(Ret(Class:: *)(Args...) const);
  962. template<typename Class, typename Type>
  963. static Class * clazz(Type Class:: *);
  964. public:
  965. /*! @brief The class of the given non-static member object or function. */
  966. using type = std::remove_pointer_t<decltype(clazz(std::declval<Member>()))>;
  967. };
  968. /**
  969. * @brief Helper type.
  970. * @tparam Member A pointer to a non-static member object or function.
  971. */
  972. template<typename Member>
  973. using member_class_t = typename member_class<Member>::type;
  974. }
  975. #endif
  976. // #include "core/utility.hpp"
  977. // #include "entity/actor.hpp"
  978. #ifndef ENTT_ENTITY_ACTOR_HPP
  979. #define ENTT_ENTITY_ACTOR_HPP
  980. #include <utility>
  981. #include <type_traits>
  982. // #include "../config/config.h"
  983. #ifndef ENTT_CONFIG_CONFIG_H
  984. #define ENTT_CONFIG_CONFIG_H
  985. #ifndef ENTT_NOEXCEPT
  986. # define ENTT_NOEXCEPT noexcept
  987. #endif
  988. #ifndef ENTT_HS_SUFFIX
  989. # define ENTT_HS_SUFFIX _hs
  990. #endif
  991. #ifndef ENTT_HWS_SUFFIX
  992. # define ENTT_HWS_SUFFIX _hws
  993. #endif
  994. #ifndef ENTT_USE_ATOMIC
  995. # define ENTT_MAYBE_ATOMIC(Type) Type
  996. #else
  997. # include <atomic>
  998. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  999. #endif
  1000. #ifndef ENTT_ID_TYPE
  1001. # include <cstdint>
  1002. # define ENTT_ID_TYPE std::uint32_t
  1003. #endif
  1004. #ifndef ENTT_PAGE_SIZE
  1005. # define ENTT_PAGE_SIZE 32768
  1006. #endif
  1007. #ifndef ENTT_ASSERT
  1008. # include <cassert>
  1009. # define ENTT_ASSERT(condition) assert(condition)
  1010. #endif
  1011. #ifndef ENTT_NO_ETO
  1012. # include <type_traits>
  1013. # define ENTT_IS_EMPTY(Type) std::is_empty<Type>
  1014. #else
  1015. # include <type_traits>
  1016. # define ENTT_IS_EMPTY(Type) std::false_type
  1017. #endif
  1018. #ifndef ENTT_STANDARD_CPP
  1019. # if defined __clang__ || (defined __GNUC__ && __GNUC__ > 8)
  1020. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  1021. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  1022. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  1023. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  1024. # elif defined __GNUC__
  1025. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  1026. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  1027. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  1028. # elif defined _MSC_VER
  1029. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  1030. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  1031. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  1032. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  1033. # endif
  1034. #endif
  1035. #ifndef ENTT_STANDALONE
  1036. # define ENTT_FAST_PATH(...) false
  1037. #else
  1038. # define ENTT_FAST_PATH(Cond) Cond
  1039. #endif
  1040. #endif
  1041. // #include "registry.hpp"
  1042. #ifndef ENTT_ENTITY_REGISTRY_HPP
  1043. #define ENTT_ENTITY_REGISTRY_HPP
  1044. #include <algorithm>
  1045. #include <cstddef>
  1046. #include <iterator>
  1047. #include <memory>
  1048. #include <tuple>
  1049. #include <type_traits>
  1050. #include <utility>
  1051. #include <vector>
  1052. // #include "../config/config.h"
  1053. // #include "../core/algorithm.hpp"
  1054. #ifndef ENTT_CORE_ALGORITHM_HPP
  1055. #define ENTT_CORE_ALGORITHM_HPP
  1056. #include <vector>
  1057. #include <utility>
  1058. #include <iterator>
  1059. #include <algorithm>
  1060. #include <functional>
  1061. // #include "utility.hpp"
  1062. #ifndef ENTT_CORE_UTILITY_HPP
  1063. #define ENTT_CORE_UTILITY_HPP
  1064. #include <utility>
  1065. // #include "../config/config.h"
  1066. #ifndef ENTT_CONFIG_CONFIG_H
  1067. #define ENTT_CONFIG_CONFIG_H
  1068. #ifndef ENTT_NOEXCEPT
  1069. # define ENTT_NOEXCEPT noexcept
  1070. #endif
  1071. #ifndef ENTT_HS_SUFFIX
  1072. # define ENTT_HS_SUFFIX _hs
  1073. #endif
  1074. #ifndef ENTT_HWS_SUFFIX
  1075. # define ENTT_HWS_SUFFIX _hws
  1076. #endif
  1077. #ifndef ENTT_USE_ATOMIC
  1078. # define ENTT_MAYBE_ATOMIC(Type) Type
  1079. #else
  1080. # include <atomic>
  1081. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  1082. #endif
  1083. #ifndef ENTT_ID_TYPE
  1084. # include <cstdint>
  1085. # define ENTT_ID_TYPE std::uint32_t
  1086. #endif
  1087. #ifndef ENTT_PAGE_SIZE
  1088. # define ENTT_PAGE_SIZE 32768
  1089. #endif
  1090. #ifndef ENTT_ASSERT
  1091. # include <cassert>
  1092. # define ENTT_ASSERT(condition) assert(condition)
  1093. #endif
  1094. #ifndef ENTT_NO_ETO
  1095. # include <type_traits>
  1096. # define ENTT_IS_EMPTY(Type) std::is_empty<Type>
  1097. #else
  1098. # include <type_traits>
  1099. # define ENTT_IS_EMPTY(Type) std::false_type
  1100. #endif
  1101. #ifndef ENTT_STANDARD_CPP
  1102. # if defined __clang__ || (defined __GNUC__ && __GNUC__ > 8)
  1103. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  1104. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  1105. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  1106. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  1107. # elif defined __GNUC__
  1108. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  1109. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  1110. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  1111. # elif defined _MSC_VER
  1112. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  1113. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  1114. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  1115. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  1116. # endif
  1117. #endif
  1118. #ifndef ENTT_STANDALONE
  1119. # define ENTT_FAST_PATH(...) false
  1120. #else
  1121. # define ENTT_FAST_PATH(Cond) Cond
  1122. #endif
  1123. #endif
  1124. namespace entt {
  1125. /*! @brief Identity function object (waiting for C++20). */
  1126. struct identity {
  1127. /**
  1128. * @brief Returns its argument unchanged.
  1129. * @tparam Type Type of the argument.
  1130. * @param value The actual argument.
  1131. * @return The submitted value as-is.
  1132. */
  1133. template<class Type>
  1134. [[nodiscard]] constexpr Type && operator()(Type &&value) const ENTT_NOEXCEPT {
  1135. return std::forward<Type>(value);
  1136. }
  1137. };
  1138. /**
  1139. * @brief Constant utility to disambiguate overloaded members of a class.
  1140. * @tparam Type Type of the desired overload.
  1141. * @tparam Class Type of class to which the member belongs.
  1142. * @param member A valid pointer to a member.
  1143. * @return Pointer to the member.
  1144. */
  1145. template<typename Type, typename Class>
  1146. [[nodiscard]] constexpr auto overload(Type Class:: *member) ENTT_NOEXCEPT { return member; }
  1147. /**
  1148. * @brief Constant utility to disambiguate overloaded functions.
  1149. * @tparam Func Function type of the desired overload.
  1150. * @param func A valid pointer to a function.
  1151. * @return Pointer to the function.
  1152. */
  1153. template<typename Func>
  1154. [[nodiscard]] constexpr auto overload(Func *func) ENTT_NOEXCEPT { return func; }
  1155. /**
  1156. * @brief Helper type for visitors.
  1157. * @tparam Func Types of function objects.
  1158. */
  1159. template<class... Func>
  1160. struct overloaded: Func... {
  1161. using Func::operator()...;
  1162. };
  1163. /**
  1164. * @brief Deduction guide.
  1165. * @tparam Func Types of function objects.
  1166. */
  1167. template<class... Func>
  1168. overloaded(Func...) -> overloaded<Func...>;
  1169. /**
  1170. * @brief Basic implementation of a y-combinator.
  1171. * @tparam Func Type of a potentially recursive function.
  1172. */
  1173. template<class Func>
  1174. struct y_combinator {
  1175. /**
  1176. * @brief Constructs a y-combinator from a given function.
  1177. * @param recursive A potentially recursive function.
  1178. */
  1179. y_combinator(Func recursive):
  1180. func{std::move(recursive)}
  1181. {}
  1182. /**
  1183. * @brief Invokes a y-combinator and therefore its underlying function.
  1184. * @tparam Args Types of arguments to use to invoke the underlying function.
  1185. * @param args Parameters to use to invoke the underlying function.
  1186. * @return Return value of the underlying function, if any.
  1187. */
  1188. template <class... Args>
  1189. decltype(auto) operator()(Args &&... args) const {
  1190. return func(*this, std::forward<Args>(args)...);
  1191. }
  1192. /*! @copydoc operator()() */
  1193. template <class... Args>
  1194. decltype(auto) operator()(Args &&... args) {
  1195. return func(*this, std::forward<Args>(args)...);
  1196. }
  1197. private:
  1198. Func func;
  1199. };
  1200. }
  1201. #endif
  1202. namespace entt {
  1203. /**
  1204. * @brief Function object to wrap `std::sort` in a class type.
  1205. *
  1206. * Unfortunately, `std::sort` cannot be passed as template argument to a class
  1207. * template or a function template.<br/>
  1208. * This class fills the gap by wrapping some flavors of `std::sort` in a
  1209. * function object.
  1210. */
  1211. struct std_sort {
  1212. /**
  1213. * @brief Sorts the elements in a range.
  1214. *
  1215. * Sorts the elements in a range using the given binary comparison function.
  1216. *
  1217. * @tparam It Type of random access iterator.
  1218. * @tparam Compare Type of comparison function object.
  1219. * @tparam Args Types of arguments to forward to the sort function.
  1220. * @param first An iterator to the first element of the range to sort.
  1221. * @param last An iterator past the last element of the range to sort.
  1222. * @param compare A valid comparison function object.
  1223. * @param args Arguments to forward to the sort function, if any.
  1224. */
  1225. template<typename It, typename Compare = std::less<>, typename... Args>
  1226. void operator()(It first, It last, Compare compare = Compare{}, Args &&... args) const {
  1227. std::sort(std::forward<Args>(args)..., std::move(first), std::move(last), std::move(compare));
  1228. }
  1229. };
  1230. /*! @brief Function object for performing insertion sort. */
  1231. struct insertion_sort {
  1232. /**
  1233. * @brief Sorts the elements in a range.
  1234. *
  1235. * Sorts the elements in a range using the given binary comparison function.
  1236. *
  1237. * @tparam It Type of random access iterator.
  1238. * @tparam Compare Type of comparison function object.
  1239. * @param first An iterator to the first element of the range to sort.
  1240. * @param last An iterator past the last element of the range to sort.
  1241. * @param compare A valid comparison function object.
  1242. */
  1243. template<typename It, typename Compare = std::less<>>
  1244. void operator()(It first, It last, Compare compare = Compare{}) const {
  1245. if(first < last) {
  1246. for(auto it = first+1; it < last; ++it) {
  1247. auto value = std::move(*it);
  1248. auto pre = it;
  1249. for(; pre > first && compare(value, *(pre-1)); --pre) {
  1250. *pre = std::move(*(pre-1));
  1251. }
  1252. *pre = std::move(value);
  1253. }
  1254. }
  1255. }
  1256. };
  1257. /**
  1258. * @brief Function object for performing LSD radix sort.
  1259. * @tparam Bit Number of bits processed per pass.
  1260. * @tparam N Maximum number of bits to sort.
  1261. */
  1262. template<std::size_t Bit, std::size_t N>
  1263. struct radix_sort {
  1264. static_assert((N % Bit) == 0, "The maximum number of bits to sort must be a multiple of the number of bits processed per pass");
  1265. /**
  1266. * @brief Sorts the elements in a range.
  1267. *
  1268. * Sorts the elements in a range using the given _getter_ to access the
  1269. * actual data to be sorted.
  1270. *
  1271. * This implementation is inspired by the online book
  1272. * [Physically Based Rendering](http://www.pbr-book.org/3ed-2018/Primitives_and_Intersection_Acceleration/Bounding_Volume_Hierarchies.html#RadixSort).
  1273. *
  1274. * @tparam It Type of random access iterator.
  1275. * @tparam Getter Type of _getter_ function object.
  1276. * @param first An iterator to the first element of the range to sort.
  1277. * @param last An iterator past the last element of the range to sort.
  1278. * @param getter A valid _getter_ function object.
  1279. */
  1280. template<typename It, typename Getter = identity>
  1281. void operator()(It first, It last, Getter getter = Getter{}) const {
  1282. if(first < last) {
  1283. static constexpr auto mask = (1 << Bit) - 1;
  1284. static constexpr auto buckets = 1 << Bit;
  1285. static constexpr auto passes = N / Bit;
  1286. using value_type = typename std::iterator_traits<It>::value_type;
  1287. std::vector<value_type> aux(std::distance(first, last));
  1288. auto part = [getter = std::move(getter)](auto from, auto to, auto out, auto start) {
  1289. std::size_t index[buckets]{};
  1290. std::size_t count[buckets]{};
  1291. std::for_each(from, to, [&getter, &count, start](const value_type &item) {
  1292. ++count[(getter(item) >> start) & mask];
  1293. });
  1294. std::for_each(std::next(std::begin(index)), std::end(index), [index = std::begin(index), count = std::begin(count)](auto &item) mutable {
  1295. item = *(index++) + *(count++);
  1296. });
  1297. std::for_each(from, to, [&getter, &out, &index, start](value_type &item) {
  1298. out[index[(getter(item) >> start) & mask]++] = std::move(item);
  1299. });
  1300. };
  1301. for(std::size_t pass = 0; pass < (passes & ~1); pass += 2) {
  1302. part(first, last, aux.begin(), pass * Bit);
  1303. part(aux.begin(), aux.end(), first, (pass + 1) * Bit);
  1304. }
  1305. if constexpr(passes & 1) {
  1306. part(first, last, aux.begin(), (passes - 1) * Bit);
  1307. std::move(aux.begin(), aux.end(), first);
  1308. }
  1309. }
  1310. }
  1311. };
  1312. }
  1313. #endif
  1314. // #include "../core/fwd.hpp"
  1315. #ifndef ENTT_CORE_FWD_HPP
  1316. #define ENTT_CORE_FWD_HPP
  1317. // #include "../config/config.h"
  1318. namespace entt {
  1319. /*! @brief Alias declaration for type identifiers. */
  1320. using id_type = ENTT_ID_TYPE;
  1321. }
  1322. #endif
  1323. // #include "../core/type_info.hpp"
  1324. #ifndef ENTT_CORE_TYPE_INFO_HPP
  1325. #define ENTT_CORE_TYPE_INFO_HPP
  1326. #include <string_view>
  1327. // #include "../config/config.h"
  1328. // #include "../core/attribute.h"
  1329. #ifndef ENTT_CORE_ATTRIBUTE_H
  1330. #define ENTT_CORE_ATTRIBUTE_H
  1331. #ifndef ENTT_EXPORT
  1332. # if defined _WIN32 || defined __CYGWIN__ || defined _MSC_VER
  1333. # define ENTT_EXPORT __declspec(dllexport)
  1334. # define ENTT_IMPORT __declspec(dllimport)
  1335. # define ENTT_HIDDEN
  1336. # elif defined __GNUC__ && __GNUC__ >= 4
  1337. # define ENTT_EXPORT __attribute__((visibility("default")))
  1338. # define ENTT_IMPORT __attribute__((visibility("default")))
  1339. # define ENTT_HIDDEN __attribute__((visibility("hidden")))
  1340. # else /* Unsupported compiler */
  1341. # define ENTT_EXPORT
  1342. # define ENTT_IMPORT
  1343. # define ENTT_HIDDEN
  1344. # endif
  1345. #endif
  1346. #ifndef ENTT_API
  1347. # if defined ENTT_API_EXPORT
  1348. # define ENTT_API ENTT_EXPORT
  1349. # elif defined ENTT_API_IMPORT
  1350. # define ENTT_API ENTT_IMPORT
  1351. # else /* No API */
  1352. # define ENTT_API
  1353. # endif
  1354. #endif
  1355. #endif
  1356. // #include "hashed_string.hpp"
  1357. #ifndef ENTT_CORE_HASHED_STRING_HPP
  1358. #define ENTT_CORE_HASHED_STRING_HPP
  1359. #include <cstddef>
  1360. #include <cstdint>
  1361. // #include "../config/config.h"
  1362. // #include "fwd.hpp"
  1363. namespace entt {
  1364. /**
  1365. * @cond TURN_OFF_DOXYGEN
  1366. * Internal details not to be documented.
  1367. */
  1368. namespace internal {
  1369. template<typename>
  1370. struct fnv1a_traits;
  1371. template<>
  1372. struct fnv1a_traits<std::uint32_t> {
  1373. using type = std::uint32_t;
  1374. static constexpr std::uint32_t offset = 2166136261;
  1375. static constexpr std::uint32_t prime = 16777619;
  1376. };
  1377. template<>
  1378. struct fnv1a_traits<std::uint64_t> {
  1379. using type = std::uint64_t;
  1380. static constexpr std::uint64_t offset = 14695981039346656037ull;
  1381. static constexpr std::uint64_t prime = 1099511628211ull;
  1382. };
  1383. }
  1384. /**
  1385. * Internal details not to be documented.
  1386. * @endcond
  1387. */
  1388. /**
  1389. * @brief Zero overhead unique identifier.
  1390. *
  1391. * A hashed string is a compile-time tool that allows users to use
  1392. * human-readable identifers in the codebase while using their numeric
  1393. * counterparts at runtime.<br/>
  1394. * Because of that, a hashed string can also be used in constant expressions if
  1395. * required.
  1396. *
  1397. * @tparam Char Character type.
  1398. */
  1399. template<typename Char>
  1400. class basic_hashed_string {
  1401. using traits_type = internal::fnv1a_traits<id_type>;
  1402. struct const_wrapper {
  1403. // non-explicit constructor on purpose
  1404. constexpr const_wrapper(const Char *curr) ENTT_NOEXCEPT: str{curr} {}
  1405. const Char *str;
  1406. };
  1407. // Fowler–Noll–Vo hash function v. 1a - the good
  1408. [[nodiscard]] static constexpr id_type helper(const Char *curr) ENTT_NOEXCEPT {
  1409. auto value = traits_type::offset;
  1410. while(*curr != 0) {
  1411. value = (value ^ static_cast<traits_type::type>(*(curr++))) * traits_type::prime;
  1412. }
  1413. return value;
  1414. }
  1415. public:
  1416. /*! @brief Character type. */
  1417. using value_type = Char;
  1418. /*! @brief Unsigned integer type. */
  1419. using hash_type = id_type;
  1420. /**
  1421. * @brief Returns directly the numeric representation of a string.
  1422. *
  1423. * Forcing template resolution avoids implicit conversions. An
  1424. * human-readable identifier can be anything but a plain, old bunch of
  1425. * characters.<br/>
  1426. * Example of use:
  1427. * @code{.cpp}
  1428. * const auto value = basic_hashed_string<char>::to_value("my.png");
  1429. * @endcode
  1430. *
  1431. * @tparam N Number of characters of the identifier.
  1432. * @param str Human-readable identifer.
  1433. * @return The numeric representation of the string.
  1434. */
  1435. template<std::size_t N>
  1436. [[nodiscard]] static constexpr hash_type value(const value_type (&str)[N]) ENTT_NOEXCEPT {
  1437. return helper(str);
  1438. }
  1439. /**
  1440. * @brief Returns directly the numeric representation of a string.
  1441. * @param wrapper Helps achieving the purpose by relying on overloading.
  1442. * @return The numeric representation of the string.
  1443. */
  1444. [[nodiscard]] static hash_type value(const_wrapper wrapper) ENTT_NOEXCEPT {
  1445. return helper(wrapper.str);
  1446. }
  1447. /**
  1448. * @brief Returns directly the numeric representation of a string view.
  1449. * @param str Human-readable identifer.
  1450. * @param size Length of the string to hash.
  1451. * @return The numeric representation of the string.
  1452. */
  1453. [[nodiscard]] static hash_type value(const value_type *str, std::size_t size) ENTT_NOEXCEPT {
  1454. id_type partial{traits_type::offset};
  1455. while(size--) { partial = (partial^(str++)[0])*traits_type::prime; }
  1456. return partial;
  1457. }
  1458. /*! @brief Constructs an empty hashed string. */
  1459. constexpr basic_hashed_string() ENTT_NOEXCEPT
  1460. : str{nullptr}, hash{}
  1461. {}
  1462. /**
  1463. * @brief Constructs a hashed string from an array of const characters.
  1464. *
  1465. * Forcing template resolution avoids implicit conversions. An
  1466. * human-readable identifier can be anything but a plain, old bunch of
  1467. * characters.<br/>
  1468. * Example of use:
  1469. * @code{.cpp}
  1470. * basic_hashed_string<char> hs{"my.png"};
  1471. * @endcode
  1472. *
  1473. * @tparam N Number of characters of the identifier.
  1474. * @param curr Human-readable identifer.
  1475. */
  1476. template<std::size_t N>
  1477. constexpr basic_hashed_string(const value_type (&curr)[N]) ENTT_NOEXCEPT
  1478. : str{curr}, hash{helper(curr)}
  1479. {}
  1480. /**
  1481. * @brief Explicit constructor on purpose to avoid constructing a hashed
  1482. * string directly from a `const value_type *`.
  1483. * @param wrapper Helps achieving the purpose by relying on overloading.
  1484. */
  1485. explicit constexpr basic_hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
  1486. : str{wrapper.str}, hash{helper(wrapper.str)}
  1487. {}
  1488. /**
  1489. * @brief Returns the human-readable representation of a hashed string.
  1490. * @return The string used to initialize the instance.
  1491. */
  1492. [[nodiscard]] constexpr const value_type * data() const ENTT_NOEXCEPT {
  1493. return str;
  1494. }
  1495. /**
  1496. * @brief Returns the numeric representation of a hashed string.
  1497. * @return The numeric representation of the instance.
  1498. */
  1499. [[nodiscard]] constexpr hash_type value() const ENTT_NOEXCEPT {
  1500. return hash;
  1501. }
  1502. /*! @copydoc data */
  1503. [[nodiscard]] constexpr operator const value_type *() const ENTT_NOEXCEPT { return data(); }
  1504. /**
  1505. * @brief Returns the numeric representation of a hashed string.
  1506. * @return The numeric representation of the instance.
  1507. */
  1508. [[nodiscard]] constexpr operator hash_type() const ENTT_NOEXCEPT { return value(); }
  1509. /**
  1510. * @brief Compares two hashed strings.
  1511. * @param other Hashed string with which to compare.
  1512. * @return True if the two hashed strings are identical, false otherwise.
  1513. */
  1514. [[nodiscard]] constexpr bool operator==(const basic_hashed_string &other) const ENTT_NOEXCEPT {
  1515. return hash == other.hash;
  1516. }
  1517. private:
  1518. const value_type *str;
  1519. hash_type hash;
  1520. };
  1521. /**
  1522. * @brief Deduction guide.
  1523. *
  1524. * It allows to deduce the character type of the hashed string directly from a
  1525. * human-readable identifer provided to the constructor.
  1526. *
  1527. * @tparam Char Character type.
  1528. * @tparam N Number of characters of the identifier.
  1529. * @param str Human-readable identifer.
  1530. */
  1531. template<typename Char, std::size_t N>
  1532. basic_hashed_string(const Char (&str)[N]) ENTT_NOEXCEPT
  1533. -> basic_hashed_string<Char>;
  1534. /**
  1535. * @brief Compares two hashed strings.
  1536. * @tparam Char Character type.
  1537. * @param lhs A valid hashed string.
  1538. * @param rhs A valid hashed string.
  1539. * @return True if the two hashed strings are identical, false otherwise.
  1540. */
  1541. template<typename Char>
  1542. [[nodiscard]] constexpr bool operator!=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) ENTT_NOEXCEPT {
  1543. return !(lhs == rhs);
  1544. }
  1545. /*! @brief Aliases for common character types. */
  1546. using hashed_string = basic_hashed_string<char>;
  1547. /*! @brief Aliases for common character types. */
  1548. using hashed_wstring = basic_hashed_string<wchar_t>;
  1549. }
  1550. /**
  1551. * @brief User defined literal for hashed strings.
  1552. * @param str The literal without its suffix.
  1553. * @return A properly initialized hashed string.
  1554. */
  1555. [[nodiscard]] constexpr entt::hashed_string operator"" ENTT_HS_SUFFIX(const char *str, std::size_t) ENTT_NOEXCEPT {
  1556. return entt::hashed_string{str};
  1557. }
  1558. /**
  1559. * @brief User defined literal for hashed wstrings.
  1560. * @param str The literal without its suffix.
  1561. * @return A properly initialized hashed wstring.
  1562. */
  1563. [[nodiscard]] constexpr entt::hashed_wstring operator"" ENTT_HWS_SUFFIX(const wchar_t *str, std::size_t) ENTT_NOEXCEPT {
  1564. return entt::hashed_wstring{str};
  1565. }
  1566. #endif
  1567. // #include "fwd.hpp"
  1568. namespace entt {
  1569. /**
  1570. * @cond TURN_OFF_DOXYGEN
  1571. * Internal details not to be documented.
  1572. */
  1573. namespace internal {
  1574. struct ENTT_API type_index {
  1575. [[nodiscard]] static id_type next() ENTT_NOEXCEPT {
  1576. static ENTT_MAYBE_ATOMIC(id_type) value{};
  1577. return value++;
  1578. }
  1579. };
  1580. template<typename Type>
  1581. [[nodiscard]] constexpr auto type_name() ENTT_NOEXCEPT {
  1582. #if defined ENTT_PRETTY_FUNCTION
  1583. std::string_view pretty_function{ENTT_PRETTY_FUNCTION};
  1584. auto first = pretty_function.find_first_not_of(' ', pretty_function.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX)+1);
  1585. auto value = pretty_function.substr(first, pretty_function.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
  1586. return value;
  1587. #else
  1588. return std::string_view{};
  1589. #endif
  1590. }
  1591. }
  1592. /**
  1593. * Internal details not to be documented.
  1594. * @endcond
  1595. */
  1596. /**
  1597. * @brief Type index.
  1598. * @tparam Type Type for which to generate a sequential identifier.
  1599. */
  1600. template<typename Type, typename = void>
  1601. struct ENTT_API type_index {
  1602. /**
  1603. * @brief Returns the sequential identifier of a given type.
  1604. * @return The sequential identifier of a given type.
  1605. */
  1606. [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
  1607. static const id_type value = internal::type_index::next();
  1608. return value;
  1609. }
  1610. };
  1611. /**
  1612. * @brief Provides the member constant `value` to true if a given type is
  1613. * indexable, false otherwise.
  1614. * @tparam Type Potentially indexable type.
  1615. */
  1616. template<typename, typename = void>
  1617. struct has_type_index: std::false_type {};
  1618. /*! @brief has_type_index */
  1619. template<typename Type>
  1620. struct has_type_index<Type, std::void_t<decltype(type_index<Type>::value())>>: std::true_type {};
  1621. /**
  1622. * @brief Helper variable template.
  1623. * @tparam Type Potentially indexable type.
  1624. */
  1625. template<typename Type>
  1626. inline constexpr bool has_type_index_v = has_type_index<Type>::value;
  1627. /**
  1628. * @brief Type info.
  1629. * @tparam Type Type for which to generate information.
  1630. */
  1631. template<typename Type, typename = void>
  1632. struct type_info {
  1633. /**
  1634. * @brief Returns the numeric representation of a given type.
  1635. * @return The numeric representation of the given type.
  1636. */
  1637. #if defined ENTT_PRETTY_FUNCTION_CONSTEXPR
  1638. [[nodiscard]] static constexpr id_type id() ENTT_NOEXCEPT {
  1639. constexpr auto value = hashed_string::value(ENTT_PRETTY_FUNCTION);
  1640. return value;
  1641. }
  1642. #elif defined ENTT_PRETTY_FUNCTION
  1643. [[nodiscard]] static id_type id() ENTT_NOEXCEPT {
  1644. static const auto value = hashed_string::value(ENTT_PRETTY_FUNCTION);
  1645. return value;
  1646. }
  1647. #else
  1648. [[nodiscard]] static id_type id() ENTT_NOEXCEPT {
  1649. return type_index<Type>::value();
  1650. }
  1651. #endif
  1652. /**
  1653. * @brief Returns the name of a given type.
  1654. * @return The name of the given type.
  1655. */
  1656. #if defined ENTT_PRETTY_FUNCTION_CONSTEXPR
  1657. [[nodiscard]] static constexpr std::string_view name() ENTT_NOEXCEPT {
  1658. constexpr auto value = internal::type_name<Type>();
  1659. return value;
  1660. }
  1661. #elif defined ENTT_PRETTY_FUNCTION
  1662. [[nodiscard]] static std::string_view name() ENTT_NOEXCEPT {
  1663. static const auto value = internal::type_name<Type>();
  1664. return value;
  1665. }
  1666. #else
  1667. [[nodiscard]] static constexpr std::string_view name() ENTT_NOEXCEPT {
  1668. return internal::type_name<Type>();
  1669. }
  1670. #endif
  1671. };
  1672. }
  1673. #endif
  1674. // #include "../core/type_traits.hpp"
  1675. #ifndef ENTT_CORE_TYPE_TRAITS_HPP
  1676. #define ENTT_CORE_TYPE_TRAITS_HPP
  1677. #include <cstddef>
  1678. #include <utility>
  1679. #include <type_traits>
  1680. // #include "../config/config.h"
  1681. // #include "fwd.hpp"
  1682. namespace entt {
  1683. /**
  1684. * @brief Using declaration to be used to _repeat_ the same type a number of
  1685. * times equal to the size of a given parameter pack.
  1686. * @tparam Type A type to repeat.
  1687. */
  1688. template<typename Type, typename>
  1689. using unpack_as_t = Type;
  1690. /**
  1691. * @brief Helper variable template to be used to _repeat_ the same value a
  1692. * number of times equal to the size of a given parameter pack.
  1693. * @tparam Value A value to repeat.
  1694. */
  1695. template<auto Value, typename>
  1696. inline constexpr auto unpack_as_v = Value;
  1697. /**
  1698. * @brief Wraps a static constant.
  1699. * @tparam Value A static constant.
  1700. */
  1701. template<auto Value>
  1702. using integral_constant = std::integral_constant<decltype(Value), Value>;
  1703. /**
  1704. * @brief Alias template to ease the creation of named values.
  1705. * @tparam Value A constant value at least convertible to `id_type`.
  1706. */
  1707. template<id_type Value>
  1708. using tag = integral_constant<Value>;
  1709. /**
  1710. * @brief Utility class to disambiguate overloaded functions.
  1711. * @tparam N Number of choices available.
  1712. */
  1713. template<std::size_t N>
  1714. struct choice_t
  1715. // Unfortunately, doxygen cannot parse such a construct.
  1716. /*! @cond TURN_OFF_DOXYGEN */
  1717. : choice_t<N-1>
  1718. /*! @endcond */
  1719. {};
  1720. /*! @copybrief choice_t */
  1721. template<>
  1722. struct choice_t<0> {};
  1723. /**
  1724. * @brief Variable template for the choice trick.
  1725. * @tparam N Number of choices available.
  1726. */
  1727. template<std::size_t N>
  1728. inline constexpr choice_t<N> choice{};
  1729. /*! @brief A class to use to push around lists of types, nothing more. */
  1730. template<typename...>
  1731. struct type_list {};
  1732. /*! @brief Primary template isn't defined on purpose. */
  1733. template<typename>
  1734. struct type_list_size;
  1735. /**
  1736. * @brief Compile-time number of elements in a type list.
  1737. * @tparam Type Types provided by the type list.
  1738. */
  1739. template<typename... Type>
  1740. struct type_list_size<type_list<Type...>>
  1741. : std::integral_constant<std::size_t, sizeof...(Type)>
  1742. {};
  1743. /**
  1744. * @brief Helper variable template.
  1745. * @tparam List Type list.
  1746. */
  1747. template<class List>
  1748. inline constexpr auto type_list_size_v = type_list_size<List>::value;
  1749. /*! @brief Primary template isn't defined on purpose. */
  1750. template<typename...>
  1751. struct type_list_cat;
  1752. /*! @brief Concatenates multiple type lists. */
  1753. template<>
  1754. struct type_list_cat<> {
  1755. /*! @brief A type list composed by the types of all the type lists. */
  1756. using type = type_list<>;
  1757. };
  1758. /**
  1759. * @brief Concatenates multiple type lists.
  1760. * @tparam Type Types provided by the first type list.
  1761. * @tparam Other Types provided by the second type list.
  1762. * @tparam List Other type lists, if any.
  1763. */
  1764. template<typename... Type, typename... Other, typename... List>
  1765. struct type_list_cat<type_list<Type...>, type_list<Other...>, List...> {
  1766. /*! @brief A type list composed by the types of all the type lists. */
  1767. using type = typename type_list_cat<type_list<Type..., Other...>, List...>::type;
  1768. };
  1769. /**
  1770. * @brief Concatenates multiple type lists.
  1771. * @tparam Type Types provided by the type list.
  1772. */
  1773. template<typename... Type>
  1774. struct type_list_cat<type_list<Type...>> {
  1775. /*! @brief A type list composed by the types of all the type lists. */
  1776. using type = type_list<Type...>;
  1777. };
  1778. /**
  1779. * @brief Helper type.
  1780. * @tparam List Type lists to concatenate.
  1781. */
  1782. template<typename... List>
  1783. using type_list_cat_t = typename type_list_cat<List...>::type;
  1784. /*! @brief Primary template isn't defined on purpose. */
  1785. template<typename>
  1786. struct type_list_unique;
  1787. /**
  1788. * @brief Removes duplicates types from a type list.
  1789. * @tparam Type One of the types provided by the given type list.
  1790. * @tparam Other The other types provided by the given type list.
  1791. */
  1792. template<typename Type, typename... Other>
  1793. struct type_list_unique<type_list<Type, Other...>> {
  1794. /*! @brief A type list without duplicate types. */
  1795. using type = std::conditional_t<
  1796. std::disjunction_v<std::is_same<Type, Other>...>,
  1797. typename type_list_unique<type_list<Other...>>::type,
  1798. type_list_cat_t<type_list<Type>, typename type_list_unique<type_list<Other...>>::type>
  1799. >;
  1800. };
  1801. /*! @brief Removes duplicates types from a type list. */
  1802. template<>
  1803. struct type_list_unique<type_list<>> {
  1804. /*! @brief A type list without duplicate types. */
  1805. using type = type_list<>;
  1806. };
  1807. /**
  1808. * @brief Helper type.
  1809. * @tparam Type A type list.
  1810. */
  1811. template<typename Type>
  1812. using type_list_unique_t = typename type_list_unique<Type>::type;
  1813. /**
  1814. * @brief Provides the member constant `value` to true if a given type is
  1815. * equality comparable, false otherwise.
  1816. * @tparam Type Potentially equality comparable type.
  1817. */
  1818. template<typename Type, typename = std::void_t<>>
  1819. struct is_equality_comparable: std::false_type {};
  1820. /*! @copydoc is_equality_comparable */
  1821. template<typename Type>
  1822. struct is_equality_comparable<Type, std::void_t<decltype(std::declval<Type>() == std::declval<Type>())>>
  1823. : std::true_type
  1824. {};
  1825. /**
  1826. * @brief Helper variable template.
  1827. * @tparam Type Potentially equality comparable type.
  1828. */
  1829. template<class Type>
  1830. inline constexpr auto is_equality_comparable_v = is_equality_comparable<Type>::value;
  1831. /**
  1832. * @brief Provides the member constant `value` to true if a given type is empty
  1833. * and the empty type optimization is enabled, false otherwise.
  1834. * @tparam Type Potential empty type.
  1835. */
  1836. template<typename Type, typename = void>
  1837. struct is_eto_eligible
  1838. : ENTT_IS_EMPTY(Type)
  1839. {};
  1840. /**
  1841. * @brief Helper variable template.
  1842. * @tparam Type Potential empty type.
  1843. */
  1844. template<typename Type>
  1845. inline constexpr auto is_eto_eligible_v = is_eto_eligible<Type>::value;
  1846. /**
  1847. * @brief Extracts the class of a non-static member object or function.
  1848. * @tparam Member A pointer to a non-static member object or function.
  1849. */
  1850. template<typename Member>
  1851. class member_class {
  1852. static_assert(std::is_member_pointer_v<Member>, "Invalid pointer type to non-static member object or function");
  1853. template<typename Class, typename Ret, typename... Args>
  1854. static Class * clazz(Ret(Class:: *)(Args...));
  1855. template<typename Class, typename Ret, typename... Args>
  1856. static Class * clazz(Ret(Class:: *)(Args...) const);
  1857. template<typename Class, typename Type>
  1858. static Class * clazz(Type Class:: *);
  1859. public:
  1860. /*! @brief The class of the given non-static member object or function. */
  1861. using type = std::remove_pointer_t<decltype(clazz(std::declval<Member>()))>;
  1862. };
  1863. /**
  1864. * @brief Helper type.
  1865. * @tparam Member A pointer to a non-static member object or function.
  1866. */
  1867. template<typename Member>
  1868. using member_class_t = typename member_class<Member>::type;
  1869. }
  1870. #endif
  1871. // #include "../signal/sigh.hpp"
  1872. #ifndef ENTT_SIGNAL_SIGH_HPP
  1873. #define ENTT_SIGNAL_SIGH_HPP
  1874. #include <vector>
  1875. #include <utility>
  1876. #include <iterator>
  1877. #include <algorithm>
  1878. #include <functional>
  1879. #include <type_traits>
  1880. // #include "../config/config.h"
  1881. #ifndef ENTT_CONFIG_CONFIG_H
  1882. #define ENTT_CONFIG_CONFIG_H
  1883. #ifndef ENTT_NOEXCEPT
  1884. # define ENTT_NOEXCEPT noexcept
  1885. #endif
  1886. #ifndef ENTT_HS_SUFFIX
  1887. # define ENTT_HS_SUFFIX _hs
  1888. #endif
  1889. #ifndef ENTT_HWS_SUFFIX
  1890. # define ENTT_HWS_SUFFIX _hws
  1891. #endif
  1892. #ifndef ENTT_USE_ATOMIC
  1893. # define ENTT_MAYBE_ATOMIC(Type) Type
  1894. #else
  1895. # include <atomic>
  1896. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  1897. #endif
  1898. #ifndef ENTT_ID_TYPE
  1899. # include <cstdint>
  1900. # define ENTT_ID_TYPE std::uint32_t
  1901. #endif
  1902. #ifndef ENTT_PAGE_SIZE
  1903. # define ENTT_PAGE_SIZE 32768
  1904. #endif
  1905. #ifndef ENTT_ASSERT
  1906. # include <cassert>
  1907. # define ENTT_ASSERT(condition) assert(condition)
  1908. #endif
  1909. #ifndef ENTT_NO_ETO
  1910. # include <type_traits>
  1911. # define ENTT_IS_EMPTY(Type) std::is_empty<Type>
  1912. #else
  1913. # include <type_traits>
  1914. # define ENTT_IS_EMPTY(Type) std::false_type
  1915. #endif
  1916. #ifndef ENTT_STANDARD_CPP
  1917. # if defined __clang__ || (defined __GNUC__ && __GNUC__ > 8)
  1918. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  1919. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  1920. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  1921. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  1922. # elif defined __GNUC__
  1923. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  1924. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  1925. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  1926. # elif defined _MSC_VER
  1927. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  1928. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  1929. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  1930. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  1931. # endif
  1932. #endif
  1933. #ifndef ENTT_STANDALONE
  1934. # define ENTT_FAST_PATH(...) false
  1935. #else
  1936. # define ENTT_FAST_PATH(Cond) Cond
  1937. #endif
  1938. #endif
  1939. // #include "delegate.hpp"
  1940. #ifndef ENTT_SIGNAL_DELEGATE_HPP
  1941. #define ENTT_SIGNAL_DELEGATE_HPP
  1942. #include <tuple>
  1943. #include <cstddef>
  1944. #include <utility>
  1945. #include <functional>
  1946. #include <type_traits>
  1947. // #include "../config/config.h"
  1948. namespace entt {
  1949. /**
  1950. * @cond TURN_OFF_DOXYGEN
  1951. * Internal details not to be documented.
  1952. */
  1953. namespace internal {
  1954. template<typename Ret, typename... Args>
  1955. auto function_pointer(Ret(*)(Args...)) -> Ret(*)(Args...);
  1956. template<typename Ret, typename Type, typename... Args, typename Other>
  1957. auto function_pointer(Ret(*)(Type, Args...), Other &&) -> Ret(*)(Args...);
  1958. template<typename Class, typename Ret, typename... Args, typename... Other>
  1959. auto function_pointer(Ret(Class:: *)(Args...), Other &&...) -> Ret(*)(Args...);
  1960. template<typename Class, typename Ret, typename... Args, typename... Other>
  1961. auto function_pointer(Ret(Class:: *)(Args...) const, Other &&...) -> Ret(*)(Args...);
  1962. template<typename Class, typename Type, typename... Other>
  1963. auto function_pointer(Type Class:: *, Other &&...) -> Type(*)();
  1964. template<typename... Type>
  1965. using function_pointer_t = decltype(internal::function_pointer(std::declval<Type>()...));
  1966. template<typename... Class, typename Ret, typename... Args>
  1967. [[nodiscard]] constexpr auto index_sequence_for(Ret(*)(Args...)) {
  1968. return std::index_sequence_for<Class..., Args...>{};
  1969. }
  1970. }
  1971. /**
  1972. * Internal details not to be documented.
  1973. * @endcond
  1974. */
  1975. /*! @brief Used to wrap a function or a member of a specified type. */
  1976. template<auto>
  1977. struct connect_arg_t {};
  1978. /*! @brief Constant of type connect_arg_t used to disambiguate calls. */
  1979. template<auto Func>
  1980. inline constexpr connect_arg_t<Func> connect_arg{};
  1981. /**
  1982. * @brief Basic delegate implementation.
  1983. *
  1984. * Primary template isn't defined on purpose. All the specializations give a
  1985. * compile-time error unless the template parameter is a function type.
  1986. */
  1987. template<typename>
  1988. class delegate;
  1989. /**
  1990. * @brief Utility class to use to send around functions and members.
  1991. *
  1992. * Unmanaged delegate for function pointers and members. Users of this class are
  1993. * in charge of disconnecting instances before deleting them.
  1994. *
  1995. * A delegate can be used as a general purpose invoker without memory overhead
  1996. * for free functions possibly with payloads and bound or unbound members.
  1997. *
  1998. * @tparam Ret Return type of a function type.
  1999. * @tparam Args Types of arguments of a function type.
  2000. */
  2001. template<typename Ret, typename... Args>
  2002. class delegate<Ret(Args...)> {
  2003. template<auto Candidate, std::size_t... Index>
  2004. [[nodiscard]] auto wrap(std::index_sequence<Index...>) ENTT_NOEXCEPT {
  2005. return [](const void *, Args... args) -> Ret {
  2006. [[maybe_unused]] const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
  2007. return Ret(std::invoke(Candidate, std::forward<std::tuple_element_t<Index, std::tuple<Args...>>>(std::get<Index>(arguments))...));
  2008. };
  2009. }
  2010. template<auto Candidate, typename Type, std::size_t... Index>
  2011. [[nodiscard]] auto wrap(Type &, std::index_sequence<Index...>) ENTT_NOEXCEPT {
  2012. return [](const void *payload, Args... args) -> Ret {
  2013. [[maybe_unused]] const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
  2014. Type *curr = static_cast<Type *>(const_cast<std::conditional_t<std::is_const_v<Type>, const void *, void *>>(payload));
  2015. return Ret(std::invoke(Candidate, *curr, std::forward<std::tuple_element_t<Index, std::tuple<Args...>>>(std::get<Index>(arguments))...));
  2016. };
  2017. }
  2018. template<auto Candidate, typename Type, std::size_t... Index>
  2019. [[nodiscard]] auto wrap(Type *, std::index_sequence<Index...>) ENTT_NOEXCEPT {
  2020. return [](const void *payload, Args... args) -> Ret {
  2021. [[maybe_unused]] const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
  2022. Type *curr = static_cast<Type *>(const_cast<std::conditional_t<std::is_const_v<Type>, const void *, void *>>(payload));
  2023. return Ret(std::invoke(Candidate, curr, std::forward<std::tuple_element_t<Index, std::tuple<Args...>>>(std::get<Index>(arguments))...));
  2024. };
  2025. }
  2026. public:
  2027. /*! @brief Function type of the contained target. */
  2028. using function_type = Ret(const void *, Args...);
  2029. /*! @brief Function type of the delegate. */
  2030. using type = Ret(Args...);
  2031. /*! @brief Return type of the delegate. */
  2032. using result_type = Ret;
  2033. /*! @brief Default constructor. */
  2034. delegate() ENTT_NOEXCEPT
  2035. : fn{nullptr}, data{nullptr}
  2036. {}
  2037. /**
  2038. * @brief Constructs a delegate and connects a free function or an unbound
  2039. * member.
  2040. * @tparam Candidate Function or member to connect to the delegate.
  2041. */
  2042. template<auto Candidate>
  2043. delegate(connect_arg_t<Candidate>) ENTT_NOEXCEPT {
  2044. connect<Candidate>();
  2045. }
  2046. /**
  2047. * @brief Constructs a delegate and connects a free function with payload or
  2048. * a bound member.
  2049. * @tparam Candidate Function or member to connect to the delegate.
  2050. * @tparam Type Type of class or type of payload.
  2051. * @param value_or_instance A valid object that fits the purpose.
  2052. */
  2053. template<auto Candidate, typename Type>
  2054. delegate(connect_arg_t<Candidate>, Type &&value_or_instance) ENTT_NOEXCEPT {
  2055. connect<Candidate>(std::forward<Type>(value_or_instance));
  2056. }
  2057. /**
  2058. * @brief Constructs a delegate and connects an user defined function with
  2059. * optional payload.
  2060. * @param function Function to connect to the delegate.
  2061. * @param payload User defined arbitrary data.
  2062. */
  2063. delegate(function_type *function, const void *payload = nullptr) ENTT_NOEXCEPT {
  2064. connect(function, payload);
  2065. }
  2066. /**
  2067. * @brief Connects a free function or an unbound member to a delegate.
  2068. * @tparam Candidate Function or member to connect to the delegate.
  2069. */
  2070. template<auto Candidate>
  2071. void connect() ENTT_NOEXCEPT {
  2072. data = nullptr;
  2073. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Args...>) {
  2074. fn = [](const void *, Args... args) -> Ret {
  2075. return Ret(std::invoke(Candidate, std::forward<Args>(args)...));
  2076. };
  2077. } else if constexpr(std::is_member_pointer_v<decltype(Candidate)>) {
  2078. fn = wrap<Candidate>(internal::index_sequence_for<std::tuple_element_t<0, std::tuple<Args...>>>(internal::function_pointer_t<decltype(Candidate)>{}));
  2079. } else {
  2080. fn = wrap<Candidate>(internal::index_sequence_for(internal::function_pointer_t<decltype(Candidate)>{}));
  2081. }
  2082. }
  2083. /**
  2084. * @brief Connects a free function with payload or a bound member to a
  2085. * delegate.
  2086. *
  2087. * The delegate isn't responsible for the connected object or the payload.
  2088. * Users must always guarantee that the lifetime of the instance overcomes
  2089. * the one of the delegate.<br/>
  2090. * When used to connect a free function with payload, its signature must be
  2091. * such that the instance is the first argument before the ones used to
  2092. * define the delegate itself.
  2093. *
  2094. * @tparam Candidate Function or member to connect to the delegate.
  2095. * @tparam Type Type of class or type of payload.
  2096. * @param value_or_instance A valid reference that fits the purpose.
  2097. */
  2098. template<auto Candidate, typename Type>
  2099. void connect(Type &value_or_instance) ENTT_NOEXCEPT {
  2100. data = &value_or_instance;
  2101. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Type &, Args...>) {
  2102. fn = [](const void *payload, Args... args) -> Ret {
  2103. Type *curr = static_cast<Type *>(const_cast<std::conditional_t<std::is_const_v<Type>, const void *, void *>>(payload));
  2104. return Ret(std::invoke(Candidate, *curr, std::forward<Args>(args)...));
  2105. };
  2106. } else {
  2107. fn = wrap<Candidate>(value_or_instance, internal::index_sequence_for(internal::function_pointer_t<decltype(Candidate), Type>{}));
  2108. }
  2109. }
  2110. /**
  2111. * @brief Connects a free function with payload or a bound member to a
  2112. * delegate.
  2113. *
  2114. * @sa connect(Type &)
  2115. *
  2116. * @tparam Candidate Function or member to connect to the delegate.
  2117. * @tparam Type Type of class or type of payload.
  2118. * @param value_or_instance A valid pointer that fits the purpose.
  2119. */
  2120. template<auto Candidate, typename Type>
  2121. void connect(Type *value_or_instance) ENTT_NOEXCEPT {
  2122. data = value_or_instance;
  2123. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Type *, Args...>) {
  2124. fn = [](const void *payload, Args... args) -> Ret {
  2125. Type *curr = static_cast<Type *>(const_cast<std::conditional_t<std::is_const_v<Type>, const void *, void *>>(payload));
  2126. return Ret(std::invoke(Candidate, curr, std::forward<Args>(args)...));
  2127. };
  2128. } else {
  2129. fn = wrap<Candidate>(value_or_instance, internal::index_sequence_for(internal::function_pointer_t<decltype(Candidate), Type>{}));
  2130. }
  2131. }
  2132. /**
  2133. * @brief Connects an user defined function with optional payload to a
  2134. * delegate.
  2135. *
  2136. * The delegate isn't responsible for the connected object or the payload.
  2137. * Users must always guarantee that the lifetime of an instance overcomes
  2138. * the one of the delegate.<br/>
  2139. * The payload is returned as the first argument to the target function in
  2140. * all cases.
  2141. *
  2142. * @param function Function to connect to the delegate.
  2143. * @param payload User defined arbitrary data.
  2144. */
  2145. void connect(function_type *function, const void *payload = nullptr) ENTT_NOEXCEPT {
  2146. fn = function;
  2147. data = payload;
  2148. }
  2149. /**
  2150. * @brief Resets a delegate.
  2151. *
  2152. * After a reset, a delegate cannot be invoked anymore.
  2153. */
  2154. void reset() ENTT_NOEXCEPT {
  2155. fn = nullptr;
  2156. data = nullptr;
  2157. }
  2158. /**
  2159. * @brief Returns the instance or the payload linked to a delegate, if any.
  2160. * @return An opaque pointer to the underlying data.
  2161. */
  2162. [[nodiscard]] const void * instance() const ENTT_NOEXCEPT {
  2163. return data;
  2164. }
  2165. /**
  2166. * @brief Triggers a delegate.
  2167. *
  2168. * The delegate invokes the underlying function and returns the result.
  2169. *
  2170. * @warning
  2171. * Attempting to trigger an invalid delegate results in undefined
  2172. * behavior.<br/>
  2173. * An assertion will abort the execution at runtime in debug mode if the
  2174. * delegate has not yet been set.
  2175. *
  2176. * @param args Arguments to use to invoke the underlying function.
  2177. * @return The value returned by the underlying function.
  2178. */
  2179. Ret operator()(Args... args) const {
  2180. ENTT_ASSERT(fn);
  2181. return fn(data, std::forward<Args>(args)...);
  2182. }
  2183. /**
  2184. * @brief Checks whether a delegate actually stores a listener.
  2185. * @return False if the delegate is empty, true otherwise.
  2186. */
  2187. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  2188. // no need to test also data
  2189. return !(fn == nullptr);
  2190. }
  2191. /**
  2192. * @brief Compares the contents of two delegates.
  2193. * @param other Delegate with which to compare.
  2194. * @return False if the two contents differ, true otherwise.
  2195. */
  2196. [[nodiscard]] bool operator==(const delegate<Ret(Args...)> &other) const ENTT_NOEXCEPT {
  2197. return fn == other.fn && data == other.data;
  2198. }
  2199. private:
  2200. function_type *fn;
  2201. const void *data;
  2202. };
  2203. /**
  2204. * @brief Compares the contents of two delegates.
  2205. * @tparam Ret Return type of a function type.
  2206. * @tparam Args Types of arguments of a function type.
  2207. * @param lhs A valid delegate object.
  2208. * @param rhs A valid delegate object.
  2209. * @return True if the two contents differ, false otherwise.
  2210. */
  2211. template<typename Ret, typename... Args>
  2212. [[nodiscard]] bool operator!=(const delegate<Ret(Args...)> &lhs, const delegate<Ret(Args...)> &rhs) ENTT_NOEXCEPT {
  2213. return !(lhs == rhs);
  2214. }
  2215. /**
  2216. * @brief Deduction guide.
  2217. * @tparam Candidate Function or member to connect to the delegate.
  2218. */
  2219. template<auto Candidate>
  2220. delegate(connect_arg_t<Candidate>) ENTT_NOEXCEPT
  2221. -> delegate<std::remove_pointer_t<internal::function_pointer_t<decltype(Candidate)>>>;
  2222. /**
  2223. * @brief Deduction guide.
  2224. * @tparam Candidate Function or member to connect to the delegate.
  2225. * @tparam Type Type of class or type of payload.
  2226. */
  2227. template<auto Candidate, typename Type>
  2228. delegate(connect_arg_t<Candidate>, Type &&) ENTT_NOEXCEPT
  2229. -> delegate<std::remove_pointer_t<internal::function_pointer_t<decltype(Candidate), Type>>>;
  2230. /*! @brief Deduction guide. */
  2231. template<typename Ret, typename... Args>
  2232. delegate(Ret(*)(const void *, Args...), const void * = nullptr) ENTT_NOEXCEPT
  2233. -> delegate<Ret(Args...)>;
  2234. }
  2235. #endif
  2236. // #include "fwd.hpp"
  2237. #ifndef ENTT_SIGNAL_FWD_HPP
  2238. #define ENTT_SIGNAL_FWD_HPP
  2239. namespace entt {
  2240. template<typename>
  2241. class delegate;
  2242. class dispatcher;
  2243. template<typename>
  2244. class emitter;
  2245. class connection;
  2246. struct scoped_connection;
  2247. template<typename>
  2248. class sink;
  2249. template<typename>
  2250. class sigh;
  2251. }
  2252. #endif
  2253. namespace entt {
  2254. /**
  2255. * @brief Sink class.
  2256. *
  2257. * Primary template isn't defined on purpose. All the specializations give a
  2258. * compile-time error unless the template parameter is a function type.
  2259. *
  2260. * @tparam Function A valid function type.
  2261. */
  2262. template<typename Function>
  2263. class sink;
  2264. /**
  2265. * @brief Unmanaged signal handler.
  2266. *
  2267. * Primary template isn't defined on purpose. All the specializations give a
  2268. * compile-time error unless the template parameter is a function type.
  2269. *
  2270. * @tparam Function A valid function type.
  2271. */
  2272. template<typename Function>
  2273. class sigh;
  2274. /**
  2275. * @brief Unmanaged signal handler.
  2276. *
  2277. * It works directly with references to classes and pointers to member functions
  2278. * as well as pointers to free functions. Users of this class are in charge of
  2279. * disconnecting instances before deleting them.
  2280. *
  2281. * This class serves mainly two purposes:
  2282. *
  2283. * * Creating signals to use later to notify a bunch of listeners.
  2284. * * Collecting results from a set of functions like in a voting system.
  2285. *
  2286. * @tparam Ret Return type of a function type.
  2287. * @tparam Args Types of arguments of a function type.
  2288. */
  2289. template<typename Ret, typename... Args>
  2290. class sigh<Ret(Args...)> {
  2291. /*! @brief A sink is allowed to modify a signal. */
  2292. friend class sink<Ret(Args...)>;
  2293. public:
  2294. /*! @brief Unsigned integer type. */
  2295. using size_type = std::size_t;
  2296. /*! @brief Sink type. */
  2297. using sink_type = sink<Ret(Args...)>;
  2298. /**
  2299. * @brief Instance type when it comes to connecting member functions.
  2300. * @tparam Class Type of class to which the member function belongs.
  2301. */
  2302. template<typename Class>
  2303. using instance_type = Class *;
  2304. /**
  2305. * @brief Number of listeners connected to the signal.
  2306. * @return Number of listeners currently connected.
  2307. */
  2308. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  2309. return calls.size();
  2310. }
  2311. /**
  2312. * @brief Returns false if at least a listener is connected to the signal.
  2313. * @return True if the signal has no listeners connected, false otherwise.
  2314. */
  2315. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  2316. return calls.empty();
  2317. }
  2318. /**
  2319. * @brief Triggers a signal.
  2320. *
  2321. * All the listeners are notified. Order isn't guaranteed.
  2322. *
  2323. * @param args Arguments to use to invoke listeners.
  2324. */
  2325. void publish(Args... args) const {
  2326. for(auto &&call: std::as_const(calls)) {
  2327. call(args...);
  2328. }
  2329. }
  2330. /**
  2331. * @brief Collects return values from the listeners.
  2332. *
  2333. * The collector must expose a call operator with the following properties:
  2334. *
  2335. * * The return type is either `void` or such that it's convertible to
  2336. * `bool`. In the second case, a true value will stop the iteration.
  2337. * * The list of parameters is empty if `Ret` is `void`, otherwise it
  2338. * contains a single element such that `Ret` is convertible to it.
  2339. *
  2340. * @tparam Func Type of collector to use, if any.
  2341. * @param func A valid function object.
  2342. * @param args Arguments to use to invoke listeners.
  2343. */
  2344. template<typename Func>
  2345. void collect(Func func, Args... args) const {
  2346. for(auto &&call: calls) {
  2347. if constexpr(std::is_void_v<Ret>) {
  2348. if constexpr(std::is_invocable_r_v<bool, Func>) {
  2349. call(args...);
  2350. if(func()) { break; }
  2351. } else {
  2352. call(args...);
  2353. func();
  2354. }
  2355. } else {
  2356. if constexpr(std::is_invocable_r_v<bool, Func, Ret>) {
  2357. if(func(call(args...))) { break; }
  2358. } else {
  2359. func(call(args...));
  2360. }
  2361. }
  2362. }
  2363. }
  2364. private:
  2365. std::vector<delegate<Ret(Args...)>> calls;
  2366. };
  2367. /**
  2368. * @brief Connection class.
  2369. *
  2370. * Opaque object the aim of which is to allow users to release an already
  2371. * estabilished connection without having to keep a reference to the signal or
  2372. * the sink that generated it.
  2373. */
  2374. class connection {
  2375. /*! @brief A sink is allowed to create connection objects. */
  2376. template<typename>
  2377. friend class sink;
  2378. connection(delegate<void(void *)> fn, void *ref)
  2379. : disconnect{fn}, signal{ref}
  2380. {}
  2381. public:
  2382. /*! @brief Default constructor. */
  2383. connection() = default;
  2384. /**
  2385. * @brief Checks whether a connection is properly initialized.
  2386. * @return True if the connection is properly initialized, false otherwise.
  2387. */
  2388. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  2389. return static_cast<bool>(disconnect);
  2390. }
  2391. /*! @brief Breaks the connection. */
  2392. void release() {
  2393. if(disconnect) {
  2394. disconnect(signal);
  2395. disconnect.reset();
  2396. }
  2397. }
  2398. private:
  2399. delegate<void(void *)> disconnect;
  2400. void *signal{};
  2401. };
  2402. /**
  2403. * @brief Scoped connection class.
  2404. *
  2405. * Opaque object the aim of which is to allow users to release an already
  2406. * estabilished connection without having to keep a reference to the signal or
  2407. * the sink that generated it.<br/>
  2408. * A scoped connection automatically breaks the link between the two objects
  2409. * when it goes out of scope.
  2410. */
  2411. struct scoped_connection {
  2412. /*! @brief Default constructor. */
  2413. scoped_connection() = default;
  2414. /**
  2415. * @brief Constructs a scoped connection from a basic connection.
  2416. * @param other A valid connection object.
  2417. */
  2418. scoped_connection(const connection &other)
  2419. : conn{other}
  2420. {}
  2421. /*! @brief Default copy constructor, deleted on purpose. */
  2422. scoped_connection(const scoped_connection &) = delete;
  2423. /*! @brief Automatically breaks the link on destruction. */
  2424. ~scoped_connection() {
  2425. conn.release();
  2426. }
  2427. /**
  2428. * @brief Default copy assignment operator, deleted on purpose.
  2429. * @return This scoped connection.
  2430. */
  2431. scoped_connection & operator=(const scoped_connection &) = delete;
  2432. /**
  2433. * @brief Acquires a connection.
  2434. * @param other The connection object to acquire.
  2435. * @return This scoped connection.
  2436. */
  2437. scoped_connection & operator=(connection other) {
  2438. conn = std::move(other);
  2439. return *this;
  2440. }
  2441. /**
  2442. * @brief Checks whether a scoped connection is properly initialized.
  2443. * @return True if the connection is properly initialized, false otherwise.
  2444. */
  2445. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  2446. return static_cast<bool>(conn);
  2447. }
  2448. /*! @brief Breaks the connection. */
  2449. void release() {
  2450. conn.release();
  2451. }
  2452. private:
  2453. connection conn;
  2454. };
  2455. /**
  2456. * @brief Sink class.
  2457. *
  2458. * A sink is used to connect listeners to signals and to disconnect them.<br/>
  2459. * The function type for a listener is the one of the signal to which it
  2460. * belongs.
  2461. *
  2462. * The clear separation between a signal and a sink permits to store the former
  2463. * as private data member without exposing the publish functionality to the
  2464. * users of the class.
  2465. *
  2466. * @warning
  2467. * Lifetime of a sink must not overcome that of the signal to which it refers.
  2468. * In any other case, attempting to use a sink results in undefined behavior.
  2469. *
  2470. * @tparam Ret Return type of a function type.
  2471. * @tparam Args Types of arguments of a function type.
  2472. */
  2473. template<typename Ret, typename... Args>
  2474. class sink<Ret(Args...)> {
  2475. using signal_type = sigh<Ret(Args...)>;
  2476. using difference_type = typename std::iterator_traits<typename decltype(signal_type::calls)::iterator>::difference_type;
  2477. template<auto Candidate, typename Type>
  2478. static void release(Type value_or_instance, void *signal) {
  2479. sink{*static_cast<signal_type *>(signal)}.disconnect<Candidate>(value_or_instance);
  2480. }
  2481. template<auto Candidate>
  2482. static void release(void *signal) {
  2483. sink{*static_cast<signal_type *>(signal)}.disconnect<Candidate>();
  2484. }
  2485. public:
  2486. /**
  2487. * @brief Constructs a sink that is allowed to modify a given signal.
  2488. * @param ref A valid reference to a signal object.
  2489. */
  2490. sink(sigh<Ret(Args...)> &ref) ENTT_NOEXCEPT
  2491. : offset{},
  2492. signal{&ref}
  2493. {}
  2494. /**
  2495. * @brief Returns false if at least a listener is connected to the sink.
  2496. * @return True if the sink has no listeners connected, false otherwise.
  2497. */
  2498. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  2499. return signal->calls.empty();
  2500. }
  2501. /**
  2502. * @brief Returns a sink that connects before a given free function or an
  2503. * unbound member.
  2504. * @tparam Function A valid free function pointer.
  2505. * @return A properly initialized sink object.
  2506. */
  2507. template<auto Function>
  2508. [[nodiscard]] sink before() {
  2509. delegate<Ret(Args...)> call{};
  2510. call.template connect<Function>();
  2511. const auto &calls = signal->calls;
  2512. const auto it = std::find(calls.cbegin(), calls.cend(), std::move(call));
  2513. sink other{*this};
  2514. other.offset = std::distance(it, calls.cend());
  2515. return other;
  2516. }
  2517. /**
  2518. * @brief Returns a sink that connects before a free function with payload
  2519. * or a bound member.
  2520. * @tparam Candidate Member or free function to look for.
  2521. * @tparam Type Type of class or type of payload.
  2522. * @param value_or_instance A valid object that fits the purpose.
  2523. * @return A properly initialized sink object.
  2524. */
  2525. template<auto Candidate, typename Type>
  2526. [[nodiscard]] sink before(Type &&value_or_instance) {
  2527. delegate<Ret(Args...)> call{};
  2528. call.template connect<Candidate>(value_or_instance);
  2529. const auto &calls = signal->calls;
  2530. const auto it = std::find(calls.cbegin(), calls.cend(), std::move(call));
  2531. sink other{*this};
  2532. other.offset = std::distance(it, calls.cend());
  2533. return other;
  2534. }
  2535. /**
  2536. * @brief Returns a sink that connects before a given instance or specific
  2537. * payload.
  2538. * @tparam Type Type of class or type of payload.
  2539. * @param value_or_instance A valid object that fits the purpose.
  2540. * @return A properly initialized sink object.
  2541. */
  2542. template<typename Type>
  2543. [[nodiscard]] sink before(Type &value_or_instance) {
  2544. return before(&value_or_instance);
  2545. }
  2546. /**
  2547. * @brief Returns a sink that connects before a given instance or specific
  2548. * payload.
  2549. * @tparam Type Type of class or type of payload.
  2550. * @param value_or_instance A valid pointer that fits the purpose.
  2551. * @return A properly initialized sink object.
  2552. */
  2553. template<typename Type>
  2554. [[nodiscard]] sink before(Type *value_or_instance) {
  2555. sink other{*this};
  2556. if(value_or_instance) {
  2557. const auto &calls = signal->calls;
  2558. const auto it = std::find_if(calls.cbegin(), calls.cend(), [value_or_instance](const auto &delegate) {
  2559. return delegate.instance() == value_or_instance;
  2560. });
  2561. other.offset = std::distance(it, calls.cend());
  2562. }
  2563. return other;
  2564. }
  2565. /**
  2566. * @brief Returns a sink that connects before anything else.
  2567. * @return A properly initialized sink object.
  2568. */
  2569. [[nodiscard]] sink before() {
  2570. sink other{*this};
  2571. other.offset = signal->calls.size();
  2572. return other;
  2573. }
  2574. /**
  2575. * @brief Connects a free function or an unbound member to a signal.
  2576. *
  2577. * The signal handler performs checks to avoid multiple connections for the
  2578. * same function.
  2579. *
  2580. * @tparam Candidate Function or member to connect to the signal.
  2581. * @return A properly initialized connection object.
  2582. */
  2583. template<auto Candidate>
  2584. connection connect() {
  2585. disconnect<Candidate>();
  2586. delegate<Ret(Args...)> call{};
  2587. call.template connect<Candidate>();
  2588. signal->calls.insert(signal->calls.end() - offset, std::move(call));
  2589. delegate<void(void *)> conn{};
  2590. conn.template connect<&release<Candidate>>();
  2591. return { std::move(conn), signal };
  2592. }
  2593. /**
  2594. * @brief Connects a free function with payload or a bound member to a
  2595. * signal.
  2596. *
  2597. * The signal isn't responsible for the connected object or the payload.
  2598. * Users must always guarantee that the lifetime of the instance overcomes
  2599. * the one of the signal. On the other side, the signal handler performs
  2600. * checks to avoid multiple connections for the same function.<br/>
  2601. * When used to connect a free function with payload, its signature must be
  2602. * such that the instance is the first argument before the ones used to
  2603. * define the signal itself.
  2604. *
  2605. * @tparam Candidate Function or member to connect to the signal.
  2606. * @tparam Type Type of class or type of payload.
  2607. * @param value_or_instance A valid object that fits the purpose.
  2608. * @return A properly initialized connection object.
  2609. */
  2610. template<auto Candidate, typename Type>
  2611. connection connect(Type &&value_or_instance) {
  2612. disconnect<Candidate>(value_or_instance);
  2613. delegate<Ret(Args...)> call{};
  2614. call.template connect<Candidate>(value_or_instance);
  2615. signal->calls.insert(signal->calls.end() - offset, std::move(call));
  2616. delegate<void(void *)> conn{};
  2617. conn.template connect<&release<Candidate, Type>>(value_or_instance);
  2618. return { std::move(conn), signal };
  2619. }
  2620. /**
  2621. * @brief Disconnects a free function or an unbound member from a signal.
  2622. * @tparam Candidate Function or member to disconnect from the signal.
  2623. */
  2624. template<auto Candidate>
  2625. void disconnect() {
  2626. auto &calls = signal->calls;
  2627. delegate<Ret(Args...)> call{};
  2628. call.template connect<Candidate>();
  2629. calls.erase(std::remove(calls.begin(), calls.end(), std::move(call)), calls.end());
  2630. }
  2631. /**
  2632. * @brief Disconnects a free function with payload or a bound member from a
  2633. * signal.
  2634. * @tparam Candidate Function or member to disconnect from the signal.
  2635. * @tparam Type Type of class or type of payload.
  2636. * @param value_or_instance A valid object that fits the purpose.
  2637. */
  2638. template<auto Candidate, typename Type>
  2639. void disconnect(Type &&value_or_instance) {
  2640. auto &calls = signal->calls;
  2641. delegate<Ret(Args...)> call{};
  2642. call.template connect<Candidate>(value_or_instance);
  2643. calls.erase(std::remove(calls.begin(), calls.end(), std::move(call)), calls.end());
  2644. }
  2645. /**
  2646. * @brief Disconnects free functions with payload or bound members from a
  2647. * signal.
  2648. * @tparam Type Type of class or type of payload.
  2649. * @param value_or_instance A valid object that fits the purpose.
  2650. */
  2651. template<typename Type>
  2652. void disconnect(Type &value_or_instance) {
  2653. disconnect(&value_or_instance);
  2654. }
  2655. /**
  2656. * @brief Disconnects free functions with payload or bound members from a
  2657. * signal.
  2658. * @tparam Type Type of class or type of payload.
  2659. * @param value_or_instance A valid object that fits the purpose.
  2660. */
  2661. template<typename Type>
  2662. void disconnect(Type *value_or_instance) {
  2663. if(value_or_instance) {
  2664. auto &calls = signal->calls;
  2665. calls.erase(std::remove_if(calls.begin(), calls.end(), [value_or_instance](const auto &delegate) {
  2666. return delegate.instance() == value_or_instance;
  2667. }), calls.end());
  2668. }
  2669. }
  2670. /*! @brief Disconnects all the listeners from a signal. */
  2671. void disconnect() {
  2672. signal->calls.clear();
  2673. }
  2674. private:
  2675. difference_type offset;
  2676. signal_type *signal;
  2677. };
  2678. /**
  2679. * @brief Deduction guide.
  2680. *
  2681. * It allows to deduce the function type of a sink directly from the signal it
  2682. * refers to.
  2683. *
  2684. * @tparam Ret Return type of a function type.
  2685. * @tparam Args Types of arguments of a function type.
  2686. */
  2687. template<typename Ret, typename... Args>
  2688. sink(sigh<Ret(Args...)> &) ENTT_NOEXCEPT -> sink<Ret(Args...)>;
  2689. }
  2690. #endif
  2691. // #include "entity.hpp"
  2692. #ifndef ENTT_ENTITY_ENTITY_HPP
  2693. #define ENTT_ENTITY_ENTITY_HPP
  2694. #include <cstddef>
  2695. #include <cstdint>
  2696. #include <type_traits>
  2697. // #include "../config/config.h"
  2698. namespace entt {
  2699. /**
  2700. * @brief Entity traits.
  2701. *
  2702. * Primary template isn't defined on purpose. All the specializations give a
  2703. * compile-time error unless the template parameter is an accepted entity type.
  2704. */
  2705. template<typename, typename = void>
  2706. struct entt_traits;
  2707. /**
  2708. * @brief Entity traits for enumeration types.
  2709. * @tparam Type The type to check.
  2710. */
  2711. template<typename Type>
  2712. struct entt_traits<Type, std::enable_if_t<std::is_enum_v<Type>>>
  2713. : entt_traits<std::underlying_type_t<Type>>
  2714. {};
  2715. /**
  2716. * @brief Entity traits for a 16 bits entity identifier.
  2717. *
  2718. * A 16 bits entity identifier guarantees:
  2719. *
  2720. * * 12 bits for the entity number (up to 4k entities).
  2721. * * 4 bit for the version (resets in [0-15]).
  2722. */
  2723. template<>
  2724. struct entt_traits<std::uint16_t> {
  2725. /*! @brief Underlying entity type. */
  2726. using entity_type = std::uint16_t;
  2727. /*! @brief Underlying version type. */
  2728. using version_type = std::uint8_t;
  2729. /*! @brief Difference type. */
  2730. using difference_type = std::int16_t;
  2731. /*! @brief Mask to use to get the entity number out of an identifier. */
  2732. static constexpr entity_type entity_mask = 0xFFF;
  2733. /*! @brief Mask to use to get the version out of an identifier. */
  2734. static constexpr entity_type version_mask = 0xF;
  2735. /*! @brief Extent of the entity number within an identifier. */
  2736. static constexpr std::size_t entity_shift = 12u;
  2737. };
  2738. /**
  2739. * @brief Entity traits for a 32 bits entity identifier.
  2740. *
  2741. * A 32 bits entity identifier guarantees:
  2742. *
  2743. * * 20 bits for the entity number (suitable for almost all the games).
  2744. * * 12 bit for the version (resets in [0-4095]).
  2745. */
  2746. template<>
  2747. struct entt_traits<std::uint32_t> {
  2748. /*! @brief Underlying entity type. */
  2749. using entity_type = std::uint32_t;
  2750. /*! @brief Underlying version type. */
  2751. using version_type = std::uint16_t;
  2752. /*! @brief Difference type. */
  2753. using difference_type = std::int32_t;
  2754. /*! @brief Mask to use to get the entity number out of an identifier. */
  2755. static constexpr entity_type entity_mask = 0xFFFFF;
  2756. /*! @brief Mask to use to get the version out of an identifier. */
  2757. static constexpr entity_type version_mask = 0xFFF;
  2758. /*! @brief Extent of the entity number within an identifier. */
  2759. static constexpr std::size_t entity_shift = 20u;
  2760. };
  2761. /**
  2762. * @brief Entity traits for a 64 bits entity identifier.
  2763. *
  2764. * A 64 bits entity identifier guarantees:
  2765. *
  2766. * * 32 bits for the entity number (an indecently large number).
  2767. * * 32 bit for the version (an indecently large number).
  2768. */
  2769. template<>
  2770. struct entt_traits<std::uint64_t> {
  2771. /*! @brief Underlying entity type. */
  2772. using entity_type = std::uint64_t;
  2773. /*! @brief Underlying version type. */
  2774. using version_type = std::uint32_t;
  2775. /*! @brief Difference type. */
  2776. using difference_type = std::int64_t;
  2777. /*! @brief Mask to use to get the entity number out of an identifier. */
  2778. static constexpr entity_type entity_mask = 0xFFFFFFFF;
  2779. /*! @brief Mask to use to get the version out of an identifier. */
  2780. static constexpr entity_type version_mask = 0xFFFFFFFF;
  2781. /*! @brief Extent of the entity number within an identifier. */
  2782. static constexpr std::size_t entity_shift = 32u;
  2783. };
  2784. /**
  2785. * @brief Converts an entity type to its underlying type.
  2786. * @tparam Entity The value type.
  2787. * @param entity The value to convert.
  2788. * @return The integral representation of the given value.
  2789. */
  2790. template<typename Entity>
  2791. [[nodiscard]] constexpr auto to_integral(const Entity entity) ENTT_NOEXCEPT {
  2792. return static_cast<typename entt_traits<Entity>::entity_type>(entity);
  2793. }
  2794. /*! @brief Null object for all entity identifiers. */
  2795. struct null_t {
  2796. /**
  2797. * @brief Converts the null object to identifiers of any type.
  2798. * @tparam Entity Type of entity identifier.
  2799. * @return The null representation for the given identifier.
  2800. */
  2801. template<typename Entity>
  2802. [[nodiscard]] constexpr operator Entity() const ENTT_NOEXCEPT {
  2803. return Entity{entt_traits<Entity>::entity_mask};
  2804. }
  2805. /**
  2806. * @brief Compares two null objects.
  2807. * @return True in all cases.
  2808. */
  2809. [[nodiscard]] constexpr bool operator==(null_t) const ENTT_NOEXCEPT {
  2810. return true;
  2811. }
  2812. /**
  2813. * @brief Compares two null objects.
  2814. * @return False in all cases.
  2815. */
  2816. [[nodiscard]] constexpr bool operator!=(null_t) const ENTT_NOEXCEPT {
  2817. return false;
  2818. }
  2819. /**
  2820. * @brief Compares a null object and an entity identifier of any type.
  2821. * @tparam Entity Type of entity identifier.
  2822. * @param entity Entity identifier with which to compare.
  2823. * @return False if the two elements differ, true otherwise.
  2824. */
  2825. template<typename Entity>
  2826. [[nodiscard]] constexpr bool operator==(const Entity entity) const ENTT_NOEXCEPT {
  2827. return (to_integral(entity) & entt_traits<Entity>::entity_mask) == to_integral(static_cast<Entity>(*this));
  2828. }
  2829. /**
  2830. * @brief Compares a null object and an entity identifier of any type.
  2831. * @tparam Entity Type of entity identifier.
  2832. * @param entity Entity identifier with which to compare.
  2833. * @return True if the two elements differ, false otherwise.
  2834. */
  2835. template<typename Entity>
  2836. [[nodiscard]] constexpr bool operator!=(const Entity entity) const ENTT_NOEXCEPT {
  2837. return !(entity == *this);
  2838. }
  2839. };
  2840. /**
  2841. * @brief Compares a null object and an entity identifier of any type.
  2842. * @tparam Entity Type of entity identifier.
  2843. * @param entity Entity identifier with which to compare.
  2844. * @param other A null object yet to be converted.
  2845. * @return False if the two elements differ, true otherwise.
  2846. */
  2847. template<typename Entity>
  2848. [[nodiscard]] constexpr bool operator==(const Entity entity, null_t other) ENTT_NOEXCEPT {
  2849. return other.operator==(entity);
  2850. }
  2851. /**
  2852. * @brief Compares a null object and an entity identifier of any type.
  2853. * @tparam Entity Type of entity identifier.
  2854. * @param entity Entity identifier with which to compare.
  2855. * @param other A null object yet to be converted.
  2856. * @return True if the two elements differ, false otherwise.
  2857. */
  2858. template<typename Entity>
  2859. [[nodiscard]] constexpr bool operator!=(const Entity entity, null_t other) ENTT_NOEXCEPT {
  2860. return !(other == entity);
  2861. }
  2862. /**
  2863. * Internal details not to be documented.
  2864. * @endcond
  2865. */
  2866. /**
  2867. * @brief Compile-time constant for null entities.
  2868. *
  2869. * There exist implicit conversions from this variable to entity identifiers of
  2870. * any allowed type. Similarly, there exist comparision operators between the
  2871. * null entity and any other entity identifier.
  2872. */
  2873. inline constexpr null_t null{};
  2874. }
  2875. #endif
  2876. // #include "fwd.hpp"
  2877. #ifndef ENTT_ENTITY_FWD_HPP
  2878. #define ENTT_ENTITY_FWD_HPP
  2879. // #include "../core/fwd.hpp"
  2880. namespace entt {
  2881. template <typename>
  2882. class basic_registry;
  2883. template<typename...>
  2884. class basic_view;
  2885. template<typename>
  2886. class basic_runtime_view;
  2887. template<typename...>
  2888. class basic_group;
  2889. template<typename>
  2890. class basic_observer;
  2891. template <typename>
  2892. struct basic_actor;
  2893. template<typename>
  2894. struct basic_handle;
  2895. template<typename>
  2896. class basic_snapshot;
  2897. template<typename>
  2898. class basic_snapshot_loader;
  2899. template<typename>
  2900. class basic_continuous_loader;
  2901. /*! @brief Default entity identifier. */
  2902. enum class entity: id_type {};
  2903. /*! @brief Alias declaration for the most common use case. */
  2904. using registry = basic_registry<entity>;
  2905. /*! @brief Alias declaration for the most common use case. */
  2906. using observer = basic_observer<entity>;
  2907. /*! @brief Alias declaration for the most common use case. */
  2908. using actor [[deprecated("Consider using the handle class instead")]] = basic_actor<entity>;
  2909. /*! @brief Alias declaration for the most common use case. */
  2910. using handle = basic_handle<entity>;
  2911. /*! @brief Alias declaration for the most common use case. */
  2912. using const_handle = basic_handle<const entity>;
  2913. /*! @brief Alias declaration for the most common use case. */
  2914. using snapshot = basic_snapshot<entity>;
  2915. /*! @brief Alias declaration for the most common use case. */
  2916. using snapshot_loader = basic_snapshot_loader<entity>;
  2917. /*! @brief Alias declaration for the most common use case. */
  2918. using continuous_loader = basic_continuous_loader<entity>;
  2919. /**
  2920. * @brief Alias declaration for the most common use case.
  2921. * @tparam Types Types of components iterated by the view.
  2922. */
  2923. template<typename... Types>
  2924. using view = basic_view<entity, Types...>;
  2925. /*! @brief Alias declaration for the most common use case. */
  2926. using runtime_view = basic_runtime_view<entity>;
  2927. /**
  2928. * @brief Alias declaration for the most common use case.
  2929. * @tparam Types Types of components iterated by the group.
  2930. */
  2931. template<typename... Types>
  2932. using group = basic_group<entity, Types...>;
  2933. }
  2934. #endif
  2935. // #include "group.hpp"
  2936. #ifndef ENTT_ENTITY_GROUP_HPP
  2937. #define ENTT_ENTITY_GROUP_HPP
  2938. #include <tuple>
  2939. #include <utility>
  2940. #include <type_traits>
  2941. // #include "../config/config.h"
  2942. // #include "../core/type_traits.hpp"
  2943. // #include "entity.hpp"
  2944. // #include "fwd.hpp"
  2945. // #include "pool.hpp"
  2946. #ifndef ENTT_ENTITY_POOL_HPP
  2947. #define ENTT_ENTITY_POOL_HPP
  2948. #include <type_traits>
  2949. // #include "storage.hpp"
  2950. #ifndef ENTT_ENTITY_STORAGE_HPP
  2951. #define ENTT_ENTITY_STORAGE_HPP
  2952. #include <algorithm>
  2953. #include <iterator>
  2954. #include <utility>
  2955. #include <vector>
  2956. #include <cstddef>
  2957. #include <type_traits>
  2958. // #include "../config/config.h"
  2959. // #include "../core/algorithm.hpp"
  2960. // #include "../core/type_traits.hpp"
  2961. // #include "entity.hpp"
  2962. // #include "sparse_set.hpp"
  2963. #ifndef ENTT_ENTITY_SPARSE_SET_HPP
  2964. #define ENTT_ENTITY_SPARSE_SET_HPP
  2965. #include <iterator>
  2966. #include <utility>
  2967. #include <vector>
  2968. #include <memory>
  2969. #include <cstddef>
  2970. #include <type_traits>
  2971. // #include "../config/config.h"
  2972. // #include "../core/algorithm.hpp"
  2973. // #include "entity.hpp"
  2974. // #include "fwd.hpp"
  2975. namespace entt {
  2976. /**
  2977. * @brief Basic sparse set implementation.
  2978. *
  2979. * Sparse set or packed array or whatever is the name users give it.<br/>
  2980. * Two arrays: an _external_ one and an _internal_ one; a _sparse_ one and a
  2981. * _packed_ one; one used for direct access through contiguous memory, the other
  2982. * one used to get the data through an extra level of indirection.<br/>
  2983. * This is largely used by the registry to offer users the fastest access ever
  2984. * to the components. Views and groups in general are almost entirely designed
  2985. * around sparse sets.
  2986. *
  2987. * This type of data structure is widely documented in the literature and on the
  2988. * web. This is nothing more than a customized implementation suitable for the
  2989. * purpose of the framework.
  2990. *
  2991. * @note
  2992. * Internal data structures arrange elements to maximize performance. There are
  2993. * no guarantees that entities are returned in the insertion order when iterate
  2994. * a sparse set. Do not make assumption on the order in any case.
  2995. *
  2996. * @tparam Entity A valid entity type (see entt_traits for more details).
  2997. */
  2998. template<typename Entity>
  2999. class sparse_set {
  3000. static_assert(ENTT_PAGE_SIZE && ((ENTT_PAGE_SIZE & (ENTT_PAGE_SIZE - 1)) == 0), "ENTT_PAGE_SIZE must be a power of two");
  3001. static constexpr auto entt_per_page = ENTT_PAGE_SIZE / sizeof(Entity);
  3002. using traits_type = entt_traits<Entity>;
  3003. using page_type = std::unique_ptr<Entity[]>;
  3004. class sparse_set_iterator final {
  3005. friend class sparse_set<Entity>;
  3006. using packed_type = std::vector<Entity>;
  3007. using index_type = typename traits_type::difference_type;
  3008. sparse_set_iterator(const packed_type &ref, const index_type idx) ENTT_NOEXCEPT
  3009. : packed{&ref}, index{idx}
  3010. {}
  3011. public:
  3012. using difference_type = index_type;
  3013. using value_type = Entity;
  3014. using pointer = const value_type *;
  3015. using reference = const value_type &;
  3016. using iterator_category = std::random_access_iterator_tag;
  3017. sparse_set_iterator() ENTT_NOEXCEPT = default;
  3018. sparse_set_iterator & operator++() ENTT_NOEXCEPT {
  3019. return --index, *this;
  3020. }
  3021. sparse_set_iterator operator++(int) ENTT_NOEXCEPT {
  3022. iterator orig = *this;
  3023. return ++(*this), orig;
  3024. }
  3025. sparse_set_iterator & operator--() ENTT_NOEXCEPT {
  3026. return ++index, *this;
  3027. }
  3028. sparse_set_iterator operator--(int) ENTT_NOEXCEPT {
  3029. sparse_set_iterator orig = *this;
  3030. return operator--(), orig;
  3031. }
  3032. sparse_set_iterator & operator+=(const difference_type value) ENTT_NOEXCEPT {
  3033. index -= value;
  3034. return *this;
  3035. }
  3036. sparse_set_iterator operator+(const difference_type value) const ENTT_NOEXCEPT {
  3037. sparse_set_iterator copy = *this;
  3038. return (copy += value);
  3039. }
  3040. sparse_set_iterator & operator-=(const difference_type value) ENTT_NOEXCEPT {
  3041. return (*this += -value);
  3042. }
  3043. sparse_set_iterator operator-(const difference_type value) const ENTT_NOEXCEPT {
  3044. return (*this + -value);
  3045. }
  3046. difference_type operator-(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  3047. return other.index - index;
  3048. }
  3049. [[nodiscard]] reference operator[](const difference_type value) const {
  3050. const auto pos = size_type(index-value-1u);
  3051. return (*packed)[pos];
  3052. }
  3053. [[nodiscard]] bool operator==(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  3054. return other.index == index;
  3055. }
  3056. [[nodiscard]] bool operator!=(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  3057. return !(*this == other);
  3058. }
  3059. [[nodiscard]] bool operator<(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  3060. return index > other.index;
  3061. }
  3062. [[nodiscard]] bool operator>(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  3063. return index < other.index;
  3064. }
  3065. [[nodiscard]] bool operator<=(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  3066. return !(*this > other);
  3067. }
  3068. [[nodiscard]] bool operator>=(const sparse_set_iterator &other) const ENTT_NOEXCEPT {
  3069. return !(*this < other);
  3070. }
  3071. [[nodiscard]] pointer operator->() const {
  3072. const auto pos = size_type(index-1u);
  3073. return &(*packed)[pos];
  3074. }
  3075. [[nodiscard]] reference operator*() const {
  3076. return *operator->();
  3077. }
  3078. private:
  3079. const packed_type *packed;
  3080. index_type index;
  3081. };
  3082. [[nodiscard]] auto page(const Entity entt) const ENTT_NOEXCEPT {
  3083. return size_type{(to_integral(entt) & traits_type::entity_mask) / entt_per_page};
  3084. }
  3085. [[nodiscard]] auto offset(const Entity entt) const ENTT_NOEXCEPT {
  3086. return size_type{to_integral(entt) & (entt_per_page - 1)};
  3087. }
  3088. [[nodiscard]] page_type & assure(const std::size_t pos) {
  3089. if(!(pos < sparse.size())) {
  3090. sparse.resize(pos+1);
  3091. }
  3092. if(!sparse[pos]) {
  3093. sparse[pos].reset(new entity_type[entt_per_page]);
  3094. // null is safe in all cases for our purposes
  3095. for(auto *first = sparse[pos].get(), *last = first + entt_per_page; first != last; ++first) {
  3096. *first = null;
  3097. }
  3098. }
  3099. return sparse[pos];
  3100. }
  3101. public:
  3102. /*! @brief Underlying entity identifier. */
  3103. using entity_type = Entity;
  3104. /*! @brief Unsigned integer type. */
  3105. using size_type = std::size_t;
  3106. /*! @brief Random access iterator type. */
  3107. using iterator = sparse_set_iterator;
  3108. /*! @brief Reverse iterator type. */
  3109. using reverse_iterator = const entity_type *;
  3110. /*! @brief Default constructor. */
  3111. sparse_set() = default;
  3112. /*! @brief Default move constructor. */
  3113. sparse_set(sparse_set &&) = default;
  3114. /*! @brief Default destructor. */
  3115. virtual ~sparse_set() = default;
  3116. /*! @brief Default move assignment operator. @return This sparse set. */
  3117. sparse_set & operator=(sparse_set &&) = default;
  3118. /**
  3119. * @brief Increases the capacity of a sparse set.
  3120. *
  3121. * If the new capacity is greater than the current capacity, new storage is
  3122. * allocated, otherwise the method does nothing.
  3123. *
  3124. * @param cap Desired capacity.
  3125. */
  3126. void reserve(const size_type cap) {
  3127. packed.reserve(cap);
  3128. }
  3129. /**
  3130. * @brief Returns the number of elements that a sparse set has currently
  3131. * allocated space for.
  3132. * @return Capacity of the sparse set.
  3133. */
  3134. [[nodiscard]] size_type capacity() const ENTT_NOEXCEPT {
  3135. return packed.capacity();
  3136. }
  3137. /*! @brief Requests the removal of unused capacity. */
  3138. void shrink_to_fit() {
  3139. // conservative approach
  3140. if(packed.empty()) {
  3141. sparse.clear();
  3142. }
  3143. sparse.shrink_to_fit();
  3144. packed.shrink_to_fit();
  3145. }
  3146. /**
  3147. * @brief Returns the extent of a sparse set.
  3148. *
  3149. * The extent of a sparse set is also the size of the internal sparse array.
  3150. * There is no guarantee that the internal packed array has the same size.
  3151. * Usually the size of the internal sparse array is equal or greater than
  3152. * the one of the internal packed array.
  3153. *
  3154. * @return Extent of the sparse set.
  3155. */
  3156. [[nodiscard]] size_type extent() const ENTT_NOEXCEPT {
  3157. return sparse.size() * entt_per_page;
  3158. }
  3159. /**
  3160. * @brief Returns the number of elements in a sparse set.
  3161. *
  3162. * The number of elements is also the size of the internal packed array.
  3163. * There is no guarantee that the internal sparse array has the same size.
  3164. * Usually the size of the internal sparse array is equal or greater than
  3165. * the one of the internal packed array.
  3166. *
  3167. * @return Number of elements.
  3168. */
  3169. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  3170. return packed.size();
  3171. }
  3172. /**
  3173. * @brief Checks whether a sparse set is empty.
  3174. * @return True if the sparse set is empty, false otherwise.
  3175. */
  3176. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  3177. return packed.empty();
  3178. }
  3179. /**
  3180. * @brief Direct access to the internal packed array.
  3181. *
  3182. * The returned pointer is such that range `[data(), data() + size()]` is
  3183. * always a valid range, even if the container is empty.
  3184. *
  3185. * @note
  3186. * Entities are in the reverse order as returned by the `begin`/`end`
  3187. * iterators.
  3188. *
  3189. * @return A pointer to the internal packed array.
  3190. */
  3191. [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT {
  3192. return packed.data();
  3193. }
  3194. /**
  3195. * @brief Returns an iterator to the beginning.
  3196. *
  3197. * The returned iterator points to the first entity of the internal packed
  3198. * array. If the sparse set is empty, the returned iterator will be equal to
  3199. * `end()`.
  3200. *
  3201. * @return An iterator to the first entity of the internal packed array.
  3202. */
  3203. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  3204. const typename traits_type::difference_type pos = packed.size();
  3205. return iterator{packed, pos};
  3206. }
  3207. /**
  3208. * @brief Returns an iterator to the end.
  3209. *
  3210. * The returned iterator points to the element following the last entity in
  3211. * the internal packed array. Attempting to dereference the returned
  3212. * iterator results in undefined behavior.
  3213. *
  3214. * @return An iterator to the element following the last entity of the
  3215. * internal packed array.
  3216. */
  3217. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  3218. return iterator{packed, {}};
  3219. }
  3220. /**
  3221. * @brief Returns a reverse iterator to the beginning.
  3222. *
  3223. * The returned iterator points to the first entity of the reversed internal
  3224. * packed array. If the sparse set is empty, the returned iterator will be
  3225. * equal to `rend()`.
  3226. *
  3227. * @return An iterator to the first entity of the reversed internal packed
  3228. * array.
  3229. */
  3230. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  3231. return packed.data();
  3232. }
  3233. /**
  3234. * @brief Returns a reverse iterator to the end.
  3235. *
  3236. * The returned iterator points to the element following the last entity in
  3237. * the reversed internal packed array. Attempting to dereference the
  3238. * returned iterator results in undefined behavior.
  3239. *
  3240. * @return An iterator to the element following the last entity of the
  3241. * reversed internal packed array.
  3242. */
  3243. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  3244. return rbegin() + packed.size();
  3245. }
  3246. /**
  3247. * @brief Finds an entity.
  3248. * @param entt A valid entity identifier.
  3249. * @return An iterator to the given entity if it's found, past the end
  3250. * iterator otherwise.
  3251. */
  3252. [[nodiscard]] iterator find(const entity_type entt) const {
  3253. return contains(entt) ? --(end() - index(entt)) : end();
  3254. }
  3255. /**
  3256. * @brief Checks if a sparse set contains an entity.
  3257. * @param entt A valid entity identifier.
  3258. * @return True if the sparse set contains the entity, false otherwise.
  3259. */
  3260. [[nodiscard]] bool contains(const entity_type entt) const {
  3261. const auto curr = page(entt);
  3262. // testing against null permits to avoid accessing the packed array
  3263. return (curr < sparse.size() && sparse[curr] && sparse[curr][offset(entt)] != null);
  3264. }
  3265. /**
  3266. * @brief Returns the position of an entity in a sparse set.
  3267. *
  3268. * @warning
  3269. * Attempting to get the position of an entity that doesn't belong to the
  3270. * sparse set results in undefined behavior.<br/>
  3271. * An assertion will abort the execution at runtime in debug mode if the
  3272. * sparse set doesn't contain the given entity.
  3273. *
  3274. * @param entt A valid entity identifier.
  3275. * @return The position of the entity in the sparse set.
  3276. */
  3277. [[nodiscard]] size_type index(const entity_type entt) const {
  3278. ENTT_ASSERT(contains(entt));
  3279. return size_type{to_integral(sparse[page(entt)][offset(entt)])};
  3280. }
  3281. /**
  3282. * @brief Assigns an entity to a sparse set.
  3283. *
  3284. * @warning
  3285. * Attempting to assign an entity that already belongs to the sparse set
  3286. * results in undefined behavior.<br/>
  3287. * An assertion will abort the execution at runtime in debug mode if the
  3288. * sparse set already contains the given entity.
  3289. *
  3290. * @param entt A valid entity identifier.
  3291. */
  3292. void emplace(const entity_type entt) {
  3293. ENTT_ASSERT(!contains(entt));
  3294. assure(page(entt))[offset(entt)] = entity_type(static_cast<typename traits_type::entity_type>(packed.size()));
  3295. packed.push_back(entt);
  3296. }
  3297. /**
  3298. * @brief Assigns one or more entities to a sparse set.
  3299. *
  3300. * @warning
  3301. * Attempting to assign an entity that already belongs to the sparse set
  3302. * results in undefined behavior.<br/>
  3303. * An assertion will abort the execution at runtime in debug mode if the
  3304. * sparse set already contains the given entity.
  3305. *
  3306. * @tparam It Type of input iterator.
  3307. * @param first An iterator to the first element of the range of entities.
  3308. * @param last An iterator past the last element of the range of entities.
  3309. */
  3310. template<typename It>
  3311. void insert(It first, It last) {
  3312. auto next = static_cast<typename traits_type::entity_type>(packed.size());
  3313. packed.insert(packed.end(), first, last);
  3314. while(first != last) {
  3315. const auto entt = *(first++);
  3316. ENTT_ASSERT(!contains(entt));
  3317. assure(page(entt))[offset(entt)] = entity_type(next++);
  3318. }
  3319. }
  3320. /**
  3321. * @brief Removes an entity from a sparse set.
  3322. *
  3323. * @warning
  3324. * Attempting to remove an entity that doesn't belong to the sparse set
  3325. * results in undefined behavior.<br/>
  3326. * An assertion will abort the execution at runtime in debug mode if the
  3327. * sparse set doesn't contain the given entity.
  3328. *
  3329. * @param entt A valid entity identifier.
  3330. */
  3331. void erase(const entity_type entt) {
  3332. ENTT_ASSERT(contains(entt));
  3333. const auto curr = page(entt);
  3334. const auto pos = offset(entt);
  3335. packed[size_type{to_integral(sparse[curr][pos])}] = packed.back();
  3336. sparse[page(packed.back())][offset(packed.back())] = sparse[curr][pos];
  3337. sparse[curr][pos] = null;
  3338. packed.pop_back();
  3339. }
  3340. /**
  3341. * @brief Swaps two entities in the internal packed array.
  3342. *
  3343. * For what it's worth, this function affects both the internal sparse array
  3344. * and the internal packed array. Users should not care of that anyway.
  3345. *
  3346. * @warning
  3347. * Attempting to swap entities that don't belong to the sparse set results
  3348. * in undefined behavior.<br/>
  3349. * An assertion will abort the execution at runtime in debug mode if the
  3350. * sparse set doesn't contain the given entities.
  3351. *
  3352. * @param lhs A valid entity identifier.
  3353. * @param rhs A valid entity identifier.
  3354. */
  3355. virtual void swap(const entity_type lhs, const entity_type rhs) {
  3356. auto &from = sparse[page(lhs)][offset(lhs)];
  3357. auto &to = sparse[page(rhs)][offset(rhs)];
  3358. std::swap(packed[size_type{to_integral(from)}], packed[size_type{to_integral(to)}]);
  3359. std::swap(from, to);
  3360. }
  3361. /**
  3362. * @brief Sort elements according to the given comparison function.
  3363. *
  3364. * Sort the elements so that iterating the range with a couple of iterators
  3365. * returns them in the expected order. See `begin` and `end` for more
  3366. * details.
  3367. *
  3368. * The comparison function object must return `true` if the first element
  3369. * is _less_ than the second one, `false` otherwise. The signature of the
  3370. * comparison function should be equivalent to the following:
  3371. *
  3372. * @code{.cpp}
  3373. * bool(const Entity, const Entity);
  3374. * @endcode
  3375. *
  3376. * Moreover, the comparison function object shall induce a
  3377. * _strict weak ordering_ on the values.
  3378. *
  3379. * The sort function oject must offer a member function template
  3380. * `operator()` that accepts three arguments:
  3381. *
  3382. * * An iterator to the first element of the range to sort.
  3383. * * An iterator past the last element of the range to sort.
  3384. * * A comparison function to use to compare the elements.
  3385. *
  3386. * @tparam Compare Type of comparison function object.
  3387. * @tparam Sort Type of sort function object.
  3388. * @tparam Args Types of arguments to forward to the sort function object.
  3389. * @param first An iterator to the first element of the range to sort.
  3390. * @param last An iterator past the last element of the range to sort.
  3391. * @param compare A valid comparison function object.
  3392. * @param algo A valid sort function object.
  3393. * @param args Arguments to forward to the sort function object, if any.
  3394. */
  3395. template<typename Compare, typename Sort = std_sort, typename... Args>
  3396. void sort(iterator first, iterator last, Compare compare, Sort algo = Sort{}, Args &&... args) {
  3397. ENTT_ASSERT(!(last < first));
  3398. ENTT_ASSERT(!(last > end()));
  3399. const auto length = std::distance(first, last);
  3400. const auto skip = std::distance(last, end());
  3401. const auto to = packed.rend() - skip;
  3402. const auto from = to - length;
  3403. algo(from, to, std::move(compare), std::forward<Args>(args)...);
  3404. for(size_type pos = skip, end = skip+length; pos < end; ++pos) {
  3405. sparse[page(packed[pos])][offset(packed[pos])] = entity_type(static_cast<typename traits_type::entity_type>(pos));
  3406. }
  3407. }
  3408. /**
  3409. * @brief Sort elements according to the given comparison function.
  3410. *
  3411. * @sa sort
  3412. *
  3413. * This function is a slightly slower version of `sort` that invokes the
  3414. * caller to indicate which entities are swapped.<br/>
  3415. * It's recommended when the caller wants to sort its own data structures to
  3416. * align them with the order induced in the sparse set.
  3417. *
  3418. * The signature of the callback should be equivalent to the following:
  3419. *
  3420. * @code{.cpp}
  3421. * bool(const Entity, const Entity);
  3422. * @endcode
  3423. *
  3424. * @tparam Apply Type of function object to invoke to notify the caller.
  3425. * @tparam Compare Type of comparison function object.
  3426. * @tparam Sort Type of sort function object.
  3427. * @tparam Args Types of arguments to forward to the sort function object.
  3428. * @param first An iterator to the first element of the range to sort.
  3429. * @param last An iterator past the last element of the range to sort.
  3430. * @param apply A valid function object to use as a callback.
  3431. * @param compare A valid comparison function object.
  3432. * @param algo A valid sort function object.
  3433. * @param args Arguments to forward to the sort function object, if any.
  3434. */
  3435. template<typename Apply, typename Compare, typename Sort = std_sort, typename... Args>
  3436. void arrange(iterator first, iterator last, Apply apply, Compare compare, Sort algo = Sort{}, Args &&... args) {
  3437. ENTT_ASSERT(!(last < first));
  3438. ENTT_ASSERT(!(last > end()));
  3439. const auto length = std::distance(first, last);
  3440. const auto skip = std::distance(last, end());
  3441. const auto to = packed.rend() - skip;
  3442. const auto from = to - length;
  3443. algo(from, to, std::move(compare), std::forward<Args>(args)...);
  3444. for(size_type pos = skip, end = skip+length; pos < end; ++pos) {
  3445. auto curr = pos;
  3446. auto next = index(packed[curr]);
  3447. while(curr != next) {
  3448. apply(packed[curr], packed[next]);
  3449. sparse[page(packed[curr])][offset(packed[curr])] = entity_type(static_cast<typename traits_type::entity_type>(curr));
  3450. curr = next;
  3451. next = index(packed[curr]);
  3452. }
  3453. }
  3454. }
  3455. /**
  3456. * @brief Sort entities according to their order in another sparse set.
  3457. *
  3458. * Entities that are part of both the sparse sets are ordered internally
  3459. * according to the order they have in `other`. All the other entities goes
  3460. * to the end of the list and there are no guarantees on their order.<br/>
  3461. * In other terms, this function can be used to impose the same order on two
  3462. * sets by using one of them as a master and the other one as a slave.
  3463. *
  3464. * Iterating the sparse set with a couple of iterators returns elements in
  3465. * the expected order after a call to `respect`. See `begin` and `end` for
  3466. * more details.
  3467. *
  3468. * @param other The sparse sets that imposes the order of the entities.
  3469. */
  3470. void respect(const sparse_set &other) {
  3471. const auto to = other.end();
  3472. auto from = other.begin();
  3473. size_type pos = packed.size() - 1;
  3474. while(pos && from != to) {
  3475. if(contains(*from)) {
  3476. if(*from != packed[pos]) {
  3477. swap(packed[pos], *from);
  3478. }
  3479. --pos;
  3480. }
  3481. ++from;
  3482. }
  3483. }
  3484. /**
  3485. * @brief Clears a sparse set.
  3486. */
  3487. void clear() ENTT_NOEXCEPT {
  3488. sparse.clear();
  3489. packed.clear();
  3490. }
  3491. private:
  3492. std::vector<page_type> sparse;
  3493. std::vector<entity_type> packed;
  3494. };
  3495. }
  3496. #endif
  3497. namespace entt {
  3498. /**
  3499. * @brief Basic storage implementation.
  3500. *
  3501. * This class is a refinement of a sparse set that associates an object to an
  3502. * entity. The main purpose of this class is to extend sparse sets to store
  3503. * components in a registry. It guarantees fast access both to the elements and
  3504. * to the entities.
  3505. *
  3506. * @note
  3507. * Entities and objects have the same order. It's guaranteed both in case of raw
  3508. * access (either to entities or objects) and when using random or input access
  3509. * iterators.
  3510. *
  3511. * @note
  3512. * Internal data structures arrange elements to maximize performance. There are
  3513. * no guarantees that objects are returned in the insertion order when iterate
  3514. * a storage. Do not make assumption on the order in any case.
  3515. *
  3516. * @warning
  3517. * Empty types aren't explicitly instantiated. Therefore, many of the functions
  3518. * normally available for non-empty types will not be available for empty ones.
  3519. *
  3520. * @sa sparse_set<Entity>
  3521. *
  3522. * @tparam Entity A valid entity type (see entt_traits for more details).
  3523. * @tparam Type Type of objects assigned to the entities.
  3524. */
  3525. template<typename Entity, typename Type, typename = std::void_t<>>
  3526. class storage: public sparse_set<Entity> {
  3527. static_assert(std::is_move_constructible_v<Type> && std::is_move_assignable_v<Type>, "The managed type must be at least move constructible and assignable");
  3528. using underlying_type = sparse_set<Entity>;
  3529. using traits_type = entt_traits<Entity>;
  3530. template<bool Const>
  3531. class storage_iterator final {
  3532. friend class storage<Entity, Type>;
  3533. using instance_type = std::conditional_t<Const, const std::vector<Type>, std::vector<Type>>;
  3534. using index_type = typename traits_type::difference_type;
  3535. storage_iterator(instance_type &ref, const index_type idx) ENTT_NOEXCEPT
  3536. : instances{&ref}, index{idx}
  3537. {}
  3538. public:
  3539. using difference_type = index_type;
  3540. using value_type = Type;
  3541. using pointer = std::conditional_t<Const, const value_type *, value_type *>;
  3542. using reference = std::conditional_t<Const, const value_type &, value_type &>;
  3543. using iterator_category = std::random_access_iterator_tag;
  3544. storage_iterator() ENTT_NOEXCEPT = default;
  3545. storage_iterator & operator++() ENTT_NOEXCEPT {
  3546. return --index, *this;
  3547. }
  3548. storage_iterator operator++(int) ENTT_NOEXCEPT {
  3549. storage_iterator orig = *this;
  3550. return ++(*this), orig;
  3551. }
  3552. storage_iterator & operator--() ENTT_NOEXCEPT {
  3553. return ++index, *this;
  3554. }
  3555. storage_iterator operator--(int) ENTT_NOEXCEPT {
  3556. storage_iterator orig = *this;
  3557. return operator--(), orig;
  3558. }
  3559. storage_iterator & operator+=(const difference_type value) ENTT_NOEXCEPT {
  3560. index -= value;
  3561. return *this;
  3562. }
  3563. storage_iterator operator+(const difference_type value) const ENTT_NOEXCEPT {
  3564. storage_iterator copy = *this;
  3565. return (copy += value);
  3566. }
  3567. storage_iterator & operator-=(const difference_type value) ENTT_NOEXCEPT {
  3568. return (*this += -value);
  3569. }
  3570. storage_iterator operator-(const difference_type value) const ENTT_NOEXCEPT {
  3571. return (*this + -value);
  3572. }
  3573. difference_type operator-(const storage_iterator &other) const ENTT_NOEXCEPT {
  3574. return other.index - index;
  3575. }
  3576. [[nodiscard]] reference operator[](const difference_type value) const ENTT_NOEXCEPT {
  3577. const auto pos = size_type(index-value-1);
  3578. return (*instances)[pos];
  3579. }
  3580. [[nodiscard]] bool operator==(const storage_iterator &other) const ENTT_NOEXCEPT {
  3581. return other.index == index;
  3582. }
  3583. [[nodiscard]] bool operator!=(const storage_iterator &other) const ENTT_NOEXCEPT {
  3584. return !(*this == other);
  3585. }
  3586. [[nodiscard]] bool operator<(const storage_iterator &other) const ENTT_NOEXCEPT {
  3587. return index > other.index;
  3588. }
  3589. [[nodiscard]] bool operator>(const storage_iterator &other) const ENTT_NOEXCEPT {
  3590. return index < other.index;
  3591. }
  3592. [[nodiscard]] bool operator<=(const storage_iterator &other) const ENTT_NOEXCEPT {
  3593. return !(*this > other);
  3594. }
  3595. [[nodiscard]] bool operator>=(const storage_iterator &other) const ENTT_NOEXCEPT {
  3596. return !(*this < other);
  3597. }
  3598. [[nodiscard]] pointer operator->() const ENTT_NOEXCEPT {
  3599. const auto pos = size_type(index-1u);
  3600. return &(*instances)[pos];
  3601. }
  3602. [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
  3603. return *operator->();
  3604. }
  3605. private:
  3606. instance_type *instances;
  3607. index_type index;
  3608. };
  3609. public:
  3610. /*! @brief Type of the objects associated with the entities. */
  3611. using object_type = Type;
  3612. /*! @brief Underlying entity identifier. */
  3613. using entity_type = Entity;
  3614. /*! @brief Unsigned integer type. */
  3615. using size_type = std::size_t;
  3616. /*! @brief Random access iterator type. */
  3617. using iterator = storage_iterator<false>;
  3618. /*! @brief Constant random access iterator type. */
  3619. using const_iterator = storage_iterator<true>;
  3620. /*! @brief Reverse iterator type. */
  3621. using reverse_iterator = Type *;
  3622. /*! @brief Constant reverse iterator type. */
  3623. using const_reverse_iterator = const Type *;
  3624. /**
  3625. * @brief Increases the capacity of a storage.
  3626. *
  3627. * If the new capacity is greater than the current capacity, new storage is
  3628. * allocated, otherwise the method does nothing.
  3629. *
  3630. * @param cap Desired capacity.
  3631. */
  3632. void reserve(const size_type cap) {
  3633. underlying_type::reserve(cap);
  3634. instances.reserve(cap);
  3635. }
  3636. /*! @brief Requests the removal of unused capacity. */
  3637. void shrink_to_fit() {
  3638. underlying_type::shrink_to_fit();
  3639. instances.shrink_to_fit();
  3640. }
  3641. /**
  3642. * @brief Direct access to the array of objects.
  3643. *
  3644. * The returned pointer is such that range `[raw(), raw() + size()]` is
  3645. * always a valid range, even if the container is empty.
  3646. *
  3647. * @note
  3648. * Objects are in the reverse order as returned by the `begin`/`end`
  3649. * iterators.
  3650. *
  3651. * @return A pointer to the array of objects.
  3652. */
  3653. [[nodiscard]] const object_type * raw() const ENTT_NOEXCEPT {
  3654. return instances.data();
  3655. }
  3656. /*! @copydoc raw */
  3657. [[nodiscard]] object_type * raw() ENTT_NOEXCEPT {
  3658. return const_cast<object_type *>(std::as_const(*this).raw());
  3659. }
  3660. /**
  3661. * @brief Returns an iterator to the beginning.
  3662. *
  3663. * The returned iterator points to the first instance of the internal array.
  3664. * If the storage is empty, the returned iterator will be equal to `end()`.
  3665. *
  3666. * @return An iterator to the first instance of the internal array.
  3667. */
  3668. [[nodiscard]] const_iterator cbegin() const ENTT_NOEXCEPT {
  3669. const typename traits_type::difference_type pos = underlying_type::size();
  3670. return const_iterator{instances, pos};
  3671. }
  3672. /*! @copydoc cbegin */
  3673. [[nodiscard]] const_iterator begin() const ENTT_NOEXCEPT {
  3674. return cbegin();
  3675. }
  3676. /*! @copydoc begin */
  3677. [[nodiscard]] iterator begin() ENTT_NOEXCEPT {
  3678. const typename traits_type::difference_type pos = underlying_type::size();
  3679. return iterator{instances, pos};
  3680. }
  3681. /**
  3682. * @brief Returns an iterator to the end.
  3683. *
  3684. * The returned iterator points to the element following the last instance
  3685. * of the internal array. Attempting to dereference the returned iterator
  3686. * results in undefined behavior.
  3687. *
  3688. * @return An iterator to the element following the last instance of the
  3689. * internal array.
  3690. */
  3691. [[nodiscard]] const_iterator cend() const ENTT_NOEXCEPT {
  3692. return const_iterator{instances, {}};
  3693. }
  3694. /*! @copydoc cend */
  3695. [[nodiscard]] const_iterator end() const ENTT_NOEXCEPT {
  3696. return cend();
  3697. }
  3698. /*! @copydoc end */
  3699. [[nodiscard]] iterator end() ENTT_NOEXCEPT {
  3700. return iterator{instances, {}};
  3701. }
  3702. /**
  3703. * @brief Returns a reverse iterator to the beginning.
  3704. *
  3705. * The returned iterator points to the first instance of the reversed
  3706. * internal array. If the storage is empty, the returned iterator will be
  3707. * equal to `rend()`.
  3708. *
  3709. * @return An iterator to the first instance of the reversed internal array.
  3710. */
  3711. [[nodiscard]] const_reverse_iterator crbegin() const ENTT_NOEXCEPT {
  3712. return instances.data();
  3713. }
  3714. /*! @copydoc crbegin */
  3715. [[nodiscard]] const_reverse_iterator rbegin() const ENTT_NOEXCEPT {
  3716. return crbegin();
  3717. }
  3718. /*! @copydoc rbegin */
  3719. [[nodiscard]] reverse_iterator rbegin() ENTT_NOEXCEPT {
  3720. return instances.data();
  3721. }
  3722. /**
  3723. * @brief Returns a reverse iterator to the end.
  3724. *
  3725. * The returned iterator points to the element following the last instance
  3726. * of the reversed internal array. Attempting to dereference the returned
  3727. * iterator results in undefined behavior.
  3728. *
  3729. * @return An iterator to the element following the last instance of the
  3730. * reversed internal array.
  3731. */
  3732. [[nodiscard]] const_reverse_iterator crend() const ENTT_NOEXCEPT {
  3733. return crbegin() + instances.size();
  3734. }
  3735. /*! @copydoc crend */
  3736. [[nodiscard]] const_reverse_iterator rend() const ENTT_NOEXCEPT {
  3737. return crend();
  3738. }
  3739. /*! @copydoc rend */
  3740. [[nodiscard]] reverse_iterator rend() ENTT_NOEXCEPT {
  3741. return rbegin() + instances.size();
  3742. }
  3743. /**
  3744. * @brief Returns the object associated with an entity.
  3745. *
  3746. * @warning
  3747. * Attempting to use an entity that doesn't belong to the storage results in
  3748. * undefined behavior.<br/>
  3749. * An assertion will abort the execution at runtime in debug mode if the
  3750. * storage doesn't contain the given entity.
  3751. *
  3752. * @param entt A valid entity identifier.
  3753. * @return The object associated with the entity.
  3754. */
  3755. [[nodiscard]] const object_type & get(const entity_type entt) const {
  3756. return instances[underlying_type::index(entt)];
  3757. }
  3758. /*! @copydoc get */
  3759. [[nodiscard]] object_type & get(const entity_type entt) {
  3760. return const_cast<object_type &>(std::as_const(*this).get(entt));
  3761. }
  3762. /**
  3763. * @brief Returns a pointer to the object associated with an entity, if any.
  3764. * @param entt A valid entity identifier.
  3765. * @return The object associated with the entity, if any.
  3766. */
  3767. [[nodiscard]] const object_type * try_get(const entity_type entt) const {
  3768. return underlying_type::contains(entt) ? (instances.data() + underlying_type::index(entt)) : nullptr;
  3769. }
  3770. /*! @copydoc try_get */
  3771. [[nodiscard]] object_type * try_get(const entity_type entt) {
  3772. return const_cast<object_type *>(std::as_const(*this).try_get(entt));
  3773. }
  3774. /**
  3775. * @brief Assigns an entity to a storage and constructs its object.
  3776. *
  3777. * This version accept both types that can be constructed in place directly
  3778. * and types like aggregates that do not work well with a placement new as
  3779. * performed usually under the hood during an _emplace back_.
  3780. *
  3781. * @warning
  3782. * Attempting to use an entity that already belongs to the storage results
  3783. * in undefined behavior.<br/>
  3784. * An assertion will abort the execution at runtime in debug mode if the
  3785. * storage already contains the given entity.
  3786. *
  3787. * @tparam Args Types of arguments to use to construct the object.
  3788. * @param entt A valid entity identifier.
  3789. * @param args Parameters to use to construct an object for the entity.
  3790. */
  3791. template<typename... Args>
  3792. void emplace(const entity_type entt, Args &&... args) {
  3793. if constexpr(std::is_aggregate_v<object_type>) {
  3794. instances.push_back(Type{std::forward<Args>(args)...});
  3795. } else {
  3796. instances.emplace_back(std::forward<Args>(args)...);
  3797. }
  3798. // entity goes after component in case constructor throws
  3799. underlying_type::emplace(entt);
  3800. }
  3801. /**
  3802. * @brief Assigns one or more entities to a storage and constructs their
  3803. * objects from a given instance.
  3804. *
  3805. * @warning
  3806. * Attempting to assign an entity that already belongs to the storage
  3807. * results in undefined behavior.<br/>
  3808. * An assertion will abort the execution at runtime in debug mode if the
  3809. * storage already contains the given entity.
  3810. *
  3811. * @tparam It Type of input iterator.
  3812. * @param first An iterator to the first element of the range of entities.
  3813. * @param last An iterator past the last element of the range of entities.
  3814. * @param value An instance of the object to construct.
  3815. */
  3816. template<typename It>
  3817. void insert(It first, It last, const object_type &value = {}) {
  3818. instances.insert(instances.end(), std::distance(first, last), value);
  3819. // entities go after components in case constructors throw
  3820. underlying_type::insert(first, last);
  3821. }
  3822. /**
  3823. * @brief Assigns one or more entities to a storage and constructs their
  3824. * objects from a given range.
  3825. *
  3826. * @sa construct
  3827. *
  3828. * @tparam EIt Type of input iterator.
  3829. * @tparam CIt Type of input iterator.
  3830. * @param first An iterator to the first element of the range of entities.
  3831. * @param last An iterator past the last element of the range of entities.
  3832. * @param from An iterator to the first element of the range of objects.
  3833. * @param to An iterator past the last element of the range of objects.
  3834. */
  3835. template<typename EIt, typename CIt>
  3836. void insert(EIt first, EIt last, CIt from, CIt to) {
  3837. instances.insert(instances.end(), from, to);
  3838. // entities go after components in case constructors throw
  3839. underlying_type::insert(first, last);
  3840. }
  3841. /**
  3842. * @brief Removes an entity from a storage and destroys its object.
  3843. *
  3844. * @warning
  3845. * Attempting to use an entity that doesn't belong to the storage results in
  3846. * undefined behavior.<br/>
  3847. * An assertion will abort the execution at runtime in debug mode if the
  3848. * storage doesn't contain the given entity.
  3849. *
  3850. * @param entt A valid entity identifier.
  3851. */
  3852. void erase(const entity_type entt) {
  3853. auto other = std::move(instances.back());
  3854. instances[underlying_type::index(entt)] = std::move(other);
  3855. instances.pop_back();
  3856. underlying_type::erase(entt);
  3857. }
  3858. /**
  3859. * @brief Swaps entities and objects in the internal packed arrays.
  3860. *
  3861. * @warning
  3862. * Attempting to swap entities that don't belong to the sparse set results
  3863. * in undefined behavior.<br/>
  3864. * An assertion will abort the execution at runtime in debug mode if the
  3865. * sparse set doesn't contain the given entities.
  3866. *
  3867. * @param lhs A valid entity identifier.
  3868. * @param rhs A valid entity identifier.
  3869. */
  3870. void swap(const entity_type lhs, const entity_type rhs) override {
  3871. std::swap(instances[underlying_type::index(lhs)], instances[underlying_type::index(rhs)]);
  3872. underlying_type::swap(lhs, rhs);
  3873. }
  3874. /**
  3875. * @brief Sort elements according to the given comparison function.
  3876. *
  3877. * Sort the elements so that iterating the range with a couple of iterators
  3878. * returns them in the expected order. See `begin` and `end` for more
  3879. * details.
  3880. *
  3881. * The comparison function object must return `true` if the first element
  3882. * is _less_ than the second one, `false` otherwise. The signature of the
  3883. * comparison function should be equivalent to one of the following:
  3884. *
  3885. * @code{.cpp}
  3886. * bool(const Entity, const Entity);
  3887. * bool(const Type &, const Type &);
  3888. * @endcode
  3889. *
  3890. * Moreover, the comparison function object shall induce a
  3891. * _strict weak ordering_ on the values.
  3892. *
  3893. * The sort function oject must offer a member function template
  3894. * `operator()` that accepts three arguments:
  3895. *
  3896. * * An iterator to the first element of the range to sort.
  3897. * * An iterator past the last element of the range to sort.
  3898. * * A comparison function to use to compare the elements.
  3899. *
  3900. * @warning
  3901. * Empty types are never instantiated. Therefore, only comparison function
  3902. * objects that require to return entities rather than components are
  3903. * accepted.
  3904. *
  3905. * @tparam Compare Type of comparison function object.
  3906. * @tparam Sort Type of sort function object.
  3907. * @tparam Args Types of arguments to forward to the sort function object.
  3908. * @param first An iterator to the first element of the range to sort.
  3909. * @param last An iterator past the last element of the range to sort.
  3910. * @param compare A valid comparison function object.
  3911. * @param algo A valid sort function object.
  3912. * @param args Arguments to forward to the sort function object, if any.
  3913. */
  3914. template<typename Compare, typename Sort = std_sort, typename... Args>
  3915. void sort(iterator first, iterator last, Compare compare, Sort algo = Sort{}, Args &&... args) {
  3916. ENTT_ASSERT(!(last < first));
  3917. ENTT_ASSERT(!(last > end()));
  3918. const auto from = underlying_type::begin() + std::distance(begin(), first);
  3919. const auto to = from + std::distance(first, last);
  3920. const auto apply = [this](const auto lhs, const auto rhs) {
  3921. std::swap(instances[underlying_type::index(lhs)], instances[underlying_type::index(rhs)]);
  3922. };
  3923. if constexpr(std::is_invocable_v<Compare, const object_type &, const object_type &>) {
  3924. underlying_type::arrange(from, to, std::move(apply), [this, compare = std::move(compare)](const auto lhs, const auto rhs) {
  3925. return compare(std::as_const(instances[underlying_type::index(lhs)]), std::as_const(instances[underlying_type::index(rhs)]));
  3926. }, std::move(algo), std::forward<Args>(args)...);
  3927. } else {
  3928. underlying_type::arrange(from, to, std::move(apply), std::move(compare), std::move(algo), std::forward<Args>(args)...);
  3929. }
  3930. }
  3931. /*! @brief Clears a storage. */
  3932. void clear() {
  3933. underlying_type::clear();
  3934. instances.clear();
  3935. }
  3936. private:
  3937. std::vector<object_type> instances;
  3938. };
  3939. /*! @copydoc storage */
  3940. template<typename Entity, typename Type>
  3941. class storage<Entity, Type, std::enable_if_t<is_eto_eligible_v<Type>>>: public sparse_set<Entity> {
  3942. using underlying_type = sparse_set<Entity>;
  3943. public:
  3944. /*! @brief Type of the objects associated with the entities. */
  3945. using object_type = Type;
  3946. /*! @brief Underlying entity identifier. */
  3947. using entity_type = Entity;
  3948. /*! @brief Unsigned integer type. */
  3949. using size_type = std::size_t;
  3950. /**
  3951. * @brief Assigns an entity to a storage and constructs its object.
  3952. *
  3953. * @warning
  3954. * Attempting to use an entity that already belongs to the storage results
  3955. * in undefined behavior.<br/>
  3956. * An assertion will abort the execution at runtime in debug mode if the
  3957. * storage already contains the given entity.
  3958. *
  3959. * @tparam Args Types of arguments to use to construct the object.
  3960. * @param entt A valid entity identifier.
  3961. * @param args Parameters to use to construct an object for the entity.
  3962. */
  3963. template<typename... Args>
  3964. void emplace(const entity_type entt, Args &&... args) {
  3965. [[maybe_unused]] object_type instance{std::forward<Args>(args)...};
  3966. underlying_type::emplace(entt);
  3967. }
  3968. /**
  3969. * @brief Assigns one or more entities to a storage.
  3970. *
  3971. * @warning
  3972. * Attempting to assign an entity that already belongs to the storage
  3973. * results in undefined behavior.<br/>
  3974. * An assertion will abort the execution at runtime in debug mode if the
  3975. * storage already contains the given entity.
  3976. *
  3977. * @tparam It Type of input iterator.
  3978. * @param first An iterator to the first element of the range of entities.
  3979. * @param last An iterator past the last element of the range of entities.
  3980. */
  3981. template<typename It>
  3982. void insert(It first, It last, const object_type & = {}) {
  3983. underlying_type::insert(first, last);
  3984. }
  3985. };
  3986. }
  3987. #endif
  3988. namespace entt {
  3989. /**
  3990. * @brief Applies component-to-pool conversion and defines the resulting type as
  3991. * the member typedef type.
  3992. *
  3993. * Formally:
  3994. *
  3995. * * If the component type is a non-const one, the member typedef type is the
  3996. * declared storage type.
  3997. * * If the component type is a const one, the member typedef type is the
  3998. * declared storage type, except it has a const-qualifier added.
  3999. *
  4000. * @tparam Entity A valid entity type (see entt_traits for more details).
  4001. * @tparam Type Type of objects assigned to the entities.
  4002. */
  4003. template<typename Entity, typename Type, typename = void>
  4004. struct pool {
  4005. /*! @brief Resulting type after component-to-pool conversion. */
  4006. using type = storage<Entity, Type>;
  4007. };
  4008. /*! @copydoc pool */
  4009. template<typename Entity, typename Type>
  4010. struct pool<Entity, const Type> {
  4011. /*! @brief Resulting type after component-to-pool conversion. */
  4012. using type = std::add_const_t<typename pool<Entity, std::remove_const_t<Type>>::type>;
  4013. };
  4014. /**
  4015. * @brief Alias declaration to use to make component-to-pool conversions.
  4016. * @tparam Entity A valid entity type (see entt_traits for more details).
  4017. * @tparam Type Type of objects assigned to the entities.
  4018. */
  4019. template<typename Entity, typename Type>
  4020. using pool_t = typename pool<Entity, Type>::type;
  4021. }
  4022. #endif
  4023. // #include "sparse_set.hpp"
  4024. // #include "utility.hpp"
  4025. #ifndef ENTT_ENTITY_UTILITY_HPP
  4026. #define ENTT_ENTITY_UTILITY_HPP
  4027. // #include "../core/type_traits.hpp"
  4028. namespace entt {
  4029. /**
  4030. * @brief Alias for exclusion lists.
  4031. * @tparam Type List of types.
  4032. */
  4033. template<typename... Type>
  4034. struct exclude_t: type_list<Type...> {};
  4035. /**
  4036. * @brief Variable template for exclusion lists.
  4037. * @tparam Type List of types.
  4038. */
  4039. template<typename... Type>
  4040. inline constexpr exclude_t<Type...> exclude{};
  4041. /**
  4042. * @brief Alias for lists of observed components.
  4043. * @tparam Type List of types.
  4044. */
  4045. template<typename... Type>
  4046. struct get_t: type_list<Type...>{};
  4047. /**
  4048. * @brief Variable template for lists of observed components.
  4049. * @tparam Type List of types.
  4050. */
  4051. template<typename... Type>
  4052. inline constexpr get_t<Type...> get{};
  4053. }
  4054. #endif
  4055. namespace entt {
  4056. /**
  4057. * @brief Group.
  4058. *
  4059. * Primary template isn't defined on purpose. All the specializations give a
  4060. * compile-time error, but for a few reasonable cases.
  4061. */
  4062. template<typename...>
  4063. class basic_group;
  4064. /**
  4065. * @brief Non-owning group.
  4066. *
  4067. * A non-owning group returns all entities and only the entities that have at
  4068. * least the given components. Moreover, it's guaranteed that the entity list
  4069. * is tightly packed in memory for fast iterations.
  4070. *
  4071. * @b Important
  4072. *
  4073. * Iterators aren't invalidated if:
  4074. *
  4075. * * New instances of the given components are created and assigned to entities.
  4076. * * The entity currently pointed is modified (as an example, if one of the
  4077. * given components is removed from the entity to which the iterator points).
  4078. * * The entity currently pointed is destroyed.
  4079. *
  4080. * In all other cases, modifying the pools iterated by the group in any way
  4081. * invalidates all the iterators and using them results in undefined behavior.
  4082. *
  4083. * @note
  4084. * Groups share references to the underlying data structures of the registry
  4085. * that generated them. Therefore any change to the entities and to the
  4086. * components made by means of the registry are immediately reflected by all the
  4087. * groups.<br/>
  4088. * Moreover, sorting a non-owning group affects all the instances of the same
  4089. * group (it means that users don't have to call `sort` on each instance to sort
  4090. * all of them because they _share_ entities and components).
  4091. *
  4092. * @warning
  4093. * Lifetime of a group must not overcome that of the registry that generated it.
  4094. * In any other case, attempting to use a group results in undefined behavior.
  4095. *
  4096. * @tparam Entity A valid entity type (see entt_traits for more details).
  4097. * @tparam Exclude Types of components used to filter the group.
  4098. * @tparam Get Type of components observed by the group.
  4099. */
  4100. template<typename Entity, typename... Exclude, typename... Get>
  4101. class basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>> {
  4102. /*! @brief A registry is allowed to create groups. */
  4103. friend class basic_registry<Entity>;
  4104. template<typename Component>
  4105. using pool_type = pool_t<Entity, Component>;
  4106. class group_proxy {
  4107. friend class basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>>;
  4108. class proxy_iterator {
  4109. friend class group_proxy;
  4110. using it_type = typename sparse_set<Entity>::iterator;
  4111. using ref_type = decltype(std::tuple_cat(std::declval<std::conditional_t<is_eto_eligible_v<Get>, std::tuple<>, std::tuple<pool_type<Get> *>>>()...));
  4112. proxy_iterator(it_type from, ref_type ref) ENTT_NOEXCEPT
  4113. : it{from},
  4114. pools{ref}
  4115. {}
  4116. public:
  4117. using difference_type = std::ptrdiff_t;
  4118. using value_type = decltype(std::tuple_cat(
  4119. std::declval<std::tuple<Entity>>(),
  4120. std::declval<std::conditional_t<is_eto_eligible_v<Get>, std::tuple<>, std::tuple<Get>>>()...
  4121. ));
  4122. using pointer = void;
  4123. using reference = decltype(std::tuple_cat(
  4124. std::declval<std::tuple<Entity>>(),
  4125. std::declval<std::conditional_t<is_eto_eligible_v<Get>, std::tuple<>, std::tuple<Get &>>>()...
  4126. ));
  4127. using iterator_category = std::input_iterator_tag;
  4128. proxy_iterator & operator++() ENTT_NOEXCEPT {
  4129. return ++it, *this;
  4130. }
  4131. proxy_iterator operator++(int) ENTT_NOEXCEPT {
  4132. proxy_iterator orig = *this;
  4133. return ++(*this), orig;
  4134. }
  4135. [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
  4136. return std::apply([entt = *it](auto *... cpool) { return reference{entt, cpool->get(entt)...}; }, pools);
  4137. }
  4138. [[nodiscard]] bool operator==(const proxy_iterator &other) const ENTT_NOEXCEPT {
  4139. return other.it == it;
  4140. }
  4141. [[nodiscard]] bool operator!=(const proxy_iterator &other) const ENTT_NOEXCEPT {
  4142. return !(*this == other);
  4143. }
  4144. private:
  4145. it_type it{};
  4146. ref_type pools{};
  4147. };
  4148. group_proxy(const sparse_set<Entity> &ref, std::tuple<pool_type<Get> *...> gpools)
  4149. : handler{&ref},
  4150. pools{gpools}
  4151. {}
  4152. public:
  4153. using iterator = proxy_iterator;
  4154. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  4155. return proxy_iterator{handler->begin(), std::tuple_cat([](auto *cpool) {
  4156. if constexpr(is_eto_eligible_v<typename std::decay_t<decltype(*cpool)>::object_type>) {
  4157. return std::make_tuple();
  4158. } else {
  4159. return std::make_tuple(cpool);
  4160. }
  4161. }(std::get<pool_type<Get> *>(pools))...)};
  4162. }
  4163. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  4164. return proxy_iterator{handler->end(), std::tuple_cat([](auto *cpool) {
  4165. if constexpr(is_eto_eligible_v<typename std::decay_t<decltype(*cpool)>::object_type>) {
  4166. return std::make_tuple();
  4167. } else {
  4168. return std::make_tuple(cpool);
  4169. }
  4170. }(std::get<pool_type<Get> *>(pools))...)};
  4171. }
  4172. private:
  4173. const sparse_set<Entity> *handler;
  4174. std::tuple<pool_type<Get> *...> pools;
  4175. };
  4176. basic_group(sparse_set<Entity> &ref, pool_type<Get> &... gpool) ENTT_NOEXCEPT
  4177. : handler{&ref},
  4178. pools{&gpool...}
  4179. {}
  4180. template<typename Func, typename... Weak>
  4181. void traverse(Func func, type_list<Weak...>) const {
  4182. for(const auto entt: *handler) {
  4183. if constexpr(std::is_invocable_v<Func, decltype(get<Weak>({}))...>) {
  4184. func(std::get<pool_type<Weak> *>(pools)->get(entt)...);
  4185. } else {
  4186. func(entt, std::get<pool_type<Weak> *>(pools)->get(entt)...);
  4187. }
  4188. }
  4189. }
  4190. public:
  4191. /*! @brief Underlying entity identifier. */
  4192. using entity_type = Entity;
  4193. /*! @brief Unsigned integer type. */
  4194. using size_type = std::size_t;
  4195. /*! @brief Random access iterator type. */
  4196. using iterator = typename sparse_set<Entity>::iterator;
  4197. /*! @brief Reversed iterator type. */
  4198. using reverse_iterator = typename sparse_set<Entity>::reverse_iterator;
  4199. /**
  4200. * @brief Returns the number of existing components of the given type.
  4201. * @tparam Component Type of component of which to return the size.
  4202. * @return Number of existing components of the given type.
  4203. */
  4204. template<typename Component>
  4205. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  4206. return std::get<pool_type<Component> *>(pools)->size();
  4207. }
  4208. /**
  4209. * @brief Returns the number of entities that have the given components.
  4210. * @return Number of entities that have the given components.
  4211. */
  4212. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  4213. return handler->size();
  4214. }
  4215. /**
  4216. * @brief Returns the number of elements that a group has currently
  4217. * allocated space for.
  4218. * @return Capacity of the group.
  4219. */
  4220. [[nodiscard]] size_type capacity() const ENTT_NOEXCEPT {
  4221. return handler->capacity();
  4222. }
  4223. /*! @brief Requests the removal of unused capacity. */
  4224. void shrink_to_fit() {
  4225. handler->shrink_to_fit();
  4226. }
  4227. /**
  4228. * @brief Checks whether a group or some pools are empty.
  4229. * @tparam Component Types of components in which one is interested.
  4230. * @return True if the group or the pools are empty, false otherwise.
  4231. */
  4232. template<typename... Component>
  4233. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  4234. if constexpr(sizeof...(Component) == 0) {
  4235. return handler->empty();
  4236. } else {
  4237. return (std::get<pool_type<Component> *>(pools)->empty() && ...);
  4238. }
  4239. }
  4240. /**
  4241. * @brief Direct access to the list of components of a given pool.
  4242. *
  4243. * The returned pointer is such that range
  4244. * `[raw<Component>(), raw<Component>() + size<Component>()]` is always a
  4245. * valid range, even if the container is empty.
  4246. *
  4247. * @note
  4248. * Components are in the reverse order as returned by the `begin`/`end`
  4249. * iterators.
  4250. *
  4251. * @tparam Component Type of component in which one is interested.
  4252. * @return A pointer to the array of components.
  4253. */
  4254. template<typename Component>
  4255. [[nodiscard]] Component * raw() const ENTT_NOEXCEPT {
  4256. return std::get<pool_type<Component> *>(pools)->raw();
  4257. }
  4258. /**
  4259. * @brief Direct access to the list of entities of a given pool.
  4260. *
  4261. * The returned pointer is such that range
  4262. * `[data<Component>(), data<Component>() + size<Component>()]` is always a
  4263. * valid range, even if the container is empty.
  4264. *
  4265. * @note
  4266. * Entities are in the reverse order as returned by the `begin`/`end`
  4267. * iterators.
  4268. *
  4269. * @tparam Component Type of component in which one is interested.
  4270. * @return A pointer to the array of entities.
  4271. */
  4272. template<typename Component>
  4273. [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT {
  4274. return std::get<pool_type<Component> *>(pools)->data();
  4275. }
  4276. /**
  4277. * @brief Direct access to the list of entities.
  4278. *
  4279. * The returned pointer is such that range `[data(), data() + size()]` is
  4280. * always a valid range, even if the container is empty.
  4281. *
  4282. * @note
  4283. * Entities are in the reverse order as returned by the `begin`/`end`
  4284. * iterators.
  4285. *
  4286. * @return A pointer to the array of entities.
  4287. */
  4288. [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT {
  4289. return handler->data();
  4290. }
  4291. /**
  4292. * @brief Returns an iterator to the first entity of the group.
  4293. *
  4294. * The returned iterator points to the first entity of the group. If the
  4295. * group is empty, the returned iterator will be equal to `end()`.
  4296. *
  4297. * @note
  4298. * Iterators stay true to the order imposed to the underlying data
  4299. * structures.
  4300. *
  4301. * @return An iterator to the first entity of the group.
  4302. */
  4303. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  4304. return handler->begin();
  4305. }
  4306. /**
  4307. * @brief Returns an iterator that is past the last entity of the group.
  4308. *
  4309. * The returned iterator points to the entity following the last entity of
  4310. * the group. Attempting to dereference the returned iterator results in
  4311. * undefined behavior.
  4312. *
  4313. * @note
  4314. * Iterators stay true to the order imposed to the underlying data
  4315. * structures.
  4316. *
  4317. * @return An iterator to the entity following the last entity of the
  4318. * group.
  4319. */
  4320. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  4321. return handler->end();
  4322. }
  4323. /**
  4324. * @brief Returns an iterator to the first entity of the reversed group.
  4325. *
  4326. * The returned iterator points to the first entity of the reversed group.
  4327. * If the group is empty, the returned iterator will be equal to `rend()`.
  4328. *
  4329. * @note
  4330. * Iterators stay true to the order imposed to the underlying data
  4331. * structures.
  4332. *
  4333. * @return An iterator to the first entity of the reversed group.
  4334. */
  4335. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  4336. return handler->rbegin();
  4337. }
  4338. /**
  4339. * @brief Returns an iterator that is past the last entity of the reversed
  4340. * group.
  4341. *
  4342. * The returned iterator points to the entity following the last entity of
  4343. * the reversed group. Attempting to dereference the returned iterator
  4344. * results in undefined behavior.
  4345. *
  4346. * @note
  4347. * Iterators stay true to the order imposed to the underlying data
  4348. * structures.
  4349. *
  4350. * @return An iterator to the entity following the last entity of the
  4351. * reversed group.
  4352. */
  4353. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  4354. return handler->rend();
  4355. }
  4356. /**
  4357. * @brief Returns the first entity of the group, if any.
  4358. * @return The first entity of the group if one exists, the null entity
  4359. * otherwise.
  4360. */
  4361. [[nodiscard]] entity_type front() const {
  4362. const auto it = begin();
  4363. return it != end() ? *it : null;
  4364. }
  4365. /**
  4366. * @brief Returns the last entity of the group, if any.
  4367. * @return The last entity of the group if one exists, the null entity
  4368. * otherwise.
  4369. */
  4370. [[nodiscard]] entity_type back() const {
  4371. const auto it = rbegin();
  4372. return it != rend() ? *it : null;
  4373. }
  4374. /**
  4375. * @brief Finds an entity.
  4376. * @param entt A valid entity identifier.
  4377. * @return An iterator to the given entity if it's found, past the end
  4378. * iterator otherwise.
  4379. */
  4380. [[nodiscard]] iterator find(const entity_type entt) const {
  4381. const auto it = handler->find(entt);
  4382. return it != end() && *it == entt ? it : end();
  4383. }
  4384. /**
  4385. * @brief Returns the identifier that occupies the given position.
  4386. * @param pos Position of the element to return.
  4387. * @return The identifier that occupies the given position.
  4388. */
  4389. [[nodiscard]] entity_type operator[](const size_type pos) const {
  4390. return begin()[pos];
  4391. }
  4392. /**
  4393. * @brief Checks if a group contains an entity.
  4394. * @param entt A valid entity identifier.
  4395. * @return True if the group contains the given entity, false otherwise.
  4396. */
  4397. [[nodiscard]] bool contains(const entity_type entt) const {
  4398. return handler->contains(entt);
  4399. }
  4400. /**
  4401. * @brief Returns the components assigned to the given entity.
  4402. *
  4403. * Prefer this function instead of `registry::get` during iterations. It has
  4404. * far better performance than its counterpart.
  4405. *
  4406. * @warning
  4407. * Attempting to use an invalid component type results in a compilation
  4408. * error. Attempting to use an entity that doesn't belong to the group
  4409. * results in undefined behavior.<br/>
  4410. * An assertion will abort the execution at runtime in debug mode if the
  4411. * group doesn't contain the given entity.
  4412. *
  4413. * @tparam Component Types of components to get.
  4414. * @param entt A valid entity identifier.
  4415. * @return The components assigned to the entity.
  4416. */
  4417. template<typename... Component>
  4418. [[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entt) const {
  4419. ENTT_ASSERT(contains(entt));
  4420. if constexpr(sizeof...(Component) == 1) {
  4421. return (std::get<pool_type<Component> *>(pools)->get(entt), ...);
  4422. } else {
  4423. return std::tuple<decltype(get<Component>({}))...>{get<Component>(entt)...};
  4424. }
  4425. }
  4426. /**
  4427. * @brief Iterates entities and components and applies the given function
  4428. * object to them.
  4429. *
  4430. * The function object is invoked for each entity. It is provided with the
  4431. * entity itself and a set of references to non-empty components. The
  4432. * _constness_ of the components is as requested.<br/>
  4433. * The signature of the function must be equivalent to one of the following
  4434. * forms:
  4435. *
  4436. * @code{.cpp}
  4437. * void(const entity_type, Type &...);
  4438. * void(Type &...);
  4439. * @endcode
  4440. *
  4441. * @note
  4442. * Empty types aren't explicitly instantiated and therefore they are never
  4443. * returned during iterations.
  4444. *
  4445. * @tparam Func Type of the function object to invoke.
  4446. * @param func A valid function object.
  4447. */
  4448. template<typename Func>
  4449. void each(Func func) const {
  4450. using get_type_list = type_list_cat_t<std::conditional_t<is_eto_eligible_v<Get>, type_list<>, type_list<Get>>...>;
  4451. traverse(std::move(func), get_type_list{});
  4452. }
  4453. /**
  4454. * @brief Returns an iterable object to use to _visit_ the group.
  4455. *
  4456. * The iterable object returns tuples that contain the current entity and a
  4457. * set of references to its non-empty components. The _constness_ of the
  4458. * components is as requested.
  4459. *
  4460. * @note
  4461. * Empty types aren't explicitly instantiated and therefore they are never
  4462. * returned during iterations.
  4463. *
  4464. * @return An iterable object to use to _visit_ the group.
  4465. */
  4466. [[nodiscard]] auto proxy() const ENTT_NOEXCEPT {
  4467. return group_proxy{*handler, pools};
  4468. }
  4469. /**
  4470. * @brief Sort a group according to the given comparison function.
  4471. *
  4472. * Sort the group so that iterating it with a couple of iterators returns
  4473. * entities and components in the expected order. See `begin` and `end` for
  4474. * more details.
  4475. *
  4476. * The comparison function object must return `true` if the first element
  4477. * is _less_ than the second one, `false` otherwise. The signature of the
  4478. * comparison function should be equivalent to one of the following:
  4479. *
  4480. * @code{.cpp}
  4481. * bool(std::tuple<Component &...>, std::tuple<Component &...>);
  4482. * bool(const Component &..., const Component &...);
  4483. * bool(const Entity, const Entity);
  4484. * @endcode
  4485. *
  4486. * Where `Component` are such that they are iterated by the group.<br/>
  4487. * Moreover, the comparison function object shall induce a
  4488. * _strict weak ordering_ on the values.
  4489. *
  4490. * The sort function oject must offer a member function template
  4491. * `operator()` that accepts three arguments:
  4492. *
  4493. * * An iterator to the first element of the range to sort.
  4494. * * An iterator past the last element of the range to sort.
  4495. * * A comparison function to use to compare the elements.
  4496. *
  4497. * @tparam Component Optional types of components to compare.
  4498. * @tparam Compare Type of comparison function object.
  4499. * @tparam Sort Type of sort function object.
  4500. * @tparam Args Types of arguments to forward to the sort function object.
  4501. * @param compare A valid comparison function object.
  4502. * @param algo A valid sort function object.
  4503. * @param args Arguments to forward to the sort function object, if any.
  4504. */
  4505. template<typename... Component, typename Compare, typename Sort = std_sort, typename... Args>
  4506. void sort(Compare compare, Sort algo = Sort{}, Args &&... args) {
  4507. if constexpr(sizeof...(Component) == 0) {
  4508. static_assert(std::is_invocable_v<Compare, const entity_type, const entity_type>, "Invalid comparison function");
  4509. handler->sort(handler->begin(), handler->end(), std::move(compare), std::move(algo), std::forward<Args>(args)...);
  4510. } else if constexpr(sizeof...(Component) == 1) {
  4511. handler->sort(handler->begin(), handler->end(), [this, compare = std::move(compare)](const entity_type lhs, const entity_type rhs) {
  4512. return compare((std::get<pool_type<Component> *>(pools)->get(lhs), ...), (std::get<pool_type<Component> *>(pools)->get(rhs), ...));
  4513. }, std::move(algo), std::forward<Args>(args)...);
  4514. } else {
  4515. handler->sort(handler->begin(), handler->end(), [this, compare = std::move(compare)](const entity_type lhs, const entity_type rhs) {
  4516. return compare(std::tuple<decltype(get<Component>({}))...>{std::get<pool_type<Component> *>(pools)->get(lhs)...}, std::tuple<decltype(get<Component>({}))...>{std::get<pool_type<Component> *>(pools)->get(rhs)...});
  4517. }, std::move(algo), std::forward<Args>(args)...);
  4518. }
  4519. }
  4520. /**
  4521. * @brief Sort the shared pool of entities according to the given component.
  4522. *
  4523. * Non-owning groups of the same type share with the registry a pool of
  4524. * entities with its own order that doesn't depend on the order of any pool
  4525. * of components. Users can order the underlying data structure so that it
  4526. * respects the order of the pool of the given component.
  4527. *
  4528. * @note
  4529. * The shared pool of entities and thus its order is affected by the changes
  4530. * to each and every pool that it tracks. Therefore changes to those pools
  4531. * can quickly ruin the order imposed to the pool of entities shared between
  4532. * the non-owning groups.
  4533. *
  4534. * @tparam Component Type of component to use to impose the order.
  4535. */
  4536. template<typename Component>
  4537. void sort() const {
  4538. handler->respect(*std::get<pool_type<Component> *>(pools));
  4539. }
  4540. private:
  4541. sparse_set<entity_type> *handler;
  4542. const std::tuple<pool_type<Get> *...> pools;
  4543. };
  4544. /**
  4545. * @brief Owning group.
  4546. *
  4547. * Owning groups return all entities and only the entities that have at least
  4548. * the given components. Moreover:
  4549. *
  4550. * * It's guaranteed that the entity list is tightly packed in memory for fast
  4551. * iterations.
  4552. * * It's guaranteed that the lists of owned components are tightly packed in
  4553. * memory for even faster iterations and to allow direct access.
  4554. * * They stay true to the order of the owned components and all instances have
  4555. * the same order in memory.
  4556. *
  4557. * The more types of components are owned by a group, the faster it is to
  4558. * iterate them.
  4559. *
  4560. * @b Important
  4561. *
  4562. * Iterators aren't invalidated if:
  4563. *
  4564. * * New instances of the given components are created and assigned to entities.
  4565. * * The entity currently pointed is modified (as an example, if one of the
  4566. * given components is removed from the entity to which the iterator points).
  4567. * * The entity currently pointed is destroyed.
  4568. *
  4569. * In all other cases, modifying the pools iterated by the group in any way
  4570. * invalidates all the iterators and using them results in undefined behavior.
  4571. *
  4572. * @note
  4573. * Groups share references to the underlying data structures of the registry
  4574. * that generated them. Therefore any change to the entities and to the
  4575. * components made by means of the registry are immediately reflected by all the
  4576. * groups.
  4577. * Moreover, sorting an owning group affects all the instance of the same group
  4578. * (it means that users don't have to call `sort` on each instance to sort all
  4579. * of them because they share the underlying data structure).
  4580. *
  4581. * @warning
  4582. * Lifetime of a group must not overcome that of the registry that generated it.
  4583. * In any other case, attempting to use a group results in undefined behavior.
  4584. *
  4585. * @tparam Entity A valid entity type (see entt_traits for more details).
  4586. * @tparam Exclude Types of components used to filter the group.
  4587. * @tparam Get Types of components observed by the group.
  4588. * @tparam Owned Types of components owned by the group.
  4589. */
  4590. template<typename Entity, typename... Exclude, typename... Get, typename... Owned>
  4591. class basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>, Owned...> {
  4592. /*! @brief A registry is allowed to create groups. */
  4593. friend class basic_registry<Entity>;
  4594. template<typename Component>
  4595. using pool_type = pool_t<Entity, Component>;
  4596. template<typename Component>
  4597. using component_iterator = decltype(std::declval<pool_type<Component>>().begin());
  4598. class group_proxy {
  4599. friend class basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>, Owned...>;
  4600. class proxy_iterator {
  4601. friend class group_proxy;
  4602. using it_type = typename sparse_set<Entity>::iterator;
  4603. using owned_type = decltype(std::tuple_cat(std::declval<std::conditional_t<is_eto_eligible_v<Owned>, std::tuple<>, std::tuple<component_iterator<Owned>>>>()...));
  4604. using get_type = decltype(std::tuple_cat(std::declval<std::conditional_t<is_eto_eligible_v<Get>, std::tuple<>, std::tuple<pool_type<Get> *>>>()...));
  4605. proxy_iterator(it_type from, owned_type oref, get_type gref) ENTT_NOEXCEPT
  4606. : it{from},
  4607. owned{oref},
  4608. get{gref}
  4609. {}
  4610. public:
  4611. using difference_type = std::ptrdiff_t;
  4612. using value_type = decltype(std::tuple_cat(
  4613. std::declval<std::tuple<Entity>>(),
  4614. std::declval<std::conditional_t<is_eto_eligible_v<Owned>, std::tuple<>, std::tuple<Owned>>>()...,
  4615. std::declval<std::conditional_t<is_eto_eligible_v<Get>, std::tuple<>, std::tuple<Get>>>()...
  4616. ));
  4617. using pointer = void;
  4618. using reference = decltype(std::tuple_cat(
  4619. std::declval<std::tuple<Entity>>(),
  4620. std::declval<std::conditional_t<is_eto_eligible_v<Owned>, std::tuple<>, std::tuple<Owned &>>>()...,
  4621. std::declval<std::conditional_t<is_eto_eligible_v<Get>, std::tuple<>, std::tuple<Get &>>>()...
  4622. ));
  4623. using iterator_category = std::input_iterator_tag;
  4624. proxy_iterator & operator++() ENTT_NOEXCEPT {
  4625. return ++it, std::apply([](auto &&... curr) { (++curr, ...); }, owned), *this;
  4626. }
  4627. proxy_iterator operator++(int) ENTT_NOEXCEPT {
  4628. proxy_iterator orig = *this;
  4629. return ++(*this), orig;
  4630. }
  4631. [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
  4632. return std::tuple_cat(
  4633. std::make_tuple(*it),
  4634. std::apply([](auto &&... curr) { return std::forward_as_tuple(*curr...); }, owned),
  4635. std::apply([entt = *it](auto &&... curr) { return std::forward_as_tuple(curr->get(entt)...); }, get)
  4636. );
  4637. }
  4638. [[nodiscard]] bool operator==(const proxy_iterator &other) const ENTT_NOEXCEPT {
  4639. return other.it == it;
  4640. }
  4641. [[nodiscard]] bool operator!=(const proxy_iterator &other) const ENTT_NOEXCEPT {
  4642. return !(*this == other);
  4643. }
  4644. private:
  4645. it_type it{};
  4646. owned_type owned{};
  4647. get_type get{};
  4648. };
  4649. group_proxy(std::tuple<pool_type<Owned> *..., pool_type<Get> *...> cpools, const std::size_t &extent)
  4650. : pools{cpools},
  4651. length{&extent}
  4652. {}
  4653. public:
  4654. using iterator = proxy_iterator;
  4655. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  4656. return proxy_iterator{
  4657. std::get<0>(pools)->sparse_set<Entity>::end() - *length,
  4658. std::tuple_cat([length = *length](auto *cpool) {
  4659. if constexpr(is_eto_eligible_v<typename std::decay_t<decltype(*cpool)>::object_type>) {
  4660. return std::make_tuple();
  4661. } else {
  4662. return std::make_tuple(cpool->end() - length);
  4663. }
  4664. }(std::get<pool_type<Owned> *>(pools))...),
  4665. std::tuple_cat([](auto *cpool) {
  4666. if constexpr(is_eto_eligible_v<typename std::decay_t<decltype(*cpool)>::object_type>) {
  4667. return std::make_tuple();
  4668. } else {
  4669. return std::make_tuple(cpool);
  4670. }
  4671. }(std::get<pool_type<Get> *>(pools))...)
  4672. };
  4673. }
  4674. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  4675. return proxy_iterator{
  4676. std::get<0>(pools)->sparse_set<Entity>::end(),
  4677. std::tuple_cat([](auto *cpool) {
  4678. if constexpr(is_eto_eligible_v<typename std::decay_t<decltype(*cpool)>::object_type>) {
  4679. return std::make_tuple();
  4680. } else {
  4681. return std::make_tuple(cpool->end());
  4682. }
  4683. }(std::get<pool_type<Owned> *>(pools))...),
  4684. std::tuple_cat([](auto *cpool) {
  4685. if constexpr(is_eto_eligible_v<typename std::decay_t<decltype(*cpool)>::object_type>) {
  4686. return std::make_tuple();
  4687. } else {
  4688. return std::make_tuple(cpool);
  4689. }
  4690. }(std::get<pool_type<Get> *>(pools))...)
  4691. };
  4692. }
  4693. private:
  4694. const std::tuple<pool_type<Owned> *..., pool_type<Get> *...> pools;
  4695. const std::size_t *length;
  4696. };
  4697. basic_group(const std::size_t &extent, pool_type<Owned> &... opool, pool_type<Get> &... gpool) ENTT_NOEXCEPT
  4698. : pools{&opool..., &gpool...},
  4699. length{&extent}
  4700. {}
  4701. template<typename Func, typename... Strong, typename... Weak>
  4702. void traverse(Func func, type_list<Strong...>, type_list<Weak...>) const {
  4703. [[maybe_unused]] auto it = std::make_tuple((std::get<pool_type<Strong> *>(pools)->end() - *length)...);
  4704. [[maybe_unused]] auto data = std::get<0>(pools)->sparse_set<entity_type>::end() - *length;
  4705. for(auto next = *length; next; --next) {
  4706. if constexpr(std::is_invocable_v<Func, decltype(get<Strong>({}))..., decltype(get<Weak>({}))...>) {
  4707. if constexpr(sizeof...(Weak) == 0) {
  4708. func(*(std::get<component_iterator<Strong>>(it)++)...);
  4709. } else {
  4710. const auto entt = *(data++);
  4711. func(*(std::get<component_iterator<Strong>>(it)++)..., std::get<pool_type<Weak> *>(pools)->get(entt)...);
  4712. }
  4713. } else {
  4714. const auto entt = *(data++);
  4715. func(entt, *(std::get<component_iterator<Strong>>(it)++)..., std::get<pool_type<Weak> *>(pools)->get(entt)...);
  4716. }
  4717. }
  4718. }
  4719. public:
  4720. /*! @brief Underlying entity identifier. */
  4721. using entity_type = Entity;
  4722. /*! @brief Unsigned integer type. */
  4723. using size_type = std::size_t;
  4724. /*! @brief Random access iterator type. */
  4725. using iterator = typename sparse_set<Entity>::iterator;
  4726. /*! @brief Reversed iterator type. */
  4727. using reverse_iterator = typename sparse_set<Entity>::reverse_iterator;
  4728. /**
  4729. * @brief Returns the number of existing components of the given type.
  4730. * @tparam Component Type of component of which to return the size.
  4731. * @return Number of existing components of the given type.
  4732. */
  4733. template<typename Component>
  4734. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  4735. return std::get<pool_type<Component> *>(pools)->size();
  4736. }
  4737. /**
  4738. * @brief Returns the number of entities that have the given components.
  4739. * @return Number of entities that have the given components.
  4740. */
  4741. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  4742. return *length;
  4743. }
  4744. /**
  4745. * @brief Checks whether a group or some pools are empty.
  4746. * @tparam Component Types of components in which one is interested.
  4747. * @return True if the group or the pools are empty, false otherwise.
  4748. */
  4749. template<typename... Component>
  4750. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  4751. if constexpr(sizeof...(Component) == 0) {
  4752. return !*length;
  4753. } else {
  4754. return (std::get<pool_type<Component> *>(pools)->empty() && ...);
  4755. }
  4756. }
  4757. /**
  4758. * @brief Direct access to the list of components of a given pool.
  4759. *
  4760. * The returned pointer is such that range
  4761. * `[raw<Component>(), raw<Component>() + size<Component>()]` is always a
  4762. * valid range, even if the container is empty.<br/>
  4763. * Moreover, in case the group owns the given component, the range
  4764. * `[raw<Component>(), raw<Component>() + size()]` is such that it contains
  4765. * the instances that are part of the group itself.
  4766. *
  4767. * @note
  4768. * Components are in the reverse order as returned by the `begin`/`end`
  4769. * iterators.
  4770. *
  4771. * @tparam Component Type of component in which one is interested.
  4772. * @return A pointer to the array of components.
  4773. */
  4774. template<typename Component>
  4775. [[nodiscard]] Component * raw() const ENTT_NOEXCEPT {
  4776. return std::get<pool_type<Component> *>(pools)->raw();
  4777. }
  4778. /**
  4779. * @brief Direct access to the list of entities of a given pool.
  4780. *
  4781. * The returned pointer is such that range
  4782. * `[data<Component>(), data<Component>() + size<Component>()]` is always a
  4783. * valid range, even if the container is empty.<br/>
  4784. * Moreover, in case the group owns the given component, the range
  4785. * `[data<Component>(), data<Component>() + size()]` is such that it
  4786. * contains the entities that are part of the group itself.
  4787. *
  4788. * @note
  4789. * Entities are in the reverse order as returned by the `begin`/`end`
  4790. * iterators.
  4791. *
  4792. * @tparam Component Type of component in which one is interested.
  4793. * @return A pointer to the array of entities.
  4794. */
  4795. template<typename Component>
  4796. [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT {
  4797. return std::get<pool_type<Component> *>(pools)->data();
  4798. }
  4799. /**
  4800. * @brief Direct access to the list of entities.
  4801. *
  4802. * The returned pointer is such that range `[data(), data() + size()]` is
  4803. * always a valid range, even if the container is empty.
  4804. *
  4805. * @note
  4806. * Entities are in the reverse order as returned by the `begin`/`end`
  4807. * iterators.
  4808. *
  4809. * @return A pointer to the array of entities.
  4810. */
  4811. [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT {
  4812. return std::get<0>(pools)->data();
  4813. }
  4814. /**
  4815. * @brief Returns an iterator to the first entity of the group.
  4816. *
  4817. * The returned iterator points to the first entity of the group. If the
  4818. * group is empty, the returned iterator will be equal to `end()`.
  4819. *
  4820. * @note
  4821. * Iterators stay true to the order imposed to the underlying data
  4822. * structures.
  4823. *
  4824. * @return An iterator to the first entity of the group.
  4825. */
  4826. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  4827. return std::get<0>(pools)->sparse_set<entity_type>::end() - *length;
  4828. }
  4829. /**
  4830. * @brief Returns an iterator that is past the last entity of the group.
  4831. *
  4832. * The returned iterator points to the entity following the last entity of
  4833. * the group. Attempting to dereference the returned iterator results in
  4834. * undefined behavior.
  4835. *
  4836. * @note
  4837. * Iterators stay true to the order imposed to the underlying data
  4838. * structures.
  4839. *
  4840. * @return An iterator to the entity following the last entity of the
  4841. * group.
  4842. */
  4843. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  4844. return std::get<0>(pools)->sparse_set<entity_type>::end();
  4845. }
  4846. /**
  4847. * @brief Returns an iterator to the first entity of the reversed group.
  4848. *
  4849. * The returned iterator points to the first entity of the reversed group.
  4850. * If the group is empty, the returned iterator will be equal to `rend()`.
  4851. *
  4852. * @note
  4853. * Iterators stay true to the order imposed to the underlying data
  4854. * structures.
  4855. *
  4856. * @return An iterator to the first entity of the reversed group.
  4857. */
  4858. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  4859. return std::get<0>(pools)->sparse_set<entity_type>::rbegin();
  4860. }
  4861. /**
  4862. * @brief Returns an iterator that is past the last entity of the reversed
  4863. * group.
  4864. *
  4865. * The returned iterator points to the entity following the last entity of
  4866. * the reversed group. Attempting to dereference the returned iterator
  4867. * results in undefined behavior.
  4868. *
  4869. * @note
  4870. * Iterators stay true to the order imposed to the underlying data
  4871. * structures.
  4872. *
  4873. * @return An iterator to the entity following the last entity of the
  4874. * reversed group.
  4875. */
  4876. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  4877. return std::get<0>(pools)->sparse_set<entity_type>::rbegin() + *length;
  4878. }
  4879. /**
  4880. * @brief Returns the first entity of the group, if any.
  4881. * @return The first entity of the group if one exists, the null entity
  4882. * otherwise.
  4883. */
  4884. [[nodiscard]] entity_type front() const {
  4885. const auto it = begin();
  4886. return it != end() ? *it : null;
  4887. }
  4888. /**
  4889. * @brief Returns the last entity of the group, if any.
  4890. * @return The last entity of the group if one exists, the null entity
  4891. * otherwise.
  4892. */
  4893. [[nodiscard]] entity_type back() const {
  4894. const auto it = rbegin();
  4895. return it != rend() ? *it : null;
  4896. }
  4897. /**
  4898. * @brief Finds an entity.
  4899. * @param entt A valid entity identifier.
  4900. * @return An iterator to the given entity if it's found, past the end
  4901. * iterator otherwise.
  4902. */
  4903. [[nodiscard]] iterator find(const entity_type entt) const {
  4904. const auto it = std::get<0>(pools)->find(entt);
  4905. return it != end() && it >= begin() && *it == entt ? it : end();
  4906. }
  4907. /**
  4908. * @brief Returns the identifier that occupies the given position.
  4909. * @param pos Position of the element to return.
  4910. * @return The identifier that occupies the given position.
  4911. */
  4912. [[nodiscard]] entity_type operator[](const size_type pos) const {
  4913. return begin()[pos];
  4914. }
  4915. /**
  4916. * @brief Checks if a group contains an entity.
  4917. * @param entt A valid entity identifier.
  4918. * @return True if the group contains the given entity, false otherwise.
  4919. */
  4920. [[nodiscard]] bool contains(const entity_type entt) const {
  4921. return std::get<0>(pools)->contains(entt) && (std::get<0>(pools)->index(entt) < (*length));
  4922. }
  4923. /**
  4924. * @brief Returns the components assigned to the given entity.
  4925. *
  4926. * Prefer this function instead of `registry::get` during iterations. It has
  4927. * far better performance than its counterpart.
  4928. *
  4929. * @warning
  4930. * Attempting to use an invalid component type results in a compilation
  4931. * error. Attempting to use an entity that doesn't belong to the group
  4932. * results in undefined behavior.<br/>
  4933. * An assertion will abort the execution at runtime in debug mode if the
  4934. * group doesn't contain the given entity.
  4935. *
  4936. * @tparam Component Types of components to get.
  4937. * @param entt A valid entity identifier.
  4938. * @return The components assigned to the entity.
  4939. */
  4940. template<typename... Component>
  4941. [[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entt) const {
  4942. ENTT_ASSERT(contains(entt));
  4943. if constexpr(sizeof...(Component) == 1) {
  4944. return (std::get<pool_type<Component> *>(pools)->get(entt), ...);
  4945. } else {
  4946. return std::tuple<decltype(get<Component>({}))...>{get<Component>(entt)...};
  4947. }
  4948. }
  4949. /**
  4950. * @brief Iterates entities and components and applies the given function
  4951. * object to them.
  4952. *
  4953. * The function object is invoked for each entity. It is provided with the
  4954. * entity itself and a set of references to non-empty components. The
  4955. * _constness_ of the components is as requested.<br/>
  4956. * The signature of the function must be equivalent to one of the following
  4957. * forms:
  4958. *
  4959. * @code{.cpp}
  4960. * void(const entity_type, Type &...);
  4961. * void(Type &...);
  4962. * @endcode
  4963. *
  4964. * @note
  4965. * Empty types aren't explicitly instantiated and therefore they are never
  4966. * returned during iterations.
  4967. *
  4968. * @tparam Func Type of the function object to invoke.
  4969. * @param func A valid function object.
  4970. */
  4971. template<typename Func>
  4972. void each(Func func) const {
  4973. using owned_type_list = type_list_cat_t<std::conditional_t<is_eto_eligible_v<Owned>, type_list<>, type_list<Owned>>...>;
  4974. using get_type_list = type_list_cat_t<std::conditional_t<is_eto_eligible_v<Get>, type_list<>, type_list<Get>>...>;
  4975. traverse(std::move(func), owned_type_list{}, get_type_list{});
  4976. }
  4977. /**
  4978. * @brief Returns an iterable object to use to _visit_ the group.
  4979. *
  4980. * The iterable object returns tuples that contain the current entity and a
  4981. * set of references to its non-empty components. The _constness_ of the
  4982. * components is as requested.
  4983. *
  4984. * @note
  4985. * Empty types aren't explicitly instantiated and therefore they are never
  4986. * returned during iterations.
  4987. *
  4988. * @return An iterable object to use to _visit_ the group.
  4989. */
  4990. [[nodiscard]] auto proxy() const ENTT_NOEXCEPT {
  4991. return group_proxy{pools, *length};
  4992. }
  4993. /**
  4994. * @brief Sort a group according to the given comparison function.
  4995. *
  4996. * Sort the group so that iterating it with a couple of iterators returns
  4997. * entities and components in the expected order. See `begin` and `end` for
  4998. * more details.
  4999. *
  5000. * The comparison function object must return `true` if the first element
  5001. * is _less_ than the second one, `false` otherwise. The signature of the
  5002. * comparison function should be equivalent to one of the following:
  5003. *
  5004. * @code{.cpp}
  5005. * bool(std::tuple<Component &...>, std::tuple<Component &...>);
  5006. * bool(const Component &, const Component &);
  5007. * bool(const Entity, const Entity);
  5008. * @endcode
  5009. *
  5010. * Where `Component` are either owned types or not but still such that they
  5011. * are iterated by the group.<br/>
  5012. * Moreover, the comparison function object shall induce a
  5013. * _strict weak ordering_ on the values.
  5014. *
  5015. * The sort function oject must offer a member function template
  5016. * `operator()` that accepts three arguments:
  5017. *
  5018. * * An iterator to the first element of the range to sort.
  5019. * * An iterator past the last element of the range to sort.
  5020. * * A comparison function to use to compare the elements.
  5021. *
  5022. * @tparam Component Optional types of components to compare.
  5023. * @tparam Compare Type of comparison function object.
  5024. * @tparam Sort Type of sort function object.
  5025. * @tparam Args Types of arguments to forward to the sort function object.
  5026. * @param compare A valid comparison function object.
  5027. * @param algo A valid sort function object.
  5028. * @param args Arguments to forward to the sort function object, if any.
  5029. */
  5030. template<typename... Component, typename Compare, typename Sort = std_sort, typename... Args>
  5031. void sort(Compare compare, Sort algo = Sort{}, Args &&... args) {
  5032. auto *cpool = std::get<0>(pools);
  5033. if constexpr(sizeof...(Component) == 0) {
  5034. static_assert(std::is_invocable_v<Compare, const entity_type, const entity_type>, "Invalid comparison function");
  5035. cpool->sort(cpool->end()-*length, cpool->end(), std::move(compare), std::move(algo), std::forward<Args>(args)...);
  5036. } else if constexpr(sizeof...(Component) == 1) {
  5037. cpool->sort(cpool->end()-*length, cpool->end(), [this, compare = std::move(compare)](const entity_type lhs, const entity_type rhs) {
  5038. return compare((std::get<pool_type<Component> *>(pools)->get(lhs), ...), (std::get<pool_type<Component> *>(pools)->get(rhs), ...));
  5039. }, std::move(algo), std::forward<Args>(args)...);
  5040. } else {
  5041. cpool->sort(cpool->end()-*length, cpool->end(), [this, compare = std::move(compare)](const entity_type lhs, const entity_type rhs) {
  5042. return compare(std::tuple<decltype(get<Component>({}))...>{std::get<pool_type<Component> *>(pools)->get(lhs)...}, std::tuple<decltype(get<Component>({}))...>{std::get<pool_type<Component> *>(pools)->get(rhs)...});
  5043. }, std::move(algo), std::forward<Args>(args)...);
  5044. }
  5045. [this](auto *head, auto *... other) {
  5046. for(auto next = *length; next; --next) {
  5047. const auto pos = next - 1;
  5048. [[maybe_unused]] const auto entt = head->data()[pos];
  5049. (other->swap(other->data()[pos], entt), ...);
  5050. }
  5051. }(std::get<pool_type<Owned> *>(pools)...);
  5052. }
  5053. private:
  5054. const std::tuple<pool_type<Owned> *..., pool_type<Get> *...> pools;
  5055. const size_type *length;
  5056. };
  5057. }
  5058. #endif
  5059. // #include "runtime_view.hpp"
  5060. #ifndef ENTT_ENTITY_RUNTIME_VIEW_HPP
  5061. #define ENTT_ENTITY_RUNTIME_VIEW_HPP
  5062. #include <iterator>
  5063. #include <vector>
  5064. #include <utility>
  5065. #include <algorithm>
  5066. #include <type_traits>
  5067. // #include "../config/config.h"
  5068. // #include "sparse_set.hpp"
  5069. // #include "fwd.hpp"
  5070. namespace entt {
  5071. /**
  5072. * @brief Runtime view.
  5073. *
  5074. * Runtime views iterate over those entities that have at least all the given
  5075. * components in their bags. During initialization, a runtime view looks at the
  5076. * number of entities available for each component and picks up a reference to
  5077. * the smallest set of candidate entities in order to get a performance boost
  5078. * when iterate.<br/>
  5079. * Order of elements during iterations are highly dependent on the order of the
  5080. * underlying data structures. See sparse_set and its specializations for more
  5081. * details.
  5082. *
  5083. * @b Important
  5084. *
  5085. * Iterators aren't invalidated if:
  5086. *
  5087. * * New instances of the given components are created and assigned to entities.
  5088. * * The entity currently pointed is modified (as an example, if one of the
  5089. * given components is removed from the entity to which the iterator points).
  5090. * * The entity currently pointed is destroyed.
  5091. *
  5092. * In all the other cases, modifying the pools of the given components in any
  5093. * way invalidates all the iterators and using them results in undefined
  5094. * behavior.
  5095. *
  5096. * @note
  5097. * Views share references to the underlying data structures of the registry that
  5098. * generated them. Therefore any change to the entities and to the components
  5099. * made by means of the registry are immediately reflected by the views, unless
  5100. * a pool was missing when the view was built (in this case, the view won't
  5101. * have a valid reference and won't be updated accordingly).
  5102. *
  5103. * @warning
  5104. * Lifetime of a view must not overcome that of the registry that generated it.
  5105. * In any other case, attempting to use a view results in undefined behavior.
  5106. *
  5107. * @tparam Entity A valid entity type (see entt_traits for more details).
  5108. */
  5109. template<typename Entity>
  5110. class basic_runtime_view {
  5111. /*! @brief A registry is allowed to create views. */
  5112. friend class basic_registry<Entity>;
  5113. using underlying_iterator = typename sparse_set<Entity>::iterator;
  5114. class view_iterator final {
  5115. friend class basic_runtime_view<Entity>;
  5116. view_iterator(const std::vector<const sparse_set<Entity> *> &cpools, const std::vector<const sparse_set<Entity> *> &ignore, underlying_iterator curr) ENTT_NOEXCEPT
  5117. : pools{&cpools},
  5118. filter{&ignore},
  5119. it{curr}
  5120. {
  5121. if(it != (*pools)[0]->end() && !valid()) {
  5122. ++(*this);
  5123. }
  5124. }
  5125. [[nodiscard]] bool valid() const {
  5126. return std::all_of(pools->begin()++, pools->end(), [entt = *it](const auto *curr) { return curr->contains(entt); })
  5127. && std::none_of(filter->cbegin(), filter->cend(), [entt = *it](const auto *curr) { return curr && curr->contains(entt); });
  5128. }
  5129. public:
  5130. using difference_type = typename underlying_iterator::difference_type;
  5131. using value_type = typename underlying_iterator::value_type;
  5132. using pointer = typename underlying_iterator::pointer;
  5133. using reference = typename underlying_iterator::reference;
  5134. using iterator_category = std::bidirectional_iterator_tag;
  5135. view_iterator() ENTT_NOEXCEPT = default;
  5136. view_iterator & operator++() {
  5137. while(++it != (*pools)[0]->end() && !valid());
  5138. return *this;
  5139. }
  5140. view_iterator operator++(int) {
  5141. view_iterator orig = *this;
  5142. return ++(*this), orig;
  5143. }
  5144. view_iterator & operator--() ENTT_NOEXCEPT {
  5145. while(--it != (*pools)[0]->begin() && !valid());
  5146. return *this;
  5147. }
  5148. view_iterator operator--(int) ENTT_NOEXCEPT {
  5149. view_iterator orig = *this;
  5150. return operator--(), orig;
  5151. }
  5152. [[nodiscard]] bool operator==(const view_iterator &other) const ENTT_NOEXCEPT {
  5153. return other.it == it;
  5154. }
  5155. [[nodiscard]] bool operator!=(const view_iterator &other) const ENTT_NOEXCEPT {
  5156. return !(*this == other);
  5157. }
  5158. [[nodiscard]] pointer operator->() const {
  5159. return it.operator->();
  5160. }
  5161. [[nodiscard]] reference operator*() const {
  5162. return *operator->();
  5163. }
  5164. private:
  5165. const std::vector<const sparse_set<Entity> *> *pools;
  5166. const std::vector<const sparse_set<Entity> *> *filter;
  5167. underlying_iterator it;
  5168. };
  5169. basic_runtime_view(std::vector<const sparse_set<Entity> *> cpools, std::vector<const sparse_set<Entity> *> epools) ENTT_NOEXCEPT
  5170. : pools{std::move(cpools)},
  5171. filter{std::move(epools)}
  5172. {
  5173. const auto it = std::min_element(pools.begin(), pools.end(), [](const auto *lhs, const auto *rhs) {
  5174. return (!lhs && rhs) || (lhs && rhs && lhs->size() < rhs->size());
  5175. });
  5176. // brings the best candidate (if any) on front of the vector
  5177. std::rotate(pools.begin(), it, pools.end());
  5178. }
  5179. [[nodiscard]] bool valid() const {
  5180. return !pools.empty() && pools.front();
  5181. }
  5182. public:
  5183. /*! @brief Underlying entity identifier. */
  5184. using entity_type = Entity;
  5185. /*! @brief Unsigned integer type. */
  5186. using size_type = std::size_t;
  5187. /*! @brief Bidirectional iterator type. */
  5188. using iterator = view_iterator;
  5189. /**
  5190. * @brief Estimates the number of entities that have the given components.
  5191. * @return Estimated number of entities that have the given components.
  5192. */
  5193. [[nodiscard]] size_type size() const {
  5194. return valid() ? pools.front()->size() : size_type{};
  5195. }
  5196. /**
  5197. * @brief Checks if the view is definitely empty.
  5198. * @return True if the view is definitely empty, false otherwise.
  5199. */
  5200. [[nodiscard]] bool empty() const {
  5201. return !valid() || pools.front()->empty();
  5202. }
  5203. /**
  5204. * @brief Returns an iterator to the first entity that has the given
  5205. * components.
  5206. *
  5207. * The returned iterator points to the first entity that has the given
  5208. * components. If the view is empty, the returned iterator will be equal to
  5209. * `end()`.
  5210. *
  5211. * @note
  5212. * Iterators stay true to the order imposed to the underlying data
  5213. * structures.
  5214. *
  5215. * @return An iterator to the first entity that has the given components.
  5216. */
  5217. [[nodiscard]] iterator begin() const {
  5218. return valid() ? iterator{pools, filter, pools[0]->begin()} : iterator{};
  5219. }
  5220. /**
  5221. * @brief Returns an iterator that is past the last entity that has the
  5222. * given components.
  5223. *
  5224. * The returned iterator points to the entity following the last entity that
  5225. * has the given components. Attempting to dereference the returned iterator
  5226. * results in undefined behavior.
  5227. *
  5228. * @note
  5229. * Iterators stay true to the order imposed to the underlying data
  5230. * structures.
  5231. *
  5232. * @return An iterator to the entity following the last entity that has the
  5233. * given components.
  5234. */
  5235. [[nodiscard]] iterator end() const {
  5236. return valid() ? iterator{pools, filter, pools[0]->end()} : iterator{};
  5237. }
  5238. /**
  5239. * @brief Checks if a view contains an entity.
  5240. * @param entt A valid entity identifier.
  5241. * @return True if the view contains the given entity, false otherwise.
  5242. */
  5243. [[nodiscard]] bool contains(const entity_type entt) const {
  5244. return valid() && std::all_of(pools.cbegin(), pools.cend(), [entt](const auto *curr) { return curr->contains(entt); })
  5245. && std::none_of(filter.cbegin(), filter.cend(), [entt](const auto *curr) { return curr && curr->contains(entt); });
  5246. }
  5247. /**
  5248. * @brief Iterates entities and applies the given function object to them.
  5249. *
  5250. * The function object is invoked for each entity. It is provided only with
  5251. * the entity itself. To get the components, users can use the registry with
  5252. * which the view was built.<br/>
  5253. * The signature of the function should be equivalent to the following:
  5254. *
  5255. * @code{.cpp}
  5256. * void(const entity_type);
  5257. * @endcode
  5258. *
  5259. * @tparam Func Type of the function object to invoke.
  5260. * @param func A valid function object.
  5261. */
  5262. template<typename Func>
  5263. void each(Func func) const {
  5264. for(const auto entity: *this) {
  5265. func(entity);
  5266. }
  5267. }
  5268. private:
  5269. std::vector<const sparse_set<Entity> *> pools;
  5270. std::vector<const sparse_set<Entity> *> filter;
  5271. };
  5272. }
  5273. #endif
  5274. // #include "sparse_set.hpp"
  5275. // #include "storage.hpp"
  5276. // #include "utility.hpp"
  5277. // #include "view.hpp"
  5278. #ifndef ENTT_ENTITY_VIEW_HPP
  5279. #define ENTT_ENTITY_VIEW_HPP
  5280. #include <iterator>
  5281. #include <array>
  5282. #include <tuple>
  5283. #include <utility>
  5284. #include <algorithm>
  5285. #include <type_traits>
  5286. // #include "../config/config.h"
  5287. // #include "../core/type_traits.hpp"
  5288. // #include "entity.hpp"
  5289. // #include "fwd.hpp"
  5290. // #include "pool.hpp"
  5291. // #include "sparse_set.hpp"
  5292. // #include "utility.hpp"
  5293. namespace entt {
  5294. /**
  5295. * @brief View.
  5296. *
  5297. * Primary template isn't defined on purpose. All the specializations give a
  5298. * compile-time error, but for a few reasonable cases.
  5299. */
  5300. template<typename...>
  5301. class basic_view;
  5302. /**
  5303. * @brief Multi component view.
  5304. *
  5305. * Multi component views iterate over those entities that have at least all the
  5306. * given components in their bags. During initialization, a multi component view
  5307. * looks at the number of entities available for each component and uses the
  5308. * smallest set in order to get a performance boost when iterate.
  5309. *
  5310. * @b Important
  5311. *
  5312. * Iterators aren't invalidated if:
  5313. *
  5314. * * New instances of the given components are created and assigned to entities.
  5315. * * The entity currently pointed is modified (as an example, if one of the
  5316. * given components is removed from the entity to which the iterator points).
  5317. * * The entity currently pointed is destroyed.
  5318. *
  5319. * In all other cases, modifying the pools iterated by the view in any way
  5320. * invalidates all the iterators and using them results in undefined behavior.
  5321. *
  5322. * @note
  5323. * Views share references to the underlying data structures of the registry that
  5324. * generated them. Therefore any change to the entities and to the components
  5325. * made by means of the registry are immediately reflected by views.
  5326. *
  5327. * @warning
  5328. * Lifetime of a view must not overcome that of the registry that generated it.
  5329. * In any other case, attempting to use a view results in undefined behavior.
  5330. *
  5331. * @tparam Entity A valid entity type (see entt_traits for more details).
  5332. * @tparam Exclude Types of components used to filter the view.
  5333. * @tparam Component Types of components iterated by the view.
  5334. */
  5335. template<typename Entity, typename... Exclude, typename... Component>
  5336. class basic_view<Entity, exclude_t<Exclude...>, Component...> {
  5337. /*! @brief A registry is allowed to create views. */
  5338. friend class basic_registry<Entity>;
  5339. template<typename Comp>
  5340. using pool_type = pool_t<Entity, Comp>;
  5341. template<typename Comp>
  5342. using component_iterator = decltype(std::declval<pool_type<Comp>>().begin());
  5343. using unchecked_type = std::array<const sparse_set<Entity> *, (sizeof...(Component) - 1)>;
  5344. using filter_type = std::array<const sparse_set<Entity> *, sizeof...(Exclude)>;
  5345. template<typename It>
  5346. class view_iterator final {
  5347. friend class basic_view<Entity, exclude_t<Exclude...>, Component...>;
  5348. view_iterator(It from, It to, It curr, unchecked_type other, filter_type ignore) ENTT_NOEXCEPT
  5349. : first{from},
  5350. last{to},
  5351. it{curr},
  5352. unchecked{other},
  5353. filter{ignore}
  5354. {
  5355. if(it != last && !valid()) {
  5356. ++(*this);
  5357. }
  5358. }
  5359. [[nodiscard]] bool valid() const {
  5360. return std::all_of(unchecked.cbegin(), unchecked.cend(), [entt = *it](const sparse_set<Entity> *curr) { return curr->contains(entt); })
  5361. && (sizeof...(Exclude) == 0 || std::none_of(filter.cbegin(), filter.cend(), [entt = *it](const sparse_set<Entity> *cpool) { return cpool->contains(entt); }));
  5362. }
  5363. public:
  5364. using difference_type = typename std::iterator_traits<It>::difference_type;
  5365. using value_type = typename std::iterator_traits<It>::value_type;
  5366. using pointer = typename std::iterator_traits<It>::pointer;
  5367. using reference = typename std::iterator_traits<It>::reference;
  5368. using iterator_category = std::bidirectional_iterator_tag;
  5369. view_iterator() ENTT_NOEXCEPT = default;
  5370. view_iterator & operator++() {
  5371. while(++it != last && !valid());
  5372. return *this;
  5373. }
  5374. view_iterator operator++(int) {
  5375. view_iterator orig = *this;
  5376. return ++(*this), orig;
  5377. }
  5378. view_iterator & operator--() ENTT_NOEXCEPT {
  5379. while(--it != first && !valid());
  5380. return *this;
  5381. }
  5382. view_iterator operator--(int) ENTT_NOEXCEPT {
  5383. view_iterator orig = *this;
  5384. return operator--(), orig;
  5385. }
  5386. [[nodiscard]] bool operator==(const view_iterator &other) const ENTT_NOEXCEPT {
  5387. return other.it == it;
  5388. }
  5389. [[nodiscard]] bool operator!=(const view_iterator &other) const ENTT_NOEXCEPT {
  5390. return !(*this == other);
  5391. }
  5392. [[nodiscard]] pointer operator->() const {
  5393. return &*it;
  5394. }
  5395. [[nodiscard]] reference operator*() const {
  5396. return *operator->();
  5397. }
  5398. private:
  5399. It first;
  5400. It last;
  5401. It it;
  5402. unchecked_type unchecked;
  5403. filter_type filter;
  5404. };
  5405. class view_proxy {
  5406. friend class basic_view<Entity, exclude_t<Exclude...>, Component...>;
  5407. using proxy_view_iterator = view_iterator<typename sparse_set<Entity>::iterator>;
  5408. class proxy_iterator {
  5409. friend class view_proxy;
  5410. using ref_type = decltype(std::tuple_cat(std::declval<std::conditional_t<is_eto_eligible_v<Component>, std::tuple<>, std::tuple<pool_type<Component> *>>>()...));
  5411. proxy_iterator(proxy_view_iterator from, ref_type ref) ENTT_NOEXCEPT
  5412. : it{from},
  5413. pools{ref}
  5414. {}
  5415. public:
  5416. using difference_type = std::ptrdiff_t;
  5417. using value_type = decltype(std::tuple_cat(
  5418. std::declval<std::tuple<Entity>>(),
  5419. std::declval<std::conditional_t<is_eto_eligible_v<Component>, std::tuple<>, std::tuple<Component>>>()...
  5420. ));
  5421. using pointer = void;
  5422. using reference = decltype(std::tuple_cat(
  5423. std::declval<std::tuple<Entity>>(),
  5424. std::declval<std::conditional_t<is_eto_eligible_v<Component>, std::tuple<>, std::tuple<Component &>>>()...
  5425. ));
  5426. using iterator_category = std::input_iterator_tag;
  5427. proxy_iterator & operator++() ENTT_NOEXCEPT {
  5428. return ++it, *this;
  5429. }
  5430. proxy_iterator operator++(int) ENTT_NOEXCEPT {
  5431. proxy_iterator orig = *this;
  5432. return ++(*this), orig;
  5433. }
  5434. [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
  5435. return std::apply([entt = *it](auto *... cpool) { return reference{entt, cpool->get(entt)...}; }, pools);
  5436. }
  5437. [[nodiscard]] bool operator==(const proxy_iterator &other) const ENTT_NOEXCEPT {
  5438. return other.it == it;
  5439. }
  5440. [[nodiscard]] bool operator!=(const proxy_iterator &other) const ENTT_NOEXCEPT {
  5441. return !(*this == other);
  5442. }
  5443. private:
  5444. proxy_view_iterator it{};
  5445. const ref_type pools{};
  5446. };
  5447. view_proxy(proxy_view_iterator from, proxy_view_iterator to, std::tuple<pool_type<Component> *...> ref)
  5448. : first{from},
  5449. last{to},
  5450. pools{ref}
  5451. {}
  5452. public:
  5453. using iterator = proxy_iterator;
  5454. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  5455. return proxy_iterator{first, std::tuple_cat([](auto *cpool) {
  5456. if constexpr(is_eto_eligible_v<typename std::decay_t<decltype(*cpool)>::object_type>) {
  5457. return std::make_tuple();
  5458. } else {
  5459. return std::make_tuple(cpool);
  5460. }
  5461. }(std::get<pool_type<Component> *>(pools))...)};
  5462. }
  5463. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  5464. return proxy_iterator{last, std::tuple_cat([](auto *cpool) {
  5465. if constexpr(is_eto_eligible_v<typename std::decay_t<decltype(*cpool)>::object_type>) {
  5466. return std::make_tuple();
  5467. } else {
  5468. return std::make_tuple(cpool);
  5469. }
  5470. }(std::get<pool_type<Component> *>(pools))...)};
  5471. }
  5472. private:
  5473. proxy_view_iterator first;
  5474. proxy_view_iterator last;
  5475. const std::tuple<pool_type<Component> *...> pools;
  5476. };
  5477. basic_view(pool_type<Component> &... component, unpack_as_t<const sparse_set<Entity>, Exclude> &... epool) ENTT_NOEXCEPT
  5478. : pools{&component...},
  5479. view{candidate()},
  5480. filter{&epool...}
  5481. {}
  5482. [[nodiscard]] const sparse_set<Entity> * candidate() const ENTT_NOEXCEPT {
  5483. return (std::min)({ static_cast<const sparse_set<Entity> *>(std::get<pool_type<Component> *>(pools))... }, [](const auto *lhs, const auto *rhs) {
  5484. return lhs->size() < rhs->size();
  5485. });
  5486. }
  5487. [[nodiscard]] unchecked_type unchecked(const sparse_set<Entity> *cpool) const {
  5488. std::size_t pos{};
  5489. unchecked_type other{};
  5490. ((std::get<pool_type<Component> *>(pools) == cpool ? nullptr : (other[pos] = std::get<pool_type<Component> *>(pools), other[pos++])), ...);
  5491. return other;
  5492. }
  5493. template<typename Comp, typename Other>
  5494. [[nodiscard]] decltype(auto) get([[maybe_unused]] component_iterator<Comp> &it, [[maybe_unused]] pool_type<Other> *cpool, [[maybe_unused]] const Entity entt) const {
  5495. if constexpr(std::is_same_v<Comp, Other>) {
  5496. return *it;
  5497. } else {
  5498. return cpool->get(entt);
  5499. }
  5500. }
  5501. template<typename Comp, typename Func, typename... Type>
  5502. void traverse(Func func, type_list<Type...>) const {
  5503. if constexpr(std::disjunction_v<std::is_same<Comp, Type>...>) {
  5504. auto it = std::get<pool_type<Comp> *>(pools)->begin();
  5505. for(const auto entt: static_cast<const sparse_set<entity_type> &>(*std::get<pool_type<Comp> *>(pools))) {
  5506. if(((std::is_same_v<Comp, Component> || std::get<pool_type<Component> *>(pools)->contains(entt)) && ...)
  5507. && (sizeof...(Exclude) == 0 || std::none_of(filter.cbegin(), filter.cend(), [entt](const sparse_set<Entity> *cpool) { return cpool->contains(entt); })))
  5508. {
  5509. if constexpr(std::is_invocable_v<Func, decltype(get<Type>({}))...>) {
  5510. func(get<Comp, Type>(it, std::get<pool_type<Type> *>(pools), entt)...);
  5511. } else {
  5512. func(entt, get<Comp, Type>(it, std::get<pool_type<Type> *>(pools), entt)...);
  5513. }
  5514. }
  5515. ++it;
  5516. }
  5517. } else {
  5518. for(const auto entt: static_cast<const sparse_set<entity_type> &>(*std::get<pool_type<Comp> *>(pools))) {
  5519. if(((std::is_same_v<Comp, Component> || std::get<pool_type<Component> *>(pools)->contains(entt)) && ...)
  5520. && (sizeof...(Exclude) == 0 || std::none_of(filter.cbegin(), filter.cend(), [entt](const sparse_set<Entity> *cpool) { return cpool->contains(entt); })))
  5521. {
  5522. if constexpr(std::is_invocable_v<Func, decltype(get<Type>({}))...>) {
  5523. func(std::get<pool_type<Type> *>(pools)->get(entt)...);
  5524. } else {
  5525. func(entt, std::get<pool_type<Type> *>(pools)->get(entt)...);
  5526. }
  5527. }
  5528. }
  5529. }
  5530. }
  5531. template<typename Func, typename... Type>
  5532. void iterate(Func func, type_list<Type...>) const {
  5533. const auto last = view->data() + view->size();
  5534. auto first = view->data();
  5535. while(first != last) {
  5536. if((std::get<pool_type<Component> *>(pools)->contains(*first) && ...)
  5537. && (sizeof...(Exclude) == 0 || std::none_of(filter.cbegin(), filter.cend(), [entt = *first](const sparse_set<Entity> *cpool) { return cpool->contains(entt); })))
  5538. {
  5539. const auto base = *(first++);
  5540. const auto chunk = (std::min)({ (std::get<pool_type<Component> *>(pools)->size() - std::get<pool_type<Component> *>(pools)->index(base))... });
  5541. size_type length{};
  5542. for(++length;
  5543. length < chunk
  5544. && ((*(std::get<pool_type<Component> *>(pools)->data() + std::get<pool_type<Component> *>(pools)->index(base) + length) == *first) && ...)
  5545. && (sizeof...(Exclude) == 0 || std::none_of(filter.cbegin(), filter.cend(), [entt = *first](const sparse_set<Entity> *cpool) { return cpool->contains(entt); }));
  5546. ++length, ++first);
  5547. func(view->data() + view->index(base), (std::get<pool_type<Type> *>(pools)->raw() + std::get<pool_type<Type> *>(pools)->index(base))..., length);
  5548. } else {
  5549. ++first;
  5550. }
  5551. }
  5552. }
  5553. public:
  5554. /*! @brief Underlying entity identifier. */
  5555. using entity_type = Entity;
  5556. /*! @brief Unsigned integer type. */
  5557. using size_type = std::size_t;
  5558. /*! @brief Bidirectional iterator type. */
  5559. using iterator = view_iterator<typename sparse_set<entity_type>::iterator>;
  5560. /*! @brief Reverse iterator type. */
  5561. using reverse_iterator = view_iterator<typename sparse_set<entity_type>::reverse_iterator>;
  5562. /**
  5563. * @brief Returns the number of existing components of the given type.
  5564. *
  5565. * This isn't the number of entities iterated by the view.
  5566. *
  5567. * @tparam Comp Type of component of which to return the size.
  5568. * @return Number of existing components of the given type.
  5569. */
  5570. template<typename Comp>
  5571. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  5572. return std::get<pool_type<Comp> *>(pools)->size();
  5573. }
  5574. /**
  5575. * @brief Estimates the number of entities iterated by the view.
  5576. * @return Estimated number of entities iterated by the view.
  5577. */
  5578. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  5579. return (std::min)({ std::get<pool_type<Component> *>(pools)->size()... });
  5580. }
  5581. /**
  5582. * @brief Checks whether a view or some pools are empty.
  5583. *
  5584. * The view is definitely empty if one of the pools it uses is empty. In all
  5585. * other cases, the view may be empty and not return entities even if this
  5586. * function returns false.
  5587. *
  5588. * @tparam Comp Types of components in which one is interested.
  5589. * @return True if the view or the pools are empty, false otherwise.
  5590. */
  5591. template<typename... Comp>
  5592. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  5593. if constexpr(sizeof...(Comp) == 0) {
  5594. return (std::get<pool_type<Component> *>(pools)->empty() || ...);
  5595. } else {
  5596. return (std::get<pool_type<Comp> *>(pools)->empty() && ...);
  5597. }
  5598. }
  5599. /**
  5600. * @brief Direct access to the list of components of a given pool.
  5601. *
  5602. * The returned pointer is such that range
  5603. * `[raw<Comp>(), raw<Comp>() + size<Comp>()]` is always a valid range, even
  5604. * if the container is empty.
  5605. *
  5606. * @note
  5607. * Components are in the reverse order as returned by the `begin`/`end`
  5608. * iterators.
  5609. *
  5610. * @tparam Comp Type of component in which one is interested.
  5611. * @return A pointer to the array of components.
  5612. */
  5613. template<typename Comp>
  5614. [[nodiscard]] Comp * raw() const ENTT_NOEXCEPT {
  5615. return std::get<pool_type<Comp> *>(pools)->raw();
  5616. }
  5617. /**
  5618. * @brief Direct access to the list of entities of a given pool.
  5619. *
  5620. * The returned pointer is such that range
  5621. * `[data<Comp>(), data<Comp>() + size<Comp>()]` is always a valid range,
  5622. * even if the container is empty.
  5623. *
  5624. * @note
  5625. * Entities are in the reverse order as returned by the `begin`/`end`
  5626. * iterators.
  5627. *
  5628. * @tparam Comp Type of component in which one is interested.
  5629. * @return A pointer to the array of entities.
  5630. */
  5631. template<typename Comp>
  5632. [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT {
  5633. return std::get<pool_type<Comp> *>(pools)->data();
  5634. }
  5635. /**
  5636. * @brief Returns an iterator to the first entity of the view.
  5637. *
  5638. * The returned iterator points to the first entity of the view. If the view
  5639. * is empty, the returned iterator will be equal to `end()`.
  5640. *
  5641. * @note
  5642. * Iterators stay true to the order imposed to the underlying data
  5643. * structures.
  5644. *
  5645. * @return An iterator to the first entity of the view.
  5646. */
  5647. [[nodiscard]] iterator begin() const {
  5648. return iterator{view->begin(), view->end(), view->begin(), unchecked(view), filter};
  5649. }
  5650. /**
  5651. * @brief Returns an iterator that is past the last entity of the view.
  5652. *
  5653. * The returned iterator points to the entity following the last entity of
  5654. * the view. Attempting to dereference the returned iterator results in
  5655. * undefined behavior.
  5656. *
  5657. * @note
  5658. * Iterators stay true to the order imposed to the underlying data
  5659. * structures.
  5660. *
  5661. * @return An iterator to the entity following the last entity of the view.
  5662. */
  5663. [[nodiscard]] iterator end() const {
  5664. return iterator{view->begin(), view->end(), view->end(), unchecked(view), filter};
  5665. }
  5666. /**
  5667. * @brief Returns an iterator to the first entity of the reversed view.
  5668. *
  5669. * The returned iterator points to the first entity of the reversed view. If
  5670. * the view is empty, the returned iterator will be equal to `rend()`.
  5671. *
  5672. * @note
  5673. * Iterators stay true to the order imposed to the underlying data
  5674. * structures.
  5675. *
  5676. * @return An iterator to the first entity of the reversed view.
  5677. */
  5678. [[nodiscard]] reverse_iterator rbegin() const {
  5679. return reverse_iterator{view->rbegin(), view->rend(), view->rbegin(), unchecked(view), filter};
  5680. }
  5681. /**
  5682. * @brief Returns an iterator that is past the last entity of the reversed
  5683. * view.
  5684. *
  5685. * The returned iterator points to the entity following the last entity of
  5686. * the reversed view. Attempting to dereference the returned iterator
  5687. * results in undefined behavior.
  5688. *
  5689. * @note
  5690. * Iterators stay true to the order imposed to the underlying data
  5691. * structures.
  5692. *
  5693. * @return An iterator to the entity following the last entity of the
  5694. * reversed view.
  5695. */
  5696. [[nodiscard]] reverse_iterator rend() const {
  5697. return reverse_iterator{view->rbegin(), view->rend(), view->rend(), unchecked(view), filter};
  5698. }
  5699. /**
  5700. * @brief Returns the first entity of the view, if any.
  5701. * @return The first entity of the view if one exists, the null entity
  5702. * otherwise.
  5703. */
  5704. [[nodiscard]] entity_type front() const {
  5705. const auto it = begin();
  5706. return it != end() ? *it : null;
  5707. }
  5708. /**
  5709. * @brief Returns the last entity of the view, if any.
  5710. * @return The last entity of the view if one exists, the null entity
  5711. * otherwise.
  5712. */
  5713. [[nodiscard]] entity_type back() const {
  5714. const auto it = rbegin();
  5715. return it != rend() ? *it : null;
  5716. }
  5717. /**
  5718. * @brief Finds an entity.
  5719. * @param entt A valid entity identifier.
  5720. * @return An iterator to the given entity if it's found, past the end
  5721. * iterator otherwise.
  5722. */
  5723. [[nodiscard]] iterator find(const entity_type entt) const {
  5724. iterator it{view->begin(), view->end(), view->find(entt), unchecked(view), filter};
  5725. return (it != end() && *it == entt) ? it : end();
  5726. }
  5727. /**
  5728. * @brief Checks if a view contains an entity.
  5729. * @param entt A valid entity identifier.
  5730. * @return True if the view contains the given entity, false otherwise.
  5731. */
  5732. [[nodiscard]] bool contains(const entity_type entt) const {
  5733. return (std::get<pool_type<Component> *>(pools)->contains(entt) && ...)
  5734. && (sizeof...(Exclude) == 0 || std::none_of(filter.begin(), filter.end(), [entt](const auto *cpool) { return cpool->contains(entt); }));
  5735. }
  5736. /**
  5737. * @brief Returns the components assigned to the given entity.
  5738. *
  5739. * Prefer this function instead of `registry::get` during iterations. It has
  5740. * far better performance than its counterpart.
  5741. *
  5742. * @warning
  5743. * Attempting to use an invalid component type results in a compilation
  5744. * error. Attempting to use an entity that doesn't belong to the view
  5745. * results in undefined behavior.<br/>
  5746. * An assertion will abort the execution at runtime in debug mode if the
  5747. * view doesn't contain the given entity.
  5748. *
  5749. * @tparam Comp Types of components to get.
  5750. * @param entt A valid entity identifier.
  5751. * @return The components assigned to the entity.
  5752. */
  5753. template<typename... Comp>
  5754. [[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entt) const {
  5755. ENTT_ASSERT(contains(entt));
  5756. if constexpr(sizeof...(Comp) == 0) {
  5757. static_assert(sizeof...(Component) == 1, "Invalid component type");
  5758. return (std::get<pool_type<Component> *>(pools)->get(entt), ...);
  5759. } else if constexpr(sizeof...(Comp) == 1) {
  5760. return (std::get<pool_type<Comp> *>(pools)->get(entt), ...);
  5761. } else {
  5762. return std::tuple<decltype(get<Comp>({}))...>{get<Comp>(entt)...};
  5763. }
  5764. }
  5765. /**
  5766. * @brief Iterates entities and components and applies the given function
  5767. * object to them.
  5768. *
  5769. * The function object is invoked for each entity. It is provided with the
  5770. * entity itself and a set of references to non-empty components. The
  5771. * _constness_ of the components is as requested.<br/>
  5772. * The signature of the function must be equivalent to one of the following
  5773. * forms:
  5774. *
  5775. * @code{.cpp}
  5776. * void(const entity_type, Type &...);
  5777. * void(Type &...);
  5778. * @endcode
  5779. *
  5780. * @note
  5781. * Empty types aren't explicitly instantiated and therefore they are never
  5782. * returned during iterations.
  5783. *
  5784. * @tparam Func Type of the function object to invoke.
  5785. * @param func A valid function object.
  5786. */
  5787. template<typename Func>
  5788. void each(Func func) const {
  5789. view = candidate();
  5790. ((std::get<pool_type<Component> *>(pools) == view ? each<Component>(std::move(func)) : void()), ...);
  5791. }
  5792. /**
  5793. * @brief Iterates entities and components and applies the given function
  5794. * object to them.
  5795. *
  5796. * The pool of the suggested component is used to lead the iterations. The
  5797. * returned entities will therefore respect the order of the pool associated
  5798. * with that type.<br/>
  5799. * It is no longer guaranteed that the performance is the best possible, but
  5800. * there will be greater control over the order of iteration.
  5801. *
  5802. * @sa each
  5803. *
  5804. * @tparam Comp Type of component to use to enforce the iteration order.
  5805. * @tparam Func Type of the function object to invoke.
  5806. * @param func A valid function object.
  5807. */
  5808. template<typename Comp, typename Func>
  5809. void each(Func func) const {
  5810. using non_empty_type = type_list_cat_t<std::conditional_t<is_eto_eligible_v<Component>, type_list<>, type_list<Component>>...>;
  5811. traverse<Comp>(std::move(func), non_empty_type{});
  5812. }
  5813. /**
  5814. * @brief Returns an iterable object to use to _visit_ the view.
  5815. *
  5816. * The iterable object returns tuples that contain the current entity and a
  5817. * set of references to its non-empty components. The _constness_ of the
  5818. * components is as requested.
  5819. *
  5820. * @note
  5821. * Empty types aren't explicitly instantiated and therefore they are never
  5822. * returned during iterations.
  5823. *
  5824. * @return An iterable object to use to _visit_ the view.
  5825. */
  5826. [[nodiscard]] auto proxy() const ENTT_NOEXCEPT {
  5827. view = candidate();
  5828. return view_proxy{begin(), end(), pools};
  5829. }
  5830. /**
  5831. * @brief Returns an iterable object to use to _visit_ the view.
  5832. *
  5833. * The pool of the suggested component is used to lead the iterations. The
  5834. * returned elements will therefore respect the order of the pool associated
  5835. * with that type.<br/>
  5836. * It is no longer guaranteed that the performance is the best possible, but
  5837. * there will be greater control over the order of iteration.
  5838. *
  5839. * @sa each
  5840. *
  5841. * @tparam Comp Type of component to use to enforce the iteration order.
  5842. * @return An iterable object to use to _visit_ the view.
  5843. */
  5844. template<typename Comp>
  5845. [[nodiscard]] auto proxy() const ENTT_NOEXCEPT {
  5846. const sparse_set<entity_type> *cpool = std::get<pool_type<Comp> *>(pools);
  5847. iterator first{cpool->begin(), cpool->end(), cpool->begin(), unchecked(cpool), filter};
  5848. iterator last{cpool->begin(), cpool->end(), cpool->end(), unchecked(cpool), filter};
  5849. return view_proxy{std::move(first), std::move(last), pools};
  5850. }
  5851. /**
  5852. * @brief Chunked iteration for entities and components
  5853. *
  5854. * Chunked iteration tries to spot chunks in the sets of entities and
  5855. * components and return them one at a time along with their sizes.<br/>
  5856. * This type of iteration is intended where it's known a priori that the
  5857. * creation of entities and components takes place in chunk, which is
  5858. * actually quite common. In this case, various optimizations can be applied
  5859. * downstream to obtain even better performances from the views.
  5860. *
  5861. * The signature of the function must be equivalent to the following:
  5862. *
  5863. * @code{.cpp}
  5864. * void(const entity_type *, Type *..., size_type);
  5865. * @endcode
  5866. *
  5867. * The arguments are as follows:
  5868. *
  5869. * * A pointer to the entities belonging to the chunk.
  5870. * * Pointers to the components associated with the returned entities.
  5871. * * The length of the chunk.
  5872. *
  5873. * Note that the callback can be invoked 0 or more times and no guarantee is
  5874. * given on the order of the elements.
  5875. *
  5876. * @note
  5877. * Empty types aren't explicitly instantiated and therefore they are never
  5878. * returned during iterations.
  5879. *
  5880. * @tparam Func Type of the function object to invoke.
  5881. * @param func A valid function object.
  5882. */
  5883. template<typename Func>
  5884. void chunked(Func func) const {
  5885. using non_empty_type = type_list_cat_t<std::conditional_t<is_eto_eligible_v<Component>, type_list<>, type_list<Component>>...>;
  5886. view = candidate();
  5887. iterate(std::move(func), non_empty_type{});
  5888. }
  5889. private:
  5890. const std::tuple<pool_type<Component> *...> pools;
  5891. mutable const sparse_set<entity_type>* view;
  5892. filter_type filter;
  5893. };
  5894. /**
  5895. * @brief Single component view specialization.
  5896. *
  5897. * Single component views are specialized in order to get a boost in terms of
  5898. * performance. This kind of views can access the underlying data structure
  5899. * directly and avoid superfluous checks.
  5900. *
  5901. * @b Important
  5902. *
  5903. * Iterators aren't invalidated if:
  5904. *
  5905. * * New instances of the given component are created and assigned to entities.
  5906. * * The entity currently pointed is modified (as an example, the given
  5907. * component is removed from the entity to which the iterator points).
  5908. * * The entity currently pointed is destroyed.
  5909. *
  5910. * In all other cases, modifying the pool iterated by the view in any way
  5911. * invalidates all the iterators and using them results in undefined behavior.
  5912. *
  5913. * @note
  5914. * Views share a reference to the underlying data structure of the registry that
  5915. * generated them. Therefore any change to the entities and to the components
  5916. * made by means of the registry are immediately reflected by views.
  5917. *
  5918. * @warning
  5919. * Lifetime of a view must not overcome that of the registry that generated it.
  5920. * In any other case, attempting to use a view results in undefined behavior.
  5921. *
  5922. * @tparam Entity A valid entity type (see entt_traits for more details).
  5923. * @tparam Component Type of component iterated by the view.
  5924. */
  5925. template<typename Entity, typename Component>
  5926. class basic_view<Entity, exclude_t<>, Component> {
  5927. /*! @brief A registry is allowed to create views. */
  5928. friend class basic_registry<Entity>;
  5929. using pool_type = pool_t<Entity, Component>;
  5930. class view_proxy {
  5931. friend class basic_view<Entity, exclude_t<>, Component>;
  5932. class proxy_iterator {
  5933. friend class view_proxy;
  5934. using it_type = std::conditional_t<
  5935. is_eto_eligible_v<Component>,
  5936. std::tuple<typename sparse_set<Entity>::iterator>,
  5937. std::tuple<typename sparse_set<Entity>::iterator, decltype(std::declval<pool_type>().begin())>
  5938. >;
  5939. proxy_iterator(it_type from) ENTT_NOEXCEPT
  5940. : it{from}
  5941. {}
  5942. public:
  5943. using difference_type = std::ptrdiff_t;
  5944. using value_type = std::conditional_t<is_eto_eligible_v<Component>, std::tuple<Entity>, std::tuple<Entity, Component>>;
  5945. using pointer = void;
  5946. using reference = std::conditional_t<is_eto_eligible_v<Component>, std::tuple<Entity>, std::tuple<Entity, Component &>>;
  5947. using iterator_category = std::input_iterator_tag;
  5948. proxy_iterator & operator++() ENTT_NOEXCEPT {
  5949. return std::apply([](auto &&... curr) { (++curr, ...); }, it), *this;
  5950. }
  5951. proxy_iterator operator++(int) ENTT_NOEXCEPT {
  5952. proxy_iterator orig = *this;
  5953. return ++(*this), orig;
  5954. }
  5955. [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
  5956. return std::apply([](auto &&... curr) { return reference{*curr...}; }, it);
  5957. }
  5958. [[nodiscard]] bool operator==(const proxy_iterator &other) const ENTT_NOEXCEPT {
  5959. return std::get<0>(other.it) == std::get<0>(it);
  5960. }
  5961. [[nodiscard]] bool operator!=(const proxy_iterator &other) const ENTT_NOEXCEPT {
  5962. return !(*this == other);
  5963. }
  5964. private:
  5965. it_type it{};
  5966. };
  5967. view_proxy(pool_type &ref)
  5968. : pool{&ref}
  5969. {}
  5970. public:
  5971. using iterator = proxy_iterator;
  5972. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  5973. if constexpr(is_eto_eligible_v<Component>) {
  5974. return proxy_iterator{std::make_tuple(pool->sparse_set<entity_type>::begin())};
  5975. } else {
  5976. return proxy_iterator{std::make_tuple(pool->sparse_set<entity_type>::begin(), pool->begin())};
  5977. }
  5978. }
  5979. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  5980. if constexpr(is_eto_eligible_v<Component>) {
  5981. return proxy_iterator{std::make_tuple(pool->sparse_set<entity_type>::end())};
  5982. } else {
  5983. return proxy_iterator{std::make_tuple(pool->sparse_set<entity_type>::end(), pool->end())};
  5984. }
  5985. }
  5986. private:
  5987. pool_type *pool;
  5988. };
  5989. basic_view(pool_type &ref) ENTT_NOEXCEPT
  5990. : pool{&ref}
  5991. {}
  5992. public:
  5993. /*! @brief Type of component iterated by the view. */
  5994. using raw_type = Component;
  5995. /*! @brief Underlying entity identifier. */
  5996. using entity_type = Entity;
  5997. /*! @brief Unsigned integer type. */
  5998. using size_type = std::size_t;
  5999. /*! @brief Random access iterator type. */
  6000. using iterator = typename sparse_set<Entity>::iterator;
  6001. /*! @brief Reversed iterator type. */
  6002. using reverse_iterator = typename sparse_set<Entity>::reverse_iterator;
  6003. /**
  6004. * @brief Returns the number of entities that have the given component.
  6005. * @return Number of entities that have the given component.
  6006. */
  6007. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  6008. return pool->size();
  6009. }
  6010. /**
  6011. * @brief Checks whether a view is empty.
  6012. * @return True if the view is empty, false otherwise.
  6013. */
  6014. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  6015. return pool->empty();
  6016. }
  6017. /**
  6018. * @brief Direct access to the list of components.
  6019. *
  6020. * The returned pointer is such that range `[raw(), raw() + size()]` is
  6021. * always a valid range, even if the container is empty.
  6022. *
  6023. * @note
  6024. * Components are in the reverse order as returned by the `begin`/`end`
  6025. * iterators.
  6026. *
  6027. * @return A pointer to the array of components.
  6028. */
  6029. [[nodiscard]] raw_type * raw() const ENTT_NOEXCEPT {
  6030. return pool->raw();
  6031. }
  6032. /**
  6033. * @brief Direct access to the list of entities.
  6034. *
  6035. * The returned pointer is such that range `[data(), data() + size()]` is
  6036. * always a valid range, even if the container is empty.
  6037. *
  6038. * @note
  6039. * Entities are in the reverse order as returned by the `begin`/`end`
  6040. * iterators.
  6041. *
  6042. * @return A pointer to the array of entities.
  6043. */
  6044. [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT {
  6045. return pool->data();
  6046. }
  6047. /**
  6048. * @brief Returns an iterator to the first entity of the view.
  6049. *
  6050. * The returned iterator points to the first entity of the view. If the view
  6051. * is empty, the returned iterator will be equal to `end()`.
  6052. *
  6053. * @note
  6054. * Iterators stay true to the order imposed to the underlying data
  6055. * structures.
  6056. *
  6057. * @return An iterator to the first entity of the view.
  6058. */
  6059. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  6060. return pool->sparse_set<Entity>::begin();
  6061. }
  6062. /**
  6063. * @brief Returns an iterator that is past the last entity of the view.
  6064. *
  6065. * The returned iterator points to the entity following the last entity of
  6066. * the view. Attempting to dereference the returned iterator results in
  6067. * undefined behavior.
  6068. *
  6069. * @note
  6070. * Iterators stay true to the order imposed to the underlying data
  6071. * structures.
  6072. *
  6073. * @return An iterator to the entity following the last entity of the view.
  6074. */
  6075. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  6076. return pool->sparse_set<Entity>::end();
  6077. }
  6078. /**
  6079. * @brief Returns an iterator to the first entity of the reversed view.
  6080. *
  6081. * The returned iterator points to the first entity of the reversed view. If
  6082. * the view is empty, the returned iterator will be equal to `rend()`.
  6083. *
  6084. * @note
  6085. * Iterators stay true to the order imposed to the underlying data
  6086. * structures.
  6087. *
  6088. * @return An iterator to the first entity of the reversed view.
  6089. */
  6090. [[nodiscard]] reverse_iterator rbegin() const ENTT_NOEXCEPT {
  6091. return pool->sparse_set<Entity>::rbegin();
  6092. }
  6093. /**
  6094. * @brief Returns an iterator that is past the last entity of the reversed
  6095. * view.
  6096. *
  6097. * The returned iterator points to the entity following the last entity of
  6098. * the reversed view. Attempting to dereference the returned iterator
  6099. * results in undefined behavior.
  6100. *
  6101. * @note
  6102. * Iterators stay true to the order imposed to the underlying data
  6103. * structures.
  6104. *
  6105. * @return An iterator to the entity following the last entity of the
  6106. * reversed view.
  6107. */
  6108. [[nodiscard]] reverse_iterator rend() const ENTT_NOEXCEPT {
  6109. return pool->sparse_set<Entity>::rend();
  6110. }
  6111. /**
  6112. * @brief Returns the first entity of the view, if any.
  6113. * @return The first entity of the view if one exists, the null entity
  6114. * otherwise.
  6115. */
  6116. [[nodiscard]] entity_type front() const {
  6117. const auto it = begin();
  6118. return it != end() ? *it : null;
  6119. }
  6120. /**
  6121. * @brief Returns the last entity of the view, if any.
  6122. * @return The last entity of the view if one exists, the null entity
  6123. * otherwise.
  6124. */
  6125. [[nodiscard]] entity_type back() const {
  6126. const auto it = rbegin();
  6127. return it != rend() ? *it : null;
  6128. }
  6129. /**
  6130. * @brief Finds an entity.
  6131. * @param entt A valid entity identifier.
  6132. * @return An iterator to the given entity if it's found, past the end
  6133. * iterator otherwise.
  6134. */
  6135. [[nodiscard]] iterator find(const entity_type entt) const {
  6136. const auto it = pool->find(entt);
  6137. return it != end() && *it == entt ? it : end();
  6138. }
  6139. /**
  6140. * @brief Returns the identifier that occupies the given position.
  6141. * @param pos Position of the element to return.
  6142. * @return The identifier that occupies the given position.
  6143. */
  6144. [[nodiscard]] entity_type operator[](const size_type pos) const {
  6145. return begin()[pos];
  6146. }
  6147. /**
  6148. * @brief Checks if a view contains an entity.
  6149. * @param entt A valid entity identifier.
  6150. * @return True if the view contains the given entity, false otherwise.
  6151. */
  6152. [[nodiscard]] bool contains(const entity_type entt) const {
  6153. return pool->contains(entt);
  6154. }
  6155. /**
  6156. * @brief Returns the component assigned to the given entity.
  6157. *
  6158. * Prefer this function instead of `registry::get` during iterations. It has
  6159. * far better performance than its counterpart.
  6160. *
  6161. * @warning
  6162. * Attempting to use an entity that doesn't belong to the view results in
  6163. * undefined behavior.<br/>
  6164. * An assertion will abort the execution at runtime in debug mode if the
  6165. * view doesn't contain the given entity.
  6166. *
  6167. * @param entt A valid entity identifier.
  6168. * @return The component assigned to the entity.
  6169. */
  6170. template<typename Comp = Component>
  6171. [[nodiscard]] decltype(auto) get(const entity_type entt) const {
  6172. static_assert(std::is_same_v<Comp, Component>, "Invalid component type");
  6173. ENTT_ASSERT(contains(entt));
  6174. return pool->get(entt);
  6175. }
  6176. /**
  6177. * @brief Iterates entities and components and applies the given function
  6178. * object to them.
  6179. *
  6180. * The function object is invoked for each entity. It is provided with the
  6181. * entity itself and a reference to the component if it's a non-empty one.
  6182. * The _constness_ of the component is as requested.<br/>
  6183. * The signature of the function must be equivalent to one of the following
  6184. * forms:
  6185. *
  6186. * @code{.cpp}
  6187. * void(const entity_type, Component &);
  6188. * void(Component &);
  6189. * @endcode
  6190. *
  6191. * @note
  6192. * Empty types aren't explicitly instantiated and therefore they are never
  6193. * returned during iterations.
  6194. *
  6195. * @tparam Func Type of the function object to invoke.
  6196. * @param func A valid function object.
  6197. */
  6198. template<typename Func>
  6199. void each(Func func) const {
  6200. if constexpr(is_eto_eligible_v<Component>) {
  6201. if constexpr(std::is_invocable_v<Func>) {
  6202. for(auto pos = pool->size(); pos; --pos) {
  6203. func();
  6204. }
  6205. } else {
  6206. for(const auto entt: *this) {
  6207. func(entt);
  6208. }
  6209. }
  6210. } else {
  6211. if constexpr(std::is_invocable_v<Func, decltype(get({}))>) {
  6212. for(auto &&component: *pool) {
  6213. func(component);
  6214. }
  6215. } else {
  6216. auto raw = pool->begin();
  6217. for(const auto entt: *this) {
  6218. func(entt, *(raw++));
  6219. }
  6220. }
  6221. }
  6222. }
  6223. /**
  6224. * @brief Returns an iterable object to use to _visit_ the view.
  6225. *
  6226. * The iterable object returns tuples that contain the current entity and a
  6227. * reference to its component if it's a non-empty one. The _constness_ of
  6228. * the component is as requested.
  6229. *
  6230. * @note
  6231. * Empty types aren't explicitly instantiated and therefore they are never
  6232. * returned during iterations.
  6233. *
  6234. * @return An iterable object to use to _visit_ the view.
  6235. */
  6236. [[nodiscard]] auto proxy() const ENTT_NOEXCEPT {
  6237. return view_proxy{*pool};
  6238. }
  6239. private:
  6240. pool_type *pool;
  6241. };
  6242. }
  6243. #endif
  6244. namespace entt {
  6245. /**
  6246. * @brief Fast and reliable entity-component system.
  6247. *
  6248. * The registry is the core class of the entity-component framework.<br/>
  6249. * It stores entities and arranges pools of components on a per request basis.
  6250. * By means of a registry, users can manage entities and components, then create
  6251. * views or groups to iterate them.
  6252. *
  6253. * @tparam Entity A valid entity type (see entt_traits for more details).
  6254. */
  6255. template<typename Entity>
  6256. class basic_registry {
  6257. using traits_type = entt_traits<Entity>;
  6258. template<typename Component>
  6259. struct pool_handler final: storage<Entity, Component> {
  6260. static_assert(std::is_same_v<Component, std::decay_t<Component>>, "Invalid component type");
  6261. [[nodiscard]] auto on_construct() ENTT_NOEXCEPT {
  6262. return sink{construction};
  6263. }
  6264. [[nodiscard]] auto on_update() ENTT_NOEXCEPT {
  6265. return sink{update};
  6266. }
  6267. [[nodiscard]] auto on_destroy() ENTT_NOEXCEPT {
  6268. return sink{destruction};
  6269. }
  6270. template<typename... Args>
  6271. decltype(auto) emplace(basic_registry &owner, const Entity entt, Args &&... args) {
  6272. storage<entity_type, Component>::emplace(entt, std::forward<Args>(args)...);
  6273. construction.publish(owner, entt);
  6274. if constexpr(!is_eto_eligible_v<Component>) {
  6275. return this->get(entt);
  6276. }
  6277. }
  6278. template<typename It, typename... Args>
  6279. void insert(basic_registry &owner, It first, It last, Args &&... args) {
  6280. storage<entity_type, Component>::insert(first, last, std::forward<Args>(args)...);
  6281. if(!construction.empty()) {
  6282. while(first != last) { construction.publish(owner, *(first++)); }
  6283. }
  6284. }
  6285. void remove(basic_registry &owner, const Entity entt) {
  6286. destruction.publish(owner, entt);
  6287. this->erase(entt);
  6288. }
  6289. template<typename It>
  6290. void remove(basic_registry &owner, It first, It last) {
  6291. if(std::distance(first, last) == std::distance(this->begin(), this->end())) {
  6292. if(!destruction.empty()) {
  6293. while(first != last) { destruction.publish(owner, *(first++)); }
  6294. }
  6295. this->clear();
  6296. } else {
  6297. while(first != last) { this->remove(owner, *(first++)); }
  6298. }
  6299. }
  6300. template<typename... Func>
  6301. decltype(auto) patch(basic_registry &owner, const Entity entt, [[maybe_unused]] Func &&... func) {
  6302. if constexpr(is_eto_eligible_v<Component>) {
  6303. update.publish(owner, entt);
  6304. } else {
  6305. (std::forward<Func>(func)(this->get(entt)), ...);
  6306. update.publish(owner, entt);
  6307. return this->get(entt);
  6308. }
  6309. }
  6310. decltype(auto) replace(basic_registry &owner, const Entity entt, Component component) {
  6311. return patch(owner, entt, [&component](auto &&curr) { curr = std::move(component); });
  6312. }
  6313. private:
  6314. sigh<void(basic_registry &, const Entity)> construction{};
  6315. sigh<void(basic_registry &, const Entity)> destruction{};
  6316. sigh<void(basic_registry &, const Entity)> update{};
  6317. };
  6318. struct pool_data {
  6319. id_type type_id{};
  6320. std::unique_ptr<sparse_set<Entity>> pool{};
  6321. void(* remove)(sparse_set<Entity> &, basic_registry &, const Entity){};
  6322. };
  6323. template<typename...>
  6324. struct group_handler;
  6325. template<typename... Exclude, typename... Get, typename... Owned>
  6326. struct group_handler<exclude_t<Exclude...>, get_t<Get...>, Owned...> {
  6327. static_assert(std::conjunction_v<std::is_same<Owned, std::decay_t<Owned>>..., std::is_same<Get, std::decay_t<Get>>..., std::is_same<Exclude, std::decay_t<Exclude>>...>, "One or more component types are invalid");
  6328. std::conditional_t<sizeof...(Owned) == 0, sparse_set<Entity>, std::size_t> current{};
  6329. template<typename Component>
  6330. void maybe_valid_if(basic_registry &owner, const Entity entt) {
  6331. [[maybe_unused]] const auto cpools = std::forward_as_tuple(owner.assure<Owned>()...);
  6332. const auto is_valid = ((std::is_same_v<Component, Owned> || std::get<pool_handler<Owned> &>(cpools).contains(entt)) && ...)
  6333. && ((std::is_same_v<Component, Get> || owner.assure<Get>().contains(entt)) && ...)
  6334. && ((std::is_same_v<Component, Exclude> || !owner.assure<Exclude>().contains(entt)) && ...);
  6335. if constexpr(sizeof...(Owned) == 0) {
  6336. if(is_valid && !current.contains(entt)) {
  6337. current.emplace(entt);
  6338. }
  6339. } else {
  6340. if(is_valid && !(std::get<0>(cpools).index(entt) < current)) {
  6341. const auto pos = current++;
  6342. (std::get<pool_handler<Owned> &>(cpools).swap(std::get<pool_handler<Owned> &>(cpools).data()[pos], entt), ...);
  6343. }
  6344. }
  6345. }
  6346. void discard_if([[maybe_unused]] basic_registry &owner, const Entity entt) {
  6347. if constexpr(sizeof...(Owned) == 0) {
  6348. if(current.contains(entt)) {
  6349. current.erase(entt);
  6350. }
  6351. } else {
  6352. if(const auto cpools = std::forward_as_tuple(owner.assure<Owned>()...); std::get<0>(cpools).contains(entt) && (std::get<0>(cpools).index(entt) < current)) {
  6353. const auto pos = --current;
  6354. (std::get<pool_handler<Owned> &>(cpools).swap(std::get<pool_handler<Owned> &>(cpools).data()[pos], entt), ...);
  6355. }
  6356. }
  6357. }
  6358. };
  6359. struct group_data {
  6360. std::size_t size;
  6361. std::unique_ptr<void, void(*)(void *)> group;
  6362. bool (* owned)(const id_type) ENTT_NOEXCEPT;
  6363. bool (* get)(const id_type) ENTT_NOEXCEPT;
  6364. bool (* exclude)(const id_type) ENTT_NOEXCEPT;
  6365. };
  6366. struct variable_data {
  6367. id_type type_id;
  6368. std::unique_ptr<void, void(*)(void *)> value;
  6369. };
  6370. template<typename Component>
  6371. [[nodiscard]] const pool_handler<Component> & assure() const {
  6372. const sparse_set<entity_type> *cpool;
  6373. if constexpr(ENTT_FAST_PATH(has_type_index_v<Component>)) {
  6374. const auto index = type_index<Component>::value();
  6375. if(!(index < pools.size())) {
  6376. pools.resize(size_type(index+1u));
  6377. }
  6378. if(auto &&pdata = pools[index]; !pdata.pool) {
  6379. pdata.type_id = type_info<Component>::id();
  6380. pdata.pool.reset(new pool_handler<Component>());
  6381. pdata.remove = [](sparse_set<entity_type> &target, basic_registry &owner, const entity_type entt) {
  6382. static_cast<pool_handler<Component> &>(target).remove(owner, entt);
  6383. };
  6384. }
  6385. cpool = pools[index].pool.get();
  6386. } else {
  6387. if(const auto it = std::find_if(pools.cbegin(), pools.cend(), [id = type_info<Component>::id()](const auto &pdata) { return id == pdata.type_id; }); it == pools.cend()) {
  6388. cpool = pools.emplace_back(pool_data{
  6389. type_info<Component>::id(),
  6390. std::unique_ptr<sparse_set<entity_type>>{new pool_handler<Component>()},
  6391. [](sparse_set<entity_type> &target, basic_registry &owner, const entity_type entt) {
  6392. static_cast<pool_handler<Component> &>(target).remove(owner, entt);
  6393. }
  6394. }).pool.get();
  6395. } else {
  6396. cpool = it->pool.get();
  6397. }
  6398. }
  6399. return *static_cast<const pool_handler<Component> *>(cpool);
  6400. }
  6401. template<typename Component>
  6402. [[nodiscard]] pool_handler<Component> & assure() {
  6403. return const_cast<pool_handler<Component> &>(std::as_const(*this).template assure<Component>());
  6404. }
  6405. public:
  6406. /*! @brief Underlying entity identifier. */
  6407. using entity_type = Entity;
  6408. /*! @brief Underlying version type. */
  6409. using version_type = typename traits_type::version_type;
  6410. /*! @brief Unsigned integer type. */
  6411. using size_type = std::size_t;
  6412. /*! @brief Default constructor. */
  6413. basic_registry() = default;
  6414. /*! @brief Default move constructor. */
  6415. basic_registry(basic_registry &&) = default;
  6416. /*! @brief Default move assignment operator. @return This registry. */
  6417. basic_registry & operator=(basic_registry &&) = default;
  6418. /**
  6419. * @brief Prepares a pool for the given type if required.
  6420. * @tparam Component Type of component for which to prepare a pool.
  6421. */
  6422. template<typename Component>
  6423. void prepare() {
  6424. // suppress the warning due to the [[nodiscard]] attribute
  6425. static_cast<void>(assure<Component>());
  6426. }
  6427. /**
  6428. * @brief Returns the number of existing components of the given type.
  6429. * @tparam Component Type of component of which to return the size.
  6430. * @return Number of existing components of the given type.
  6431. */
  6432. template<typename Component>
  6433. [[nodiscard]] size_type size() const {
  6434. return assure<Component>().size();
  6435. }
  6436. /**
  6437. * @brief Returns the number of entities created so far.
  6438. * @return Number of entities created so far.
  6439. */
  6440. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  6441. return entities.size();
  6442. }
  6443. /**
  6444. * @brief Returns the number of entities still in use.
  6445. * @return Number of entities still in use.
  6446. */
  6447. [[nodiscard]] size_type alive() const {
  6448. auto sz = entities.size();
  6449. auto curr = destroyed;
  6450. for(; curr != null; --sz) {
  6451. curr = entities[to_integral(curr) & traits_type::entity_mask];
  6452. }
  6453. return sz;
  6454. }
  6455. /**
  6456. * @brief Increases the capacity of the registry or of the pools for the
  6457. * given components.
  6458. *
  6459. * If no components are specified, the capacity of the registry is
  6460. * increased, that is the number of entities it contains. Otherwise the
  6461. * capacity of the pools for the given components is increased.<br/>
  6462. * In both cases, if the new capacity is greater than the current capacity,
  6463. * new storage is allocated, otherwise the method does nothing.
  6464. *
  6465. * @tparam Component Types of components for which to reserve storage.
  6466. * @param cap Desired capacity.
  6467. */
  6468. template<typename... Component>
  6469. void reserve(const size_type cap) {
  6470. if constexpr(sizeof...(Component) == 0) {
  6471. entities.reserve(cap);
  6472. } else {
  6473. (assure<Component>().reserve(cap), ...);
  6474. }
  6475. }
  6476. /**
  6477. * @brief Returns the capacity of the pool for the given component.
  6478. * @tparam Component Type of component in which one is interested.
  6479. * @return Capacity of the pool of the given component.
  6480. */
  6481. template<typename Component>
  6482. [[nodiscard]] size_type capacity() const {
  6483. return assure<Component>().capacity();
  6484. }
  6485. /**
  6486. * @brief Returns the number of entities that a registry has currently
  6487. * allocated space for.
  6488. * @return Capacity of the registry.
  6489. */
  6490. [[nodiscard]] size_type capacity() const ENTT_NOEXCEPT {
  6491. return entities.capacity();
  6492. }
  6493. /**
  6494. * @brief Requests the removal of unused capacity for the given components.
  6495. * @tparam Component Types of components for which to reclaim unused
  6496. * capacity.
  6497. */
  6498. template<typename... Component>
  6499. void shrink_to_fit() {
  6500. (assure<Component>().shrink_to_fit(), ...);
  6501. }
  6502. /**
  6503. * @brief Checks whether the registry or the pools of the given components
  6504. * are empty.
  6505. *
  6506. * A registry is considered empty when it doesn't contain entities that are
  6507. * still in use.
  6508. *
  6509. * @tparam Component Types of components in which one is interested.
  6510. * @return True if the registry or the pools of the given components are
  6511. * empty, false otherwise.
  6512. */
  6513. template<typename... Component>
  6514. [[nodiscard]] bool empty() const {
  6515. if constexpr(sizeof...(Component) == 0) {
  6516. return !alive();
  6517. } else {
  6518. return (assure<Component>().empty() && ...);
  6519. }
  6520. }
  6521. /**
  6522. * @brief Direct access to the list of components of a given pool.
  6523. *
  6524. * The returned pointer is such that range
  6525. * `[raw<Component>(), raw<Component>() + size<Component>()]` is always a
  6526. * valid range, even if the container is empty.
  6527. *
  6528. * Components are in the reverse order as imposed by the sorting
  6529. * functionalities.
  6530. *
  6531. * @note
  6532. * Empty components aren't explicitly instantiated. Therefore, this function
  6533. * isn't available for them. A compilation error will occur if invoked.
  6534. *
  6535. * @tparam Component Type of component in which one is interested.
  6536. * @return A pointer to the array of components of the given type.
  6537. */
  6538. template<typename Component>
  6539. [[nodiscard]] const Component * raw() const {
  6540. return assure<Component>().raw();
  6541. }
  6542. /*! @copydoc raw */
  6543. template<typename Component>
  6544. [[nodiscard]] Component * raw() {
  6545. return const_cast<Component *>(std::as_const(*this).template raw<Component>());
  6546. }
  6547. /**
  6548. * @brief Direct access to the list of entities of a given pool.
  6549. *
  6550. * The returned pointer is such that range
  6551. * `[data<Component>(), data<Component>() + size<Component>()]` is always a
  6552. * valid range, even if the container is empty.
  6553. *
  6554. * Entities are in the reverse order as imposed by the sorting
  6555. * functionalities.
  6556. *
  6557. * @tparam Component Type of component in which one is interested.
  6558. * @return A pointer to the array of entities.
  6559. */
  6560. template<typename Component>
  6561. [[nodiscard]] const entity_type * data() const {
  6562. return assure<Component>().data();
  6563. }
  6564. /**
  6565. * @brief Direct access to the list of entities of a registry.
  6566. *
  6567. * The returned pointer is such that range `[data(), data() + size()]` is
  6568. * always a valid range, even if the container is empty.
  6569. *
  6570. * @warning
  6571. * This list contains both valid and destroyed entities and isn't suitable
  6572. * for direct use.
  6573. *
  6574. * @return A pointer to the array of entities.
  6575. */
  6576. [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT {
  6577. return entities.data();
  6578. }
  6579. /**
  6580. * @brief Checks if an entity identifier refers to a valid entity.
  6581. * @param entity An entity identifier, either valid or not.
  6582. * @return True if the identifier is valid, false otherwise.
  6583. */
  6584. [[nodiscard]] bool valid(const entity_type entity) const {
  6585. const auto pos = size_type(to_integral(entity) & traits_type::entity_mask);
  6586. return (pos < entities.size() && entities[pos] == entity);
  6587. }
  6588. /**
  6589. * @brief Returns the entity identifier without the version.
  6590. * @param entity An entity identifier, either valid or not.
  6591. * @return The entity identifier without the version.
  6592. */
  6593. [[nodiscard]] static entity_type entity(const entity_type entity) ENTT_NOEXCEPT {
  6594. return entity_type{to_integral(entity) & traits_type::entity_mask};
  6595. }
  6596. /**
  6597. * @brief Returns the version stored along with an entity identifier.
  6598. * @param entity An entity identifier, either valid or not.
  6599. * @return The version stored along with the given entity identifier.
  6600. */
  6601. [[nodiscard]] static version_type version(const entity_type entity) ENTT_NOEXCEPT {
  6602. return version_type(to_integral(entity) >> traits_type::entity_shift);
  6603. }
  6604. /**
  6605. * @brief Returns the actual version for an entity identifier.
  6606. *
  6607. * @warning
  6608. * Attempting to use an entity that doesn't belong to the registry results
  6609. * in undefined behavior. An entity belongs to the registry even if it has
  6610. * been previously destroyed and/or recycled.<br/>
  6611. * An assertion will abort the execution at runtime in debug mode if the
  6612. * registry doesn't own the given entity.
  6613. *
  6614. * @param entity A valid entity identifier.
  6615. * @return Actual version for the given entity identifier.
  6616. */
  6617. [[nodiscard]] version_type current(const entity_type entity) const {
  6618. const auto pos = size_type(to_integral(entity) & traits_type::entity_mask);
  6619. ENTT_ASSERT(pos < entities.size());
  6620. return version_type(to_integral(entities[pos]) >> traits_type::entity_shift);
  6621. }
  6622. /**
  6623. * @brief Creates a new entity and returns it.
  6624. *
  6625. * There are two kinds of possible entity identifiers:
  6626. *
  6627. * * Newly created ones in case no entities have been previously destroyed.
  6628. * * Recycled ones with updated versions.
  6629. *
  6630. * @return A valid entity identifier.
  6631. */
  6632. entity_type create() {
  6633. entity_type entt;
  6634. if(destroyed == null) {
  6635. entt = entities.emplace_back(entity_type{static_cast<typename traits_type::entity_type>(entities.size())});
  6636. // traits_type::entity_mask is reserved to allow for null identifiers
  6637. ENTT_ASSERT(to_integral(entt) < traits_type::entity_mask);
  6638. } else {
  6639. const auto curr = to_integral(destroyed);
  6640. const auto version = to_integral(entities[curr]) & (traits_type::version_mask << traits_type::entity_shift);
  6641. destroyed = entity_type{to_integral(entities[curr]) & traits_type::entity_mask};
  6642. entt = entities[curr] = entity_type{curr | version};
  6643. }
  6644. return entt;
  6645. }
  6646. /**
  6647. * @brief Creates a new entity and returns it.
  6648. *
  6649. * @sa create
  6650. *
  6651. * If the requested entity isn't in use, the suggested identifier is created
  6652. * and returned. Otherwise, a new one will be generated for this purpose.
  6653. *
  6654. * @param hint A desired entity identifier.
  6655. * @return A valid entity identifier.
  6656. */
  6657. [[nodiscard]] entity_type create(const entity_type hint) {
  6658. ENTT_ASSERT(hint != null);
  6659. entity_type entt;
  6660. if(const auto req = (to_integral(hint) & traits_type::entity_mask); !(req < entities.size())) {
  6661. entities.reserve(req + 1);
  6662. for(auto pos = entities.size(); pos < req; ++pos) {
  6663. entities.emplace_back(destroyed);
  6664. destroyed = entity_type{static_cast<typename traits_type::entity_type>(pos)};
  6665. }
  6666. entt = entities.emplace_back(hint);
  6667. } else if(const auto curr = (to_integral(entities[req]) & traits_type::entity_mask); req == curr) {
  6668. entt = create();
  6669. } else {
  6670. auto *it = &destroyed;
  6671. for(; (to_integral(*it) & traits_type::entity_mask) != req; it = &entities[to_integral(*it) & traits_type::entity_mask]);
  6672. *it = entity_type{curr | (to_integral(*it) & (traits_type::version_mask << traits_type::entity_shift))};
  6673. entt = entities[req] = hint;
  6674. }
  6675. return entt;
  6676. }
  6677. /**
  6678. * @brief Assigns each element in a range an entity.
  6679. *
  6680. * @sa create
  6681. *
  6682. * @tparam It Type of forward iterator.
  6683. * @param first An iterator to the first element of the range to generate.
  6684. * @param last An iterator past the last element of the range to generate.
  6685. */
  6686. template<typename It>
  6687. void create(It first, It last) {
  6688. std::generate(first, last, [this]() { return create(); });
  6689. }
  6690. /**
  6691. * @brief Assigns entities to an empty registry.
  6692. *
  6693. * This function is intended for use in conjunction with `raw`.<br/>
  6694. * Don't try to inject ranges of randomly generated entities. There is no
  6695. * guarantee that a registry will continue to work properly in this case.
  6696. *
  6697. * @warning
  6698. * An assertion will abort the execution at runtime in debug mode if all
  6699. * pools aren't empty.
  6700. *
  6701. * @tparam It Type of input iterator.
  6702. * @param first An iterator to the first element of the range of entities.
  6703. * @param last An iterator past the last element of the range of entities.
  6704. */
  6705. template<typename It>
  6706. void assign(It first, It last) {
  6707. ENTT_ASSERT(std::all_of(pools.cbegin(), pools.cend(), [](auto &&pdata) { return !pdata.pool || pdata.pool->empty(); }));
  6708. entities.assign(first, last);
  6709. destroyed = null;
  6710. for(std::size_t pos{}, end = entities.size(); pos < end; ++pos) {
  6711. if((to_integral(entities[pos]) & traits_type::entity_mask) != pos) {
  6712. const auto version = to_integral(entities[pos]) & (traits_type::version_mask << traits_type::entity_shift);
  6713. entities[pos] = entity_type{to_integral(destroyed) | version};
  6714. destroyed = entity_type{static_cast<typename traits_type::entity_type>(pos)};
  6715. }
  6716. }
  6717. }
  6718. /**
  6719. * @brief Destroys an entity.
  6720. *
  6721. * When an entity is destroyed, its version is updated and the identifier
  6722. * can be recycled at any time.
  6723. *
  6724. * @sa remove_all
  6725. *
  6726. * @param entity A valid entity identifier.
  6727. */
  6728. void destroy(const entity_type entity) {
  6729. destroy(entity, version_type((to_integral(entity) >> traits_type::entity_shift) + 1));
  6730. }
  6731. /**
  6732. * @brief Destroys an entity.
  6733. *
  6734. * If the entity isn't already destroyed, the suggested version is used
  6735. * instead of the implicitly generated one.
  6736. *
  6737. * @sa remove_all
  6738. *
  6739. * @param entity A valid entity identifier.
  6740. * @param version A desired version upon destruction.
  6741. */
  6742. void destroy(const entity_type entity, const version_type version) {
  6743. remove_all(entity);
  6744. // lengthens the implicit list of destroyed entities
  6745. const auto entt = to_integral(entity) & traits_type::entity_mask;
  6746. entities[entt] = entity_type{to_integral(destroyed) | (typename traits_type::entity_type{version} << traits_type::entity_shift)};
  6747. destroyed = entity_type{entt};
  6748. }
  6749. /**
  6750. * @brief Destroys all the entities in a range.
  6751. *
  6752. * @sa destroy
  6753. *
  6754. * @tparam It Type of input iterator.
  6755. * @param first An iterator to the first element of the range of entities.
  6756. * @param last An iterator past the last element of the range of entities.
  6757. */
  6758. template<typename It>
  6759. void destroy(It first, It last) {
  6760. while(first != last) { destroy(*(first++)); }
  6761. }
  6762. /**
  6763. * @brief Assigns the given component to an entity.
  6764. *
  6765. * A new instance of the given component is created and initialized with the
  6766. * arguments provided (the component must have a proper constructor or be of
  6767. * aggregate type). Then the component is assigned to the given entity.
  6768. *
  6769. * @warning
  6770. * Attempting to use an invalid entity or to assign a component to an entity
  6771. * that already owns it results in undefined behavior.<br/>
  6772. * An assertion will abort the execution at runtime in debug mode in case of
  6773. * invalid entity or if the entity already owns an instance of the given
  6774. * component.
  6775. *
  6776. * @tparam Component Type of component to create.
  6777. * @tparam Args Types of arguments to use to construct the component.
  6778. * @param entity A valid entity identifier.
  6779. * @param args Parameters to use to initialize the component.
  6780. * @return A reference to the newly created component.
  6781. */
  6782. template<typename Component, typename... Args>
  6783. decltype(auto) emplace(const entity_type entity, Args &&... args) {
  6784. ENTT_ASSERT(valid(entity));
  6785. return assure<Component>().emplace(*this, entity, std::forward<Args>(args)...);
  6786. }
  6787. /**
  6788. * @brief Assigns each entity in a range the given component.
  6789. *
  6790. * @sa emplace
  6791. *
  6792. * @tparam Component Type of component to create.
  6793. * @tparam It Type of input iterator.
  6794. * @param first An iterator to the first element of the range of entities.
  6795. * @param last An iterator past the last element of the range of entities.
  6796. * @param value An instance of the component to assign.
  6797. */
  6798. template<typename Component, typename It>
  6799. void insert(It first, It last, const Component &value = {}) {
  6800. ENTT_ASSERT(std::all_of(first, last, [this](const auto entity) { return valid(entity); }));
  6801. assure<Component>().insert(*this, first, last, value);
  6802. }
  6803. /**
  6804. * @brief Assigns each entity in a range the given components.
  6805. *
  6806. * @sa emplace
  6807. *
  6808. * @tparam Component Type of component to create.
  6809. * @tparam EIt Type of input iterator.
  6810. * @tparam CIt Type of input iterator.
  6811. * @param first An iterator to the first element of the range of entities.
  6812. * @param last An iterator past the last element of the range of entities.
  6813. * @param from An iterator to the first element of the range of components.
  6814. * @param to An iterator past the last element of the range of components.
  6815. */
  6816. template<typename Component, typename EIt, typename CIt>
  6817. void insert(EIt first, EIt last, CIt from, CIt to) {
  6818. static_assert(std::is_constructible_v<Component, typename std::iterator_traits<CIt>::value_type>, "Invalid value type");
  6819. ENTT_ASSERT(std::all_of(first, last, [this](const auto entity) { return valid(entity); }));
  6820. assure<Component>().insert(*this, first, last, from, to);
  6821. }
  6822. /**
  6823. * @brief Assigns or replaces the given component for an entity.
  6824. *
  6825. * Equivalent to the following snippet (pseudocode):
  6826. *
  6827. * @code{.cpp}
  6828. * auto &component = registry.has<Component>(entity) ? registry.replace<Component>(entity, args...) : registry.emplace<Component>(entity, args...);
  6829. * @endcode
  6830. *
  6831. * Prefer this function anyway because it has slightly better performance.
  6832. *
  6833. * @warning
  6834. * Attempting to use an invalid entity results in undefined behavior.<br/>
  6835. * An assertion will abort the execution at runtime in debug mode in case of
  6836. * invalid entity.
  6837. *
  6838. * @tparam Component Type of component to assign or replace.
  6839. * @tparam Args Types of arguments to use to construct the component.
  6840. * @param entity A valid entity identifier.
  6841. * @param args Parameters to use to initialize the component.
  6842. * @return A reference to the newly created component.
  6843. */
  6844. template<typename Component, typename... Args>
  6845. decltype(auto) emplace_or_replace(const entity_type entity, Args &&... args) {
  6846. ENTT_ASSERT(valid(entity));
  6847. auto &cpool = assure<Component>();
  6848. return cpool.contains(entity)
  6849. ? cpool.replace(*this, entity, Component{std::forward<Args>(args)...})
  6850. : cpool.emplace(*this, entity, std::forward<Args>(args)...);
  6851. }
  6852. /**
  6853. * @brief Patches the given component for an entity.
  6854. *
  6855. * The signature of the functions should be equivalent to the following:
  6856. *
  6857. * @code{.cpp}
  6858. * void(Component &);
  6859. * @endcode
  6860. *
  6861. * @note
  6862. * Empty types aren't explicitly instantiated and therefore they are never
  6863. * returned. However, this function can be used to trigger an update signal
  6864. * for them.
  6865. *
  6866. * @warning
  6867. * Attempting to use an invalid entity or to patch a component of an entity
  6868. * that doesn't own it results in undefined behavior.<br/>
  6869. * An assertion will abort the execution at runtime in debug mode in case of
  6870. * invalid entity or if the entity doesn't own an instance of the given
  6871. * component.
  6872. *
  6873. * @tparam Component Type of component to patch.
  6874. * @tparam Func Types of the function objects to invoke.
  6875. * @param entity A valid entity identifier.
  6876. * @param func Valid function objects.
  6877. * @return A reference to the patched component.
  6878. */
  6879. template<typename Component, typename... Func>
  6880. decltype(auto) patch(const entity_type entity, Func &&... func) {
  6881. ENTT_ASSERT(valid(entity));
  6882. return assure<Component>().patch(*this, entity, std::forward<Func>(func)...);
  6883. }
  6884. /**
  6885. * @brief Replaces the given component for an entity.
  6886. *
  6887. * A new instance of the given component is created and initialized with the
  6888. * arguments provided (the component must have a proper constructor or be of
  6889. * aggregate type). Then the component is assigned to the given entity.
  6890. *
  6891. * @warning
  6892. * Attempting to use an invalid entity or to replace a component of an
  6893. * entity that doesn't own it results in undefined behavior.<br/>
  6894. * An assertion will abort the execution at runtime in debug mode in case of
  6895. * invalid entity or if the entity doesn't own an instance of the given
  6896. * component.
  6897. *
  6898. * @tparam Component Type of component to replace.
  6899. * @tparam Args Types of arguments to use to construct the component.
  6900. * @param entity A valid entity identifier.
  6901. * @param args Parameters to use to initialize the component.
  6902. * @return A reference to the component being replaced.
  6903. */
  6904. template<typename Component, typename... Args>
  6905. decltype(auto) replace(const entity_type entity, Args &&... args) {
  6906. return assure<Component>().replace(*this, entity, Component{std::forward<Args>(args)...});
  6907. }
  6908. /**
  6909. * @brief Removes the given components from an entity.
  6910. *
  6911. * @warning
  6912. * Attempting to use an invalid entity or to remove a component from an
  6913. * entity that doesn't own it results in undefined behavior.<br/>
  6914. * An assertion will abort the execution at runtime in debug mode in case of
  6915. * invalid entity or if the entity doesn't own an instance of the given
  6916. * component.
  6917. *
  6918. * @tparam Component Types of components to remove.
  6919. * @param entity A valid entity identifier.
  6920. */
  6921. template<typename... Component>
  6922. void remove(const entity_type entity) {
  6923. ENTT_ASSERT(valid(entity));
  6924. (assure<Component>().remove(*this, entity), ...);
  6925. }
  6926. /**
  6927. * @brief Removes the given components from all the entities in a range.
  6928. *
  6929. * @see remove
  6930. *
  6931. * @tparam Component Types of components to remove.
  6932. * @tparam It Type of input iterator.
  6933. * @param first An iterator to the first element of the range of entities.
  6934. * @param last An iterator past the last element of the range of entities.
  6935. */
  6936. template<typename... Component, typename It>
  6937. void remove(It first, It last) {
  6938. ENTT_ASSERT(std::all_of(first, last, [this](const auto entity) { return valid(entity); }));
  6939. (assure<Component>().remove(*this, first, last), ...);
  6940. }
  6941. /**
  6942. * @brief Removes the given components from an entity.
  6943. *
  6944. * Equivalent to the following snippet (pseudocode):
  6945. *
  6946. * @code{.cpp}
  6947. * if(registry.has<Component>(entity)) { registry.remove<Component>(entity) }
  6948. * @endcode
  6949. *
  6950. * Prefer this function anyway because it has slightly better performance.
  6951. *
  6952. * @warning
  6953. * Attempting to use an invalid entity results in undefined behavior.<br/>
  6954. * An assertion will abort the execution at runtime in debug mode in case of
  6955. * invalid entity.
  6956. *
  6957. * @tparam Component Types of components to remove.
  6958. * @param entity A valid entity identifier.
  6959. * @return The number of components actually removed.
  6960. */
  6961. template<typename... Component>
  6962. size_type remove_if_exists(const entity_type entity) {
  6963. ENTT_ASSERT(valid(entity));
  6964. return ([this, entity](auto &&cpool) {
  6965. return cpool.contains(entity) ? (cpool.remove(*this, entity), true) : false;
  6966. }(assure<Component>()) + ... + size_type{});
  6967. }
  6968. /**
  6969. * @brief Removes all the components from an entity and makes it orphaned.
  6970. *
  6971. * @warning
  6972. * In case there are listeners that observe the destruction of components
  6973. * and assign other components to the entity in their bodies, the result of
  6974. * invoking this function may not be as expected. In the worst case, it
  6975. * could lead to undefined behavior.
  6976. *
  6977. * @warning
  6978. * Attempting to use an invalid entity results in undefined behavior.<br/>
  6979. * An assertion will abort the execution at runtime in debug mode in case of
  6980. * invalid entity.
  6981. *
  6982. * @param entity A valid entity identifier.
  6983. */
  6984. void remove_all(const entity_type entity) {
  6985. ENTT_ASSERT(valid(entity));
  6986. for(auto pos = pools.size(); pos; --pos) {
  6987. if(auto &pdata = pools[pos-1]; pdata.pool && pdata.pool->contains(entity)) {
  6988. pdata.remove(*pdata.pool, *this, entity);
  6989. }
  6990. }
  6991. }
  6992. /**
  6993. * @brief Checks if an entity has all the given components.
  6994. *
  6995. * @warning
  6996. * Attempting to use an invalid entity results in undefined behavior.<br/>
  6997. * An assertion will abort the execution at runtime in debug mode in case of
  6998. * invalid entity.
  6999. *
  7000. * @tparam Component Components for which to perform the check.
  7001. * @param entity A valid entity identifier.
  7002. * @return True if the entity has all the components, false otherwise.
  7003. */
  7004. template<typename... Component>
  7005. [[nodiscard]] bool has(const entity_type entity) const {
  7006. ENTT_ASSERT(valid(entity));
  7007. return (assure<Component>().contains(entity) && ...);
  7008. }
  7009. /**
  7010. * @brief Checks if an entity has at least one of the given components.
  7011. *
  7012. * @warning
  7013. * Attempting to use an invalid entity results in undefined behavior.<br/>
  7014. * An assertion will abort the execution at runtime in debug mode in case of
  7015. * invalid entity.
  7016. *
  7017. * @tparam Component Components for which to perform the check.
  7018. * @param entity A valid entity identifier.
  7019. * @return True if the entity has at least one of the given components,
  7020. * false otherwise.
  7021. */
  7022. template<typename... Component>
  7023. [[nodiscard]] bool any(const entity_type entity) const {
  7024. ENTT_ASSERT(valid(entity));
  7025. return (assure<Component>().contains(entity) || ...);
  7026. }
  7027. /**
  7028. * @brief Returns references to the given components for an entity.
  7029. *
  7030. * @warning
  7031. * Attempting to use an invalid entity or to get a component from an entity
  7032. * that doesn't own it results in undefined behavior.<br/>
  7033. * An assertion will abort the execution at runtime in debug mode in case of
  7034. * invalid entity or if the entity doesn't own an instance of the given
  7035. * component.
  7036. *
  7037. * @tparam Component Types of components to get.
  7038. * @param entity A valid entity identifier.
  7039. * @return References to the components owned by the entity.
  7040. */
  7041. template<typename... Component>
  7042. [[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entity) const {
  7043. ENTT_ASSERT(valid(entity));
  7044. if constexpr(sizeof...(Component) == 1) {
  7045. return (assure<Component>().get(entity), ...);
  7046. } else {
  7047. return std::forward_as_tuple(get<Component>(entity)...);
  7048. }
  7049. }
  7050. /*! @copydoc get */
  7051. template<typename... Component>
  7052. [[nodiscard]] decltype(auto) get([[maybe_unused]] const entity_type entity) {
  7053. ENTT_ASSERT(valid(entity));
  7054. if constexpr(sizeof...(Component) == 1) {
  7055. return (assure<Component>().get(entity), ...);
  7056. } else {
  7057. return std::forward_as_tuple(get<Component>(entity)...);
  7058. }
  7059. }
  7060. /**
  7061. * @brief Returns a reference to the given component for an entity.
  7062. *
  7063. * In case the entity doesn't own the component, the parameters provided are
  7064. * used to construct it.<br/>
  7065. * Equivalent to the following snippet (pseudocode):
  7066. *
  7067. * @code{.cpp}
  7068. * auto &component = registry.has<Component>(entity) ? registry.get<Component>(entity) : registry.emplace<Component>(entity, args...);
  7069. * @endcode
  7070. *
  7071. * Prefer this function anyway because it has slightly better performance.
  7072. *
  7073. * @warning
  7074. * Attempting to use an invalid entity results in undefined behavior.<br/>
  7075. * An assertion will abort the execution at runtime in debug mode in case of
  7076. * invalid entity.
  7077. *
  7078. * @tparam Component Type of component to get.
  7079. * @tparam Args Types of arguments to use to construct the component.
  7080. * @param entity A valid entity identifier.
  7081. * @param args Parameters to use to initialize the component.
  7082. * @return Reference to the component owned by the entity.
  7083. */
  7084. template<typename Component, typename... Args>
  7085. [[nodiscard]] decltype(auto) get_or_emplace(const entity_type entity, Args &&... args) {
  7086. ENTT_ASSERT(valid(entity));
  7087. auto &cpool = assure<Component>();
  7088. return cpool.contains(entity) ? cpool.get(entity) : cpool.emplace(*this, entity, std::forward<Args>(args)...);
  7089. }
  7090. /**
  7091. * @brief Returns pointers to the given components for an entity.
  7092. *
  7093. * @warning
  7094. * Attempting to use an invalid entity results in undefined behavior.<br/>
  7095. * An assertion will abort the execution at runtime in debug mode in case of
  7096. * invalid entity.
  7097. *
  7098. * @tparam Component Types of components to get.
  7099. * @param entity A valid entity identifier.
  7100. * @return Pointers to the components owned by the entity.
  7101. */
  7102. template<typename... Component>
  7103. [[nodiscard]] auto try_get([[maybe_unused]] const entity_type entity) const {
  7104. ENTT_ASSERT(valid(entity));
  7105. if constexpr(sizeof...(Component) == 1) {
  7106. return (assure<Component>().try_get(entity), ...);
  7107. } else {
  7108. return std::make_tuple(try_get<Component>(entity)...);
  7109. }
  7110. }
  7111. /*! @copydoc try_get */
  7112. template<typename... Component>
  7113. [[nodiscard]] auto try_get([[maybe_unused]] const entity_type entity) {
  7114. ENTT_ASSERT(valid(entity));
  7115. if constexpr(sizeof...(Component) == 1) {
  7116. return (assure<Component>().try_get(entity), ...);
  7117. } else {
  7118. return std::make_tuple(try_get<Component>(entity)...);
  7119. }
  7120. }
  7121. /**
  7122. * @brief Clears a whole registry or the pools for the given components.
  7123. * @tparam Component Types of components to remove from their entities.
  7124. */
  7125. template<typename... Component>
  7126. void clear() {
  7127. if constexpr(sizeof...(Component) == 0) {
  7128. // useless this-> used to suppress a warning with clang
  7129. each([this](const auto entity) { this->destroy(entity); });
  7130. } else {
  7131. ([this](auto &&cpool) {
  7132. cpool.remove(*this, cpool.sparse_set<entity_type>::begin(), cpool.sparse_set<entity_type>::end());
  7133. }(assure<Component>()), ...);
  7134. }
  7135. }
  7136. /**
  7137. * @brief Iterates all the entities that are still in use.
  7138. *
  7139. * The function object is invoked for each entity that is still in use.<br/>
  7140. * The signature of the function should be equivalent to the following:
  7141. *
  7142. * @code{.cpp}
  7143. * void(const Entity);
  7144. * @endcode
  7145. *
  7146. * This function is fairly slow and should not be used frequently. However,
  7147. * it's useful for iterating all the entities still in use, regardless of
  7148. * their components.
  7149. *
  7150. * @tparam Func Type of the function object to invoke.
  7151. * @param func A valid function object.
  7152. */
  7153. template<typename Func>
  7154. void each(Func func) const {
  7155. if(destroyed == null) {
  7156. for(auto pos = entities.size(); pos; --pos) {
  7157. func(entities[pos-1]);
  7158. }
  7159. } else {
  7160. for(auto pos = entities.size(); pos; --pos) {
  7161. if(const auto entt = entities[pos - 1]; (to_integral(entt) & traits_type::entity_mask) == (pos - 1)) {
  7162. func(entt);
  7163. }
  7164. }
  7165. }
  7166. }
  7167. /**
  7168. * @brief Checks if an entity has components assigned.
  7169. * @param entity A valid entity identifier.
  7170. * @return True if the entity has no components assigned, false otherwise.
  7171. */
  7172. [[nodiscard]] bool orphan(const entity_type entity) const {
  7173. ENTT_ASSERT(valid(entity));
  7174. return std::none_of(pools.cbegin(), pools.cend(), [entity](auto &&pdata) { return pdata.pool && pdata.pool->contains(entity); });
  7175. }
  7176. /**
  7177. * @brief Iterates orphans and applies them the given function object.
  7178. *
  7179. * The function object is invoked for each entity that is still in use and
  7180. * has no components assigned.<br/>
  7181. * The signature of the function should be equivalent to the following:
  7182. *
  7183. * @code{.cpp}
  7184. * void(const Entity);
  7185. * @endcode
  7186. *
  7187. * This function can be very slow and should not be used frequently.
  7188. *
  7189. * @tparam Func Type of the function object to invoke.
  7190. * @param func A valid function object.
  7191. */
  7192. template<typename Func>
  7193. void orphans(Func func) const {
  7194. each([this, &func](const auto entity) {
  7195. if(orphan(entity)) {
  7196. func(entity);
  7197. }
  7198. });
  7199. }
  7200. /**
  7201. * @brief Returns a sink object for the given component.
  7202. *
  7203. * A sink is an opaque object used to connect listeners to components.<br/>
  7204. * The sink returned by this function can be used to receive notifications
  7205. * whenever a new instance of the given component is created and assigned to
  7206. * an entity.
  7207. *
  7208. * The function type for a listener is equivalent to:
  7209. *
  7210. * @code{.cpp}
  7211. * void(registry<Entity> &, Entity);
  7212. * @endcode
  7213. *
  7214. * Listeners are invoked **after** the component has been assigned to the
  7215. * entity.
  7216. *
  7217. * @sa sink
  7218. *
  7219. * @tparam Component Type of component of which to get the sink.
  7220. * @return A temporary sink object.
  7221. */
  7222. template<typename Component>
  7223. [[nodiscard]] auto on_construct() {
  7224. return assure<Component>().on_construct();
  7225. }
  7226. /**
  7227. * @brief Returns a sink object for the given component.
  7228. *
  7229. * A sink is an opaque object used to connect listeners to components.<br/>
  7230. * The sink returned by this function can be used to receive notifications
  7231. * whenever an instance of the given component is explicitly updated.
  7232. *
  7233. * The function type for a listener is equivalent to:
  7234. *
  7235. * @code{.cpp}
  7236. * void(registry<Entity> &, Entity);
  7237. * @endcode
  7238. *
  7239. * Listeners are invoked **after** the component has been updated.
  7240. *
  7241. * @sa sink
  7242. *
  7243. * @tparam Component Type of component of which to get the sink.
  7244. * @return A temporary sink object.
  7245. */
  7246. template<typename Component>
  7247. [[nodiscard]] auto on_update() {
  7248. return assure<Component>().on_update();
  7249. }
  7250. /**
  7251. * @brief Returns a sink object for the given component.
  7252. *
  7253. * A sink is an opaque object used to connect listeners to components.<br/>
  7254. * The sink returned by this function can be used to receive notifications
  7255. * whenever an instance of the given component is removed from an entity and
  7256. * thus destroyed.
  7257. *
  7258. * The function type for a listener is equivalent to:
  7259. *
  7260. * @code{.cpp}
  7261. * void(registry<Entity> &, Entity);
  7262. * @endcode
  7263. *
  7264. * Listeners are invoked **before** the component has been removed from the
  7265. * entity.
  7266. *
  7267. * @sa sink
  7268. *
  7269. * @tparam Component Type of component of which to get the sink.
  7270. * @return A temporary sink object.
  7271. */
  7272. template<typename Component>
  7273. [[nodiscard]] auto on_destroy() {
  7274. return assure<Component>().on_destroy();
  7275. }
  7276. /**
  7277. * @brief Returns a view for the given components.
  7278. *
  7279. * This kind of objects are created on the fly and share with the registry
  7280. * its internal data structures.<br/>
  7281. * Feel free to discard a view after the use. Creating and destroying a view
  7282. * is an incredibly cheap operation because they do not require any type of
  7283. * initialization.<br/>
  7284. * As a rule of thumb, storing a view should never be an option.
  7285. *
  7286. * Views do their best to iterate the smallest set of candidate entities.
  7287. * In particular:
  7288. *
  7289. * * Single component views are incredibly fast and iterate a packed array
  7290. * of entities, all of which has the given component.
  7291. * * Multi component views look at the number of entities available for each
  7292. * component and pick up a reference to the smallest set of candidates to
  7293. * test for the given components.
  7294. *
  7295. * Views in no way affect the functionalities of the registry nor those of
  7296. * the underlying pools.
  7297. *
  7298. * @note
  7299. * Multi component views are pretty fast. However their performance tend to
  7300. * degenerate when the number of components to iterate grows up and the most
  7301. * of the entities have all the given components.<br/>
  7302. * To get a performance boost, consider using a group instead.
  7303. *
  7304. * @tparam Component Type of components used to construct the view.
  7305. * @tparam Exclude Types of components used to filter the view.
  7306. * @return A newly created view.
  7307. */
  7308. template<typename... Component, typename... Exclude>
  7309. [[nodiscard]] basic_view<Entity, exclude_t<Exclude...>, Component...> view(exclude_t<Exclude...> = {}) const {
  7310. static_assert(sizeof...(Component) > 0, "Exclusion-only views are not supported");
  7311. return { assure<std::decay_t<Component>>()..., assure<Exclude>()... };
  7312. }
  7313. /*! @copydoc view */
  7314. template<typename... Component, typename... Exclude>
  7315. [[nodiscard]] basic_view<Entity, exclude_t<Exclude...>, Component...> view(exclude_t<Exclude...> = {}) {
  7316. static_assert(sizeof...(Component) > 0, "Exclusion-only views are not supported");
  7317. return { assure<std::decay_t<Component>>()..., assure<Exclude>()... };
  7318. }
  7319. /**
  7320. * @brief Returns a runtime view for the given components.
  7321. *
  7322. * This kind of objects are created on the fly and share with the registry
  7323. * its internal data structures.<br/>
  7324. * Users should throw away the view after use. Fortunately, creating and
  7325. * destroying a runtime view is an incredibly cheap operation because they
  7326. * do not require any type of initialization.<br/>
  7327. * As a rule of thumb, storing a view should never be an option.
  7328. *
  7329. * Runtime views are to be used when users want to construct a view from
  7330. * some external inputs and don't know at compile-time what are the required
  7331. * components.
  7332. *
  7333. * @tparam ItComp Type of input iterator for the components to use to
  7334. * construct the view.
  7335. * @tparam ItExcl Type of input iterator for the components to use to filter
  7336. * the view.
  7337. * @param first An iterator to the first element of the range of components
  7338. * to use to construct the view.
  7339. * @param last An iterator past the last element of the range of components
  7340. * to use to construct the view.
  7341. * @param from An iterator to the first element of the range of components
  7342. * to use to filter the view.
  7343. * @param to An iterator past the last element of the range of components to
  7344. * use to filter the view.
  7345. * @return A newly created runtime view.
  7346. */
  7347. template<typename ItComp, typename ItExcl = id_type *>
  7348. [[nodiscard]] basic_runtime_view<Entity> runtime_view(ItComp first, ItComp last, ItExcl from = {}, ItExcl to = {}) const {
  7349. std::vector<const sparse_set<Entity> *> component(std::distance(first, last));
  7350. std::vector<const sparse_set<Entity> *> filter(std::distance(from, to));
  7351. std::transform(first, last, component.begin(), [this](const auto ctype) {
  7352. const auto it = std::find_if(pools.cbegin(), pools.cend(), [ctype](auto &&pdata) { return pdata.pool && pdata.type_id == ctype; });
  7353. return it == pools.cend() ? nullptr : it->pool.get();
  7354. });
  7355. std::transform(from, to, filter.begin(), [this](const auto ctype) {
  7356. const auto it = std::find_if(pools.cbegin(), pools.cend(), [ctype](auto &&pdata) { return pdata.pool && pdata.type_id == ctype; });
  7357. return it == pools.cend() ? nullptr : it->pool.get();
  7358. });
  7359. return { std::move(component), std::move(filter) };
  7360. }
  7361. /**
  7362. * @brief Returns a group for the given components.
  7363. *
  7364. * This kind of objects are created on the fly and share with the registry
  7365. * its internal data structures.<br/>
  7366. * Feel free to discard a group after the use. Creating and destroying a
  7367. * group is an incredibly cheap operation because they do not require any
  7368. * type of initialization, but for the first time they are requested.<br/>
  7369. * As a rule of thumb, storing a group should never be an option.
  7370. *
  7371. * Groups support exclusion lists and can own types of components. The more
  7372. * types are owned by a group, the faster it is to iterate entities and
  7373. * components.<br/>
  7374. * However, groups also affect some features of the registry such as the
  7375. * creation and destruction of components, which will consequently be
  7376. * slightly slower (nothing that can be noticed in most cases).
  7377. *
  7378. * @note
  7379. * Pools of components that are owned by a group cannot be sorted anymore.
  7380. * The group takes the ownership of the pools and arrange components so as
  7381. * to iterate them as fast as possible.
  7382. *
  7383. * @tparam Owned Types of components owned by the group.
  7384. * @tparam Get Types of components observed by the group.
  7385. * @tparam Exclude Types of components used to filter the group.
  7386. * @return A newly created group.
  7387. */
  7388. template<typename... Owned, typename... Get, typename... Exclude>
  7389. [[nodiscard]] basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>, Owned...> group(get_t<Get...>, exclude_t<Exclude...> = {}) {
  7390. static_assert(sizeof...(Owned) + sizeof...(Get) > 0, "Exclusion-only views are not supported");
  7391. static_assert(sizeof...(Owned) + sizeof...(Get) + sizeof...(Exclude) > 1, "Single component groups are not allowed");
  7392. using handler_type = group_handler<exclude_t<Exclude...>, get_t<std::decay_t<Get>...>, std::decay_t<Owned>...>;
  7393. const auto cpools = std::forward_as_tuple(assure<std::decay_t<Owned>>()..., assure<std::decay_t<Get>>()...);
  7394. constexpr auto size = sizeof...(Owned) + sizeof...(Get) + sizeof...(Exclude);
  7395. handler_type *handler = nullptr;
  7396. if(auto it = std::find_if(groups.cbegin(), groups.cend(), [size](const auto &gdata) {
  7397. return gdata.size == size
  7398. && (gdata.owned(type_info<std::decay_t<Owned>>::id()) && ...)
  7399. && (gdata.get(type_info<std::decay_t<Get>>::id()) && ...)
  7400. && (gdata.exclude(type_info<Exclude>::id()) && ...);
  7401. }); it != groups.cend())
  7402. {
  7403. handler = static_cast<handler_type *>(it->group.get());
  7404. }
  7405. if(!handler) {
  7406. group_data candidate = {
  7407. size,
  7408. { new handler_type{}, [](void *instance) { delete static_cast<handler_type *>(instance); } },
  7409. []([[maybe_unused]] const id_type ctype) ENTT_NOEXCEPT { return ((ctype == type_info<std::decay_t<Owned>>::id()) || ...); },
  7410. []([[maybe_unused]] const id_type ctype) ENTT_NOEXCEPT { return ((ctype == type_info<std::decay_t<Get>>::id()) || ...); },
  7411. []([[maybe_unused]] const id_type ctype) ENTT_NOEXCEPT { return ((ctype == type_info<Exclude>::id()) || ...); },
  7412. };
  7413. handler = static_cast<handler_type *>(candidate.group.get());
  7414. const void *maybe_valid_if = nullptr;
  7415. const void *discard_if = nullptr;
  7416. if constexpr(sizeof...(Owned) == 0) {
  7417. groups.push_back(std::move(candidate));
  7418. } else {
  7419. ENTT_ASSERT(std::all_of(groups.cbegin(), groups.cend(), [size](const auto &gdata) {
  7420. const auto overlapping = (0u + ... + gdata.owned(type_info<std::decay_t<Owned>>::id()));
  7421. const auto sz = overlapping + (0u + ... + gdata.get(type_info<std::decay_t<Get>>::id())) + (0u + ... + gdata.exclude(type_info<Exclude>::id()));
  7422. return !overlapping || ((sz == size) || (sz == gdata.size));
  7423. }));
  7424. const auto next = std::find_if_not(groups.cbegin(), groups.cend(), [size](const auto &gdata) {
  7425. return !(0u + ... + gdata.owned(type_info<std::decay_t<Owned>>::id())) || (size > gdata.size);
  7426. });
  7427. const auto prev = std::find_if(std::make_reverse_iterator(next), groups.crend(), [](const auto &gdata) {
  7428. return (0u + ... + gdata.owned(type_info<std::decay_t<Owned>>::id()));
  7429. });
  7430. maybe_valid_if = (next == groups.cend() ? maybe_valid_if : next->group.get());
  7431. discard_if = (prev == groups.crend() ? discard_if : prev->group.get());
  7432. groups.insert(next, std::move(candidate));
  7433. }
  7434. (on_construct<std::decay_t<Owned>>().before(maybe_valid_if).template connect<&handler_type::template maybe_valid_if<std::decay_t<Owned>>>(*handler), ...);
  7435. (on_construct<std::decay_t<Get>>().before(maybe_valid_if).template connect<&handler_type::template maybe_valid_if<std::decay_t<Get>>>(*handler), ...);
  7436. (on_destroy<Exclude>().before(maybe_valid_if).template connect<&handler_type::template maybe_valid_if<Exclude>>(*handler), ...);
  7437. (on_destroy<std::decay_t<Owned>>().before(discard_if).template connect<&handler_type::discard_if>(*handler), ...);
  7438. (on_destroy<std::decay_t<Get>>().before(discard_if).template connect<&handler_type::discard_if>(*handler), ...);
  7439. (on_construct<Exclude>().before(discard_if).template connect<&handler_type::discard_if>(*handler), ...);
  7440. if constexpr(sizeof...(Owned) == 0) {
  7441. for(const auto entity: view<Owned..., Get...>(exclude<Exclude...>)) {
  7442. handler->current.emplace(entity);
  7443. }
  7444. } else {
  7445. // we cannot iterate backwards because we want to leave behind valid entities in case of owned types
  7446. for(auto *first = std::get<0>(cpools).data(), *last = first + std::get<0>(cpools).size(); first != last; ++first) {
  7447. handler->template maybe_valid_if<std::tuple_element_t<0, std::tuple<std::decay_t<Owned>...>>>(*this, *first);
  7448. }
  7449. }
  7450. }
  7451. if constexpr(sizeof...(Owned) == 0) {
  7452. return { handler->current, std::get<pool_handler<std::decay_t<Get>> &>(cpools)... };
  7453. } else {
  7454. return { handler->current, std::get<pool_handler<std::decay_t<Owned>> &>(cpools)... , std::get<pool_handler<std::decay_t<Get>> &>(cpools)... };
  7455. }
  7456. }
  7457. /**
  7458. * @brief Returns a group for the given components.
  7459. *
  7460. * @sa group
  7461. *
  7462. * @tparam Owned Types of components owned by the group.
  7463. * @tparam Get Types of components observed by the group.
  7464. * @tparam Exclude Types of components used to filter the group.
  7465. * @return A newly created group.
  7466. */
  7467. template<typename... Owned, typename... Get, typename... Exclude>
  7468. [[nodiscard]] basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>, Owned...> group(get_t<Get...>, exclude_t<Exclude...> = {}) const {
  7469. static_assert(std::conjunction_v<std::is_const<Owned>..., std::is_const<Get>...>, "Invalid non-const type");
  7470. return const_cast<basic_registry *>(this)->group<Owned...>(get_t<Get...>{}, exclude<Exclude...>);
  7471. }
  7472. /**
  7473. * @brief Returns a group for the given components.
  7474. *
  7475. * @sa group
  7476. *
  7477. * @tparam Owned Types of components owned by the group.
  7478. * @tparam Exclude Types of components used to filter the group.
  7479. * @return A newly created group.
  7480. */
  7481. template<typename... Owned, typename... Exclude>
  7482. [[nodiscard]] basic_group<Entity, exclude_t<Exclude...>, get_t<>, Owned...> group(exclude_t<Exclude...> = {}) {
  7483. return group<Owned...>(get_t<>{}, exclude<Exclude...>);
  7484. }
  7485. /**
  7486. * @brief Returns a group for the given components.
  7487. *
  7488. * @sa group
  7489. *
  7490. * @tparam Owned Types of components owned by the group.
  7491. * @tparam Exclude Types of components used to filter the group.
  7492. * @return A newly created group.
  7493. */
  7494. template<typename... Owned, typename... Exclude>
  7495. [[nodiscard]] basic_group<Entity, exclude_t<Exclude...>, get_t<>, Owned...> group(exclude_t<Exclude...> = {}) const {
  7496. static_assert(std::conjunction_v<std::is_const<Owned>...>, "Invalid non-const type");
  7497. return const_cast<basic_registry *>(this)->group<Owned...>(exclude<Exclude...>);
  7498. }
  7499. /**
  7500. * @brief Checks whether the given components belong to any group.
  7501. * @tparam Component Types of components in which one is interested.
  7502. * @return True if the pools of the given components are sortable, false
  7503. * otherwise.
  7504. */
  7505. template<typename... Component>
  7506. [[nodiscard]] bool sortable() const {
  7507. return std::none_of(groups.cbegin(), groups.cend(), [](auto &&gdata) { return (gdata.owned(type_info<std::decay_t<Component>>::id()) || ...); });
  7508. }
  7509. /**
  7510. * @brief Checks whether a group can be sorted.
  7511. * @tparam Owned Types of components owned by the group.
  7512. * @tparam Get Types of components observed by the group.
  7513. * @tparam Exclude Types of components used to filter the group.
  7514. * @return True if the group can be sorted, false otherwise.
  7515. */
  7516. template<typename... Owned, typename... Get, typename... Exclude>
  7517. [[nodiscard]] bool sortable(const basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>, Owned...> &) ENTT_NOEXCEPT {
  7518. constexpr auto size = sizeof...(Owned) + sizeof...(Get) + sizeof...(Exclude);
  7519. return std::find_if(groups.cbegin(), groups.cend(), [size](const auto &gdata) {
  7520. return (0u + ... + gdata.owned(type_info<std::decay_t<Owned>>::id())) && (size < gdata.size);
  7521. }) == groups.cend();
  7522. }
  7523. /**
  7524. * @brief Sorts the pool of entities for the given component.
  7525. *
  7526. * The order of the elements in a pool is highly affected by assignments
  7527. * of components to entities and deletions. Components are arranged to
  7528. * maximize the performance during iterations and users should not make any
  7529. * assumption on the order.<br/>
  7530. * This function can be used to impose an order to the elements in the pool
  7531. * of the given component. The order is kept valid until a component of the
  7532. * given type is assigned or removed from an entity.
  7533. *
  7534. * The comparison function object must return `true` if the first element
  7535. * is _less_ than the second one, `false` otherwise. The signature of the
  7536. * comparison function should be equivalent to one of the following:
  7537. *
  7538. * @code{.cpp}
  7539. * bool(const Entity, const Entity);
  7540. * bool(const Component &, const Component &);
  7541. * @endcode
  7542. *
  7543. * Moreover, the comparison function object shall induce a
  7544. * _strict weak ordering_ on the values.
  7545. *
  7546. * The sort function oject must offer a member function template
  7547. * `operator()` that accepts three arguments:
  7548. *
  7549. * * An iterator to the first element of the range to sort.
  7550. * * An iterator past the last element of the range to sort.
  7551. * * A comparison function to use to compare the elements.
  7552. *
  7553. * The comparison funtion object received by the sort function object hasn't
  7554. * necessarily the type of the one passed along with the other parameters to
  7555. * this member function.
  7556. *
  7557. * @warning
  7558. * Pools of components owned by a group cannot be sorted.<br/>
  7559. * An assertion will abort the execution at runtime in debug mode in case
  7560. * the pool is owned by a group.
  7561. *
  7562. * @tparam Component Type of components to sort.
  7563. * @tparam Compare Type of comparison function object.
  7564. * @tparam Sort Type of sort function object.
  7565. * @tparam Args Types of arguments to forward to the sort function object.
  7566. * @param compare A valid comparison function object.
  7567. * @param algo A valid sort function object.
  7568. * @param args Arguments to forward to the sort function object, if any.
  7569. */
  7570. template<typename Component, typename Compare, typename Sort = std_sort, typename... Args>
  7571. void sort(Compare compare, Sort algo = Sort{}, Args &&... args) {
  7572. ENTT_ASSERT(sortable<Component>());
  7573. auto &cpool = assure<Component>();
  7574. cpool.sort(cpool.begin(), cpool.end(), std::move(compare), std::move(algo), std::forward<Args>(args)...);
  7575. }
  7576. /**
  7577. * @brief Sorts two pools of components in the same way.
  7578. *
  7579. * The order of the elements in a pool is highly affected by assignments
  7580. * of components to entities and deletions. Components are arranged to
  7581. * maximize the performance during iterations and users should not make any
  7582. * assumption on the order.
  7583. *
  7584. * It happens that different pools of components must be sorted the same way
  7585. * because of runtime and/or performance constraints. This function can be
  7586. * used to order a pool of components according to the order between the
  7587. * entities in another pool of components.
  7588. *
  7589. * @b How @b it @b works
  7590. *
  7591. * Being `A` and `B` the two sets where `B` is the master (the one the order
  7592. * of which rules) and `A` is the slave (the one to sort), after a call to
  7593. * this function an iterator for `A` will return the entities according to
  7594. * the following rules:
  7595. *
  7596. * * All the entities in `A` that are also in `B` are returned first
  7597. * according to the order they have in `B`.
  7598. * * All the entities in `A` that are not in `B` are returned in no
  7599. * particular order after all the other entities.
  7600. *
  7601. * Any subsequent change to `B` won't affect the order in `A`.
  7602. *
  7603. * @warning
  7604. * Pools of components owned by a group cannot be sorted.<br/>
  7605. * An assertion will abort the execution at runtime in debug mode in case
  7606. * the pool is owned by a group.
  7607. *
  7608. * @tparam To Type of components to sort.
  7609. * @tparam From Type of components to use to sort.
  7610. */
  7611. template<typename To, typename From>
  7612. void sort() {
  7613. ENTT_ASSERT(sortable<To>());
  7614. assure<To>().respect(assure<From>());
  7615. }
  7616. /**
  7617. * @brief Visits an entity and returns the types for its components.
  7618. *
  7619. * The signature of the function should be equivalent to the following:
  7620. *
  7621. * @code{.cpp}
  7622. * void(const id_type);
  7623. * @endcode
  7624. *
  7625. * Returned identifiers are those of the components owned by the entity.
  7626. *
  7627. * @sa type_info
  7628. *
  7629. * @warning
  7630. * It's not specified whether a component attached to or removed from the
  7631. * given entity during the visit is returned or not to the caller.
  7632. *
  7633. * @tparam Func Type of the function object to invoke.
  7634. * @param entity A valid entity identifier.
  7635. * @param func A valid function object.
  7636. */
  7637. template<typename Func>
  7638. void visit(entity_type entity, Func func) const {
  7639. for(auto pos = pools.size(); pos; --pos) {
  7640. if(const auto &pdata = pools[pos-1]; pdata.pool && pdata.pool->contains(entity)) {
  7641. func(pdata.type_id);
  7642. }
  7643. }
  7644. }
  7645. /**
  7646. * @brief Visits a registry and returns the types for its components.
  7647. *
  7648. * The signature of the function should be equivalent to the following:
  7649. *
  7650. * @code{.cpp}
  7651. * void(const id_type);
  7652. * @endcode
  7653. *
  7654. * Returned identifiers are those of the components managed by the registry.
  7655. *
  7656. * @sa type_info
  7657. *
  7658. * @warning
  7659. * It's not specified whether a component for which a pool is created during
  7660. * the visit is returned or not to the caller.
  7661. *
  7662. * @tparam Func Type of the function object to invoke.
  7663. * @param func A valid function object.
  7664. */
  7665. template<typename Func>
  7666. void visit(Func func) const {
  7667. for(auto pos = pools.size(); pos; --pos) {
  7668. if(const auto &pdata = pools[pos-1]; pdata.pool) {
  7669. func(pdata.type_id);
  7670. }
  7671. }
  7672. }
  7673. /**
  7674. * @brief Binds an object to the context of the registry.
  7675. *
  7676. * If the value already exists it is overwritten, otherwise a new instance
  7677. * of the given type is created and initialized with the arguments provided.
  7678. *
  7679. * @tparam Type Type of object to set.
  7680. * @tparam Args Types of arguments to use to construct the object.
  7681. * @param args Parameters to use to initialize the value.
  7682. * @return A reference to the newly created object.
  7683. */
  7684. template<typename Type, typename... Args>
  7685. Type & set(Args &&... args) {
  7686. unset<Type>();
  7687. vars.push_back(variable_data{type_info<Type>::id(), { new Type{std::forward<Args>(args)...}, [](void *instance) { delete static_cast<Type *>(instance); } }});
  7688. return *static_cast<Type *>(vars.back().value.get());
  7689. }
  7690. /**
  7691. * @brief Unsets a context variable if it exists.
  7692. * @tparam Type Type of object to set.
  7693. */
  7694. template<typename Type>
  7695. void unset() {
  7696. vars.erase(std::remove_if(vars.begin(), vars.end(), [](auto &&var) {
  7697. return var.type_id == type_info<Type>::id();
  7698. }), vars.end());
  7699. }
  7700. /**
  7701. * @brief Binds an object to the context of the registry.
  7702. *
  7703. * In case the context doesn't contain the given object, the parameters
  7704. * provided are used to construct it.
  7705. *
  7706. * @tparam Type Type of object to set.
  7707. * @tparam Args Types of arguments to use to construct the object.
  7708. * @param args Parameters to use to initialize the object.
  7709. * @return A reference to the object in the context of the registry.
  7710. */
  7711. template<typename Type, typename... Args>
  7712. [[nodiscard]] Type & ctx_or_set(Args &&... args) {
  7713. auto *value = try_ctx<Type>();
  7714. return value ? *value : set<Type>(std::forward<Args>(args)...);
  7715. }
  7716. /**
  7717. * @brief Returns a pointer to an object in the context of the registry.
  7718. * @tparam Type Type of object to get.
  7719. * @return A pointer to the object if it exists in the context of the
  7720. * registry, a null pointer otherwise.
  7721. */
  7722. template<typename Type>
  7723. [[nodiscard]] const Type * try_ctx() const {
  7724. auto it = std::find_if(vars.cbegin(), vars.cend(), [](auto &&var) { return var.type_id == type_info<Type>::id(); });
  7725. return it == vars.cend() ? nullptr : static_cast<const Type *>(it->value.get());
  7726. }
  7727. /*! @copydoc try_ctx */
  7728. template<typename Type>
  7729. [[nodiscard]] Type * try_ctx() {
  7730. return const_cast<Type *>(std::as_const(*this).template try_ctx<Type>());
  7731. }
  7732. /**
  7733. * @brief Returns a reference to an object in the context of the registry.
  7734. *
  7735. * @warning
  7736. * Attempting to get a context variable that doesn't exist results in
  7737. * undefined behavior.<br/>
  7738. * An assertion will abort the execution at runtime in debug mode in case of
  7739. * invalid requests.
  7740. *
  7741. * @tparam Type Type of object to get.
  7742. * @return A valid reference to the object in the context of the registry.
  7743. */
  7744. template<typename Type>
  7745. [[nodiscard]] const Type & ctx() const {
  7746. const auto *instance = try_ctx<Type>();
  7747. ENTT_ASSERT(instance);
  7748. return *instance;
  7749. }
  7750. /*! @copydoc ctx */
  7751. template<typename Type>
  7752. [[nodiscard]] Type & ctx() {
  7753. return const_cast<Type &>(std::as_const(*this).template ctx<Type>());
  7754. }
  7755. /**
  7756. * @brief Visits a registry and returns the types for its context variables.
  7757. *
  7758. * The signature of the function should be equivalent to the following:
  7759. *
  7760. * @code{.cpp}
  7761. * void(const id_type);
  7762. * @endcode
  7763. *
  7764. * Returned identifiers are those of the context variables currently set.
  7765. *
  7766. * @sa type_info
  7767. *
  7768. * @warning
  7769. * It's not specified whether a context variable created during the visit is
  7770. * returned or not to the caller.
  7771. *
  7772. * @tparam Func Type of the function object to invoke.
  7773. * @param func A valid function object.
  7774. */
  7775. template<typename Func>
  7776. void ctx(Func func) const {
  7777. for(auto pos = vars.size(); pos; --pos) {
  7778. func(vars[pos-1].type_id);
  7779. }
  7780. }
  7781. private:
  7782. std::vector<group_data> groups{};
  7783. mutable std::vector<pool_data> pools{};
  7784. std::vector<entity_type> entities{};
  7785. std::vector<variable_data> vars{};
  7786. entity_type destroyed{null};
  7787. };
  7788. }
  7789. #endif
  7790. // #include "entity.hpp"
  7791. // #include "fwd.hpp"
  7792. namespace entt {
  7793. /**
  7794. * @brief Dedicated to those who aren't confident with the
  7795. * entity-component-system architecture.
  7796. *
  7797. * Tiny wrapper around a registry, for all those users that aren't confident
  7798. * with entity-component-system architecture and prefer to iterate objects
  7799. * directly.
  7800. *
  7801. * @tparam Entity A valid entity type (see entt_traits for more details).
  7802. */
  7803. template<typename Entity>
  7804. struct [[deprecated("Consider using the handle class instead")]] basic_actor {
  7805. /*! @brief Type of registry used internally. */
  7806. using registry_type = basic_registry<Entity>;
  7807. /*! @brief Underlying entity identifier. */
  7808. using entity_type = Entity;
  7809. basic_actor() ENTT_NOEXCEPT
  7810. : entt{null}, reg{nullptr}
  7811. {}
  7812. /**
  7813. * @brief Move constructor.
  7814. *
  7815. * After actor move construction, instances that have been moved from are
  7816. * placed in a valid but unspecified state. It's highly discouraged to
  7817. * continue using them.
  7818. *
  7819. * @param other The instance to move from.
  7820. */
  7821. basic_actor(basic_actor &&other) ENTT_NOEXCEPT
  7822. : entt{other.entt}, reg{other.reg}
  7823. {
  7824. other.entt = null;
  7825. }
  7826. /**
  7827. * @brief Constructs an actor from a given registry.
  7828. * @param ref An instance of the registry class.
  7829. */
  7830. explicit basic_actor(registry_type &ref)
  7831. : entt{ref.create()}, reg{&ref}
  7832. {}
  7833. /**
  7834. * @brief Constructs an actor from a given entity.
  7835. * @param entity A valid entity identifier.
  7836. * @param ref An instance of the registry class.
  7837. */
  7838. explicit basic_actor(entity_type entity, registry_type &ref) ENTT_NOEXCEPT
  7839. : entt{entity}, reg{&ref}
  7840. {
  7841. ENTT_ASSERT(ref.valid(entity));
  7842. }
  7843. /*! @brief Default destructor. */
  7844. virtual ~basic_actor() {
  7845. if(*this) {
  7846. reg->destroy(entt);
  7847. }
  7848. }
  7849. /**
  7850. * @brief Move assignment operator.
  7851. *
  7852. * After actor move assignment, instances that have been moved from are
  7853. * placed in a valid but unspecified state. It's highly discouraged to
  7854. * continue using them.
  7855. *
  7856. * @param other The instance to move from.
  7857. * @return This actor.
  7858. */
  7859. basic_actor & operator=(basic_actor &&other) ENTT_NOEXCEPT {
  7860. if(this != &other) {
  7861. auto tmp{std::move(other)};
  7862. std::swap(reg, tmp.reg);
  7863. std::swap(entt, tmp.entt);
  7864. }
  7865. return *this;
  7866. }
  7867. /**
  7868. * @brief Assigns the given component to an actor.
  7869. *
  7870. * A new instance of the given component is created and initialized with the
  7871. * arguments provided (the component must have a proper constructor or be of
  7872. * aggregate type). Then the component is assigned to the actor.<br/>
  7873. * In case the actor already has a component of the given type, it's
  7874. * replaced with the new one.
  7875. *
  7876. * @tparam Component Type of the component to create.
  7877. * @tparam Args Types of arguments to use to construct the component.
  7878. * @param args Parameters to use to initialize the component.
  7879. * @return A reference to the newly created component.
  7880. */
  7881. template<typename Component, typename... Args>
  7882. decltype(auto) assign(Args &&... args) {
  7883. return reg->template emplace_or_replace<Component>(entt, std::forward<Args>(args)...);
  7884. }
  7885. /**
  7886. * @brief Removes the given component from an actor.
  7887. * @tparam Component Type of the component to remove.
  7888. */
  7889. template<typename Component>
  7890. void remove() {
  7891. reg->template remove<Component>(entt);
  7892. }
  7893. /**
  7894. * @brief Checks if an actor has the given components.
  7895. * @tparam Component Components for which to perform the check.
  7896. * @return True if the actor has all the components, false otherwise.
  7897. */
  7898. template<typename... Component>
  7899. [[nodiscard]] bool has() const {
  7900. return reg->template has<Component...>(entt);
  7901. }
  7902. /**
  7903. * @brief Returns references to the given components for an actor.
  7904. * @tparam Component Types of components to get.
  7905. * @return References to the components owned by the actor.
  7906. */
  7907. template<typename... Component>
  7908. [[nodiscard]] decltype(auto) get() const {
  7909. return std::as_const(*reg).template get<Component...>(entt);
  7910. }
  7911. /*! @copydoc get */
  7912. template<typename... Component>
  7913. [[nodiscard]] decltype(auto) get() {
  7914. return reg->template get<Component...>(entt);
  7915. }
  7916. /**
  7917. * @brief Returns pointers to the given components for an actor.
  7918. * @tparam Component Types of components to get.
  7919. * @return Pointers to the components owned by the actor.
  7920. */
  7921. template<typename... Component>
  7922. [[nodiscard]] auto try_get() const {
  7923. return std::as_const(*reg).template try_get<Component...>(entt);
  7924. }
  7925. /*! @copydoc try_get */
  7926. template<typename... Component>
  7927. [[nodiscard]] auto try_get() {
  7928. return reg->template try_get<Component...>(entt);
  7929. }
  7930. /**
  7931. * @brief Returns a reference to the underlying registry.
  7932. * @return A reference to the underlying registry.
  7933. */
  7934. [[nodiscard]] const registry_type & backend() const ENTT_NOEXCEPT {
  7935. return *reg;
  7936. }
  7937. /*! @copydoc backend */
  7938. [[nodiscard]] registry_type & backend() ENTT_NOEXCEPT {
  7939. return const_cast<registry_type &>(std::as_const(*this).backend());
  7940. }
  7941. /**
  7942. * @brief Returns the entity associated with an actor.
  7943. * @return The entity associated with the actor.
  7944. */
  7945. [[nodiscard]] entity_type entity() const ENTT_NOEXCEPT {
  7946. return entt;
  7947. }
  7948. /**
  7949. * @brief Checks if an actor refers to a valid entity or not.
  7950. * @return True if the actor refers to a valid entity, false otherwise.
  7951. */
  7952. [[nodiscard]] explicit operator bool() const {
  7953. return reg && reg->valid(entt);
  7954. }
  7955. private:
  7956. entity_type entt;
  7957. registry_type *reg;
  7958. };
  7959. }
  7960. #endif
  7961. // #include "entity/entity.hpp"
  7962. // #include "entity/group.hpp"
  7963. // #include "entity/handle.hpp"
  7964. #ifndef ENTT_ENTITY_HANDLE_HPP
  7965. #define ENTT_ENTITY_HANDLE_HPP
  7966. // #include "registry.hpp"
  7967. namespace entt {
  7968. /**
  7969. * @brief Non-owning handle to an entity.
  7970. *
  7971. * Tiny wrapper around a registry and an entity.
  7972. *
  7973. * @tparam Entity A valid entity type (see entt_traits for more details).
  7974. */
  7975. template<typename Entity>
  7976. struct basic_handle {
  7977. /*! @brief Underlying entity identifier. */
  7978. using entity_type = std::remove_const_t<Entity>;
  7979. /*! @brief Type of registry accepted by the handle. */
  7980. using registry_type = std::conditional_t<
  7981. std::is_const_v<Entity>,
  7982. const basic_registry<entity_type>,
  7983. basic_registry<entity_type>
  7984. >;
  7985. /**
  7986. * @brief Constructs a handle from a given registry and entity.
  7987. * @param ref An instance of the registry class.
  7988. * @param value An entity identifier.
  7989. */
  7990. basic_handle(registry_type &ref, entity_type value = null) ENTT_NOEXCEPT
  7991. : reg{&ref}, entt{value}
  7992. {}
  7993. /**
  7994. * @brief Assigns an entity to a handle.
  7995. * @param value An entity identifier.
  7996. * @return This handle.
  7997. */
  7998. basic_handle & operator=(const entity_type value) ENTT_NOEXCEPT {
  7999. entt = value;
  8000. return *this;
  8001. }
  8002. /**
  8003. * @brief Assigns the null object to a handle.
  8004. * @return This handle.
  8005. */
  8006. basic_handle & operator=(null_t) ENTT_NOEXCEPT {
  8007. return (*this = static_cast<entity_type>(null));
  8008. }
  8009. /**
  8010. * @brief Constructs a const handle from a non-const one.
  8011. * @return A const handle referring to the same entity.
  8012. */
  8013. [[nodiscard]] operator basic_handle<const entity_type>() const ENTT_NOEXCEPT {
  8014. return {*reg, entt};
  8015. }
  8016. /**
  8017. * @brief Converts a handle to its underlying entity.
  8018. * @return An entity identifier.
  8019. */
  8020. [[nodiscard]] operator entity_type() const ENTT_NOEXCEPT {
  8021. return entity();
  8022. }
  8023. /**
  8024. * @brief Checks if a handle refers to a valid entity or not.
  8025. * @return True if the handle refers to a valid entity, false otherwise.
  8026. */
  8027. [[nodiscard]] explicit operator bool() const {
  8028. return reg->valid(entt);
  8029. }
  8030. /**
  8031. * @brief Returns a reference to the underlying registry.
  8032. * @return A reference to the underlying registry.
  8033. */
  8034. [[nodiscard]] registry_type & registry() const ENTT_NOEXCEPT {
  8035. return *reg;
  8036. }
  8037. /**
  8038. * @brief Returns the entity associated with a handle.
  8039. * @return The entity associated with the handle.
  8040. */
  8041. [[nodiscard]] entity_type entity() const ENTT_NOEXCEPT {
  8042. return entt;
  8043. }
  8044. /**
  8045. * @brief Assigns the given component to a handle.
  8046. * @sa basic_registry::emplace
  8047. * @tparam Component Type of component to create.
  8048. * @tparam Args Types of arguments to use to construct the component.
  8049. * @param args Parameters to use to initialize the component.
  8050. * @return A reference to the newly created component.
  8051. */
  8052. template<typename Component, typename... Args>
  8053. decltype(auto) emplace(Args &&... args) const {
  8054. return reg->template emplace<Component>(entt, std::forward<Args>(args)...);
  8055. }
  8056. /**
  8057. * @brief Assigns or replaces the given component for a handle.
  8058. * @sa basic_registry::emplace_or_replace
  8059. * @tparam Component Type of component to assign or replace.
  8060. * @tparam Args Types of arguments to use to construct the component.
  8061. * @param args Parameters to use to initialize the component.
  8062. * @return A reference to the newly created component.
  8063. */
  8064. template<typename Component, typename... Args>
  8065. decltype(auto) emplace_or_replace(Args &&... args) const {
  8066. return reg->template emplace_or_replace<Component>(entt, std::forward<Args>(args)...);
  8067. }
  8068. /**
  8069. * @brief Patches the given component for a handle.
  8070. * @sa basic_registry::patch
  8071. * @tparam Component Type of component to patch.
  8072. * @tparam Func Types of the function objects to invoke.
  8073. * @param func Valid function objects.
  8074. * @return A reference to the patched component.
  8075. */
  8076. template<typename Component, typename... Func>
  8077. decltype(auto) patch(Func &&... func) const {
  8078. return reg->template patch<Component>(entt, std::forward<Func>(func)...);
  8079. }
  8080. /**
  8081. * @brief Replaces the given component for a handle.
  8082. * @sa basic_registry::replace
  8083. * @tparam Component Type of component to replace.
  8084. * @tparam Args Types of arguments to use to construct the component.
  8085. * @param args Parameters to use to initialize the component.
  8086. * @return A reference to the component being replaced.
  8087. */
  8088. template<typename Component, typename... Args>
  8089. decltype(auto) replace(Args &&... args) const {
  8090. return reg->template replace<Component>(entt, std::forward<Args>(args)...);
  8091. }
  8092. /**
  8093. * @brief Removes the given components from a handle.
  8094. * @sa basic_registry::remove
  8095. * @tparam Component Types of components to remove.
  8096. */
  8097. template<typename... Components>
  8098. void remove() const {
  8099. reg->template remove<Components...>(entt);
  8100. }
  8101. /**
  8102. * @brief Removes the given components from a handle.
  8103. * @sa basic_registry::remove_if_exists
  8104. * @tparam Component Types of components to remove.
  8105. * @return The number of components actually removed.
  8106. */
  8107. template<typename... Components>
  8108. decltype(auto) remove_if_exists() const {
  8109. return reg->template remove_if_exists<Components...>(entt);
  8110. }
  8111. /**
  8112. * @brief Removes all the components from a handle and makes it orphaned.
  8113. * @sa basic_registry::remove_all
  8114. */
  8115. void remove_all() const {
  8116. reg->remove_all(entt);
  8117. }
  8118. /**
  8119. * @brief Checks if a handle has all the given components.
  8120. * @sa basic_registry::has
  8121. * @tparam Component Components for which to perform the check.
  8122. * @return True if the handle has all the components, false otherwise.
  8123. */
  8124. template<typename... Components>
  8125. [[nodiscard]] decltype(auto) has() const {
  8126. return reg->template has<Components...>(entt);
  8127. }
  8128. /**
  8129. * @brief Checks if a handle has at least one of the given components.
  8130. * @sa basic_registry::any
  8131. * @tparam Component Components for which to perform the check.
  8132. * @return True if the handle has at least one of the given components,
  8133. * false otherwise.
  8134. */
  8135. template<typename... Components>
  8136. [[nodiscard]] decltype(auto) any() const {
  8137. return reg->template any<Components...>(entt);
  8138. }
  8139. /**
  8140. * @brief Returns references to the given components for a handle.
  8141. * @sa basic_registry::get
  8142. * @tparam Component Types of components to get.
  8143. * @return References to the components owned by the handle.
  8144. */
  8145. template<typename... Components>
  8146. [[nodiscard]] decltype(auto) get() const {
  8147. return reg->template get<Components...>(entt);
  8148. }
  8149. /**
  8150. * @brief Returns a reference to the given component for a handle.
  8151. * @sa basic_registry::get_or_emplace
  8152. * @tparam Component Type of component to get.
  8153. * @tparam Args Types of arguments to use to construct the component.
  8154. * @param args Parameters to use to initialize the component.
  8155. * @return Reference to the component owned by the handle.
  8156. */
  8157. template<typename Component, typename... Args>
  8158. [[nodiscard]] decltype(auto) get_or_emplace(Args &&... args) const {
  8159. return reg->template get_or_emplace<Component>(entt, std::forward<Args>(args)...);
  8160. }
  8161. /**
  8162. * @brief Returns pointers to the given components for a handle.
  8163. * @sa basic_registry::try_get
  8164. * @tparam Component Types of components to get.
  8165. * @return Pointers to the components owned by the handle.
  8166. */
  8167. template<typename... Components>
  8168. [[nodiscard]] decltype(auto) try_get() const {
  8169. return reg->template try_get<Components...>(entt);
  8170. }
  8171. /**
  8172. * @brief Checks if a handle has components assigned.
  8173. * @return True if the handle has no components assigned, false otherwise.
  8174. */
  8175. [[nodiscard]] bool orphan() const {
  8176. return reg->orphan(entt);
  8177. }
  8178. /**
  8179. * @brief Visits a handle and returns the types for its components.
  8180. * @sa basic_registry::visit
  8181. * @tparam Func Type of the function object to invoke.
  8182. * @param func A valid function object.
  8183. */
  8184. template<typename Func>
  8185. void visit(Func &&func) const {
  8186. reg->visit(entt, std::forward<Func>(func));
  8187. }
  8188. private:
  8189. registry_type *reg;
  8190. entity_type entt;
  8191. };
  8192. /**
  8193. * @brief Deduction guide.
  8194. * @tparam Entity A valid entity type (see entt_traits for more details).
  8195. */
  8196. template<typename Entity>
  8197. basic_handle(basic_registry<Entity> &, Entity) -> basic_handle<Entity>;
  8198. /**
  8199. * @brief Deduction guide.
  8200. * @tparam Entity A valid entity type (see entt_traits for more details).
  8201. */
  8202. template<typename Entity>
  8203. basic_handle(const basic_registry<Entity> &, Entity) -> basic_handle<const Entity>;
  8204. }
  8205. #endif
  8206. // #include "entity/helper.hpp"
  8207. #ifndef ENTT_ENTITY_HELPER_HPP
  8208. #define ENTT_ENTITY_HELPER_HPP
  8209. #include <type_traits>
  8210. // #include "../config/config.h"
  8211. // #include "../core/type_traits.hpp"
  8212. // #include "../signal/delegate.hpp"
  8213. // #include "registry.hpp"
  8214. // #include "fwd.hpp"
  8215. namespace entt {
  8216. /**
  8217. * @brief Converts a registry to a view.
  8218. * @tparam Const Constness of the accepted registry.
  8219. * @tparam Entity A valid entity type (see entt_traits for more details).
  8220. */
  8221. template<bool Const, typename Entity>
  8222. struct as_view {
  8223. /*! @brief Type of registry to convert. */
  8224. using registry_type = std::conditional_t<Const, const basic_registry<Entity>, basic_registry<Entity>>;
  8225. /**
  8226. * @brief Constructs a converter for a given registry.
  8227. * @param source A valid reference to a registry.
  8228. */
  8229. as_view(registry_type &source) ENTT_NOEXCEPT: reg{source} {}
  8230. /**
  8231. * @brief Conversion function from a registry to a view.
  8232. * @tparam Exclude Types of components used to filter the view.
  8233. * @tparam Component Type of components used to construct the view.
  8234. * @return A newly created view.
  8235. */
  8236. template<typename Exclude, typename... Component>
  8237. operator basic_view<Entity, Exclude, Component...>() const {
  8238. return reg.template view<Component...>(Exclude{});
  8239. }
  8240. private:
  8241. registry_type &reg;
  8242. };
  8243. /**
  8244. * @brief Deduction guide.
  8245. *
  8246. * It allows to deduce the constness of a registry directly from the instance
  8247. * provided to the constructor.
  8248. *
  8249. * @tparam Entity A valid entity type (see entt_traits for more details).
  8250. */
  8251. template<typename Entity>
  8252. as_view(basic_registry<Entity> &) ENTT_NOEXCEPT -> as_view<false, Entity>;
  8253. /*! @copydoc as_view */
  8254. template<typename Entity>
  8255. as_view(const basic_registry<Entity> &) ENTT_NOEXCEPT -> as_view<true, Entity>;
  8256. /**
  8257. * @brief Converts a registry to a group.
  8258. * @tparam Const Constness of the accepted registry.
  8259. * @tparam Entity A valid entity type (see entt_traits for more details).
  8260. */
  8261. template<bool Const, typename Entity>
  8262. struct as_group {
  8263. /*! @brief Type of registry to convert. */
  8264. using registry_type = std::conditional_t<Const, const basic_registry<Entity>, basic_registry<Entity>>;
  8265. /**
  8266. * @brief Constructs a converter for a given registry.
  8267. * @param source A valid reference to a registry.
  8268. */
  8269. as_group(registry_type &source) ENTT_NOEXCEPT: reg{source} {}
  8270. /**
  8271. * @brief Conversion function from a registry to a group.
  8272. * @tparam Exclude Types of components used to filter the group.
  8273. * @tparam Get Types of components observed by the group.
  8274. * @tparam Owned Types of components owned by the group.
  8275. * @return A newly created group.
  8276. */
  8277. template<typename Exclude, typename Get, typename... Owned>
  8278. operator basic_group<Entity, Exclude, Get, Owned...>() const {
  8279. return reg.template group<Owned...>(Get{}, Exclude{});
  8280. }
  8281. private:
  8282. registry_type &reg;
  8283. };
  8284. /**
  8285. * @brief Deduction guide.
  8286. *
  8287. * It allows to deduce the constness of a registry directly from the instance
  8288. * provided to the constructor.
  8289. *
  8290. * @tparam Entity A valid entity type (see entt_traits for more details).
  8291. */
  8292. template<typename Entity>
  8293. as_group(basic_registry<Entity> &) ENTT_NOEXCEPT -> as_group<false, Entity>;
  8294. /*! @copydoc as_group */
  8295. template<typename Entity>
  8296. as_group(const basic_registry<Entity> &) ENTT_NOEXCEPT -> as_group<true, Entity>;
  8297. /**
  8298. * @brief Helper to create a listener that directly invokes a member function.
  8299. * @tparam Member Member function to invoke on a component of the given type.
  8300. * @tparam Entity A valid entity type (see entt_traits for more details).
  8301. * @param reg A registry that contains the given entity and its components.
  8302. * @param entt Entity from which to get the component.
  8303. */
  8304. template<auto Member, typename Entity = entity>
  8305. void invoke(basic_registry<Entity> &reg, const Entity entt) {
  8306. static_assert(std::is_member_function_pointer_v<decltype(Member)>, "Invalid pointer to non-static member function");
  8307. delegate<void(basic_registry<Entity> &, const Entity)> func;
  8308. func.template connect<Member>(reg.template get<member_class_t<decltype(Member)>>(entt));
  8309. func(reg, entt);
  8310. }
  8311. /**
  8312. * @brief Returns the entity associated with a given component.
  8313. * @tparam Entity A valid entity type (see entt_traits for more details).
  8314. * @tparam Component Type of component.
  8315. * @param reg A registry that contains the given entity and its components.
  8316. * @param component A valid component instance.
  8317. * @return The entity associated with the given component.
  8318. */
  8319. template<typename Entity, typename Component>
  8320. Entity to_entity(const basic_registry<Entity> &reg, const Component &component) {
  8321. return *(reg.template data<Component>() + (&component - reg.template raw<Component>()));
  8322. }
  8323. }
  8324. #endif
  8325. // #include "entity/observer.hpp"
  8326. #ifndef ENTT_ENTITY_OBSERVER_HPP
  8327. #define ENTT_ENTITY_OBSERVER_HPP
  8328. #include <limits>
  8329. #include <cstddef>
  8330. #include <cstdint>
  8331. #include <utility>
  8332. #include <type_traits>
  8333. // #include "../config/config.h"
  8334. // #include "../core/type_traits.hpp"
  8335. // #include "registry.hpp"
  8336. // #include "storage.hpp"
  8337. // #include "utility.hpp"
  8338. // #include "entity.hpp"
  8339. // #include "fwd.hpp"
  8340. namespace entt {
  8341. /*! @brief Grouping matcher. */
  8342. template<typename...>
  8343. struct matcher {};
  8344. /**
  8345. * @brief Collector.
  8346. *
  8347. * Primary template isn't defined on purpose. All the specializations give a
  8348. * compile-time error, but for a few reasonable cases.
  8349. */
  8350. template<typename...>
  8351. struct basic_collector;
  8352. /**
  8353. * @brief Collector.
  8354. *
  8355. * A collector contains a set of rules (literally, matchers) to use to track
  8356. * entities.<br/>
  8357. * Its main purpose is to generate a descriptor that allows an observer to know
  8358. * how to connect to a registry.
  8359. */
  8360. template<>
  8361. struct basic_collector<> {
  8362. /**
  8363. * @brief Adds a grouping matcher to the collector.
  8364. * @tparam AllOf Types of components tracked by the matcher.
  8365. * @tparam NoneOf Types of components used to filter out entities.
  8366. * @return The updated collector.
  8367. */
  8368. template<typename... AllOf, typename... NoneOf>
  8369. static constexpr auto group(exclude_t<NoneOf...> = {}) ENTT_NOEXCEPT {
  8370. return basic_collector<matcher<type_list<>, type_list<>, type_list<NoneOf...>, AllOf...>>{};
  8371. }
  8372. /**
  8373. * @brief Adds an observing matcher to the collector.
  8374. * @tparam AnyOf Type of component for which changes should be detected.
  8375. * @return The updated collector.
  8376. */
  8377. template<typename AnyOf>
  8378. static constexpr auto update() ENTT_NOEXCEPT {
  8379. return basic_collector<matcher<type_list<>, type_list<>, AnyOf>>{};
  8380. }
  8381. };
  8382. /**
  8383. * @brief Collector.
  8384. * @copydetails basic_collector<>
  8385. * @tparam Reject Untracked types used to filter out entities.
  8386. * @tparam Require Untracked types required by the matcher.
  8387. * @tparam Rule Specific details of the current matcher.
  8388. * @tparam Other Other matchers.
  8389. */
  8390. template<typename... Reject, typename... Require, typename... Rule, typename... Other>
  8391. struct basic_collector<matcher<type_list<Reject...>, type_list<Require...>, Rule...>, Other...> {
  8392. /*! @brief Current matcher. */
  8393. using current_type = matcher<type_list<Reject...>, type_list<Require...>, Rule...>;
  8394. /**
  8395. * @brief Adds a grouping matcher to the collector.
  8396. * @tparam AllOf Types of components tracked by the matcher.
  8397. * @tparam NoneOf Types of components used to filter out entities.
  8398. * @return The updated collector.
  8399. */
  8400. template<typename... AllOf, typename... NoneOf>
  8401. static constexpr auto group(exclude_t<NoneOf...> = {}) ENTT_NOEXCEPT {
  8402. return basic_collector<matcher<type_list<>, type_list<>, type_list<NoneOf...>, AllOf...>, current_type, Other...>{};
  8403. }
  8404. /**
  8405. * @brief Adds an observing matcher to the collector.
  8406. * @tparam AnyOf Type of component for which changes should be detected.
  8407. * @return The updated collector.
  8408. */
  8409. template<typename AnyOf>
  8410. static constexpr auto update() ENTT_NOEXCEPT {
  8411. return basic_collector<matcher<type_list<>, type_list<>, AnyOf>, current_type, Other...>{};
  8412. }
  8413. /**
  8414. * @brief Updates the filter of the last added matcher.
  8415. * @tparam AllOf Types of components required by the matcher.
  8416. * @tparam NoneOf Types of components used to filter out entities.
  8417. * @return The updated collector.
  8418. */
  8419. template<typename... AllOf, typename... NoneOf>
  8420. static constexpr auto where(exclude_t<NoneOf...> = {}) ENTT_NOEXCEPT {
  8421. using extended_type = matcher<type_list<Reject..., NoneOf...>, type_list<Require..., AllOf...>, Rule...>;
  8422. return basic_collector<extended_type, Other...>{};
  8423. }
  8424. };
  8425. /*! @brief Variable template used to ease the definition of collectors. */
  8426. inline constexpr basic_collector<> collector{};
  8427. /**
  8428. * @brief Observer.
  8429. *
  8430. * An observer returns all the entities and only the entities that fit the
  8431. * requirements of at least one matcher. Moreover, it's guaranteed that the
  8432. * entity list is tightly packed in memory for fast iterations.<br/>
  8433. * In general, observers don't stay true to the order of any set of components.
  8434. *
  8435. * Observers work mainly with two types of matchers, provided through a
  8436. * collector:
  8437. *
  8438. * * Observing matcher: an observer will return at least all the living entities
  8439. * for which one or more of the given components have been updated and not yet
  8440. * destroyed.
  8441. * * Grouping matcher: an observer will return at least all the living entities
  8442. * that would have entered the given group if it existed and that would have
  8443. * not yet left it.
  8444. *
  8445. * If an entity respects the requirements of multiple matchers, it will be
  8446. * returned once and only once by the observer in any case.
  8447. *
  8448. * Matchers support also filtering by means of a _where_ clause that accepts
  8449. * both a list of types and an exclusion list.<br/>
  8450. * Whenever a matcher finds that an entity matches its requirements, the
  8451. * condition of the filter is verified before to register the entity itself.
  8452. * Moreover, a registered entity isn't returned by the observer if the condition
  8453. * set by the filter is broken in the meantime.
  8454. *
  8455. * @b Important
  8456. *
  8457. * Iterators aren't invalidated if:
  8458. *
  8459. * * New instances of the given components are created and assigned to entities.
  8460. * * The entity currently pointed is modified (as an example, if one of the
  8461. * given components is removed from the entity to which the iterator points).
  8462. * * The entity currently pointed is destroyed.
  8463. *
  8464. * In all the other cases, modifying the pools of the given components in any
  8465. * way invalidates all the iterators and using them results in undefined
  8466. * behavior.
  8467. *
  8468. * @warning
  8469. * Lifetime of an observer doesn't necessarily have to overcome that of the
  8470. * registry to which it is connected. However, the observer must be disconnected
  8471. * from the registry before being destroyed to avoid crashes due to dangling
  8472. * pointers.
  8473. *
  8474. * @tparam Entity A valid entity type (see entt_traits for more details).
  8475. */
  8476. template<typename Entity>
  8477. class basic_observer {
  8478. using payload_type = std::uint32_t;
  8479. template<typename>
  8480. struct matcher_handler;
  8481. template<typename... Reject, typename... Require, typename AnyOf>
  8482. struct matcher_handler<matcher<type_list<Reject...>, type_list<Require...>, AnyOf>> {
  8483. template<std::size_t Index>
  8484. static void maybe_valid_if(basic_observer &obs, const basic_registry<Entity> &reg, const Entity entt) {
  8485. if(reg.template has<Require...>(entt) && !reg.template any<Reject...>(entt)) {
  8486. if(auto *comp = obs.view.try_get(entt); !comp) {
  8487. obs.view.emplace(entt);
  8488. }
  8489. obs.view.get(entt) |= (1 << Index);
  8490. }
  8491. }
  8492. template<std::size_t Index>
  8493. static void discard_if(basic_observer &obs, const basic_registry<Entity> &, const Entity entt) {
  8494. if(auto *value = obs.view.try_get(entt); value && !(*value &= (~(1 << Index)))) {
  8495. obs.view.erase(entt);
  8496. }
  8497. }
  8498. template<std::size_t Index>
  8499. static void connect(basic_observer &obs, basic_registry<Entity> &reg) {
  8500. (reg.template on_destroy<Require>().template connect<&discard_if<Index>>(obs), ...);
  8501. (reg.template on_construct<Reject>().template connect<&discard_if<Index>>(obs), ...);
  8502. reg.template on_update<AnyOf>().template connect<&maybe_valid_if<Index>>(obs);
  8503. reg.template on_destroy<AnyOf>().template connect<&discard_if<Index>>(obs);
  8504. }
  8505. static void disconnect(basic_observer &obs, basic_registry<Entity> &reg) {
  8506. (reg.template on_destroy<Require>().disconnect(obs), ...);
  8507. (reg.template on_construct<Reject>().disconnect(obs), ...);
  8508. reg.template on_update<AnyOf>().disconnect(obs);
  8509. reg.template on_destroy<AnyOf>().disconnect(obs);
  8510. }
  8511. };
  8512. template<typename... Reject, typename... Require, typename... NoneOf, typename... AllOf>
  8513. struct matcher_handler<matcher<type_list<Reject...>, type_list<Require...>, type_list<NoneOf...>, AllOf...>> {
  8514. template<std::size_t Index>
  8515. static void maybe_valid_if(basic_observer &obs, const basic_registry<Entity> &reg, const Entity entt) {
  8516. if(reg.template has<AllOf..., Require...>(entt) && !reg.template any<NoneOf..., Reject...>(entt)) {
  8517. if(auto *comp = obs.view.try_get(entt); !comp) {
  8518. obs.view.emplace(entt);
  8519. }
  8520. obs.view.get(entt) |= (1 << Index);
  8521. }
  8522. }
  8523. template<std::size_t Index>
  8524. static void discard_if(basic_observer &obs, const basic_registry<Entity> &, const Entity entt) {
  8525. if(auto *value = obs.view.try_get(entt); value && !(*value &= (~(1 << Index)))) {
  8526. obs.view.erase(entt);
  8527. }
  8528. }
  8529. template<std::size_t Index>
  8530. static void connect(basic_observer &obs, basic_registry<Entity> &reg) {
  8531. (reg.template on_destroy<Require>().template connect<&discard_if<Index>>(obs), ...);
  8532. (reg.template on_construct<Reject>().template connect<&discard_if<Index>>(obs), ...);
  8533. (reg.template on_construct<AllOf>().template connect<&maybe_valid_if<Index>>(obs), ...);
  8534. (reg.template on_destroy<NoneOf>().template connect<&maybe_valid_if<Index>>(obs), ...);
  8535. (reg.template on_destroy<AllOf>().template connect<&discard_if<Index>>(obs), ...);
  8536. (reg.template on_construct<NoneOf>().template connect<&discard_if<Index>>(obs), ...);
  8537. }
  8538. static void disconnect(basic_observer &obs, basic_registry<Entity> &reg) {
  8539. (reg.template on_destroy<Require>().disconnect(obs), ...);
  8540. (reg.template on_construct<Reject>().disconnect(obs), ...);
  8541. (reg.template on_construct<AllOf>().disconnect(obs), ...);
  8542. (reg.template on_destroy<NoneOf>().disconnect(obs), ...);
  8543. (reg.template on_destroy<AllOf>().disconnect(obs), ...);
  8544. (reg.template on_construct<NoneOf>().disconnect(obs), ...);
  8545. }
  8546. };
  8547. template<typename... Matcher>
  8548. static void disconnect(basic_observer &obs, basic_registry<Entity> &reg) {
  8549. (matcher_handler<Matcher>::disconnect(obs, reg), ...);
  8550. }
  8551. template<typename... Matcher, std::size_t... Index>
  8552. void connect(basic_registry<Entity> &reg, std::index_sequence<Index...>) {
  8553. static_assert(sizeof...(Matcher) < std::numeric_limits<payload_type>::digits, "Too many matchers");
  8554. (matcher_handler<Matcher>::template connect<Index>(*this, reg), ...);
  8555. release = &basic_observer::disconnect<Matcher...>;
  8556. }
  8557. public:
  8558. /*! @brief Underlying entity identifier. */
  8559. using entity_type = Entity;
  8560. /*! @brief Unsigned integer type. */
  8561. using size_type = std::size_t;
  8562. /*! @brief Random access iterator type. */
  8563. using iterator = typename sparse_set<Entity>::iterator;
  8564. /*! @brief Default constructor. */
  8565. basic_observer()
  8566. : target{}, release{}, view{}
  8567. {}
  8568. /*! @brief Default copy constructor, deleted on purpose. */
  8569. basic_observer(const basic_observer &) = delete;
  8570. /*! @brief Default move constructor, deleted on purpose. */
  8571. basic_observer(basic_observer &&) = delete;
  8572. /**
  8573. * @brief Creates an observer and connects it to a given registry.
  8574. * @tparam Matcher Types of matchers to use to initialize the observer.
  8575. * @param reg A valid reference to a registry.
  8576. */
  8577. template<typename... Matcher>
  8578. basic_observer(basic_registry<entity_type> &reg, basic_collector<Matcher...>)
  8579. : target{&reg},
  8580. release{},
  8581. view{}
  8582. {
  8583. connect<Matcher...>(reg, std::index_sequence_for<Matcher...>{});
  8584. }
  8585. /*! @brief Default destructor. */
  8586. ~basic_observer() = default;
  8587. /**
  8588. * @brief Default copy assignment operator, deleted on purpose.
  8589. * @return This observer.
  8590. */
  8591. basic_observer & operator=(const basic_observer &) = delete;
  8592. /**
  8593. * @brief Default move assignment operator, deleted on purpose.
  8594. * @return This observer.
  8595. */
  8596. basic_observer & operator=(basic_observer &&) = delete;
  8597. /**
  8598. * @brief Connects an observer to a given registry.
  8599. * @tparam Matcher Types of matchers to use to initialize the observer.
  8600. * @param reg A valid reference to a registry.
  8601. */
  8602. template<typename... Matcher>
  8603. void connect(basic_registry<entity_type> &reg, basic_collector<Matcher...>) {
  8604. disconnect();
  8605. connect<Matcher...>(reg, std::index_sequence_for<Matcher...>{});
  8606. target = &reg;
  8607. view.clear();
  8608. }
  8609. /*! @brief Disconnects an observer from the registry it keeps track of. */
  8610. void disconnect() {
  8611. if(release) {
  8612. release(*this, *target);
  8613. release = nullptr;
  8614. }
  8615. }
  8616. /**
  8617. * @brief Returns the number of elements in an observer.
  8618. * @return Number of elements.
  8619. */
  8620. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  8621. return view.size();
  8622. }
  8623. /**
  8624. * @brief Checks whether an observer is empty.
  8625. * @return True if the observer is empty, false otherwise.
  8626. */
  8627. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  8628. return view.empty();
  8629. }
  8630. /**
  8631. * @brief Direct access to the list of entities of the observer.
  8632. *
  8633. * The returned pointer is such that range `[data(), data() + size()]` is
  8634. * always a valid range, even if the container is empty.
  8635. *
  8636. * @note
  8637. * Entities are in the reverse order as returned by the `begin`/`end`
  8638. * iterators.
  8639. *
  8640. * @return A pointer to the array of entities.
  8641. */
  8642. [[nodiscard]] const entity_type * data() const ENTT_NOEXCEPT {
  8643. return view.data();
  8644. }
  8645. /**
  8646. * @brief Returns an iterator to the first entity of the observer.
  8647. *
  8648. * The returned iterator points to the first entity of the observer. If the
  8649. * container is empty, the returned iterator will be equal to `end()`.
  8650. *
  8651. * @return An iterator to the first entity of the observer.
  8652. */
  8653. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  8654. return view.sparse_set<entity_type>::begin();
  8655. }
  8656. /**
  8657. * @brief Returns an iterator that is past the last entity of the observer.
  8658. *
  8659. * The returned iterator points to the entity following the last entity of
  8660. * the observer. Attempting to dereference the returned iterator results in
  8661. * undefined behavior.
  8662. *
  8663. * @return An iterator to the entity following the last entity of the
  8664. * observer.
  8665. */
  8666. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  8667. return view.sparse_set<entity_type>::end();
  8668. }
  8669. /*! @brief Clears the underlying container. */
  8670. void clear() ENTT_NOEXCEPT {
  8671. view.clear();
  8672. }
  8673. /**
  8674. * @brief Iterates entities and applies the given function object to them.
  8675. *
  8676. * The function object is invoked for each entity.<br/>
  8677. * The signature of the function must be equivalent to the following form:
  8678. *
  8679. * @code{.cpp}
  8680. * void(const entity_type);
  8681. * @endcode
  8682. *
  8683. * @tparam Func Type of the function object to invoke.
  8684. * @param func A valid function object.
  8685. */
  8686. template<typename Func>
  8687. void each(Func func) const {
  8688. for(const auto entity: *this) {
  8689. func(entity);
  8690. }
  8691. }
  8692. /**
  8693. * @brief Iterates entities and applies the given function object to them,
  8694. * then clears the observer.
  8695. *
  8696. * @sa each
  8697. *
  8698. * @tparam Func Type of the function object to invoke.
  8699. * @param func A valid function object.
  8700. */
  8701. template<typename Func>
  8702. void each(Func func) {
  8703. std::as_const(*this).each(std::move(func));
  8704. clear();
  8705. }
  8706. private:
  8707. basic_registry<entity_type> *target;
  8708. void(* release)(basic_observer &, basic_registry<entity_type> &);
  8709. storage<entity_type, payload_type> view;
  8710. };
  8711. }
  8712. #endif
  8713. // #include "entity/pool.hpp"
  8714. // #include "entity/registry.hpp"
  8715. // #include "entity/runtime_view.hpp"
  8716. // #include "entity/snapshot.hpp"
  8717. #ifndef ENTT_ENTITY_SNAPSHOT_HPP
  8718. #define ENTT_ENTITY_SNAPSHOT_HPP
  8719. #include <array>
  8720. #include <vector>
  8721. #include <cstddef>
  8722. #include <utility>
  8723. #include <iterator>
  8724. #include <type_traits>
  8725. #include <unordered_map>
  8726. // #include "../config/config.h"
  8727. // #include "entity.hpp"
  8728. // #include "fwd.hpp"
  8729. namespace entt {
  8730. /**
  8731. * @brief Utility class to create snapshots from a registry.
  8732. *
  8733. * A _snapshot_ can be either a dump of the entire registry or a narrower
  8734. * selection of components of interest.<br/>
  8735. * This type can be used in both cases if provided with a correctly configured
  8736. * output archive.
  8737. *
  8738. * @tparam Entity A valid entity type (see entt_traits for more details).
  8739. */
  8740. template<typename Entity>
  8741. class basic_snapshot {
  8742. /*! @brief A registry is allowed to create snapshots. */
  8743. friend class basic_registry<Entity>;
  8744. using traits_type = entt_traits<Entity>;
  8745. template<typename Component, typename Archive, typename It>
  8746. void get(Archive &archive, std::size_t sz, It first, It last) const {
  8747. archive(typename traits_type::entity_type(sz));
  8748. while(first != last) {
  8749. const auto entt = *(first++);
  8750. if(reg->template has<Component>(entt)) {
  8751. if constexpr(std::is_empty_v<Component>) {
  8752. archive(entt);
  8753. } else {
  8754. archive(entt, reg->template get<Component>(entt));
  8755. }
  8756. }
  8757. }
  8758. }
  8759. template<typename... Component, typename Archive, typename It, std::size_t... Indexes>
  8760. void component(Archive &archive, It first, It last, std::index_sequence<Indexes...>) const {
  8761. std::array<std::size_t, sizeof...(Indexes)> size{};
  8762. auto begin = first;
  8763. while(begin != last) {
  8764. const auto entt = *(begin++);
  8765. ((reg->template has<Component>(entt) ? ++size[Indexes] : size[Indexes]), ...);
  8766. }
  8767. (get<Component>(archive, size[Indexes], first, last), ...);
  8768. }
  8769. public:
  8770. /*! @brief Underlying entity identifier. */
  8771. using entity_type = Entity;
  8772. /**
  8773. * @brief Constructs an instance that is bound to a given registry.
  8774. * @param source A valid reference to a registry.
  8775. */
  8776. basic_snapshot(const basic_registry<entity_type> &source) ENTT_NOEXCEPT
  8777. : reg{&source}
  8778. {}
  8779. /*! @brief Default move constructor. */
  8780. basic_snapshot(basic_snapshot &&) = default;
  8781. /*! @brief Default move assignment operator. @return This snapshot. */
  8782. basic_snapshot & operator=(basic_snapshot &&) = default;
  8783. /**
  8784. * @brief Puts aside all the entities from the underlying registry.
  8785. *
  8786. * Entities are serialized along with their versions. Destroyed entities are
  8787. * taken in consideration as well by this function.
  8788. *
  8789. * @tparam Archive Type of output archive.
  8790. * @param archive A valid reference to an output archive.
  8791. * @return An object of this type to continue creating the snapshot.
  8792. */
  8793. template<typename Archive>
  8794. const basic_snapshot & entities(Archive &archive) const {
  8795. const auto sz = reg->size();
  8796. auto first = reg->data();
  8797. const auto last = first + sz;
  8798. archive(typename traits_type::entity_type(sz));
  8799. while(first != last) {
  8800. archive(*(first++));
  8801. }
  8802. return *this;
  8803. }
  8804. /**
  8805. * @brief Puts aside the given components.
  8806. *
  8807. * Each instance is serialized together with the entity to which it belongs.
  8808. * Entities are serialized along with their versions.
  8809. *
  8810. * @tparam Component Types of components to serialize.
  8811. * @tparam Archive Type of output archive.
  8812. * @param archive A valid reference to an output archive.
  8813. * @return An object of this type to continue creating the snapshot.
  8814. */
  8815. template<typename... Component, typename Archive>
  8816. const basic_snapshot & component(Archive &archive) const {
  8817. (component<Component>(archive, reg->template data<Component>(), reg->template data<Component>() + reg->template size<Component>()), ...);
  8818. return *this;
  8819. }
  8820. /**
  8821. * @brief Puts aside the given components for the entities in a range.
  8822. *
  8823. * Each instance is serialized together with the entity to which it belongs.
  8824. * Entities are serialized along with their versions.
  8825. *
  8826. * @tparam Component Types of components to serialize.
  8827. * @tparam Archive Type of output archive.
  8828. * @tparam It Type of input iterator.
  8829. * @param archive A valid reference to an output archive.
  8830. * @param first An iterator to the first element of the range to serialize.
  8831. * @param last An iterator past the last element of the range to serialize.
  8832. * @return An object of this type to continue creating the snapshot.
  8833. */
  8834. template<typename... Component, typename Archive, typename It>
  8835. const basic_snapshot & component(Archive &archive, It first, It last) const {
  8836. component<Component...>(archive, first, last, std::index_sequence_for<Component...>{});
  8837. return *this;
  8838. }
  8839. private:
  8840. const basic_registry<entity_type> *reg;
  8841. };
  8842. /**
  8843. * @brief Utility class to restore a snapshot as a whole.
  8844. *
  8845. * A snapshot loader requires that the destination registry be empty and loads
  8846. * all the data at once while keeping intact the identifiers that the entities
  8847. * originally had.<br/>
  8848. * An example of use is the implementation of a save/restore utility.
  8849. *
  8850. * @tparam Entity A valid entity type (see entt_traits for more details).
  8851. */
  8852. template<typename Entity>
  8853. class basic_snapshot_loader {
  8854. /*! @brief A registry is allowed to create snapshot loaders. */
  8855. friend class basic_registry<Entity>;
  8856. using traits_type = entt_traits<Entity>;
  8857. template<typename Type, typename Archive>
  8858. void assign(Archive &archive) const {
  8859. typename traits_type::entity_type length{};
  8860. archive(length);
  8861. entity_type entt{};
  8862. if constexpr(std::is_empty_v<Type>) {
  8863. while(length--) {
  8864. archive(entt);
  8865. const auto entity = reg->valid(entt) ? entt : reg->create(entt);
  8866. ENTT_ASSERT(entity == entt);
  8867. reg->template emplace<Type>(entity);
  8868. }
  8869. } else {
  8870. Type instance{};
  8871. while(length--) {
  8872. archive(entt, instance);
  8873. const auto entity = reg->valid(entt) ? entt : reg->create(entt);
  8874. ENTT_ASSERT(entity == entt);
  8875. reg->template emplace<Type>(entity, std::move(instance));
  8876. }
  8877. }
  8878. }
  8879. public:
  8880. /*! @brief Underlying entity identifier. */
  8881. using entity_type = Entity;
  8882. /**
  8883. * @brief Constructs an instance that is bound to a given registry.
  8884. * @param source A valid reference to a registry.
  8885. */
  8886. basic_snapshot_loader(basic_registry<entity_type> &source) ENTT_NOEXCEPT
  8887. : reg{&source}
  8888. {
  8889. // restoring a snapshot as a whole requires a clean registry
  8890. ENTT_ASSERT(reg->empty());
  8891. }
  8892. /*! @brief Default move constructor. */
  8893. basic_snapshot_loader(basic_snapshot_loader &&) = default;
  8894. /*! @brief Default move assignment operator. @return This loader. */
  8895. basic_snapshot_loader & operator=(basic_snapshot_loader &&) = default;
  8896. /**
  8897. * @brief Restores entities that were in use during serialization.
  8898. *
  8899. * This function restores the entities that were in use during serialization
  8900. * and gives them the versions they originally had.
  8901. *
  8902. * @tparam Archive Type of input archive.
  8903. * @param archive A valid reference to an input archive.
  8904. * @return A valid loader to continue restoring data.
  8905. */
  8906. template<typename Archive>
  8907. const basic_snapshot_loader & entities(Archive &archive) const {
  8908. typename traits_type::entity_type length{};
  8909. archive(length);
  8910. std::vector<entity_type> all(length);
  8911. for(decltype(length) pos{}; pos < length; ++pos) {
  8912. archive(all[pos]);
  8913. }
  8914. reg->assign(all.cbegin(), all.cend());
  8915. return *this;
  8916. }
  8917. /**
  8918. * @brief Restores components and assigns them to the right entities.
  8919. *
  8920. * The template parameter list must be exactly the same used during
  8921. * serialization. In the event that the entity to which the component is
  8922. * assigned doesn't exist yet, the loader will take care to create it with
  8923. * the version it originally had.
  8924. *
  8925. * @tparam Component Types of components to restore.
  8926. * @tparam Archive Type of input archive.
  8927. * @param archive A valid reference to an input archive.
  8928. * @return A valid loader to continue restoring data.
  8929. */
  8930. template<typename... Component, typename Archive>
  8931. const basic_snapshot_loader & component(Archive &archive) const {
  8932. (assign<Component>(archive), ...);
  8933. return *this;
  8934. }
  8935. /**
  8936. * @brief Destroys those entities that have no components.
  8937. *
  8938. * In case all the entities were serialized but only part of the components
  8939. * was saved, it could happen that some of the entities have no components
  8940. * once restored.<br/>
  8941. * This functions helps to identify and destroy those entities.
  8942. *
  8943. * @return A valid loader to continue restoring data.
  8944. */
  8945. const basic_snapshot_loader & orphans() const {
  8946. reg->orphans([this](const auto entt) {
  8947. reg->destroy(entt);
  8948. });
  8949. return *this;
  8950. }
  8951. private:
  8952. basic_registry<entity_type> *reg;
  8953. };
  8954. /**
  8955. * @brief Utility class for _continuous loading_.
  8956. *
  8957. * A _continuous loader_ is designed to load data from a source registry to a
  8958. * (possibly) non-empty destination. The loader can accommodate in a registry
  8959. * more than one snapshot in a sort of _continuous loading_ that updates the
  8960. * destination one step at a time.<br/>
  8961. * Identifiers that entities originally had are not transferred to the target.
  8962. * Instead, the loader maps remote identifiers to local ones while restoring a
  8963. * snapshot.<br/>
  8964. * An example of use is the implementation of a client-server applications with
  8965. * the requirement of transferring somehow parts of the representation side to
  8966. * side.
  8967. *
  8968. * @tparam Entity A valid entity type (see entt_traits for more details).
  8969. */
  8970. template<typename Entity>
  8971. class basic_continuous_loader {
  8972. using traits_type = entt_traits<Entity>;
  8973. void destroy(Entity entt) {
  8974. const auto it = remloc.find(entt);
  8975. if(it == remloc.cend()) {
  8976. const auto local = reg->create();
  8977. remloc.emplace(entt, std::make_pair(local, true));
  8978. reg->destroy(local);
  8979. }
  8980. }
  8981. void restore(Entity entt) {
  8982. const auto it = remloc.find(entt);
  8983. if(it == remloc.cend()) {
  8984. const auto local = reg->create();
  8985. remloc.emplace(entt, std::make_pair(local, true));
  8986. } else {
  8987. remloc[entt].first = reg->valid(remloc[entt].first) ? remloc[entt].first : reg->create();
  8988. // set the dirty flag
  8989. remloc[entt].second = true;
  8990. }
  8991. }
  8992. template<typename Container>
  8993. auto update(int, Container &container)
  8994. -> decltype(typename Container::mapped_type{}, void()) {
  8995. // map like container
  8996. Container other;
  8997. for(auto &&pair: container) {
  8998. using first_type = std::remove_const_t<typename std::decay_t<decltype(pair)>::first_type>;
  8999. using second_type = typename std::decay_t<decltype(pair)>::second_type;
  9000. if constexpr(std::is_same_v<first_type, entity_type> && std::is_same_v<second_type, entity_type>) {
  9001. other.emplace(map(pair.first), map(pair.second));
  9002. } else if constexpr(std::is_same_v<first_type, entity_type>) {
  9003. other.emplace(map(pair.first), std::move(pair.second));
  9004. } else {
  9005. static_assert(std::is_same_v<second_type, entity_type>, "Neither the key nor the value are of entity type");
  9006. other.emplace(std::move(pair.first), map(pair.second));
  9007. }
  9008. }
  9009. std::swap(container, other);
  9010. }
  9011. template<typename Container>
  9012. auto update(char, Container &container)
  9013. -> decltype(typename Container::value_type{}, void()) {
  9014. // vector like container
  9015. static_assert(std::is_same_v<typename Container::value_type, entity_type>, "Invalid value type");
  9016. for(auto &&entt: container) {
  9017. entt = map(entt);
  9018. }
  9019. }
  9020. template<typename Other, typename Type, typename Member>
  9021. void update([[maybe_unused]] Other &instance, [[maybe_unused]] Member Type:: *member) {
  9022. if constexpr(!std::is_same_v<Other, Type>) {
  9023. return;
  9024. } else if constexpr(std::is_same_v<Member, entity_type>) {
  9025. instance.*member = map(instance.*member);
  9026. } else {
  9027. // maybe a container? let's try...
  9028. update(0, instance.*member);
  9029. }
  9030. }
  9031. template<typename Component>
  9032. void remove_if_exists() {
  9033. for(auto &&ref: remloc) {
  9034. const auto local = ref.second.first;
  9035. if(reg->valid(local)) {
  9036. reg->template remove_if_exists<Component>(local);
  9037. }
  9038. }
  9039. }
  9040. template<typename Other, typename Archive, typename... Type, typename... Member>
  9041. void assign(Archive &archive, [[maybe_unused]] Member Type:: *... member) {
  9042. typename traits_type::entity_type length{};
  9043. archive(length);
  9044. entity_type entt{};
  9045. if constexpr(std::is_empty_v<Other>) {
  9046. while(length--) {
  9047. archive(entt);
  9048. restore(entt);
  9049. reg->template emplace_or_replace<Other>(map(entt));
  9050. }
  9051. } else {
  9052. Other instance{};
  9053. while(length--) {
  9054. archive(entt, instance);
  9055. (update(instance, member), ...);
  9056. restore(entt);
  9057. reg->template emplace_or_replace<Other>(map(entt), std::move(instance));
  9058. }
  9059. }
  9060. }
  9061. public:
  9062. /*! @brief Underlying entity identifier. */
  9063. using entity_type = Entity;
  9064. /**
  9065. * @brief Constructs an instance that is bound to a given registry.
  9066. * @param source A valid reference to a registry.
  9067. */
  9068. basic_continuous_loader(basic_registry<entity_type> &source) ENTT_NOEXCEPT
  9069. : reg{&source}
  9070. {}
  9071. /*! @brief Default move constructor. */
  9072. basic_continuous_loader(basic_continuous_loader &&) = default;
  9073. /*! @brief Default move assignment operator. @return This loader. */
  9074. basic_continuous_loader & operator=(basic_continuous_loader &&) = default;
  9075. /**
  9076. * @brief Restores entities that were in use during serialization.
  9077. *
  9078. * This function restores the entities that were in use during serialization
  9079. * and creates local counterparts for them if required.
  9080. *
  9081. * @tparam Archive Type of input archive.
  9082. * @param archive A valid reference to an input archive.
  9083. * @return A non-const reference to this loader.
  9084. */
  9085. template<typename Archive>
  9086. basic_continuous_loader & entities(Archive &archive) {
  9087. typename traits_type::entity_type length{};
  9088. entity_type entt{};
  9089. archive(length);
  9090. for(decltype(length) pos{}; pos < length; ++pos) {
  9091. archive(entt);
  9092. if(const auto entity = (to_integral(entt) & traits_type::entity_mask); entity == pos) {
  9093. restore(entt);
  9094. } else {
  9095. destroy(entt);
  9096. }
  9097. }
  9098. return *this;
  9099. }
  9100. /**
  9101. * @brief Restores components and assigns them to the right entities.
  9102. *
  9103. * The template parameter list must be exactly the same used during
  9104. * serialization. In the event that the entity to which the component is
  9105. * assigned doesn't exist yet, the loader will take care to create a local
  9106. * counterpart for it.<br/>
  9107. * Members can be either data members of type entity_type or containers of
  9108. * entities. In both cases, the loader will visit them and update the
  9109. * entities by replacing each one with its local counterpart.
  9110. *
  9111. * @tparam Component Type of component to restore.
  9112. * @tparam Archive Type of input archive.
  9113. * @tparam Type Types of components to update with local counterparts.
  9114. * @tparam Member Types of members to update with their local counterparts.
  9115. * @param archive A valid reference to an input archive.
  9116. * @param member Members to update with their local counterparts.
  9117. * @return A non-const reference to this loader.
  9118. */
  9119. template<typename... Component, typename Archive, typename... Type, typename... Member>
  9120. basic_continuous_loader & component(Archive &archive, Member Type:: *... member) {
  9121. (remove_if_exists<Component>(), ...);
  9122. (assign<Component>(archive, member...), ...);
  9123. return *this;
  9124. }
  9125. /**
  9126. * @brief Helps to purge entities that no longer have a conterpart.
  9127. *
  9128. * Users should invoke this member function after restoring each snapshot,
  9129. * unless they know exactly what they are doing.
  9130. *
  9131. * @return A non-const reference to this loader.
  9132. */
  9133. basic_continuous_loader & shrink() {
  9134. auto it = remloc.begin();
  9135. while(it != remloc.cend()) {
  9136. const auto local = it->second.first;
  9137. bool &dirty = it->second.second;
  9138. if(dirty) {
  9139. dirty = false;
  9140. ++it;
  9141. } else {
  9142. if(reg->valid(local)) {
  9143. reg->destroy(local);
  9144. }
  9145. it = remloc.erase(it);
  9146. }
  9147. }
  9148. return *this;
  9149. }
  9150. /**
  9151. * @brief Destroys those entities that have no components.
  9152. *
  9153. * In case all the entities were serialized but only part of the components
  9154. * was saved, it could happen that some of the entities have no components
  9155. * once restored.<br/>
  9156. * This functions helps to identify and destroy those entities.
  9157. *
  9158. * @return A non-const reference to this loader.
  9159. */
  9160. basic_continuous_loader & orphans() {
  9161. reg->orphans([this](const auto entt) {
  9162. reg->destroy(entt);
  9163. });
  9164. return *this;
  9165. }
  9166. /**
  9167. * @brief Tests if a loader knows about a given entity.
  9168. * @param entt An entity identifier.
  9169. * @return True if `entity` is managed by the loader, false otherwise.
  9170. */
  9171. [[nodiscard]] bool contains(entity_type entt) const ENTT_NOEXCEPT {
  9172. return (remloc.find(entt) != remloc.cend());
  9173. }
  9174. /**
  9175. * @brief Returns the identifier to which an entity refers.
  9176. * @param entt An entity identifier.
  9177. * @return The local identifier if any, the null entity otherwise.
  9178. */
  9179. [[nodiscard]] entity_type map(entity_type entt) const ENTT_NOEXCEPT {
  9180. const auto it = remloc.find(entt);
  9181. entity_type other = null;
  9182. if(it != remloc.cend()) {
  9183. other = it->second.first;
  9184. }
  9185. return other;
  9186. }
  9187. private:
  9188. std::unordered_map<entity_type, std::pair<entity_type, bool>> remloc;
  9189. basic_registry<entity_type> *reg;
  9190. };
  9191. }
  9192. #endif
  9193. // #include "entity/sparse_set.hpp"
  9194. // #include "entity/storage.hpp"
  9195. // #include "entity/utility.hpp"
  9196. // #include "entity/view.hpp"
  9197. // #include "locator/locator.hpp"
  9198. #ifndef ENTT_LOCATOR_LOCATOR_HPP
  9199. #define ENTT_LOCATOR_LOCATOR_HPP
  9200. #include <memory>
  9201. #include <utility>
  9202. // #include "../config/config.h"
  9203. #ifndef ENTT_CONFIG_CONFIG_H
  9204. #define ENTT_CONFIG_CONFIG_H
  9205. #ifndef ENTT_NOEXCEPT
  9206. # define ENTT_NOEXCEPT noexcept
  9207. #endif
  9208. #ifndef ENTT_HS_SUFFIX
  9209. # define ENTT_HS_SUFFIX _hs
  9210. #endif
  9211. #ifndef ENTT_HWS_SUFFIX
  9212. # define ENTT_HWS_SUFFIX _hws
  9213. #endif
  9214. #ifndef ENTT_USE_ATOMIC
  9215. # define ENTT_MAYBE_ATOMIC(Type) Type
  9216. #else
  9217. # include <atomic>
  9218. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  9219. #endif
  9220. #ifndef ENTT_ID_TYPE
  9221. # include <cstdint>
  9222. # define ENTT_ID_TYPE std::uint32_t
  9223. #endif
  9224. #ifndef ENTT_PAGE_SIZE
  9225. # define ENTT_PAGE_SIZE 32768
  9226. #endif
  9227. #ifndef ENTT_ASSERT
  9228. # include <cassert>
  9229. # define ENTT_ASSERT(condition) assert(condition)
  9230. #endif
  9231. #ifndef ENTT_NO_ETO
  9232. # include <type_traits>
  9233. # define ENTT_IS_EMPTY(Type) std::is_empty<Type>
  9234. #else
  9235. # include <type_traits>
  9236. # define ENTT_IS_EMPTY(Type) std::false_type
  9237. #endif
  9238. #ifndef ENTT_STANDARD_CPP
  9239. # if defined __clang__ || (defined __GNUC__ && __GNUC__ > 8)
  9240. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  9241. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  9242. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  9243. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  9244. # elif defined __GNUC__
  9245. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  9246. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  9247. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  9248. # elif defined _MSC_VER
  9249. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  9250. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  9251. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  9252. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  9253. # endif
  9254. #endif
  9255. #ifndef ENTT_STANDALONE
  9256. # define ENTT_FAST_PATH(...) false
  9257. #else
  9258. # define ENTT_FAST_PATH(Cond) Cond
  9259. #endif
  9260. #endif
  9261. namespace entt {
  9262. /**
  9263. * @brief Service locator, nothing more.
  9264. *
  9265. * A service locator can be used to do what it promises: locate services.<br/>
  9266. * Usually service locators are tightly bound to the services they expose and
  9267. * thus it's hard to define a general purpose class to do that. This template
  9268. * based implementation tries to fill the gap and to get rid of the burden of
  9269. * defining a different specific locator for each application.
  9270. *
  9271. * @tparam Service Type of service managed by the locator.
  9272. */
  9273. template<typename Service>
  9274. struct service_locator {
  9275. /*! @brief Type of service offered. */
  9276. using service_type = Service;
  9277. /*! @brief Default constructor, deleted on purpose. */
  9278. service_locator() = delete;
  9279. /*! @brief Default destructor, deleted on purpose. */
  9280. ~service_locator() = delete;
  9281. /**
  9282. * @brief Tests if a valid service implementation is set.
  9283. * @return True if the service is set, false otherwise.
  9284. */
  9285. [[nodiscard]] static bool empty() ENTT_NOEXCEPT {
  9286. return !static_cast<bool>(service);
  9287. }
  9288. /**
  9289. * @brief Returns a weak pointer to a service implementation, if any.
  9290. *
  9291. * Clients of a service shouldn't retain references to it. The recommended
  9292. * way is to retrieve the service implementation currently set each and
  9293. * every time the need of using it arises. Otherwise users can incur in
  9294. * unexpected behaviors.
  9295. *
  9296. * @return A reference to the service implementation currently set, if any.
  9297. */
  9298. [[nodiscard]] static std::weak_ptr<Service> get() ENTT_NOEXCEPT {
  9299. return service;
  9300. }
  9301. /**
  9302. * @brief Returns a weak reference to a service implementation, if any.
  9303. *
  9304. * Clients of a service shouldn't retain references to it. The recommended
  9305. * way is to retrieve the service implementation currently set each and
  9306. * every time the need of using it arises. Otherwise users can incur in
  9307. * unexpected behaviors.
  9308. *
  9309. * @warning
  9310. * In case no service implementation has been set, a call to this function
  9311. * results in undefined behavior.
  9312. *
  9313. * @return A reference to the service implementation currently set, if any.
  9314. */
  9315. [[nodiscard]] static Service & ref() ENTT_NOEXCEPT {
  9316. return *service;
  9317. }
  9318. /**
  9319. * @brief Sets or replaces a service.
  9320. * @tparam Impl Type of the new service to use.
  9321. * @tparam Args Types of arguments to use to construct the service.
  9322. * @param args Parameters to use to construct the service.
  9323. */
  9324. template<typename Impl = Service, typename... Args>
  9325. static void set(Args &&... args) {
  9326. service = std::make_shared<Impl>(std::forward<Args>(args)...);
  9327. }
  9328. /**
  9329. * @brief Sets or replaces a service.
  9330. * @param ptr Service to use to replace the current one.
  9331. */
  9332. static void set(std::shared_ptr<Service> ptr) {
  9333. ENTT_ASSERT(static_cast<bool>(ptr));
  9334. service = std::move(ptr);
  9335. }
  9336. /**
  9337. * @brief Resets a service.
  9338. *
  9339. * The service is no longer valid after a reset.
  9340. */
  9341. static void reset() {
  9342. service.reset();
  9343. }
  9344. private:
  9345. inline static std::shared_ptr<Service> service = nullptr;
  9346. };
  9347. }
  9348. #endif
  9349. // #include "meta/container.hpp"
  9350. #ifndef ENTT_META_CONTAINER_HPP
  9351. #define ENTT_META_CONTAINER_HPP
  9352. #include <array>
  9353. #include <map>
  9354. #include <set>
  9355. #include <unordered_map>
  9356. #include <unordered_set>
  9357. #include <utility>
  9358. #include <vector>
  9359. // #include "../config/config.h"
  9360. #ifndef ENTT_CONFIG_CONFIG_H
  9361. #define ENTT_CONFIG_CONFIG_H
  9362. #ifndef ENTT_NOEXCEPT
  9363. # define ENTT_NOEXCEPT noexcept
  9364. #endif
  9365. #ifndef ENTT_HS_SUFFIX
  9366. # define ENTT_HS_SUFFIX _hs
  9367. #endif
  9368. #ifndef ENTT_HWS_SUFFIX
  9369. # define ENTT_HWS_SUFFIX _hws
  9370. #endif
  9371. #ifndef ENTT_USE_ATOMIC
  9372. # define ENTT_MAYBE_ATOMIC(Type) Type
  9373. #else
  9374. # include <atomic>
  9375. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  9376. #endif
  9377. #ifndef ENTT_ID_TYPE
  9378. # include <cstdint>
  9379. # define ENTT_ID_TYPE std::uint32_t
  9380. #endif
  9381. #ifndef ENTT_PAGE_SIZE
  9382. # define ENTT_PAGE_SIZE 32768
  9383. #endif
  9384. #ifndef ENTT_ASSERT
  9385. # include <cassert>
  9386. # define ENTT_ASSERT(condition) assert(condition)
  9387. #endif
  9388. #ifndef ENTT_NO_ETO
  9389. # include <type_traits>
  9390. # define ENTT_IS_EMPTY(Type) std::is_empty<Type>
  9391. #else
  9392. # include <type_traits>
  9393. # define ENTT_IS_EMPTY(Type) std::false_type
  9394. #endif
  9395. #ifndef ENTT_STANDARD_CPP
  9396. # if defined __clang__ || (defined __GNUC__ && __GNUC__ > 8)
  9397. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  9398. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  9399. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  9400. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  9401. # elif defined __GNUC__
  9402. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  9403. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  9404. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  9405. # elif defined _MSC_VER
  9406. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  9407. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  9408. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  9409. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  9410. # endif
  9411. #endif
  9412. #ifndef ENTT_STANDALONE
  9413. # define ENTT_FAST_PATH(...) false
  9414. #else
  9415. # define ENTT_FAST_PATH(Cond) Cond
  9416. #endif
  9417. #endif
  9418. // #include "type_traits.hpp"
  9419. #ifndef ENTT_META_TYPE_TRAITS_HPP
  9420. #define ENTT_META_TYPE_TRAITS_HPP
  9421. #include <type_traits>
  9422. namespace entt {
  9423. /**
  9424. * @brief Traits class template to be specialized to enable support for meta
  9425. * sequence containers.
  9426. */
  9427. template<typename>
  9428. struct meta_sequence_container_traits;
  9429. /**
  9430. * @brief Traits class template to be specialized to enable support for meta
  9431. * associative containers.
  9432. */
  9433. template<typename>
  9434. struct meta_associative_container_traits;
  9435. /**
  9436. * @brief Provides the member constant `value` to true if support for meta
  9437. * sequence containers is enabled for the given type, false otherwise.
  9438. * @tparam Type Potentially sequence container type.
  9439. */
  9440. template<typename Type, typename = void>
  9441. struct has_meta_sequence_container_traits: std::false_type {};
  9442. /*! @copydoc has_meta_sequence_container_traits */
  9443. template<typename Type>
  9444. struct has_meta_sequence_container_traits<Type, std::void_t<typename meta_sequence_container_traits<Type>::value_type>>
  9445. : std::true_type
  9446. {};
  9447. /**
  9448. * @brief Helper variable template.
  9449. * @tparam Type Potentially sequence container type.
  9450. */
  9451. template<typename Type>
  9452. inline constexpr auto has_meta_sequence_container_traits_v = has_meta_sequence_container_traits<Type>::value;
  9453. /**
  9454. * @brief Provides the member constant `value` to true if support for meta
  9455. * associative containers is enabled for the given type, false otherwise.
  9456. * @tparam Type Potentially associative container type.
  9457. */
  9458. template<typename, typename = void>
  9459. struct has_meta_associative_container_traits: std::false_type {};
  9460. /*! @copydoc has_meta_associative_container_traits */
  9461. template<typename Type>
  9462. struct has_meta_associative_container_traits<Type, std::void_t<typename meta_associative_container_traits<Type>::key_type>>
  9463. : std::true_type
  9464. {};
  9465. /**
  9466. * @brief Helper variable template.
  9467. * @tparam Type Potentially associative container type.
  9468. */
  9469. template<typename Type>
  9470. inline constexpr auto has_meta_associative_container_traits_v = has_meta_associative_container_traits<Type>::value;
  9471. /**
  9472. * @brief Provides the member constant `value` to true if a meta associative
  9473. * container claims to wrap a key-only type, false otherwise.
  9474. * @tparam Type Potentially key-only meta associative container type.
  9475. */
  9476. template<typename, typename = void>
  9477. struct is_key_only_meta_associative_container: std::true_type {};
  9478. /*! @copydoc is_key_only_meta_associative_container */
  9479. template<typename Type>
  9480. struct is_key_only_meta_associative_container<Type, std::void_t<typename meta_associative_container_traits<Type>::mapped_type>>
  9481. : std::false_type
  9482. {};
  9483. /**
  9484. * @brief Helper variable template.
  9485. * @tparam Type Potentially key-only meta associative container type.
  9486. */
  9487. template<typename Type>
  9488. inline constexpr auto is_key_only_meta_associative_container_v = is_key_only_meta_associative_container<Type>::value;
  9489. /**
  9490. * @brief Provides the member constant `value` to true if a given type is a
  9491. * pointer-like type from the point of view of the meta system, false otherwise.
  9492. * @tparam Type Potentially pointer-like type.
  9493. */
  9494. template<typename>
  9495. struct is_meta_pointer_like: std::false_type {};
  9496. /**
  9497. * @brief Helper variable template.
  9498. * @tparam Type Potentially pointer-like type.
  9499. */
  9500. template<typename Type>
  9501. inline constexpr auto is_meta_pointer_like_v = is_meta_pointer_like<Type>::value;
  9502. }
  9503. #endif
  9504. namespace entt {
  9505. namespace internal {
  9506. template<typename Container, template<typename> class... Trait>
  9507. struct container_traits: public Trait<Container>... {};
  9508. /**
  9509. * @brief Basic STL-compatible container traits
  9510. * @tparam Container The type of the container.
  9511. */
  9512. template<typename Container>
  9513. struct basic_container {
  9514. /*! @brief Iterator type of the container. */
  9515. using iterator = typename Container::iterator;
  9516. /*! @brief Unsigned integer type. */
  9517. using size_type = typename Container::size_type;
  9518. /*! @brief Value type of the container. */
  9519. using value_type = typename Container::value_type;
  9520. /**
  9521. * @brief Returns the size of the given container.
  9522. * @param cont The container for which to return the size.
  9523. * @return The size of the given container.
  9524. */
  9525. [[nodiscard]] static size_type size(const Container &cont) ENTT_NOEXCEPT {
  9526. return cont.size();
  9527. }
  9528. /**
  9529. * @brief Returns an iterator to the first element of the given container.
  9530. * @param cont The container for which to return the iterator.
  9531. * @return An iterator to the first element of the given container.
  9532. */
  9533. [[nodiscard]] static iterator begin(Container &cont) {
  9534. return cont.begin();
  9535. }
  9536. /**
  9537. * @brief Returns an iterator past the last element of the given container.
  9538. * @param cont The container for which to return the iterator.
  9539. * @return An iterator past the last element of the given container.
  9540. */
  9541. [[nodiscard]] static iterator end(Container &cont) {
  9542. return cont.end();
  9543. }
  9544. };
  9545. /**
  9546. * @brief Basic STL-compatible associative container traits
  9547. * @tparam Container The type of the container.
  9548. */
  9549. template<typename Container>
  9550. struct basic_associative_container {
  9551. /*! @brief Key type of the sequence container. */
  9552. using key_type = typename Container::key_type;
  9553. /**
  9554. * @brief Returns an iterator to the element with key equivalent to the given
  9555. * one, if any.
  9556. * @param cont The container in which to search for the element.
  9557. * @param key The key of the element to search.
  9558. * @return An iterator to the element with the given key, if any.
  9559. */
  9560. [[nodiscard]] static typename Container::iterator find(Container &cont, const key_type &key) {
  9561. return cont.find(key);
  9562. }
  9563. };
  9564. /**
  9565. * @brief Basic STL-compatible dynamic container traits
  9566. * @tparam Container The type of the container.
  9567. */
  9568. template<typename Container>
  9569. struct basic_dynamic_container {
  9570. /**
  9571. * @brief Clears the content of the given container.
  9572. * @param cont The container for which to clear the content.
  9573. * @return True in case of success, false otherwise.
  9574. */
  9575. [[nodiscard]] static bool clear(Container &cont) {
  9576. return cont.clear(), true;
  9577. }
  9578. };
  9579. /**
  9580. * @brief Basic STL-compatible dynamic associative container traits
  9581. * @tparam Container The type of the container.
  9582. */
  9583. template<typename Container>
  9584. struct basic_dynamic_associative_container {
  9585. /**
  9586. * @brief Removes the specified element from the given container.
  9587. * @param cont The container from which to remove the element.
  9588. * @param key The element to remove.
  9589. * @return A bool denoting whether the removal took place.
  9590. */
  9591. [[nodiscard]] static bool erase(Container &cont, const typename Container::key_type &key) {
  9592. const auto sz = cont.size();
  9593. return cont.erase(key) != sz;
  9594. }
  9595. };
  9596. /**
  9597. * @brief Basic STL-compatible sequence container traits
  9598. * @tparam Container The type of the container.
  9599. */
  9600. template<typename Container>
  9601. struct basic_sequence_container {
  9602. /**
  9603. * @brief Returns a reference to the element at the specified location of the
  9604. * given container (no bounds checking is performed).
  9605. * @param cont The container from which to get the element.
  9606. * @param pos The position of the element to return.
  9607. * @return A reference to the requested element.
  9608. */
  9609. [[nodiscard]] static typename Container::value_type & get(Container &cont, typename Container::size_type pos) {
  9610. return cont[pos];
  9611. }
  9612. };
  9613. /**
  9614. * @brief STL-compatible dynamic associative key-only container traits
  9615. * @tparam Container The type of the container.
  9616. */
  9617. template<typename Container>
  9618. struct dynamic_associative_key_only_container {
  9619. /**
  9620. * @brief Inserts an element into the given container.
  9621. * @param cont The container in which to insert the element.
  9622. * @param key The element to insert.
  9623. * @return A bool denoting whether the insertion took place.
  9624. */
  9625. [[nodiscard]] static bool insert(Container &cont, const typename Container::key_type &key) {
  9626. return cont.insert(key).second;
  9627. }
  9628. };
  9629. /**
  9630. * @brief STL-compatible dynamic key-value associative container traits
  9631. * @tparam Container The type of the container.
  9632. */
  9633. template<typename Container>
  9634. struct dynamic_associative_key_value_container {
  9635. /**
  9636. * @brief Inserts an element (a key/value pair) into the given container.
  9637. * @param cont The container in which to insert the element.
  9638. * @param key The key of the element to insert.
  9639. * @param value The value of the element to insert.
  9640. * @return A bool denoting whether the insertion took place.
  9641. */
  9642. [[nodiscard]] static bool insert(Container &cont, const typename Container::key_type &key, const typename Container::mapped_type &value) {
  9643. return cont.insert(std::make_pair(key, value)).second;
  9644. }
  9645. };
  9646. /**
  9647. * @brief STL-compatible dynamic sequence container traits
  9648. * @tparam Container The type of the container.
  9649. */
  9650. template<typename Container>
  9651. struct dynamic_sequence_container {
  9652. /**
  9653. * @brief Resizes the given container to contain the given number of elements.
  9654. * @param cont The container to resize.
  9655. * @param sz The new size of the container.
  9656. * @return True in case of success, false otherwise.
  9657. */
  9658. [[nodiscard]] static bool resize(Container &cont, typename Container::size_type sz) {
  9659. return (cont.resize(sz), true);
  9660. }
  9661. /**
  9662. * @brief Inserts an element at the specified location of the given container.
  9663. * @param cont The container into which to insert the element.
  9664. * @param it Iterator before which the element will be inserted.
  9665. * @param value Element value to insert.
  9666. * @return A pair consisting of an iterator to the inserted element (in case
  9667. * of success) and a bool denoting whether the insertion took place.
  9668. */
  9669. [[nodiscard]] static std::pair<typename Container::iterator, bool> insert(Container &cont, typename Container::iterator it, const typename Container::value_type &value) {
  9670. return { cont.insert(it, value), true };
  9671. }
  9672. /**
  9673. * @brief Removes the element at the specified location from the given container.
  9674. * @param cont The container from which to remove the element.
  9675. * @param it Iterator to the element to remove.
  9676. * @return A pair consisting of an iterator following the last removed
  9677. * element (in case of success) and a bool denoting whether the insertion
  9678. * took place.
  9679. */
  9680. [[nodiscard]] static std::pair<typename Container::iterator, bool> erase(Container &cont, typename Container::iterator it) {
  9681. return { cont.erase(it), true };
  9682. }
  9683. };
  9684. /**
  9685. * @brief STL-compatible fixed sequence container traits
  9686. * @tparam Container The type of the container.
  9687. */
  9688. template<typename Container>
  9689. struct fixed_sequence_container {
  9690. /**
  9691. * @brief Does nothing.
  9692. * @return False to indicate failure in all cases.
  9693. */
  9694. [[nodiscard]] static bool resize(const Container &, typename Container::size_type) {
  9695. return false;
  9696. }
  9697. /**
  9698. * @brief Does nothing.
  9699. * @return False to indicate failure in all cases.
  9700. */
  9701. [[nodiscard]] static bool clear(const Container &) {
  9702. return false;
  9703. }
  9704. /**
  9705. * @brief Does nothing.
  9706. * @return A pair consisting of an invalid iterator and a false value to
  9707. * indicate failure in all cases.
  9708. */
  9709. [[nodiscard]] static std::pair<typename Container::iterator, bool> insert(const Container &, typename Container::iterator, const typename Container::value_type &) {
  9710. return { {}, false };
  9711. }
  9712. /**
  9713. * @brief Does nothing.
  9714. * @return A pair consisting of an invalid iterator and a false value to
  9715. * indicate failure in all cases.
  9716. */
  9717. [[nodiscard]] static std::pair<typename Container::iterator, bool> erase(const Container &, typename Container::iterator) {
  9718. return { {}, false };
  9719. }
  9720. };
  9721. }
  9722. /**
  9723. * @brief Meta sequence container traits for `std::vector`s of any type.
  9724. * @tparam Type The type of elements.
  9725. * @tparam Args Other arguments.
  9726. */
  9727. template<typename Type, typename... Args>
  9728. struct meta_sequence_container_traits<std::vector<Type, Args...>>
  9729. : internal::container_traits<
  9730. std::vector<Type, Args...>,
  9731. internal::basic_container,
  9732. internal::basic_dynamic_container,
  9733. internal::basic_sequence_container,
  9734. internal::dynamic_sequence_container
  9735. >
  9736. {};
  9737. /**
  9738. * @brief Meta sequence container traits for `std::array`s of any type.
  9739. * @tparam Type The type of elements.
  9740. * @tparam N The number of elements.
  9741. */
  9742. template<typename Type, auto N>
  9743. struct meta_sequence_container_traits<std::array<Type, N>>
  9744. : internal::container_traits<
  9745. std::array<Type, N>,
  9746. internal::basic_container,
  9747. internal::basic_sequence_container,
  9748. internal::fixed_sequence_container
  9749. >
  9750. {};
  9751. /**
  9752. * @brief Meta associative container traits for `std::map`s of any type.
  9753. * @tparam Key The key type of elements.
  9754. * @tparam Value The value type of elements.
  9755. * @tparam Args Other arguments.
  9756. */
  9757. template<typename Key, typename Value, typename... Args>
  9758. struct meta_associative_container_traits<std::map<Key, Value, Args...>>
  9759. : internal::container_traits<
  9760. std::map<Key, Value, Args...>,
  9761. internal::basic_container,
  9762. internal::basic_associative_container,
  9763. internal::basic_dynamic_container,
  9764. internal::basic_dynamic_associative_container,
  9765. internal::dynamic_associative_key_value_container
  9766. >
  9767. {
  9768. /*! @brief Mapped type of the sequence container. */
  9769. using mapped_type = typename std::map<Key, Value, Args...>::mapped_type;
  9770. };
  9771. /**
  9772. * @brief Meta associative container traits for `std::unordered_map`s of any
  9773. * type.
  9774. * @tparam Key The key type of elements.
  9775. * @tparam Value The value type of elements.
  9776. * @tparam Args Other arguments.
  9777. */
  9778. template<typename Key, typename Value, typename... Args>
  9779. struct meta_associative_container_traits<std::unordered_map<Key, Value, Args...>>
  9780. : internal::container_traits<
  9781. std::unordered_map<Key, Value, Args...>,
  9782. internal::basic_container,
  9783. internal::basic_associative_container,
  9784. internal::basic_dynamic_container,
  9785. internal::basic_dynamic_associative_container,
  9786. internal::dynamic_associative_key_value_container
  9787. >
  9788. {
  9789. /*! @brief Mapped type of the sequence container. */
  9790. using mapped_type = typename std::unordered_map<Key, Value, Args...>::mapped_type;
  9791. };
  9792. /**
  9793. * @brief Meta associative container traits for `std::set`s of any type.
  9794. * @tparam Key The type of elements.
  9795. * @tparam Args Other arguments.
  9796. */
  9797. template<typename Key, typename... Args>
  9798. struct meta_associative_container_traits<std::set<Key, Args...>>
  9799. : internal::container_traits<
  9800. std::set<Key, Args...>,
  9801. internal::basic_container,
  9802. internal::basic_associative_container,
  9803. internal::basic_dynamic_container,
  9804. internal::basic_dynamic_associative_container,
  9805. internal::dynamic_associative_key_only_container
  9806. >
  9807. {};
  9808. /**
  9809. * @brief Meta associative container traits for `std::unordered_set`s of any
  9810. * type.
  9811. * @tparam Key The type of elements.
  9812. * @tparam Args Other arguments.
  9813. */
  9814. template<typename Key, typename... Args>
  9815. struct meta_associative_container_traits<std::unordered_set<Key, Args...>>
  9816. : internal::container_traits<
  9817. std::unordered_set<Key, Args...>,
  9818. internal::basic_container,
  9819. internal::basic_associative_container,
  9820. internal::basic_dynamic_container,
  9821. internal::basic_dynamic_associative_container,
  9822. internal::dynamic_associative_key_only_container
  9823. >
  9824. {};
  9825. }
  9826. #endif
  9827. // #include "meta/ctx.hpp"
  9828. #ifndef ENTT_META_CTX_HPP
  9829. #define ENTT_META_CTX_HPP
  9830. // #include "../core/attribute.h"
  9831. #ifndef ENTT_CORE_ATTRIBUTE_H
  9832. #define ENTT_CORE_ATTRIBUTE_H
  9833. #ifndef ENTT_EXPORT
  9834. # if defined _WIN32 || defined __CYGWIN__ || defined _MSC_VER
  9835. # define ENTT_EXPORT __declspec(dllexport)
  9836. # define ENTT_IMPORT __declspec(dllimport)
  9837. # define ENTT_HIDDEN
  9838. # elif defined __GNUC__ && __GNUC__ >= 4
  9839. # define ENTT_EXPORT __attribute__((visibility("default")))
  9840. # define ENTT_IMPORT __attribute__((visibility("default")))
  9841. # define ENTT_HIDDEN __attribute__((visibility("hidden")))
  9842. # else /* Unsupported compiler */
  9843. # define ENTT_EXPORT
  9844. # define ENTT_IMPORT
  9845. # define ENTT_HIDDEN
  9846. # endif
  9847. #endif
  9848. #ifndef ENTT_API
  9849. # if defined ENTT_API_EXPORT
  9850. # define ENTT_API ENTT_EXPORT
  9851. # elif defined ENTT_API_IMPORT
  9852. # define ENTT_API ENTT_IMPORT
  9853. # else /* No API */
  9854. # define ENTT_API
  9855. # endif
  9856. #endif
  9857. #endif
  9858. // #include "../config/config.h"
  9859. namespace entt {
  9860. /**
  9861. * @cond TURN_OFF_DOXYGEN
  9862. * Internal details not to be documented.
  9863. */
  9864. namespace internal {
  9865. struct meta_type_node;
  9866. struct ENTT_API meta_context {
  9867. // we could use the lines below but VS2017 returns with an ICE if combined with ENTT_API despite the code being valid C++
  9868. // inline static meta_type_node *local = nullptr;
  9869. // inline static meta_type_node **global = &local;
  9870. [[nodiscard]] static meta_type_node * & local() ENTT_NOEXCEPT {
  9871. static meta_type_node *chain = nullptr;
  9872. return chain;
  9873. }
  9874. [[nodiscard]] static meta_type_node ** & global() ENTT_NOEXCEPT {
  9875. static meta_type_node **chain = &local();
  9876. return chain;
  9877. }
  9878. };
  9879. }
  9880. /**
  9881. * Internal details not to be documented.
  9882. * @endcond
  9883. */
  9884. /*! @brief Opaque container for a meta context. */
  9885. struct meta_ctx {
  9886. /**
  9887. * @brief Binds the meta system to a given context.
  9888. * @param other A valid context to which to bind.
  9889. */
  9890. static void bind(meta_ctx other) ENTT_NOEXCEPT {
  9891. internal::meta_context::global() = other.ctx;
  9892. }
  9893. private:
  9894. internal::meta_type_node **ctx{&internal::meta_context::local()};
  9895. };
  9896. }
  9897. #endif
  9898. // #include "meta/factory.hpp"
  9899. #ifndef ENTT_META_FACTORY_HPP
  9900. #define ENTT_META_FACTORY_HPP
  9901. #include <array>
  9902. #include <cstddef>
  9903. #include <functional>
  9904. #include <tuple>
  9905. #include <type_traits>
  9906. #include <utility>
  9907. // #include "../config/config.h"
  9908. // #include "../core/fwd.hpp"
  9909. #ifndef ENTT_CORE_FWD_HPP
  9910. #define ENTT_CORE_FWD_HPP
  9911. // #include "../config/config.h"
  9912. #ifndef ENTT_CONFIG_CONFIG_H
  9913. #define ENTT_CONFIG_CONFIG_H
  9914. #ifndef ENTT_NOEXCEPT
  9915. # define ENTT_NOEXCEPT noexcept
  9916. #endif
  9917. #ifndef ENTT_HS_SUFFIX
  9918. # define ENTT_HS_SUFFIX _hs
  9919. #endif
  9920. #ifndef ENTT_HWS_SUFFIX
  9921. # define ENTT_HWS_SUFFIX _hws
  9922. #endif
  9923. #ifndef ENTT_USE_ATOMIC
  9924. # define ENTT_MAYBE_ATOMIC(Type) Type
  9925. #else
  9926. # include <atomic>
  9927. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  9928. #endif
  9929. #ifndef ENTT_ID_TYPE
  9930. # include <cstdint>
  9931. # define ENTT_ID_TYPE std::uint32_t
  9932. #endif
  9933. #ifndef ENTT_PAGE_SIZE
  9934. # define ENTT_PAGE_SIZE 32768
  9935. #endif
  9936. #ifndef ENTT_ASSERT
  9937. # include <cassert>
  9938. # define ENTT_ASSERT(condition) assert(condition)
  9939. #endif
  9940. #ifndef ENTT_NO_ETO
  9941. # include <type_traits>
  9942. # define ENTT_IS_EMPTY(Type) std::is_empty<Type>
  9943. #else
  9944. # include <type_traits>
  9945. # define ENTT_IS_EMPTY(Type) std::false_type
  9946. #endif
  9947. #ifndef ENTT_STANDARD_CPP
  9948. # if defined __clang__ || (defined __GNUC__ && __GNUC__ > 8)
  9949. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  9950. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  9951. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  9952. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  9953. # elif defined __GNUC__
  9954. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  9955. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  9956. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  9957. # elif defined _MSC_VER
  9958. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  9959. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  9960. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  9961. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  9962. # endif
  9963. #endif
  9964. #ifndef ENTT_STANDALONE
  9965. # define ENTT_FAST_PATH(...) false
  9966. #else
  9967. # define ENTT_FAST_PATH(Cond) Cond
  9968. #endif
  9969. #endif
  9970. namespace entt {
  9971. /*! @brief Alias declaration for type identifiers. */
  9972. using id_type = ENTT_ID_TYPE;
  9973. }
  9974. #endif
  9975. // #include "../core/type_info.hpp"
  9976. #ifndef ENTT_CORE_TYPE_INFO_HPP
  9977. #define ENTT_CORE_TYPE_INFO_HPP
  9978. #include <string_view>
  9979. // #include "../config/config.h"
  9980. // #include "../core/attribute.h"
  9981. #ifndef ENTT_CORE_ATTRIBUTE_H
  9982. #define ENTT_CORE_ATTRIBUTE_H
  9983. #ifndef ENTT_EXPORT
  9984. # if defined _WIN32 || defined __CYGWIN__ || defined _MSC_VER
  9985. # define ENTT_EXPORT __declspec(dllexport)
  9986. # define ENTT_IMPORT __declspec(dllimport)
  9987. # define ENTT_HIDDEN
  9988. # elif defined __GNUC__ && __GNUC__ >= 4
  9989. # define ENTT_EXPORT __attribute__((visibility("default")))
  9990. # define ENTT_IMPORT __attribute__((visibility("default")))
  9991. # define ENTT_HIDDEN __attribute__((visibility("hidden")))
  9992. # else /* Unsupported compiler */
  9993. # define ENTT_EXPORT
  9994. # define ENTT_IMPORT
  9995. # define ENTT_HIDDEN
  9996. # endif
  9997. #endif
  9998. #ifndef ENTT_API
  9999. # if defined ENTT_API_EXPORT
  10000. # define ENTT_API ENTT_EXPORT
  10001. # elif defined ENTT_API_IMPORT
  10002. # define ENTT_API ENTT_IMPORT
  10003. # else /* No API */
  10004. # define ENTT_API
  10005. # endif
  10006. #endif
  10007. #endif
  10008. // #include "hashed_string.hpp"
  10009. #ifndef ENTT_CORE_HASHED_STRING_HPP
  10010. #define ENTT_CORE_HASHED_STRING_HPP
  10011. #include <cstddef>
  10012. #include <cstdint>
  10013. // #include "../config/config.h"
  10014. // #include "fwd.hpp"
  10015. namespace entt {
  10016. /**
  10017. * @cond TURN_OFF_DOXYGEN
  10018. * Internal details not to be documented.
  10019. */
  10020. namespace internal {
  10021. template<typename>
  10022. struct fnv1a_traits;
  10023. template<>
  10024. struct fnv1a_traits<std::uint32_t> {
  10025. using type = std::uint32_t;
  10026. static constexpr std::uint32_t offset = 2166136261;
  10027. static constexpr std::uint32_t prime = 16777619;
  10028. };
  10029. template<>
  10030. struct fnv1a_traits<std::uint64_t> {
  10031. using type = std::uint64_t;
  10032. static constexpr std::uint64_t offset = 14695981039346656037ull;
  10033. static constexpr std::uint64_t prime = 1099511628211ull;
  10034. };
  10035. }
  10036. /**
  10037. * Internal details not to be documented.
  10038. * @endcond
  10039. */
  10040. /**
  10041. * @brief Zero overhead unique identifier.
  10042. *
  10043. * A hashed string is a compile-time tool that allows users to use
  10044. * human-readable identifers in the codebase while using their numeric
  10045. * counterparts at runtime.<br/>
  10046. * Because of that, a hashed string can also be used in constant expressions if
  10047. * required.
  10048. *
  10049. * @tparam Char Character type.
  10050. */
  10051. template<typename Char>
  10052. class basic_hashed_string {
  10053. using traits_type = internal::fnv1a_traits<id_type>;
  10054. struct const_wrapper {
  10055. // non-explicit constructor on purpose
  10056. constexpr const_wrapper(const Char *curr) ENTT_NOEXCEPT: str{curr} {}
  10057. const Char *str;
  10058. };
  10059. // Fowler–Noll–Vo hash function v. 1a - the good
  10060. [[nodiscard]] static constexpr id_type helper(const Char *curr) ENTT_NOEXCEPT {
  10061. auto value = traits_type::offset;
  10062. while(*curr != 0) {
  10063. value = (value ^ static_cast<traits_type::type>(*(curr++))) * traits_type::prime;
  10064. }
  10065. return value;
  10066. }
  10067. public:
  10068. /*! @brief Character type. */
  10069. using value_type = Char;
  10070. /*! @brief Unsigned integer type. */
  10071. using hash_type = id_type;
  10072. /**
  10073. * @brief Returns directly the numeric representation of a string.
  10074. *
  10075. * Forcing template resolution avoids implicit conversions. An
  10076. * human-readable identifier can be anything but a plain, old bunch of
  10077. * characters.<br/>
  10078. * Example of use:
  10079. * @code{.cpp}
  10080. * const auto value = basic_hashed_string<char>::to_value("my.png");
  10081. * @endcode
  10082. *
  10083. * @tparam N Number of characters of the identifier.
  10084. * @param str Human-readable identifer.
  10085. * @return The numeric representation of the string.
  10086. */
  10087. template<std::size_t N>
  10088. [[nodiscard]] static constexpr hash_type value(const value_type (&str)[N]) ENTT_NOEXCEPT {
  10089. return helper(str);
  10090. }
  10091. /**
  10092. * @brief Returns directly the numeric representation of a string.
  10093. * @param wrapper Helps achieving the purpose by relying on overloading.
  10094. * @return The numeric representation of the string.
  10095. */
  10096. [[nodiscard]] static hash_type value(const_wrapper wrapper) ENTT_NOEXCEPT {
  10097. return helper(wrapper.str);
  10098. }
  10099. /**
  10100. * @brief Returns directly the numeric representation of a string view.
  10101. * @param str Human-readable identifer.
  10102. * @param size Length of the string to hash.
  10103. * @return The numeric representation of the string.
  10104. */
  10105. [[nodiscard]] static hash_type value(const value_type *str, std::size_t size) ENTT_NOEXCEPT {
  10106. id_type partial{traits_type::offset};
  10107. while(size--) { partial = (partial^(str++)[0])*traits_type::prime; }
  10108. return partial;
  10109. }
  10110. /*! @brief Constructs an empty hashed string. */
  10111. constexpr basic_hashed_string() ENTT_NOEXCEPT
  10112. : str{nullptr}, hash{}
  10113. {}
  10114. /**
  10115. * @brief Constructs a hashed string from an array of const characters.
  10116. *
  10117. * Forcing template resolution avoids implicit conversions. An
  10118. * human-readable identifier can be anything but a plain, old bunch of
  10119. * characters.<br/>
  10120. * Example of use:
  10121. * @code{.cpp}
  10122. * basic_hashed_string<char> hs{"my.png"};
  10123. * @endcode
  10124. *
  10125. * @tparam N Number of characters of the identifier.
  10126. * @param curr Human-readable identifer.
  10127. */
  10128. template<std::size_t N>
  10129. constexpr basic_hashed_string(const value_type (&curr)[N]) ENTT_NOEXCEPT
  10130. : str{curr}, hash{helper(curr)}
  10131. {}
  10132. /**
  10133. * @brief Explicit constructor on purpose to avoid constructing a hashed
  10134. * string directly from a `const value_type *`.
  10135. * @param wrapper Helps achieving the purpose by relying on overloading.
  10136. */
  10137. explicit constexpr basic_hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
  10138. : str{wrapper.str}, hash{helper(wrapper.str)}
  10139. {}
  10140. /**
  10141. * @brief Returns the human-readable representation of a hashed string.
  10142. * @return The string used to initialize the instance.
  10143. */
  10144. [[nodiscard]] constexpr const value_type * data() const ENTT_NOEXCEPT {
  10145. return str;
  10146. }
  10147. /**
  10148. * @brief Returns the numeric representation of a hashed string.
  10149. * @return The numeric representation of the instance.
  10150. */
  10151. [[nodiscard]] constexpr hash_type value() const ENTT_NOEXCEPT {
  10152. return hash;
  10153. }
  10154. /*! @copydoc data */
  10155. [[nodiscard]] constexpr operator const value_type *() const ENTT_NOEXCEPT { return data(); }
  10156. /**
  10157. * @brief Returns the numeric representation of a hashed string.
  10158. * @return The numeric representation of the instance.
  10159. */
  10160. [[nodiscard]] constexpr operator hash_type() const ENTT_NOEXCEPT { return value(); }
  10161. /**
  10162. * @brief Compares two hashed strings.
  10163. * @param other Hashed string with which to compare.
  10164. * @return True if the two hashed strings are identical, false otherwise.
  10165. */
  10166. [[nodiscard]] constexpr bool operator==(const basic_hashed_string &other) const ENTT_NOEXCEPT {
  10167. return hash == other.hash;
  10168. }
  10169. private:
  10170. const value_type *str;
  10171. hash_type hash;
  10172. };
  10173. /**
  10174. * @brief Deduction guide.
  10175. *
  10176. * It allows to deduce the character type of the hashed string directly from a
  10177. * human-readable identifer provided to the constructor.
  10178. *
  10179. * @tparam Char Character type.
  10180. * @tparam N Number of characters of the identifier.
  10181. * @param str Human-readable identifer.
  10182. */
  10183. template<typename Char, std::size_t N>
  10184. basic_hashed_string(const Char (&str)[N]) ENTT_NOEXCEPT
  10185. -> basic_hashed_string<Char>;
  10186. /**
  10187. * @brief Compares two hashed strings.
  10188. * @tparam Char Character type.
  10189. * @param lhs A valid hashed string.
  10190. * @param rhs A valid hashed string.
  10191. * @return True if the two hashed strings are identical, false otherwise.
  10192. */
  10193. template<typename Char>
  10194. [[nodiscard]] constexpr bool operator!=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) ENTT_NOEXCEPT {
  10195. return !(lhs == rhs);
  10196. }
  10197. /*! @brief Aliases for common character types. */
  10198. using hashed_string = basic_hashed_string<char>;
  10199. /*! @brief Aliases for common character types. */
  10200. using hashed_wstring = basic_hashed_string<wchar_t>;
  10201. }
  10202. /**
  10203. * @brief User defined literal for hashed strings.
  10204. * @param str The literal without its suffix.
  10205. * @return A properly initialized hashed string.
  10206. */
  10207. [[nodiscard]] constexpr entt::hashed_string operator"" ENTT_HS_SUFFIX(const char *str, std::size_t) ENTT_NOEXCEPT {
  10208. return entt::hashed_string{str};
  10209. }
  10210. /**
  10211. * @brief User defined literal for hashed wstrings.
  10212. * @param str The literal without its suffix.
  10213. * @return A properly initialized hashed wstring.
  10214. */
  10215. [[nodiscard]] constexpr entt::hashed_wstring operator"" ENTT_HWS_SUFFIX(const wchar_t *str, std::size_t) ENTT_NOEXCEPT {
  10216. return entt::hashed_wstring{str};
  10217. }
  10218. #endif
  10219. // #include "fwd.hpp"
  10220. namespace entt {
  10221. /**
  10222. * @cond TURN_OFF_DOXYGEN
  10223. * Internal details not to be documented.
  10224. */
  10225. namespace internal {
  10226. struct ENTT_API type_index {
  10227. [[nodiscard]] static id_type next() ENTT_NOEXCEPT {
  10228. static ENTT_MAYBE_ATOMIC(id_type) value{};
  10229. return value++;
  10230. }
  10231. };
  10232. template<typename Type>
  10233. [[nodiscard]] constexpr auto type_name() ENTT_NOEXCEPT {
  10234. #if defined ENTT_PRETTY_FUNCTION
  10235. std::string_view pretty_function{ENTT_PRETTY_FUNCTION};
  10236. auto first = pretty_function.find_first_not_of(' ', pretty_function.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX)+1);
  10237. auto value = pretty_function.substr(first, pretty_function.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
  10238. return value;
  10239. #else
  10240. return std::string_view{};
  10241. #endif
  10242. }
  10243. }
  10244. /**
  10245. * Internal details not to be documented.
  10246. * @endcond
  10247. */
  10248. /**
  10249. * @brief Type index.
  10250. * @tparam Type Type for which to generate a sequential identifier.
  10251. */
  10252. template<typename Type, typename = void>
  10253. struct ENTT_API type_index {
  10254. /**
  10255. * @brief Returns the sequential identifier of a given type.
  10256. * @return The sequential identifier of a given type.
  10257. */
  10258. [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
  10259. static const id_type value = internal::type_index::next();
  10260. return value;
  10261. }
  10262. };
  10263. /**
  10264. * @brief Provides the member constant `value` to true if a given type is
  10265. * indexable, false otherwise.
  10266. * @tparam Type Potentially indexable type.
  10267. */
  10268. template<typename, typename = void>
  10269. struct has_type_index: std::false_type {};
  10270. /*! @brief has_type_index */
  10271. template<typename Type>
  10272. struct has_type_index<Type, std::void_t<decltype(type_index<Type>::value())>>: std::true_type {};
  10273. /**
  10274. * @brief Helper variable template.
  10275. * @tparam Type Potentially indexable type.
  10276. */
  10277. template<typename Type>
  10278. inline constexpr bool has_type_index_v = has_type_index<Type>::value;
  10279. /**
  10280. * @brief Type info.
  10281. * @tparam Type Type for which to generate information.
  10282. */
  10283. template<typename Type, typename = void>
  10284. struct type_info {
  10285. /**
  10286. * @brief Returns the numeric representation of a given type.
  10287. * @return The numeric representation of the given type.
  10288. */
  10289. #if defined ENTT_PRETTY_FUNCTION_CONSTEXPR
  10290. [[nodiscard]] static constexpr id_type id() ENTT_NOEXCEPT {
  10291. constexpr auto value = hashed_string::value(ENTT_PRETTY_FUNCTION);
  10292. return value;
  10293. }
  10294. #elif defined ENTT_PRETTY_FUNCTION
  10295. [[nodiscard]] static id_type id() ENTT_NOEXCEPT {
  10296. static const auto value = hashed_string::value(ENTT_PRETTY_FUNCTION);
  10297. return value;
  10298. }
  10299. #else
  10300. [[nodiscard]] static id_type id() ENTT_NOEXCEPT {
  10301. return type_index<Type>::value();
  10302. }
  10303. #endif
  10304. /**
  10305. * @brief Returns the name of a given type.
  10306. * @return The name of the given type.
  10307. */
  10308. #if defined ENTT_PRETTY_FUNCTION_CONSTEXPR
  10309. [[nodiscard]] static constexpr std::string_view name() ENTT_NOEXCEPT {
  10310. constexpr auto value = internal::type_name<Type>();
  10311. return value;
  10312. }
  10313. #elif defined ENTT_PRETTY_FUNCTION
  10314. [[nodiscard]] static std::string_view name() ENTT_NOEXCEPT {
  10315. static const auto value = internal::type_name<Type>();
  10316. return value;
  10317. }
  10318. #else
  10319. [[nodiscard]] static constexpr std::string_view name() ENTT_NOEXCEPT {
  10320. return internal::type_name<Type>();
  10321. }
  10322. #endif
  10323. };
  10324. }
  10325. #endif
  10326. // #include "../core/type_traits.hpp"
  10327. #ifndef ENTT_CORE_TYPE_TRAITS_HPP
  10328. #define ENTT_CORE_TYPE_TRAITS_HPP
  10329. #include <cstddef>
  10330. #include <utility>
  10331. #include <type_traits>
  10332. // #include "../config/config.h"
  10333. // #include "fwd.hpp"
  10334. namespace entt {
  10335. /**
  10336. * @brief Using declaration to be used to _repeat_ the same type a number of
  10337. * times equal to the size of a given parameter pack.
  10338. * @tparam Type A type to repeat.
  10339. */
  10340. template<typename Type, typename>
  10341. using unpack_as_t = Type;
  10342. /**
  10343. * @brief Helper variable template to be used to _repeat_ the same value a
  10344. * number of times equal to the size of a given parameter pack.
  10345. * @tparam Value A value to repeat.
  10346. */
  10347. template<auto Value, typename>
  10348. inline constexpr auto unpack_as_v = Value;
  10349. /**
  10350. * @brief Wraps a static constant.
  10351. * @tparam Value A static constant.
  10352. */
  10353. template<auto Value>
  10354. using integral_constant = std::integral_constant<decltype(Value), Value>;
  10355. /**
  10356. * @brief Alias template to ease the creation of named values.
  10357. * @tparam Value A constant value at least convertible to `id_type`.
  10358. */
  10359. template<id_type Value>
  10360. using tag = integral_constant<Value>;
  10361. /**
  10362. * @brief Utility class to disambiguate overloaded functions.
  10363. * @tparam N Number of choices available.
  10364. */
  10365. template<std::size_t N>
  10366. struct choice_t
  10367. // Unfortunately, doxygen cannot parse such a construct.
  10368. /*! @cond TURN_OFF_DOXYGEN */
  10369. : choice_t<N-1>
  10370. /*! @endcond */
  10371. {};
  10372. /*! @copybrief choice_t */
  10373. template<>
  10374. struct choice_t<0> {};
  10375. /**
  10376. * @brief Variable template for the choice trick.
  10377. * @tparam N Number of choices available.
  10378. */
  10379. template<std::size_t N>
  10380. inline constexpr choice_t<N> choice{};
  10381. /*! @brief A class to use to push around lists of types, nothing more. */
  10382. template<typename...>
  10383. struct type_list {};
  10384. /*! @brief Primary template isn't defined on purpose. */
  10385. template<typename>
  10386. struct type_list_size;
  10387. /**
  10388. * @brief Compile-time number of elements in a type list.
  10389. * @tparam Type Types provided by the type list.
  10390. */
  10391. template<typename... Type>
  10392. struct type_list_size<type_list<Type...>>
  10393. : std::integral_constant<std::size_t, sizeof...(Type)>
  10394. {};
  10395. /**
  10396. * @brief Helper variable template.
  10397. * @tparam List Type list.
  10398. */
  10399. template<class List>
  10400. inline constexpr auto type_list_size_v = type_list_size<List>::value;
  10401. /*! @brief Primary template isn't defined on purpose. */
  10402. template<typename...>
  10403. struct type_list_cat;
  10404. /*! @brief Concatenates multiple type lists. */
  10405. template<>
  10406. struct type_list_cat<> {
  10407. /*! @brief A type list composed by the types of all the type lists. */
  10408. using type = type_list<>;
  10409. };
  10410. /**
  10411. * @brief Concatenates multiple type lists.
  10412. * @tparam Type Types provided by the first type list.
  10413. * @tparam Other Types provided by the second type list.
  10414. * @tparam List Other type lists, if any.
  10415. */
  10416. template<typename... Type, typename... Other, typename... List>
  10417. struct type_list_cat<type_list<Type...>, type_list<Other...>, List...> {
  10418. /*! @brief A type list composed by the types of all the type lists. */
  10419. using type = typename type_list_cat<type_list<Type..., Other...>, List...>::type;
  10420. };
  10421. /**
  10422. * @brief Concatenates multiple type lists.
  10423. * @tparam Type Types provided by the type list.
  10424. */
  10425. template<typename... Type>
  10426. struct type_list_cat<type_list<Type...>> {
  10427. /*! @brief A type list composed by the types of all the type lists. */
  10428. using type = type_list<Type...>;
  10429. };
  10430. /**
  10431. * @brief Helper type.
  10432. * @tparam List Type lists to concatenate.
  10433. */
  10434. template<typename... List>
  10435. using type_list_cat_t = typename type_list_cat<List...>::type;
  10436. /*! @brief Primary template isn't defined on purpose. */
  10437. template<typename>
  10438. struct type_list_unique;
  10439. /**
  10440. * @brief Removes duplicates types from a type list.
  10441. * @tparam Type One of the types provided by the given type list.
  10442. * @tparam Other The other types provided by the given type list.
  10443. */
  10444. template<typename Type, typename... Other>
  10445. struct type_list_unique<type_list<Type, Other...>> {
  10446. /*! @brief A type list without duplicate types. */
  10447. using type = std::conditional_t<
  10448. std::disjunction_v<std::is_same<Type, Other>...>,
  10449. typename type_list_unique<type_list<Other...>>::type,
  10450. type_list_cat_t<type_list<Type>, typename type_list_unique<type_list<Other...>>::type>
  10451. >;
  10452. };
  10453. /*! @brief Removes duplicates types from a type list. */
  10454. template<>
  10455. struct type_list_unique<type_list<>> {
  10456. /*! @brief A type list without duplicate types. */
  10457. using type = type_list<>;
  10458. };
  10459. /**
  10460. * @brief Helper type.
  10461. * @tparam Type A type list.
  10462. */
  10463. template<typename Type>
  10464. using type_list_unique_t = typename type_list_unique<Type>::type;
  10465. /**
  10466. * @brief Provides the member constant `value` to true if a given type is
  10467. * equality comparable, false otherwise.
  10468. * @tparam Type Potentially equality comparable type.
  10469. */
  10470. template<typename Type, typename = std::void_t<>>
  10471. struct is_equality_comparable: std::false_type {};
  10472. /*! @copydoc is_equality_comparable */
  10473. template<typename Type>
  10474. struct is_equality_comparable<Type, std::void_t<decltype(std::declval<Type>() == std::declval<Type>())>>
  10475. : std::true_type
  10476. {};
  10477. /**
  10478. * @brief Helper variable template.
  10479. * @tparam Type Potentially equality comparable type.
  10480. */
  10481. template<class Type>
  10482. inline constexpr auto is_equality_comparable_v = is_equality_comparable<Type>::value;
  10483. /**
  10484. * @brief Provides the member constant `value` to true if a given type is empty
  10485. * and the empty type optimization is enabled, false otherwise.
  10486. * @tparam Type Potential empty type.
  10487. */
  10488. template<typename Type, typename = void>
  10489. struct is_eto_eligible
  10490. : ENTT_IS_EMPTY(Type)
  10491. {};
  10492. /**
  10493. * @brief Helper variable template.
  10494. * @tparam Type Potential empty type.
  10495. */
  10496. template<typename Type>
  10497. inline constexpr auto is_eto_eligible_v = is_eto_eligible<Type>::value;
  10498. /**
  10499. * @brief Extracts the class of a non-static member object or function.
  10500. * @tparam Member A pointer to a non-static member object or function.
  10501. */
  10502. template<typename Member>
  10503. class member_class {
  10504. static_assert(std::is_member_pointer_v<Member>, "Invalid pointer type to non-static member object or function");
  10505. template<typename Class, typename Ret, typename... Args>
  10506. static Class * clazz(Ret(Class:: *)(Args...));
  10507. template<typename Class, typename Ret, typename... Args>
  10508. static Class * clazz(Ret(Class:: *)(Args...) const);
  10509. template<typename Class, typename Type>
  10510. static Class * clazz(Type Class:: *);
  10511. public:
  10512. /*! @brief The class of the given non-static member object or function. */
  10513. using type = std::remove_pointer_t<decltype(clazz(std::declval<Member>()))>;
  10514. };
  10515. /**
  10516. * @brief Helper type.
  10517. * @tparam Member A pointer to a non-static member object or function.
  10518. */
  10519. template<typename Member>
  10520. using member_class_t = typename member_class<Member>::type;
  10521. }
  10522. #endif
  10523. // #include "internal.hpp"
  10524. #ifndef ENTT_META_INTERNAL_HPP
  10525. #define ENTT_META_INTERNAL_HPP
  10526. #include <cstddef>
  10527. #include <functional>
  10528. #include <iterator>
  10529. #include <type_traits>
  10530. #include <utility>
  10531. // #include "../core/attribute.h"
  10532. // #include "../config/config.h"
  10533. // #include "../core/fwd.hpp"
  10534. // #include "../core/type_info.hpp"
  10535. // #include "../core/type_traits.hpp"
  10536. // #include "type_traits.hpp"
  10537. namespace entt {
  10538. class meta_any;
  10539. struct meta_handle;
  10540. /**
  10541. * @cond TURN_OFF_DOXYGEN
  10542. * Internal details not to be documented.
  10543. */
  10544. namespace internal {
  10545. class meta_storage {
  10546. using storage_type = std::aligned_storage_t<sizeof(void *), alignof(void *)>;
  10547. using copy_fn_type = void(meta_storage &, const meta_storage &);
  10548. using steal_fn_type = void(meta_storage &, meta_storage &);
  10549. using destroy_fn_type = void(meta_storage &);
  10550. template<typename Type, typename = std::void_t<>>
  10551. struct type_traits {
  10552. template<typename... Args>
  10553. static void instance(meta_storage &buffer, Args &&... args) {
  10554. buffer.instance = new Type{std::forward<Args>(args)...};
  10555. new (&buffer.storage) Type *{static_cast<Type *>(buffer.instance)};
  10556. }
  10557. static void destroy(meta_storage &buffer) {
  10558. delete static_cast<Type *>(buffer.instance);
  10559. }
  10560. static void copy(meta_storage &to, const meta_storage &from) {
  10561. to.instance = new Type{*static_cast<const Type *>(from.instance)};
  10562. new (&to.storage) Type *{static_cast<Type *>(to.instance)};
  10563. }
  10564. static void steal(meta_storage &to, meta_storage &from) {
  10565. new (&to.storage) Type *{static_cast<Type *>(from.instance)};
  10566. to.instance = from.instance;
  10567. }
  10568. };
  10569. template<typename Type>
  10570. struct type_traits<Type, std::enable_if_t<sizeof(Type) <= sizeof(void *) && std::is_nothrow_move_constructible_v<Type>>> {
  10571. template<typename... Args>
  10572. static void instance(meta_storage &buffer, Args &&... args) {
  10573. buffer.instance = new (&buffer.storage) Type{std::forward<Args>(args)...};
  10574. }
  10575. static void destroy(meta_storage &buffer) {
  10576. static_cast<Type *>(buffer.instance)->~Type();
  10577. }
  10578. static void copy(meta_storage &to, const meta_storage &from) {
  10579. to.instance = new (&to.storage) Type{*static_cast<const Type *>(from.instance)};
  10580. }
  10581. static void steal(meta_storage &to, meta_storage &from) {
  10582. to.instance = new (&to.storage) Type{std::move(*static_cast<Type *>(from.instance))};
  10583. destroy(from);
  10584. }
  10585. };
  10586. public:
  10587. /*! @brief Default constructor. */
  10588. meta_storage() ENTT_NOEXCEPT
  10589. : storage{},
  10590. instance{},
  10591. destroy_fn{},
  10592. copy_fn{},
  10593. steal_fn{}
  10594. {}
  10595. template<typename Type, typename... Args>
  10596. explicit meta_storage(std::in_place_type_t<Type>, [[maybe_unused]] Args &&... args)
  10597. : meta_storage{}
  10598. {
  10599. if constexpr(!std::is_void_v<Type>) {
  10600. type_traits<Type>::instance(*this, std::forward<Args>(args)...);
  10601. destroy_fn = &type_traits<Type>::destroy;
  10602. copy_fn = &type_traits<Type>::copy;
  10603. steal_fn = &type_traits<Type>::steal;
  10604. }
  10605. }
  10606. template<typename Type>
  10607. meta_storage(std::reference_wrapper<Type> value)
  10608. : meta_storage{}
  10609. {
  10610. instance = &value.get();
  10611. }
  10612. template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::remove_cv_t<std::remove_reference_t<Type>>, meta_storage>>>
  10613. meta_storage(Type &&value)
  10614. : meta_storage{std::in_place_type<std::remove_cv_t<std::remove_reference_t<Type>>>, std::forward<Type>(value)}
  10615. {}
  10616. meta_storage(const meta_storage &other)
  10617. : meta_storage{}
  10618. {
  10619. (other.copy_fn ? other.copy_fn : [](auto &to, const auto &from) { to.instance = from.instance; })(*this, other);
  10620. destroy_fn = other.destroy_fn;
  10621. copy_fn = other.copy_fn;
  10622. steal_fn = other.steal_fn;
  10623. }
  10624. meta_storage(meta_storage &&other)
  10625. : meta_storage{}
  10626. {
  10627. swap(*this, other);
  10628. }
  10629. ~meta_storage() {
  10630. if(destroy_fn) {
  10631. destroy_fn(*this);
  10632. }
  10633. }
  10634. meta_storage & operator=(meta_storage other) {
  10635. swap(other, *this);
  10636. return *this;
  10637. }
  10638. [[nodiscard]] const void * data() const ENTT_NOEXCEPT {
  10639. return instance;
  10640. }
  10641. [[nodiscard]] void * data() ENTT_NOEXCEPT {
  10642. return const_cast<void *>(std::as_const(*this).data());
  10643. }
  10644. template<typename Type, typename... Args>
  10645. void emplace(Args &&... args) {
  10646. *this = meta_storage{std::in_place_type<Type>, std::forward<Args>(args)...};
  10647. }
  10648. [[nodiscard]] meta_storage ref() const ENTT_NOEXCEPT {
  10649. meta_storage other{};
  10650. other.instance = instance;
  10651. return other;
  10652. }
  10653. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  10654. return !(instance == nullptr);
  10655. }
  10656. friend void swap(meta_storage &lhs, meta_storage &rhs) {
  10657. using std::swap;
  10658. if(lhs.steal_fn && rhs.steal_fn) {
  10659. meta_storage buffer{};
  10660. lhs.steal_fn(buffer, lhs);
  10661. rhs.steal_fn(lhs, rhs);
  10662. lhs.steal_fn(rhs, buffer);
  10663. } else if(lhs.steal_fn) {
  10664. lhs.steal_fn(rhs, lhs);
  10665. } else if(rhs.steal_fn) {
  10666. rhs.steal_fn(lhs, rhs);
  10667. } else {
  10668. swap(lhs.instance, rhs.instance);
  10669. }
  10670. swap(lhs.destroy_fn, rhs.destroy_fn);
  10671. swap(lhs.copy_fn, rhs.copy_fn);
  10672. swap(lhs.steal_fn, rhs.steal_fn);
  10673. }
  10674. private:
  10675. storage_type storage;
  10676. void *instance;
  10677. destroy_fn_type *destroy_fn;
  10678. copy_fn_type *copy_fn;
  10679. steal_fn_type *steal_fn;
  10680. };
  10681. struct meta_type_node;
  10682. struct meta_prop_node {
  10683. meta_prop_node * next;
  10684. meta_any(* const key)();
  10685. meta_any(* const value)();
  10686. };
  10687. struct meta_base_node {
  10688. meta_type_node * const parent;
  10689. meta_base_node * next;
  10690. meta_type_node *(* const type)() ENTT_NOEXCEPT;
  10691. const void *(* const cast)(const void *) ENTT_NOEXCEPT;
  10692. };
  10693. struct meta_conv_node {
  10694. meta_type_node * const parent;
  10695. meta_conv_node * next;
  10696. meta_type_node *(* const type)() ENTT_NOEXCEPT;
  10697. meta_any(* const conv)(const void *);
  10698. };
  10699. struct meta_ctor_node {
  10700. using size_type = std::size_t;
  10701. meta_type_node * const parent;
  10702. meta_ctor_node * next;
  10703. meta_prop_node * prop;
  10704. const size_type size;
  10705. meta_type_node *(* const arg)(size_type) ENTT_NOEXCEPT;
  10706. meta_any(* const invoke)(meta_any * const);
  10707. };
  10708. struct meta_data_node {
  10709. id_type id;
  10710. meta_type_node * const parent;
  10711. meta_data_node * next;
  10712. meta_prop_node * prop;
  10713. const bool is_static;
  10714. meta_type_node *(* const type)() ENTT_NOEXCEPT;
  10715. bool(* const set)(meta_handle, meta_any);
  10716. meta_any(* const get)(meta_handle);
  10717. };
  10718. struct meta_func_node {
  10719. using size_type = std::size_t;
  10720. id_type id;
  10721. meta_type_node * const parent;
  10722. meta_func_node * next;
  10723. meta_prop_node * prop;
  10724. const size_type size;
  10725. const bool is_const;
  10726. const bool is_static;
  10727. meta_type_node *(* const ret)() ENTT_NOEXCEPT;
  10728. meta_type_node *(* const arg)(size_type) ENTT_NOEXCEPT;
  10729. meta_any(* const invoke)(meta_handle, meta_any *);
  10730. };
  10731. struct meta_type_node {
  10732. using size_type = std::size_t;
  10733. const id_type type_id;
  10734. id_type id;
  10735. meta_type_node * next;
  10736. meta_prop_node * prop;
  10737. const bool is_void;
  10738. const bool is_integral;
  10739. const bool is_floating_point;
  10740. const bool is_array;
  10741. const bool is_enum;
  10742. const bool is_union;
  10743. const bool is_class;
  10744. const bool is_pointer;
  10745. const bool is_function_pointer;
  10746. const bool is_member_object_pointer;
  10747. const bool is_member_function_pointer;
  10748. const bool is_pointer_like;
  10749. const bool is_sequence_container;
  10750. const bool is_associative_container;
  10751. const size_type rank;
  10752. size_type(* const extent)(size_type);
  10753. bool(* const compare)(const void *, const void *);
  10754. meta_type_node *(* const remove_pointer)() ENTT_NOEXCEPT;
  10755. meta_type_node *(* const remove_extent)() ENTT_NOEXCEPT;
  10756. meta_base_node *base{nullptr};
  10757. meta_conv_node *conv{nullptr};
  10758. meta_ctor_node *ctor{nullptr};
  10759. meta_data_node *data{nullptr};
  10760. meta_func_node *func{nullptr};
  10761. void(* dtor)(void *){nullptr};
  10762. };
  10763. template<typename Node>
  10764. class meta_range {
  10765. struct range_iterator {
  10766. using difference_type = std::ptrdiff_t;
  10767. using value_type = Node;
  10768. using pointer = value_type *;
  10769. using reference = value_type &;
  10770. using iterator_category = std::forward_iterator_tag;
  10771. range_iterator() ENTT_NOEXCEPT = default;
  10772. range_iterator(Node *head) ENTT_NOEXCEPT
  10773. : node{head}
  10774. {}
  10775. range_iterator & operator++() ENTT_NOEXCEPT {
  10776. return node = node->next, *this;
  10777. }
  10778. range_iterator operator++(int) ENTT_NOEXCEPT {
  10779. range_iterator orig = *this;
  10780. return ++(*this), orig;
  10781. }
  10782. [[nodiscard]] bool operator==(const range_iterator &other) const ENTT_NOEXCEPT {
  10783. return other.node == node;
  10784. }
  10785. [[nodiscard]] bool operator!=(const range_iterator &other) const ENTT_NOEXCEPT {
  10786. return !(*this == other);
  10787. }
  10788. [[nodiscard]] pointer operator->() const ENTT_NOEXCEPT {
  10789. return node;
  10790. }
  10791. [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
  10792. return *operator->();
  10793. }
  10794. private:
  10795. Node *node{nullptr};
  10796. };
  10797. public:
  10798. using iterator = range_iterator;
  10799. meta_range() ENTT_NOEXCEPT = default;
  10800. meta_range(Node *head)
  10801. : node{head}
  10802. {}
  10803. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  10804. return iterator{node};
  10805. }
  10806. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  10807. return iterator{};
  10808. }
  10809. private:
  10810. Node *node{nullptr};
  10811. };
  10812. template<auto Member, typename Op>
  10813. auto find_if(const Op &op, const meta_type_node *node)
  10814. -> std::decay_t<decltype(node->*Member)> {
  10815. std::decay_t<decltype(node->*Member)> ret = nullptr;
  10816. for(auto &&curr: meta_range{node->*Member}) {
  10817. if(op(&curr)) {
  10818. ret = &curr;
  10819. break;
  10820. }
  10821. }
  10822. if(!ret) {
  10823. for(auto &&curr: meta_range{node->base}) {
  10824. if(ret = find_if<Member>(op, curr.type()); ret) {
  10825. break;
  10826. }
  10827. }
  10828. }
  10829. return ret;
  10830. }
  10831. template<typename Type>
  10832. class ENTT_API meta_node {
  10833. static_assert(std::is_same_v<Type, std::remove_cv_t<std::remove_reference_t<Type>>>, "Invalid type");
  10834. [[nodiscard]] static bool compare(const void *lhs, const void *rhs) {
  10835. if constexpr(!std::is_function_v<Type> && is_equality_comparable_v<Type>) {
  10836. return *static_cast<const Type *>(lhs) == *static_cast<const Type *>(rhs);
  10837. } else {
  10838. return lhs == rhs;
  10839. }
  10840. }
  10841. template<std::size_t... Index>
  10842. [[nodiscard]] static auto extent(meta_type_node::size_type dim, std::index_sequence<Index...>) {
  10843. meta_type_node::size_type ext{};
  10844. ((ext = (dim == Index ? std::extent_v<Type, Index> : ext)), ...);
  10845. return ext;
  10846. }
  10847. public:
  10848. [[nodiscard]] static meta_type_node * resolve() ENTT_NOEXCEPT {
  10849. static meta_type_node node{
  10850. type_info<Type>::id(),
  10851. {},
  10852. nullptr,
  10853. nullptr,
  10854. std::is_void_v<Type>,
  10855. std::is_integral_v<Type>,
  10856. std::is_floating_point_v<Type>,
  10857. std::is_array_v<Type>,
  10858. std::is_enum_v<Type>,
  10859. std::is_union_v<Type>,
  10860. std::is_class_v<Type>,
  10861. std::is_pointer_v<Type>,
  10862. std::is_pointer_v<Type> && std::is_function_v<std::remove_pointer_t<Type>>,
  10863. std::is_member_object_pointer_v<Type>,
  10864. std::is_member_function_pointer_v<Type>,
  10865. is_meta_pointer_like_v<Type>,
  10866. has_meta_sequence_container_traits_v<Type>,
  10867. has_meta_associative_container_traits_v<Type>,
  10868. std::rank_v<Type>,
  10869. [](meta_type_node::size_type dim) {
  10870. return extent(dim, std::make_index_sequence<std::rank_v<Type>>{});
  10871. },
  10872. &compare, // workaround for an issue with VS2017
  10873. &meta_node<std::remove_const_t<std::remove_pointer_t<Type>>>::resolve,
  10874. &meta_node<std::remove_const_t<std::remove_extent_t<Type>>>::resolve
  10875. };
  10876. return &node;
  10877. }
  10878. };
  10879. template<typename... Type>
  10880. struct meta_info: meta_node<std::remove_cv_t<std::remove_reference_t<Type>>...> {};
  10881. }
  10882. /**
  10883. * Internal details not to be documented.
  10884. * @endcond
  10885. */
  10886. }
  10887. #endif
  10888. // #include "meta.hpp"
  10889. #ifndef ENTT_META_META_HPP
  10890. #define ENTT_META_META_HPP
  10891. #include <algorithm>
  10892. #include <array>
  10893. #include <cstddef>
  10894. #include <iterator>
  10895. #include <functional>
  10896. #include <type_traits>
  10897. #include <utility>
  10898. // #include "../config/config.h"
  10899. // #include "../core/fwd.hpp"
  10900. // #include "../core/utility.hpp"
  10901. #ifndef ENTT_CORE_UTILITY_HPP
  10902. #define ENTT_CORE_UTILITY_HPP
  10903. #include <utility>
  10904. // #include "../config/config.h"
  10905. namespace entt {
  10906. /*! @brief Identity function object (waiting for C++20). */
  10907. struct identity {
  10908. /**
  10909. * @brief Returns its argument unchanged.
  10910. * @tparam Type Type of the argument.
  10911. * @param value The actual argument.
  10912. * @return The submitted value as-is.
  10913. */
  10914. template<class Type>
  10915. [[nodiscard]] constexpr Type && operator()(Type &&value) const ENTT_NOEXCEPT {
  10916. return std::forward<Type>(value);
  10917. }
  10918. };
  10919. /**
  10920. * @brief Constant utility to disambiguate overloaded members of a class.
  10921. * @tparam Type Type of the desired overload.
  10922. * @tparam Class Type of class to which the member belongs.
  10923. * @param member A valid pointer to a member.
  10924. * @return Pointer to the member.
  10925. */
  10926. template<typename Type, typename Class>
  10927. [[nodiscard]] constexpr auto overload(Type Class:: *member) ENTT_NOEXCEPT { return member; }
  10928. /**
  10929. * @brief Constant utility to disambiguate overloaded functions.
  10930. * @tparam Func Function type of the desired overload.
  10931. * @param func A valid pointer to a function.
  10932. * @return Pointer to the function.
  10933. */
  10934. template<typename Func>
  10935. [[nodiscard]] constexpr auto overload(Func *func) ENTT_NOEXCEPT { return func; }
  10936. /**
  10937. * @brief Helper type for visitors.
  10938. * @tparam Func Types of function objects.
  10939. */
  10940. template<class... Func>
  10941. struct overloaded: Func... {
  10942. using Func::operator()...;
  10943. };
  10944. /**
  10945. * @brief Deduction guide.
  10946. * @tparam Func Types of function objects.
  10947. */
  10948. template<class... Func>
  10949. overloaded(Func...) -> overloaded<Func...>;
  10950. /**
  10951. * @brief Basic implementation of a y-combinator.
  10952. * @tparam Func Type of a potentially recursive function.
  10953. */
  10954. template<class Func>
  10955. struct y_combinator {
  10956. /**
  10957. * @brief Constructs a y-combinator from a given function.
  10958. * @param recursive A potentially recursive function.
  10959. */
  10960. y_combinator(Func recursive):
  10961. func{std::move(recursive)}
  10962. {}
  10963. /**
  10964. * @brief Invokes a y-combinator and therefore its underlying function.
  10965. * @tparam Args Types of arguments to use to invoke the underlying function.
  10966. * @param args Parameters to use to invoke the underlying function.
  10967. * @return Return value of the underlying function, if any.
  10968. */
  10969. template <class... Args>
  10970. decltype(auto) operator()(Args &&... args) const {
  10971. return func(*this, std::forward<Args>(args)...);
  10972. }
  10973. /*! @copydoc operator()() */
  10974. template <class... Args>
  10975. decltype(auto) operator()(Args &&... args) {
  10976. return func(*this, std::forward<Args>(args)...);
  10977. }
  10978. private:
  10979. Func func;
  10980. };
  10981. }
  10982. #endif
  10983. // #include "ctx.hpp"
  10984. // #include "internal.hpp"
  10985. // #include "range.hpp"
  10986. #ifndef ENTT_META_RANGE_HPP
  10987. #define ENTT_META_RANGE_HPP
  10988. // #include "internal.hpp"
  10989. namespace entt {
  10990. /**
  10991. * @brief Iterable range to use to iterate all types of meta objects.
  10992. * @tparam Type Type of meta objects iterated.
  10993. */
  10994. template<typename Type>
  10995. class meta_range {
  10996. struct range_iterator {
  10997. using difference_type = std::ptrdiff_t;
  10998. using value_type = Type;
  10999. using pointer = void;
  11000. using reference = value_type;
  11001. using iterator_category = std::input_iterator_tag;
  11002. using node_type = typename Type::node_type;
  11003. range_iterator() ENTT_NOEXCEPT = default;
  11004. range_iterator(node_type *head) ENTT_NOEXCEPT
  11005. : it{head}
  11006. {}
  11007. range_iterator & operator++() ENTT_NOEXCEPT {
  11008. return ++it, *this;
  11009. }
  11010. range_iterator operator++(int) ENTT_NOEXCEPT {
  11011. range_iterator orig = *this;
  11012. return ++(*this), orig;
  11013. }
  11014. [[nodiscard]] reference operator*() const ENTT_NOEXCEPT {
  11015. return it.operator->();
  11016. }
  11017. [[nodiscard]] bool operator==(const range_iterator &other) const ENTT_NOEXCEPT {
  11018. return other.it == it;
  11019. }
  11020. [[nodiscard]] bool operator!=(const range_iterator &other) const ENTT_NOEXCEPT {
  11021. return !(*this == other);
  11022. }
  11023. private:
  11024. typename internal::meta_range<node_type>::iterator it{};
  11025. };
  11026. public:
  11027. /*! @brief Node type. */
  11028. using node_type = typename Type::node_type;
  11029. /*! @brief Input iterator type. */
  11030. using iterator = range_iterator;
  11031. /*! @brief Default constructor. */
  11032. meta_range() ENTT_NOEXCEPT = default;
  11033. /**
  11034. * @brief Constructs a meta range from a given node.
  11035. * @param head The underlying node with which to construct the range.
  11036. */
  11037. meta_range(node_type *head)
  11038. : node{head}
  11039. {}
  11040. /**
  11041. * @brief Returns an iterator to the beginning.
  11042. * @return An iterator to the first meta object of the range.
  11043. */
  11044. [[nodiscard]] iterator begin() const ENTT_NOEXCEPT {
  11045. return iterator{node};
  11046. }
  11047. /**
  11048. * @brief Returns an iterator to the end.
  11049. * @return An iterator to the element following the last meta object of the
  11050. * range.
  11051. */
  11052. [[nodiscard]] iterator end() const ENTT_NOEXCEPT {
  11053. return iterator{};
  11054. }
  11055. private:
  11056. node_type *node{nullptr};
  11057. };
  11058. }
  11059. #endif
  11060. // #include "type_traits.hpp"
  11061. namespace entt {
  11062. class meta_type;
  11063. class meta_any;
  11064. /*! @brief Proxy object for sequence containers. */
  11065. class meta_sequence_container {
  11066. template<typename>
  11067. struct meta_sequence_container_proxy;
  11068. class meta_iterator;
  11069. public:
  11070. /*! @brief Unsigned integer type. */
  11071. using size_type = std::size_t;
  11072. /*! @brief Meta iterator type. */
  11073. using iterator = meta_iterator;
  11074. /*! @brief Default constructor. */
  11075. meta_sequence_container() ENTT_NOEXCEPT
  11076. : instance{nullptr}
  11077. {}
  11078. /**
  11079. * @brief Construct a proxy object for sequence containers.
  11080. * @tparam Type Type of container to wrap.
  11081. * @param container The container to wrap.
  11082. */
  11083. template<typename Type>
  11084. meta_sequence_container(Type *container) ENTT_NOEXCEPT
  11085. : value_type_fn{&meta_sequence_container_proxy<Type>::value_type},
  11086. size_fn{&meta_sequence_container_proxy<Type>::size},
  11087. resize_fn{&meta_sequence_container_proxy<Type>::resize},
  11088. clear_fn{&meta_sequence_container_proxy<Type>::clear},
  11089. begin_fn{&meta_sequence_container_proxy<Type>::begin},
  11090. end_fn{&meta_sequence_container_proxy<Type>::end},
  11091. insert_fn{&meta_sequence_container_proxy<Type>::insert},
  11092. erase_fn{&meta_sequence_container_proxy<Type>::erase},
  11093. get_fn{&meta_sequence_container_proxy<Type>::get},
  11094. instance{container}
  11095. {}
  11096. [[nodiscard]] inline meta_type value_type() const ENTT_NOEXCEPT;
  11097. [[nodiscard]] inline size_type size() const ENTT_NOEXCEPT;
  11098. inline bool resize(size_type) const;
  11099. inline bool clear();
  11100. [[nodiscard]] inline iterator begin();
  11101. [[nodiscard]] inline iterator end();
  11102. inline std::pair<iterator, bool> insert(iterator, meta_any);
  11103. inline std::pair<iterator, bool> erase(iterator);
  11104. [[nodiscard]] inline meta_any operator[](size_type);
  11105. [[nodiscard]] inline explicit operator bool() const ENTT_NOEXCEPT;
  11106. private:
  11107. meta_type(* value_type_fn)() ENTT_NOEXCEPT;
  11108. size_type(* size_fn)(const void *) ENTT_NOEXCEPT;
  11109. bool(* resize_fn)(void *, size_type);
  11110. bool(* clear_fn)(void *);
  11111. iterator(* begin_fn)(void *);
  11112. iterator(* end_fn)(void *);
  11113. std::pair<iterator, bool>(* insert_fn)(void *, iterator, meta_any);
  11114. std::pair<iterator, bool>(* erase_fn)(void *, iterator);
  11115. meta_any(* get_fn)(void *, size_type);
  11116. void *instance;
  11117. };
  11118. /*! @brief Proxy object for associative containers. */
  11119. class meta_associative_container {
  11120. template<typename>
  11121. struct meta_associative_container_proxy;
  11122. class meta_iterator;
  11123. public:
  11124. /*! @brief Unsigned integer type. */
  11125. using size_type = std::size_t;
  11126. /*! @brief Meta iterator type. */
  11127. using iterator = meta_iterator;
  11128. /*! @brief Default constructor. */
  11129. meta_associative_container() ENTT_NOEXCEPT
  11130. : instance{nullptr}
  11131. {}
  11132. /**
  11133. * @brief Construct a proxy object for associative containers.
  11134. * @tparam Type Type of container to wrap.
  11135. * @param container The container to wrap.
  11136. */
  11137. template<typename Type>
  11138. meta_associative_container(Type *container) ENTT_NOEXCEPT
  11139. : key_only_container{is_key_only_meta_associative_container_v<Type>},
  11140. key_type_fn{&meta_associative_container_proxy<Type>::key_type},
  11141. mapped_type_fn{&meta_associative_container_proxy<Type>::mapped_type},
  11142. value_type_fn{&meta_associative_container_proxy<Type>::value_type},
  11143. size_fn{&meta_associative_container_proxy<Type>::size},
  11144. clear_fn{&meta_associative_container_proxy<Type>::clear},
  11145. begin_fn{&meta_associative_container_proxy<Type>::begin},
  11146. end_fn{&meta_associative_container_proxy<Type>::end},
  11147. insert_fn{&meta_associative_container_proxy<Type>::insert},
  11148. erase_fn{&meta_associative_container_proxy<Type>::erase},
  11149. find_fn{&meta_associative_container_proxy<Type>::find},
  11150. instance{container}
  11151. {}
  11152. [[nodiscard]] inline bool key_only() const ENTT_NOEXCEPT;
  11153. [[nodiscard]] inline meta_type key_type() const ENTT_NOEXCEPT;
  11154. [[nodiscard]] inline meta_type mapped_type() const ENTT_NOEXCEPT;
  11155. [[nodiscard]] inline meta_type value_type() const ENTT_NOEXCEPT;
  11156. [[nodiscard]] inline size_type size() const ENTT_NOEXCEPT;
  11157. inline bool clear();
  11158. [[nodiscard]] inline iterator begin();
  11159. [[nodiscard]] inline iterator end();
  11160. inline bool insert(meta_any, meta_any);
  11161. inline bool erase(meta_any);
  11162. [[nodiscard]] inline iterator find(meta_any);
  11163. [[nodiscard]] inline explicit operator bool() const ENTT_NOEXCEPT;
  11164. private:
  11165. bool key_only_container;
  11166. meta_type(* key_type_fn)() ENTT_NOEXCEPT;
  11167. meta_type(* mapped_type_fn)() ENTT_NOEXCEPT;
  11168. meta_type(* value_type_fn)() ENTT_NOEXCEPT;
  11169. size_type(* size_fn)(const void *) ENTT_NOEXCEPT;
  11170. bool(* clear_fn)(void *);
  11171. iterator(* begin_fn)(void *);
  11172. iterator(* end_fn)(void *);
  11173. bool(* insert_fn)(void *, meta_any, meta_any);
  11174. bool(* erase_fn)(void *, meta_any);
  11175. iterator(* find_fn)(void *, meta_any);
  11176. void *instance;
  11177. };
  11178. /**
  11179. * @brief Opaque wrapper for values of any type.
  11180. *
  11181. * This class uses a technique called small buffer optimization (SBO) to get rid
  11182. * of memory allocations if possible. This should improve overall performance.
  11183. */
  11184. class meta_any {
  11185. using dereference_operator_type = meta_any(meta_any &);
  11186. template<typename Type>
  11187. [[nodiscard]] static meta_any dereference_operator(meta_any &any) {
  11188. if constexpr(is_meta_pointer_like_v<Type>) {
  11189. if constexpr(std::is_const_v<std::remove_reference_t<decltype(*std::declval<Type>())>>) {
  11190. return *any.cast<Type>();
  11191. } else {
  11192. return std::ref(*any.cast<Type>());
  11193. }
  11194. } else {
  11195. return {};
  11196. }
  11197. }
  11198. template<typename Type>
  11199. [[nodiscard]] static meta_sequence_container meta_sequence_container_factory([[maybe_unused]] void *container) ENTT_NOEXCEPT {
  11200. if constexpr(has_meta_sequence_container_traits_v<Type>) {
  11201. return static_cast<Type *>(container);
  11202. } else {
  11203. return {};
  11204. }
  11205. }
  11206. template<typename Type>
  11207. [[nodiscard]] static meta_associative_container meta_associative_container_factory([[maybe_unused]] void *container) ENTT_NOEXCEPT {
  11208. if constexpr(has_meta_associative_container_traits_v<Type>) {
  11209. return static_cast<Type *>(container);
  11210. } else {
  11211. return {};
  11212. }
  11213. }
  11214. public:
  11215. /*! @brief Default constructor. */
  11216. meta_any() ENTT_NOEXCEPT
  11217. : storage{},
  11218. node{},
  11219. deref{nullptr},
  11220. seq_factory{nullptr},
  11221. assoc_factory{nullptr}
  11222. {}
  11223. /**
  11224. * @brief Constructs a meta any by directly initializing the new object.
  11225. * @tparam Type Type of object to use to initialize the wrapper.
  11226. * @tparam Args Types of arguments to use to construct the new instance.
  11227. * @param args Parameters to use to construct the instance.
  11228. */
  11229. template<typename Type, typename... Args>
  11230. explicit meta_any(std::in_place_type_t<Type>, [[maybe_unused]] Args &&... args)
  11231. : storage(std::in_place_type<Type>, std::forward<Args>(args)...),
  11232. node{internal::meta_info<Type>::resolve()},
  11233. deref{&dereference_operator<Type>},
  11234. seq_factory{&meta_sequence_container_factory<Type>},
  11235. assoc_factory{&meta_associative_container_factory<Type>}
  11236. {}
  11237. /**
  11238. * @brief Constructs a meta any that holds an unmanaged object.
  11239. * @tparam Type Type of object to use to initialize the wrapper.
  11240. * @param value An instance of an object to use to initialize the wrapper.
  11241. */
  11242. template<typename Type>
  11243. meta_any(std::reference_wrapper<Type> value)
  11244. : storage{value},
  11245. node{internal::meta_info<Type>::resolve()},
  11246. deref{&dereference_operator<Type>},
  11247. seq_factory{&meta_sequence_container_factory<Type>},
  11248. assoc_factory{&meta_associative_container_factory<Type>}
  11249. {}
  11250. /**
  11251. * @brief Constructs a meta any from a given value.
  11252. * @tparam Type Type of object to use to initialize the wrapper.
  11253. * @param value An instance of an object to use to initialize the wrapper.
  11254. */
  11255. template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::remove_cv_t<std::remove_reference_t<Type>>, meta_any>>>
  11256. meta_any(Type &&value)
  11257. : meta_any{std::in_place_type<std::remove_cv_t<std::remove_reference_t<Type>>>, std::forward<Type>(value)}
  11258. {}
  11259. /**
  11260. * @brief Copy constructor.
  11261. * @param other The instance to copy from.
  11262. */
  11263. meta_any(const meta_any &other) = default;
  11264. /**
  11265. * @brief Move constructor.
  11266. * @param other The instance to move from.
  11267. */
  11268. meta_any(meta_any &&other)
  11269. : meta_any{}
  11270. {
  11271. swap(*this, other);
  11272. }
  11273. /*! @brief Frees the internal storage, whatever it means. */
  11274. ~meta_any() {
  11275. if(node && node->dtor) {
  11276. node->dtor(storage.data());
  11277. }
  11278. }
  11279. /**
  11280. * @brief Assignment operator.
  11281. * @param other The instance to assign from.
  11282. * @return This meta any object.
  11283. */
  11284. meta_any & operator=(meta_any other) {
  11285. swap(other, *this);
  11286. return *this;
  11287. }
  11288. /**
  11289. * @brief Returns the meta type of the underlying object.
  11290. * @return The meta type of the underlying object, if any.
  11291. */
  11292. [[nodiscard]] inline meta_type type() const ENTT_NOEXCEPT;
  11293. /**
  11294. * @brief Returns an opaque pointer to the contained instance.
  11295. * @return An opaque pointer the contained instance, if any.
  11296. */
  11297. [[nodiscard]] const void * data() const ENTT_NOEXCEPT {
  11298. return storage.data();
  11299. }
  11300. /*! @copydoc data */
  11301. [[nodiscard]] void * data() ENTT_NOEXCEPT {
  11302. return storage.data();
  11303. }
  11304. /**
  11305. * @brief Tries to cast an instance to a given type.
  11306. * @tparam Type Type to which to cast the instance.
  11307. * @return A (possibly null) pointer to the contained instance.
  11308. */
  11309. template<typename Type>
  11310. [[nodiscard]] const Type * try_cast() const {
  11311. if(node) {
  11312. if(const auto type_id = internal::meta_info<Type>::resolve()->type_id; node->type_id == type_id) {
  11313. return static_cast<const Type *>(storage.data());
  11314. } else if(const auto *base = internal::find_if<&internal::meta_type_node::base>([type_id](const auto *curr) { return curr->type()->type_id == type_id; }, node); base) {
  11315. return static_cast<const Type *>(base->cast(storage.data()));
  11316. }
  11317. }
  11318. return nullptr;
  11319. }
  11320. /*! @copydoc try_cast */
  11321. template<typename Type>
  11322. [[nodiscard]] Type * try_cast() {
  11323. return const_cast<Type *>(std::as_const(*this).try_cast<Type>());
  11324. }
  11325. /**
  11326. * @brief Tries to cast an instance to a given type.
  11327. *
  11328. * The type of the instance must be such that the cast is possible.
  11329. *
  11330. * @warning
  11331. * Attempting to perform a cast that isn't viable results in undefined
  11332. * behavior.<br/>
  11333. * An assertion will abort the execution at runtime in debug mode in case
  11334. * the cast is not feasible.
  11335. *
  11336. * @tparam Type Type to which to cast the instance.
  11337. * @return A reference to the contained instance.
  11338. */
  11339. template<typename Type>
  11340. [[nodiscard]] const Type & cast() const {
  11341. auto * const actual = try_cast<Type>();
  11342. ENTT_ASSERT(actual);
  11343. return *actual;
  11344. }
  11345. /*! @copydoc cast */
  11346. template<typename Type>
  11347. [[nodiscard]] Type & cast() {
  11348. return const_cast<Type &>(std::as_const(*this).cast<Type>());
  11349. }
  11350. /**
  11351. * @brief Tries to convert an instance to a given type and returns it.
  11352. * @tparam Type Type to which to convert the instance.
  11353. * @return A valid meta any object if the conversion is possible, an invalid
  11354. * one otherwise.
  11355. */
  11356. template<typename Type>
  11357. [[nodiscard]] meta_any convert() const {
  11358. if(node) {
  11359. if(const auto type_id = internal::meta_info<Type>::resolve()->type_id; node->type_id == type_id) {
  11360. return *this;
  11361. } else if(const auto * const conv = internal::find_if<&internal::meta_type_node::conv>([type_id](const auto *curr) { return curr->type()->type_id == type_id; }, node); conv) {
  11362. return conv->conv(storage.data());
  11363. }
  11364. }
  11365. return {};
  11366. }
  11367. /**
  11368. * @brief Tries to convert an instance to a given type.
  11369. * @tparam Type Type to which to convert the instance.
  11370. * @return True if the conversion is possible, false otherwise.
  11371. */
  11372. template<typename Type>
  11373. bool convert() {
  11374. bool valid = (node && node->type_id == internal::meta_info<Type>::resolve()->type_id);
  11375. if(!valid) {
  11376. if(auto any = std::as_const(*this).convert<Type>(); any) {
  11377. swap(any, *this);
  11378. valid = true;
  11379. }
  11380. }
  11381. return valid;
  11382. }
  11383. /**
  11384. * @brief Replaces the contained object by creating a new instance directly.
  11385. * @tparam Type Type of object to use to initialize the wrapper.
  11386. * @tparam Args Types of arguments to use to construct the new instance.
  11387. * @param args Parameters to use to construct the instance.
  11388. */
  11389. template<typename Type, typename... Args>
  11390. void emplace(Args &&... args) {
  11391. *this = meta_any{std::in_place_type<Type>, std::forward<Args>(args)...};
  11392. }
  11393. /**
  11394. * @brief Aliasing constructor.
  11395. * @return A meta any that shares a reference to an unmanaged object.
  11396. */
  11397. [[nodiscard]] meta_any ref() const ENTT_NOEXCEPT {
  11398. meta_any other{};
  11399. other.node = node;
  11400. other.storage = storage.ref();
  11401. other.deref = deref;
  11402. other.seq_factory = seq_factory;
  11403. other.assoc_factory = assoc_factory;
  11404. return other;
  11405. }
  11406. /**
  11407. * @brief Returns a sequence container proxy.
  11408. * @return A sequence container proxy for the underlying object.
  11409. */
  11410. [[nodiscard]] meta_sequence_container as_sequence_container() ENTT_NOEXCEPT {
  11411. return seq_factory(storage.data());
  11412. }
  11413. /**
  11414. * @brief Returns an associative container proxy.
  11415. * @return An associative container proxy for the underlying object.
  11416. */
  11417. [[nodiscard]] meta_associative_container as_associative_container() ENTT_NOEXCEPT {
  11418. return assoc_factory(storage.data());
  11419. }
  11420. /**
  11421. * @brief Indirection operator for dereferencing opaque objects.
  11422. * @return A meta any that shares a reference to an unmanaged object if the
  11423. * wrapped element is dereferenceable, an invalid meta any otherwise.
  11424. */
  11425. [[nodiscard]] meta_any operator*() ENTT_NOEXCEPT {
  11426. return deref(*this);
  11427. }
  11428. /**
  11429. * @brief Returns false if a wrapper is empty, true otherwise.
  11430. * @return False if the wrapper is empty, true otherwise.
  11431. */
  11432. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  11433. return !(node == nullptr);
  11434. }
  11435. /**
  11436. * @brief Checks if two wrappers differ in their content.
  11437. * @param other Wrapper with which to compare.
  11438. * @return False if the two objects differ in their content, true otherwise.
  11439. */
  11440. [[nodiscard]] bool operator==(const meta_any &other) const {
  11441. return (!node && !other.node) || (node && other.node && node->type_id == other.node->type_id && node->compare(storage.data(), other.storage.data()));
  11442. }
  11443. /**
  11444. * @brief Swaps two meta any objects.
  11445. * @param lhs A valid meta any object.
  11446. * @param rhs A valid meta any object.
  11447. */
  11448. friend void swap(meta_any &lhs, meta_any &rhs) {
  11449. using std::swap;
  11450. swap(lhs.storage, rhs.storage);
  11451. swap(lhs.node, rhs.node);
  11452. swap(lhs.deref, rhs.deref);
  11453. swap(lhs.seq_factory, rhs.seq_factory);
  11454. swap(lhs.assoc_factory, rhs.assoc_factory);
  11455. }
  11456. private:
  11457. internal::meta_storage storage;
  11458. internal::meta_type_node *node;
  11459. dereference_operator_type *deref;
  11460. meta_sequence_container(* seq_factory)(void *);
  11461. meta_associative_container(* assoc_factory)(void *);
  11462. };
  11463. /**
  11464. * @brief Checks if two wrappers differ in their content.
  11465. * @param lhs A meta any object, either empty or not.
  11466. * @param rhs A meta any object, either empty or not.
  11467. * @return True if the two wrappers differ in their content, false otherwise.
  11468. */
  11469. [[nodiscard]] inline bool operator!=(const meta_any &lhs, const meta_any &rhs) ENTT_NOEXCEPT {
  11470. return !(lhs == rhs);
  11471. }
  11472. /**
  11473. * @brief Opaque pointers to instances of any type.
  11474. *
  11475. * A handle doesn't perform copies and isn't responsible for the contained
  11476. * object. It doesn't prolong the lifetime of the pointed instance.<br/>
  11477. * Handles are used to generate meta references to actual objects when needed.
  11478. */
  11479. struct meta_handle {
  11480. /*! @brief Default constructor. */
  11481. meta_handle() = default;
  11482. /**
  11483. * @brief Creates a handle that points to an unmanaged object.
  11484. * @tparam Type Type of object to use to initialize the handle.
  11485. * @param value An instance of an object to use to initialize the handle.
  11486. */
  11487. template<typename Type, typename = std::enable_if_t<!std::is_same_v<std::remove_cv_t<std::remove_reference_t<Type>>, meta_handle>>>
  11488. meta_handle(Type &&value) ENTT_NOEXCEPT
  11489. : meta_handle{}
  11490. {
  11491. if constexpr(std::is_same_v<std::remove_cv_t<std::remove_reference_t<Type>>, meta_any>) {
  11492. any = value.ref();
  11493. } else {
  11494. static_assert(std::is_lvalue_reference_v<Type>, "Lvalue reference required");
  11495. any = std::ref(value);
  11496. }
  11497. }
  11498. /**
  11499. * @brief Dereference operator for accessing the contained opaque object.
  11500. * @return A meta any that shares a reference to an unmanaged object.
  11501. */
  11502. [[nodiscard]] meta_any operator*() const {
  11503. return any;
  11504. }
  11505. /**
  11506. * @brief Access operator for accessing the contained opaque object.
  11507. * @return A meta any that shares a reference to an unmanaged object.
  11508. */
  11509. [[nodiscard]] meta_any * operator->() {
  11510. return &any;
  11511. }
  11512. private:
  11513. meta_any any;
  11514. };
  11515. /*! @brief Opaque wrapper for meta properties of any type. */
  11516. struct meta_prop {
  11517. /*! @brief Node type. */
  11518. using node_type = internal::meta_prop_node;
  11519. /**
  11520. * @brief Constructs an instance from a given node.
  11521. * @param curr The underlying node with which to construct the instance.
  11522. */
  11523. meta_prop(const node_type *curr = nullptr) ENTT_NOEXCEPT
  11524. : node{curr}
  11525. {}
  11526. /**
  11527. * @brief Returns the stored key.
  11528. * @return A meta any containing the key stored with the property.
  11529. */
  11530. [[nodiscard]] meta_any key() const {
  11531. return node->key();
  11532. }
  11533. /**
  11534. * @brief Returns the stored value.
  11535. * @return A meta any containing the value stored with the property.
  11536. */
  11537. [[nodiscard]] meta_any value() const {
  11538. return node->value();
  11539. }
  11540. /**
  11541. * @brief Returns true if a meta object is valid, false otherwise.
  11542. * @return True if the meta object is valid, false otherwise.
  11543. */
  11544. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  11545. return !(node == nullptr);
  11546. }
  11547. private:
  11548. const node_type *node;
  11549. };
  11550. /*! @brief Opaque wrapper for meta base classes. */
  11551. struct meta_base {
  11552. /*! @brief Node type. */
  11553. using node_type = internal::meta_base_node;
  11554. /*! @copydoc meta_prop::meta_prop */
  11555. meta_base(const node_type *curr = nullptr) ENTT_NOEXCEPT
  11556. : node{curr}
  11557. {}
  11558. /**
  11559. * @brief Returns the meta type to which a meta object belongs.
  11560. * @return The meta type to which the meta object belongs.
  11561. */
  11562. [[nodiscard]] inline meta_type parent() const ENTT_NOEXCEPT;
  11563. /*! @copydoc meta_any::type */
  11564. [[nodiscard]] inline meta_type type() const ENTT_NOEXCEPT;
  11565. /**
  11566. * @brief Casts an instance from a parent type to a base type.
  11567. * @param instance The instance to cast.
  11568. * @return An opaque pointer to the base type.
  11569. */
  11570. [[nodiscard]] const void * cast(const void *instance) const ENTT_NOEXCEPT {
  11571. return node->cast(instance);
  11572. }
  11573. /**
  11574. * @brief Returns true if a meta object is valid, false otherwise.
  11575. * @return True if the meta object is valid, false otherwise.
  11576. */
  11577. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  11578. return !(node == nullptr);
  11579. }
  11580. private:
  11581. const node_type *node;
  11582. };
  11583. /*! @brief Opaque wrapper for meta conversion functions. */
  11584. struct meta_conv {
  11585. /*! @brief Node type. */
  11586. using node_type = internal::meta_conv_node;
  11587. /*! @copydoc meta_prop::meta_prop */
  11588. meta_conv(const node_type *curr = nullptr) ENTT_NOEXCEPT
  11589. : node{curr}
  11590. {}
  11591. /*! @copydoc meta_base::parent */
  11592. [[nodiscard]] inline meta_type parent() const ENTT_NOEXCEPT;
  11593. /*! @copydoc meta_any::type */
  11594. [[nodiscard]] inline meta_type type() const ENTT_NOEXCEPT;
  11595. /**
  11596. * @brief Converts an instance to the underlying type.
  11597. * @param instance The instance to convert.
  11598. * @return An opaque pointer to the instance to convert.
  11599. */
  11600. [[nodiscard]] meta_any convert(const void *instance) const {
  11601. return node->conv(instance);
  11602. }
  11603. /**
  11604. * @brief Returns true if a meta object is valid, false otherwise.
  11605. * @return True if the meta object is valid, false otherwise.
  11606. */
  11607. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  11608. return !(node == nullptr);
  11609. }
  11610. private:
  11611. const node_type *node;
  11612. };
  11613. /*! @brief Opaque wrapper for meta constructors. */
  11614. struct meta_ctor {
  11615. /*! @brief Node type. */
  11616. using node_type = internal::meta_ctor_node;
  11617. /*! @brief Unsigned integer type. */
  11618. using size_type = typename node_type::size_type;
  11619. /*! @copydoc meta_prop::meta_prop */
  11620. meta_ctor(const node_type *curr = nullptr) ENTT_NOEXCEPT
  11621. : node{curr}
  11622. {}
  11623. /*! @copydoc meta_base::parent */
  11624. [[nodiscard]] inline meta_type parent() const ENTT_NOEXCEPT;
  11625. /**
  11626. * @brief Returns the number of arguments accepted by a meta constructor.
  11627. * @return The number of arguments accepted by the meta constructor.
  11628. */
  11629. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  11630. return node->size;
  11631. }
  11632. /**
  11633. * @brief Returns the meta type of the i-th argument of a meta constructor.
  11634. * @param index The index of the argument of which to return the meta type.
  11635. * @return The meta type of the i-th argument of a meta constructor, if any.
  11636. */
  11637. [[nodiscard]] meta_type arg(size_type index) const ENTT_NOEXCEPT;
  11638. /**
  11639. * @brief Creates an instance of the underlying type, if possible.
  11640. *
  11641. * To create a valid instance, the parameters must be such that a cast or
  11642. * conversion to the required types is possible. Otherwise, an empty and
  11643. * thus invalid wrapper is returned.
  11644. *
  11645. * @param args Parameters to use to construct the instance.
  11646. * @param sz Number of parameters to use to construct the instance.
  11647. * @return A meta any containing the new instance, if any.
  11648. */
  11649. [[nodiscard]] meta_any invoke(meta_any * const args, const std::size_t sz) const {
  11650. return sz == size() ? node->invoke(args) : meta_any{};
  11651. }
  11652. /**
  11653. * @copybrief invoke
  11654. *
  11655. * @sa invoke
  11656. *
  11657. * @tparam Args Types of arguments to use to construct the instance.
  11658. * @param args Parameters to use to construct the instance.
  11659. * @return A meta any containing the new instance, if any.
  11660. */
  11661. template<typename... Args>
  11662. [[nodiscard]] meta_any invoke([[maybe_unused]] Args &&... args) const {
  11663. std::array<meta_any, sizeof...(Args)> arguments{std::forward<Args>(args)...};
  11664. return invoke(arguments.data(), sizeof...(Args));
  11665. }
  11666. /**
  11667. * @brief Returns a range to use to visit all meta properties.
  11668. * @return An iterable range to use to visit all meta properties.
  11669. */
  11670. [[nodiscard]] meta_range<meta_prop> prop() const ENTT_NOEXCEPT {
  11671. return node->prop;
  11672. }
  11673. /**
  11674. * @brief Returns the property associated with a given key.
  11675. * @param key The key to use to search for a property.
  11676. * @return The property associated with the given key, if any.
  11677. */
  11678. [[nodiscard]] meta_prop prop(meta_any key) const {
  11679. internal::meta_range range{node->prop};
  11680. return std::find_if(range.begin(), range.end(), [&key](const auto &curr) { return curr.key() == key; }).operator->();
  11681. }
  11682. /**
  11683. * @brief Returns true if a meta object is valid, false otherwise.
  11684. * @return True if the meta object is valid, false otherwise.
  11685. */
  11686. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  11687. return !(node == nullptr);
  11688. }
  11689. private:
  11690. const node_type *node;
  11691. };
  11692. /*! @brief Opaque wrapper for meta data. */
  11693. struct meta_data {
  11694. /*! @brief Node type. */
  11695. using node_type = internal::meta_data_node;
  11696. /*! @copydoc meta_prop::meta_prop */
  11697. meta_data(const node_type *curr = nullptr) ENTT_NOEXCEPT
  11698. : node{curr}
  11699. {}
  11700. /*! @copydoc meta_type::id */
  11701. [[nodiscard]] id_type id() const ENTT_NOEXCEPT {
  11702. return node->id;
  11703. }
  11704. /*! @copydoc meta_base::parent */
  11705. [[nodiscard]] inline meta_type parent() const ENTT_NOEXCEPT;
  11706. /**
  11707. * @brief Indicates whether a meta data is constant or not.
  11708. * @return True if the meta data is constant, false otherwise.
  11709. */
  11710. [[nodiscard]] bool is_const() const ENTT_NOEXCEPT {
  11711. return (node->set == nullptr);
  11712. }
  11713. /**
  11714. * @brief Indicates whether a meta data is static or not.
  11715. * @return True if the meta data is static, false otherwise.
  11716. */
  11717. [[nodiscard]] bool is_static() const ENTT_NOEXCEPT {
  11718. return node->is_static;
  11719. }
  11720. /*! @copydoc meta_any::type */
  11721. [[nodiscard]] inline meta_type type() const ENTT_NOEXCEPT;
  11722. /**
  11723. * @brief Sets the value of a given variable.
  11724. *
  11725. * It must be possible to cast the instance to the parent type of the meta
  11726. * data. Otherwise, invoking the setter results in an undefined
  11727. * behavior.<br/>
  11728. * The type of the value must be such that a cast or conversion to the type
  11729. * of the variable is possible. Otherwise, invoking the setter does nothing.
  11730. *
  11731. * @tparam Type Type of value to assign.
  11732. * @param instance An opaque instance of the underlying type.
  11733. * @param value Parameter to use to set the underlying variable.
  11734. * @return True in case of success, false otherwise.
  11735. */
  11736. template<typename Type>
  11737. bool set(meta_handle instance, Type &&value) const {
  11738. return node->set && node->set(std::move(instance), std::forward<Type>(value));
  11739. }
  11740. /**
  11741. * @brief Gets the value of a given variable.
  11742. *
  11743. * It must be possible to cast the instance to the parent type of the meta
  11744. * data. Otherwise, invoking the getter results in an undefined behavior.
  11745. *
  11746. * @param instance An opaque instance of the underlying type.
  11747. * @return A meta any containing the value of the underlying variable.
  11748. */
  11749. [[nodiscard]] meta_any get(meta_handle instance) const {
  11750. return node->get(std::move(instance));
  11751. }
  11752. /*! @copydoc meta_ctor::prop */
  11753. [[nodiscard]] meta_range<meta_prop> prop() const ENTT_NOEXCEPT {
  11754. return node->prop;
  11755. }
  11756. /**
  11757. * @brief Returns the property associated with a given key.
  11758. * @param key The key to use to search for a property.
  11759. * @return The property associated with the given key, if any.
  11760. */
  11761. [[nodiscard]] meta_prop prop(meta_any key) const {
  11762. internal::meta_range range{node->prop};
  11763. return std::find_if(range.begin(), range.end(), [&key](const auto &curr) { return curr.key() == key; }).operator->();
  11764. }
  11765. /**
  11766. * @brief Returns true if a meta object is valid, false otherwise.
  11767. * @return True if the meta object is valid, false otherwise.
  11768. */
  11769. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  11770. return !(node == nullptr);
  11771. }
  11772. private:
  11773. const node_type *node;
  11774. };
  11775. /*! @brief Opaque wrapper for meta functions. */
  11776. struct meta_func {
  11777. /*! @brief Node type. */
  11778. using node_type = internal::meta_func_node;
  11779. /*! @brief Unsigned integer type. */
  11780. using size_type = typename node_type::size_type;
  11781. /*! @copydoc meta_prop::meta_prop */
  11782. meta_func(const node_type *curr = nullptr) ENTT_NOEXCEPT
  11783. : node{curr}
  11784. {}
  11785. /*! @copydoc meta_type::id */
  11786. [[nodiscard]] id_type id() const ENTT_NOEXCEPT {
  11787. return node->id;
  11788. }
  11789. /*! @copydoc meta_base::parent */
  11790. [[nodiscard]] inline meta_type parent() const ENTT_NOEXCEPT;
  11791. /**
  11792. * @brief Returns the number of arguments accepted by a meta function.
  11793. * @return The number of arguments accepted by the meta function.
  11794. */
  11795. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  11796. return node->size;
  11797. }
  11798. /**
  11799. * @brief Indicates whether a meta function is constant or not.
  11800. * @return True if the meta function is constant, false otherwise.
  11801. */
  11802. [[nodiscard]] bool is_const() const ENTT_NOEXCEPT {
  11803. return node->is_const;
  11804. }
  11805. /**
  11806. * @brief Indicates whether a meta function is static or not.
  11807. * @return True if the meta function is static, false otherwise.
  11808. */
  11809. [[nodiscard]] bool is_static() const ENTT_NOEXCEPT {
  11810. return node->is_static;
  11811. }
  11812. /**
  11813. * @brief Returns the meta type of the return type of a meta function.
  11814. * @return The meta type of the return type of the meta function.
  11815. */
  11816. [[nodiscard]] inline meta_type ret() const ENTT_NOEXCEPT;
  11817. /**
  11818. * @brief Returns the meta type of the i-th argument of a meta function.
  11819. * @param index The index of the argument of which to return the meta type.
  11820. * @return The meta type of the i-th argument of a meta function, if any.
  11821. */
  11822. [[nodiscard]] inline meta_type arg(size_type index) const ENTT_NOEXCEPT;
  11823. /**
  11824. * @brief Invokes the underlying function, if possible.
  11825. *
  11826. * To invoke a meta function, the parameters must be such that a cast or
  11827. * conversion to the required types is possible. Otherwise, an empty and
  11828. * thus invalid wrapper is returned.<br/>
  11829. * It must be possible to cast the instance to the parent type of the meta
  11830. * function. Otherwise, invoking the underlying function results in an
  11831. * undefined behavior.
  11832. *
  11833. * @param instance An opaque instance of the underlying type.
  11834. * @param args Parameters to use to invoke the function.
  11835. * @param sz Number of parameters to use to invoke the function.
  11836. * @return A meta any containing the returned value, if any.
  11837. */
  11838. [[nodiscard]] meta_any invoke(meta_handle instance, meta_any * const args, const std::size_t sz) const {
  11839. return sz == size() ? node->invoke(instance, args) : meta_any{};
  11840. }
  11841. /**
  11842. * @copybrief invoke
  11843. *
  11844. * @sa invoke
  11845. *
  11846. * @tparam Args Types of arguments to use to invoke the function.
  11847. * @param instance An opaque instance of the underlying type.
  11848. * @param args Parameters to use to invoke the function.
  11849. * @return A meta any containing the new instance, if any.
  11850. */
  11851. template<typename... Args>
  11852. meta_any invoke(meta_handle instance, Args &&... args) const {
  11853. std::array<meta_any, sizeof...(Args)> arguments{std::forward<Args>(args)...};
  11854. return invoke(instance, arguments.data(), sizeof...(Args));
  11855. }
  11856. /*! @copydoc meta_ctor::prop */
  11857. [[nodiscard]] meta_range<meta_prop> prop() const ENTT_NOEXCEPT {
  11858. return node->prop;
  11859. }
  11860. /**
  11861. * @brief Returns the property associated with a given key.
  11862. * @param key The key to use to search for a property.
  11863. * @return The property associated with the given key, if any.
  11864. */
  11865. [[nodiscard]] meta_prop prop(meta_any key) const {
  11866. internal::meta_range range{node->prop};
  11867. return std::find_if(range.begin(), range.end(), [&key](const auto &curr) { return curr.key() == key; }).operator->();
  11868. }
  11869. /**
  11870. * @brief Returns true if a meta object is valid, false otherwise.
  11871. * @return True if the meta object is valid, false otherwise.
  11872. */
  11873. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  11874. return !(node == nullptr);
  11875. }
  11876. private:
  11877. const node_type *node;
  11878. };
  11879. /*! @brief Opaque wrapper for meta types. */
  11880. class meta_type {
  11881. template<typename... Args, std::size_t... Indexes>
  11882. [[nodiscard]] auto ctor(std::index_sequence<Indexes...>) const {
  11883. internal::meta_range range{node->ctor};
  11884. return std::find_if(range.begin(), range.end(), [](const auto &candidate) {
  11885. return candidate.size == sizeof...(Args) && ([](auto *from, auto *to) {
  11886. return (from->type_id == to->type_id)
  11887. || internal::find_if<&node_type::base>([to](const auto *curr) { return curr->type()->type_id == to->type_id; }, from)
  11888. || internal::find_if<&node_type::conv>([to](const auto *curr) { return curr->type()->type_id == to->type_id; }, from);
  11889. }(internal::meta_info<Args>::resolve(), candidate.arg(Indexes)) && ...);
  11890. }).operator->();
  11891. }
  11892. public:
  11893. /*! @brief Node type. */
  11894. using node_type = internal::meta_type_node;
  11895. /*! @brief Unsigned integer type. */
  11896. using size_type = typename node_type::size_type;
  11897. /*! @copydoc meta_prop::meta_prop */
  11898. meta_type(node_type *curr = nullptr) ENTT_NOEXCEPT
  11899. : node{curr}
  11900. {}
  11901. /**
  11902. * @brief Returns the type id of the underlying type.
  11903. * @return The type id of the underlying type.
  11904. */
  11905. [[nodiscard]] id_type type_id() const ENTT_NOEXCEPT {
  11906. return node->type_id;
  11907. }
  11908. /**
  11909. * @brief Returns the identifier assigned to a meta object.
  11910. * @return The identifier assigned to the meta object.
  11911. */
  11912. [[nodiscard]] id_type id() const ENTT_NOEXCEPT {
  11913. return node->id;
  11914. }
  11915. /**
  11916. * @brief Checks whether a type refers to void or not.
  11917. * @return True if the underlying type is void, false otherwise.
  11918. */
  11919. [[nodiscard]] bool is_void() const ENTT_NOEXCEPT {
  11920. return node->is_void;
  11921. }
  11922. /**
  11923. * @brief Checks whether a type refers to an integral type or not.
  11924. * @return True if the underlying type is an integral type, false otherwise.
  11925. */
  11926. [[nodiscard]] bool is_integral() const ENTT_NOEXCEPT {
  11927. return node->is_integral;
  11928. }
  11929. /**
  11930. * @brief Checks whether a type refers to a floating-point type or not.
  11931. * @return True if the underlying type is a floating-point type, false
  11932. * otherwise.
  11933. */
  11934. [[nodiscard]] bool is_floating_point() const ENTT_NOEXCEPT {
  11935. return node->is_floating_point;
  11936. }
  11937. /**
  11938. * @brief Checks whether a type refers to an array type or not.
  11939. * @return True if the underlying type is an array type, false otherwise.
  11940. */
  11941. [[nodiscard]] bool is_array() const ENTT_NOEXCEPT {
  11942. return node->is_array;
  11943. }
  11944. /**
  11945. * @brief Checks whether a type refers to an enum or not.
  11946. * @return True if the underlying type is an enum, false otherwise.
  11947. */
  11948. [[nodiscard]] bool is_enum() const ENTT_NOEXCEPT {
  11949. return node->is_enum;
  11950. }
  11951. /**
  11952. * @brief Checks whether a type refers to an union or not.
  11953. * @return True if the underlying type is an union, false otherwise.
  11954. */
  11955. [[nodiscard]] bool is_union() const ENTT_NOEXCEPT {
  11956. return node->is_union;
  11957. }
  11958. /**
  11959. * @brief Checks whether a type refers to a class or not.
  11960. * @return True if the underlying type is a class, false otherwise.
  11961. */
  11962. [[nodiscard]] bool is_class() const ENTT_NOEXCEPT {
  11963. return node->is_class;
  11964. }
  11965. /**
  11966. * @brief Checks whether a type refers to a pointer or not.
  11967. * @return True if the underlying type is a pointer, false otherwise.
  11968. */
  11969. [[nodiscard]] bool is_pointer() const ENTT_NOEXCEPT {
  11970. return node->is_pointer;
  11971. }
  11972. /**
  11973. * @brief Checks whether a type refers to a function pointer or not.
  11974. * @return True if the underlying type is a function pointer, false
  11975. * otherwise.
  11976. */
  11977. [[nodiscard]] bool is_function_pointer() const ENTT_NOEXCEPT {
  11978. return node->is_function_pointer;
  11979. }
  11980. /**
  11981. * @brief Checks whether a type refers to a pointer to data member or not.
  11982. * @return True if the underlying type is a pointer to data member, false
  11983. * otherwise.
  11984. */
  11985. [[nodiscard]] bool is_member_object_pointer() const ENTT_NOEXCEPT {
  11986. return node->is_member_object_pointer;
  11987. }
  11988. /**
  11989. * @brief Checks whether a type refers to a pointer to member function or
  11990. * not.
  11991. * @return True if the underlying type is a pointer to member function,
  11992. * false otherwise.
  11993. */
  11994. [[nodiscard]] bool is_member_function_pointer() const ENTT_NOEXCEPT {
  11995. return node->is_member_function_pointer;
  11996. }
  11997. /**
  11998. * @brief Checks whether a type is a pointer-like type or not.
  11999. * @return True if the underlying type is a pointer-like one, false
  12000. * otherwise.
  12001. */
  12002. [[nodiscard]] bool is_pointer_like() const ENTT_NOEXCEPT {
  12003. return node->is_pointer_like;
  12004. }
  12005. /**
  12006. * @brief Checks whether a type refers to a sequence container or not.
  12007. * @return True if the underlying type is a sequence container, false
  12008. * otherwise.
  12009. */
  12010. [[nodiscard]] bool is_sequence_container() const ENTT_NOEXCEPT {
  12011. return node->is_sequence_container;
  12012. }
  12013. /**
  12014. * @brief Checks whether a type refers to an associative container or not.
  12015. * @return True if the underlying type is an associative container, false
  12016. * otherwise.
  12017. */
  12018. [[nodiscard]] bool is_associative_container() const ENTT_NOEXCEPT {
  12019. return node->is_associative_container;
  12020. }
  12021. /**
  12022. * @brief If a type refers to an array type, provides the number of
  12023. * dimensions of the array.
  12024. * @return The number of dimensions of the array if the underlying type is
  12025. * an array type, 0 otherwise.
  12026. */
  12027. [[nodiscard]] size_type rank() const ENTT_NOEXCEPT {
  12028. return node->rank;
  12029. }
  12030. /**
  12031. * @brief If a type refers to an array type, provides the number of elements
  12032. * along the given dimension of the array.
  12033. * @param dim The dimension of which to return the number of elements.
  12034. * @return The number of elements along the given dimension of the array if
  12035. * the underlying type is an array type, 0 otherwise.
  12036. */
  12037. [[nodiscard]] size_type extent(size_type dim = {}) const ENTT_NOEXCEPT {
  12038. return node->extent(dim);
  12039. }
  12040. /**
  12041. * @brief Provides the meta type for which the pointer is defined.
  12042. * @return The meta type for which the pointer is defined or this meta type
  12043. * if it doesn't refer to a pointer type.
  12044. */
  12045. [[nodiscard]] meta_type remove_pointer() const ENTT_NOEXCEPT {
  12046. return node->remove_pointer();
  12047. }
  12048. /**
  12049. * @brief Provides the meta type for which the array is defined.
  12050. * @return The meta type for which the array is defined or this meta type
  12051. * if it doesn't refer to an array type.
  12052. */
  12053. [[nodiscard]] meta_type remove_extent() const ENTT_NOEXCEPT {
  12054. return node->remove_extent();
  12055. }
  12056. /**
  12057. * @brief Returns a range to use to visit top-level meta bases.
  12058. * @return An iterable range to use to visit top-level meta bases.
  12059. */
  12060. [[nodiscard]] meta_range<meta_base> base() const ENTT_NOEXCEPT {
  12061. return node->base;
  12062. }
  12063. /**
  12064. * @brief Returns the meta base associated with a given identifier.
  12065. * @param id Unique identifier.
  12066. * @return The meta base associated with the given identifier, if any.
  12067. */
  12068. [[nodiscard]] meta_base base(const id_type id) const {
  12069. return internal::find_if<&node_type::base>([id](const auto *curr) {
  12070. return curr->type()->id == id;
  12071. }, node);
  12072. }
  12073. /**
  12074. * @brief Returns a range to use to visit top-level meta conversion
  12075. * functions.
  12076. * @return An iterable range to use to visit top-level meta conversion
  12077. * functions.
  12078. */
  12079. [[nodiscard]] meta_range<meta_conv> conv() const ENTT_NOEXCEPT {
  12080. return node->conv;
  12081. }
  12082. /**
  12083. * @brief Returns the meta conversion function associated with a given type.
  12084. * @tparam Type The type to use to search for a meta conversion function.
  12085. * @return The meta conversion function associated with the given type, if
  12086. * any.
  12087. */
  12088. template<typename Type>
  12089. [[nodiscard]] meta_conv conv() const {
  12090. return internal::find_if<&node_type::conv>([type_id = internal::meta_info<Type>::resolve()->type_id](const auto *curr) {
  12091. return curr->type()->type_id == type_id;
  12092. }, node);
  12093. }
  12094. /**
  12095. * @brief Returns a range to use to visit top-level meta constructors.
  12096. * @return An iterable range to use to visit top-level meta constructors.
  12097. */
  12098. [[nodiscard]] meta_range<meta_ctor> ctor() const ENTT_NOEXCEPT {
  12099. return node->ctor;
  12100. }
  12101. /**
  12102. * @brief Returns the meta constructor that accepts a given list of types of
  12103. * arguments.
  12104. * @return The requested meta constructor, if any.
  12105. */
  12106. template<typename... Args>
  12107. [[nodiscard]] meta_ctor ctor() const {
  12108. return ctor<Args...>(std::index_sequence_for<Args...>{});
  12109. }
  12110. /**
  12111. * @brief Returns a range to use to visit top-level meta data.
  12112. * @return An iterable range to use to visit top-level meta data.
  12113. */
  12114. [[nodiscard]] meta_range<meta_data> data() const ENTT_NOEXCEPT {
  12115. return node->data;
  12116. }
  12117. /**
  12118. * @brief Returns the meta data associated with a given identifier.
  12119. *
  12120. * The meta data of the base classes will also be visited, if any.
  12121. *
  12122. * @param id Unique identifier.
  12123. * @return The meta data associated with the given identifier, if any.
  12124. */
  12125. [[nodiscard]] meta_data data(const id_type id) const {
  12126. return internal::find_if<&node_type::data>([id](const auto *curr) {
  12127. return curr->id == id;
  12128. }, node);
  12129. }
  12130. /**
  12131. * @brief Returns a range to use to visit top-level meta functions.
  12132. * @return An iterable range to use to visit top-level meta functions.
  12133. */
  12134. [[nodiscard]] meta_range<meta_func> func() const ENTT_NOEXCEPT {
  12135. return node->func;
  12136. }
  12137. /**
  12138. * @brief Returns the meta function associated with a given identifier.
  12139. *
  12140. * The meta functions of the base classes will also be visited, if any.
  12141. *
  12142. * @param id Unique identifier.
  12143. * @return The meta function associated with the given identifier, if any.
  12144. */
  12145. [[nodiscard]] meta_func func(const id_type id) const {
  12146. return internal::find_if<&node_type::func>([id](const auto *curr) {
  12147. return curr->id == id;
  12148. }, node);
  12149. }
  12150. /**
  12151. * @brief Creates an instance of the underlying type, if possible.
  12152. *
  12153. * To create a valid instance, the parameters must be such that a cast or
  12154. * conversion to the required types is possible. Otherwise, an empty and
  12155. * thus invalid wrapper is returned.
  12156. *
  12157. * @param args Parameters to use to construct the instance.
  12158. * @param sz Number of parameters to use to construct the instance.
  12159. * @return A meta any containing the new instance, if any.
  12160. */
  12161. [[nodiscard]] meta_any construct(meta_any * const args, const std::size_t sz) const {
  12162. meta_any any{};
  12163. internal::find_if<&node_type::ctor>([args, sz, &any](const auto *curr) {
  12164. return (curr->size == sz) && (any = curr->invoke(args));
  12165. }, node);
  12166. return any;
  12167. }
  12168. /**
  12169. * @copybrief construct
  12170. *
  12171. * @sa construct
  12172. *
  12173. * @tparam Args Types of arguments to use to construct the instance.
  12174. * @param args Parameters to use to construct the instance.
  12175. * @return A meta any containing the new instance, if any.
  12176. */
  12177. template<typename... Args>
  12178. [[nodiscard]] meta_any construct(Args &&... args) const {
  12179. std::array<meta_any, sizeof...(Args)> arguments{std::forward<Args>(args)...};
  12180. return construct(arguments.data(), sizeof...(Args));
  12181. }
  12182. /**
  12183. * @brief Returns a range to use to visit top-level meta properties.
  12184. * @return An iterable range to use to visit top-level meta properties.
  12185. */
  12186. [[nodiscard]] meta_range<meta_prop> prop() const ENTT_NOEXCEPT {
  12187. return node->prop;
  12188. }
  12189. /**
  12190. * @brief Returns the property associated with a given key.
  12191. *
  12192. * Properties of the base classes will also be visited, if any.
  12193. *
  12194. * @param key The key to use to search for a property.
  12195. * @return The property associated with the given key, if any.
  12196. */
  12197. [[nodiscard]] meta_prop prop(meta_any key) const {
  12198. return internal::find_if<&node_type::prop>([key = std::move(key)](const auto *curr) {
  12199. return curr->key() == key;
  12200. }, node);
  12201. }
  12202. /**
  12203. * @brief Returns true if a meta object is valid, false otherwise.
  12204. * @return True if the meta object is valid, false otherwise.
  12205. */
  12206. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  12207. return !(node == nullptr);
  12208. }
  12209. /**
  12210. * @brief Checks if two meta objects refer to the same type.
  12211. * @param other The meta object with which to compare.
  12212. * @return True if the two meta objects refer to the same type, false
  12213. * otherwise.
  12214. */
  12215. [[nodiscard]] bool operator==(const meta_type &other) const ENTT_NOEXCEPT {
  12216. return (!node && !other.node) || (node && other.node && node->type_id == other.node->type_id);
  12217. }
  12218. /**
  12219. * @brief Resets a meta type and all its parts.
  12220. *
  12221. * This function resets a meta type and all its data members, member
  12222. * functions and properties, as well as its constructors, destructors and
  12223. * conversion functions if any.<br/>
  12224. * Base classes aren't reset but the link between the two types is removed.
  12225. *
  12226. * The meta type is also removed from the list of searchable types.
  12227. */
  12228. void reset() ENTT_NOEXCEPT {
  12229. auto** it = internal::meta_context::global();
  12230. while (*it && *it != node) {
  12231. it = &(*it)->next;
  12232. }
  12233. if(*it) {
  12234. *it = (*it)->next;
  12235. }
  12236. const auto unregister_all = y_combinator{
  12237. [](auto &&self, auto **curr, auto... member) {
  12238. while(*curr) {
  12239. auto *prev = *curr;
  12240. (self(&(prev->*member)), ...);
  12241. *curr = prev->next;
  12242. prev->next = nullptr;
  12243. }
  12244. }
  12245. };
  12246. unregister_all(&node->prop);
  12247. unregister_all(&node->base);
  12248. unregister_all(&node->conv);
  12249. unregister_all(&node->ctor, &internal::meta_ctor_node::prop);
  12250. unregister_all(&node->data, &internal::meta_data_node::prop);
  12251. unregister_all(&node->func, &internal::meta_func_node::prop);
  12252. node->id = {};
  12253. node->dtor = nullptr;
  12254. }
  12255. private:
  12256. node_type *node;
  12257. };
  12258. /**
  12259. * @brief Checks if two meta objects refer to the same type.
  12260. * @param lhs A meta object, either valid or not.
  12261. * @param rhs A meta object, either valid or not.
  12262. * @return False if the two meta objects refer to the same node, true otherwise.
  12263. */
  12264. [[nodiscard]] inline bool operator!=(const meta_type &lhs, const meta_type &rhs) ENTT_NOEXCEPT {
  12265. return !(lhs == rhs);
  12266. }
  12267. [[nodiscard]] inline meta_type meta_any::type() const ENTT_NOEXCEPT {
  12268. return node;
  12269. }
  12270. [[nodiscard]] inline meta_type meta_base::parent() const ENTT_NOEXCEPT {
  12271. return node->parent;
  12272. }
  12273. [[nodiscard]] inline meta_type meta_base::type() const ENTT_NOEXCEPT {
  12274. return node->type();
  12275. }
  12276. [[nodiscard]] inline meta_type meta_conv::parent() const ENTT_NOEXCEPT {
  12277. return node->parent;
  12278. }
  12279. [[nodiscard]] inline meta_type meta_conv::type() const ENTT_NOEXCEPT {
  12280. return node->type();
  12281. }
  12282. [[nodiscard]] inline meta_type meta_ctor::parent() const ENTT_NOEXCEPT {
  12283. return node->parent;
  12284. }
  12285. [[nodiscard]] inline meta_type meta_ctor::arg(size_type index) const ENTT_NOEXCEPT {
  12286. return index < size() ? node->arg(index) : nullptr;
  12287. }
  12288. [[nodiscard]] inline meta_type meta_data::parent() const ENTT_NOEXCEPT {
  12289. return node->parent;
  12290. }
  12291. [[nodiscard]] inline meta_type meta_data::type() const ENTT_NOEXCEPT {
  12292. return node->type();
  12293. }
  12294. [[nodiscard]] inline meta_type meta_func::parent() const ENTT_NOEXCEPT {
  12295. return node->parent;
  12296. }
  12297. [[nodiscard]] inline meta_type meta_func::ret() const ENTT_NOEXCEPT {
  12298. return node->ret();
  12299. }
  12300. [[nodiscard]] inline meta_type meta_func::arg(size_type index) const ENTT_NOEXCEPT {
  12301. return index < size() ? node->arg(index) : nullptr;
  12302. }
  12303. /*! @brief Opaque iterator for meta sequence containers. */
  12304. class meta_sequence_container::meta_iterator {
  12305. /*! @brief A meta sequence container can access the underlying iterator. */
  12306. friend class meta_sequence_container;
  12307. template<typename It>
  12308. static void incr(meta_any any) {
  12309. ++any.cast<It>();
  12310. }
  12311. template<typename It>
  12312. [[nodiscard]] static meta_any deref(meta_any any) {
  12313. if constexpr(std::is_const_v<std::remove_reference_t<decltype(*std::declval<It>())>>) {
  12314. return *any.cast<It>();
  12315. } else {
  12316. return std::ref(*any.cast<It>());
  12317. }
  12318. }
  12319. public:
  12320. /*! @brief Signed integer type. */
  12321. using difference_type = std::ptrdiff_t;
  12322. /*! @brief Type of elements returned by the iterator. */
  12323. using value_type = meta_any;
  12324. /*! @brief Pointer type, `void` on purpose. */
  12325. using pointer = void;
  12326. /*! @brief Reference type, it is **not** an actual reference. */
  12327. using reference = value_type;
  12328. /*! @brief Iterator category. */
  12329. using iterator_category = std::input_iterator_tag;
  12330. /*! @brief Default constructor. */
  12331. meta_iterator() ENTT_NOEXCEPT = default;
  12332. /**
  12333. * @brief Constructs a meta iterator from a given iterator.
  12334. * @tparam It Type of actual iterator with which to build the meta iterator.
  12335. * @param iter The actual iterator with which to build the meta iterator.
  12336. */
  12337. template<typename It>
  12338. meta_iterator(It iter)
  12339. : next_fn{&incr<It>},
  12340. get_fn{&deref<It>},
  12341. handle{std::move(iter)}
  12342. {}
  12343. /*! @brief Pre-increment operator. @return This iterator. */
  12344. meta_iterator & operator++() ENTT_NOEXCEPT {
  12345. return next_fn(handle.ref()), *this;
  12346. }
  12347. /*! @brief Post-increment operator. @return This iterator. */
  12348. meta_iterator operator++(int) ENTT_NOEXCEPT {
  12349. meta_iterator orig = *this;
  12350. return ++(*this), orig;
  12351. }
  12352. /**
  12353. * @brief Checks if two meta iterators refer to the same element.
  12354. * @param other The meta iterator with which to compare.
  12355. * @return True if the two meta iterators refer to the same element, false
  12356. * otherwise.
  12357. */
  12358. [[nodiscard]] bool operator==(const meta_iterator &other) const ENTT_NOEXCEPT {
  12359. return handle == other.handle;
  12360. }
  12361. /**
  12362. * @brief Checks if two meta iterators refer to the same element.
  12363. * @param other The meta iterator with which to compare.
  12364. * @return False if the two meta iterators refer to the same element, true
  12365. * otherwise.
  12366. */
  12367. [[nodiscard]] bool operator!=(const meta_iterator &other) const ENTT_NOEXCEPT {
  12368. return !(*this == other);
  12369. }
  12370. /**
  12371. * @brief Indirection operator.
  12372. * @return The element to which the meta pointer points.
  12373. */
  12374. [[nodiscard]] reference operator*() const {
  12375. return get_fn(handle.ref());
  12376. }
  12377. /**
  12378. * @brief Returns false if an iterator is invalid, true otherwise.
  12379. * @return False if the iterator is invalid, true otherwise.
  12380. */
  12381. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  12382. return static_cast<bool>(handle);
  12383. }
  12384. private:
  12385. void(* next_fn)(meta_any);
  12386. meta_any(* get_fn)(meta_any);
  12387. meta_any handle;
  12388. };
  12389. template<typename Type>
  12390. struct meta_sequence_container::meta_sequence_container_proxy {
  12391. using traits_type = meta_sequence_container_traits<Type>;
  12392. [[nodiscard]] static meta_type value_type() ENTT_NOEXCEPT {
  12393. return internal::meta_info<typename traits_type::value_type>::resolve();
  12394. }
  12395. [[nodiscard]] static size_type size(const void *container) ENTT_NOEXCEPT {
  12396. return traits_type::size(*static_cast<const Type *>(container));
  12397. }
  12398. [[nodiscard]] static bool resize(void *container, size_type sz) {
  12399. return traits_type::resize(*static_cast<Type *>(container), sz);
  12400. }
  12401. [[nodiscard]] static bool clear(void *container) {
  12402. return traits_type::clear(*static_cast<Type *>(container));
  12403. }
  12404. [[nodiscard]] static iterator begin(void *container) {
  12405. return iterator{traits_type::begin(*static_cast<Type *>(container))};
  12406. }
  12407. [[nodiscard]] static iterator end(void *container) {
  12408. return iterator{traits_type::end(*static_cast<Type *>(container))};
  12409. }
  12410. [[nodiscard]] static std::pair<iterator, bool> insert(void *container, iterator it, meta_any value) {
  12411. if(const auto *v_ptr = value.try_cast<typename traits_type::value_type>(); v_ptr || value.convert<typename traits_type::value_type>()) {
  12412. auto ret = traits_type::insert(*static_cast<Type *>(container), it.handle.cast<typename traits_type::iterator>(), v_ptr ? *v_ptr : value.cast<typename traits_type::value_type>());
  12413. return {iterator{std::move(ret.first)}, ret.second};
  12414. }
  12415. return {};
  12416. }
  12417. [[nodiscard]] static std::pair<iterator, bool> erase(void *container, iterator it) {
  12418. auto ret = traits_type::erase(*static_cast<Type *>(container), it.handle.cast<typename traits_type::iterator>());
  12419. return {iterator{std::move(ret.first)}, ret.second};
  12420. }
  12421. [[nodiscard]] static meta_any get(void *container, size_type pos) {
  12422. return std::ref(traits_type::get(*static_cast<Type *>(container), pos));
  12423. }
  12424. };
  12425. /**
  12426. * @brief Returns the value meta type of the wrapped container type.
  12427. * @return The value meta type of the wrapped container type.
  12428. */
  12429. [[nodiscard]] inline meta_type meta_sequence_container::value_type() const ENTT_NOEXCEPT {
  12430. return value_type_fn();
  12431. }
  12432. /**
  12433. * @brief Returns the size of the wrapped container.
  12434. * @return The size of the wrapped container.
  12435. */
  12436. [[nodiscard]] inline meta_sequence_container::size_type meta_sequence_container::size() const ENTT_NOEXCEPT {
  12437. return size_fn(instance);
  12438. }
  12439. /**
  12440. * @brief Resizes the wrapped container to contain a given number of elements.
  12441. * @param sz The new size of the container.
  12442. * @return True in case of success, false otherwise.
  12443. */
  12444. inline bool meta_sequence_container::resize(size_type sz) const {
  12445. return resize_fn(instance, sz);
  12446. }
  12447. /**
  12448. * @brief Clears the content of the wrapped container.
  12449. * @return True in case of success, false otherwise.
  12450. */
  12451. inline bool meta_sequence_container::clear() {
  12452. return clear_fn(instance);
  12453. }
  12454. /**
  12455. * @brief Returns a meta iterator to the first element of the wrapped container.
  12456. * @return A meta iterator to the first element of the wrapped container.
  12457. */
  12458. [[nodiscard]] inline meta_sequence_container::iterator meta_sequence_container::begin() {
  12459. return begin_fn(instance);
  12460. }
  12461. /**
  12462. * @brief Returns a meta iterator that is past the last element of the wrapped
  12463. * container.
  12464. * @return A meta iterator that is past the last element of the wrapped
  12465. * container.
  12466. */
  12467. [[nodiscard]] inline meta_sequence_container::iterator meta_sequence_container::end() {
  12468. return end_fn(instance);
  12469. }
  12470. /**
  12471. * @brief Inserts an element at a specified location of the wrapped container.
  12472. * @param it Meta iterator before which the element will be inserted.
  12473. * @param value Element value to insert.
  12474. * @return A pair consisting of a meta iterator to the inserted element (in
  12475. * case of success) and a bool denoting whether the insertion took place.
  12476. */
  12477. inline std::pair<meta_sequence_container::iterator, bool> meta_sequence_container::insert(iterator it, meta_any value) {
  12478. return insert_fn(instance, it, value.ref());
  12479. }
  12480. /**
  12481. * @brief Removes the specified element from the wrapped container.
  12482. * @param it Meta iterator to the element to remove.
  12483. * @return A pair consisting of a meta iterator following the last removed
  12484. * element (in case of success) and a bool denoting whether the insertion
  12485. * took place.
  12486. */
  12487. inline std::pair<meta_sequence_container::iterator, bool> meta_sequence_container::erase(iterator it) {
  12488. return erase_fn(instance, it);
  12489. }
  12490. /**
  12491. * @brief Returns a reference to the element at a specified location of the
  12492. * wrapped container (no bounds checking is performed).
  12493. * @param pos The position of the element to return.
  12494. * @return A reference to the requested element properly wrapped.
  12495. */
  12496. [[nodiscard]] inline meta_any meta_sequence_container::operator[](size_type pos) {
  12497. return get_fn(instance, pos);
  12498. }
  12499. /**
  12500. * @brief Returns false if a proxy is invalid, true otherwise.
  12501. * @return False if the proxy is invalid, true otherwise.
  12502. */
  12503. [[nodiscard]] inline meta_sequence_container::operator bool() const ENTT_NOEXCEPT {
  12504. return (instance != nullptr);
  12505. }
  12506. /*! @brief Opaque iterator for meta associative containers. */
  12507. class meta_associative_container::meta_iterator {
  12508. template<typename It>
  12509. static void incr(meta_any any) {
  12510. ++any.cast<It>();
  12511. }
  12512. template<bool KeyOnly, typename It>
  12513. [[nodiscard]] static meta_any key(meta_any any) {
  12514. if constexpr(KeyOnly) {
  12515. return *any.cast<It>();
  12516. } else {
  12517. return any.cast<It>()->first;
  12518. }
  12519. }
  12520. template<bool KeyOnly, typename It>
  12521. [[nodiscard]] static meta_any value([[maybe_unused]] meta_any any) {
  12522. if constexpr(KeyOnly) {
  12523. return meta_any{};
  12524. } else {
  12525. return std::ref(any.cast<It>()->second);
  12526. }
  12527. }
  12528. public:
  12529. /*! @brief Signed integer type. */
  12530. using difference_type = std::ptrdiff_t;
  12531. /*! @brief Type of elements returned by the iterator. */
  12532. using value_type = std::pair<meta_any, meta_any>;
  12533. /*! @brief Pointer type, `void` on purpose. */
  12534. using pointer = void;
  12535. /*! @brief Reference type, it is **not** an actual reference. */
  12536. using reference = value_type;
  12537. /*! @brief Iterator category. */
  12538. using iterator_category = std::input_iterator_tag;
  12539. /*! @brief Default constructor. */
  12540. meta_iterator() ENTT_NOEXCEPT = default;
  12541. /**
  12542. * @brief Constructs a meta iterator from a given iterator.
  12543. * @tparam KeyOnly True if the associative container is also key-only, false
  12544. * otherwise.
  12545. * @tparam It Type of actual iterator with which to build the meta iterator.
  12546. * @param iter The actual iterator with which to build the meta iterator.
  12547. */
  12548. template<bool KeyOnly, typename It>
  12549. meta_iterator(std::integral_constant<bool, KeyOnly>, It iter)
  12550. : next_fn{&incr<It>},
  12551. key_fn{&key<KeyOnly, It>},
  12552. value_fn{&value<KeyOnly, It>},
  12553. handle{std::move(iter)}
  12554. {}
  12555. /*! @brief Pre-increment operator. @return This iterator. */
  12556. meta_iterator & operator++() ENTT_NOEXCEPT {
  12557. return next_fn(handle.ref()), *this;
  12558. }
  12559. /*! @brief Post-increment operator. @return This iterator. */
  12560. meta_iterator operator++(int) ENTT_NOEXCEPT {
  12561. meta_iterator orig = *this;
  12562. return ++(*this), orig;
  12563. }
  12564. /**
  12565. * @brief Checks if two meta iterators refer to the same element.
  12566. * @param other The meta iterator with which to compare.
  12567. * @return True if the two meta iterators refer to the same element, false
  12568. * otherwise.
  12569. */
  12570. [[nodiscard]] bool operator==(const meta_iterator &other) const ENTT_NOEXCEPT {
  12571. return handle == other.handle;
  12572. }
  12573. /**
  12574. * @brief Checks if two meta iterators refer to the same element.
  12575. * @param other The meta iterator with which to compare.
  12576. * @return False if the two meta iterators refer to the same element, true
  12577. * otherwise.
  12578. */
  12579. [[nodiscard]] bool operator!=(const meta_iterator &other) const ENTT_NOEXCEPT {
  12580. return !(*this == other);
  12581. }
  12582. /**
  12583. * @brief Indirection operator.
  12584. * @return The element to which the meta pointer points.
  12585. */
  12586. [[nodiscard]] reference operator*() const {
  12587. return { key_fn(handle.ref()), value_fn(handle.ref()) };
  12588. }
  12589. /**
  12590. * @brief Returns false if an iterator is invalid, true otherwise.
  12591. * @return False if the iterator is invalid, true otherwise.
  12592. */
  12593. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  12594. return static_cast<bool>(handle);
  12595. }
  12596. private:
  12597. void(* next_fn)(meta_any);
  12598. meta_any(* key_fn)(meta_any);
  12599. meta_any(* value_fn)(meta_any);
  12600. meta_any handle;
  12601. };
  12602. template<typename Type>
  12603. struct meta_associative_container::meta_associative_container_proxy {
  12604. using traits_type = meta_associative_container_traits<Type>;
  12605. [[nodiscard]] static meta_type key_type() ENTT_NOEXCEPT {
  12606. return internal::meta_info<typename traits_type::key_type>::resolve();
  12607. }
  12608. [[nodiscard]] static meta_type mapped_type() ENTT_NOEXCEPT {
  12609. if constexpr(is_key_only_meta_associative_container_v<Type>) {
  12610. return meta_type{};
  12611. } else {
  12612. return internal::meta_info<typename traits_type::mapped_type>::resolve();
  12613. }
  12614. }
  12615. [[nodiscard]] static meta_type value_type() ENTT_NOEXCEPT {
  12616. return internal::meta_info<typename traits_type::value_type>::resolve();
  12617. }
  12618. [[nodiscard]] static size_type size(const void *container) ENTT_NOEXCEPT {
  12619. return traits_type::size(*static_cast<const Type *>(container));
  12620. }
  12621. [[nodiscard]] static bool clear(void *container) {
  12622. return traits_type::clear(*static_cast<Type *>(container));
  12623. }
  12624. [[nodiscard]] static iterator begin(void *container) {
  12625. return iterator{is_key_only_meta_associative_container<Type>{}, traits_type::begin(*static_cast<Type *>(container))};
  12626. }
  12627. [[nodiscard]] static iterator end(void *container) {
  12628. return iterator{is_key_only_meta_associative_container<Type>{}, traits_type::end(*static_cast<Type *>(container))};
  12629. }
  12630. [[nodiscard]] static bool insert(void *container, meta_any key, meta_any value) {
  12631. if(const auto *k_ptr = key.try_cast<typename traits_type::key_type>(); k_ptr || key.convert<typename traits_type::key_type>()) {
  12632. if constexpr(is_key_only_meta_associative_container_v<Type>) {
  12633. return traits_type::insert(*static_cast<Type *>(container), k_ptr ? *k_ptr : key.cast<typename traits_type::key_type>());
  12634. } else {
  12635. if(auto *m_ptr = value.try_cast<typename traits_type::mapped_type>(); m_ptr || value.convert<typename traits_type::mapped_type>()) {
  12636. return traits_type::insert(*static_cast<Type *>(container), k_ptr ? *k_ptr : key.cast<typename traits_type::key_type>(), m_ptr ? *m_ptr : value.cast<typename traits_type::mapped_type>());
  12637. }
  12638. }
  12639. }
  12640. return false;
  12641. }
  12642. [[nodiscard]] static bool erase(void *container, meta_any key) {
  12643. if(const auto *k_ptr = key.try_cast<typename traits_type::key_type>(); k_ptr || key.convert<typename traits_type::key_type>()) {
  12644. return traits_type::erase(*static_cast<Type *>(container), k_ptr ? *k_ptr : key.cast<typename traits_type::key_type>());
  12645. }
  12646. return false;
  12647. }
  12648. [[nodiscard]] static iterator find(void *container, meta_any key) {
  12649. if(const auto *k_ptr = key.try_cast<typename traits_type::key_type>(); k_ptr || key.convert<typename traits_type::key_type>()) {
  12650. return iterator{is_key_only_meta_associative_container<Type>{}, traits_type::find(*static_cast<Type *>(container), k_ptr ? *k_ptr : key.cast<typename traits_type::key_type>())};
  12651. }
  12652. return {};
  12653. }
  12654. };
  12655. /**
  12656. * @brief Returns true if the associative container is also key-only, false
  12657. * otherwise.
  12658. * @return True if the associative container is also key-only, false otherwise.
  12659. */
  12660. [[nodiscard]] inline bool meta_associative_container::key_only() const ENTT_NOEXCEPT {
  12661. return key_only_container;
  12662. }
  12663. /**
  12664. * @brief Returns the key meta type of the wrapped container type.
  12665. * @return The key meta type of the wrapped container type.
  12666. */
  12667. [[nodiscard]] inline meta_type meta_associative_container::key_type() const ENTT_NOEXCEPT {
  12668. return key_type_fn();
  12669. }
  12670. /**
  12671. * @brief Returns the mapped meta type of the wrapped container type.
  12672. * @return The mapped meta type of the wrapped container type.
  12673. */
  12674. [[nodiscard]] inline meta_type meta_associative_container::mapped_type() const ENTT_NOEXCEPT {
  12675. return mapped_type_fn();
  12676. }
  12677. /*! @copydoc meta_sequence_container::value_type */
  12678. [[nodiscard]] inline meta_type meta_associative_container::value_type() const ENTT_NOEXCEPT {
  12679. return value_type_fn();
  12680. }
  12681. /*! @copydoc meta_sequence_container::size */
  12682. [[nodiscard]] inline meta_associative_container::size_type meta_associative_container::size() const ENTT_NOEXCEPT {
  12683. return size_fn(instance);
  12684. }
  12685. /*! @copydoc meta_sequence_container::clear */
  12686. inline bool meta_associative_container::clear() {
  12687. return clear_fn(instance);
  12688. }
  12689. /*! @copydoc meta_sequence_container::begin */
  12690. [[nodiscard]] inline meta_associative_container::iterator meta_associative_container::begin() {
  12691. return begin_fn(instance);
  12692. }
  12693. /*! @copydoc meta_sequence_container::end */
  12694. [[nodiscard]] inline meta_associative_container::iterator meta_associative_container::end() {
  12695. return end_fn(instance);
  12696. }
  12697. /**
  12698. * @brief Inserts an element (a key/value pair) into the wrapped container.
  12699. * @param key The key of the element to insert.
  12700. * @param value The value of the element to insert.
  12701. * @return A bool denoting whether the insertion took place.
  12702. */
  12703. inline bool meta_associative_container::insert(meta_any key, meta_any value = {}) {
  12704. return insert_fn(instance, key.ref(), value.ref());
  12705. }
  12706. /**
  12707. * @brief Removes the specified element from the wrapped container.
  12708. * @param key The key of the element to remove.
  12709. * @return A bool denoting whether the removal took place.
  12710. */
  12711. inline bool meta_associative_container::erase(meta_any key) {
  12712. return erase_fn(instance, key.ref());
  12713. }
  12714. /**
  12715. * @brief Returns an iterator to the element with key equivalent to a given
  12716. * one, if any.
  12717. * @param key The key of the element to search.
  12718. * @return An iterator to the element with the given key, if any.
  12719. */
  12720. [[nodiscard]] inline meta_associative_container::iterator meta_associative_container::find(meta_any key) {
  12721. return find_fn(instance, key.ref());
  12722. }
  12723. /**
  12724. * @brief Returns false if a proxy is invalid, true otherwise.
  12725. * @return False if the proxy is invalid, true otherwise.
  12726. */
  12727. [[nodiscard]] inline meta_associative_container::operator bool() const ENTT_NOEXCEPT {
  12728. return (instance != nullptr);
  12729. }
  12730. }
  12731. #endif
  12732. // #include "policy.hpp"
  12733. #ifndef ENTT_META_POLICY_HPP
  12734. #define ENTT_META_POLICY_HPP
  12735. namespace entt {
  12736. /*! @brief Empty class type used to request the _as ref_ policy. */
  12737. struct as_ref_t {};
  12738. /*! @brief Disambiguation tag. */
  12739. inline constexpr as_ref_t as_ref;
  12740. /*! @brief Empty class type used to request the _as-is_ policy. */
  12741. struct as_is_t {};
  12742. /*! @brief Empty class type used to request the _as void_ policy. */
  12743. struct as_void_t {};
  12744. }
  12745. #endif
  12746. namespace entt {
  12747. /**
  12748. * @cond TURN_OFF_DOXYGEN
  12749. * Internal details not to be documented.
  12750. */
  12751. namespace internal {
  12752. template<typename, bool = false>
  12753. struct meta_function_helper;
  12754. template<typename Ret, typename... Args, bool Const>
  12755. struct meta_function_helper<Ret(Args...), Const> {
  12756. using return_type = std::remove_cv_t<std::remove_reference_t<Ret>>;
  12757. using args_type = std::tuple<std::remove_cv_t<std::remove_reference_t<Args>>...>;
  12758. static constexpr auto is_const = Const;
  12759. [[nodiscard]] static auto arg(typename internal::meta_func_node::size_type index) ENTT_NOEXCEPT {
  12760. return std::array<meta_type_node *, sizeof...(Args)>{{meta_info<Args>::resolve()...}}[index];
  12761. }
  12762. };
  12763. template<typename Ret, typename... Args, typename Class>
  12764. constexpr meta_function_helper<Ret(Args...), true>
  12765. to_meta_function_helper(Ret(Class:: *)(Args...) const);
  12766. template<typename Ret, typename... Args, typename Class>
  12767. constexpr meta_function_helper<Ret(Args...)>
  12768. to_meta_function_helper(Ret(Class:: *)(Args...));
  12769. template<typename Ret, typename... Args>
  12770. constexpr meta_function_helper<Ret(Args...)>
  12771. to_meta_function_helper(Ret(*)(Args...));
  12772. constexpr void to_meta_function_helper(...);
  12773. template<typename Candidate>
  12774. using meta_function_helper_t = decltype(to_meta_function_helper(std::declval<Candidate>()));
  12775. template<typename Type, typename... Args, std::size_t... Indexes>
  12776. [[nodiscard]] meta_any construct(meta_any * const args, std::index_sequence<Indexes...>) {
  12777. [[maybe_unused]] auto direct = std::make_tuple((args+Indexes)->try_cast<Args>()...);
  12778. return ((std::get<Indexes>(direct) || (args+Indexes)->convert<Args>()) && ...)
  12779. ? Type{(std::get<Indexes>(direct) ? *std::get<Indexes>(direct) : (args+Indexes)->cast<Args>())...}
  12780. : meta_any{};
  12781. }
  12782. template<typename Type, auto Data>
  12783. [[nodiscard]] bool setter([[maybe_unused]] meta_handle instance, [[maybe_unused]] meta_any value) {
  12784. bool accepted = false;
  12785. if constexpr(std::is_function_v<std::remove_reference_t<std::remove_pointer_t<decltype(Data)>>> || std::is_member_function_pointer_v<decltype(Data)>) {
  12786. using helper_type = meta_function_helper_t<decltype(Data)>;
  12787. using data_type = std::tuple_element_t<!std::is_member_function_pointer_v<decltype(Data)>, typename helper_type::args_type>;
  12788. if(auto * const clazz = instance->try_cast<Type>(); clazz) {
  12789. if(auto * const direct = value.try_cast<data_type>(); direct || value.convert<data_type>()) {
  12790. std::invoke(Data, *clazz, direct ? *direct : value.cast<data_type>());
  12791. accepted = true;
  12792. }
  12793. }
  12794. } else if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
  12795. using data_type = std::remove_cv_t<std::remove_reference_t<decltype(std::declval<Type>().*Data)>>;
  12796. if constexpr(!std::is_array_v<data_type>) {
  12797. if(auto * const clazz = instance->try_cast<Type>(); clazz) {
  12798. if(auto * const direct = value.try_cast<data_type>(); direct || value.convert<data_type>()) {
  12799. std::invoke(Data, clazz) = (direct ? *direct : value.cast<data_type>());
  12800. accepted = true;
  12801. }
  12802. }
  12803. }
  12804. } else {
  12805. using data_type = std::remove_cv_t<std::remove_reference_t<decltype(*Data)>>;
  12806. if constexpr(!std::is_array_v<data_type>) {
  12807. if(auto * const direct = value.try_cast<data_type>(); direct || value.convert<data_type>()) {
  12808. *Data = (direct ? *direct : value.cast<data_type>());
  12809. accepted = true;
  12810. }
  12811. }
  12812. }
  12813. return accepted;
  12814. }
  12815. template<typename Type, auto Data, typename Policy>
  12816. [[nodiscard]] meta_any getter([[maybe_unused]] meta_handle instance) {
  12817. [[maybe_unused]] auto dispatch = [](auto &&value) {
  12818. if constexpr(std::is_same_v<Policy, as_void_t>) {
  12819. return meta_any{std::in_place_type<void>, std::forward<decltype(value)>(value)};
  12820. } else if constexpr(std::is_same_v<Policy, as_ref_t>) {
  12821. return meta_any{std::ref(std::forward<decltype(value)>(value))};
  12822. } else {
  12823. static_assert(std::is_same_v<Policy, as_is_t>, "Policy not supported");
  12824. return meta_any{std::forward<decltype(value)>(value)};
  12825. }
  12826. };
  12827. if constexpr(std::is_function_v<std::remove_reference_t<std::remove_pointer_t<decltype(Data)>>> || std::is_member_function_pointer_v<decltype(Data)>) {
  12828. auto * const clazz = instance->try_cast<Type>();
  12829. return clazz ? dispatch(std::invoke(Data, *clazz)) : meta_any{};
  12830. } else if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
  12831. if constexpr(std::is_array_v<std::remove_cv_t<std::remove_reference_t<decltype(std::declval<Type>().*Data)>>>) {
  12832. return meta_any{};
  12833. } else {
  12834. auto * const clazz = instance->try_cast<Type>();
  12835. return clazz ? dispatch(std::invoke(Data, clazz)) : meta_any{};
  12836. }
  12837. } else if constexpr(std::is_pointer_v<std::decay_t<decltype(Data)>>) {
  12838. if constexpr(std::is_array_v<std::remove_pointer_t<decltype(Data)>>) {
  12839. return meta_any{};
  12840. } else {
  12841. return dispatch(*Data);
  12842. }
  12843. } else {
  12844. return dispatch(Data);
  12845. }
  12846. }
  12847. template<typename Type, auto Candidate, typename Policy, std::size_t... Indexes>
  12848. [[nodiscard]] meta_any invoke([[maybe_unused]] meta_handle instance, meta_any *args, std::index_sequence<Indexes...>) {
  12849. using helper_type = meta_function_helper_t<decltype(Candidate)>;
  12850. auto dispatch = [](auto *... params) {
  12851. if constexpr(std::is_void_v<typename helper_type::return_type> || std::is_same_v<Policy, as_void_t>) {
  12852. std::invoke(Candidate, *params...);
  12853. return meta_any{std::in_place_type<void>};
  12854. } else if constexpr(std::is_same_v<Policy, as_ref_t>) {
  12855. return meta_any{std::ref(std::invoke(Candidate, *params...))};
  12856. } else {
  12857. static_assert(std::is_same_v<Policy, as_is_t>, "Policy not supported");
  12858. return meta_any{std::invoke(Candidate, *params...)};
  12859. }
  12860. };
  12861. [[maybe_unused]] const auto direct = std::make_tuple([](meta_any *any, auto *value) {
  12862. using arg_type = std::remove_reference_t<decltype(*value)>;
  12863. if(!value && any->convert<arg_type>()) {
  12864. value = any->try_cast<arg_type>();
  12865. }
  12866. return value;
  12867. }(args+Indexes, (args+Indexes)->try_cast<std::tuple_element_t<Indexes, typename helper_type::args_type>>())...);
  12868. if constexpr(std::is_function_v<std::remove_reference_t<std::remove_pointer_t<decltype(Candidate)>>>) {
  12869. return (std::get<Indexes>(direct) && ...) ? dispatch(std::get<Indexes>(direct)...) : meta_any{};
  12870. } else {
  12871. auto * const clazz = instance->try_cast<Type>();
  12872. return (clazz && (std::get<Indexes>(direct) && ...)) ? dispatch(clazz, std::get<Indexes>(direct)...) : meta_any{};
  12873. }
  12874. }
  12875. }
  12876. /**
  12877. * Internal details not to be documented.
  12878. * @endcond
  12879. */
  12880. /**
  12881. * @brief Meta factory to be used for reflection purposes.
  12882. *
  12883. * The meta factory is an utility class used to reflect types, data members and
  12884. * functions of all sorts. This class ensures that the underlying web of types
  12885. * is built correctly and performs some checks in debug mode to ensure that
  12886. * there are no subtle errors at runtime.
  12887. */
  12888. template<typename...>
  12889. class meta_factory;
  12890. /**
  12891. * @brief Extended meta factory to be used for reflection purposes.
  12892. * @tparam Type Reflected type for which the factory was created.
  12893. * @tparam Spec Property specialization pack used to disambiguate overloads.
  12894. */
  12895. template<typename Type, typename... Spec>
  12896. class meta_factory<Type, Spec...>: public meta_factory<Type> {
  12897. [[nodiscard]] bool exists(const meta_any &key, const internal::meta_prop_node *node) ENTT_NOEXCEPT {
  12898. return node && (node->key() == key || exists(key, node->next));
  12899. }
  12900. template<std::size_t Step = 0, std::size_t... Index, typename... Property, typename... Other>
  12901. void unpack(std::index_sequence<Index...>, std::tuple<Property...> property, Other &&... other) {
  12902. unroll<Step>(choice<3>, std::move(std::get<Index>(property))..., std::forward<Other>(other)...);
  12903. }
  12904. template<std::size_t Step = 0, typename... Property, typename... Other>
  12905. void unroll(choice_t<3>, std::tuple<Property...> property, Other &&... other) {
  12906. unpack<Step>(std::index_sequence_for<Property...>{}, std::move(property), std::forward<Other>(other)...);
  12907. }
  12908. template<std::size_t Step = 0, typename... Property, typename... Other>
  12909. void unroll(choice_t<2>, std::pair<Property...> property, Other &&... other) {
  12910. assign<Step>(std::move(property.first), std::move(property.second));
  12911. unroll<Step+1>(choice<3>, std::forward<Other>(other)...);
  12912. }
  12913. template<std::size_t Step = 0, typename Property, typename... Other>
  12914. std::enable_if_t<!std::is_invocable_v<Property>>
  12915. unroll(choice_t<1>, Property &&property, Other &&... other) {
  12916. assign<Step>(std::forward<Property>(property));
  12917. unroll<Step+1>(choice<3>, std::forward<Other>(other)...);
  12918. }
  12919. template<std::size_t Step = 0, typename Func, typename... Other>
  12920. void unroll(choice_t<0>, Func &&invocable, Other &&... other) {
  12921. unroll<Step>(choice<3>, std::forward<Func>(invocable)(), std::forward<Other>(other)...);
  12922. }
  12923. template<std::size_t>
  12924. void unroll(choice_t<0>) {}
  12925. template<std::size_t = 0, typename Key, typename... Value>
  12926. void assign(Key &&key, Value &&... value) {
  12927. static const auto property{std::make_tuple(std::forward<Key>(key), std::forward<Value>(value)...)};
  12928. static internal::meta_prop_node node{
  12929. nullptr,
  12930. []() -> meta_any {
  12931. return std::get<0>(property);
  12932. },
  12933. []() -> meta_any {
  12934. if constexpr(sizeof...(Value) == 0) {
  12935. return {};
  12936. } else {
  12937. return std::get<1>(property);
  12938. }
  12939. }
  12940. };
  12941. ENTT_ASSERT(!exists(node.key(), *curr));
  12942. node.next = *curr;
  12943. *curr = &node;
  12944. }
  12945. public:
  12946. /**
  12947. * @brief Constructs an extended factory from a given node.
  12948. * @param target The underlying node to which to assign the properties.
  12949. */
  12950. meta_factory(internal::meta_prop_node **target) ENTT_NOEXCEPT
  12951. : curr{target}
  12952. {}
  12953. /**
  12954. * @brief Assigns a property to the last meta object created.
  12955. *
  12956. * Both the key and the value (if any) must be at least copy constructible.
  12957. *
  12958. * @tparam PropertyOrKey Type of the property or property key.
  12959. * @tparam Value Optional type of the property value.
  12960. * @param property_or_key Property or property key.
  12961. * @param value Optional property value.
  12962. * @return A meta factory for the parent type.
  12963. */
  12964. template<typename PropertyOrKey, typename... Value>
  12965. auto prop(PropertyOrKey &&property_or_key, Value &&... value) && {
  12966. if constexpr(sizeof...(Value) == 0) {
  12967. unroll(choice<3>, std::forward<PropertyOrKey>(property_or_key));
  12968. } else {
  12969. assign(std::forward<PropertyOrKey>(property_or_key), std::forward<Value>(value)...);
  12970. }
  12971. return meta_factory<Type, Spec..., PropertyOrKey, Value...>{curr};
  12972. }
  12973. /**
  12974. * @brief Assigns properties to the last meta object created.
  12975. *
  12976. * Both the keys and the values (if any) must be at least copy
  12977. * constructible.
  12978. *
  12979. * @tparam Property Types of the properties.
  12980. * @param property Properties to assign to the last meta object created.
  12981. * @return A meta factory for the parent type.
  12982. */
  12983. template <typename... Property>
  12984. auto props(Property... property) && {
  12985. unroll(choice<3>, std::forward<Property>(property)...);
  12986. return meta_factory<Type, Spec..., Property...>{curr};
  12987. }
  12988. private:
  12989. internal::meta_prop_node **curr;
  12990. };
  12991. /**
  12992. * @brief Basic meta factory to be used for reflection purposes.
  12993. * @tparam Type Reflected type for which the factory was created.
  12994. */
  12995. template<typename Type>
  12996. class meta_factory<Type> {
  12997. template<typename Node>
  12998. bool exists(const Node *candidate, const Node *node) ENTT_NOEXCEPT {
  12999. return node && (node == candidate || exists(candidate, node->next));
  13000. }
  13001. template<typename Node>
  13002. bool exists(const id_type id, const Node *node) ENTT_NOEXCEPT {
  13003. return node && (node->id == id || exists(id, node->next));
  13004. }
  13005. public:
  13006. /**
  13007. * @brief Makes a meta type _searchable_.
  13008. * @param id Optional unique identifier.
  13009. * @return An extended meta factory for the given type.
  13010. */
  13011. auto type(const id_type id = type_info<Type>::id()) {
  13012. auto * const node = internal::meta_info<Type>::resolve();
  13013. ENTT_ASSERT(!exists(id, *internal::meta_context::global()));
  13014. ENTT_ASSERT(!exists(node, *internal::meta_context::global()));
  13015. node->id = id;
  13016. node->next = *internal::meta_context::global();
  13017. *internal::meta_context::global() = node;
  13018. return meta_factory<Type, Type>{&node->prop};
  13019. }
  13020. /**
  13021. * @brief Assigns a meta base to a meta type.
  13022. *
  13023. * A reflected base class must be a real base class of the reflected type.
  13024. *
  13025. * @tparam Base Type of the base class to assign to the meta type.
  13026. * @return A meta factory for the parent type.
  13027. */
  13028. template<typename Base>
  13029. auto base() ENTT_NOEXCEPT {
  13030. static_assert(std::is_base_of_v<Base, Type>, "Invalid base type");
  13031. auto * const type = internal::meta_info<Type>::resolve();
  13032. static internal::meta_base_node node{
  13033. type,
  13034. nullptr,
  13035. &internal::meta_info<Base>::resolve,
  13036. [](const void *instance) ENTT_NOEXCEPT -> const void * {
  13037. return static_cast<const Base *>(static_cast<const Type *>(instance));
  13038. }
  13039. };
  13040. ENTT_ASSERT(!exists(&node, type->base));
  13041. node.next = type->base;
  13042. type->base = &node;
  13043. return meta_factory<Type>{};
  13044. }
  13045. /**
  13046. * @brief Assigns a meta conversion function to a meta type.
  13047. *
  13048. * The given type must be such that an instance of the reflected type can be
  13049. * converted to it.
  13050. *
  13051. * @tparam To Type of the conversion function to assign to the meta type.
  13052. * @return A meta factory for the parent type.
  13053. */
  13054. template<typename To>
  13055. auto conv() ENTT_NOEXCEPT {
  13056. static_assert(std::is_convertible_v<Type, To>, "Could not convert to the required type");
  13057. auto * const type = internal::meta_info<Type>::resolve();
  13058. static internal::meta_conv_node node{
  13059. type,
  13060. nullptr,
  13061. &internal::meta_info<To>::resolve,
  13062. [](const void *instance) -> meta_any {
  13063. return static_cast<To>(*static_cast<const Type *>(instance));
  13064. }
  13065. };
  13066. ENTT_ASSERT(!exists(&node, type->conv));
  13067. node.next = type->conv;
  13068. type->conv = &node;
  13069. return meta_factory<Type>{};
  13070. }
  13071. /**
  13072. * @brief Assigns a meta conversion function to a meta type.
  13073. *
  13074. * Conversion functions can be either free functions or member
  13075. * functions.<br/>
  13076. * In case of free functions, they must accept a const reference to an
  13077. * instance of the parent type as an argument. In case of member functions,
  13078. * they should have no arguments at all.
  13079. *
  13080. * @tparam Candidate The actual function to use for the conversion.
  13081. * @return A meta factory for the parent type.
  13082. */
  13083. template<auto Candidate>
  13084. auto conv() ENTT_NOEXCEPT {
  13085. using conv_type = std::invoke_result_t<decltype(Candidate), Type &>;
  13086. auto * const type = internal::meta_info<Type>::resolve();
  13087. static internal::meta_conv_node node{
  13088. type,
  13089. nullptr,
  13090. &internal::meta_info<conv_type>::resolve,
  13091. [](const void *instance) -> meta_any {
  13092. return std::invoke(Candidate, *static_cast<const Type *>(instance));
  13093. }
  13094. };
  13095. ENTT_ASSERT(!exists(&node, type->conv));
  13096. node.next = type->conv;
  13097. type->conv = &node;
  13098. return meta_factory<Type>{};
  13099. }
  13100. /**
  13101. * @brief Assigns a meta constructor to a meta type.
  13102. *
  13103. * Free functions can be assigned to meta types in the role of constructors.
  13104. * All that is required is that they return an instance of the underlying
  13105. * type.<br/>
  13106. * From a client's point of view, nothing changes if a constructor of a meta
  13107. * type is a built-in one or a free function.
  13108. *
  13109. * @tparam Func The actual function to use as a constructor.
  13110. * @tparam Policy Optional policy (no policy set by default).
  13111. * @return An extended meta factory for the parent type.
  13112. */
  13113. template<auto Func, typename Policy = as_is_t>
  13114. auto ctor() ENTT_NOEXCEPT {
  13115. using helper_type = internal::meta_function_helper_t<decltype(Func)>;
  13116. static_assert(std::is_same_v<typename helper_type::return_type, Type>, "The function doesn't return an object of the required type");
  13117. auto * const type = internal::meta_info<Type>::resolve();
  13118. static internal::meta_ctor_node node{
  13119. type,
  13120. nullptr,
  13121. nullptr,
  13122. std::tuple_size_v<typename helper_type::args_type>,
  13123. &helper_type::arg,
  13124. [](meta_any * const any) {
  13125. return internal::invoke<Type, Func, Policy>({}, any, std::make_index_sequence<std::tuple_size_v<typename helper_type::args_type>>{});
  13126. }
  13127. };
  13128. ENTT_ASSERT(!exists(&node, type->ctor));
  13129. node.next = type->ctor;
  13130. type->ctor = &node;
  13131. return meta_factory<Type, std::integral_constant<decltype(Func), Func>>{&node.prop};
  13132. }
  13133. /**
  13134. * @brief Assigns a meta constructor to a meta type.
  13135. *
  13136. * A meta constructor is uniquely identified by the types of its arguments
  13137. * and is such that there exists an actual constructor of the underlying
  13138. * type that can be invoked with parameters whose types are those given.
  13139. *
  13140. * @tparam Args Types of arguments to use to construct an instance.
  13141. * @return An extended meta factory for the parent type.
  13142. */
  13143. template<typename... Args>
  13144. auto ctor() ENTT_NOEXCEPT {
  13145. using helper_type = internal::meta_function_helper_t<Type(*)(Args...)>;
  13146. auto * const type = internal::meta_info<Type>::resolve();
  13147. static internal::meta_ctor_node node{
  13148. type,
  13149. nullptr,
  13150. nullptr,
  13151. std::tuple_size_v<typename helper_type::args_type>,
  13152. &helper_type::arg,
  13153. [](meta_any * const any) {
  13154. return internal::construct<Type, std::remove_cv_t<std::remove_reference_t<Args>>...>(any, std::make_index_sequence<std::tuple_size_v<typename helper_type::args_type>>{});
  13155. }
  13156. };
  13157. ENTT_ASSERT(!exists(&node, type->ctor));
  13158. node.next = type->ctor;
  13159. type->ctor = &node;
  13160. return meta_factory<Type, Type(Args...)>{&node.prop};
  13161. }
  13162. /**
  13163. * @brief Assigns a meta destructor to a meta type.
  13164. *
  13165. * Free functions can be assigned to meta types in the role of destructors.
  13166. * The signature of the function should identical to the following:
  13167. *
  13168. * @code{.cpp}
  13169. * void(Type &);
  13170. * @endcode
  13171. *
  13172. * The purpose is to give users the ability to free up resources that
  13173. * require special treatment before an object is actually destroyed.
  13174. *
  13175. * @tparam Func The actual function to use as a destructor.
  13176. * @return A meta factory for the parent type.
  13177. */
  13178. template<auto Func>
  13179. auto dtor() ENTT_NOEXCEPT {
  13180. static_assert(std::is_invocable_v<decltype(Func), Type &>, "The function doesn't accept an object of the type provided");
  13181. auto * const type = internal::meta_info<Type>::resolve();
  13182. ENTT_ASSERT(!type->dtor);
  13183. type->dtor = [](void *instance) {
  13184. if(instance) {
  13185. std::invoke(Func, *static_cast<Type *>(instance));
  13186. }
  13187. };
  13188. return meta_factory<Type>{};
  13189. }
  13190. /**
  13191. * @brief Assigns a meta data to a meta type.
  13192. *
  13193. * Both data members and static and global variables, as well as constants
  13194. * of any kind, can be assigned to a meta type.<br/>
  13195. * From a client's point of view, all the variables associated with the
  13196. * reflected object will appear as if they were part of the type itself.
  13197. *
  13198. * @tparam Data The actual variable to attach to the meta type.
  13199. * @tparam Policy Optional policy (no policy set by default).
  13200. * @param id Unique identifier.
  13201. * @return An extended meta factory for the parent type.
  13202. */
  13203. template<auto Data, typename Policy = as_is_t>
  13204. auto data(const id_type id) ENTT_NOEXCEPT {
  13205. if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
  13206. return data<Data, Data, Policy>(id);
  13207. } else {
  13208. using data_type = std::remove_pointer_t<std::decay_t<decltype(Data)>>;
  13209. auto * const type = internal::meta_info<Type>::resolve();
  13210. static internal::meta_data_node node{
  13211. {},
  13212. type,
  13213. nullptr,
  13214. nullptr,
  13215. true,
  13216. &internal::meta_info<data_type>::resolve,
  13217. []() -> std::remove_const_t<decltype(internal::meta_data_node::set)> {
  13218. if constexpr(std::is_same_v<Type, data_type> || std::is_const_v<data_type>) {
  13219. return nullptr;
  13220. } else {
  13221. return &internal::setter<Type, Data>;
  13222. }
  13223. }(),
  13224. &internal::getter<Type, Data, Policy>
  13225. };
  13226. ENTT_ASSERT(!exists(id, type->data));
  13227. ENTT_ASSERT(!exists(&node, type->data));
  13228. node.id = id;
  13229. node.next = type->data;
  13230. type->data = &node;
  13231. return meta_factory<Type, std::integral_constant<decltype(Data), Data>>{&node.prop};
  13232. }
  13233. }
  13234. /**
  13235. * @brief Assigns a meta data to a meta type by means of its setter and
  13236. * getter.
  13237. *
  13238. * Setters and getters can be either free functions, member functions or a
  13239. * mix of them.<br/>
  13240. * In case of free functions, setters and getters must accept a reference to
  13241. * an instance of the parent type as their first argument. A setter has then
  13242. * an extra argument of a type convertible to that of the parameter to
  13243. * set.<br/>
  13244. * In case of member functions, getters have no arguments at all, while
  13245. * setters has an argument of a type convertible to that of the parameter to
  13246. * set.
  13247. *
  13248. * @tparam Setter The actual function to use as a setter.
  13249. * @tparam Getter The actual function to use as a getter.
  13250. * @tparam Policy Optional policy (no policy set by default).
  13251. * @param id Unique identifier.
  13252. * @return An extended meta factory for the parent type.
  13253. */
  13254. template<auto Setter, auto Getter, typename Policy = as_is_t>
  13255. auto data(const id_type id) ENTT_NOEXCEPT {
  13256. using underlying_type = std::remove_reference_t<std::invoke_result_t<decltype(Getter), Type &>>;
  13257. auto * const type = internal::meta_info<Type>::resolve();
  13258. static internal::meta_data_node node{
  13259. {},
  13260. type,
  13261. nullptr,
  13262. nullptr,
  13263. false,
  13264. &internal::meta_info<underlying_type>::resolve,
  13265. []() -> std::remove_const_t<decltype(internal::meta_data_node::set)> {
  13266. if constexpr(std::is_same_v<decltype(Setter), std::nullptr_t> || (std::is_member_object_pointer_v<decltype(Setter)> && std::is_const_v<underlying_type>)) {
  13267. return nullptr;
  13268. } else {
  13269. return &internal::setter<Type, Setter>;
  13270. }
  13271. }(),
  13272. &internal::getter<Type, Getter, Policy>
  13273. };
  13274. ENTT_ASSERT(!exists(id, type->data));
  13275. ENTT_ASSERT(!exists(&node, type->data));
  13276. node.id = id;
  13277. node.next = type->data;
  13278. type->data = &node;
  13279. return meta_factory<Type, std::integral_constant<decltype(Setter), Setter>, std::integral_constant<decltype(Getter), Getter>>{&node.prop};
  13280. }
  13281. /**
  13282. * @brief Assigns a meta funcion to a meta type.
  13283. *
  13284. * Both member functions and free functions can be assigned to a meta
  13285. * type.<br/>
  13286. * From a client's point of view, all the functions associated with the
  13287. * reflected object will appear as if they were part of the type itself.
  13288. *
  13289. * @tparam Candidate The actual function to attach to the meta type.
  13290. * @tparam Policy Optional policy (no policy set by default).
  13291. * @param id Unique identifier.
  13292. * @return An extended meta factory for the parent type.
  13293. */
  13294. template<auto Candidate, typename Policy = as_is_t>
  13295. auto func(const id_type id) ENTT_NOEXCEPT {
  13296. using helper_type = internal::meta_function_helper_t<decltype(Candidate)>;
  13297. auto * const type = internal::meta_info<Type>::resolve();
  13298. static internal::meta_func_node node{
  13299. {},
  13300. type,
  13301. nullptr,
  13302. nullptr,
  13303. std::tuple_size_v<typename helper_type::args_type>,
  13304. helper_type::is_const,
  13305. !std::is_member_function_pointer_v<decltype(Candidate)>,
  13306. &internal::meta_info<std::conditional_t<std::is_same_v<Policy, as_void_t>, void, typename helper_type::return_type>>::resolve,
  13307. &helper_type::arg,
  13308. [](meta_handle instance, meta_any *args) {
  13309. return internal::invoke<Type, Candidate, Policy>(*instance, args, std::make_index_sequence<std::tuple_size_v<typename helper_type::args_type>>{});
  13310. }
  13311. };
  13312. ENTT_ASSERT(!exists(id, type->func));
  13313. ENTT_ASSERT(!exists(&node, type->func));
  13314. node.id = id;
  13315. node.next = type->func;
  13316. type->func = &node;
  13317. return meta_factory<Type, std::integral_constant<decltype(Candidate), Candidate>>{&node.prop};
  13318. }
  13319. };
  13320. /**
  13321. * @brief Utility function to use for reflection.
  13322. *
  13323. * This is the point from which everything starts.<br/>
  13324. * By invoking this function with a type that is not yet reflected, a meta type
  13325. * is created to which it will be possible to attach meta objects through a
  13326. * dedicated factory.
  13327. *
  13328. * @tparam Type Type to reflect.
  13329. * @return A meta factory for the given type.
  13330. */
  13331. template<typename Type>
  13332. [[nodiscard]] auto meta() ENTT_NOEXCEPT {
  13333. auto * const node = internal::meta_info<Type>::resolve();
  13334. // extended meta factory to allow assigning properties to opaque meta types
  13335. return meta_factory<Type, Type>{&node->prop};
  13336. }
  13337. }
  13338. #endif
  13339. // #include "meta/internal.hpp"
  13340. // #include "meta/meta.hpp"
  13341. // #include "meta/pointer.hpp"
  13342. #ifndef ENTT_META_POINTER_HPP
  13343. #define ENTT_META_POINTER_HPP
  13344. #include <memory>
  13345. #include <type_traits>
  13346. // #include "type_traits.hpp"
  13347. namespace entt {
  13348. /**
  13349. * @brief Makes plain pointers pointer-like types for the meta system.
  13350. * @tparam Type Element type.
  13351. */
  13352. template<typename Type>
  13353. struct is_meta_pointer_like<Type *>
  13354. : std::true_type
  13355. {};
  13356. /**
  13357. * @brief Makes `std::shared_ptr`s of any type pointer-like types for the meta
  13358. * system.
  13359. * @tparam Type Element type.
  13360. */
  13361. template<typename Type>
  13362. struct is_meta_pointer_like<std::shared_ptr<Type>>
  13363. : std::true_type
  13364. {};
  13365. /**
  13366. * @brief Makes `std::unique_ptr`s of any type pointer-like types for the meta
  13367. * system.
  13368. * @tparam Type Element type.
  13369. * @tparam Args Other arguments.
  13370. */
  13371. template<typename Type, typename... Args>
  13372. struct is_meta_pointer_like<std::unique_ptr<Type, Args...>>
  13373. : std::true_type
  13374. {};
  13375. }
  13376. #endif
  13377. // #include "meta/policy.hpp"
  13378. // #include "meta/range.hpp"
  13379. // #include "meta/resolve.hpp"
  13380. #ifndef ENTT_META_RESOLVE_HPP
  13381. #define ENTT_META_RESOLVE_HPP
  13382. #include <algorithm>
  13383. // #include "ctx.hpp"
  13384. // #include "meta.hpp"
  13385. // #include "range.hpp"
  13386. namespace entt {
  13387. /**
  13388. * @brief Returns the meta type associated with a given type.
  13389. * @tparam Type Type to use to search for a meta type.
  13390. * @return The meta type associated with the given type, if any.
  13391. */
  13392. template<typename Type>
  13393. [[nodiscard]] meta_type resolve() ENTT_NOEXCEPT {
  13394. return internal::meta_info<Type>::resolve();
  13395. }
  13396. /**
  13397. * @brief Returns a range to use to visit all meta types.
  13398. * @return An iterable range to use to visit all meta types.
  13399. */
  13400. [[nodiscard]] inline meta_range<meta_type> resolve() {
  13401. return *internal::meta_context::global();
  13402. }
  13403. /**
  13404. * @brief Returns the meta type associated with a given identifier, if any.
  13405. * @param id Unique identifier.
  13406. * @return The meta type associated with the given identifier, if any.
  13407. */
  13408. [[nodiscard]] inline meta_type resolve_id(const id_type id) ENTT_NOEXCEPT {
  13409. internal::meta_range range{*internal::meta_context::global()};
  13410. return std::find_if(range.begin(), range.end(), [id](const auto &curr) { return curr.id == id; }).operator->();
  13411. }
  13412. /**
  13413. * @brief Returns the meta type associated with a given type id, if any.
  13414. * @param id Unique identifier.
  13415. * @return The meta type associated with the given type id, if any.
  13416. */
  13417. [[nodiscard]] inline meta_type resolve_type(const id_type id) ENTT_NOEXCEPT {
  13418. internal::meta_range range{*internal::meta_context::global()};
  13419. return std::find_if(range.begin(), range.end(), [id](const auto &curr) { return curr.type_id == id; }).operator->();
  13420. }
  13421. }
  13422. #endif
  13423. // #include "meta/type_traits.hpp"
  13424. // #include "platform/android-ndk-r17.hpp"
  13425. #ifndef ENTT_PLATFORM_ANDROID_NDK_R17_HPP
  13426. #define ENTT_PLATFORM_ANDROID_NDK_R17_HPP
  13427. /**
  13428. * @cond TURN_OFF_DOXYGEN
  13429. * Internal details not to be documented.
  13430. */
  13431. #ifdef __ANDROID__
  13432. #include <android/ndk-version.h>
  13433. #if __NDK_MAJOR__ == 17
  13434. #include <functional>
  13435. #include <type_traits>
  13436. #include <utility>
  13437. namespace std {
  13438. namespace internal {
  13439. template<typename Func, typename... Args>
  13440. constexpr auto is_invocable(int) -> decltype(std::invoke(std::declval<Func>(), std::declval<Args>()...), std::true_type{});
  13441. template<typename, typename...>
  13442. constexpr std::false_type is_invocable(...);
  13443. template<typename Ret, typename Func, typename... Args>
  13444. constexpr auto is_invocable_r(int)
  13445. -> std::enable_if_t<decltype(std::is_convertible_v<decltype(std::invoke(std::declval<Func>(), std::declval<Args>()...)), Ret>, std::true_type>;
  13446. template<typename, typename, typename...>
  13447. constexpr std::false_type is_invocable_r(...);
  13448. }
  13449. template<typename Func, typename... Args>
  13450. struct is_invocable: decltype(internal::is_invocable<Func, Args...>(0)) {};
  13451. template<typename Func, typename... Argsv>
  13452. inline constexpr bool is_invocable_v = std::is_invocable<Func, Args...>::value;
  13453. template<typename Ret, typename Func, typename... Args>
  13454. struct is_invocable_r: decltype(internal::is_invocable_r<Ret, Func, Args...>(0)) {};
  13455. template<typename Ret, typename Func, typename... Args>
  13456. inline constexpr bool is_invocable_r_v = std::is_invocable_r<Ret, Func, Args...>::value;
  13457. template<typename Func, typename...Args>
  13458. struct invoke_result {
  13459. using type = decltype(std::invoke(std::declval<Func>(), std::declval<Args>()...));
  13460. };
  13461. template<typename Func, typename... Args>
  13462. using invoke_result_t = typename std::invoke_result<Func, Args...>::type;
  13463. }
  13464. #endif
  13465. #endif
  13466. /**
  13467. * Internal details not to be documented.
  13468. * @endcond
  13469. */
  13470. #endif
  13471. // #include "process/process.hpp"
  13472. #ifndef ENTT_PROCESS_PROCESS_HPP
  13473. #define ENTT_PROCESS_PROCESS_HPP
  13474. #include <utility>
  13475. #include <type_traits>
  13476. // #include "../config/config.h"
  13477. #ifndef ENTT_CONFIG_CONFIG_H
  13478. #define ENTT_CONFIG_CONFIG_H
  13479. #ifndef ENTT_NOEXCEPT
  13480. # define ENTT_NOEXCEPT noexcept
  13481. #endif
  13482. #ifndef ENTT_HS_SUFFIX
  13483. # define ENTT_HS_SUFFIX _hs
  13484. #endif
  13485. #ifndef ENTT_HWS_SUFFIX
  13486. # define ENTT_HWS_SUFFIX _hws
  13487. #endif
  13488. #ifndef ENTT_USE_ATOMIC
  13489. # define ENTT_MAYBE_ATOMIC(Type) Type
  13490. #else
  13491. # include <atomic>
  13492. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  13493. #endif
  13494. #ifndef ENTT_ID_TYPE
  13495. # include <cstdint>
  13496. # define ENTT_ID_TYPE std::uint32_t
  13497. #endif
  13498. #ifndef ENTT_PAGE_SIZE
  13499. # define ENTT_PAGE_SIZE 32768
  13500. #endif
  13501. #ifndef ENTT_ASSERT
  13502. # include <cassert>
  13503. # define ENTT_ASSERT(condition) assert(condition)
  13504. #endif
  13505. #ifndef ENTT_NO_ETO
  13506. # include <type_traits>
  13507. # define ENTT_IS_EMPTY(Type) std::is_empty<Type>
  13508. #else
  13509. # include <type_traits>
  13510. # define ENTT_IS_EMPTY(Type) std::false_type
  13511. #endif
  13512. #ifndef ENTT_STANDARD_CPP
  13513. # if defined __clang__ || (defined __GNUC__ && __GNUC__ > 8)
  13514. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  13515. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  13516. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  13517. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  13518. # elif defined __GNUC__
  13519. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  13520. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  13521. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  13522. # elif defined _MSC_VER
  13523. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  13524. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  13525. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  13526. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  13527. # endif
  13528. #endif
  13529. #ifndef ENTT_STANDALONE
  13530. # define ENTT_FAST_PATH(...) false
  13531. #else
  13532. # define ENTT_FAST_PATH(Cond) Cond
  13533. #endif
  13534. #endif
  13535. // #include "../core/type_traits.hpp"
  13536. #ifndef ENTT_CORE_TYPE_TRAITS_HPP
  13537. #define ENTT_CORE_TYPE_TRAITS_HPP
  13538. #include <cstddef>
  13539. #include <utility>
  13540. #include <type_traits>
  13541. // #include "../config/config.h"
  13542. #ifndef ENTT_CONFIG_CONFIG_H
  13543. #define ENTT_CONFIG_CONFIG_H
  13544. #ifndef ENTT_NOEXCEPT
  13545. # define ENTT_NOEXCEPT noexcept
  13546. #endif
  13547. #ifndef ENTT_HS_SUFFIX
  13548. # define ENTT_HS_SUFFIX _hs
  13549. #endif
  13550. #ifndef ENTT_HWS_SUFFIX
  13551. # define ENTT_HWS_SUFFIX _hws
  13552. #endif
  13553. #ifndef ENTT_USE_ATOMIC
  13554. # define ENTT_MAYBE_ATOMIC(Type) Type
  13555. #else
  13556. # include <atomic>
  13557. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  13558. #endif
  13559. #ifndef ENTT_ID_TYPE
  13560. # include <cstdint>
  13561. # define ENTT_ID_TYPE std::uint32_t
  13562. #endif
  13563. #ifndef ENTT_PAGE_SIZE
  13564. # define ENTT_PAGE_SIZE 32768
  13565. #endif
  13566. #ifndef ENTT_ASSERT
  13567. # include <cassert>
  13568. # define ENTT_ASSERT(condition) assert(condition)
  13569. #endif
  13570. #ifndef ENTT_NO_ETO
  13571. # include <type_traits>
  13572. # define ENTT_IS_EMPTY(Type) std::is_empty<Type>
  13573. #else
  13574. # include <type_traits>
  13575. # define ENTT_IS_EMPTY(Type) std::false_type
  13576. #endif
  13577. #ifndef ENTT_STANDARD_CPP
  13578. # if defined __clang__ || (defined __GNUC__ && __GNUC__ > 8)
  13579. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  13580. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  13581. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  13582. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  13583. # elif defined __GNUC__
  13584. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  13585. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  13586. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  13587. # elif defined _MSC_VER
  13588. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  13589. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  13590. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  13591. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  13592. # endif
  13593. #endif
  13594. #ifndef ENTT_STANDALONE
  13595. # define ENTT_FAST_PATH(...) false
  13596. #else
  13597. # define ENTT_FAST_PATH(Cond) Cond
  13598. #endif
  13599. #endif
  13600. // #include "fwd.hpp"
  13601. #ifndef ENTT_CORE_FWD_HPP
  13602. #define ENTT_CORE_FWD_HPP
  13603. // #include "../config/config.h"
  13604. namespace entt {
  13605. /*! @brief Alias declaration for type identifiers. */
  13606. using id_type = ENTT_ID_TYPE;
  13607. }
  13608. #endif
  13609. namespace entt {
  13610. /**
  13611. * @brief Using declaration to be used to _repeat_ the same type a number of
  13612. * times equal to the size of a given parameter pack.
  13613. * @tparam Type A type to repeat.
  13614. */
  13615. template<typename Type, typename>
  13616. using unpack_as_t = Type;
  13617. /**
  13618. * @brief Helper variable template to be used to _repeat_ the same value a
  13619. * number of times equal to the size of a given parameter pack.
  13620. * @tparam Value A value to repeat.
  13621. */
  13622. template<auto Value, typename>
  13623. inline constexpr auto unpack_as_v = Value;
  13624. /**
  13625. * @brief Wraps a static constant.
  13626. * @tparam Value A static constant.
  13627. */
  13628. template<auto Value>
  13629. using integral_constant = std::integral_constant<decltype(Value), Value>;
  13630. /**
  13631. * @brief Alias template to ease the creation of named values.
  13632. * @tparam Value A constant value at least convertible to `id_type`.
  13633. */
  13634. template<id_type Value>
  13635. using tag = integral_constant<Value>;
  13636. /**
  13637. * @brief Utility class to disambiguate overloaded functions.
  13638. * @tparam N Number of choices available.
  13639. */
  13640. template<std::size_t N>
  13641. struct choice_t
  13642. // Unfortunately, doxygen cannot parse such a construct.
  13643. /*! @cond TURN_OFF_DOXYGEN */
  13644. : choice_t<N-1>
  13645. /*! @endcond */
  13646. {};
  13647. /*! @copybrief choice_t */
  13648. template<>
  13649. struct choice_t<0> {};
  13650. /**
  13651. * @brief Variable template for the choice trick.
  13652. * @tparam N Number of choices available.
  13653. */
  13654. template<std::size_t N>
  13655. inline constexpr choice_t<N> choice{};
  13656. /*! @brief A class to use to push around lists of types, nothing more. */
  13657. template<typename...>
  13658. struct type_list {};
  13659. /*! @brief Primary template isn't defined on purpose. */
  13660. template<typename>
  13661. struct type_list_size;
  13662. /**
  13663. * @brief Compile-time number of elements in a type list.
  13664. * @tparam Type Types provided by the type list.
  13665. */
  13666. template<typename... Type>
  13667. struct type_list_size<type_list<Type...>>
  13668. : std::integral_constant<std::size_t, sizeof...(Type)>
  13669. {};
  13670. /**
  13671. * @brief Helper variable template.
  13672. * @tparam List Type list.
  13673. */
  13674. template<class List>
  13675. inline constexpr auto type_list_size_v = type_list_size<List>::value;
  13676. /*! @brief Primary template isn't defined on purpose. */
  13677. template<typename...>
  13678. struct type_list_cat;
  13679. /*! @brief Concatenates multiple type lists. */
  13680. template<>
  13681. struct type_list_cat<> {
  13682. /*! @brief A type list composed by the types of all the type lists. */
  13683. using type = type_list<>;
  13684. };
  13685. /**
  13686. * @brief Concatenates multiple type lists.
  13687. * @tparam Type Types provided by the first type list.
  13688. * @tparam Other Types provided by the second type list.
  13689. * @tparam List Other type lists, if any.
  13690. */
  13691. template<typename... Type, typename... Other, typename... List>
  13692. struct type_list_cat<type_list<Type...>, type_list<Other...>, List...> {
  13693. /*! @brief A type list composed by the types of all the type lists. */
  13694. using type = typename type_list_cat<type_list<Type..., Other...>, List...>::type;
  13695. };
  13696. /**
  13697. * @brief Concatenates multiple type lists.
  13698. * @tparam Type Types provided by the type list.
  13699. */
  13700. template<typename... Type>
  13701. struct type_list_cat<type_list<Type...>> {
  13702. /*! @brief A type list composed by the types of all the type lists. */
  13703. using type = type_list<Type...>;
  13704. };
  13705. /**
  13706. * @brief Helper type.
  13707. * @tparam List Type lists to concatenate.
  13708. */
  13709. template<typename... List>
  13710. using type_list_cat_t = typename type_list_cat<List...>::type;
  13711. /*! @brief Primary template isn't defined on purpose. */
  13712. template<typename>
  13713. struct type_list_unique;
  13714. /**
  13715. * @brief Removes duplicates types from a type list.
  13716. * @tparam Type One of the types provided by the given type list.
  13717. * @tparam Other The other types provided by the given type list.
  13718. */
  13719. template<typename Type, typename... Other>
  13720. struct type_list_unique<type_list<Type, Other...>> {
  13721. /*! @brief A type list without duplicate types. */
  13722. using type = std::conditional_t<
  13723. std::disjunction_v<std::is_same<Type, Other>...>,
  13724. typename type_list_unique<type_list<Other...>>::type,
  13725. type_list_cat_t<type_list<Type>, typename type_list_unique<type_list<Other...>>::type>
  13726. >;
  13727. };
  13728. /*! @brief Removes duplicates types from a type list. */
  13729. template<>
  13730. struct type_list_unique<type_list<>> {
  13731. /*! @brief A type list without duplicate types. */
  13732. using type = type_list<>;
  13733. };
  13734. /**
  13735. * @brief Helper type.
  13736. * @tparam Type A type list.
  13737. */
  13738. template<typename Type>
  13739. using type_list_unique_t = typename type_list_unique<Type>::type;
  13740. /**
  13741. * @brief Provides the member constant `value` to true if a given type is
  13742. * equality comparable, false otherwise.
  13743. * @tparam Type Potentially equality comparable type.
  13744. */
  13745. template<typename Type, typename = std::void_t<>>
  13746. struct is_equality_comparable: std::false_type {};
  13747. /*! @copydoc is_equality_comparable */
  13748. template<typename Type>
  13749. struct is_equality_comparable<Type, std::void_t<decltype(std::declval<Type>() == std::declval<Type>())>>
  13750. : std::true_type
  13751. {};
  13752. /**
  13753. * @brief Helper variable template.
  13754. * @tparam Type Potentially equality comparable type.
  13755. */
  13756. template<class Type>
  13757. inline constexpr auto is_equality_comparable_v = is_equality_comparable<Type>::value;
  13758. /**
  13759. * @brief Provides the member constant `value` to true if a given type is empty
  13760. * and the empty type optimization is enabled, false otherwise.
  13761. * @tparam Type Potential empty type.
  13762. */
  13763. template<typename Type, typename = void>
  13764. struct is_eto_eligible
  13765. : ENTT_IS_EMPTY(Type)
  13766. {};
  13767. /**
  13768. * @brief Helper variable template.
  13769. * @tparam Type Potential empty type.
  13770. */
  13771. template<typename Type>
  13772. inline constexpr auto is_eto_eligible_v = is_eto_eligible<Type>::value;
  13773. /**
  13774. * @brief Extracts the class of a non-static member object or function.
  13775. * @tparam Member A pointer to a non-static member object or function.
  13776. */
  13777. template<typename Member>
  13778. class member_class {
  13779. static_assert(std::is_member_pointer_v<Member>, "Invalid pointer type to non-static member object or function");
  13780. template<typename Class, typename Ret, typename... Args>
  13781. static Class * clazz(Ret(Class:: *)(Args...));
  13782. template<typename Class, typename Ret, typename... Args>
  13783. static Class * clazz(Ret(Class:: *)(Args...) const);
  13784. template<typename Class, typename Type>
  13785. static Class * clazz(Type Class:: *);
  13786. public:
  13787. /*! @brief The class of the given non-static member object or function. */
  13788. using type = std::remove_pointer_t<decltype(clazz(std::declval<Member>()))>;
  13789. };
  13790. /**
  13791. * @brief Helper type.
  13792. * @tparam Member A pointer to a non-static member object or function.
  13793. */
  13794. template<typename Member>
  13795. using member_class_t = typename member_class<Member>::type;
  13796. }
  13797. #endif
  13798. namespace entt {
  13799. /**
  13800. * @brief Base class for processes.
  13801. *
  13802. * This class stays true to the CRTP idiom. Derived classes must specify what's
  13803. * the intended type for elapsed times.<br/>
  13804. * A process should expose publicly the following member functions whether
  13805. * required:
  13806. *
  13807. * * @code{.cpp}
  13808. * void update(Delta, void *);
  13809. * @endcode
  13810. *
  13811. * It's invoked once per tick until a process is explicitly aborted or it
  13812. * terminates either with or without errors. Even though it's not mandatory to
  13813. * declare this member function, as a rule of thumb each process should at
  13814. * least define it to work properly. The `void *` parameter is an opaque
  13815. * pointer to user data (if any) forwarded directly to the process during an
  13816. * update.
  13817. *
  13818. * * @code{.cpp}
  13819. * void init();
  13820. * @endcode
  13821. *
  13822. * It's invoked when the process joins the running queue of a scheduler. This
  13823. * happens as soon as it's attached to the scheduler if the process is a top
  13824. * level one, otherwise when it replaces its parent if the process is a
  13825. * continuation.
  13826. *
  13827. * * @code{.cpp}
  13828. * void succeeded();
  13829. * @endcode
  13830. *
  13831. * It's invoked in case of success, immediately after an update and during the
  13832. * same tick.
  13833. *
  13834. * * @code{.cpp}
  13835. * void failed();
  13836. * @endcode
  13837. *
  13838. * It's invoked in case of errors, immediately after an update and during the
  13839. * same tick.
  13840. *
  13841. * * @code{.cpp}
  13842. * void aborted();
  13843. * @endcode
  13844. *
  13845. * It's invoked only if a process is explicitly aborted. There is no guarantee
  13846. * that it executes in the same tick, this depends solely on whether the
  13847. * process is aborted immediately or not.
  13848. *
  13849. * Derived classes can change the internal state of a process by invoking the
  13850. * `succeed` and `fail` protected member functions and even pause or unpause the
  13851. * process itself.
  13852. *
  13853. * @sa scheduler
  13854. *
  13855. * @tparam Derived Actual type of process that extends the class template.
  13856. * @tparam Delta Type to use to provide elapsed time.
  13857. */
  13858. template<typename Derived, typename Delta>
  13859. class process {
  13860. enum class state: unsigned int {
  13861. UNINITIALIZED = 0,
  13862. RUNNING,
  13863. PAUSED,
  13864. SUCCEEDED,
  13865. FAILED,
  13866. ABORTED,
  13867. FINISHED
  13868. };
  13869. template<typename Target = Derived>
  13870. auto next(integral_constant<state::UNINITIALIZED>)
  13871. -> decltype(std::declval<Target>().init(), void()) {
  13872. static_cast<Target *>(this)->init();
  13873. }
  13874. template<typename Target = Derived>
  13875. auto next(integral_constant<state::RUNNING>, Delta delta, void *data)
  13876. -> decltype(std::declval<Target>().update(delta, data), void()) {
  13877. static_cast<Target *>(this)->update(delta, data);
  13878. }
  13879. template<typename Target = Derived>
  13880. auto next(integral_constant<state::SUCCEEDED>)
  13881. -> decltype(std::declval<Target>().succeeded(), void()) {
  13882. static_cast<Target *>(this)->succeeded();
  13883. }
  13884. template<typename Target = Derived>
  13885. auto next(integral_constant<state::FAILED>)
  13886. -> decltype(std::declval<Target>().failed(), void()) {
  13887. static_cast<Target *>(this)->failed();
  13888. }
  13889. template<typename Target = Derived>
  13890. auto next(integral_constant<state::ABORTED>)
  13891. -> decltype(std::declval<Target>().aborted(), void()) {
  13892. static_cast<Target *>(this)->aborted();
  13893. }
  13894. void next(...) const ENTT_NOEXCEPT {}
  13895. protected:
  13896. /**
  13897. * @brief Terminates a process with success if it's still alive.
  13898. *
  13899. * The function is idempotent and it does nothing if the process isn't
  13900. * alive.
  13901. */
  13902. void succeed() ENTT_NOEXCEPT {
  13903. if(alive()) {
  13904. current = state::SUCCEEDED;
  13905. }
  13906. }
  13907. /**
  13908. * @brief Terminates a process with errors if it's still alive.
  13909. *
  13910. * The function is idempotent and it does nothing if the process isn't
  13911. * alive.
  13912. */
  13913. void fail() ENTT_NOEXCEPT {
  13914. if(alive()) {
  13915. current = state::FAILED;
  13916. }
  13917. }
  13918. /**
  13919. * @brief Stops a process if it's in a running state.
  13920. *
  13921. * The function is idempotent and it does nothing if the process isn't
  13922. * running.
  13923. */
  13924. void pause() ENTT_NOEXCEPT {
  13925. if(current == state::RUNNING) {
  13926. current = state::PAUSED;
  13927. }
  13928. }
  13929. /**
  13930. * @brief Restarts a process if it's paused.
  13931. *
  13932. * The function is idempotent and it does nothing if the process isn't
  13933. * paused.
  13934. */
  13935. void unpause() ENTT_NOEXCEPT {
  13936. if(current == state::PAUSED) {
  13937. current = state::RUNNING;
  13938. }
  13939. }
  13940. public:
  13941. /*! @brief Type used to provide elapsed time. */
  13942. using delta_type = Delta;
  13943. /*! @brief Default destructor. */
  13944. virtual ~process() {
  13945. static_assert(std::is_base_of_v<process, Derived>, "Incorrect use of the class template");
  13946. }
  13947. /**
  13948. * @brief Aborts a process if it's still alive.
  13949. *
  13950. * The function is idempotent and it does nothing if the process isn't
  13951. * alive.
  13952. *
  13953. * @param immediately Requests an immediate operation.
  13954. */
  13955. void abort(const bool immediately = false) {
  13956. if(alive()) {
  13957. current = state::ABORTED;
  13958. if(immediately) {
  13959. tick({});
  13960. }
  13961. }
  13962. }
  13963. /**
  13964. * @brief Returns true if a process is either running or paused.
  13965. * @return True if the process is still alive, false otherwise.
  13966. */
  13967. [[nodiscard]] bool alive() const ENTT_NOEXCEPT {
  13968. return current == state::RUNNING || current == state::PAUSED;
  13969. }
  13970. /**
  13971. * @brief Returns true if a process is already terminated.
  13972. * @return True if the process is terminated, false otherwise.
  13973. */
  13974. [[nodiscard]] bool dead() const ENTT_NOEXCEPT {
  13975. return current == state::FINISHED;
  13976. }
  13977. /**
  13978. * @brief Returns true if a process is currently paused.
  13979. * @return True if the process is paused, false otherwise.
  13980. */
  13981. [[nodiscard]] bool paused() const ENTT_NOEXCEPT {
  13982. return current == state::PAUSED;
  13983. }
  13984. /**
  13985. * @brief Returns true if a process terminated with errors.
  13986. * @return True if the process terminated with errors, false otherwise.
  13987. */
  13988. [[nodiscard]] bool rejected() const ENTT_NOEXCEPT {
  13989. return stopped;
  13990. }
  13991. /**
  13992. * @brief Updates a process and its internal state if required.
  13993. * @param delta Elapsed time.
  13994. * @param data Optional data.
  13995. */
  13996. void tick(const Delta delta, void *data = nullptr) {
  13997. switch (current) {
  13998. case state::UNINITIALIZED:
  13999. next(integral_constant<state::UNINITIALIZED>{});
  14000. current = state::RUNNING;
  14001. break;
  14002. case state::RUNNING:
  14003. next(integral_constant<state::RUNNING>{}, delta, data);
  14004. break;
  14005. default:
  14006. // suppress warnings
  14007. break;
  14008. }
  14009. // if it's dead, it must be notified and removed immediately
  14010. switch(current) {
  14011. case state::SUCCEEDED:
  14012. next(integral_constant<state::SUCCEEDED>{});
  14013. current = state::FINISHED;
  14014. break;
  14015. case state::FAILED:
  14016. next(integral_constant<state::FAILED>{});
  14017. current = state::FINISHED;
  14018. stopped = true;
  14019. break;
  14020. case state::ABORTED:
  14021. next(integral_constant<state::ABORTED>{});
  14022. current = state::FINISHED;
  14023. stopped = true;
  14024. break;
  14025. default:
  14026. // suppress warnings
  14027. break;
  14028. }
  14029. }
  14030. private:
  14031. state current{state::UNINITIALIZED};
  14032. bool stopped{false};
  14033. };
  14034. /**
  14035. * @brief Adaptor for lambdas and functors to turn them into processes.
  14036. *
  14037. * Lambdas and functors can't be used directly with a scheduler for they are not
  14038. * properly defined processes with managed life cycles.<br/>
  14039. * This class helps in filling the gap and turning lambdas and functors into
  14040. * full featured processes usable by a scheduler.
  14041. *
  14042. * The signature of the function call operator should be equivalent to the
  14043. * following:
  14044. *
  14045. * @code{.cpp}
  14046. * void(Delta delta, void *data, auto succeed, auto fail);
  14047. * @endcode
  14048. *
  14049. * Where:
  14050. *
  14051. * * `delta` is the elapsed time.
  14052. * * `data` is an opaque pointer to user data if any, `nullptr` otherwise.
  14053. * * `succeed` is a function to call when a process terminates with success.
  14054. * * `fail` is a function to call when a process terminates with errors.
  14055. *
  14056. * The signature of the function call operator of both `succeed` and `fail`
  14057. * is equivalent to the following:
  14058. *
  14059. * @code{.cpp}
  14060. * void();
  14061. * @endcode
  14062. *
  14063. * Usually users shouldn't worry about creating adaptors. A scheduler will
  14064. * create them internally each and avery time a lambda or a functor is used as
  14065. * a process.
  14066. *
  14067. * @sa process
  14068. * @sa scheduler
  14069. *
  14070. * @tparam Func Actual type of process.
  14071. * @tparam Delta Type to use to provide elapsed time.
  14072. */
  14073. template<typename Func, typename Delta>
  14074. struct process_adaptor: process<process_adaptor<Func, Delta>, Delta>, private Func {
  14075. /**
  14076. * @brief Constructs a process adaptor from a lambda or a functor.
  14077. * @tparam Args Types of arguments to use to initialize the actual process.
  14078. * @param args Parameters to use to initialize the actual process.
  14079. */
  14080. template<typename... Args>
  14081. process_adaptor(Args &&... args)
  14082. : Func{std::forward<Args>(args)...}
  14083. {}
  14084. /**
  14085. * @brief Updates a process and its internal state if required.
  14086. * @param delta Elapsed time.
  14087. * @param data Optional data.
  14088. */
  14089. void update(const Delta delta, void *data) {
  14090. Func::operator()(delta, data, [this]() { this->succeed(); }, [this]() { this->fail(); });
  14091. }
  14092. };
  14093. }
  14094. #endif
  14095. // #include "process/scheduler.hpp"
  14096. #ifndef ENTT_PROCESS_SCHEDULER_HPP
  14097. #define ENTT_PROCESS_SCHEDULER_HPP
  14098. #include <vector>
  14099. #include <memory>
  14100. #include <utility>
  14101. #include <algorithm>
  14102. #include <type_traits>
  14103. // #include "../config/config.h"
  14104. // #include "process.hpp"
  14105. namespace entt {
  14106. /**
  14107. * @brief Cooperative scheduler for processes.
  14108. *
  14109. * A cooperative scheduler runs processes and helps managing their life cycles.
  14110. *
  14111. * Each process is invoked once per tick. If a process terminates, it's
  14112. * removed automatically from the scheduler and it's never invoked again.<br/>
  14113. * A process can also have a child. In this case, the process is replaced with
  14114. * its child when it terminates if it returns with success. In case of errors,
  14115. * both the process and its child are discarded.
  14116. *
  14117. * Example of use (pseudocode):
  14118. *
  14119. * @code{.cpp}
  14120. * scheduler.attach([](auto delta, void *, auto succeed, auto fail) {
  14121. * // code
  14122. * }).then<my_process>(arguments...);
  14123. * @endcode
  14124. *
  14125. * In order to invoke all scheduled processes, call the `update` member function
  14126. * passing it the elapsed time to forward to the tasks.
  14127. *
  14128. * @sa process
  14129. *
  14130. * @tparam Delta Type to use to provide elapsed time.
  14131. */
  14132. template<typename Delta>
  14133. class scheduler {
  14134. struct process_handler {
  14135. using instance_type = std::unique_ptr<void, void(*)(void *)>;
  14136. using update_fn_type = bool(process_handler &, Delta, void *);
  14137. using abort_fn_type = void(process_handler &, bool);
  14138. using next_type = std::unique_ptr<process_handler>;
  14139. instance_type instance;
  14140. update_fn_type *update;
  14141. abort_fn_type *abort;
  14142. next_type next;
  14143. };
  14144. struct continuation {
  14145. continuation(process_handler *ref)
  14146. : handler{ref}
  14147. {
  14148. ENTT_ASSERT(handler);
  14149. }
  14150. template<typename Proc, typename... Args>
  14151. continuation then(Args &&... args) {
  14152. static_assert(std::is_base_of_v<process<Proc, Delta>, Proc>, "Invalid process type");
  14153. auto proc = typename process_handler::instance_type{new Proc{std::forward<Args>(args)...}, &scheduler::deleter<Proc>};
  14154. handler->next.reset(new process_handler{std::move(proc), &scheduler::update<Proc>, &scheduler::abort<Proc>, nullptr});
  14155. handler = handler->next.get();
  14156. return *this;
  14157. }
  14158. template<typename Func>
  14159. continuation then(Func &&func) {
  14160. return then<process_adaptor<std::decay_t<Func>, Delta>>(std::forward<Func>(func));
  14161. }
  14162. private:
  14163. process_handler *handler;
  14164. };
  14165. template<typename Proc>
  14166. [[nodiscard]] static bool update(process_handler &handler, const Delta delta, void *data) {
  14167. auto *process = static_cast<Proc *>(handler.instance.get());
  14168. process->tick(delta, data);
  14169. auto dead = process->dead();
  14170. if(dead) {
  14171. if(handler.next && !process->rejected()) {
  14172. handler = std::move(*handler.next);
  14173. // forces the process to exit the uninitialized state
  14174. dead = handler.update(handler, {}, nullptr);
  14175. } else {
  14176. handler.instance.reset();
  14177. }
  14178. }
  14179. return dead;
  14180. }
  14181. template<typename Proc>
  14182. static void abort(process_handler &handler, const bool immediately) {
  14183. static_cast<Proc *>(handler.instance.get())->abort(immediately);
  14184. }
  14185. template<typename Proc>
  14186. static void deleter(void *proc) {
  14187. delete static_cast<Proc *>(proc);
  14188. }
  14189. public:
  14190. /*! @brief Unsigned integer type. */
  14191. using size_type = std::size_t;
  14192. /*! @brief Default constructor. */
  14193. scheduler() = default;
  14194. /*! @brief Default move constructor. */
  14195. scheduler(scheduler &&) = default;
  14196. /*! @brief Default move assignment operator. @return This scheduler. */
  14197. scheduler & operator=(scheduler &&) = default;
  14198. /**
  14199. * @brief Number of processes currently scheduled.
  14200. * @return Number of processes currently scheduled.
  14201. */
  14202. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  14203. return handlers.size();
  14204. }
  14205. /**
  14206. * @brief Returns true if at least a process is currently scheduled.
  14207. * @return True if there are scheduled processes, false otherwise.
  14208. */
  14209. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  14210. return handlers.empty();
  14211. }
  14212. /**
  14213. * @brief Discards all scheduled processes.
  14214. *
  14215. * Processes aren't aborted. They are discarded along with their children
  14216. * and never executed again.
  14217. */
  14218. void clear() {
  14219. handlers.clear();
  14220. }
  14221. /**
  14222. * @brief Schedules a process for the next tick.
  14223. *
  14224. * Returned value is an opaque object that can be used to attach a child to
  14225. * the given process. The child is automatically scheduled when the process
  14226. * terminates and only if the process returns with success.
  14227. *
  14228. * Example of use (pseudocode):
  14229. *
  14230. * @code{.cpp}
  14231. * // schedules a task in the form of a process class
  14232. * scheduler.attach<my_process>(arguments...)
  14233. * // appends a child in the form of a lambda function
  14234. * .then([](auto delta, void *, auto succeed, auto fail) {
  14235. * // code
  14236. * })
  14237. * // appends a child in the form of another process class
  14238. * .then<my_other_process>();
  14239. * @endcode
  14240. *
  14241. * @tparam Proc Type of process to schedule.
  14242. * @tparam Args Types of arguments to use to initialize the process.
  14243. * @param args Parameters to use to initialize the process.
  14244. * @return An opaque object to use to concatenate processes.
  14245. */
  14246. template<typename Proc, typename... Args>
  14247. auto attach(Args &&... args) {
  14248. static_assert(std::is_base_of_v<process<Proc, Delta>, Proc>, "Invalid process type");
  14249. auto proc = typename process_handler::instance_type{new Proc{std::forward<Args>(args)...}, &scheduler::deleter<Proc>};
  14250. process_handler handler{std::move(proc), &scheduler::update<Proc>, &scheduler::abort<Proc>, nullptr};
  14251. // forces the process to exit the uninitialized state
  14252. handler.update(handler, {}, nullptr);
  14253. return continuation{&handlers.emplace_back(std::move(handler))};
  14254. }
  14255. /**
  14256. * @brief Schedules a process for the next tick.
  14257. *
  14258. * A process can be either a lambda or a functor. The scheduler wraps both
  14259. * of them in a process adaptor internally.<br/>
  14260. * The signature of the function call operator should be equivalent to the
  14261. * following:
  14262. *
  14263. * @code{.cpp}
  14264. * void(Delta delta, void *data, auto succeed, auto fail);
  14265. * @endcode
  14266. *
  14267. * Where:
  14268. *
  14269. * * `delta` is the elapsed time.
  14270. * * `data` is an opaque pointer to user data if any, `nullptr` otherwise.
  14271. * * `succeed` is a function to call when a process terminates with success.
  14272. * * `fail` is a function to call when a process terminates with errors.
  14273. *
  14274. * The signature of the function call operator of both `succeed` and `fail`
  14275. * is equivalent to the following:
  14276. *
  14277. * @code{.cpp}
  14278. * void();
  14279. * @endcode
  14280. *
  14281. * Returned value is an opaque object that can be used to attach a child to
  14282. * the given process. The child is automatically scheduled when the process
  14283. * terminates and only if the process returns with success.
  14284. *
  14285. * Example of use (pseudocode):
  14286. *
  14287. * @code{.cpp}
  14288. * // schedules a task in the form of a lambda function
  14289. * scheduler.attach([](auto delta, void *, auto succeed, auto fail) {
  14290. * // code
  14291. * })
  14292. * // appends a child in the form of another lambda function
  14293. * .then([](auto delta, void *, auto succeed, auto fail) {
  14294. * // code
  14295. * })
  14296. * // appends a child in the form of a process class
  14297. * .then<my_process>(arguments...);
  14298. * @endcode
  14299. *
  14300. * @sa process_adaptor
  14301. *
  14302. * @tparam Func Type of process to schedule.
  14303. * @param func Either a lambda or a functor to use as a process.
  14304. * @return An opaque object to use to concatenate processes.
  14305. */
  14306. template<typename Func>
  14307. auto attach(Func &&func) {
  14308. using Proc = process_adaptor<std::decay_t<Func>, Delta>;
  14309. return attach<Proc>(std::forward<Func>(func));
  14310. }
  14311. /**
  14312. * @brief Updates all scheduled processes.
  14313. *
  14314. * All scheduled processes are executed in no specific order.<br/>
  14315. * If a process terminates with success, it's replaced with its child, if
  14316. * any. Otherwise, if a process terminates with an error, it's removed along
  14317. * with its child.
  14318. *
  14319. * @param delta Elapsed time.
  14320. * @param data Optional data.
  14321. */
  14322. void update(const Delta delta, void *data = nullptr) {
  14323. bool clean = false;
  14324. for(auto pos = handlers.size(); pos; --pos) {
  14325. auto &handler = handlers[pos-1];
  14326. const bool dead = handler.update(handler, delta, data);
  14327. clean = clean || dead;
  14328. }
  14329. if(clean) {
  14330. handlers.erase(std::remove_if(handlers.begin(), handlers.end(), [](auto &handler) {
  14331. return !handler.instance;
  14332. }), handlers.end());
  14333. }
  14334. }
  14335. /**
  14336. * @brief Aborts all scheduled processes.
  14337. *
  14338. * Unless an immediate operation is requested, the abort is scheduled for
  14339. * the next tick. Processes won't be executed anymore in any case.<br/>
  14340. * Once a process is fully aborted and thus finished, it's discarded along
  14341. * with its child, if any.
  14342. *
  14343. * @param immediately Requests an immediate operation.
  14344. */
  14345. void abort(const bool immediately = false) {
  14346. decltype(handlers) exec;
  14347. exec.swap(handlers);
  14348. for(auto &&handler: exec) {
  14349. handler.abort(handler, immediately);
  14350. }
  14351. std::move(handlers.begin(), handlers.end(), std::back_inserter(exec));
  14352. handlers.swap(exec);
  14353. }
  14354. private:
  14355. std::vector<process_handler> handlers{};
  14356. };
  14357. }
  14358. #endif
  14359. // #include "resource/cache.hpp"
  14360. #ifndef ENTT_RESOURCE_CACHE_HPP
  14361. #define ENTT_RESOURCE_CACHE_HPP
  14362. #include <memory>
  14363. #include <type_traits>
  14364. #include <unordered_map>
  14365. #include <utility>
  14366. // #include "../config/config.h"
  14367. #ifndef ENTT_CONFIG_CONFIG_H
  14368. #define ENTT_CONFIG_CONFIG_H
  14369. #ifndef ENTT_NOEXCEPT
  14370. # define ENTT_NOEXCEPT noexcept
  14371. #endif
  14372. #ifndef ENTT_HS_SUFFIX
  14373. # define ENTT_HS_SUFFIX _hs
  14374. #endif
  14375. #ifndef ENTT_HWS_SUFFIX
  14376. # define ENTT_HWS_SUFFIX _hws
  14377. #endif
  14378. #ifndef ENTT_USE_ATOMIC
  14379. # define ENTT_MAYBE_ATOMIC(Type) Type
  14380. #else
  14381. # include <atomic>
  14382. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  14383. #endif
  14384. #ifndef ENTT_ID_TYPE
  14385. # include <cstdint>
  14386. # define ENTT_ID_TYPE std::uint32_t
  14387. #endif
  14388. #ifndef ENTT_PAGE_SIZE
  14389. # define ENTT_PAGE_SIZE 32768
  14390. #endif
  14391. #ifndef ENTT_ASSERT
  14392. # include <cassert>
  14393. # define ENTT_ASSERT(condition) assert(condition)
  14394. #endif
  14395. #ifndef ENTT_NO_ETO
  14396. # include <type_traits>
  14397. # define ENTT_IS_EMPTY(Type) std::is_empty<Type>
  14398. #else
  14399. # include <type_traits>
  14400. # define ENTT_IS_EMPTY(Type) std::false_type
  14401. #endif
  14402. #ifndef ENTT_STANDARD_CPP
  14403. # if defined __clang__ || (defined __GNUC__ && __GNUC__ > 8)
  14404. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  14405. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  14406. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  14407. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  14408. # elif defined __GNUC__
  14409. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  14410. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  14411. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  14412. # elif defined _MSC_VER
  14413. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  14414. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  14415. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  14416. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  14417. # endif
  14418. #endif
  14419. #ifndef ENTT_STANDALONE
  14420. # define ENTT_FAST_PATH(...) false
  14421. #else
  14422. # define ENTT_FAST_PATH(Cond) Cond
  14423. #endif
  14424. #endif
  14425. // #include "../core/fwd.hpp"
  14426. #ifndef ENTT_CORE_FWD_HPP
  14427. #define ENTT_CORE_FWD_HPP
  14428. // #include "../config/config.h"
  14429. #ifndef ENTT_CONFIG_CONFIG_H
  14430. #define ENTT_CONFIG_CONFIG_H
  14431. #ifndef ENTT_NOEXCEPT
  14432. # define ENTT_NOEXCEPT noexcept
  14433. #endif
  14434. #ifndef ENTT_HS_SUFFIX
  14435. # define ENTT_HS_SUFFIX _hs
  14436. #endif
  14437. #ifndef ENTT_HWS_SUFFIX
  14438. # define ENTT_HWS_SUFFIX _hws
  14439. #endif
  14440. #ifndef ENTT_USE_ATOMIC
  14441. # define ENTT_MAYBE_ATOMIC(Type) Type
  14442. #else
  14443. # include <atomic>
  14444. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  14445. #endif
  14446. #ifndef ENTT_ID_TYPE
  14447. # include <cstdint>
  14448. # define ENTT_ID_TYPE std::uint32_t
  14449. #endif
  14450. #ifndef ENTT_PAGE_SIZE
  14451. # define ENTT_PAGE_SIZE 32768
  14452. #endif
  14453. #ifndef ENTT_ASSERT
  14454. # include <cassert>
  14455. # define ENTT_ASSERT(condition) assert(condition)
  14456. #endif
  14457. #ifndef ENTT_NO_ETO
  14458. # include <type_traits>
  14459. # define ENTT_IS_EMPTY(Type) std::is_empty<Type>
  14460. #else
  14461. # include <type_traits>
  14462. # define ENTT_IS_EMPTY(Type) std::false_type
  14463. #endif
  14464. #ifndef ENTT_STANDARD_CPP
  14465. # if defined __clang__ || (defined __GNUC__ && __GNUC__ > 8)
  14466. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  14467. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  14468. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  14469. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  14470. # elif defined __GNUC__
  14471. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  14472. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  14473. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  14474. # elif defined _MSC_VER
  14475. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  14476. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  14477. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  14478. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  14479. # endif
  14480. #endif
  14481. #ifndef ENTT_STANDALONE
  14482. # define ENTT_FAST_PATH(...) false
  14483. #else
  14484. # define ENTT_FAST_PATH(Cond) Cond
  14485. #endif
  14486. #endif
  14487. namespace entt {
  14488. /*! @brief Alias declaration for type identifiers. */
  14489. using id_type = ENTT_ID_TYPE;
  14490. }
  14491. #endif
  14492. // #include "handle.hpp"
  14493. #ifndef ENTT_RESOURCE_HANDLE_HPP
  14494. #define ENTT_RESOURCE_HANDLE_HPP
  14495. #include <memory>
  14496. #include <utility>
  14497. // #include "../config/config.h"
  14498. // #include "fwd.hpp"
  14499. #ifndef ENTT_RESOURCE_FWD_HPP
  14500. #define ENTT_RESOURCE_FWD_HPP
  14501. namespace entt {
  14502. template<typename>
  14503. struct resource_cache;
  14504. template<typename>
  14505. class resource_handle;
  14506. template<typename, typename>
  14507. class resource_loader;
  14508. }
  14509. #endif
  14510. namespace entt {
  14511. /**
  14512. * @brief Shared resource handle.
  14513. *
  14514. * A shared resource handle is a small class that wraps a resource and keeps it
  14515. * alive even if it's deleted from the cache. It can be either copied or
  14516. * moved. A handle shares a reference to the same resource with all the other
  14517. * handles constructed for the same identifier.<br/>
  14518. * As a rule of thumb, resources should never be copied nor moved. Handles are
  14519. * the way to go to keep references to them.
  14520. *
  14521. * @tparam Resource Type of resource managed by a handle.
  14522. */
  14523. template<typename Resource>
  14524. class resource_handle {
  14525. /*! @brief Resource handles are friends of their caches. */
  14526. friend struct resource_cache<Resource>;
  14527. resource_handle(std::shared_ptr<Resource> res) ENTT_NOEXCEPT
  14528. : resource{std::move(res)}
  14529. {}
  14530. public:
  14531. /*! @brief Default constructor. */
  14532. resource_handle() ENTT_NOEXCEPT = default;
  14533. /**
  14534. * @brief Gets a reference to the managed resource.
  14535. *
  14536. * @warning
  14537. * The behavior is undefined if the handle doesn't contain a resource.<br/>
  14538. * An assertion will abort the execution at runtime in debug mode if the
  14539. * handle is empty.
  14540. *
  14541. * @return A reference to the managed resource.
  14542. */
  14543. [[nodiscard]] const Resource & get() const ENTT_NOEXCEPT {
  14544. ENTT_ASSERT(static_cast<bool>(resource));
  14545. return *resource;
  14546. }
  14547. /*! @copydoc get */
  14548. [[nodiscard]] Resource & get() ENTT_NOEXCEPT {
  14549. return const_cast<Resource &>(std::as_const(*this).get());
  14550. }
  14551. /*! @copydoc get */
  14552. [[nodiscard]] operator const Resource & () const ENTT_NOEXCEPT {
  14553. return get();
  14554. }
  14555. /*! @copydoc get */
  14556. [[nodiscard]] operator Resource & () ENTT_NOEXCEPT {
  14557. return get();
  14558. }
  14559. /*! @copydoc get */
  14560. [[nodiscard]] const Resource & operator *() const ENTT_NOEXCEPT {
  14561. return get();
  14562. }
  14563. /*! @copydoc get */
  14564. [[nodiscard]] Resource & operator *() ENTT_NOEXCEPT {
  14565. return get();
  14566. }
  14567. /**
  14568. * @brief Gets a pointer to the managed resource.
  14569. *
  14570. * @warning
  14571. * The behavior is undefined if the handle doesn't contain a resource.<br/>
  14572. * An assertion will abort the execution at runtime in debug mode if the
  14573. * handle is empty.
  14574. *
  14575. * @return A pointer to the managed resource or `nullptr` if the handle
  14576. * contains no resource at all.
  14577. */
  14578. [[nodiscard]] const Resource * operator->() const ENTT_NOEXCEPT {
  14579. ENTT_ASSERT(static_cast<bool>(resource));
  14580. return resource.get();
  14581. }
  14582. /*! @copydoc operator-> */
  14583. [[nodiscard]] Resource * operator->() ENTT_NOEXCEPT {
  14584. return const_cast<Resource *>(std::as_const(*this).operator->());
  14585. }
  14586. /**
  14587. * @brief Returns true if a handle contains a resource, false otherwise.
  14588. * @return True if the handle contains a resource, false otherwise.
  14589. */
  14590. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  14591. return static_cast<bool>(resource);
  14592. }
  14593. private:
  14594. std::shared_ptr<Resource> resource;
  14595. };
  14596. }
  14597. #endif
  14598. // #include "loader.hpp"
  14599. #ifndef ENTT_RESOURCE_LOADER_HPP
  14600. #define ENTT_RESOURCE_LOADER_HPP
  14601. #include <memory>
  14602. // #include "fwd.hpp"
  14603. namespace entt {
  14604. /**
  14605. * @brief Base class for resource loaders.
  14606. *
  14607. * Resource loaders must inherit from this class and stay true to the CRTP
  14608. * idiom. Moreover, a resource loader must expose a public, const member
  14609. * function named `load` that accepts a variable number of arguments and returns
  14610. * a shared pointer to the resource just created.<br/>
  14611. * As an example:
  14612. *
  14613. * @code{.cpp}
  14614. * struct my_resource {};
  14615. *
  14616. * struct my_loader: entt::resource_loader<my_loader, my_resource> {
  14617. * std::shared_ptr<my_resource> load(int) const {
  14618. * // use the integer value somehow
  14619. * return std::make_shared<my_resource>();
  14620. * }
  14621. * };
  14622. * @endcode
  14623. *
  14624. * In general, resource loaders should not have a state or retain data of any
  14625. * type. They should let the cache manage their resources instead.
  14626. *
  14627. * @note
  14628. * Base class and CRTP idiom aren't strictly required with the current
  14629. * implementation. One could argue that a cache can easily work with loaders of
  14630. * any type. However, future changes won't be breaking ones by forcing the use
  14631. * of a base class today and that's why the model is already in its place.
  14632. *
  14633. * @tparam Loader Type of the derived class.
  14634. * @tparam Resource Type of resource for which to use the loader.
  14635. */
  14636. template<typename Loader, typename Resource>
  14637. class resource_loader {
  14638. /*! @brief Resource loaders are friends of their caches. */
  14639. friend struct resource_cache<Resource>;
  14640. /**
  14641. * @brief Loads the resource and returns it.
  14642. * @tparam Args Types of arguments for the loader.
  14643. * @param args Arguments for the loader.
  14644. * @return The resource just loaded or an empty pointer in case of errors.
  14645. */
  14646. template<typename... Args>
  14647. [[nodiscard]] std::shared_ptr<Resource> get(Args &&... args) const {
  14648. return static_cast<const Loader *>(this)->load(std::forward<Args>(args)...);
  14649. }
  14650. };
  14651. }
  14652. #endif
  14653. // #include "fwd.hpp"
  14654. namespace entt {
  14655. /**
  14656. * @brief Simple cache for resources of a given type.
  14657. *
  14658. * Minimal implementation of a cache for resources of a given type. It doesn't
  14659. * offer much functionalities but it's suitable for small or medium sized
  14660. * applications and can be freely inherited to add targeted functionalities for
  14661. * large sized applications.
  14662. *
  14663. * @tparam Resource Type of resources managed by a cache.
  14664. */
  14665. template<typename Resource>
  14666. struct resource_cache {
  14667. /*! @brief Unsigned integer type. */
  14668. using size_type = std::size_t;
  14669. /*! @brief Type of resources managed by a cache. */
  14670. using resource_type = Resource;
  14671. /*! @brief Default constructor. */
  14672. resource_cache() = default;
  14673. /*! @brief Default move constructor. */
  14674. resource_cache(resource_cache &&) = default;
  14675. /*! @brief Default move assignment operator. @return This cache. */
  14676. resource_cache & operator=(resource_cache &&) = default;
  14677. /**
  14678. * @brief Number of resources managed by a cache.
  14679. * @return Number of resources currently stored.
  14680. */
  14681. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  14682. return resources.size();
  14683. }
  14684. /**
  14685. * @brief Returns true if a cache contains no resources, false otherwise.
  14686. * @return True if the cache contains no resources, false otherwise.
  14687. */
  14688. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  14689. return resources.empty();
  14690. }
  14691. /**
  14692. * @brief Clears a cache and discards all its resources.
  14693. *
  14694. * Handles are not invalidated and the memory used by a resource isn't
  14695. * freed as long as at least a handle keeps the resource itself alive.
  14696. */
  14697. void clear() ENTT_NOEXCEPT {
  14698. resources.clear();
  14699. }
  14700. /**
  14701. * @brief Loads the resource that corresponds to a given identifier.
  14702. *
  14703. * In case an identifier isn't already present in the cache, it loads its
  14704. * resource and stores it aside for future uses. Arguments are forwarded
  14705. * directly to the loader in order to construct properly the requested
  14706. * resource.
  14707. *
  14708. * @note
  14709. * If the identifier is already present in the cache, this function does
  14710. * nothing and the arguments are simply discarded.
  14711. *
  14712. * @warning
  14713. * If the resource cannot be loaded correctly, the returned handle will be
  14714. * invalid and any use of it will result in undefined behavior.
  14715. *
  14716. * @tparam Loader Type of loader to use to load the resource if required.
  14717. * @tparam Args Types of arguments to use to load the resource if required.
  14718. * @param id Unique resource identifier.
  14719. * @param args Arguments to use to load the resource if required.
  14720. * @return A handle for the given resource.
  14721. */
  14722. template<typename Loader, typename... Args>
  14723. resource_handle<Resource> load(const id_type id, Args &&... args) {
  14724. static_assert(std::is_base_of_v<resource_loader<Loader, Resource>, Loader>, "Invalid loader type");
  14725. resource_handle<Resource> resource{};
  14726. if(auto it = resources.find(id); it == resources.cend()) {
  14727. if(auto instance = Loader{}.get(std::forward<Args>(args)...); instance) {
  14728. resources[id] = instance;
  14729. resource = std::move(instance);
  14730. }
  14731. } else {
  14732. resource = it->second;
  14733. }
  14734. return resource;
  14735. }
  14736. /**
  14737. * @brief Reloads a resource or loads it for the first time if not present.
  14738. *
  14739. * Equivalent to the following snippet (pseudocode):
  14740. *
  14741. * @code{.cpp}
  14742. * cache.discard(id);
  14743. * cache.load(id, args...);
  14744. * @endcode
  14745. *
  14746. * Arguments are forwarded directly to the loader in order to construct
  14747. * properly the requested resource.
  14748. *
  14749. * @warning
  14750. * If the resource cannot be loaded correctly, the returned handle will be
  14751. * invalid and any use of it will result in undefined behavior.
  14752. *
  14753. * @tparam Loader Type of loader to use to load the resource.
  14754. * @tparam Args Types of arguments to use to load the resource.
  14755. * @param id Unique resource identifier.
  14756. * @param args Arguments to use to load the resource.
  14757. * @return A handle for the given resource.
  14758. */
  14759. template<typename Loader, typename... Args>
  14760. resource_handle<Resource> reload(const id_type id, Args &&... args) {
  14761. return (discard(id), load<Loader>(id, std::forward<Args>(args)...));
  14762. }
  14763. /**
  14764. * @brief Creates a temporary handle for a resource.
  14765. *
  14766. * Arguments are forwarded directly to the loader in order to construct
  14767. * properly the requested resource. The handle isn't stored aside and the
  14768. * cache isn't in charge of the lifetime of the resource itself.
  14769. *
  14770. * @tparam Loader Type of loader to use to load the resource.
  14771. * @tparam Args Types of arguments to use to load the resource.
  14772. * @param args Arguments to use to load the resource.
  14773. * @return A handle for the given resource.
  14774. */
  14775. template<typename Loader, typename... Args>
  14776. [[nodiscard]] resource_handle<Resource> temp(Args &&... args) const {
  14777. return { Loader{}.get(std::forward<Args>(args)...) };
  14778. }
  14779. /**
  14780. * @brief Creates a handle for a given resource identifier.
  14781. *
  14782. * A resource handle can be in a either valid or invalid state. In other
  14783. * terms, a resource handle is properly initialized with a resource if the
  14784. * cache contains the resource itself. Otherwise the returned handle is
  14785. * uninitialized and accessing it results in undefined behavior.
  14786. *
  14787. * @sa resource_handle
  14788. *
  14789. * @param id Unique resource identifier.
  14790. * @return A handle for the given resource.
  14791. */
  14792. [[nodiscard]] resource_handle<Resource> handle(const id_type id) const {
  14793. auto it = resources.find(id);
  14794. return { it == resources.end() ? nullptr : it->second };
  14795. }
  14796. /**
  14797. * @brief Checks if a cache contains a given identifier.
  14798. * @param id Unique resource identifier.
  14799. * @return True if the cache contains the resource, false otherwise.
  14800. */
  14801. [[nodiscard]] bool contains(const id_type id) const {
  14802. return (resources.find(id) != resources.cend());
  14803. }
  14804. /**
  14805. * @brief Discards the resource that corresponds to a given identifier.
  14806. *
  14807. * Handles are not invalidated and the memory used by the resource isn't
  14808. * freed as long as at least a handle keeps the resource itself alive.
  14809. *
  14810. * @param id Unique resource identifier.
  14811. */
  14812. void discard(const id_type id) {
  14813. if(auto it = resources.find(id); it != resources.end()) {
  14814. resources.erase(it);
  14815. }
  14816. }
  14817. /**
  14818. * @brief Iterates all resources.
  14819. *
  14820. * The function object is invoked for each element. It is provided with
  14821. * either the resource identifier, the resource handle or both of them.<br/>
  14822. * The signature of the function must be equivalent to one of the following
  14823. * forms:
  14824. *
  14825. * @code{.cpp}
  14826. * void(const entt::id_type);
  14827. * void(entt::resource_handle<Resource>);
  14828. * void(const entt::id_type, entt::resource_handle<Resource>);
  14829. * @endcode
  14830. *
  14831. * @tparam Func Type of the function object to invoke.
  14832. * @param func A valid function object.
  14833. */
  14834. template <typename Func>
  14835. void each(Func func) const {
  14836. auto begin = resources.begin();
  14837. auto end = resources.end();
  14838. while(begin != end) {
  14839. auto curr = begin++;
  14840. if constexpr(std::is_invocable_v<Func, id_type>) {
  14841. func(curr->first);
  14842. } else if constexpr(std::is_invocable_v<Func, resource_handle<Resource>>) {
  14843. func(resource_handle{ curr->second });
  14844. } else {
  14845. func(curr->first, resource_handle{ curr->second });
  14846. }
  14847. }
  14848. }
  14849. private:
  14850. std::unordered_map<id_type, std::shared_ptr<Resource>> resources;
  14851. };
  14852. }
  14853. #endif
  14854. // #include "resource/handle.hpp"
  14855. // #include "resource/loader.hpp"
  14856. // #include "signal/delegate.hpp"
  14857. #ifndef ENTT_SIGNAL_DELEGATE_HPP
  14858. #define ENTT_SIGNAL_DELEGATE_HPP
  14859. #include <tuple>
  14860. #include <cstddef>
  14861. #include <utility>
  14862. #include <functional>
  14863. #include <type_traits>
  14864. // #include "../config/config.h"
  14865. #ifndef ENTT_CONFIG_CONFIG_H
  14866. #define ENTT_CONFIG_CONFIG_H
  14867. #ifndef ENTT_NOEXCEPT
  14868. # define ENTT_NOEXCEPT noexcept
  14869. #endif
  14870. #ifndef ENTT_HS_SUFFIX
  14871. # define ENTT_HS_SUFFIX _hs
  14872. #endif
  14873. #ifndef ENTT_HWS_SUFFIX
  14874. # define ENTT_HWS_SUFFIX _hws
  14875. #endif
  14876. #ifndef ENTT_USE_ATOMIC
  14877. # define ENTT_MAYBE_ATOMIC(Type) Type
  14878. #else
  14879. # include <atomic>
  14880. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  14881. #endif
  14882. #ifndef ENTT_ID_TYPE
  14883. # include <cstdint>
  14884. # define ENTT_ID_TYPE std::uint32_t
  14885. #endif
  14886. #ifndef ENTT_PAGE_SIZE
  14887. # define ENTT_PAGE_SIZE 32768
  14888. #endif
  14889. #ifndef ENTT_ASSERT
  14890. # include <cassert>
  14891. # define ENTT_ASSERT(condition) assert(condition)
  14892. #endif
  14893. #ifndef ENTT_NO_ETO
  14894. # include <type_traits>
  14895. # define ENTT_IS_EMPTY(Type) std::is_empty<Type>
  14896. #else
  14897. # include <type_traits>
  14898. # define ENTT_IS_EMPTY(Type) std::false_type
  14899. #endif
  14900. #ifndef ENTT_STANDARD_CPP
  14901. # if defined __clang__ || (defined __GNUC__ && __GNUC__ > 8)
  14902. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  14903. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  14904. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  14905. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  14906. # elif defined __GNUC__
  14907. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  14908. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  14909. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  14910. # elif defined _MSC_VER
  14911. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  14912. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  14913. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  14914. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  14915. # endif
  14916. #endif
  14917. #ifndef ENTT_STANDALONE
  14918. # define ENTT_FAST_PATH(...) false
  14919. #else
  14920. # define ENTT_FAST_PATH(Cond) Cond
  14921. #endif
  14922. #endif
  14923. namespace entt {
  14924. /**
  14925. * @cond TURN_OFF_DOXYGEN
  14926. * Internal details not to be documented.
  14927. */
  14928. namespace internal {
  14929. template<typename Ret, typename... Args>
  14930. auto function_pointer(Ret(*)(Args...)) -> Ret(*)(Args...);
  14931. template<typename Ret, typename Type, typename... Args, typename Other>
  14932. auto function_pointer(Ret(*)(Type, Args...), Other &&) -> Ret(*)(Args...);
  14933. template<typename Class, typename Ret, typename... Args, typename... Other>
  14934. auto function_pointer(Ret(Class:: *)(Args...), Other &&...) -> Ret(*)(Args...);
  14935. template<typename Class, typename Ret, typename... Args, typename... Other>
  14936. auto function_pointer(Ret(Class:: *)(Args...) const, Other &&...) -> Ret(*)(Args...);
  14937. template<typename Class, typename Type, typename... Other>
  14938. auto function_pointer(Type Class:: *, Other &&...) -> Type(*)();
  14939. template<typename... Type>
  14940. using function_pointer_t = decltype(internal::function_pointer(std::declval<Type>()...));
  14941. template<typename... Class, typename Ret, typename... Args>
  14942. [[nodiscard]] constexpr auto index_sequence_for(Ret(*)(Args...)) {
  14943. return std::index_sequence_for<Class..., Args...>{};
  14944. }
  14945. }
  14946. /**
  14947. * Internal details not to be documented.
  14948. * @endcond
  14949. */
  14950. /*! @brief Used to wrap a function or a member of a specified type. */
  14951. template<auto>
  14952. struct connect_arg_t {};
  14953. /*! @brief Constant of type connect_arg_t used to disambiguate calls. */
  14954. template<auto Func>
  14955. inline constexpr connect_arg_t<Func> connect_arg{};
  14956. /**
  14957. * @brief Basic delegate implementation.
  14958. *
  14959. * Primary template isn't defined on purpose. All the specializations give a
  14960. * compile-time error unless the template parameter is a function type.
  14961. */
  14962. template<typename>
  14963. class delegate;
  14964. /**
  14965. * @brief Utility class to use to send around functions and members.
  14966. *
  14967. * Unmanaged delegate for function pointers and members. Users of this class are
  14968. * in charge of disconnecting instances before deleting them.
  14969. *
  14970. * A delegate can be used as a general purpose invoker without memory overhead
  14971. * for free functions possibly with payloads and bound or unbound members.
  14972. *
  14973. * @tparam Ret Return type of a function type.
  14974. * @tparam Args Types of arguments of a function type.
  14975. */
  14976. template<typename Ret, typename... Args>
  14977. class delegate<Ret(Args...)> {
  14978. template<auto Candidate, std::size_t... Index>
  14979. [[nodiscard]] auto wrap(std::index_sequence<Index...>) ENTT_NOEXCEPT {
  14980. return [](const void *, Args... args) -> Ret {
  14981. [[maybe_unused]] const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
  14982. return Ret(std::invoke(Candidate, std::forward<std::tuple_element_t<Index, std::tuple<Args...>>>(std::get<Index>(arguments))...));
  14983. };
  14984. }
  14985. template<auto Candidate, typename Type, std::size_t... Index>
  14986. [[nodiscard]] auto wrap(Type &, std::index_sequence<Index...>) ENTT_NOEXCEPT {
  14987. return [](const void *payload, Args... args) -> Ret {
  14988. [[maybe_unused]] const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
  14989. Type *curr = static_cast<Type *>(const_cast<std::conditional_t<std::is_const_v<Type>, const void *, void *>>(payload));
  14990. return Ret(std::invoke(Candidate, *curr, std::forward<std::tuple_element_t<Index, std::tuple<Args...>>>(std::get<Index>(arguments))...));
  14991. };
  14992. }
  14993. template<auto Candidate, typename Type, std::size_t... Index>
  14994. [[nodiscard]] auto wrap(Type *, std::index_sequence<Index...>) ENTT_NOEXCEPT {
  14995. return [](const void *payload, Args... args) -> Ret {
  14996. [[maybe_unused]] const auto arguments = std::forward_as_tuple(std::forward<Args>(args)...);
  14997. Type *curr = static_cast<Type *>(const_cast<std::conditional_t<std::is_const_v<Type>, const void *, void *>>(payload));
  14998. return Ret(std::invoke(Candidate, curr, std::forward<std::tuple_element_t<Index, std::tuple<Args...>>>(std::get<Index>(arguments))...));
  14999. };
  15000. }
  15001. public:
  15002. /*! @brief Function type of the contained target. */
  15003. using function_type = Ret(const void *, Args...);
  15004. /*! @brief Function type of the delegate. */
  15005. using type = Ret(Args...);
  15006. /*! @brief Return type of the delegate. */
  15007. using result_type = Ret;
  15008. /*! @brief Default constructor. */
  15009. delegate() ENTT_NOEXCEPT
  15010. : fn{nullptr}, data{nullptr}
  15011. {}
  15012. /**
  15013. * @brief Constructs a delegate and connects a free function or an unbound
  15014. * member.
  15015. * @tparam Candidate Function or member to connect to the delegate.
  15016. */
  15017. template<auto Candidate>
  15018. delegate(connect_arg_t<Candidate>) ENTT_NOEXCEPT {
  15019. connect<Candidate>();
  15020. }
  15021. /**
  15022. * @brief Constructs a delegate and connects a free function with payload or
  15023. * a bound member.
  15024. * @tparam Candidate Function or member to connect to the delegate.
  15025. * @tparam Type Type of class or type of payload.
  15026. * @param value_or_instance A valid object that fits the purpose.
  15027. */
  15028. template<auto Candidate, typename Type>
  15029. delegate(connect_arg_t<Candidate>, Type &&value_or_instance) ENTT_NOEXCEPT {
  15030. connect<Candidate>(std::forward<Type>(value_or_instance));
  15031. }
  15032. /**
  15033. * @brief Constructs a delegate and connects an user defined function with
  15034. * optional payload.
  15035. * @param function Function to connect to the delegate.
  15036. * @param payload User defined arbitrary data.
  15037. */
  15038. delegate(function_type *function, const void *payload = nullptr) ENTT_NOEXCEPT {
  15039. connect(function, payload);
  15040. }
  15041. /**
  15042. * @brief Connects a free function or an unbound member to a delegate.
  15043. * @tparam Candidate Function or member to connect to the delegate.
  15044. */
  15045. template<auto Candidate>
  15046. void connect() ENTT_NOEXCEPT {
  15047. data = nullptr;
  15048. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Args...>) {
  15049. fn = [](const void *, Args... args) -> Ret {
  15050. return Ret(std::invoke(Candidate, std::forward<Args>(args)...));
  15051. };
  15052. } else if constexpr(std::is_member_pointer_v<decltype(Candidate)>) {
  15053. fn = wrap<Candidate>(internal::index_sequence_for<std::tuple_element_t<0, std::tuple<Args...>>>(internal::function_pointer_t<decltype(Candidate)>{}));
  15054. } else {
  15055. fn = wrap<Candidate>(internal::index_sequence_for(internal::function_pointer_t<decltype(Candidate)>{}));
  15056. }
  15057. }
  15058. /**
  15059. * @brief Connects a free function with payload or a bound member to a
  15060. * delegate.
  15061. *
  15062. * The delegate isn't responsible for the connected object or the payload.
  15063. * Users must always guarantee that the lifetime of the instance overcomes
  15064. * the one of the delegate.<br/>
  15065. * When used to connect a free function with payload, its signature must be
  15066. * such that the instance is the first argument before the ones used to
  15067. * define the delegate itself.
  15068. *
  15069. * @tparam Candidate Function or member to connect to the delegate.
  15070. * @tparam Type Type of class or type of payload.
  15071. * @param value_or_instance A valid reference that fits the purpose.
  15072. */
  15073. template<auto Candidate, typename Type>
  15074. void connect(Type &value_or_instance) ENTT_NOEXCEPT {
  15075. data = &value_or_instance;
  15076. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Type &, Args...>) {
  15077. fn = [](const void *payload, Args... args) -> Ret {
  15078. Type *curr = static_cast<Type *>(const_cast<std::conditional_t<std::is_const_v<Type>, const void *, void *>>(payload));
  15079. return Ret(std::invoke(Candidate, *curr, std::forward<Args>(args)...));
  15080. };
  15081. } else {
  15082. fn = wrap<Candidate>(value_or_instance, internal::index_sequence_for(internal::function_pointer_t<decltype(Candidate), Type>{}));
  15083. }
  15084. }
  15085. /**
  15086. * @brief Connects a free function with payload or a bound member to a
  15087. * delegate.
  15088. *
  15089. * @sa connect(Type &)
  15090. *
  15091. * @tparam Candidate Function or member to connect to the delegate.
  15092. * @tparam Type Type of class or type of payload.
  15093. * @param value_or_instance A valid pointer that fits the purpose.
  15094. */
  15095. template<auto Candidate, typename Type>
  15096. void connect(Type *value_or_instance) ENTT_NOEXCEPT {
  15097. data = value_or_instance;
  15098. if constexpr(std::is_invocable_r_v<Ret, decltype(Candidate), Type *, Args...>) {
  15099. fn = [](const void *payload, Args... args) -> Ret {
  15100. Type *curr = static_cast<Type *>(const_cast<std::conditional_t<std::is_const_v<Type>, const void *, void *>>(payload));
  15101. return Ret(std::invoke(Candidate, curr, std::forward<Args>(args)...));
  15102. };
  15103. } else {
  15104. fn = wrap<Candidate>(value_or_instance, internal::index_sequence_for(internal::function_pointer_t<decltype(Candidate), Type>{}));
  15105. }
  15106. }
  15107. /**
  15108. * @brief Connects an user defined function with optional payload to a
  15109. * delegate.
  15110. *
  15111. * The delegate isn't responsible for the connected object or the payload.
  15112. * Users must always guarantee that the lifetime of an instance overcomes
  15113. * the one of the delegate.<br/>
  15114. * The payload is returned as the first argument to the target function in
  15115. * all cases.
  15116. *
  15117. * @param function Function to connect to the delegate.
  15118. * @param payload User defined arbitrary data.
  15119. */
  15120. void connect(function_type *function, const void *payload = nullptr) ENTT_NOEXCEPT {
  15121. fn = function;
  15122. data = payload;
  15123. }
  15124. /**
  15125. * @brief Resets a delegate.
  15126. *
  15127. * After a reset, a delegate cannot be invoked anymore.
  15128. */
  15129. void reset() ENTT_NOEXCEPT {
  15130. fn = nullptr;
  15131. data = nullptr;
  15132. }
  15133. /**
  15134. * @brief Returns the instance or the payload linked to a delegate, if any.
  15135. * @return An opaque pointer to the underlying data.
  15136. */
  15137. [[nodiscard]] const void * instance() const ENTT_NOEXCEPT {
  15138. return data;
  15139. }
  15140. /**
  15141. * @brief Triggers a delegate.
  15142. *
  15143. * The delegate invokes the underlying function and returns the result.
  15144. *
  15145. * @warning
  15146. * Attempting to trigger an invalid delegate results in undefined
  15147. * behavior.<br/>
  15148. * An assertion will abort the execution at runtime in debug mode if the
  15149. * delegate has not yet been set.
  15150. *
  15151. * @param args Arguments to use to invoke the underlying function.
  15152. * @return The value returned by the underlying function.
  15153. */
  15154. Ret operator()(Args... args) const {
  15155. ENTT_ASSERT(fn);
  15156. return fn(data, std::forward<Args>(args)...);
  15157. }
  15158. /**
  15159. * @brief Checks whether a delegate actually stores a listener.
  15160. * @return False if the delegate is empty, true otherwise.
  15161. */
  15162. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  15163. // no need to test also data
  15164. return !(fn == nullptr);
  15165. }
  15166. /**
  15167. * @brief Compares the contents of two delegates.
  15168. * @param other Delegate with which to compare.
  15169. * @return False if the two contents differ, true otherwise.
  15170. */
  15171. [[nodiscard]] bool operator==(const delegate<Ret(Args...)> &other) const ENTT_NOEXCEPT {
  15172. return fn == other.fn && data == other.data;
  15173. }
  15174. private:
  15175. function_type *fn;
  15176. const void *data;
  15177. };
  15178. /**
  15179. * @brief Compares the contents of two delegates.
  15180. * @tparam Ret Return type of a function type.
  15181. * @tparam Args Types of arguments of a function type.
  15182. * @param lhs A valid delegate object.
  15183. * @param rhs A valid delegate object.
  15184. * @return True if the two contents differ, false otherwise.
  15185. */
  15186. template<typename Ret, typename... Args>
  15187. [[nodiscard]] bool operator!=(const delegate<Ret(Args...)> &lhs, const delegate<Ret(Args...)> &rhs) ENTT_NOEXCEPT {
  15188. return !(lhs == rhs);
  15189. }
  15190. /**
  15191. * @brief Deduction guide.
  15192. * @tparam Candidate Function or member to connect to the delegate.
  15193. */
  15194. template<auto Candidate>
  15195. delegate(connect_arg_t<Candidate>) ENTT_NOEXCEPT
  15196. -> delegate<std::remove_pointer_t<internal::function_pointer_t<decltype(Candidate)>>>;
  15197. /**
  15198. * @brief Deduction guide.
  15199. * @tparam Candidate Function or member to connect to the delegate.
  15200. * @tparam Type Type of class or type of payload.
  15201. */
  15202. template<auto Candidate, typename Type>
  15203. delegate(connect_arg_t<Candidate>, Type &&) ENTT_NOEXCEPT
  15204. -> delegate<std::remove_pointer_t<internal::function_pointer_t<decltype(Candidate), Type>>>;
  15205. /*! @brief Deduction guide. */
  15206. template<typename Ret, typename... Args>
  15207. delegate(Ret(*)(const void *, Args...), const void * = nullptr) ENTT_NOEXCEPT
  15208. -> delegate<Ret(Args...)>;
  15209. }
  15210. #endif
  15211. // #include "signal/dispatcher.hpp"
  15212. #ifndef ENTT_SIGNAL_DISPATCHER_HPP
  15213. #define ENTT_SIGNAL_DISPATCHER_HPP
  15214. #include <cstddef>
  15215. #include <memory>
  15216. #include <type_traits>
  15217. #include <utility>
  15218. #include <vector>
  15219. // #include "../config/config.h"
  15220. // #include "../core/fwd.hpp"
  15221. #ifndef ENTT_CORE_FWD_HPP
  15222. #define ENTT_CORE_FWD_HPP
  15223. // #include "../config/config.h"
  15224. #ifndef ENTT_CONFIG_CONFIG_H
  15225. #define ENTT_CONFIG_CONFIG_H
  15226. #ifndef ENTT_NOEXCEPT
  15227. # define ENTT_NOEXCEPT noexcept
  15228. #endif
  15229. #ifndef ENTT_HS_SUFFIX
  15230. # define ENTT_HS_SUFFIX _hs
  15231. #endif
  15232. #ifndef ENTT_HWS_SUFFIX
  15233. # define ENTT_HWS_SUFFIX _hws
  15234. #endif
  15235. #ifndef ENTT_USE_ATOMIC
  15236. # define ENTT_MAYBE_ATOMIC(Type) Type
  15237. #else
  15238. # include <atomic>
  15239. # define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
  15240. #endif
  15241. #ifndef ENTT_ID_TYPE
  15242. # include <cstdint>
  15243. # define ENTT_ID_TYPE std::uint32_t
  15244. #endif
  15245. #ifndef ENTT_PAGE_SIZE
  15246. # define ENTT_PAGE_SIZE 32768
  15247. #endif
  15248. #ifndef ENTT_ASSERT
  15249. # include <cassert>
  15250. # define ENTT_ASSERT(condition) assert(condition)
  15251. #endif
  15252. #ifndef ENTT_NO_ETO
  15253. # include <type_traits>
  15254. # define ENTT_IS_EMPTY(Type) std::is_empty<Type>
  15255. #else
  15256. # include <type_traits>
  15257. # define ENTT_IS_EMPTY(Type) std::false_type
  15258. #endif
  15259. #ifndef ENTT_STANDARD_CPP
  15260. # if defined __clang__ || (defined __GNUC__ && __GNUC__ > 8)
  15261. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  15262. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  15263. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  15264. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  15265. # elif defined __GNUC__
  15266. # define ENTT_PRETTY_FUNCTION __PRETTY_FUNCTION__
  15267. # define ENTT_PRETTY_FUNCTION_PREFIX '='
  15268. # define ENTT_PRETTY_FUNCTION_SUFFIX ']'
  15269. # elif defined _MSC_VER
  15270. # define ENTT_PRETTY_FUNCTION_CONSTEXPR
  15271. # define ENTT_PRETTY_FUNCTION __FUNCSIG__
  15272. # define ENTT_PRETTY_FUNCTION_PREFIX '<'
  15273. # define ENTT_PRETTY_FUNCTION_SUFFIX '>'
  15274. # endif
  15275. #endif
  15276. #ifndef ENTT_STANDALONE
  15277. # define ENTT_FAST_PATH(...) false
  15278. #else
  15279. # define ENTT_FAST_PATH(Cond) Cond
  15280. #endif
  15281. #endif
  15282. namespace entt {
  15283. /*! @brief Alias declaration for type identifiers. */
  15284. using id_type = ENTT_ID_TYPE;
  15285. }
  15286. #endif
  15287. // #include "../core/type_info.hpp"
  15288. #ifndef ENTT_CORE_TYPE_INFO_HPP
  15289. #define ENTT_CORE_TYPE_INFO_HPP
  15290. #include <string_view>
  15291. // #include "../config/config.h"
  15292. // #include "../core/attribute.h"
  15293. #ifndef ENTT_CORE_ATTRIBUTE_H
  15294. #define ENTT_CORE_ATTRIBUTE_H
  15295. #ifndef ENTT_EXPORT
  15296. # if defined _WIN32 || defined __CYGWIN__ || defined _MSC_VER
  15297. # define ENTT_EXPORT __declspec(dllexport)
  15298. # define ENTT_IMPORT __declspec(dllimport)
  15299. # define ENTT_HIDDEN
  15300. # elif defined __GNUC__ && __GNUC__ >= 4
  15301. # define ENTT_EXPORT __attribute__((visibility("default")))
  15302. # define ENTT_IMPORT __attribute__((visibility("default")))
  15303. # define ENTT_HIDDEN __attribute__((visibility("hidden")))
  15304. # else /* Unsupported compiler */
  15305. # define ENTT_EXPORT
  15306. # define ENTT_IMPORT
  15307. # define ENTT_HIDDEN
  15308. # endif
  15309. #endif
  15310. #ifndef ENTT_API
  15311. # if defined ENTT_API_EXPORT
  15312. # define ENTT_API ENTT_EXPORT
  15313. # elif defined ENTT_API_IMPORT
  15314. # define ENTT_API ENTT_IMPORT
  15315. # else /* No API */
  15316. # define ENTT_API
  15317. # endif
  15318. #endif
  15319. #endif
  15320. // #include "hashed_string.hpp"
  15321. #ifndef ENTT_CORE_HASHED_STRING_HPP
  15322. #define ENTT_CORE_HASHED_STRING_HPP
  15323. #include <cstddef>
  15324. #include <cstdint>
  15325. // #include "../config/config.h"
  15326. // #include "fwd.hpp"
  15327. namespace entt {
  15328. /**
  15329. * @cond TURN_OFF_DOXYGEN
  15330. * Internal details not to be documented.
  15331. */
  15332. namespace internal {
  15333. template<typename>
  15334. struct fnv1a_traits;
  15335. template<>
  15336. struct fnv1a_traits<std::uint32_t> {
  15337. using type = std::uint32_t;
  15338. static constexpr std::uint32_t offset = 2166136261;
  15339. static constexpr std::uint32_t prime = 16777619;
  15340. };
  15341. template<>
  15342. struct fnv1a_traits<std::uint64_t> {
  15343. using type = std::uint64_t;
  15344. static constexpr std::uint64_t offset = 14695981039346656037ull;
  15345. static constexpr std::uint64_t prime = 1099511628211ull;
  15346. };
  15347. }
  15348. /**
  15349. * Internal details not to be documented.
  15350. * @endcond
  15351. */
  15352. /**
  15353. * @brief Zero overhead unique identifier.
  15354. *
  15355. * A hashed string is a compile-time tool that allows users to use
  15356. * human-readable identifers in the codebase while using their numeric
  15357. * counterparts at runtime.<br/>
  15358. * Because of that, a hashed string can also be used in constant expressions if
  15359. * required.
  15360. *
  15361. * @tparam Char Character type.
  15362. */
  15363. template<typename Char>
  15364. class basic_hashed_string {
  15365. using traits_type = internal::fnv1a_traits<id_type>;
  15366. struct const_wrapper {
  15367. // non-explicit constructor on purpose
  15368. constexpr const_wrapper(const Char *curr) ENTT_NOEXCEPT: str{curr} {}
  15369. const Char *str;
  15370. };
  15371. // Fowler–Noll–Vo hash function v. 1a - the good
  15372. [[nodiscard]] static constexpr id_type helper(const Char *curr) ENTT_NOEXCEPT {
  15373. auto value = traits_type::offset;
  15374. while(*curr != 0) {
  15375. value = (value ^ static_cast<traits_type::type>(*(curr++))) * traits_type::prime;
  15376. }
  15377. return value;
  15378. }
  15379. public:
  15380. /*! @brief Character type. */
  15381. using value_type = Char;
  15382. /*! @brief Unsigned integer type. */
  15383. using hash_type = id_type;
  15384. /**
  15385. * @brief Returns directly the numeric representation of a string.
  15386. *
  15387. * Forcing template resolution avoids implicit conversions. An
  15388. * human-readable identifier can be anything but a plain, old bunch of
  15389. * characters.<br/>
  15390. * Example of use:
  15391. * @code{.cpp}
  15392. * const auto value = basic_hashed_string<char>::to_value("my.png");
  15393. * @endcode
  15394. *
  15395. * @tparam N Number of characters of the identifier.
  15396. * @param str Human-readable identifer.
  15397. * @return The numeric representation of the string.
  15398. */
  15399. template<std::size_t N>
  15400. [[nodiscard]] static constexpr hash_type value(const value_type (&str)[N]) ENTT_NOEXCEPT {
  15401. return helper(str);
  15402. }
  15403. /**
  15404. * @brief Returns directly the numeric representation of a string.
  15405. * @param wrapper Helps achieving the purpose by relying on overloading.
  15406. * @return The numeric representation of the string.
  15407. */
  15408. [[nodiscard]] static hash_type value(const_wrapper wrapper) ENTT_NOEXCEPT {
  15409. return helper(wrapper.str);
  15410. }
  15411. /**
  15412. * @brief Returns directly the numeric representation of a string view.
  15413. * @param str Human-readable identifer.
  15414. * @param size Length of the string to hash.
  15415. * @return The numeric representation of the string.
  15416. */
  15417. [[nodiscard]] static hash_type value(const value_type *str, std::size_t size) ENTT_NOEXCEPT {
  15418. id_type partial{traits_type::offset};
  15419. while(size--) { partial = (partial^(str++)[0])*traits_type::prime; }
  15420. return partial;
  15421. }
  15422. /*! @brief Constructs an empty hashed string. */
  15423. constexpr basic_hashed_string() ENTT_NOEXCEPT
  15424. : str{nullptr}, hash{}
  15425. {}
  15426. /**
  15427. * @brief Constructs a hashed string from an array of const characters.
  15428. *
  15429. * Forcing template resolution avoids implicit conversions. An
  15430. * human-readable identifier can be anything but a plain, old bunch of
  15431. * characters.<br/>
  15432. * Example of use:
  15433. * @code{.cpp}
  15434. * basic_hashed_string<char> hs{"my.png"};
  15435. * @endcode
  15436. *
  15437. * @tparam N Number of characters of the identifier.
  15438. * @param curr Human-readable identifer.
  15439. */
  15440. template<std::size_t N>
  15441. constexpr basic_hashed_string(const value_type (&curr)[N]) ENTT_NOEXCEPT
  15442. : str{curr}, hash{helper(curr)}
  15443. {}
  15444. /**
  15445. * @brief Explicit constructor on purpose to avoid constructing a hashed
  15446. * string directly from a `const value_type *`.
  15447. * @param wrapper Helps achieving the purpose by relying on overloading.
  15448. */
  15449. explicit constexpr basic_hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
  15450. : str{wrapper.str}, hash{helper(wrapper.str)}
  15451. {}
  15452. /**
  15453. * @brief Returns the human-readable representation of a hashed string.
  15454. * @return The string used to initialize the instance.
  15455. */
  15456. [[nodiscard]] constexpr const value_type * data() const ENTT_NOEXCEPT {
  15457. return str;
  15458. }
  15459. /**
  15460. * @brief Returns the numeric representation of a hashed string.
  15461. * @return The numeric representation of the instance.
  15462. */
  15463. [[nodiscard]] constexpr hash_type value() const ENTT_NOEXCEPT {
  15464. return hash;
  15465. }
  15466. /*! @copydoc data */
  15467. [[nodiscard]] constexpr operator const value_type *() const ENTT_NOEXCEPT { return data(); }
  15468. /**
  15469. * @brief Returns the numeric representation of a hashed string.
  15470. * @return The numeric representation of the instance.
  15471. */
  15472. [[nodiscard]] constexpr operator hash_type() const ENTT_NOEXCEPT { return value(); }
  15473. /**
  15474. * @brief Compares two hashed strings.
  15475. * @param other Hashed string with which to compare.
  15476. * @return True if the two hashed strings are identical, false otherwise.
  15477. */
  15478. [[nodiscard]] constexpr bool operator==(const basic_hashed_string &other) const ENTT_NOEXCEPT {
  15479. return hash == other.hash;
  15480. }
  15481. private:
  15482. const value_type *str;
  15483. hash_type hash;
  15484. };
  15485. /**
  15486. * @brief Deduction guide.
  15487. *
  15488. * It allows to deduce the character type of the hashed string directly from a
  15489. * human-readable identifer provided to the constructor.
  15490. *
  15491. * @tparam Char Character type.
  15492. * @tparam N Number of characters of the identifier.
  15493. * @param str Human-readable identifer.
  15494. */
  15495. template<typename Char, std::size_t N>
  15496. basic_hashed_string(const Char (&str)[N]) ENTT_NOEXCEPT
  15497. -> basic_hashed_string<Char>;
  15498. /**
  15499. * @brief Compares two hashed strings.
  15500. * @tparam Char Character type.
  15501. * @param lhs A valid hashed string.
  15502. * @param rhs A valid hashed string.
  15503. * @return True if the two hashed strings are identical, false otherwise.
  15504. */
  15505. template<typename Char>
  15506. [[nodiscard]] constexpr bool operator!=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) ENTT_NOEXCEPT {
  15507. return !(lhs == rhs);
  15508. }
  15509. /*! @brief Aliases for common character types. */
  15510. using hashed_string = basic_hashed_string<char>;
  15511. /*! @brief Aliases for common character types. */
  15512. using hashed_wstring = basic_hashed_string<wchar_t>;
  15513. }
  15514. /**
  15515. * @brief User defined literal for hashed strings.
  15516. * @param str The literal without its suffix.
  15517. * @return A properly initialized hashed string.
  15518. */
  15519. [[nodiscard]] constexpr entt::hashed_string operator"" ENTT_HS_SUFFIX(const char *str, std::size_t) ENTT_NOEXCEPT {
  15520. return entt::hashed_string{str};
  15521. }
  15522. /**
  15523. * @brief User defined literal for hashed wstrings.
  15524. * @param str The literal without its suffix.
  15525. * @return A properly initialized hashed wstring.
  15526. */
  15527. [[nodiscard]] constexpr entt::hashed_wstring operator"" ENTT_HWS_SUFFIX(const wchar_t *str, std::size_t) ENTT_NOEXCEPT {
  15528. return entt::hashed_wstring{str};
  15529. }
  15530. #endif
  15531. // #include "fwd.hpp"
  15532. namespace entt {
  15533. /**
  15534. * @cond TURN_OFF_DOXYGEN
  15535. * Internal details not to be documented.
  15536. */
  15537. namespace internal {
  15538. struct ENTT_API type_index {
  15539. [[nodiscard]] static id_type next() ENTT_NOEXCEPT {
  15540. static ENTT_MAYBE_ATOMIC(id_type) value{};
  15541. return value++;
  15542. }
  15543. };
  15544. template<typename Type>
  15545. [[nodiscard]] constexpr auto type_name() ENTT_NOEXCEPT {
  15546. #if defined ENTT_PRETTY_FUNCTION
  15547. std::string_view pretty_function{ENTT_PRETTY_FUNCTION};
  15548. auto first = pretty_function.find_first_not_of(' ', pretty_function.find_first_of(ENTT_PRETTY_FUNCTION_PREFIX)+1);
  15549. auto value = pretty_function.substr(first, pretty_function.find_last_of(ENTT_PRETTY_FUNCTION_SUFFIX) - first);
  15550. return value;
  15551. #else
  15552. return std::string_view{};
  15553. #endif
  15554. }
  15555. }
  15556. /**
  15557. * Internal details not to be documented.
  15558. * @endcond
  15559. */
  15560. /**
  15561. * @brief Type index.
  15562. * @tparam Type Type for which to generate a sequential identifier.
  15563. */
  15564. template<typename Type, typename = void>
  15565. struct ENTT_API type_index {
  15566. /**
  15567. * @brief Returns the sequential identifier of a given type.
  15568. * @return The sequential identifier of a given type.
  15569. */
  15570. [[nodiscard]] static id_type value() ENTT_NOEXCEPT {
  15571. static const id_type value = internal::type_index::next();
  15572. return value;
  15573. }
  15574. };
  15575. /**
  15576. * @brief Provides the member constant `value` to true if a given type is
  15577. * indexable, false otherwise.
  15578. * @tparam Type Potentially indexable type.
  15579. */
  15580. template<typename, typename = void>
  15581. struct has_type_index: std::false_type {};
  15582. /*! @brief has_type_index */
  15583. template<typename Type>
  15584. struct has_type_index<Type, std::void_t<decltype(type_index<Type>::value())>>: std::true_type {};
  15585. /**
  15586. * @brief Helper variable template.
  15587. * @tparam Type Potentially indexable type.
  15588. */
  15589. template<typename Type>
  15590. inline constexpr bool has_type_index_v = has_type_index<Type>::value;
  15591. /**
  15592. * @brief Type info.
  15593. * @tparam Type Type for which to generate information.
  15594. */
  15595. template<typename Type, typename = void>
  15596. struct type_info {
  15597. /**
  15598. * @brief Returns the numeric representation of a given type.
  15599. * @return The numeric representation of the given type.
  15600. */
  15601. #if defined ENTT_PRETTY_FUNCTION_CONSTEXPR
  15602. [[nodiscard]] static constexpr id_type id() ENTT_NOEXCEPT {
  15603. constexpr auto value = hashed_string::value(ENTT_PRETTY_FUNCTION);
  15604. return value;
  15605. }
  15606. #elif defined ENTT_PRETTY_FUNCTION
  15607. [[nodiscard]] static id_type id() ENTT_NOEXCEPT {
  15608. static const auto value = hashed_string::value(ENTT_PRETTY_FUNCTION);
  15609. return value;
  15610. }
  15611. #else
  15612. [[nodiscard]] static id_type id() ENTT_NOEXCEPT {
  15613. return type_index<Type>::value();
  15614. }
  15615. #endif
  15616. /**
  15617. * @brief Returns the name of a given type.
  15618. * @return The name of the given type.
  15619. */
  15620. #if defined ENTT_PRETTY_FUNCTION_CONSTEXPR
  15621. [[nodiscard]] static constexpr std::string_view name() ENTT_NOEXCEPT {
  15622. constexpr auto value = internal::type_name<Type>();
  15623. return value;
  15624. }
  15625. #elif defined ENTT_PRETTY_FUNCTION
  15626. [[nodiscard]] static std::string_view name() ENTT_NOEXCEPT {
  15627. static const auto value = internal::type_name<Type>();
  15628. return value;
  15629. }
  15630. #else
  15631. [[nodiscard]] static constexpr std::string_view name() ENTT_NOEXCEPT {
  15632. return internal::type_name<Type>();
  15633. }
  15634. #endif
  15635. };
  15636. }
  15637. #endif
  15638. // #include "sigh.hpp"
  15639. #ifndef ENTT_SIGNAL_SIGH_HPP
  15640. #define ENTT_SIGNAL_SIGH_HPP
  15641. #include <vector>
  15642. #include <utility>
  15643. #include <iterator>
  15644. #include <algorithm>
  15645. #include <functional>
  15646. #include <type_traits>
  15647. // #include "../config/config.h"
  15648. // #include "delegate.hpp"
  15649. // #include "fwd.hpp"
  15650. #ifndef ENTT_SIGNAL_FWD_HPP
  15651. #define ENTT_SIGNAL_FWD_HPP
  15652. namespace entt {
  15653. template<typename>
  15654. class delegate;
  15655. class dispatcher;
  15656. template<typename>
  15657. class emitter;
  15658. class connection;
  15659. struct scoped_connection;
  15660. template<typename>
  15661. class sink;
  15662. template<typename>
  15663. class sigh;
  15664. }
  15665. #endif
  15666. namespace entt {
  15667. /**
  15668. * @brief Sink class.
  15669. *
  15670. * Primary template isn't defined on purpose. All the specializations give a
  15671. * compile-time error unless the template parameter is a function type.
  15672. *
  15673. * @tparam Function A valid function type.
  15674. */
  15675. template<typename Function>
  15676. class sink;
  15677. /**
  15678. * @brief Unmanaged signal handler.
  15679. *
  15680. * Primary template isn't defined on purpose. All the specializations give a
  15681. * compile-time error unless the template parameter is a function type.
  15682. *
  15683. * @tparam Function A valid function type.
  15684. */
  15685. template<typename Function>
  15686. class sigh;
  15687. /**
  15688. * @brief Unmanaged signal handler.
  15689. *
  15690. * It works directly with references to classes and pointers to member functions
  15691. * as well as pointers to free functions. Users of this class are in charge of
  15692. * disconnecting instances before deleting them.
  15693. *
  15694. * This class serves mainly two purposes:
  15695. *
  15696. * * Creating signals to use later to notify a bunch of listeners.
  15697. * * Collecting results from a set of functions like in a voting system.
  15698. *
  15699. * @tparam Ret Return type of a function type.
  15700. * @tparam Args Types of arguments of a function type.
  15701. */
  15702. template<typename Ret, typename... Args>
  15703. class sigh<Ret(Args...)> {
  15704. /*! @brief A sink is allowed to modify a signal. */
  15705. friend class sink<Ret(Args...)>;
  15706. public:
  15707. /*! @brief Unsigned integer type. */
  15708. using size_type = std::size_t;
  15709. /*! @brief Sink type. */
  15710. using sink_type = sink<Ret(Args...)>;
  15711. /**
  15712. * @brief Instance type when it comes to connecting member functions.
  15713. * @tparam Class Type of class to which the member function belongs.
  15714. */
  15715. template<typename Class>
  15716. using instance_type = Class *;
  15717. /**
  15718. * @brief Number of listeners connected to the signal.
  15719. * @return Number of listeners currently connected.
  15720. */
  15721. [[nodiscard]] size_type size() const ENTT_NOEXCEPT {
  15722. return calls.size();
  15723. }
  15724. /**
  15725. * @brief Returns false if at least a listener is connected to the signal.
  15726. * @return True if the signal has no listeners connected, false otherwise.
  15727. */
  15728. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  15729. return calls.empty();
  15730. }
  15731. /**
  15732. * @brief Triggers a signal.
  15733. *
  15734. * All the listeners are notified. Order isn't guaranteed.
  15735. *
  15736. * @param args Arguments to use to invoke listeners.
  15737. */
  15738. void publish(Args... args) const {
  15739. for(auto &&call: std::as_const(calls)) {
  15740. call(args...);
  15741. }
  15742. }
  15743. /**
  15744. * @brief Collects return values from the listeners.
  15745. *
  15746. * The collector must expose a call operator with the following properties:
  15747. *
  15748. * * The return type is either `void` or such that it's convertible to
  15749. * `bool`. In the second case, a true value will stop the iteration.
  15750. * * The list of parameters is empty if `Ret` is `void`, otherwise it
  15751. * contains a single element such that `Ret` is convertible to it.
  15752. *
  15753. * @tparam Func Type of collector to use, if any.
  15754. * @param func A valid function object.
  15755. * @param args Arguments to use to invoke listeners.
  15756. */
  15757. template<typename Func>
  15758. void collect(Func func, Args... args) const {
  15759. for(auto &&call: calls) {
  15760. if constexpr(std::is_void_v<Ret>) {
  15761. if constexpr(std::is_invocable_r_v<bool, Func>) {
  15762. call(args...);
  15763. if(func()) { break; }
  15764. } else {
  15765. call(args...);
  15766. func();
  15767. }
  15768. } else {
  15769. if constexpr(std::is_invocable_r_v<bool, Func, Ret>) {
  15770. if(func(call(args...))) { break; }
  15771. } else {
  15772. func(call(args...));
  15773. }
  15774. }
  15775. }
  15776. }
  15777. private:
  15778. std::vector<delegate<Ret(Args...)>> calls;
  15779. };
  15780. /**
  15781. * @brief Connection class.
  15782. *
  15783. * Opaque object the aim of which is to allow users to release an already
  15784. * estabilished connection without having to keep a reference to the signal or
  15785. * the sink that generated it.
  15786. */
  15787. class connection {
  15788. /*! @brief A sink is allowed to create connection objects. */
  15789. template<typename>
  15790. friend class sink;
  15791. connection(delegate<void(void *)> fn, void *ref)
  15792. : disconnect{fn}, signal{ref}
  15793. {}
  15794. public:
  15795. /*! @brief Default constructor. */
  15796. connection() = default;
  15797. /**
  15798. * @brief Checks whether a connection is properly initialized.
  15799. * @return True if the connection is properly initialized, false otherwise.
  15800. */
  15801. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  15802. return static_cast<bool>(disconnect);
  15803. }
  15804. /*! @brief Breaks the connection. */
  15805. void release() {
  15806. if(disconnect) {
  15807. disconnect(signal);
  15808. disconnect.reset();
  15809. }
  15810. }
  15811. private:
  15812. delegate<void(void *)> disconnect;
  15813. void *signal{};
  15814. };
  15815. /**
  15816. * @brief Scoped connection class.
  15817. *
  15818. * Opaque object the aim of which is to allow users to release an already
  15819. * estabilished connection without having to keep a reference to the signal or
  15820. * the sink that generated it.<br/>
  15821. * A scoped connection automatically breaks the link between the two objects
  15822. * when it goes out of scope.
  15823. */
  15824. struct scoped_connection {
  15825. /*! @brief Default constructor. */
  15826. scoped_connection() = default;
  15827. /**
  15828. * @brief Constructs a scoped connection from a basic connection.
  15829. * @param other A valid connection object.
  15830. */
  15831. scoped_connection(const connection &other)
  15832. : conn{other}
  15833. {}
  15834. /*! @brief Default copy constructor, deleted on purpose. */
  15835. scoped_connection(const scoped_connection &) = delete;
  15836. /*! @brief Automatically breaks the link on destruction. */
  15837. ~scoped_connection() {
  15838. conn.release();
  15839. }
  15840. /**
  15841. * @brief Default copy assignment operator, deleted on purpose.
  15842. * @return This scoped connection.
  15843. */
  15844. scoped_connection & operator=(const scoped_connection &) = delete;
  15845. /**
  15846. * @brief Acquires a connection.
  15847. * @param other The connection object to acquire.
  15848. * @return This scoped connection.
  15849. */
  15850. scoped_connection & operator=(connection other) {
  15851. conn = std::move(other);
  15852. return *this;
  15853. }
  15854. /**
  15855. * @brief Checks whether a scoped connection is properly initialized.
  15856. * @return True if the connection is properly initialized, false otherwise.
  15857. */
  15858. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  15859. return static_cast<bool>(conn);
  15860. }
  15861. /*! @brief Breaks the connection. */
  15862. void release() {
  15863. conn.release();
  15864. }
  15865. private:
  15866. connection conn;
  15867. };
  15868. /**
  15869. * @brief Sink class.
  15870. *
  15871. * A sink is used to connect listeners to signals and to disconnect them.<br/>
  15872. * The function type for a listener is the one of the signal to which it
  15873. * belongs.
  15874. *
  15875. * The clear separation between a signal and a sink permits to store the former
  15876. * as private data member without exposing the publish functionality to the
  15877. * users of the class.
  15878. *
  15879. * @warning
  15880. * Lifetime of a sink must not overcome that of the signal to which it refers.
  15881. * In any other case, attempting to use a sink results in undefined behavior.
  15882. *
  15883. * @tparam Ret Return type of a function type.
  15884. * @tparam Args Types of arguments of a function type.
  15885. */
  15886. template<typename Ret, typename... Args>
  15887. class sink<Ret(Args...)> {
  15888. using signal_type = sigh<Ret(Args...)>;
  15889. using difference_type = typename std::iterator_traits<typename decltype(signal_type::calls)::iterator>::difference_type;
  15890. template<auto Candidate, typename Type>
  15891. static void release(Type value_or_instance, void *signal) {
  15892. sink{*static_cast<signal_type *>(signal)}.disconnect<Candidate>(value_or_instance);
  15893. }
  15894. template<auto Candidate>
  15895. static void release(void *signal) {
  15896. sink{*static_cast<signal_type *>(signal)}.disconnect<Candidate>();
  15897. }
  15898. public:
  15899. /**
  15900. * @brief Constructs a sink that is allowed to modify a given signal.
  15901. * @param ref A valid reference to a signal object.
  15902. */
  15903. sink(sigh<Ret(Args...)> &ref) ENTT_NOEXCEPT
  15904. : offset{},
  15905. signal{&ref}
  15906. {}
  15907. /**
  15908. * @brief Returns false if at least a listener is connected to the sink.
  15909. * @return True if the sink has no listeners connected, false otherwise.
  15910. */
  15911. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  15912. return signal->calls.empty();
  15913. }
  15914. /**
  15915. * @brief Returns a sink that connects before a given free function or an
  15916. * unbound member.
  15917. * @tparam Function A valid free function pointer.
  15918. * @return A properly initialized sink object.
  15919. */
  15920. template<auto Function>
  15921. [[nodiscard]] sink before() {
  15922. delegate<Ret(Args...)> call{};
  15923. call.template connect<Function>();
  15924. const auto &calls = signal->calls;
  15925. const auto it = std::find(calls.cbegin(), calls.cend(), std::move(call));
  15926. sink other{*this};
  15927. other.offset = std::distance(it, calls.cend());
  15928. return other;
  15929. }
  15930. /**
  15931. * @brief Returns a sink that connects before a free function with payload
  15932. * or a bound member.
  15933. * @tparam Candidate Member or free function to look for.
  15934. * @tparam Type Type of class or type of payload.
  15935. * @param value_or_instance A valid object that fits the purpose.
  15936. * @return A properly initialized sink object.
  15937. */
  15938. template<auto Candidate, typename Type>
  15939. [[nodiscard]] sink before(Type &&value_or_instance) {
  15940. delegate<Ret(Args...)> call{};
  15941. call.template connect<Candidate>(value_or_instance);
  15942. const auto &calls = signal->calls;
  15943. const auto it = std::find(calls.cbegin(), calls.cend(), std::move(call));
  15944. sink other{*this};
  15945. other.offset = std::distance(it, calls.cend());
  15946. return other;
  15947. }
  15948. /**
  15949. * @brief Returns a sink that connects before a given instance or specific
  15950. * payload.
  15951. * @tparam Type Type of class or type of payload.
  15952. * @param value_or_instance A valid object that fits the purpose.
  15953. * @return A properly initialized sink object.
  15954. */
  15955. template<typename Type>
  15956. [[nodiscard]] sink before(Type &value_or_instance) {
  15957. return before(&value_or_instance);
  15958. }
  15959. /**
  15960. * @brief Returns a sink that connects before a given instance or specific
  15961. * payload.
  15962. * @tparam Type Type of class or type of payload.
  15963. * @param value_or_instance A valid pointer that fits the purpose.
  15964. * @return A properly initialized sink object.
  15965. */
  15966. template<typename Type>
  15967. [[nodiscard]] sink before(Type *value_or_instance) {
  15968. sink other{*this};
  15969. if(value_or_instance) {
  15970. const auto &calls = signal->calls;
  15971. const auto it = std::find_if(calls.cbegin(), calls.cend(), [value_or_instance](const auto &delegate) {
  15972. return delegate.instance() == value_or_instance;
  15973. });
  15974. other.offset = std::distance(it, calls.cend());
  15975. }
  15976. return other;
  15977. }
  15978. /**
  15979. * @brief Returns a sink that connects before anything else.
  15980. * @return A properly initialized sink object.
  15981. */
  15982. [[nodiscard]] sink before() {
  15983. sink other{*this};
  15984. other.offset = signal->calls.size();
  15985. return other;
  15986. }
  15987. /**
  15988. * @brief Connects a free function or an unbound member to a signal.
  15989. *
  15990. * The signal handler performs checks to avoid multiple connections for the
  15991. * same function.
  15992. *
  15993. * @tparam Candidate Function or member to connect to the signal.
  15994. * @return A properly initialized connection object.
  15995. */
  15996. template<auto Candidate>
  15997. connection connect() {
  15998. disconnect<Candidate>();
  15999. delegate<Ret(Args...)> call{};
  16000. call.template connect<Candidate>();
  16001. signal->calls.insert(signal->calls.end() - offset, std::move(call));
  16002. delegate<void(void *)> conn{};
  16003. conn.template connect<&release<Candidate>>();
  16004. return { std::move(conn), signal };
  16005. }
  16006. /**
  16007. * @brief Connects a free function with payload or a bound member to a
  16008. * signal.
  16009. *
  16010. * The signal isn't responsible for the connected object or the payload.
  16011. * Users must always guarantee that the lifetime of the instance overcomes
  16012. * the one of the signal. On the other side, the signal handler performs
  16013. * checks to avoid multiple connections for the same function.<br/>
  16014. * When used to connect a free function with payload, its signature must be
  16015. * such that the instance is the first argument before the ones used to
  16016. * define the signal itself.
  16017. *
  16018. * @tparam Candidate Function or member to connect to the signal.
  16019. * @tparam Type Type of class or type of payload.
  16020. * @param value_or_instance A valid object that fits the purpose.
  16021. * @return A properly initialized connection object.
  16022. */
  16023. template<auto Candidate, typename Type>
  16024. connection connect(Type &&value_or_instance) {
  16025. disconnect<Candidate>(value_or_instance);
  16026. delegate<Ret(Args...)> call{};
  16027. call.template connect<Candidate>(value_or_instance);
  16028. signal->calls.insert(signal->calls.end() - offset, std::move(call));
  16029. delegate<void(void *)> conn{};
  16030. conn.template connect<&release<Candidate, Type>>(value_or_instance);
  16031. return { std::move(conn), signal };
  16032. }
  16033. /**
  16034. * @brief Disconnects a free function or an unbound member from a signal.
  16035. * @tparam Candidate Function or member to disconnect from the signal.
  16036. */
  16037. template<auto Candidate>
  16038. void disconnect() {
  16039. auto &calls = signal->calls;
  16040. delegate<Ret(Args...)> call{};
  16041. call.template connect<Candidate>();
  16042. calls.erase(std::remove(calls.begin(), calls.end(), std::move(call)), calls.end());
  16043. }
  16044. /**
  16045. * @brief Disconnects a free function with payload or a bound member from a
  16046. * signal.
  16047. * @tparam Candidate Function or member to disconnect from the signal.
  16048. * @tparam Type Type of class or type of payload.
  16049. * @param value_or_instance A valid object that fits the purpose.
  16050. */
  16051. template<auto Candidate, typename Type>
  16052. void disconnect(Type &&value_or_instance) {
  16053. auto &calls = signal->calls;
  16054. delegate<Ret(Args...)> call{};
  16055. call.template connect<Candidate>(value_or_instance);
  16056. calls.erase(std::remove(calls.begin(), calls.end(), std::move(call)), calls.end());
  16057. }
  16058. /**
  16059. * @brief Disconnects free functions with payload or bound members from a
  16060. * signal.
  16061. * @tparam Type Type of class or type of payload.
  16062. * @param value_or_instance A valid object that fits the purpose.
  16063. */
  16064. template<typename Type>
  16065. void disconnect(Type &value_or_instance) {
  16066. disconnect(&value_or_instance);
  16067. }
  16068. /**
  16069. * @brief Disconnects free functions with payload or bound members from a
  16070. * signal.
  16071. * @tparam Type Type of class or type of payload.
  16072. * @param value_or_instance A valid object that fits the purpose.
  16073. */
  16074. template<typename Type>
  16075. void disconnect(Type *value_or_instance) {
  16076. if(value_or_instance) {
  16077. auto &calls = signal->calls;
  16078. calls.erase(std::remove_if(calls.begin(), calls.end(), [value_or_instance](const auto &delegate) {
  16079. return delegate.instance() == value_or_instance;
  16080. }), calls.end());
  16081. }
  16082. }
  16083. /*! @brief Disconnects all the listeners from a signal. */
  16084. void disconnect() {
  16085. signal->calls.clear();
  16086. }
  16087. private:
  16088. difference_type offset;
  16089. signal_type *signal;
  16090. };
  16091. /**
  16092. * @brief Deduction guide.
  16093. *
  16094. * It allows to deduce the function type of a sink directly from the signal it
  16095. * refers to.
  16096. *
  16097. * @tparam Ret Return type of a function type.
  16098. * @tparam Args Types of arguments of a function type.
  16099. */
  16100. template<typename Ret, typename... Args>
  16101. sink(sigh<Ret(Args...)> &) ENTT_NOEXCEPT -> sink<Ret(Args...)>;
  16102. }
  16103. #endif
  16104. namespace entt {
  16105. /**
  16106. * @brief Basic dispatcher implementation.
  16107. *
  16108. * A dispatcher can be used either to trigger an immediate event or to enqueue
  16109. * events to be published all together once per tick.<br/>
  16110. * Listeners are provided in the form of member functions. For each event of
  16111. * type `Event`, listeners are such that they can be invoked with an argument of
  16112. * type `Event &`, no matter what the return type is.
  16113. *
  16114. * The dispatcher creates instances of the `sigh` class internally. Refer to the
  16115. * documentation of the latter for more details.
  16116. */
  16117. class dispatcher {
  16118. struct basic_pool {
  16119. virtual ~basic_pool() = default;
  16120. virtual void publish() = 0;
  16121. virtual void disconnect(void *) = 0;
  16122. virtual void clear() ENTT_NOEXCEPT = 0;
  16123. [[nodiscard]] virtual id_type type_id() const ENTT_NOEXCEPT = 0;
  16124. };
  16125. template<typename Event>
  16126. struct pool_handler final: basic_pool {
  16127. using signal_type = sigh<void(Event &)>;
  16128. using sink_type = typename signal_type::sink_type;
  16129. void publish() override {
  16130. const auto length = events.size();
  16131. for(std::size_t pos{}; pos < length; ++pos) {
  16132. signal.publish(events[pos]);
  16133. }
  16134. events.erase(events.cbegin(), events.cbegin()+length);
  16135. }
  16136. void disconnect(void *instance) override {
  16137. sink().disconnect(instance);
  16138. }
  16139. void clear() ENTT_NOEXCEPT override {
  16140. events.clear();
  16141. }
  16142. [[nodiscard]] sink_type sink() ENTT_NOEXCEPT {
  16143. return entt::sink{signal};
  16144. }
  16145. template<typename... Args>
  16146. void trigger(Args &&... args) {
  16147. Event instance{std::forward<Args>(args)...};
  16148. signal.publish(instance);
  16149. }
  16150. template<typename... Args>
  16151. void enqueue(Args &&... args) {
  16152. if constexpr(std::is_aggregate_v<Event>) {
  16153. events.push_back(Event{std::forward<Args>(args)...});
  16154. } else {
  16155. events.emplace_back(std::forward<Args>(args)...);
  16156. }
  16157. }
  16158. [[nodiscard]] id_type type_id() const ENTT_NOEXCEPT override {
  16159. return type_info<Event>::id();
  16160. }
  16161. private:
  16162. signal_type signal{};
  16163. std::vector<Event> events;
  16164. };
  16165. template<typename Event>
  16166. [[nodiscard]] pool_handler<Event> & assure() {
  16167. static_assert(std::is_same_v<Event, std::decay_t<Event>>, "Invalid event type");
  16168. if constexpr(ENTT_FAST_PATH(has_type_index_v<Event>)) {
  16169. const auto index = type_index<Event>::value();
  16170. if(!(index < pools.size())) {
  16171. pools.resize(index+1u);
  16172. }
  16173. if(!pools[index]) {
  16174. pools[index].reset(new pool_handler<Event>{});
  16175. }
  16176. return static_cast<pool_handler<Event> &>(*pools[index]);
  16177. } else {
  16178. auto it = std::find_if(pools.begin(), pools.end(), [id = type_info<Event>::id()](const auto &cpool) { return id == cpool->type_id(); });
  16179. return static_cast<pool_handler<Event> &>(it == pools.cend() ? *pools.emplace_back(new pool_handler<Event>{}) : **it);
  16180. }
  16181. }
  16182. public:
  16183. /**
  16184. * @brief Returns a sink object for the given event.
  16185. *
  16186. * A sink is an opaque object used to connect listeners to events.
  16187. *
  16188. * The function type for a listener is _compatible_ with:
  16189. * @code{.cpp}
  16190. * void(Event &);
  16191. * @endcode
  16192. *
  16193. * The order of invocation of the listeners isn't guaranteed.
  16194. *
  16195. * @sa sink
  16196. *
  16197. * @tparam Event Type of event of which to get the sink.
  16198. * @return A temporary sink object.
  16199. */
  16200. template<typename Event>
  16201. [[nodiscard]] auto sink() {
  16202. return assure<Event>().sink();
  16203. }
  16204. /**
  16205. * @brief Triggers an immediate event of the given type.
  16206. *
  16207. * All the listeners registered for the given type are immediately notified.
  16208. * The event is discarded after the execution.
  16209. *
  16210. * @tparam Event Type of event to trigger.
  16211. * @tparam Args Types of arguments to use to construct the event.
  16212. * @param args Arguments to use to construct the event.
  16213. */
  16214. template<typename Event, typename... Args>
  16215. void trigger(Args &&... args) {
  16216. assure<Event>().trigger(std::forward<Args>(args)...);
  16217. }
  16218. /**
  16219. * @brief Triggers an immediate event of the given type.
  16220. *
  16221. * All the listeners registered for the given type are immediately notified.
  16222. * The event is discarded after the execution.
  16223. *
  16224. * @tparam Event Type of event to trigger.
  16225. * @param event An instance of the given type of event.
  16226. */
  16227. template<typename Event>
  16228. void trigger(Event &&event) {
  16229. assure<std::decay_t<Event>>().trigger(std::forward<Event>(event));
  16230. }
  16231. /**
  16232. * @brief Enqueues an event of the given type.
  16233. *
  16234. * An event of the given type is queued. No listener is invoked. Use the
  16235. * `update` member function to notify listeners when ready.
  16236. *
  16237. * @tparam Event Type of event to enqueue.
  16238. * @tparam Args Types of arguments to use to construct the event.
  16239. * @param args Arguments to use to construct the event.
  16240. */
  16241. template<typename Event, typename... Args>
  16242. void enqueue(Args &&... args) {
  16243. assure<Event>().enqueue(std::forward<Args>(args)...);
  16244. }
  16245. /**
  16246. * @brief Enqueues an event of the given type.
  16247. *
  16248. * An event of the given type is queued. No listener is invoked. Use the
  16249. * `update` member function to notify listeners when ready.
  16250. *
  16251. * @tparam Event Type of event to enqueue.
  16252. * @param event An instance of the given type of event.
  16253. */
  16254. template<typename Event>
  16255. void enqueue(Event &&event) {
  16256. assure<std::decay_t<Event>>().enqueue(std::forward<Event>(event));
  16257. }
  16258. /**
  16259. * @brief Utility function to disconnect everything related to a given value
  16260. * or instance from a dispatcher.
  16261. * @tparam Type Type of class or type of payload.
  16262. * @param value_or_instance A valid object that fits the purpose.
  16263. */
  16264. template<typename Type>
  16265. void disconnect(Type &value_or_instance) {
  16266. disconnect(&value_or_instance);
  16267. }
  16268. /**
  16269. * @brief Utility function to disconnect everything related to a given value
  16270. * or instance from a dispatcher.
  16271. * @tparam Type Type of class or type of payload.
  16272. * @param value_or_instance A valid object that fits the purpose.
  16273. */
  16274. template<typename Type>
  16275. void disconnect(Type *value_or_instance) {
  16276. for(auto &&cpool: pools) {
  16277. if(cpool) {
  16278. cpool->disconnect(value_or_instance);
  16279. }
  16280. }
  16281. }
  16282. /**
  16283. * @brief Discards all the events queued so far.
  16284. *
  16285. * If no types are provided, the dispatcher will clear all the existing
  16286. * pools.
  16287. *
  16288. * @tparam Event Type of events to discard.
  16289. */
  16290. template<typename... Event>
  16291. void clear() {
  16292. if constexpr(sizeof...(Event) == 0) {
  16293. for(auto &&cpool: pools) {
  16294. if(cpool) {
  16295. cpool->clear();
  16296. }
  16297. }
  16298. } else {
  16299. (assure<Event>().clear(), ...);
  16300. }
  16301. }
  16302. /**
  16303. * @brief Delivers all the pending events of the given type.
  16304. *
  16305. * This method is blocking and it doesn't return until all the events are
  16306. * delivered to the registered listeners. It's responsibility of the users
  16307. * to reduce at a minimum the time spent in the bodies of the listeners.
  16308. *
  16309. * @tparam Event Type of events to send.
  16310. */
  16311. template<typename Event>
  16312. void update() {
  16313. assure<Event>().publish();
  16314. }
  16315. /**
  16316. * @brief Delivers all the pending events.
  16317. *
  16318. * This method is blocking and it doesn't return until all the events are
  16319. * delivered to the registered listeners. It's responsibility of the users
  16320. * to reduce at a minimum the time spent in the bodies of the listeners.
  16321. */
  16322. void update() const {
  16323. for(auto pos = pools.size(); pos; --pos) {
  16324. if(auto &&cpool = pools[pos-1]; cpool) {
  16325. cpool->publish();
  16326. }
  16327. }
  16328. }
  16329. private:
  16330. std::vector<std::unique_ptr<basic_pool>> pools;
  16331. };
  16332. }
  16333. #endif
  16334. // #include "signal/emitter.hpp"
  16335. #ifndef ENTT_SIGNAL_EMITTER_HPP
  16336. #define ENTT_SIGNAL_EMITTER_HPP
  16337. #include <algorithm>
  16338. #include <functional>
  16339. #include <iterator>
  16340. #include <list>
  16341. #include <memory>
  16342. #include <type_traits>
  16343. #include <utility>
  16344. #include <vector>
  16345. // #include "../config/config.h"
  16346. // #include "../core/fwd.hpp"
  16347. // #include "../core/type_info.hpp"
  16348. namespace entt {
  16349. /**
  16350. * @brief General purpose event emitter.
  16351. *
  16352. * The emitter class template follows the CRTP idiom. To create a custom emitter
  16353. * type, derived classes must inherit directly from the base class as:
  16354. *
  16355. * @code{.cpp}
  16356. * struct my_emitter: emitter<my_emitter> {
  16357. * // ...
  16358. * }
  16359. * @endcode
  16360. *
  16361. * Pools for the type of events are created internally on the fly. It's not
  16362. * required to specify in advance the full list of accepted types.<br/>
  16363. * Moreover, whenever an event is published, an emitter provides the listeners
  16364. * with a reference to itself along with a reference to the event. Therefore
  16365. * listeners have an handy way to work with it without incurring in the need of
  16366. * capturing a reference to the emitter.
  16367. *
  16368. * @tparam Derived Actual type of emitter that extends the class template.
  16369. */
  16370. template<typename Derived>
  16371. class emitter {
  16372. struct basic_pool {
  16373. virtual ~basic_pool() = default;
  16374. virtual bool empty() const ENTT_NOEXCEPT = 0;
  16375. virtual void clear() ENTT_NOEXCEPT = 0;
  16376. virtual id_type type_id() const ENTT_NOEXCEPT = 0;
  16377. };
  16378. template<typename Event>
  16379. struct pool_handler final: basic_pool {
  16380. using listener_type = std::function<void(Event &, Derived &)>;
  16381. using element_type = std::pair<bool, listener_type>;
  16382. using container_type = std::list<element_type>;
  16383. using connection_type = typename container_type::iterator;
  16384. [[nodiscard]] bool empty() const ENTT_NOEXCEPT override {
  16385. auto pred = [](auto &&element) { return element.first; };
  16386. return std::all_of(once_list.cbegin(), once_list.cend(), pred) &&
  16387. std::all_of(on_list.cbegin(), on_list.cend(), pred);
  16388. }
  16389. void clear() ENTT_NOEXCEPT override {
  16390. if(publishing) {
  16391. for(auto &&element: once_list) {
  16392. element.first = true;
  16393. }
  16394. for(auto &&element: on_list) {
  16395. element.first = true;
  16396. }
  16397. } else {
  16398. once_list.clear();
  16399. on_list.clear();
  16400. }
  16401. }
  16402. connection_type once(listener_type listener) {
  16403. return once_list.emplace(once_list.cend(), false, std::move(listener));
  16404. }
  16405. connection_type on(listener_type listener) {
  16406. return on_list.emplace(on_list.cend(), false, std::move(listener));
  16407. }
  16408. void erase(connection_type conn) {
  16409. conn->first = true;
  16410. if(!publishing) {
  16411. auto pred = [](auto &&element) { return element.first; };
  16412. once_list.remove_if(pred);
  16413. on_list.remove_if(pred);
  16414. }
  16415. }
  16416. void publish(Event &event, Derived &ref) {
  16417. container_type swap_list;
  16418. once_list.swap(swap_list);
  16419. publishing = true;
  16420. for(auto &&element: on_list) {
  16421. element.first ? void() : element.second(event, ref);
  16422. }
  16423. for(auto &&element: swap_list) {
  16424. element.first ? void() : element.second(event, ref);
  16425. }
  16426. publishing = false;
  16427. on_list.remove_if([](auto &&element) { return element.first; });
  16428. }
  16429. [[nodiscard]] id_type type_id() const ENTT_NOEXCEPT override {
  16430. return type_info<Event>::id();
  16431. }
  16432. private:
  16433. bool publishing{false};
  16434. container_type once_list{};
  16435. container_type on_list{};
  16436. };
  16437. template<typename Event>
  16438. [[nodiscard]] const pool_handler<Event> & assure() const {
  16439. static_assert(std::is_same_v<Event, std::decay_t<Event>>, "Invalid event type");
  16440. if constexpr(ENTT_FAST_PATH(has_type_index_v<Event>)) {
  16441. const auto index = type_index<Event>::value();
  16442. if(!(index < pools.size())) {
  16443. pools.resize(index+1u);
  16444. }
  16445. if(!pools[index]) {
  16446. pools[index].reset(new pool_handler<Event>{});
  16447. }
  16448. return static_cast<pool_handler<Event> &>(*pools[index]);
  16449. } else {
  16450. auto it = std::find_if(pools.begin(), pools.end(), [id = type_info<Event>::id()](const auto &cpool) { return id == cpool->type_id(); });
  16451. return static_cast<pool_handler<Event> &>(it == pools.cend() ? *pools.emplace_back(new pool_handler<Event>{}) : **it);
  16452. }
  16453. }
  16454. template<typename Event>
  16455. [[nodiscard]] pool_handler<Event> & assure() {
  16456. return const_cast<pool_handler<Event> &>(std::as_const(*this).template assure<Event>());
  16457. }
  16458. public:
  16459. /** @brief Type of listeners accepted for the given event. */
  16460. template<typename Event>
  16461. using listener = typename pool_handler<Event>::listener_type;
  16462. /**
  16463. * @brief Generic connection type for events.
  16464. *
  16465. * Type of the connection object returned by the event emitter whenever a
  16466. * listener for the given type is registered.<br/>
  16467. * It can be used to break connections still in use.
  16468. *
  16469. * @tparam Event Type of event for which the connection is created.
  16470. */
  16471. template<typename Event>
  16472. struct connection: private pool_handler<Event>::connection_type {
  16473. /** @brief Event emitters are friend classes of connections. */
  16474. friend class emitter;
  16475. /*! @brief Default constructor. */
  16476. connection() = default;
  16477. /**
  16478. * @brief Creates a connection that wraps its underlying instance.
  16479. * @param conn A connection object to wrap.
  16480. */
  16481. connection(typename pool_handler<Event>::connection_type conn)
  16482. : pool_handler<Event>::connection_type{std::move(conn)}
  16483. {}
  16484. };
  16485. /*! @brief Default constructor. */
  16486. emitter() = default;
  16487. /*! @brief Default destructor. */
  16488. virtual ~emitter() {
  16489. static_assert(std::is_base_of_v<emitter<Derived>, Derived>, "Incorrect use of the class template");
  16490. }
  16491. /*! @brief Default move constructor. */
  16492. emitter(emitter &&) = default;
  16493. /*! @brief Default move assignment operator. @return This emitter. */
  16494. emitter & operator=(emitter &&) = default;
  16495. /**
  16496. * @brief Emits the given event.
  16497. *
  16498. * All the listeners registered for the specific event type are invoked with
  16499. * the given event. The event type must either have a proper constructor for
  16500. * the arguments provided or be an aggregate type.
  16501. *
  16502. * @tparam Event Type of event to publish.
  16503. * @tparam Args Types of arguments to use to construct the event.
  16504. * @param args Parameters to use to initialize the event.
  16505. */
  16506. template<typename Event, typename... Args>
  16507. void publish(Args &&... args) {
  16508. Event instance{std::forward<Args>(args)...};
  16509. assure<Event>().publish(instance, *static_cast<Derived *>(this));
  16510. }
  16511. /**
  16512. * @brief Registers a long-lived listener with the event emitter.
  16513. *
  16514. * This method can be used to register a listener designed to be invoked
  16515. * more than once for the given event type.<br/>
  16516. * The connection returned by the method can be freely discarded. It's meant
  16517. * to be used later to disconnect the listener if required.
  16518. *
  16519. * The listener is as a callable object that can be moved and the type of
  16520. * which is _compatible_ with `void(Event &, Derived &)`.
  16521. *
  16522. * @note
  16523. * Whenever an event is emitted, the emitter provides the listener with a
  16524. * reference to the derived class. Listeners don't have to capture those
  16525. * instances for later uses.
  16526. *
  16527. * @tparam Event Type of event to which to connect the listener.
  16528. * @param instance The listener to register.
  16529. * @return Connection object that can be used to disconnect the listener.
  16530. */
  16531. template<typename Event>
  16532. connection<Event> on(listener<Event> instance) {
  16533. return assure<Event>().on(std::move(instance));
  16534. }
  16535. /**
  16536. * @brief Registers a short-lived listener with the event emitter.
  16537. *
  16538. * This method can be used to register a listener designed to be invoked
  16539. * only once for the given event type.<br/>
  16540. * The connection returned by the method can be freely discarded. It's meant
  16541. * to be used later to disconnect the listener if required.
  16542. *
  16543. * The listener is as a callable object that can be moved and the type of
  16544. * which is _compatible_ with `void(Event &, Derived &)`.
  16545. *
  16546. * @note
  16547. * Whenever an event is emitted, the emitter provides the listener with a
  16548. * reference to the derived class. Listeners don't have to capture those
  16549. * instances for later uses.
  16550. *
  16551. * @tparam Event Type of event to which to connect the listener.
  16552. * @param instance The listener to register.
  16553. * @return Connection object that can be used to disconnect the listener.
  16554. */
  16555. template<typename Event>
  16556. connection<Event> once(listener<Event> instance) {
  16557. return assure<Event>().once(std::move(instance));
  16558. }
  16559. /**
  16560. * @brief Disconnects a listener from the event emitter.
  16561. *
  16562. * Do not use twice the same connection to disconnect a listener, it results
  16563. * in undefined behavior. Once used, discard the connection object.
  16564. *
  16565. * @tparam Event Type of event of the connection.
  16566. * @param conn A valid connection.
  16567. */
  16568. template<typename Event>
  16569. void erase(connection<Event> conn) {
  16570. assure<Event>().erase(std::move(conn));
  16571. }
  16572. /**
  16573. * @brief Disconnects all the listeners for the given event type.
  16574. *
  16575. * All the connections previously returned for the given event are
  16576. * invalidated. Using them results in undefined behavior.
  16577. *
  16578. * @tparam Event Type of event to reset.
  16579. */
  16580. template<typename Event>
  16581. void clear() {
  16582. assure<Event>().clear();
  16583. }
  16584. /**
  16585. * @brief Disconnects all the listeners.
  16586. *
  16587. * All the connections previously returned are invalidated. Using them
  16588. * results in undefined behavior.
  16589. */
  16590. void clear() ENTT_NOEXCEPT {
  16591. for(auto &&cpool: pools) {
  16592. if(cpool) {
  16593. cpool->clear();
  16594. }
  16595. }
  16596. }
  16597. /**
  16598. * @brief Checks if there are listeners registered for the specific event.
  16599. * @tparam Event Type of event to test.
  16600. * @return True if there are no listeners registered, false otherwise.
  16601. */
  16602. template<typename Event>
  16603. [[nodiscard]] bool empty() const {
  16604. return assure<Event>().empty();
  16605. }
  16606. /**
  16607. * @brief Checks if there are listeners registered with the event emitter.
  16608. * @return True if there are no listeners registered, false otherwise.
  16609. */
  16610. [[nodiscard]] bool empty() const ENTT_NOEXCEPT {
  16611. return std::all_of(pools.cbegin(), pools.cend(), [](auto &&cpool) {
  16612. return !cpool || cpool->empty();
  16613. });
  16614. }
  16615. private:
  16616. mutable std::vector<std::unique_ptr<basic_pool>> pools{};
  16617. };
  16618. }
  16619. #endif
  16620. // #include "signal/sigh.hpp"