GPBDictionary.m 376 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #import "GPBDictionary_PackagePrivate.h"
  31. #import "GPBCodedInputStream_PackagePrivate.h"
  32. #import "GPBCodedOutputStream_PackagePrivate.h"
  33. #import "GPBDescriptor_PackagePrivate.h"
  34. #import "GPBMessage_PackagePrivate.h"
  35. #import "GPBUtilities_PackagePrivate.h"
  36. // ------------------------------ NOTE ------------------------------
  37. // At the moment, this is all using NSNumbers in NSDictionaries under
  38. // the hood, but it is all hidden so we can come back and optimize
  39. // with direct CFDictionary usage later. The reason that wasn't
  40. // done yet is needing to support 32bit iOS builds. Otherwise
  41. // it would be pretty simple to store all this data in CFDictionaries
  42. // directly.
  43. // ------------------------------------------------------------------
  44. // Direct access is use for speed, to avoid even internally declaring things
  45. // read/write, etc. The warning is enabled in the project to ensure code calling
  46. // protos can turn on -Wdirect-ivar-access without issues.
  47. #pragma clang diagnostic push
  48. #pragma clang diagnostic ignored "-Wdirect-ivar-access"
  49. // Used to include code only visible to specific versions of the static
  50. // analyzer. Useful for wrapping code that only exists to silence the analyzer.
  51. // Determine the values you want to use for BEGIN_APPLE_BUILD_VERSION,
  52. // END_APPLE_BUILD_VERSION using:
  53. // xcrun clang -dM -E -x c /dev/null | grep __apple_build_version__
  54. // Example usage:
  55. // #if GPB_STATIC_ANALYZER_ONLY(5621, 5623) ... #endif
  56. #define GPB_STATIC_ANALYZER_ONLY(BEGIN_APPLE_BUILD_VERSION, END_APPLE_BUILD_VERSION) \
  57. (defined(__clang_analyzer__) && \
  58. (__apple_build_version__ >= BEGIN_APPLE_BUILD_VERSION && \
  59. __apple_build_version__ <= END_APPLE_BUILD_VERSION))
  60. enum {
  61. kMapKeyFieldNumber = 1,
  62. kMapValueFieldNumber = 2,
  63. };
  64. static BOOL DictDefault_IsValidValue(int32_t value) {
  65. // Anything but the bad value marker is allowed.
  66. return (value != kGPBUnrecognizedEnumeratorValue);
  67. }
  68. //%PDDM-DEFINE SERIALIZE_SUPPORT_2_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2)
  69. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  70. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  71. //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
  72. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  73. //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
  74. //% } else {
  75. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  76. //% return 0;
  77. //% }
  78. //%}
  79. //%
  80. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  81. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  82. //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
  83. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  84. //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
  85. //% } else {
  86. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  87. //% }
  88. //%}
  89. //%
  90. //%PDDM-DEFINE SERIALIZE_SUPPORT_3_TYPE(VALUE_NAME, VALUE_TYPE, GPBDATATYPE_NAME1, GPBDATATYPE_NAME2, GPBDATATYPE_NAME3)
  91. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  92. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  93. //% return GPBCompute##GPBDATATYPE_NAME1##Size(fieldNum, value);
  94. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  95. //% return GPBCompute##GPBDATATYPE_NAME2##Size(fieldNum, value);
  96. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
  97. //% return GPBCompute##GPBDATATYPE_NAME3##Size(fieldNum, value);
  98. //% } else {
  99. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  100. //% return 0;
  101. //% }
  102. //%}
  103. //%
  104. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE value, uint32_t fieldNum, GPBDataType dataType) {
  105. //% if (dataType == GPBDataType##GPBDATATYPE_NAME1) {
  106. //% [stream write##GPBDATATYPE_NAME1##:fieldNum value:value];
  107. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME2) {
  108. //% [stream write##GPBDATATYPE_NAME2##:fieldNum value:value];
  109. //% } else if (dataType == GPBDataType##GPBDATATYPE_NAME3) {
  110. //% [stream write##GPBDATATYPE_NAME3##:fieldNum value:value];
  111. //% } else {
  112. //% NSCAssert(NO, @"Unexpected type %d", dataType);
  113. //% }
  114. //%}
  115. //%
  116. //%PDDM-DEFINE SIMPLE_SERIALIZE_SUPPORT(VALUE_NAME, VALUE_TYPE, VisP)
  117. //%static size_t ComputeDict##VALUE_NAME##FieldSize(VALUE_TYPE VisP##value, uint32_t fieldNum, GPBDataType dataType) {
  118. //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
  119. //% #pragma unused(dataType) // For when asserts are off in release.
  120. //% return GPBCompute##VALUE_NAME##Size(fieldNum, value);
  121. //%}
  122. //%
  123. //%static void WriteDict##VALUE_NAME##Field(GPBCodedOutputStream *stream, VALUE_TYPE VisP##value, uint32_t fieldNum, GPBDataType dataType) {
  124. //% NSCAssert(dataType == GPBDataType##VALUE_NAME, @"bad type: %d", dataType);
  125. //% #pragma unused(dataType) // For when asserts are off in release.
  126. //% [stream write##VALUE_NAME##:fieldNum value:value];
  127. //%}
  128. //%
  129. //%PDDM-DEFINE SERIALIZE_SUPPORT_HELPERS()
  130. //%SERIALIZE_SUPPORT_3_TYPE(Int32, int32_t, Int32, SInt32, SFixed32)
  131. //%SERIALIZE_SUPPORT_2_TYPE(UInt32, uint32_t, UInt32, Fixed32)
  132. //%SERIALIZE_SUPPORT_3_TYPE(Int64, int64_t, Int64, SInt64, SFixed64)
  133. //%SERIALIZE_SUPPORT_2_TYPE(UInt64, uint64_t, UInt64, Fixed64)
  134. //%SIMPLE_SERIALIZE_SUPPORT(Bool, BOOL, )
  135. //%SIMPLE_SERIALIZE_SUPPORT(Enum, int32_t, )
  136. //%SIMPLE_SERIALIZE_SUPPORT(Float, float, )
  137. //%SIMPLE_SERIALIZE_SUPPORT(Double, double, )
  138. //%SIMPLE_SERIALIZE_SUPPORT(String, NSString, *)
  139. //%SERIALIZE_SUPPORT_3_TYPE(Object, id, Message, String, Bytes)
  140. //%PDDM-EXPAND SERIALIZE_SUPPORT_HELPERS()
  141. // This block of code is generated, do not edit it directly.
  142. // clang-format off
  143. static size_t ComputeDictInt32FieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  144. if (dataType == GPBDataTypeInt32) {
  145. return GPBComputeInt32Size(fieldNum, value);
  146. } else if (dataType == GPBDataTypeSInt32) {
  147. return GPBComputeSInt32Size(fieldNum, value);
  148. } else if (dataType == GPBDataTypeSFixed32) {
  149. return GPBComputeSFixed32Size(fieldNum, value);
  150. } else {
  151. NSCAssert(NO, @"Unexpected type %d", dataType);
  152. return 0;
  153. }
  154. }
  155. static void WriteDictInt32Field(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  156. if (dataType == GPBDataTypeInt32) {
  157. [stream writeInt32:fieldNum value:value];
  158. } else if (dataType == GPBDataTypeSInt32) {
  159. [stream writeSInt32:fieldNum value:value];
  160. } else if (dataType == GPBDataTypeSFixed32) {
  161. [stream writeSFixed32:fieldNum value:value];
  162. } else {
  163. NSCAssert(NO, @"Unexpected type %d", dataType);
  164. }
  165. }
  166. static size_t ComputeDictUInt32FieldSize(uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
  167. if (dataType == GPBDataTypeUInt32) {
  168. return GPBComputeUInt32Size(fieldNum, value);
  169. } else if (dataType == GPBDataTypeFixed32) {
  170. return GPBComputeFixed32Size(fieldNum, value);
  171. } else {
  172. NSCAssert(NO, @"Unexpected type %d", dataType);
  173. return 0;
  174. }
  175. }
  176. static void WriteDictUInt32Field(GPBCodedOutputStream *stream, uint32_t value, uint32_t fieldNum, GPBDataType dataType) {
  177. if (dataType == GPBDataTypeUInt32) {
  178. [stream writeUInt32:fieldNum value:value];
  179. } else if (dataType == GPBDataTypeFixed32) {
  180. [stream writeFixed32:fieldNum value:value];
  181. } else {
  182. NSCAssert(NO, @"Unexpected type %d", dataType);
  183. }
  184. }
  185. static size_t ComputeDictInt64FieldSize(int64_t value, uint32_t fieldNum, GPBDataType dataType) {
  186. if (dataType == GPBDataTypeInt64) {
  187. return GPBComputeInt64Size(fieldNum, value);
  188. } else if (dataType == GPBDataTypeSInt64) {
  189. return GPBComputeSInt64Size(fieldNum, value);
  190. } else if (dataType == GPBDataTypeSFixed64) {
  191. return GPBComputeSFixed64Size(fieldNum, value);
  192. } else {
  193. NSCAssert(NO, @"Unexpected type %d", dataType);
  194. return 0;
  195. }
  196. }
  197. static void WriteDictInt64Field(GPBCodedOutputStream *stream, int64_t value, uint32_t fieldNum, GPBDataType dataType) {
  198. if (dataType == GPBDataTypeInt64) {
  199. [stream writeInt64:fieldNum value:value];
  200. } else if (dataType == GPBDataTypeSInt64) {
  201. [stream writeSInt64:fieldNum value:value];
  202. } else if (dataType == GPBDataTypeSFixed64) {
  203. [stream writeSFixed64:fieldNum value:value];
  204. } else {
  205. NSCAssert(NO, @"Unexpected type %d", dataType);
  206. }
  207. }
  208. static size_t ComputeDictUInt64FieldSize(uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
  209. if (dataType == GPBDataTypeUInt64) {
  210. return GPBComputeUInt64Size(fieldNum, value);
  211. } else if (dataType == GPBDataTypeFixed64) {
  212. return GPBComputeFixed64Size(fieldNum, value);
  213. } else {
  214. NSCAssert(NO, @"Unexpected type %d", dataType);
  215. return 0;
  216. }
  217. }
  218. static void WriteDictUInt64Field(GPBCodedOutputStream *stream, uint64_t value, uint32_t fieldNum, GPBDataType dataType) {
  219. if (dataType == GPBDataTypeUInt64) {
  220. [stream writeUInt64:fieldNum value:value];
  221. } else if (dataType == GPBDataTypeFixed64) {
  222. [stream writeFixed64:fieldNum value:value];
  223. } else {
  224. NSCAssert(NO, @"Unexpected type %d", dataType);
  225. }
  226. }
  227. static size_t ComputeDictBoolFieldSize(BOOL value, uint32_t fieldNum, GPBDataType dataType) {
  228. NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
  229. #pragma unused(dataType) // For when asserts are off in release.
  230. return GPBComputeBoolSize(fieldNum, value);
  231. }
  232. static void WriteDictBoolField(GPBCodedOutputStream *stream, BOOL value, uint32_t fieldNum, GPBDataType dataType) {
  233. NSCAssert(dataType == GPBDataTypeBool, @"bad type: %d", dataType);
  234. #pragma unused(dataType) // For when asserts are off in release.
  235. [stream writeBool:fieldNum value:value];
  236. }
  237. static size_t ComputeDictEnumFieldSize(int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  238. NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
  239. #pragma unused(dataType) // For when asserts are off in release.
  240. return GPBComputeEnumSize(fieldNum, value);
  241. }
  242. static void WriteDictEnumField(GPBCodedOutputStream *stream, int32_t value, uint32_t fieldNum, GPBDataType dataType) {
  243. NSCAssert(dataType == GPBDataTypeEnum, @"bad type: %d", dataType);
  244. #pragma unused(dataType) // For when asserts are off in release.
  245. [stream writeEnum:fieldNum value:value];
  246. }
  247. static size_t ComputeDictFloatFieldSize(float value, uint32_t fieldNum, GPBDataType dataType) {
  248. NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
  249. #pragma unused(dataType) // For when asserts are off in release.
  250. return GPBComputeFloatSize(fieldNum, value);
  251. }
  252. static void WriteDictFloatField(GPBCodedOutputStream *stream, float value, uint32_t fieldNum, GPBDataType dataType) {
  253. NSCAssert(dataType == GPBDataTypeFloat, @"bad type: %d", dataType);
  254. #pragma unused(dataType) // For when asserts are off in release.
  255. [stream writeFloat:fieldNum value:value];
  256. }
  257. static size_t ComputeDictDoubleFieldSize(double value, uint32_t fieldNum, GPBDataType dataType) {
  258. NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
  259. #pragma unused(dataType) // For when asserts are off in release.
  260. return GPBComputeDoubleSize(fieldNum, value);
  261. }
  262. static void WriteDictDoubleField(GPBCodedOutputStream *stream, double value, uint32_t fieldNum, GPBDataType dataType) {
  263. NSCAssert(dataType == GPBDataTypeDouble, @"bad type: %d", dataType);
  264. #pragma unused(dataType) // For when asserts are off in release.
  265. [stream writeDouble:fieldNum value:value];
  266. }
  267. static size_t ComputeDictStringFieldSize(NSString *value, uint32_t fieldNum, GPBDataType dataType) {
  268. NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
  269. #pragma unused(dataType) // For when asserts are off in release.
  270. return GPBComputeStringSize(fieldNum, value);
  271. }
  272. static void WriteDictStringField(GPBCodedOutputStream *stream, NSString *value, uint32_t fieldNum, GPBDataType dataType) {
  273. NSCAssert(dataType == GPBDataTypeString, @"bad type: %d", dataType);
  274. #pragma unused(dataType) // For when asserts are off in release.
  275. [stream writeString:fieldNum value:value];
  276. }
  277. static size_t ComputeDictObjectFieldSize(id value, uint32_t fieldNum, GPBDataType dataType) {
  278. if (dataType == GPBDataTypeMessage) {
  279. return GPBComputeMessageSize(fieldNum, value);
  280. } else if (dataType == GPBDataTypeString) {
  281. return GPBComputeStringSize(fieldNum, value);
  282. } else if (dataType == GPBDataTypeBytes) {
  283. return GPBComputeBytesSize(fieldNum, value);
  284. } else {
  285. NSCAssert(NO, @"Unexpected type %d", dataType);
  286. return 0;
  287. }
  288. }
  289. static void WriteDictObjectField(GPBCodedOutputStream *stream, id value, uint32_t fieldNum, GPBDataType dataType) {
  290. if (dataType == GPBDataTypeMessage) {
  291. [stream writeMessage:fieldNum value:value];
  292. } else if (dataType == GPBDataTypeString) {
  293. [stream writeString:fieldNum value:value];
  294. } else if (dataType == GPBDataTypeBytes) {
  295. [stream writeBytes:fieldNum value:value];
  296. } else {
  297. NSCAssert(NO, @"Unexpected type %d", dataType);
  298. }
  299. }
  300. // clang-format on
  301. //%PDDM-EXPAND-END SERIALIZE_SUPPORT_HELPERS()
  302. size_t GPBDictionaryComputeSizeInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  303. GPBDataType mapValueType = GPBGetFieldDataType(field);
  304. size_t result = 0;
  305. NSString *key;
  306. NSEnumerator *keys = [dict keyEnumerator];
  307. while ((key = [keys nextObject])) {
  308. id obj = dict[key];
  309. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  310. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  311. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  312. }
  313. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  314. result += tagSize * dict.count;
  315. return result;
  316. }
  317. void GPBDictionaryWriteToStreamInternalHelper(GPBCodedOutputStream *outputStream,
  318. NSDictionary *dict,
  319. GPBFieldDescriptor *field) {
  320. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  321. GPBDataType mapValueType = GPBGetFieldDataType(field);
  322. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  323. NSString *key;
  324. NSEnumerator *keys = [dict keyEnumerator];
  325. while ((key = [keys nextObject])) {
  326. id obj = dict[key];
  327. // Write the tag.
  328. [outputStream writeInt32NoTag:tag];
  329. // Write the size of the message.
  330. size_t msgSize = GPBComputeStringSize(kMapKeyFieldNumber, key);
  331. msgSize += ComputeDictObjectFieldSize(obj, kMapValueFieldNumber, mapValueType);
  332. // Write the size and fields.
  333. [outputStream writeInt32NoTag:(int32_t)msgSize];
  334. [outputStream writeString:kMapKeyFieldNumber value:key];
  335. WriteDictObjectField(outputStream, obj, kMapValueFieldNumber, mapValueType);
  336. }
  337. }
  338. BOOL GPBDictionaryIsInitializedInternalHelper(NSDictionary *dict, GPBFieldDescriptor *field) {
  339. NSCAssert(field.mapKeyDataType == GPBDataTypeString, @"Unexpected key type");
  340. NSCAssert(GPBGetFieldDataType(field) == GPBDataTypeMessage, @"Unexpected value type");
  341. #pragma unused(field) // For when asserts are off in release.
  342. GPBMessage *msg;
  343. NSEnumerator *objects = [dict objectEnumerator];
  344. while ((msg = [objects nextObject])) {
  345. if (!msg.initialized) {
  346. return NO;
  347. }
  348. }
  349. return YES;
  350. }
  351. // Note: if the type is an object, it the retain pass back to the caller.
  352. static void ReadValue(GPBCodedInputStream *stream,
  353. GPBGenericValue *valueToFill,
  354. GPBDataType type,
  355. GPBExtensionRegistry *registry,
  356. GPBFieldDescriptor *field) {
  357. switch (type) {
  358. case GPBDataTypeBool:
  359. valueToFill->valueBool = GPBCodedInputStreamReadBool(&stream->state_);
  360. break;
  361. case GPBDataTypeFixed32:
  362. valueToFill->valueUInt32 = GPBCodedInputStreamReadFixed32(&stream->state_);
  363. break;
  364. case GPBDataTypeSFixed32:
  365. valueToFill->valueInt32 = GPBCodedInputStreamReadSFixed32(&stream->state_);
  366. break;
  367. case GPBDataTypeFloat:
  368. valueToFill->valueFloat = GPBCodedInputStreamReadFloat(&stream->state_);
  369. break;
  370. case GPBDataTypeFixed64:
  371. valueToFill->valueUInt64 = GPBCodedInputStreamReadFixed64(&stream->state_);
  372. break;
  373. case GPBDataTypeSFixed64:
  374. valueToFill->valueInt64 = GPBCodedInputStreamReadSFixed64(&stream->state_);
  375. break;
  376. case GPBDataTypeDouble:
  377. valueToFill->valueDouble = GPBCodedInputStreamReadDouble(&stream->state_);
  378. break;
  379. case GPBDataTypeInt32:
  380. valueToFill->valueInt32 = GPBCodedInputStreamReadInt32(&stream->state_);
  381. break;
  382. case GPBDataTypeInt64:
  383. valueToFill->valueInt64 = GPBCodedInputStreamReadInt64(&stream->state_);
  384. break;
  385. case GPBDataTypeSInt32:
  386. valueToFill->valueInt32 = GPBCodedInputStreamReadSInt32(&stream->state_);
  387. break;
  388. case GPBDataTypeSInt64:
  389. valueToFill->valueInt64 = GPBCodedInputStreamReadSInt64(&stream->state_);
  390. break;
  391. case GPBDataTypeUInt32:
  392. valueToFill->valueUInt32 = GPBCodedInputStreamReadUInt32(&stream->state_);
  393. break;
  394. case GPBDataTypeUInt64:
  395. valueToFill->valueUInt64 = GPBCodedInputStreamReadUInt64(&stream->state_);
  396. break;
  397. case GPBDataTypeBytes:
  398. [valueToFill->valueData release];
  399. valueToFill->valueData = GPBCodedInputStreamReadRetainedBytes(&stream->state_);
  400. break;
  401. case GPBDataTypeString:
  402. [valueToFill->valueString release];
  403. valueToFill->valueString = GPBCodedInputStreamReadRetainedString(&stream->state_);
  404. break;
  405. case GPBDataTypeMessage: {
  406. GPBMessage *message = [[field.msgClass alloc] init];
  407. [stream readMessage:message extensionRegistry:registry];
  408. [valueToFill->valueMessage release];
  409. valueToFill->valueMessage = message;
  410. break;
  411. }
  412. case GPBDataTypeGroup:
  413. NSCAssert(NO, @"Can't happen");
  414. break;
  415. case GPBDataTypeEnum:
  416. valueToFill->valueEnum = GPBCodedInputStreamReadEnum(&stream->state_);
  417. break;
  418. }
  419. }
  420. void GPBDictionaryReadEntry(id mapDictionary,
  421. GPBCodedInputStream *stream,
  422. GPBExtensionRegistry *registry,
  423. GPBFieldDescriptor *field,
  424. GPBMessage *parentMessage) {
  425. GPBDataType keyDataType = field.mapKeyDataType;
  426. GPBDataType valueDataType = GPBGetFieldDataType(field);
  427. GPBGenericValue key;
  428. GPBGenericValue value;
  429. // Zero them (but pick up any enum default for proto2).
  430. key.valueString = value.valueString = nil;
  431. if (valueDataType == GPBDataTypeEnum) {
  432. value = field.defaultValue;
  433. }
  434. GPBCodedInputStreamState *state = &stream->state_;
  435. uint32_t keyTag =
  436. GPBWireFormatMakeTag(kMapKeyFieldNumber, GPBWireFormatForType(keyDataType, NO));
  437. uint32_t valueTag =
  438. GPBWireFormatMakeTag(kMapValueFieldNumber, GPBWireFormatForType(valueDataType, NO));
  439. BOOL hitError = NO;
  440. while (YES) {
  441. uint32_t tag = GPBCodedInputStreamReadTag(state);
  442. if (tag == keyTag) {
  443. ReadValue(stream, &key, keyDataType, registry, field);
  444. } else if (tag == valueTag) {
  445. ReadValue(stream, &value, valueDataType, registry, field);
  446. } else if (tag == 0) {
  447. // zero signals EOF / limit reached
  448. break;
  449. } else { // Unknown
  450. if (![stream skipField:tag]){
  451. hitError = YES;
  452. break;
  453. }
  454. }
  455. }
  456. if (!hitError) {
  457. // Handle the special defaults and/or missing key/value.
  458. if ((keyDataType == GPBDataTypeString) && (key.valueString == nil)) {
  459. key.valueString = [@"" retain];
  460. }
  461. if (GPBDataTypeIsObject(valueDataType) && value.valueString == nil) {
  462. #pragma clang diagnostic push
  463. #pragma clang diagnostic ignored "-Wswitch-enum"
  464. switch (valueDataType) {
  465. case GPBDataTypeString:
  466. value.valueString = [@"" retain];
  467. break;
  468. case GPBDataTypeBytes:
  469. value.valueData = [GPBEmptyNSData() retain];
  470. break;
  471. #if defined(__clang_analyzer__)
  472. case GPBDataTypeGroup:
  473. // Maps can't really have Groups as the value type, but this case is needed
  474. // so the analyzer won't report the possibility of send nil in for the value
  475. // in the NSMutableDictionary case below.
  476. #endif
  477. case GPBDataTypeMessage: {
  478. value.valueMessage = [[field.msgClass alloc] init];
  479. break;
  480. }
  481. default:
  482. // Nothing
  483. break;
  484. }
  485. #pragma clang diagnostic pop
  486. }
  487. if ((keyDataType == GPBDataTypeString) && GPBDataTypeIsObject(valueDataType)) {
  488. #if GPB_STATIC_ANALYZER_ONLY(6020053, 7000181)
  489. // Limited to Xcode 6.4 - 7.2, are known to fail here. The upper end can
  490. // be raised as needed for new Xcodes.
  491. //
  492. // This is only needed on a "shallow" analyze; on a "deep" analyze, the
  493. // existing code path gets this correct. In shallow, the analyzer decides
  494. // GPBDataTypeIsObject(valueDataType) is both false and true on a single
  495. // path through this function, allowing nil to be used for the
  496. // setObject:forKey:.
  497. if (value.valueString == nil) {
  498. value.valueString = [@"" retain];
  499. }
  500. #endif
  501. // mapDictionary is an NSMutableDictionary
  502. [(NSMutableDictionary *)mapDictionary setObject:value.valueString
  503. forKey:key.valueString];
  504. } else {
  505. if (valueDataType == GPBDataTypeEnum) {
  506. if (GPBHasPreservingUnknownEnumSemantics([parentMessage descriptor].file.syntax) ||
  507. [field isValidEnumValue:value.valueEnum]) {
  508. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  509. } else {
  510. NSData *data = [mapDictionary serializedDataForUnknownValue:value.valueEnum
  511. forKey:&key
  512. keyDataType:keyDataType];
  513. [parentMessage addUnknownMapEntry:GPBFieldNumber(field) value:data];
  514. }
  515. } else {
  516. [mapDictionary setGPBGenericValue:&value forGPBGenericValueKey:&key];
  517. }
  518. }
  519. }
  520. if (GPBDataTypeIsObject(keyDataType)) {
  521. [key.valueString release];
  522. }
  523. if (GPBDataTypeIsObject(valueDataType)) {
  524. [value.valueString release];
  525. }
  526. }
  527. //
  528. // Macros for the common basic cases.
  529. //
  530. //%PDDM-DEFINE DICTIONARY_IMPL_FOR_POD_KEY(KEY_NAME, KEY_TYPE)
  531. //%DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, , POD)
  532. //%DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, Object, id)
  533. //%PDDM-DEFINE DICTIONARY_POD_IMPL_FOR_KEY(KEY_NAME, KEY_TYPE, KisP, KHELPER)
  534. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt32, uint32_t, KHELPER)
  535. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int32, int32_t, KHELPER)
  536. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, UInt64, uint64_t, KHELPER)
  537. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Int64, int64_t, KHELPER)
  538. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Bool, BOOL, KHELPER)
  539. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Float, float, KHELPER)
  540. //%DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, Double, double, KHELPER)
  541. //%DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, Enum, int32_t, KHELPER)
  542. //%PDDM-DEFINE DICTIONARY_KEY_TO_POD_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  543. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD, VALUE_NAME, value)
  544. //%PDDM-DEFINE DICTIONARY_POD_KEY_TO_OBJECT_IMPL(KEY_NAME, KEY_TYPE, VALUE_NAME, VALUE_TYPE)
  545. //%DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, , VALUE_NAME, VALUE_TYPE, POD, OBJECT, Object, object)
  546. //%PDDM-DEFINE DICTIONARY_COMMON_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR)
  547. //%#pragma mark - KEY_NAME -> VALUE_NAME
  548. //%
  549. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  550. //% @package
  551. //% NSMutableDictionary *_dictionary;
  552. //%}
  553. //%
  554. //%- (instancetype)init {
  555. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  556. //%}
  557. //%
  558. //%- (instancetype)initWith##VNAME##s:(const VALUE_TYPE [])##VNAME_VAR##s
  559. //% ##VNAME$S## forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  560. //% ##VNAME$S## count:(NSUInteger)count {
  561. //% self = [super init];
  562. //% if (self) {
  563. //% _dictionary = [[NSMutableDictionary alloc] init];
  564. //% if (count && VNAME_VAR##s && keys) {
  565. //% for (NSUInteger i = 0; i < count; ++i) {
  566. //%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME_VAR##s[i], ______)##DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______) [_dictionary setObject:WRAPPED##VHELPER(VNAME_VAR##s[i]) forKey:WRAPPED##KHELPER(keys[i])];
  567. //% }
  568. //% }
  569. //% }
  570. //% return self;
  571. //%}
  572. //%
  573. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  574. //% self = [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  575. //% if (self) {
  576. //% if (dictionary) {
  577. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  578. //% }
  579. //% }
  580. //% return self;
  581. //%}
  582. //%
  583. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  584. //% #pragma unused(numItems)
  585. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  586. //%}
  587. //%
  588. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, )
  589. //%
  590. //%VALUE_FOR_KEY_##VHELPER(KEY_TYPE##KisP$S##KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  591. //%
  592. //%DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, )
  593. //%
  594. //%@end
  595. //%
  596. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER)
  597. //%DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, POD)
  598. //%PDDM-DEFINE DICTIONARY_KEY_TO_ENUM_IMPL2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER)
  599. //%#pragma mark - KEY_NAME -> VALUE_NAME
  600. //%
  601. //%@implementation GPB##KEY_NAME##VALUE_NAME##Dictionary {
  602. //% @package
  603. //% NSMutableDictionary *_dictionary;
  604. //% GPBEnumValidationFunc _validationFunc;
  605. //%}
  606. //%
  607. //%@synthesize validationFunc = _validationFunc;
  608. //%
  609. //%- (instancetype)init {
  610. //% return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  611. //%}
  612. //%
  613. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  614. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  615. //%}
  616. //%
  617. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  618. //% rawValues:(const VALUE_TYPE [])rawValues
  619. //% forKeys:(const KEY_TYPE##KisP$S##KisP [])keys
  620. //% count:(NSUInteger)count {
  621. //% self = [super init];
  622. //% if (self) {
  623. //% _dictionary = [[NSMutableDictionary alloc] init];
  624. //% _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  625. //% if (count && rawValues && keys) {
  626. //% for (NSUInteger i = 0; i < count; ++i) {
  627. //%DICTIONARY_VALIDATE_KEY_##KHELPER(keys[i], ______) [_dictionary setObject:WRAPPED##VHELPER(rawValues[i]) forKey:WRAPPED##KHELPER(keys[i])];
  628. //% }
  629. //% }
  630. //% }
  631. //% return self;
  632. //%}
  633. //%
  634. //%- (instancetype)initWithDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)dictionary {
  635. //% self = [self initWithValidationFunction:dictionary.validationFunc
  636. //% rawValues:NULL
  637. //% forKeys:NULL
  638. //% count:0];
  639. //% if (self) {
  640. //% if (dictionary) {
  641. //% [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  642. //% }
  643. //% }
  644. //% return self;
  645. //%}
  646. //%
  647. //%- (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  648. //% capacity:(NSUInteger)numItems {
  649. //% #pragma unused(numItems)
  650. //% return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  651. //%}
  652. //%
  653. //%DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Value, value, Raw)
  654. //%
  655. //%- (BOOL)getEnum:(VALUE_TYPE *)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  656. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  657. //% if (wrapped && value) {
  658. //% VALUE_TYPE result = UNWRAP##VALUE_NAME(wrapped);
  659. //% if (!_validationFunc(result)) {
  660. //% result = kGPBUnrecognizedEnumeratorValue;
  661. //% }
  662. //% *value = result;
  663. //% }
  664. //% return (wrapped != NULL);
  665. //%}
  666. //%
  667. //%- (BOOL)getRawValue:(VALUE_TYPE *)rawValue forKey:(KEY_TYPE##KisP$S##KisP)key {
  668. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  669. //% if (wrapped && rawValue) {
  670. //% *rawValue = UNWRAP##VALUE_NAME(wrapped);
  671. //% }
  672. //% return (wrapped != NULL);
  673. //%}
  674. //%
  675. //%- (void)enumerateKeysAndEnumsUsingBlock:
  676. //% (void (NS_NOESCAPE ^)(KEY_TYPE KisP##key, VALUE_TYPE value, BOOL *stop))block {
  677. //% GPBEnumValidationFunc func = _validationFunc;
  678. //% BOOL stop = NO;
  679. //% NSEnumerator *keys = [_dictionary keyEnumerator];
  680. //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey;
  681. //% while ((aKey = [keys nextObject])) {
  682. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##aValue = _dictionary[aKey];
  683. //% VALUE_TYPE unwrapped = UNWRAP##VALUE_NAME(aValue);
  684. //% if (!func(unwrapped)) {
  685. //% unwrapped = kGPBUnrecognizedEnumeratorValue;
  686. //% }
  687. //% block(UNWRAP##KEY_NAME(aKey), unwrapped, &stop);
  688. //% if (stop) {
  689. //% break;
  690. //% }
  691. //% }
  692. //%}
  693. //%
  694. //%DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, Value, Enum, value, Raw)
  695. //%
  696. //%- (void)setEnum:(VALUE_TYPE)value forKey:(KEY_TYPE##KisP$S##KisP)key {
  697. //%DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) if (!_validationFunc(value)) {
  698. //% [NSException raise:NSInvalidArgumentException
  699. //% format:@"GPB##KEY_NAME##VALUE_NAME##Dictionary: Attempt to set an unknown enum value (%d)",
  700. //% value];
  701. //% }
  702. //%
  703. //% [_dictionary setObject:WRAPPED##VHELPER(value) forKey:WRAPPED##KHELPER(key)];
  704. //% if (_autocreator) {
  705. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  706. //% }
  707. //%}
  708. //%
  709. //%@end
  710. //%
  711. //%PDDM-DEFINE DICTIONARY_IMMUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, ACCESSOR_NAME)
  712. //%- (void)dealloc {
  713. //% NSAssert(!_autocreator,
  714. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  715. //% [self class], _autocreator);
  716. //% [_dictionary release];
  717. //% [super dealloc];
  718. //%}
  719. //%
  720. //%- (instancetype)copyWithZone:(NSZone *)zone {
  721. //% return [[GPB##KEY_NAME##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  722. //%}
  723. //%
  724. //%- (BOOL)isEqual:(id)other {
  725. //% if (self == other) {
  726. //% return YES;
  727. //% }
  728. //% if (![other isKindOfClass:[GPB##KEY_NAME##VALUE_NAME##Dictionary class]]) {
  729. //% return NO;
  730. //% }
  731. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *otherDictionary = other;
  732. //% return [_dictionary isEqual:otherDictionary->_dictionary];
  733. //%}
  734. //%
  735. //%- (NSUInteger)hash {
  736. //% return _dictionary.count;
  737. //%}
  738. //%
  739. //%- (NSString *)description {
  740. //% return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  741. //%}
  742. //%
  743. //%- (NSUInteger)count {
  744. //% return _dictionary.count;
  745. //%}
  746. //%
  747. //%- (void)enumerateKeysAnd##ACCESSOR_NAME##VNAME##sUsingBlock:
  748. //% (void (NS_NOESCAPE ^)(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, BOOL *stop))block {
  749. //% BOOL stop = NO;
  750. //% NSDictionary *internal = _dictionary;
  751. //% NSEnumerator *keys = [internal keyEnumerator];
  752. //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey;
  753. //% while ((aKey = [keys nextObject])) {
  754. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u = internal[aKey];
  755. //% block(UNWRAP##KEY_NAME(aKey), UNWRAP##VALUE_NAME(a##VNAME_VAR$u), &stop);
  756. //% if (stop) {
  757. //% break;
  758. //% }
  759. //% }
  760. //%}
  761. //%
  762. //%EXTRA_METHODS_##VHELPER(KEY_NAME, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  763. //% NSDictionary *internal = _dictionary;
  764. //% NSUInteger count = internal.count;
  765. //% if (count == 0) {
  766. //% return 0;
  767. //% }
  768. //%
  769. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  770. //% GPBDataType keyDataType = field.mapKeyDataType;
  771. //% size_t result = 0;
  772. //% NSEnumerator *keys = [internal keyEnumerator];
  773. //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey;
  774. //% while ((aKey = [keys nextObject])) {
  775. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u = internal[aKey];
  776. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(UNWRAP##KEY_NAME(aKey), kMapKeyFieldNumber, keyDataType);
  777. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(UNWRAP##VALUE_NAME(a##VNAME_VAR$u), kMapValueFieldNumber, valueDataType);
  778. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  779. //% }
  780. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  781. //% result += tagSize * count;
  782. //% return result;
  783. //%}
  784. //%
  785. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  786. //% asField:(GPBFieldDescriptor *)field {
  787. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  788. //% GPBDataType keyDataType = field.mapKeyDataType;
  789. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  790. //% NSDictionary *internal = _dictionary;
  791. //% NSEnumerator *keys = [internal keyEnumerator];
  792. //% ENUM_TYPE##KHELPER(KEY_TYPE)##aKey;
  793. //% while ((aKey = [keys nextObject])) {
  794. //% ENUM_TYPE##VHELPER(VALUE_TYPE)##a##VNAME_VAR$u = internal[aKey];
  795. //% [outputStream writeInt32NoTag:tag];
  796. //% // Write the size of the message.
  797. //% KEY_TYPE KisP##unwrappedKey = UNWRAP##KEY_NAME(aKey);
  798. //% VALUE_TYPE unwrappedValue = UNWRAP##VALUE_NAME(a##VNAME_VAR$u);
  799. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  800. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  801. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  802. //% // Write the fields.
  803. //% WriteDict##KEY_NAME##Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  804. //% WriteDict##VALUE_NAME##Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  805. //% }
  806. //%}
  807. //%
  808. //%SERIAL_DATA_FOR_ENTRY_##VHELPER(KEY_NAME, VALUE_NAME)- (void)setGPBGenericValue:(GPBGenericValue *)value
  809. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  810. //% [_dictionary setObject:WRAPPED##VHELPER(value->##GPBVALUE_##VHELPER(VALUE_NAME)##) forKey:WRAPPED##KHELPER(key->value##KEY_NAME)];
  811. //%}
  812. //%
  813. //%- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  814. //% [self enumerateKeysAnd##ACCESSOR_NAME##VNAME##sUsingBlock:^(KEY_TYPE KisP##key, VALUE_TYPE VNAME_VAR, BOOL *stop) {
  815. //% #pragma unused(stop)
  816. //% block(TEXT_FORMAT_OBJ##KEY_NAME(key), TEXT_FORMAT_OBJ##VALUE_NAME(VNAME_VAR));
  817. //% }];
  818. //%}
  819. //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_VAR, ACCESSOR_NAME)
  820. //%DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME, VNAME_VAR, ACCESSOR_NAME)
  821. //%PDDM-DEFINE DICTIONARY_MUTABLE_CORE2(KEY_NAME, KEY_TYPE, KisP, VALUE_NAME, VALUE_TYPE, KHELPER, VHELPER, VNAME, VNAME_REMOVE, VNAME_VAR, ACCESSOR_NAME)
  822. //%- (void)add##ACCESSOR_NAME##EntriesFromDictionary:(GPB##KEY_NAME##VALUE_NAME##Dictionary *)otherDictionary {
  823. //% if (otherDictionary) {
  824. //% [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  825. //% if (_autocreator) {
  826. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  827. //% }
  828. //% }
  829. //%}
  830. //%
  831. //%- (void)set##ACCESSOR_NAME##VNAME##:(VALUE_TYPE)VNAME_VAR forKey:(KEY_TYPE##KisP$S##KisP)key {
  832. //%DICTIONARY_VALIDATE_VALUE_##VHELPER(VNAME_VAR, )##DICTIONARY_VALIDATE_KEY_##KHELPER(key, ) [_dictionary setObject:WRAPPED##VHELPER(VNAME_VAR) forKey:WRAPPED##KHELPER(key)];
  833. //% if (_autocreator) {
  834. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  835. //% }
  836. //%}
  837. //%
  838. //%- (void)remove##VNAME_REMOVE##ForKey:(KEY_TYPE##KisP$S##KisP)aKey {
  839. //% [_dictionary removeObjectForKey:WRAPPED##KHELPER(aKey)];
  840. //%}
  841. //%
  842. //%- (void)removeAll {
  843. //% [_dictionary removeAllObjects];
  844. //%}
  845. //
  846. // Custom Generation for Bool keys
  847. //
  848. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_POD_IMPL(VALUE_NAME, VALUE_TYPE)
  849. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, POD, VALUE_NAME, value)
  850. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(VALUE_NAME, VALUE_TYPE)
  851. //%DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, OBJECT, Object, object)
  852. //%PDDM-DEFINE DICTIONARY_BOOL_KEY_TO_VALUE_IMPL(VALUE_NAME, VALUE_TYPE, HELPER, VNAME, VNAME_VAR)
  853. //%#pragma mark - Bool -> VALUE_NAME
  854. //%
  855. //%@implementation GPBBool##VALUE_NAME##Dictionary {
  856. //% @package
  857. //% VALUE_TYPE _values[2];
  858. //%BOOL_DICT_HAS_STORAGE_##HELPER()}
  859. //%
  860. //%- (instancetype)init {
  861. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  862. //%}
  863. //%
  864. //%BOOL_DICT_INITS_##HELPER(VALUE_NAME, VALUE_TYPE)
  865. //%
  866. //%- (instancetype)initWithCapacity:(NSUInteger)numItems {
  867. //% #pragma unused(numItems)
  868. //% return [self initWith##VNAME##s:NULL forKeys:NULL count:0];
  869. //%}
  870. //%
  871. //%BOOL_DICT_DEALLOC##HELPER()
  872. //%
  873. //%- (instancetype)copyWithZone:(NSZone *)zone {
  874. //% return [[GPBBool##VALUE_NAME##Dictionary allocWithZone:zone] initWithDictionary:self];
  875. //%}
  876. //%
  877. //%- (BOOL)isEqual:(id)other {
  878. //% if (self == other) {
  879. //% return YES;
  880. //% }
  881. //% if (![other isKindOfClass:[GPBBool##VALUE_NAME##Dictionary class]]) {
  882. //% return NO;
  883. //% }
  884. //% GPBBool##VALUE_NAME##Dictionary *otherDictionary = other;
  885. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) != BOOL_DICT_W_HAS##HELPER(0, otherDictionary->)) ||
  886. //% (BOOL_DICT_W_HAS##HELPER(1, ) != BOOL_DICT_W_HAS##HELPER(1, otherDictionary->))) {
  887. //% return NO;
  888. //% }
  889. //% if ((BOOL_DICT_W_HAS##HELPER(0, ) && (NEQ_##HELPER(_values[0], otherDictionary->_values[0]))) ||
  890. //% (BOOL_DICT_W_HAS##HELPER(1, ) && (NEQ_##HELPER(_values[1], otherDictionary->_values[1])))) {
  891. //% return NO;
  892. //% }
  893. //% return YES;
  894. //%}
  895. //%
  896. //%- (NSUInteger)hash {
  897. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  898. //%}
  899. //%
  900. //%- (NSString *)description {
  901. //% NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  902. //% if (BOOL_DICT_W_HAS##HELPER(0, )) {
  903. //% [result appendFormat:@"NO: STR_FORMAT_##HELPER(VALUE_NAME)", _values[0]];
  904. //% }
  905. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  906. //% [result appendFormat:@"YES: STR_FORMAT_##HELPER(VALUE_NAME)", _values[1]];
  907. //% }
  908. //% [result appendString:@" }"];
  909. //% return result;
  910. //%}
  911. //%
  912. //%- (NSUInteger)count {
  913. //% return (BOOL_DICT_W_HAS##HELPER(0, ) ? 1 : 0) + (BOOL_DICT_W_HAS##HELPER(1, ) ? 1 : 0);
  914. //%}
  915. //%
  916. //%BOOL_VALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE)
  917. //%
  918. //%BOOL_SET_GPBVALUE_FOR_KEY_##HELPER(VALUE_NAME, VALUE_TYPE, VisP)
  919. //%
  920. //%- (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  921. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  922. //% block(@"false", TEXT_FORMAT_OBJ##VALUE_NAME(_values[0]));
  923. //% }
  924. //% if (BOOL_DICT_W_HAS##HELPER(1, )) {
  925. //% block(@"true", TEXT_FORMAT_OBJ##VALUE_NAME(_values[1]));
  926. //% }
  927. //%}
  928. //%
  929. //%- (void)enumerateKeysAnd##VNAME##sUsingBlock:
  930. //% (void (NS_NOESCAPE ^)(BOOL key, VALUE_TYPE VNAME_VAR, BOOL *stop))block {
  931. //% BOOL stop = NO;
  932. //% if (BOOL_DICT_HAS##HELPER(0, )) {
  933. //% block(NO, _values[0], &stop);
  934. //% }
  935. //% if (!stop && BOOL_DICT_W_HAS##HELPER(1, )) {
  936. //% block(YES, _values[1], &stop);
  937. //% }
  938. //%}
  939. //%
  940. //%BOOL_EXTRA_METHODS_##HELPER(Bool, VALUE_NAME)- (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  941. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  942. //% NSUInteger count = 0;
  943. //% size_t result = 0;
  944. //% for (int i = 0; i < 2; ++i) {
  945. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  946. //% ++count;
  947. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  948. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  949. //% result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  950. //% }
  951. //% }
  952. //% size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  953. //% result += tagSize * count;
  954. //% return result;
  955. //%}
  956. //%
  957. //%- (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  958. //% asField:(GPBFieldDescriptor *)field {
  959. //% GPBDataType valueDataType = GPBGetFieldDataType(field);
  960. //% uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  961. //% for (int i = 0; i < 2; ++i) {
  962. //% if (BOOL_DICT_HAS##HELPER(i, )) {
  963. //% // Write the tag.
  964. //% [outputStream writeInt32NoTag:tag];
  965. //% // Write the size of the message.
  966. //% size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  967. //% msgSize += ComputeDict##VALUE_NAME##FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  968. //% [outputStream writeInt32NoTag:(int32_t)msgSize];
  969. //% // Write the fields.
  970. //% WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  971. //% WriteDict##VALUE_NAME##Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  972. //% }
  973. //% }
  974. //%}
  975. //%
  976. //%BOOL_DICT_MUTATIONS_##HELPER(VALUE_NAME, VALUE_TYPE)
  977. //%
  978. //%@end
  979. //%
  980. //
  981. // Helpers for PODs
  982. //
  983. //%PDDM-DEFINE VALUE_FOR_KEY_POD(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  984. //%- (BOOL)get##VALUE_NAME##:(nullable VALUE_TYPE *)value forKey:(KEY_TYPE)key {
  985. //% NSNumber *wrapped = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  986. //% if (wrapped && value) {
  987. //% *value = UNWRAP##VALUE_NAME(wrapped);
  988. //% }
  989. //% return (wrapped != NULL);
  990. //%}
  991. //%PDDM-DEFINE WRAPPEDPOD(VALUE)
  992. //%@(VALUE)
  993. //%PDDM-DEFINE UNWRAPUInt32(VALUE)
  994. //%[VALUE unsignedIntValue]
  995. //%PDDM-DEFINE UNWRAPInt32(VALUE)
  996. //%[VALUE intValue]
  997. //%PDDM-DEFINE UNWRAPUInt64(VALUE)
  998. //%[VALUE unsignedLongLongValue]
  999. //%PDDM-DEFINE UNWRAPInt64(VALUE)
  1000. //%[VALUE longLongValue]
  1001. //%PDDM-DEFINE UNWRAPBool(VALUE)
  1002. //%[VALUE boolValue]
  1003. //%PDDM-DEFINE UNWRAPFloat(VALUE)
  1004. //%[VALUE floatValue]
  1005. //%PDDM-DEFINE UNWRAPDouble(VALUE)
  1006. //%[VALUE doubleValue]
  1007. //%PDDM-DEFINE UNWRAPEnum(VALUE)
  1008. //%[VALUE intValue]
  1009. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt32(VALUE)
  1010. //%[NSString stringWithFormat:@"%u", VALUE]
  1011. //%PDDM-DEFINE TEXT_FORMAT_OBJInt32(VALUE)
  1012. //%[NSString stringWithFormat:@"%d", VALUE]
  1013. //%PDDM-DEFINE TEXT_FORMAT_OBJUInt64(VALUE)
  1014. //%[NSString stringWithFormat:@"%llu", VALUE]
  1015. //%PDDM-DEFINE TEXT_FORMAT_OBJInt64(VALUE)
  1016. //%[NSString stringWithFormat:@"%lld", VALUE]
  1017. //%PDDM-DEFINE TEXT_FORMAT_OBJBool(VALUE)
  1018. //%(VALUE ? @"true" : @"false")
  1019. //%PDDM-DEFINE TEXT_FORMAT_OBJFloat(VALUE)
  1020. //%[NSString stringWithFormat:@"%.*g", FLT_DIG, VALUE]
  1021. //%PDDM-DEFINE TEXT_FORMAT_OBJDouble(VALUE)
  1022. //%[NSString stringWithFormat:@"%.*lg", DBL_DIG, VALUE]
  1023. //%PDDM-DEFINE TEXT_FORMAT_OBJEnum(VALUE)
  1024. //%@(VALUE)
  1025. //%PDDM-DEFINE ENUM_TYPEPOD(TYPE)
  1026. //%NSNumber *
  1027. //%PDDM-DEFINE NEQ_POD(VAL1, VAL2)
  1028. //%VAL1 != VAL2
  1029. //%PDDM-DEFINE EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1030. // Empty
  1031. //%PDDM-DEFINE BOOL_EXTRA_METHODS_POD(KEY_NAME, VALUE_NAME)
  1032. // Empty
  1033. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD(KEY_NAME, VALUE_NAME)
  1034. //%SERIAL_DATA_FOR_ENTRY_POD_##VALUE_NAME(KEY_NAME)
  1035. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt32(KEY_NAME)
  1036. // Empty
  1037. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int32(KEY_NAME)
  1038. // Empty
  1039. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_UInt64(KEY_NAME)
  1040. // Empty
  1041. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Int64(KEY_NAME)
  1042. // Empty
  1043. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Bool(KEY_NAME)
  1044. // Empty
  1045. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Float(KEY_NAME)
  1046. // Empty
  1047. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Double(KEY_NAME)
  1048. // Empty
  1049. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_POD_Enum(KEY_NAME)
  1050. //%- (NSData *)serializedDataForUnknownValue:(int32_t)value
  1051. //% forKey:(GPBGenericValue *)key
  1052. //% keyDataType:(GPBDataType)keyDataType {
  1053. //% size_t msgSize = ComputeDict##KEY_NAME##FieldSize(key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  1054. //% msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  1055. //% NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  1056. //% GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  1057. //% WriteDict##KEY_NAME##Field(outputStream, key->value##KEY_NAME, kMapKeyFieldNumber, keyDataType);
  1058. //% WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  1059. //% [outputStream release];
  1060. //% return data;
  1061. //%}
  1062. //%
  1063. //%PDDM-DEFINE GPBVALUE_POD(VALUE_NAME)
  1064. //%value##VALUE_NAME
  1065. //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_POD(VALUE_NAME, EXTRA_INDENT)
  1066. // Empty
  1067. //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_POD(KEY_NAME, EXTRA_INDENT)
  1068. // Empty
  1069. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_POD()
  1070. //% BOOL _valueSet[2];
  1071. //%
  1072. //%PDDM-DEFINE BOOL_DICT_INITS_POD(VALUE_NAME, VALUE_TYPE)
  1073. //%- (instancetype)initWith##VALUE_NAME##s:(const VALUE_TYPE [])values
  1074. //% ##VALUE_NAME$S## forKeys:(const BOOL [])keys
  1075. //% ##VALUE_NAME$S## count:(NSUInteger)count {
  1076. //% self = [super init];
  1077. //% if (self) {
  1078. //% for (NSUInteger i = 0; i < count; ++i) {
  1079. //% int idx = keys[i] ? 1 : 0;
  1080. //% _values[idx] = values[i];
  1081. //% _valueSet[idx] = YES;
  1082. //% }
  1083. //% }
  1084. //% return self;
  1085. //%}
  1086. //%
  1087. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1088. //% self = [self initWith##VALUE_NAME##s:NULL forKeys:NULL count:0];
  1089. //% if (self) {
  1090. //% if (dictionary) {
  1091. //% for (int i = 0; i < 2; ++i) {
  1092. //% if (dictionary->_valueSet[i]) {
  1093. //% _values[i] = dictionary->_values[i];
  1094. //% _valueSet[i] = YES;
  1095. //% }
  1096. //% }
  1097. //% }
  1098. //% }
  1099. //% return self;
  1100. //%}
  1101. //%PDDM-DEFINE BOOL_DICT_DEALLOCPOD()
  1102. //%#if !defined(NS_BLOCK_ASSERTIONS)
  1103. //%- (void)dealloc {
  1104. //% NSAssert(!_autocreator,
  1105. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1106. //% [self class], _autocreator);
  1107. //% [super dealloc];
  1108. //%}
  1109. //%#endif // !defined(NS_BLOCK_ASSERTIONS)
  1110. //%PDDM-DEFINE BOOL_DICT_W_HASPOD(IDX, REF)
  1111. //%BOOL_DICT_HASPOD(IDX, REF)
  1112. //%PDDM-DEFINE BOOL_DICT_HASPOD(IDX, REF)
  1113. //%REF##_valueSet[IDX]
  1114. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE)
  1115. //%- (BOOL)get##VALUE_NAME##:(VALUE_TYPE *)value forKey:(BOOL)key {
  1116. //% int idx = (key ? 1 : 0);
  1117. //% if (_valueSet[idx]) {
  1118. //% if (value) {
  1119. //% *value = _values[idx];
  1120. //% }
  1121. //% return YES;
  1122. //% }
  1123. //% return NO;
  1124. //%}
  1125. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_POD(VALUE_NAME, VALUE_TYPE, VisP)
  1126. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1127. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1128. //% int idx = (key->valueBool ? 1 : 0);
  1129. //% _values[idx] = value->value##VALUE_NAME;
  1130. //% _valueSet[idx] = YES;
  1131. //%}
  1132. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_POD(VALUE_NAME, VALUE_TYPE)
  1133. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1134. //% if (otherDictionary) {
  1135. //% for (int i = 0; i < 2; ++i) {
  1136. //% if (otherDictionary->_valueSet[i]) {
  1137. //% _valueSet[i] = YES;
  1138. //% _values[i] = otherDictionary->_values[i];
  1139. //% }
  1140. //% }
  1141. //% if (_autocreator) {
  1142. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1143. //% }
  1144. //% }
  1145. //%}
  1146. //%
  1147. //%- (void)set##VALUE_NAME:(VALUE_TYPE)value forKey:(BOOL)key {
  1148. //% int idx = (key ? 1 : 0);
  1149. //% _values[idx] = value;
  1150. //% _valueSet[idx] = YES;
  1151. //% if (_autocreator) {
  1152. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1153. //% }
  1154. //%}
  1155. //%
  1156. //%- (void)remove##VALUE_NAME##ForKey:(BOOL)aKey {
  1157. //% _valueSet[aKey ? 1 : 0] = NO;
  1158. //%}
  1159. //%
  1160. //%- (void)removeAll {
  1161. //% _valueSet[0] = NO;
  1162. //% _valueSet[1] = NO;
  1163. //%}
  1164. //%PDDM-DEFINE STR_FORMAT_POD(VALUE_NAME)
  1165. //%STR_FORMAT_##VALUE_NAME()
  1166. //%PDDM-DEFINE STR_FORMAT_UInt32()
  1167. //%%u
  1168. //%PDDM-DEFINE STR_FORMAT_Int32()
  1169. //%%d
  1170. //%PDDM-DEFINE STR_FORMAT_UInt64()
  1171. //%%llu
  1172. //%PDDM-DEFINE STR_FORMAT_Int64()
  1173. //%%lld
  1174. //%PDDM-DEFINE STR_FORMAT_Bool()
  1175. //%%d
  1176. //%PDDM-DEFINE STR_FORMAT_Float()
  1177. //%%f
  1178. //%PDDM-DEFINE STR_FORMAT_Double()
  1179. //%%lf
  1180. //
  1181. // Helpers for Objects
  1182. //
  1183. //%PDDM-DEFINE VALUE_FOR_KEY_OBJECT(KEY_TYPE, VALUE_NAME, VALUE_TYPE, KHELPER)
  1184. //%- (VALUE_TYPE)objectForKey:(KEY_TYPE)key {
  1185. //% VALUE_TYPE result = [_dictionary objectForKey:WRAPPED##KHELPER(key)];
  1186. //% return result;
  1187. //%}
  1188. //%PDDM-DEFINE WRAPPEDOBJECT(VALUE)
  1189. //%VALUE
  1190. //%PDDM-DEFINE UNWRAPString(VALUE)
  1191. //%VALUE
  1192. //%PDDM-DEFINE UNWRAPObject(VALUE)
  1193. //%VALUE
  1194. //%PDDM-DEFINE TEXT_FORMAT_OBJString(VALUE)
  1195. //%VALUE
  1196. //%PDDM-DEFINE TEXT_FORMAT_OBJObject(VALUE)
  1197. //%VALUE
  1198. //%PDDM-DEFINE ENUM_TYPEOBJECT(TYPE)
  1199. //%ENUM_TYPEOBJECT_##TYPE()
  1200. //%PDDM-DEFINE ENUM_TYPEOBJECT_NSString()
  1201. //%NSString *
  1202. //%PDDM-DEFINE ENUM_TYPEOBJECT_id()
  1203. //%id ##
  1204. //%PDDM-DEFINE NEQ_OBJECT(VAL1, VAL2)
  1205. //%![VAL1 isEqual:VAL2]
  1206. //%PDDM-DEFINE EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1207. //%- (BOOL)isInitialized {
  1208. //% for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  1209. //% if (!msg.initialized) {
  1210. //% return NO;
  1211. //% }
  1212. //% }
  1213. //% return YES;
  1214. //%}
  1215. //%
  1216. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1217. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1218. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1219. //% NSEnumerator *keys = [_dictionary keyEnumerator];
  1220. //% id aKey;
  1221. //% NSMutableDictionary *internalDict = newDict->_dictionary;
  1222. //% while ((aKey = [keys nextObject])) {
  1223. //% GPBMessage *msg = _dictionary[aKey];
  1224. //% GPBMessage *copiedMsg = [msg copyWithZone:zone];
  1225. //% [internalDict setObject:copiedMsg forKey:aKey];
  1226. //% [copiedMsg release];
  1227. //% }
  1228. //% return newDict;
  1229. //%}
  1230. //%
  1231. //%
  1232. //%PDDM-DEFINE BOOL_EXTRA_METHODS_OBJECT(KEY_NAME, VALUE_NAME)
  1233. //%- (BOOL)isInitialized {
  1234. //% if (_values[0] && ![_values[0] isInitialized]) {
  1235. //% return NO;
  1236. //% }
  1237. //% if (_values[1] && ![_values[1] isInitialized]) {
  1238. //% return NO;
  1239. //% }
  1240. //% return YES;
  1241. //%}
  1242. //%
  1243. //%- (instancetype)deepCopyWithZone:(NSZone *)zone {
  1244. //% GPB##KEY_NAME##VALUE_NAME##Dictionary *newDict =
  1245. //% [[GPB##KEY_NAME##VALUE_NAME##Dictionary alloc] init];
  1246. //% for (int i = 0; i < 2; ++i) {
  1247. //% if (_values[i] != nil) {
  1248. //% newDict->_values[i] = [_values[i] copyWithZone:zone];
  1249. //% }
  1250. //% }
  1251. //% return newDict;
  1252. //%}
  1253. //%
  1254. //%
  1255. //%PDDM-DEFINE SERIAL_DATA_FOR_ENTRY_OBJECT(KEY_NAME, VALUE_NAME)
  1256. // Empty
  1257. //%PDDM-DEFINE GPBVALUE_OBJECT(VALUE_NAME)
  1258. //%valueString
  1259. //%PDDM-DEFINE DICTIONARY_VALIDATE_VALUE_OBJECT(VALUE_NAME, EXTRA_INDENT)
  1260. //%##EXTRA_INDENT$S## if (!##VALUE_NAME) {
  1261. //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException
  1262. //%##EXTRA_INDENT$S## format:@"Attempting to add nil object to a Dictionary"];
  1263. //%##EXTRA_INDENT$S## }
  1264. //%
  1265. //%PDDM-DEFINE DICTIONARY_VALIDATE_KEY_OBJECT(KEY_NAME, EXTRA_INDENT)
  1266. //%##EXTRA_INDENT$S## if (!##KEY_NAME) {
  1267. //%##EXTRA_INDENT$S## [NSException raise:NSInvalidArgumentException
  1268. //%##EXTRA_INDENT$S## format:@"Attempting to add nil key to a Dictionary"];
  1269. //%##EXTRA_INDENT$S## }
  1270. //%
  1271. //%PDDM-DEFINE BOOL_DICT_HAS_STORAGE_OBJECT()
  1272. // Empty
  1273. //%PDDM-DEFINE BOOL_DICT_INITS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1274. //%- (instancetype)initWithObjects:(const VALUE_TYPE [])objects
  1275. //% forKeys:(const BOOL [])keys
  1276. //% count:(NSUInteger)count {
  1277. //% self = [super init];
  1278. //% if (self) {
  1279. //% for (NSUInteger i = 0; i < count; ++i) {
  1280. //% if (!objects[i]) {
  1281. //% [NSException raise:NSInvalidArgumentException
  1282. //% format:@"Attempting to add nil object to a Dictionary"];
  1283. //% }
  1284. //% int idx = keys[i] ? 1 : 0;
  1285. //% [_values[idx] release];
  1286. //% _values[idx] = (VALUE_TYPE)[objects[i] retain];
  1287. //% }
  1288. //% }
  1289. //% return self;
  1290. //%}
  1291. //%
  1292. //%- (instancetype)initWithDictionary:(GPBBool##VALUE_NAME##Dictionary *)dictionary {
  1293. //% self = [self initWithObjects:NULL forKeys:NULL count:0];
  1294. //% if (self) {
  1295. //% if (dictionary) {
  1296. //% _values[0] = [dictionary->_values[0] retain];
  1297. //% _values[1] = [dictionary->_values[1] retain];
  1298. //% }
  1299. //% }
  1300. //% return self;
  1301. //%}
  1302. //%PDDM-DEFINE BOOL_DICT_DEALLOCOBJECT()
  1303. //%- (void)dealloc {
  1304. //% NSAssert(!_autocreator,
  1305. //% @"%@: Autocreator must be cleared before release, autocreator: %@",
  1306. //% [self class], _autocreator);
  1307. //% [_values[0] release];
  1308. //% [_values[1] release];
  1309. //% [super dealloc];
  1310. //%}
  1311. //%PDDM-DEFINE BOOL_DICT_W_HASOBJECT(IDX, REF)
  1312. //%(BOOL_DICT_HASOBJECT(IDX, REF))
  1313. //%PDDM-DEFINE BOOL_DICT_HASOBJECT(IDX, REF)
  1314. //%REF##_values[IDX] != nil
  1315. //%PDDM-DEFINE BOOL_VALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE)
  1316. //%- (VALUE_TYPE)objectForKey:(BOOL)key {
  1317. //% return _values[key ? 1 : 0];
  1318. //%}
  1319. //%PDDM-DEFINE BOOL_SET_GPBVALUE_FOR_KEY_OBJECT(VALUE_NAME, VALUE_TYPE, VisP)
  1320. //%- (void)setGPBGenericValue:(GPBGenericValue *)value
  1321. //% forGPBGenericValueKey:(GPBGenericValue *)key {
  1322. //% int idx = (key->valueBool ? 1 : 0);
  1323. //% [_values[idx] release];
  1324. //% _values[idx] = [value->valueString retain];
  1325. //%}
  1326. //%PDDM-DEFINE BOOL_DICT_MUTATIONS_OBJECT(VALUE_NAME, VALUE_TYPE)
  1327. //%- (void)addEntriesFromDictionary:(GPBBool##VALUE_NAME##Dictionary *)otherDictionary {
  1328. //% if (otherDictionary) {
  1329. //% for (int i = 0; i < 2; ++i) {
  1330. //% if (otherDictionary->_values[i] != nil) {
  1331. //% [_values[i] release];
  1332. //% _values[i] = [otherDictionary->_values[i] retain];
  1333. //% }
  1334. //% }
  1335. //% if (_autocreator) {
  1336. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1337. //% }
  1338. //% }
  1339. //%}
  1340. //%
  1341. //%- (void)setObject:(VALUE_TYPE)object forKey:(BOOL)key {
  1342. //% if (!object) {
  1343. //% [NSException raise:NSInvalidArgumentException
  1344. //% format:@"Attempting to add nil object to a Dictionary"];
  1345. //% }
  1346. //% int idx = (key ? 1 : 0);
  1347. //% [_values[idx] release];
  1348. //% _values[idx] = [object retain];
  1349. //% if (_autocreator) {
  1350. //% GPBAutocreatedDictionaryModified(_autocreator, self);
  1351. //% }
  1352. //%}
  1353. //%
  1354. //%- (void)removeObjectForKey:(BOOL)aKey {
  1355. //% int idx = (aKey ? 1 : 0);
  1356. //% [_values[idx] release];
  1357. //% _values[idx] = nil;
  1358. //%}
  1359. //%
  1360. //%- (void)removeAll {
  1361. //% for (int i = 0; i < 2; ++i) {
  1362. //% [_values[i] release];
  1363. //% _values[i] = nil;
  1364. //% }
  1365. //%}
  1366. //%PDDM-DEFINE STR_FORMAT_OBJECT(VALUE_NAME)
  1367. //%%@
  1368. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt32, uint32_t)
  1369. // This block of code is generated, do not edit it directly.
  1370. // clang-format off
  1371. #pragma mark - UInt32 -> UInt32
  1372. @implementation GPBUInt32UInt32Dictionary {
  1373. @package
  1374. NSMutableDictionary *_dictionary;
  1375. }
  1376. - (instancetype)init {
  1377. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  1378. }
  1379. - (instancetype)initWithUInt32s:(const uint32_t [])values
  1380. forKeys:(const uint32_t [])keys
  1381. count:(NSUInteger)count {
  1382. self = [super init];
  1383. if (self) {
  1384. _dictionary = [[NSMutableDictionary alloc] init];
  1385. if (count && values && keys) {
  1386. for (NSUInteger i = 0; i < count; ++i) {
  1387. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1388. }
  1389. }
  1390. }
  1391. return self;
  1392. }
  1393. - (instancetype)initWithDictionary:(GPBUInt32UInt32Dictionary *)dictionary {
  1394. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  1395. if (self) {
  1396. if (dictionary) {
  1397. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1398. }
  1399. }
  1400. return self;
  1401. }
  1402. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1403. #pragma unused(numItems)
  1404. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  1405. }
  1406. - (void)dealloc {
  1407. NSAssert(!_autocreator,
  1408. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1409. [self class], _autocreator);
  1410. [_dictionary release];
  1411. [super dealloc];
  1412. }
  1413. - (instancetype)copyWithZone:(NSZone *)zone {
  1414. return [[GPBUInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  1415. }
  1416. - (BOOL)isEqual:(id)other {
  1417. if (self == other) {
  1418. return YES;
  1419. }
  1420. if (![other isKindOfClass:[GPBUInt32UInt32Dictionary class]]) {
  1421. return NO;
  1422. }
  1423. GPBUInt32UInt32Dictionary *otherDictionary = other;
  1424. return [_dictionary isEqual:otherDictionary->_dictionary];
  1425. }
  1426. - (NSUInteger)hash {
  1427. return _dictionary.count;
  1428. }
  1429. - (NSString *)description {
  1430. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1431. }
  1432. - (NSUInteger)count {
  1433. return _dictionary.count;
  1434. }
  1435. - (void)enumerateKeysAndUInt32sUsingBlock:
  1436. (void (NS_NOESCAPE ^)(uint32_t key, uint32_t value, BOOL *stop))block {
  1437. BOOL stop = NO;
  1438. NSDictionary *internal = _dictionary;
  1439. NSEnumerator *keys = [internal keyEnumerator];
  1440. NSNumber *aKey;
  1441. while ((aKey = [keys nextObject])) {
  1442. NSNumber *aValue = internal[aKey];
  1443. block([aKey unsignedIntValue], [aValue unsignedIntValue], &stop);
  1444. if (stop) {
  1445. break;
  1446. }
  1447. }
  1448. }
  1449. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1450. NSDictionary *internal = _dictionary;
  1451. NSUInteger count = internal.count;
  1452. if (count == 0) {
  1453. return 0;
  1454. }
  1455. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1456. GPBDataType keyDataType = field.mapKeyDataType;
  1457. size_t result = 0;
  1458. NSEnumerator *keys = [internal keyEnumerator];
  1459. NSNumber *aKey;
  1460. while ((aKey = [keys nextObject])) {
  1461. NSNumber *aValue = internal[aKey];
  1462. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1463. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  1464. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1465. }
  1466. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1467. result += tagSize * count;
  1468. return result;
  1469. }
  1470. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1471. asField:(GPBFieldDescriptor *)field {
  1472. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1473. GPBDataType keyDataType = field.mapKeyDataType;
  1474. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1475. NSDictionary *internal = _dictionary;
  1476. NSEnumerator *keys = [internal keyEnumerator];
  1477. NSNumber *aKey;
  1478. while ((aKey = [keys nextObject])) {
  1479. NSNumber *aValue = internal[aKey];
  1480. [outputStream writeInt32NoTag:tag];
  1481. // Write the size of the message.
  1482. uint32_t unwrappedKey = [aKey unsignedIntValue];
  1483. uint32_t unwrappedValue = [aValue unsignedIntValue];
  1484. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1485. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  1486. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1487. // Write the fields.
  1488. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1489. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  1490. }
  1491. }
  1492. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1493. forGPBGenericValueKey:(GPBGenericValue *)key {
  1494. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt32)];
  1495. }
  1496. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  1497. [self enumerateKeysAndUInt32sUsingBlock:^(uint32_t key, uint32_t value, BOOL *stop) {
  1498. #pragma unused(stop)
  1499. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%u", value]);
  1500. }];
  1501. }
  1502. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(uint32_t)key {
  1503. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1504. if (wrapped && value) {
  1505. *value = [wrapped unsignedIntValue];
  1506. }
  1507. return (wrapped != NULL);
  1508. }
  1509. - (void)addEntriesFromDictionary:(GPBUInt32UInt32Dictionary *)otherDictionary {
  1510. if (otherDictionary) {
  1511. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1512. if (_autocreator) {
  1513. GPBAutocreatedDictionaryModified(_autocreator, self);
  1514. }
  1515. }
  1516. }
  1517. - (void)setUInt32:(uint32_t)value forKey:(uint32_t)key {
  1518. [_dictionary setObject:@(value) forKey:@(key)];
  1519. if (_autocreator) {
  1520. GPBAutocreatedDictionaryModified(_autocreator, self);
  1521. }
  1522. }
  1523. - (void)removeUInt32ForKey:(uint32_t)aKey {
  1524. [_dictionary removeObjectForKey:@(aKey)];
  1525. }
  1526. - (void)removeAll {
  1527. [_dictionary removeAllObjects];
  1528. }
  1529. @end
  1530. #pragma mark - UInt32 -> Int32
  1531. @implementation GPBUInt32Int32Dictionary {
  1532. @package
  1533. NSMutableDictionary *_dictionary;
  1534. }
  1535. - (instancetype)init {
  1536. return [self initWithInt32s:NULL forKeys:NULL count:0];
  1537. }
  1538. - (instancetype)initWithInt32s:(const int32_t [])values
  1539. forKeys:(const uint32_t [])keys
  1540. count:(NSUInteger)count {
  1541. self = [super init];
  1542. if (self) {
  1543. _dictionary = [[NSMutableDictionary alloc] init];
  1544. if (count && values && keys) {
  1545. for (NSUInteger i = 0; i < count; ++i) {
  1546. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1547. }
  1548. }
  1549. }
  1550. return self;
  1551. }
  1552. - (instancetype)initWithDictionary:(GPBUInt32Int32Dictionary *)dictionary {
  1553. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  1554. if (self) {
  1555. if (dictionary) {
  1556. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1557. }
  1558. }
  1559. return self;
  1560. }
  1561. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1562. #pragma unused(numItems)
  1563. return [self initWithInt32s:NULL forKeys:NULL count:0];
  1564. }
  1565. - (void)dealloc {
  1566. NSAssert(!_autocreator,
  1567. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1568. [self class], _autocreator);
  1569. [_dictionary release];
  1570. [super dealloc];
  1571. }
  1572. - (instancetype)copyWithZone:(NSZone *)zone {
  1573. return [[GPBUInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  1574. }
  1575. - (BOOL)isEqual:(id)other {
  1576. if (self == other) {
  1577. return YES;
  1578. }
  1579. if (![other isKindOfClass:[GPBUInt32Int32Dictionary class]]) {
  1580. return NO;
  1581. }
  1582. GPBUInt32Int32Dictionary *otherDictionary = other;
  1583. return [_dictionary isEqual:otherDictionary->_dictionary];
  1584. }
  1585. - (NSUInteger)hash {
  1586. return _dictionary.count;
  1587. }
  1588. - (NSString *)description {
  1589. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1590. }
  1591. - (NSUInteger)count {
  1592. return _dictionary.count;
  1593. }
  1594. - (void)enumerateKeysAndInt32sUsingBlock:
  1595. (void (NS_NOESCAPE ^)(uint32_t key, int32_t value, BOOL *stop))block {
  1596. BOOL stop = NO;
  1597. NSDictionary *internal = _dictionary;
  1598. NSEnumerator *keys = [internal keyEnumerator];
  1599. NSNumber *aKey;
  1600. while ((aKey = [keys nextObject])) {
  1601. NSNumber *aValue = internal[aKey];
  1602. block([aKey unsignedIntValue], [aValue intValue], &stop);
  1603. if (stop) {
  1604. break;
  1605. }
  1606. }
  1607. }
  1608. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1609. NSDictionary *internal = _dictionary;
  1610. NSUInteger count = internal.count;
  1611. if (count == 0) {
  1612. return 0;
  1613. }
  1614. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1615. GPBDataType keyDataType = field.mapKeyDataType;
  1616. size_t result = 0;
  1617. NSEnumerator *keys = [internal keyEnumerator];
  1618. NSNumber *aKey;
  1619. while ((aKey = [keys nextObject])) {
  1620. NSNumber *aValue = internal[aKey];
  1621. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1622. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  1623. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1624. }
  1625. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1626. result += tagSize * count;
  1627. return result;
  1628. }
  1629. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1630. asField:(GPBFieldDescriptor *)field {
  1631. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1632. GPBDataType keyDataType = field.mapKeyDataType;
  1633. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1634. NSDictionary *internal = _dictionary;
  1635. NSEnumerator *keys = [internal keyEnumerator];
  1636. NSNumber *aKey;
  1637. while ((aKey = [keys nextObject])) {
  1638. NSNumber *aValue = internal[aKey];
  1639. [outputStream writeInt32NoTag:tag];
  1640. // Write the size of the message.
  1641. uint32_t unwrappedKey = [aKey unsignedIntValue];
  1642. int32_t unwrappedValue = [aValue intValue];
  1643. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1644. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  1645. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1646. // Write the fields.
  1647. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1648. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  1649. }
  1650. }
  1651. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1652. forGPBGenericValueKey:(GPBGenericValue *)key {
  1653. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt32)];
  1654. }
  1655. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  1656. [self enumerateKeysAndInt32sUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  1657. #pragma unused(stop)
  1658. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%d", value]);
  1659. }];
  1660. }
  1661. - (BOOL)getInt32:(nullable int32_t *)value forKey:(uint32_t)key {
  1662. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1663. if (wrapped && value) {
  1664. *value = [wrapped intValue];
  1665. }
  1666. return (wrapped != NULL);
  1667. }
  1668. - (void)addEntriesFromDictionary:(GPBUInt32Int32Dictionary *)otherDictionary {
  1669. if (otherDictionary) {
  1670. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1671. if (_autocreator) {
  1672. GPBAutocreatedDictionaryModified(_autocreator, self);
  1673. }
  1674. }
  1675. }
  1676. - (void)setInt32:(int32_t)value forKey:(uint32_t)key {
  1677. [_dictionary setObject:@(value) forKey:@(key)];
  1678. if (_autocreator) {
  1679. GPBAutocreatedDictionaryModified(_autocreator, self);
  1680. }
  1681. }
  1682. - (void)removeInt32ForKey:(uint32_t)aKey {
  1683. [_dictionary removeObjectForKey:@(aKey)];
  1684. }
  1685. - (void)removeAll {
  1686. [_dictionary removeAllObjects];
  1687. }
  1688. @end
  1689. #pragma mark - UInt32 -> UInt64
  1690. @implementation GPBUInt32UInt64Dictionary {
  1691. @package
  1692. NSMutableDictionary *_dictionary;
  1693. }
  1694. - (instancetype)init {
  1695. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  1696. }
  1697. - (instancetype)initWithUInt64s:(const uint64_t [])values
  1698. forKeys:(const uint32_t [])keys
  1699. count:(NSUInteger)count {
  1700. self = [super init];
  1701. if (self) {
  1702. _dictionary = [[NSMutableDictionary alloc] init];
  1703. if (count && values && keys) {
  1704. for (NSUInteger i = 0; i < count; ++i) {
  1705. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1706. }
  1707. }
  1708. }
  1709. return self;
  1710. }
  1711. - (instancetype)initWithDictionary:(GPBUInt32UInt64Dictionary *)dictionary {
  1712. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  1713. if (self) {
  1714. if (dictionary) {
  1715. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1716. }
  1717. }
  1718. return self;
  1719. }
  1720. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1721. #pragma unused(numItems)
  1722. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  1723. }
  1724. - (void)dealloc {
  1725. NSAssert(!_autocreator,
  1726. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1727. [self class], _autocreator);
  1728. [_dictionary release];
  1729. [super dealloc];
  1730. }
  1731. - (instancetype)copyWithZone:(NSZone *)zone {
  1732. return [[GPBUInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  1733. }
  1734. - (BOOL)isEqual:(id)other {
  1735. if (self == other) {
  1736. return YES;
  1737. }
  1738. if (![other isKindOfClass:[GPBUInt32UInt64Dictionary class]]) {
  1739. return NO;
  1740. }
  1741. GPBUInt32UInt64Dictionary *otherDictionary = other;
  1742. return [_dictionary isEqual:otherDictionary->_dictionary];
  1743. }
  1744. - (NSUInteger)hash {
  1745. return _dictionary.count;
  1746. }
  1747. - (NSString *)description {
  1748. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1749. }
  1750. - (NSUInteger)count {
  1751. return _dictionary.count;
  1752. }
  1753. - (void)enumerateKeysAndUInt64sUsingBlock:
  1754. (void (NS_NOESCAPE ^)(uint32_t key, uint64_t value, BOOL *stop))block {
  1755. BOOL stop = NO;
  1756. NSDictionary *internal = _dictionary;
  1757. NSEnumerator *keys = [internal keyEnumerator];
  1758. NSNumber *aKey;
  1759. while ((aKey = [keys nextObject])) {
  1760. NSNumber *aValue = internal[aKey];
  1761. block([aKey unsignedIntValue], [aValue unsignedLongLongValue], &stop);
  1762. if (stop) {
  1763. break;
  1764. }
  1765. }
  1766. }
  1767. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1768. NSDictionary *internal = _dictionary;
  1769. NSUInteger count = internal.count;
  1770. if (count == 0) {
  1771. return 0;
  1772. }
  1773. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1774. GPBDataType keyDataType = field.mapKeyDataType;
  1775. size_t result = 0;
  1776. NSEnumerator *keys = [internal keyEnumerator];
  1777. NSNumber *aKey;
  1778. while ((aKey = [keys nextObject])) {
  1779. NSNumber *aValue = internal[aKey];
  1780. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1781. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  1782. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1783. }
  1784. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1785. result += tagSize * count;
  1786. return result;
  1787. }
  1788. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1789. asField:(GPBFieldDescriptor *)field {
  1790. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1791. GPBDataType keyDataType = field.mapKeyDataType;
  1792. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1793. NSDictionary *internal = _dictionary;
  1794. NSEnumerator *keys = [internal keyEnumerator];
  1795. NSNumber *aKey;
  1796. while ((aKey = [keys nextObject])) {
  1797. NSNumber *aValue = internal[aKey];
  1798. [outputStream writeInt32NoTag:tag];
  1799. // Write the size of the message.
  1800. uint32_t unwrappedKey = [aKey unsignedIntValue];
  1801. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  1802. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1803. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  1804. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1805. // Write the fields.
  1806. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1807. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  1808. }
  1809. }
  1810. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1811. forGPBGenericValueKey:(GPBGenericValue *)key {
  1812. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt32)];
  1813. }
  1814. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  1815. [self enumerateKeysAndUInt64sUsingBlock:^(uint32_t key, uint64_t value, BOOL *stop) {
  1816. #pragma unused(stop)
  1817. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%llu", value]);
  1818. }];
  1819. }
  1820. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(uint32_t)key {
  1821. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1822. if (wrapped && value) {
  1823. *value = [wrapped unsignedLongLongValue];
  1824. }
  1825. return (wrapped != NULL);
  1826. }
  1827. - (void)addEntriesFromDictionary:(GPBUInt32UInt64Dictionary *)otherDictionary {
  1828. if (otherDictionary) {
  1829. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1830. if (_autocreator) {
  1831. GPBAutocreatedDictionaryModified(_autocreator, self);
  1832. }
  1833. }
  1834. }
  1835. - (void)setUInt64:(uint64_t)value forKey:(uint32_t)key {
  1836. [_dictionary setObject:@(value) forKey:@(key)];
  1837. if (_autocreator) {
  1838. GPBAutocreatedDictionaryModified(_autocreator, self);
  1839. }
  1840. }
  1841. - (void)removeUInt64ForKey:(uint32_t)aKey {
  1842. [_dictionary removeObjectForKey:@(aKey)];
  1843. }
  1844. - (void)removeAll {
  1845. [_dictionary removeAllObjects];
  1846. }
  1847. @end
  1848. #pragma mark - UInt32 -> Int64
  1849. @implementation GPBUInt32Int64Dictionary {
  1850. @package
  1851. NSMutableDictionary *_dictionary;
  1852. }
  1853. - (instancetype)init {
  1854. return [self initWithInt64s:NULL forKeys:NULL count:0];
  1855. }
  1856. - (instancetype)initWithInt64s:(const int64_t [])values
  1857. forKeys:(const uint32_t [])keys
  1858. count:(NSUInteger)count {
  1859. self = [super init];
  1860. if (self) {
  1861. _dictionary = [[NSMutableDictionary alloc] init];
  1862. if (count && values && keys) {
  1863. for (NSUInteger i = 0; i < count; ++i) {
  1864. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  1865. }
  1866. }
  1867. }
  1868. return self;
  1869. }
  1870. - (instancetype)initWithDictionary:(GPBUInt32Int64Dictionary *)dictionary {
  1871. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  1872. if (self) {
  1873. if (dictionary) {
  1874. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  1875. }
  1876. }
  1877. return self;
  1878. }
  1879. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  1880. #pragma unused(numItems)
  1881. return [self initWithInt64s:NULL forKeys:NULL count:0];
  1882. }
  1883. - (void)dealloc {
  1884. NSAssert(!_autocreator,
  1885. @"%@: Autocreator must be cleared before release, autocreator: %@",
  1886. [self class], _autocreator);
  1887. [_dictionary release];
  1888. [super dealloc];
  1889. }
  1890. - (instancetype)copyWithZone:(NSZone *)zone {
  1891. return [[GPBUInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  1892. }
  1893. - (BOOL)isEqual:(id)other {
  1894. if (self == other) {
  1895. return YES;
  1896. }
  1897. if (![other isKindOfClass:[GPBUInt32Int64Dictionary class]]) {
  1898. return NO;
  1899. }
  1900. GPBUInt32Int64Dictionary *otherDictionary = other;
  1901. return [_dictionary isEqual:otherDictionary->_dictionary];
  1902. }
  1903. - (NSUInteger)hash {
  1904. return _dictionary.count;
  1905. }
  1906. - (NSString *)description {
  1907. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  1908. }
  1909. - (NSUInteger)count {
  1910. return _dictionary.count;
  1911. }
  1912. - (void)enumerateKeysAndInt64sUsingBlock:
  1913. (void (NS_NOESCAPE ^)(uint32_t key, int64_t value, BOOL *stop))block {
  1914. BOOL stop = NO;
  1915. NSDictionary *internal = _dictionary;
  1916. NSEnumerator *keys = [internal keyEnumerator];
  1917. NSNumber *aKey;
  1918. while ((aKey = [keys nextObject])) {
  1919. NSNumber *aValue = internal[aKey];
  1920. block([aKey unsignedIntValue], [aValue longLongValue], &stop);
  1921. if (stop) {
  1922. break;
  1923. }
  1924. }
  1925. }
  1926. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  1927. NSDictionary *internal = _dictionary;
  1928. NSUInteger count = internal.count;
  1929. if (count == 0) {
  1930. return 0;
  1931. }
  1932. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1933. GPBDataType keyDataType = field.mapKeyDataType;
  1934. size_t result = 0;
  1935. NSEnumerator *keys = [internal keyEnumerator];
  1936. NSNumber *aKey;
  1937. while ((aKey = [keys nextObject])) {
  1938. NSNumber *aValue = internal[aKey];
  1939. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  1940. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  1941. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  1942. }
  1943. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  1944. result += tagSize * count;
  1945. return result;
  1946. }
  1947. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  1948. asField:(GPBFieldDescriptor *)field {
  1949. GPBDataType valueDataType = GPBGetFieldDataType(field);
  1950. GPBDataType keyDataType = field.mapKeyDataType;
  1951. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  1952. NSDictionary *internal = _dictionary;
  1953. NSEnumerator *keys = [internal keyEnumerator];
  1954. NSNumber *aKey;
  1955. while ((aKey = [keys nextObject])) {
  1956. NSNumber *aValue = internal[aKey];
  1957. [outputStream writeInt32NoTag:tag];
  1958. // Write the size of the message.
  1959. uint32_t unwrappedKey = [aKey unsignedIntValue];
  1960. int64_t unwrappedValue = [aValue longLongValue];
  1961. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1962. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  1963. [outputStream writeInt32NoTag:(int32_t)msgSize];
  1964. // Write the fields.
  1965. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  1966. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  1967. }
  1968. }
  1969. - (void)setGPBGenericValue:(GPBGenericValue *)value
  1970. forGPBGenericValueKey:(GPBGenericValue *)key {
  1971. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt32)];
  1972. }
  1973. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  1974. [self enumerateKeysAndInt64sUsingBlock:^(uint32_t key, int64_t value, BOOL *stop) {
  1975. #pragma unused(stop)
  1976. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%lld", value]);
  1977. }];
  1978. }
  1979. - (BOOL)getInt64:(nullable int64_t *)value forKey:(uint32_t)key {
  1980. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  1981. if (wrapped && value) {
  1982. *value = [wrapped longLongValue];
  1983. }
  1984. return (wrapped != NULL);
  1985. }
  1986. - (void)addEntriesFromDictionary:(GPBUInt32Int64Dictionary *)otherDictionary {
  1987. if (otherDictionary) {
  1988. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  1989. if (_autocreator) {
  1990. GPBAutocreatedDictionaryModified(_autocreator, self);
  1991. }
  1992. }
  1993. }
  1994. - (void)setInt64:(int64_t)value forKey:(uint32_t)key {
  1995. [_dictionary setObject:@(value) forKey:@(key)];
  1996. if (_autocreator) {
  1997. GPBAutocreatedDictionaryModified(_autocreator, self);
  1998. }
  1999. }
  2000. - (void)removeInt64ForKey:(uint32_t)aKey {
  2001. [_dictionary removeObjectForKey:@(aKey)];
  2002. }
  2003. - (void)removeAll {
  2004. [_dictionary removeAllObjects];
  2005. }
  2006. @end
  2007. #pragma mark - UInt32 -> Bool
  2008. @implementation GPBUInt32BoolDictionary {
  2009. @package
  2010. NSMutableDictionary *_dictionary;
  2011. }
  2012. - (instancetype)init {
  2013. return [self initWithBools:NULL forKeys:NULL count:0];
  2014. }
  2015. - (instancetype)initWithBools:(const BOOL [])values
  2016. forKeys:(const uint32_t [])keys
  2017. count:(NSUInteger)count {
  2018. self = [super init];
  2019. if (self) {
  2020. _dictionary = [[NSMutableDictionary alloc] init];
  2021. if (count && values && keys) {
  2022. for (NSUInteger i = 0; i < count; ++i) {
  2023. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2024. }
  2025. }
  2026. }
  2027. return self;
  2028. }
  2029. - (instancetype)initWithDictionary:(GPBUInt32BoolDictionary *)dictionary {
  2030. self = [self initWithBools:NULL forKeys:NULL count:0];
  2031. if (self) {
  2032. if (dictionary) {
  2033. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2034. }
  2035. }
  2036. return self;
  2037. }
  2038. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2039. #pragma unused(numItems)
  2040. return [self initWithBools:NULL forKeys:NULL count:0];
  2041. }
  2042. - (void)dealloc {
  2043. NSAssert(!_autocreator,
  2044. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2045. [self class], _autocreator);
  2046. [_dictionary release];
  2047. [super dealloc];
  2048. }
  2049. - (instancetype)copyWithZone:(NSZone *)zone {
  2050. return [[GPBUInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  2051. }
  2052. - (BOOL)isEqual:(id)other {
  2053. if (self == other) {
  2054. return YES;
  2055. }
  2056. if (![other isKindOfClass:[GPBUInt32BoolDictionary class]]) {
  2057. return NO;
  2058. }
  2059. GPBUInt32BoolDictionary *otherDictionary = other;
  2060. return [_dictionary isEqual:otherDictionary->_dictionary];
  2061. }
  2062. - (NSUInteger)hash {
  2063. return _dictionary.count;
  2064. }
  2065. - (NSString *)description {
  2066. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2067. }
  2068. - (NSUInteger)count {
  2069. return _dictionary.count;
  2070. }
  2071. - (void)enumerateKeysAndBoolsUsingBlock:
  2072. (void (NS_NOESCAPE ^)(uint32_t key, BOOL value, BOOL *stop))block {
  2073. BOOL stop = NO;
  2074. NSDictionary *internal = _dictionary;
  2075. NSEnumerator *keys = [internal keyEnumerator];
  2076. NSNumber *aKey;
  2077. while ((aKey = [keys nextObject])) {
  2078. NSNumber *aValue = internal[aKey];
  2079. block([aKey unsignedIntValue], [aValue boolValue], &stop);
  2080. if (stop) {
  2081. break;
  2082. }
  2083. }
  2084. }
  2085. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2086. NSDictionary *internal = _dictionary;
  2087. NSUInteger count = internal.count;
  2088. if (count == 0) {
  2089. return 0;
  2090. }
  2091. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2092. GPBDataType keyDataType = field.mapKeyDataType;
  2093. size_t result = 0;
  2094. NSEnumerator *keys = [internal keyEnumerator];
  2095. NSNumber *aKey;
  2096. while ((aKey = [keys nextObject])) {
  2097. NSNumber *aValue = internal[aKey];
  2098. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2099. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  2100. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2101. }
  2102. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2103. result += tagSize * count;
  2104. return result;
  2105. }
  2106. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2107. asField:(GPBFieldDescriptor *)field {
  2108. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2109. GPBDataType keyDataType = field.mapKeyDataType;
  2110. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2111. NSDictionary *internal = _dictionary;
  2112. NSEnumerator *keys = [internal keyEnumerator];
  2113. NSNumber *aKey;
  2114. while ((aKey = [keys nextObject])) {
  2115. NSNumber *aValue = internal[aKey];
  2116. [outputStream writeInt32NoTag:tag];
  2117. // Write the size of the message.
  2118. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2119. BOOL unwrappedValue = [aValue boolValue];
  2120. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2121. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2122. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2123. // Write the fields.
  2124. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2125. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2126. }
  2127. }
  2128. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2129. forGPBGenericValueKey:(GPBGenericValue *)key {
  2130. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt32)];
  2131. }
  2132. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  2133. [self enumerateKeysAndBoolsUsingBlock:^(uint32_t key, BOOL value, BOOL *stop) {
  2134. #pragma unused(stop)
  2135. block([NSString stringWithFormat:@"%u", key], (value ? @"true" : @"false"));
  2136. }];
  2137. }
  2138. - (BOOL)getBool:(nullable BOOL *)value forKey:(uint32_t)key {
  2139. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2140. if (wrapped && value) {
  2141. *value = [wrapped boolValue];
  2142. }
  2143. return (wrapped != NULL);
  2144. }
  2145. - (void)addEntriesFromDictionary:(GPBUInt32BoolDictionary *)otherDictionary {
  2146. if (otherDictionary) {
  2147. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2148. if (_autocreator) {
  2149. GPBAutocreatedDictionaryModified(_autocreator, self);
  2150. }
  2151. }
  2152. }
  2153. - (void)setBool:(BOOL)value forKey:(uint32_t)key {
  2154. [_dictionary setObject:@(value) forKey:@(key)];
  2155. if (_autocreator) {
  2156. GPBAutocreatedDictionaryModified(_autocreator, self);
  2157. }
  2158. }
  2159. - (void)removeBoolForKey:(uint32_t)aKey {
  2160. [_dictionary removeObjectForKey:@(aKey)];
  2161. }
  2162. - (void)removeAll {
  2163. [_dictionary removeAllObjects];
  2164. }
  2165. @end
  2166. #pragma mark - UInt32 -> Float
  2167. @implementation GPBUInt32FloatDictionary {
  2168. @package
  2169. NSMutableDictionary *_dictionary;
  2170. }
  2171. - (instancetype)init {
  2172. return [self initWithFloats:NULL forKeys:NULL count:0];
  2173. }
  2174. - (instancetype)initWithFloats:(const float [])values
  2175. forKeys:(const uint32_t [])keys
  2176. count:(NSUInteger)count {
  2177. self = [super init];
  2178. if (self) {
  2179. _dictionary = [[NSMutableDictionary alloc] init];
  2180. if (count && values && keys) {
  2181. for (NSUInteger i = 0; i < count; ++i) {
  2182. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2183. }
  2184. }
  2185. }
  2186. return self;
  2187. }
  2188. - (instancetype)initWithDictionary:(GPBUInt32FloatDictionary *)dictionary {
  2189. self = [self initWithFloats:NULL forKeys:NULL count:0];
  2190. if (self) {
  2191. if (dictionary) {
  2192. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2193. }
  2194. }
  2195. return self;
  2196. }
  2197. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2198. #pragma unused(numItems)
  2199. return [self initWithFloats:NULL forKeys:NULL count:0];
  2200. }
  2201. - (void)dealloc {
  2202. NSAssert(!_autocreator,
  2203. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2204. [self class], _autocreator);
  2205. [_dictionary release];
  2206. [super dealloc];
  2207. }
  2208. - (instancetype)copyWithZone:(NSZone *)zone {
  2209. return [[GPBUInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  2210. }
  2211. - (BOOL)isEqual:(id)other {
  2212. if (self == other) {
  2213. return YES;
  2214. }
  2215. if (![other isKindOfClass:[GPBUInt32FloatDictionary class]]) {
  2216. return NO;
  2217. }
  2218. GPBUInt32FloatDictionary *otherDictionary = other;
  2219. return [_dictionary isEqual:otherDictionary->_dictionary];
  2220. }
  2221. - (NSUInteger)hash {
  2222. return _dictionary.count;
  2223. }
  2224. - (NSString *)description {
  2225. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2226. }
  2227. - (NSUInteger)count {
  2228. return _dictionary.count;
  2229. }
  2230. - (void)enumerateKeysAndFloatsUsingBlock:
  2231. (void (NS_NOESCAPE ^)(uint32_t key, float value, BOOL *stop))block {
  2232. BOOL stop = NO;
  2233. NSDictionary *internal = _dictionary;
  2234. NSEnumerator *keys = [internal keyEnumerator];
  2235. NSNumber *aKey;
  2236. while ((aKey = [keys nextObject])) {
  2237. NSNumber *aValue = internal[aKey];
  2238. block([aKey unsignedIntValue], [aValue floatValue], &stop);
  2239. if (stop) {
  2240. break;
  2241. }
  2242. }
  2243. }
  2244. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2245. NSDictionary *internal = _dictionary;
  2246. NSUInteger count = internal.count;
  2247. if (count == 0) {
  2248. return 0;
  2249. }
  2250. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2251. GPBDataType keyDataType = field.mapKeyDataType;
  2252. size_t result = 0;
  2253. NSEnumerator *keys = [internal keyEnumerator];
  2254. NSNumber *aKey;
  2255. while ((aKey = [keys nextObject])) {
  2256. NSNumber *aValue = internal[aKey];
  2257. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2258. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  2259. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2260. }
  2261. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2262. result += tagSize * count;
  2263. return result;
  2264. }
  2265. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2266. asField:(GPBFieldDescriptor *)field {
  2267. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2268. GPBDataType keyDataType = field.mapKeyDataType;
  2269. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2270. NSDictionary *internal = _dictionary;
  2271. NSEnumerator *keys = [internal keyEnumerator];
  2272. NSNumber *aKey;
  2273. while ((aKey = [keys nextObject])) {
  2274. NSNumber *aValue = internal[aKey];
  2275. [outputStream writeInt32NoTag:tag];
  2276. // Write the size of the message.
  2277. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2278. float unwrappedValue = [aValue floatValue];
  2279. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2280. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2281. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2282. // Write the fields.
  2283. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2284. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2285. }
  2286. }
  2287. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2288. forGPBGenericValueKey:(GPBGenericValue *)key {
  2289. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt32)];
  2290. }
  2291. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  2292. [self enumerateKeysAndFloatsUsingBlock:^(uint32_t key, float value, BOOL *stop) {
  2293. #pragma unused(stop)
  2294. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  2295. }];
  2296. }
  2297. - (BOOL)getFloat:(nullable float *)value forKey:(uint32_t)key {
  2298. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2299. if (wrapped && value) {
  2300. *value = [wrapped floatValue];
  2301. }
  2302. return (wrapped != NULL);
  2303. }
  2304. - (void)addEntriesFromDictionary:(GPBUInt32FloatDictionary *)otherDictionary {
  2305. if (otherDictionary) {
  2306. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2307. if (_autocreator) {
  2308. GPBAutocreatedDictionaryModified(_autocreator, self);
  2309. }
  2310. }
  2311. }
  2312. - (void)setFloat:(float)value forKey:(uint32_t)key {
  2313. [_dictionary setObject:@(value) forKey:@(key)];
  2314. if (_autocreator) {
  2315. GPBAutocreatedDictionaryModified(_autocreator, self);
  2316. }
  2317. }
  2318. - (void)removeFloatForKey:(uint32_t)aKey {
  2319. [_dictionary removeObjectForKey:@(aKey)];
  2320. }
  2321. - (void)removeAll {
  2322. [_dictionary removeAllObjects];
  2323. }
  2324. @end
  2325. #pragma mark - UInt32 -> Double
  2326. @implementation GPBUInt32DoubleDictionary {
  2327. @package
  2328. NSMutableDictionary *_dictionary;
  2329. }
  2330. - (instancetype)init {
  2331. return [self initWithDoubles:NULL forKeys:NULL count:0];
  2332. }
  2333. - (instancetype)initWithDoubles:(const double [])values
  2334. forKeys:(const uint32_t [])keys
  2335. count:(NSUInteger)count {
  2336. self = [super init];
  2337. if (self) {
  2338. _dictionary = [[NSMutableDictionary alloc] init];
  2339. if (count && values && keys) {
  2340. for (NSUInteger i = 0; i < count; ++i) {
  2341. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2342. }
  2343. }
  2344. }
  2345. return self;
  2346. }
  2347. - (instancetype)initWithDictionary:(GPBUInt32DoubleDictionary *)dictionary {
  2348. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  2349. if (self) {
  2350. if (dictionary) {
  2351. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2352. }
  2353. }
  2354. return self;
  2355. }
  2356. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2357. #pragma unused(numItems)
  2358. return [self initWithDoubles:NULL forKeys:NULL count:0];
  2359. }
  2360. - (void)dealloc {
  2361. NSAssert(!_autocreator,
  2362. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2363. [self class], _autocreator);
  2364. [_dictionary release];
  2365. [super dealloc];
  2366. }
  2367. - (instancetype)copyWithZone:(NSZone *)zone {
  2368. return [[GPBUInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  2369. }
  2370. - (BOOL)isEqual:(id)other {
  2371. if (self == other) {
  2372. return YES;
  2373. }
  2374. if (![other isKindOfClass:[GPBUInt32DoubleDictionary class]]) {
  2375. return NO;
  2376. }
  2377. GPBUInt32DoubleDictionary *otherDictionary = other;
  2378. return [_dictionary isEqual:otherDictionary->_dictionary];
  2379. }
  2380. - (NSUInteger)hash {
  2381. return _dictionary.count;
  2382. }
  2383. - (NSString *)description {
  2384. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2385. }
  2386. - (NSUInteger)count {
  2387. return _dictionary.count;
  2388. }
  2389. - (void)enumerateKeysAndDoublesUsingBlock:
  2390. (void (NS_NOESCAPE ^)(uint32_t key, double value, BOOL *stop))block {
  2391. BOOL stop = NO;
  2392. NSDictionary *internal = _dictionary;
  2393. NSEnumerator *keys = [internal keyEnumerator];
  2394. NSNumber *aKey;
  2395. while ((aKey = [keys nextObject])) {
  2396. NSNumber *aValue = internal[aKey];
  2397. block([aKey unsignedIntValue], [aValue doubleValue], &stop);
  2398. if (stop) {
  2399. break;
  2400. }
  2401. }
  2402. }
  2403. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2404. NSDictionary *internal = _dictionary;
  2405. NSUInteger count = internal.count;
  2406. if (count == 0) {
  2407. return 0;
  2408. }
  2409. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2410. GPBDataType keyDataType = field.mapKeyDataType;
  2411. size_t result = 0;
  2412. NSEnumerator *keys = [internal keyEnumerator];
  2413. NSNumber *aKey;
  2414. while ((aKey = [keys nextObject])) {
  2415. NSNumber *aValue = internal[aKey];
  2416. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2417. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  2418. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2419. }
  2420. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2421. result += tagSize * count;
  2422. return result;
  2423. }
  2424. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2425. asField:(GPBFieldDescriptor *)field {
  2426. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2427. GPBDataType keyDataType = field.mapKeyDataType;
  2428. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2429. NSDictionary *internal = _dictionary;
  2430. NSEnumerator *keys = [internal keyEnumerator];
  2431. NSNumber *aKey;
  2432. while ((aKey = [keys nextObject])) {
  2433. NSNumber *aValue = internal[aKey];
  2434. [outputStream writeInt32NoTag:tag];
  2435. // Write the size of the message.
  2436. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2437. double unwrappedValue = [aValue doubleValue];
  2438. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2439. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2440. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2441. // Write the fields.
  2442. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2443. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2444. }
  2445. }
  2446. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2447. forGPBGenericValueKey:(GPBGenericValue *)key {
  2448. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt32)];
  2449. }
  2450. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  2451. [self enumerateKeysAndDoublesUsingBlock:^(uint32_t key, double value, BOOL *stop) {
  2452. #pragma unused(stop)
  2453. block([NSString stringWithFormat:@"%u", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  2454. }];
  2455. }
  2456. - (BOOL)getDouble:(nullable double *)value forKey:(uint32_t)key {
  2457. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2458. if (wrapped && value) {
  2459. *value = [wrapped doubleValue];
  2460. }
  2461. return (wrapped != NULL);
  2462. }
  2463. - (void)addEntriesFromDictionary:(GPBUInt32DoubleDictionary *)otherDictionary {
  2464. if (otherDictionary) {
  2465. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2466. if (_autocreator) {
  2467. GPBAutocreatedDictionaryModified(_autocreator, self);
  2468. }
  2469. }
  2470. }
  2471. - (void)setDouble:(double)value forKey:(uint32_t)key {
  2472. [_dictionary setObject:@(value) forKey:@(key)];
  2473. if (_autocreator) {
  2474. GPBAutocreatedDictionaryModified(_autocreator, self);
  2475. }
  2476. }
  2477. - (void)removeDoubleForKey:(uint32_t)aKey {
  2478. [_dictionary removeObjectForKey:@(aKey)];
  2479. }
  2480. - (void)removeAll {
  2481. [_dictionary removeAllObjects];
  2482. }
  2483. @end
  2484. #pragma mark - UInt32 -> Enum
  2485. @implementation GPBUInt32EnumDictionary {
  2486. @package
  2487. NSMutableDictionary *_dictionary;
  2488. GPBEnumValidationFunc _validationFunc;
  2489. }
  2490. @synthesize validationFunc = _validationFunc;
  2491. - (instancetype)init {
  2492. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  2493. }
  2494. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  2495. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2496. }
  2497. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2498. rawValues:(const int32_t [])rawValues
  2499. forKeys:(const uint32_t [])keys
  2500. count:(NSUInteger)count {
  2501. self = [super init];
  2502. if (self) {
  2503. _dictionary = [[NSMutableDictionary alloc] init];
  2504. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  2505. if (count && rawValues && keys) {
  2506. for (NSUInteger i = 0; i < count; ++i) {
  2507. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  2508. }
  2509. }
  2510. }
  2511. return self;
  2512. }
  2513. - (instancetype)initWithDictionary:(GPBUInt32EnumDictionary *)dictionary {
  2514. self = [self initWithValidationFunction:dictionary.validationFunc
  2515. rawValues:NULL
  2516. forKeys:NULL
  2517. count:0];
  2518. if (self) {
  2519. if (dictionary) {
  2520. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2521. }
  2522. }
  2523. return self;
  2524. }
  2525. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  2526. capacity:(NSUInteger)numItems {
  2527. #pragma unused(numItems)
  2528. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  2529. }
  2530. - (void)dealloc {
  2531. NSAssert(!_autocreator,
  2532. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2533. [self class], _autocreator);
  2534. [_dictionary release];
  2535. [super dealloc];
  2536. }
  2537. - (instancetype)copyWithZone:(NSZone *)zone {
  2538. return [[GPBUInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  2539. }
  2540. - (BOOL)isEqual:(id)other {
  2541. if (self == other) {
  2542. return YES;
  2543. }
  2544. if (![other isKindOfClass:[GPBUInt32EnumDictionary class]]) {
  2545. return NO;
  2546. }
  2547. GPBUInt32EnumDictionary *otherDictionary = other;
  2548. return [_dictionary isEqual:otherDictionary->_dictionary];
  2549. }
  2550. - (NSUInteger)hash {
  2551. return _dictionary.count;
  2552. }
  2553. - (NSString *)description {
  2554. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2555. }
  2556. - (NSUInteger)count {
  2557. return _dictionary.count;
  2558. }
  2559. - (void)enumerateKeysAndRawValuesUsingBlock:
  2560. (void (NS_NOESCAPE ^)(uint32_t key, int32_t value, BOOL *stop))block {
  2561. BOOL stop = NO;
  2562. NSDictionary *internal = _dictionary;
  2563. NSEnumerator *keys = [internal keyEnumerator];
  2564. NSNumber *aKey;
  2565. while ((aKey = [keys nextObject])) {
  2566. NSNumber *aValue = internal[aKey];
  2567. block([aKey unsignedIntValue], [aValue intValue], &stop);
  2568. if (stop) {
  2569. break;
  2570. }
  2571. }
  2572. }
  2573. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2574. NSDictionary *internal = _dictionary;
  2575. NSUInteger count = internal.count;
  2576. if (count == 0) {
  2577. return 0;
  2578. }
  2579. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2580. GPBDataType keyDataType = field.mapKeyDataType;
  2581. size_t result = 0;
  2582. NSEnumerator *keys = [internal keyEnumerator];
  2583. NSNumber *aKey;
  2584. while ((aKey = [keys nextObject])) {
  2585. NSNumber *aValue = internal[aKey];
  2586. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2587. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  2588. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2589. }
  2590. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2591. result += tagSize * count;
  2592. return result;
  2593. }
  2594. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2595. asField:(GPBFieldDescriptor *)field {
  2596. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2597. GPBDataType keyDataType = field.mapKeyDataType;
  2598. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2599. NSDictionary *internal = _dictionary;
  2600. NSEnumerator *keys = [internal keyEnumerator];
  2601. NSNumber *aKey;
  2602. while ((aKey = [keys nextObject])) {
  2603. NSNumber *aValue = internal[aKey];
  2604. [outputStream writeInt32NoTag:tag];
  2605. // Write the size of the message.
  2606. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2607. int32_t unwrappedValue = [aValue intValue];
  2608. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2609. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2610. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2611. // Write the fields.
  2612. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2613. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2614. }
  2615. }
  2616. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  2617. forKey:(GPBGenericValue *)key
  2618. keyDataType:(GPBDataType)keyDataType {
  2619. size_t msgSize = ComputeDictUInt32FieldSize(key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2620. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  2621. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  2622. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  2623. WriteDictUInt32Field(outputStream, key->valueUInt32, kMapKeyFieldNumber, keyDataType);
  2624. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  2625. [outputStream release];
  2626. return data;
  2627. }
  2628. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2629. forGPBGenericValueKey:(GPBGenericValue *)key {
  2630. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt32)];
  2631. }
  2632. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  2633. [self enumerateKeysAndRawValuesUsingBlock:^(uint32_t key, int32_t value, BOOL *stop) {
  2634. #pragma unused(stop)
  2635. block([NSString stringWithFormat:@"%u", key], @(value));
  2636. }];
  2637. }
  2638. - (BOOL)getEnum:(int32_t *)value forKey:(uint32_t)key {
  2639. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2640. if (wrapped && value) {
  2641. int32_t result = [wrapped intValue];
  2642. if (!_validationFunc(result)) {
  2643. result = kGPBUnrecognizedEnumeratorValue;
  2644. }
  2645. *value = result;
  2646. }
  2647. return (wrapped != NULL);
  2648. }
  2649. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(uint32_t)key {
  2650. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  2651. if (wrapped && rawValue) {
  2652. *rawValue = [wrapped intValue];
  2653. }
  2654. return (wrapped != NULL);
  2655. }
  2656. - (void)enumerateKeysAndEnumsUsingBlock:
  2657. (void (NS_NOESCAPE ^)(uint32_t key, int32_t value, BOOL *stop))block {
  2658. GPBEnumValidationFunc func = _validationFunc;
  2659. BOOL stop = NO;
  2660. NSEnumerator *keys = [_dictionary keyEnumerator];
  2661. NSNumber *aKey;
  2662. while ((aKey = [keys nextObject])) {
  2663. NSNumber *aValue = _dictionary[aKey];
  2664. int32_t unwrapped = [aValue intValue];
  2665. if (!func(unwrapped)) {
  2666. unwrapped = kGPBUnrecognizedEnumeratorValue;
  2667. }
  2668. block([aKey unsignedIntValue], unwrapped, &stop);
  2669. if (stop) {
  2670. break;
  2671. }
  2672. }
  2673. }
  2674. - (void)addRawEntriesFromDictionary:(GPBUInt32EnumDictionary *)otherDictionary {
  2675. if (otherDictionary) {
  2676. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2677. if (_autocreator) {
  2678. GPBAutocreatedDictionaryModified(_autocreator, self);
  2679. }
  2680. }
  2681. }
  2682. - (void)setRawValue:(int32_t)value forKey:(uint32_t)key {
  2683. [_dictionary setObject:@(value) forKey:@(key)];
  2684. if (_autocreator) {
  2685. GPBAutocreatedDictionaryModified(_autocreator, self);
  2686. }
  2687. }
  2688. - (void)removeEnumForKey:(uint32_t)aKey {
  2689. [_dictionary removeObjectForKey:@(aKey)];
  2690. }
  2691. - (void)removeAll {
  2692. [_dictionary removeAllObjects];
  2693. }
  2694. - (void)setEnum:(int32_t)value forKey:(uint32_t)key {
  2695. if (!_validationFunc(value)) {
  2696. [NSException raise:NSInvalidArgumentException
  2697. format:@"GPBUInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  2698. value];
  2699. }
  2700. [_dictionary setObject:@(value) forKey:@(key)];
  2701. if (_autocreator) {
  2702. GPBAutocreatedDictionaryModified(_autocreator, self);
  2703. }
  2704. }
  2705. @end
  2706. #pragma mark - UInt32 -> Object
  2707. @implementation GPBUInt32ObjectDictionary {
  2708. @package
  2709. NSMutableDictionary *_dictionary;
  2710. }
  2711. - (instancetype)init {
  2712. return [self initWithObjects:NULL forKeys:NULL count:0];
  2713. }
  2714. - (instancetype)initWithObjects:(const id [])objects
  2715. forKeys:(const uint32_t [])keys
  2716. count:(NSUInteger)count {
  2717. self = [super init];
  2718. if (self) {
  2719. _dictionary = [[NSMutableDictionary alloc] init];
  2720. if (count && objects && keys) {
  2721. for (NSUInteger i = 0; i < count; ++i) {
  2722. if (!objects[i]) {
  2723. [NSException raise:NSInvalidArgumentException
  2724. format:@"Attempting to add nil object to a Dictionary"];
  2725. }
  2726. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  2727. }
  2728. }
  2729. }
  2730. return self;
  2731. }
  2732. - (instancetype)initWithDictionary:(GPBUInt32ObjectDictionary *)dictionary {
  2733. self = [self initWithObjects:NULL forKeys:NULL count:0];
  2734. if (self) {
  2735. if (dictionary) {
  2736. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2737. }
  2738. }
  2739. return self;
  2740. }
  2741. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2742. #pragma unused(numItems)
  2743. return [self initWithObjects:NULL forKeys:NULL count:0];
  2744. }
  2745. - (void)dealloc {
  2746. NSAssert(!_autocreator,
  2747. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2748. [self class], _autocreator);
  2749. [_dictionary release];
  2750. [super dealloc];
  2751. }
  2752. - (instancetype)copyWithZone:(NSZone *)zone {
  2753. return [[GPBUInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  2754. }
  2755. - (BOOL)isEqual:(id)other {
  2756. if (self == other) {
  2757. return YES;
  2758. }
  2759. if (![other isKindOfClass:[GPBUInt32ObjectDictionary class]]) {
  2760. return NO;
  2761. }
  2762. GPBUInt32ObjectDictionary *otherDictionary = other;
  2763. return [_dictionary isEqual:otherDictionary->_dictionary];
  2764. }
  2765. - (NSUInteger)hash {
  2766. return _dictionary.count;
  2767. }
  2768. - (NSString *)description {
  2769. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2770. }
  2771. - (NSUInteger)count {
  2772. return _dictionary.count;
  2773. }
  2774. - (void)enumerateKeysAndObjectsUsingBlock:
  2775. (void (NS_NOESCAPE ^)(uint32_t key, id object, BOOL *stop))block {
  2776. BOOL stop = NO;
  2777. NSDictionary *internal = _dictionary;
  2778. NSEnumerator *keys = [internal keyEnumerator];
  2779. NSNumber *aKey;
  2780. while ((aKey = [keys nextObject])) {
  2781. id aObject = internal[aKey];
  2782. block([aKey unsignedIntValue], aObject, &stop);
  2783. if (stop) {
  2784. break;
  2785. }
  2786. }
  2787. }
  2788. - (BOOL)isInitialized {
  2789. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  2790. if (!msg.initialized) {
  2791. return NO;
  2792. }
  2793. }
  2794. return YES;
  2795. }
  2796. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  2797. GPBUInt32ObjectDictionary *newDict =
  2798. [[GPBUInt32ObjectDictionary alloc] init];
  2799. NSEnumerator *keys = [_dictionary keyEnumerator];
  2800. id aKey;
  2801. NSMutableDictionary *internalDict = newDict->_dictionary;
  2802. while ((aKey = [keys nextObject])) {
  2803. GPBMessage *msg = _dictionary[aKey];
  2804. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  2805. [internalDict setObject:copiedMsg forKey:aKey];
  2806. [copiedMsg release];
  2807. }
  2808. return newDict;
  2809. }
  2810. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2811. NSDictionary *internal = _dictionary;
  2812. NSUInteger count = internal.count;
  2813. if (count == 0) {
  2814. return 0;
  2815. }
  2816. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2817. GPBDataType keyDataType = field.mapKeyDataType;
  2818. size_t result = 0;
  2819. NSEnumerator *keys = [internal keyEnumerator];
  2820. NSNumber *aKey;
  2821. while ((aKey = [keys nextObject])) {
  2822. id aObject = internal[aKey];
  2823. size_t msgSize = ComputeDictUInt32FieldSize([aKey unsignedIntValue], kMapKeyFieldNumber, keyDataType);
  2824. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  2825. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2826. }
  2827. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2828. result += tagSize * count;
  2829. return result;
  2830. }
  2831. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2832. asField:(GPBFieldDescriptor *)field {
  2833. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2834. GPBDataType keyDataType = field.mapKeyDataType;
  2835. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  2836. NSDictionary *internal = _dictionary;
  2837. NSEnumerator *keys = [internal keyEnumerator];
  2838. NSNumber *aKey;
  2839. while ((aKey = [keys nextObject])) {
  2840. id aObject = internal[aKey];
  2841. [outputStream writeInt32NoTag:tag];
  2842. // Write the size of the message.
  2843. uint32_t unwrappedKey = [aKey unsignedIntValue];
  2844. id unwrappedValue = aObject;
  2845. size_t msgSize = ComputeDictUInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2846. msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  2847. [outputStream writeInt32NoTag:(int32_t)msgSize];
  2848. // Write the fields.
  2849. WriteDictUInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  2850. WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  2851. }
  2852. }
  2853. - (void)setGPBGenericValue:(GPBGenericValue *)value
  2854. forGPBGenericValueKey:(GPBGenericValue *)key {
  2855. [_dictionary setObject:value->valueString forKey:@(key->valueUInt32)];
  2856. }
  2857. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  2858. [self enumerateKeysAndObjectsUsingBlock:^(uint32_t key, id object, BOOL *stop) {
  2859. #pragma unused(stop)
  2860. block([NSString stringWithFormat:@"%u", key], object);
  2861. }];
  2862. }
  2863. - (id)objectForKey:(uint32_t)key {
  2864. id result = [_dictionary objectForKey:@(key)];
  2865. return result;
  2866. }
  2867. - (void)addEntriesFromDictionary:(GPBUInt32ObjectDictionary *)otherDictionary {
  2868. if (otherDictionary) {
  2869. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  2870. if (_autocreator) {
  2871. GPBAutocreatedDictionaryModified(_autocreator, self);
  2872. }
  2873. }
  2874. }
  2875. - (void)setObject:(id)object forKey:(uint32_t)key {
  2876. if (!object) {
  2877. [NSException raise:NSInvalidArgumentException
  2878. format:@"Attempting to add nil object to a Dictionary"];
  2879. }
  2880. [_dictionary setObject:object forKey:@(key)];
  2881. if (_autocreator) {
  2882. GPBAutocreatedDictionaryModified(_autocreator, self);
  2883. }
  2884. }
  2885. - (void)removeObjectForKey:(uint32_t)aKey {
  2886. [_dictionary removeObjectForKey:@(aKey)];
  2887. }
  2888. - (void)removeAll {
  2889. [_dictionary removeAllObjects];
  2890. }
  2891. @end
  2892. // clang-format on
  2893. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int32, int32_t)
  2894. // This block of code is generated, do not edit it directly.
  2895. // clang-format off
  2896. #pragma mark - Int32 -> UInt32
  2897. @implementation GPBInt32UInt32Dictionary {
  2898. @package
  2899. NSMutableDictionary *_dictionary;
  2900. }
  2901. - (instancetype)init {
  2902. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  2903. }
  2904. - (instancetype)initWithUInt32s:(const uint32_t [])values
  2905. forKeys:(const int32_t [])keys
  2906. count:(NSUInteger)count {
  2907. self = [super init];
  2908. if (self) {
  2909. _dictionary = [[NSMutableDictionary alloc] init];
  2910. if (count && values && keys) {
  2911. for (NSUInteger i = 0; i < count; ++i) {
  2912. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  2913. }
  2914. }
  2915. }
  2916. return self;
  2917. }
  2918. - (instancetype)initWithDictionary:(GPBInt32UInt32Dictionary *)dictionary {
  2919. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  2920. if (self) {
  2921. if (dictionary) {
  2922. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  2923. }
  2924. }
  2925. return self;
  2926. }
  2927. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  2928. #pragma unused(numItems)
  2929. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  2930. }
  2931. - (void)dealloc {
  2932. NSAssert(!_autocreator,
  2933. @"%@: Autocreator must be cleared before release, autocreator: %@",
  2934. [self class], _autocreator);
  2935. [_dictionary release];
  2936. [super dealloc];
  2937. }
  2938. - (instancetype)copyWithZone:(NSZone *)zone {
  2939. return [[GPBInt32UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  2940. }
  2941. - (BOOL)isEqual:(id)other {
  2942. if (self == other) {
  2943. return YES;
  2944. }
  2945. if (![other isKindOfClass:[GPBInt32UInt32Dictionary class]]) {
  2946. return NO;
  2947. }
  2948. GPBInt32UInt32Dictionary *otherDictionary = other;
  2949. return [_dictionary isEqual:otherDictionary->_dictionary];
  2950. }
  2951. - (NSUInteger)hash {
  2952. return _dictionary.count;
  2953. }
  2954. - (NSString *)description {
  2955. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  2956. }
  2957. - (NSUInteger)count {
  2958. return _dictionary.count;
  2959. }
  2960. - (void)enumerateKeysAndUInt32sUsingBlock:
  2961. (void (NS_NOESCAPE ^)(int32_t key, uint32_t value, BOOL *stop))block {
  2962. BOOL stop = NO;
  2963. NSDictionary *internal = _dictionary;
  2964. NSEnumerator *keys = [internal keyEnumerator];
  2965. NSNumber *aKey;
  2966. while ((aKey = [keys nextObject])) {
  2967. NSNumber *aValue = internal[aKey];
  2968. block([aKey intValue], [aValue unsignedIntValue], &stop);
  2969. if (stop) {
  2970. break;
  2971. }
  2972. }
  2973. }
  2974. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  2975. NSDictionary *internal = _dictionary;
  2976. NSUInteger count = internal.count;
  2977. if (count == 0) {
  2978. return 0;
  2979. }
  2980. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2981. GPBDataType keyDataType = field.mapKeyDataType;
  2982. size_t result = 0;
  2983. NSEnumerator *keys = [internal keyEnumerator];
  2984. NSNumber *aKey;
  2985. while ((aKey = [keys nextObject])) {
  2986. NSNumber *aValue = internal[aKey];
  2987. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  2988. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  2989. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  2990. }
  2991. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  2992. result += tagSize * count;
  2993. return result;
  2994. }
  2995. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  2996. asField:(GPBFieldDescriptor *)field {
  2997. GPBDataType valueDataType = GPBGetFieldDataType(field);
  2998. GPBDataType keyDataType = field.mapKeyDataType;
  2999. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3000. NSDictionary *internal = _dictionary;
  3001. NSEnumerator *keys = [internal keyEnumerator];
  3002. NSNumber *aKey;
  3003. while ((aKey = [keys nextObject])) {
  3004. NSNumber *aValue = internal[aKey];
  3005. [outputStream writeInt32NoTag:tag];
  3006. // Write the size of the message.
  3007. int32_t unwrappedKey = [aKey intValue];
  3008. uint32_t unwrappedValue = [aValue unsignedIntValue];
  3009. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3010. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3011. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3012. // Write the fields.
  3013. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3014. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3015. }
  3016. }
  3017. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3018. forGPBGenericValueKey:(GPBGenericValue *)key {
  3019. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt32)];
  3020. }
  3021. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  3022. [self enumerateKeysAndUInt32sUsingBlock:^(int32_t key, uint32_t value, BOOL *stop) {
  3023. #pragma unused(stop)
  3024. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%u", value]);
  3025. }];
  3026. }
  3027. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(int32_t)key {
  3028. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3029. if (wrapped && value) {
  3030. *value = [wrapped unsignedIntValue];
  3031. }
  3032. return (wrapped != NULL);
  3033. }
  3034. - (void)addEntriesFromDictionary:(GPBInt32UInt32Dictionary *)otherDictionary {
  3035. if (otherDictionary) {
  3036. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3037. if (_autocreator) {
  3038. GPBAutocreatedDictionaryModified(_autocreator, self);
  3039. }
  3040. }
  3041. }
  3042. - (void)setUInt32:(uint32_t)value forKey:(int32_t)key {
  3043. [_dictionary setObject:@(value) forKey:@(key)];
  3044. if (_autocreator) {
  3045. GPBAutocreatedDictionaryModified(_autocreator, self);
  3046. }
  3047. }
  3048. - (void)removeUInt32ForKey:(int32_t)aKey {
  3049. [_dictionary removeObjectForKey:@(aKey)];
  3050. }
  3051. - (void)removeAll {
  3052. [_dictionary removeAllObjects];
  3053. }
  3054. @end
  3055. #pragma mark - Int32 -> Int32
  3056. @implementation GPBInt32Int32Dictionary {
  3057. @package
  3058. NSMutableDictionary *_dictionary;
  3059. }
  3060. - (instancetype)init {
  3061. return [self initWithInt32s:NULL forKeys:NULL count:0];
  3062. }
  3063. - (instancetype)initWithInt32s:(const int32_t [])values
  3064. forKeys:(const int32_t [])keys
  3065. count:(NSUInteger)count {
  3066. self = [super init];
  3067. if (self) {
  3068. _dictionary = [[NSMutableDictionary alloc] init];
  3069. if (count && values && keys) {
  3070. for (NSUInteger i = 0; i < count; ++i) {
  3071. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3072. }
  3073. }
  3074. }
  3075. return self;
  3076. }
  3077. - (instancetype)initWithDictionary:(GPBInt32Int32Dictionary *)dictionary {
  3078. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  3079. if (self) {
  3080. if (dictionary) {
  3081. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3082. }
  3083. }
  3084. return self;
  3085. }
  3086. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3087. #pragma unused(numItems)
  3088. return [self initWithInt32s:NULL forKeys:NULL count:0];
  3089. }
  3090. - (void)dealloc {
  3091. NSAssert(!_autocreator,
  3092. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3093. [self class], _autocreator);
  3094. [_dictionary release];
  3095. [super dealloc];
  3096. }
  3097. - (instancetype)copyWithZone:(NSZone *)zone {
  3098. return [[GPBInt32Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  3099. }
  3100. - (BOOL)isEqual:(id)other {
  3101. if (self == other) {
  3102. return YES;
  3103. }
  3104. if (![other isKindOfClass:[GPBInt32Int32Dictionary class]]) {
  3105. return NO;
  3106. }
  3107. GPBInt32Int32Dictionary *otherDictionary = other;
  3108. return [_dictionary isEqual:otherDictionary->_dictionary];
  3109. }
  3110. - (NSUInteger)hash {
  3111. return _dictionary.count;
  3112. }
  3113. - (NSString *)description {
  3114. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3115. }
  3116. - (NSUInteger)count {
  3117. return _dictionary.count;
  3118. }
  3119. - (void)enumerateKeysAndInt32sUsingBlock:
  3120. (void (NS_NOESCAPE ^)(int32_t key, int32_t value, BOOL *stop))block {
  3121. BOOL stop = NO;
  3122. NSDictionary *internal = _dictionary;
  3123. NSEnumerator *keys = [internal keyEnumerator];
  3124. NSNumber *aKey;
  3125. while ((aKey = [keys nextObject])) {
  3126. NSNumber *aValue = internal[aKey];
  3127. block([aKey intValue], [aValue intValue], &stop);
  3128. if (stop) {
  3129. break;
  3130. }
  3131. }
  3132. }
  3133. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3134. NSDictionary *internal = _dictionary;
  3135. NSUInteger count = internal.count;
  3136. if (count == 0) {
  3137. return 0;
  3138. }
  3139. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3140. GPBDataType keyDataType = field.mapKeyDataType;
  3141. size_t result = 0;
  3142. NSEnumerator *keys = [internal keyEnumerator];
  3143. NSNumber *aKey;
  3144. while ((aKey = [keys nextObject])) {
  3145. NSNumber *aValue = internal[aKey];
  3146. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3147. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  3148. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3149. }
  3150. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3151. result += tagSize * count;
  3152. return result;
  3153. }
  3154. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3155. asField:(GPBFieldDescriptor *)field {
  3156. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3157. GPBDataType keyDataType = field.mapKeyDataType;
  3158. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3159. NSDictionary *internal = _dictionary;
  3160. NSEnumerator *keys = [internal keyEnumerator];
  3161. NSNumber *aKey;
  3162. while ((aKey = [keys nextObject])) {
  3163. NSNumber *aValue = internal[aKey];
  3164. [outputStream writeInt32NoTag:tag];
  3165. // Write the size of the message.
  3166. int32_t unwrappedKey = [aKey intValue];
  3167. int32_t unwrappedValue = [aValue intValue];
  3168. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3169. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3170. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3171. // Write the fields.
  3172. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3173. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3174. }
  3175. }
  3176. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3177. forGPBGenericValueKey:(GPBGenericValue *)key {
  3178. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt32)];
  3179. }
  3180. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  3181. [self enumerateKeysAndInt32sUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  3182. #pragma unused(stop)
  3183. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%d", value]);
  3184. }];
  3185. }
  3186. - (BOOL)getInt32:(nullable int32_t *)value forKey:(int32_t)key {
  3187. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3188. if (wrapped && value) {
  3189. *value = [wrapped intValue];
  3190. }
  3191. return (wrapped != NULL);
  3192. }
  3193. - (void)addEntriesFromDictionary:(GPBInt32Int32Dictionary *)otherDictionary {
  3194. if (otherDictionary) {
  3195. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3196. if (_autocreator) {
  3197. GPBAutocreatedDictionaryModified(_autocreator, self);
  3198. }
  3199. }
  3200. }
  3201. - (void)setInt32:(int32_t)value forKey:(int32_t)key {
  3202. [_dictionary setObject:@(value) forKey:@(key)];
  3203. if (_autocreator) {
  3204. GPBAutocreatedDictionaryModified(_autocreator, self);
  3205. }
  3206. }
  3207. - (void)removeInt32ForKey:(int32_t)aKey {
  3208. [_dictionary removeObjectForKey:@(aKey)];
  3209. }
  3210. - (void)removeAll {
  3211. [_dictionary removeAllObjects];
  3212. }
  3213. @end
  3214. #pragma mark - Int32 -> UInt64
  3215. @implementation GPBInt32UInt64Dictionary {
  3216. @package
  3217. NSMutableDictionary *_dictionary;
  3218. }
  3219. - (instancetype)init {
  3220. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  3221. }
  3222. - (instancetype)initWithUInt64s:(const uint64_t [])values
  3223. forKeys:(const int32_t [])keys
  3224. count:(NSUInteger)count {
  3225. self = [super init];
  3226. if (self) {
  3227. _dictionary = [[NSMutableDictionary alloc] init];
  3228. if (count && values && keys) {
  3229. for (NSUInteger i = 0; i < count; ++i) {
  3230. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3231. }
  3232. }
  3233. }
  3234. return self;
  3235. }
  3236. - (instancetype)initWithDictionary:(GPBInt32UInt64Dictionary *)dictionary {
  3237. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  3238. if (self) {
  3239. if (dictionary) {
  3240. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3241. }
  3242. }
  3243. return self;
  3244. }
  3245. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3246. #pragma unused(numItems)
  3247. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  3248. }
  3249. - (void)dealloc {
  3250. NSAssert(!_autocreator,
  3251. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3252. [self class], _autocreator);
  3253. [_dictionary release];
  3254. [super dealloc];
  3255. }
  3256. - (instancetype)copyWithZone:(NSZone *)zone {
  3257. return [[GPBInt32UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  3258. }
  3259. - (BOOL)isEqual:(id)other {
  3260. if (self == other) {
  3261. return YES;
  3262. }
  3263. if (![other isKindOfClass:[GPBInt32UInt64Dictionary class]]) {
  3264. return NO;
  3265. }
  3266. GPBInt32UInt64Dictionary *otherDictionary = other;
  3267. return [_dictionary isEqual:otherDictionary->_dictionary];
  3268. }
  3269. - (NSUInteger)hash {
  3270. return _dictionary.count;
  3271. }
  3272. - (NSString *)description {
  3273. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3274. }
  3275. - (NSUInteger)count {
  3276. return _dictionary.count;
  3277. }
  3278. - (void)enumerateKeysAndUInt64sUsingBlock:
  3279. (void (NS_NOESCAPE ^)(int32_t key, uint64_t value, BOOL *stop))block {
  3280. BOOL stop = NO;
  3281. NSDictionary *internal = _dictionary;
  3282. NSEnumerator *keys = [internal keyEnumerator];
  3283. NSNumber *aKey;
  3284. while ((aKey = [keys nextObject])) {
  3285. NSNumber *aValue = internal[aKey];
  3286. block([aKey intValue], [aValue unsignedLongLongValue], &stop);
  3287. if (stop) {
  3288. break;
  3289. }
  3290. }
  3291. }
  3292. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3293. NSDictionary *internal = _dictionary;
  3294. NSUInteger count = internal.count;
  3295. if (count == 0) {
  3296. return 0;
  3297. }
  3298. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3299. GPBDataType keyDataType = field.mapKeyDataType;
  3300. size_t result = 0;
  3301. NSEnumerator *keys = [internal keyEnumerator];
  3302. NSNumber *aKey;
  3303. while ((aKey = [keys nextObject])) {
  3304. NSNumber *aValue = internal[aKey];
  3305. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3306. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  3307. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3308. }
  3309. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3310. result += tagSize * count;
  3311. return result;
  3312. }
  3313. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3314. asField:(GPBFieldDescriptor *)field {
  3315. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3316. GPBDataType keyDataType = field.mapKeyDataType;
  3317. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3318. NSDictionary *internal = _dictionary;
  3319. NSEnumerator *keys = [internal keyEnumerator];
  3320. NSNumber *aKey;
  3321. while ((aKey = [keys nextObject])) {
  3322. NSNumber *aValue = internal[aKey];
  3323. [outputStream writeInt32NoTag:tag];
  3324. // Write the size of the message.
  3325. int32_t unwrappedKey = [aKey intValue];
  3326. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  3327. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3328. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3329. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3330. // Write the fields.
  3331. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3332. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3333. }
  3334. }
  3335. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3336. forGPBGenericValueKey:(GPBGenericValue *)key {
  3337. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt32)];
  3338. }
  3339. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  3340. [self enumerateKeysAndUInt64sUsingBlock:^(int32_t key, uint64_t value, BOOL *stop) {
  3341. #pragma unused(stop)
  3342. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%llu", value]);
  3343. }];
  3344. }
  3345. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(int32_t)key {
  3346. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3347. if (wrapped && value) {
  3348. *value = [wrapped unsignedLongLongValue];
  3349. }
  3350. return (wrapped != NULL);
  3351. }
  3352. - (void)addEntriesFromDictionary:(GPBInt32UInt64Dictionary *)otherDictionary {
  3353. if (otherDictionary) {
  3354. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3355. if (_autocreator) {
  3356. GPBAutocreatedDictionaryModified(_autocreator, self);
  3357. }
  3358. }
  3359. }
  3360. - (void)setUInt64:(uint64_t)value forKey:(int32_t)key {
  3361. [_dictionary setObject:@(value) forKey:@(key)];
  3362. if (_autocreator) {
  3363. GPBAutocreatedDictionaryModified(_autocreator, self);
  3364. }
  3365. }
  3366. - (void)removeUInt64ForKey:(int32_t)aKey {
  3367. [_dictionary removeObjectForKey:@(aKey)];
  3368. }
  3369. - (void)removeAll {
  3370. [_dictionary removeAllObjects];
  3371. }
  3372. @end
  3373. #pragma mark - Int32 -> Int64
  3374. @implementation GPBInt32Int64Dictionary {
  3375. @package
  3376. NSMutableDictionary *_dictionary;
  3377. }
  3378. - (instancetype)init {
  3379. return [self initWithInt64s:NULL forKeys:NULL count:0];
  3380. }
  3381. - (instancetype)initWithInt64s:(const int64_t [])values
  3382. forKeys:(const int32_t [])keys
  3383. count:(NSUInteger)count {
  3384. self = [super init];
  3385. if (self) {
  3386. _dictionary = [[NSMutableDictionary alloc] init];
  3387. if (count && values && keys) {
  3388. for (NSUInteger i = 0; i < count; ++i) {
  3389. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3390. }
  3391. }
  3392. }
  3393. return self;
  3394. }
  3395. - (instancetype)initWithDictionary:(GPBInt32Int64Dictionary *)dictionary {
  3396. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  3397. if (self) {
  3398. if (dictionary) {
  3399. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3400. }
  3401. }
  3402. return self;
  3403. }
  3404. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3405. #pragma unused(numItems)
  3406. return [self initWithInt64s:NULL forKeys:NULL count:0];
  3407. }
  3408. - (void)dealloc {
  3409. NSAssert(!_autocreator,
  3410. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3411. [self class], _autocreator);
  3412. [_dictionary release];
  3413. [super dealloc];
  3414. }
  3415. - (instancetype)copyWithZone:(NSZone *)zone {
  3416. return [[GPBInt32Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  3417. }
  3418. - (BOOL)isEqual:(id)other {
  3419. if (self == other) {
  3420. return YES;
  3421. }
  3422. if (![other isKindOfClass:[GPBInt32Int64Dictionary class]]) {
  3423. return NO;
  3424. }
  3425. GPBInt32Int64Dictionary *otherDictionary = other;
  3426. return [_dictionary isEqual:otherDictionary->_dictionary];
  3427. }
  3428. - (NSUInteger)hash {
  3429. return _dictionary.count;
  3430. }
  3431. - (NSString *)description {
  3432. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3433. }
  3434. - (NSUInteger)count {
  3435. return _dictionary.count;
  3436. }
  3437. - (void)enumerateKeysAndInt64sUsingBlock:
  3438. (void (NS_NOESCAPE ^)(int32_t key, int64_t value, BOOL *stop))block {
  3439. BOOL stop = NO;
  3440. NSDictionary *internal = _dictionary;
  3441. NSEnumerator *keys = [internal keyEnumerator];
  3442. NSNumber *aKey;
  3443. while ((aKey = [keys nextObject])) {
  3444. NSNumber *aValue = internal[aKey];
  3445. block([aKey intValue], [aValue longLongValue], &stop);
  3446. if (stop) {
  3447. break;
  3448. }
  3449. }
  3450. }
  3451. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3452. NSDictionary *internal = _dictionary;
  3453. NSUInteger count = internal.count;
  3454. if (count == 0) {
  3455. return 0;
  3456. }
  3457. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3458. GPBDataType keyDataType = field.mapKeyDataType;
  3459. size_t result = 0;
  3460. NSEnumerator *keys = [internal keyEnumerator];
  3461. NSNumber *aKey;
  3462. while ((aKey = [keys nextObject])) {
  3463. NSNumber *aValue = internal[aKey];
  3464. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3465. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  3466. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3467. }
  3468. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3469. result += tagSize * count;
  3470. return result;
  3471. }
  3472. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3473. asField:(GPBFieldDescriptor *)field {
  3474. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3475. GPBDataType keyDataType = field.mapKeyDataType;
  3476. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3477. NSDictionary *internal = _dictionary;
  3478. NSEnumerator *keys = [internal keyEnumerator];
  3479. NSNumber *aKey;
  3480. while ((aKey = [keys nextObject])) {
  3481. NSNumber *aValue = internal[aKey];
  3482. [outputStream writeInt32NoTag:tag];
  3483. // Write the size of the message.
  3484. int32_t unwrappedKey = [aKey intValue];
  3485. int64_t unwrappedValue = [aValue longLongValue];
  3486. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3487. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3488. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3489. // Write the fields.
  3490. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3491. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3492. }
  3493. }
  3494. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3495. forGPBGenericValueKey:(GPBGenericValue *)key {
  3496. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt32)];
  3497. }
  3498. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  3499. [self enumerateKeysAndInt64sUsingBlock:^(int32_t key, int64_t value, BOOL *stop) {
  3500. #pragma unused(stop)
  3501. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%lld", value]);
  3502. }];
  3503. }
  3504. - (BOOL)getInt64:(nullable int64_t *)value forKey:(int32_t)key {
  3505. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3506. if (wrapped && value) {
  3507. *value = [wrapped longLongValue];
  3508. }
  3509. return (wrapped != NULL);
  3510. }
  3511. - (void)addEntriesFromDictionary:(GPBInt32Int64Dictionary *)otherDictionary {
  3512. if (otherDictionary) {
  3513. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3514. if (_autocreator) {
  3515. GPBAutocreatedDictionaryModified(_autocreator, self);
  3516. }
  3517. }
  3518. }
  3519. - (void)setInt64:(int64_t)value forKey:(int32_t)key {
  3520. [_dictionary setObject:@(value) forKey:@(key)];
  3521. if (_autocreator) {
  3522. GPBAutocreatedDictionaryModified(_autocreator, self);
  3523. }
  3524. }
  3525. - (void)removeInt64ForKey:(int32_t)aKey {
  3526. [_dictionary removeObjectForKey:@(aKey)];
  3527. }
  3528. - (void)removeAll {
  3529. [_dictionary removeAllObjects];
  3530. }
  3531. @end
  3532. #pragma mark - Int32 -> Bool
  3533. @implementation GPBInt32BoolDictionary {
  3534. @package
  3535. NSMutableDictionary *_dictionary;
  3536. }
  3537. - (instancetype)init {
  3538. return [self initWithBools:NULL forKeys:NULL count:0];
  3539. }
  3540. - (instancetype)initWithBools:(const BOOL [])values
  3541. forKeys:(const int32_t [])keys
  3542. count:(NSUInteger)count {
  3543. self = [super init];
  3544. if (self) {
  3545. _dictionary = [[NSMutableDictionary alloc] init];
  3546. if (count && values && keys) {
  3547. for (NSUInteger i = 0; i < count; ++i) {
  3548. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3549. }
  3550. }
  3551. }
  3552. return self;
  3553. }
  3554. - (instancetype)initWithDictionary:(GPBInt32BoolDictionary *)dictionary {
  3555. self = [self initWithBools:NULL forKeys:NULL count:0];
  3556. if (self) {
  3557. if (dictionary) {
  3558. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3559. }
  3560. }
  3561. return self;
  3562. }
  3563. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3564. #pragma unused(numItems)
  3565. return [self initWithBools:NULL forKeys:NULL count:0];
  3566. }
  3567. - (void)dealloc {
  3568. NSAssert(!_autocreator,
  3569. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3570. [self class], _autocreator);
  3571. [_dictionary release];
  3572. [super dealloc];
  3573. }
  3574. - (instancetype)copyWithZone:(NSZone *)zone {
  3575. return [[GPBInt32BoolDictionary allocWithZone:zone] initWithDictionary:self];
  3576. }
  3577. - (BOOL)isEqual:(id)other {
  3578. if (self == other) {
  3579. return YES;
  3580. }
  3581. if (![other isKindOfClass:[GPBInt32BoolDictionary class]]) {
  3582. return NO;
  3583. }
  3584. GPBInt32BoolDictionary *otherDictionary = other;
  3585. return [_dictionary isEqual:otherDictionary->_dictionary];
  3586. }
  3587. - (NSUInteger)hash {
  3588. return _dictionary.count;
  3589. }
  3590. - (NSString *)description {
  3591. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3592. }
  3593. - (NSUInteger)count {
  3594. return _dictionary.count;
  3595. }
  3596. - (void)enumerateKeysAndBoolsUsingBlock:
  3597. (void (NS_NOESCAPE ^)(int32_t key, BOOL value, BOOL *stop))block {
  3598. BOOL stop = NO;
  3599. NSDictionary *internal = _dictionary;
  3600. NSEnumerator *keys = [internal keyEnumerator];
  3601. NSNumber *aKey;
  3602. while ((aKey = [keys nextObject])) {
  3603. NSNumber *aValue = internal[aKey];
  3604. block([aKey intValue], [aValue boolValue], &stop);
  3605. if (stop) {
  3606. break;
  3607. }
  3608. }
  3609. }
  3610. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3611. NSDictionary *internal = _dictionary;
  3612. NSUInteger count = internal.count;
  3613. if (count == 0) {
  3614. return 0;
  3615. }
  3616. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3617. GPBDataType keyDataType = field.mapKeyDataType;
  3618. size_t result = 0;
  3619. NSEnumerator *keys = [internal keyEnumerator];
  3620. NSNumber *aKey;
  3621. while ((aKey = [keys nextObject])) {
  3622. NSNumber *aValue = internal[aKey];
  3623. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3624. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  3625. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3626. }
  3627. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3628. result += tagSize * count;
  3629. return result;
  3630. }
  3631. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3632. asField:(GPBFieldDescriptor *)field {
  3633. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3634. GPBDataType keyDataType = field.mapKeyDataType;
  3635. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3636. NSDictionary *internal = _dictionary;
  3637. NSEnumerator *keys = [internal keyEnumerator];
  3638. NSNumber *aKey;
  3639. while ((aKey = [keys nextObject])) {
  3640. NSNumber *aValue = internal[aKey];
  3641. [outputStream writeInt32NoTag:tag];
  3642. // Write the size of the message.
  3643. int32_t unwrappedKey = [aKey intValue];
  3644. BOOL unwrappedValue = [aValue boolValue];
  3645. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3646. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3647. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3648. // Write the fields.
  3649. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3650. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3651. }
  3652. }
  3653. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3654. forGPBGenericValueKey:(GPBGenericValue *)key {
  3655. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt32)];
  3656. }
  3657. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  3658. [self enumerateKeysAndBoolsUsingBlock:^(int32_t key, BOOL value, BOOL *stop) {
  3659. #pragma unused(stop)
  3660. block([NSString stringWithFormat:@"%d", key], (value ? @"true" : @"false"));
  3661. }];
  3662. }
  3663. - (BOOL)getBool:(nullable BOOL *)value forKey:(int32_t)key {
  3664. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3665. if (wrapped && value) {
  3666. *value = [wrapped boolValue];
  3667. }
  3668. return (wrapped != NULL);
  3669. }
  3670. - (void)addEntriesFromDictionary:(GPBInt32BoolDictionary *)otherDictionary {
  3671. if (otherDictionary) {
  3672. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3673. if (_autocreator) {
  3674. GPBAutocreatedDictionaryModified(_autocreator, self);
  3675. }
  3676. }
  3677. }
  3678. - (void)setBool:(BOOL)value forKey:(int32_t)key {
  3679. [_dictionary setObject:@(value) forKey:@(key)];
  3680. if (_autocreator) {
  3681. GPBAutocreatedDictionaryModified(_autocreator, self);
  3682. }
  3683. }
  3684. - (void)removeBoolForKey:(int32_t)aKey {
  3685. [_dictionary removeObjectForKey:@(aKey)];
  3686. }
  3687. - (void)removeAll {
  3688. [_dictionary removeAllObjects];
  3689. }
  3690. @end
  3691. #pragma mark - Int32 -> Float
  3692. @implementation GPBInt32FloatDictionary {
  3693. @package
  3694. NSMutableDictionary *_dictionary;
  3695. }
  3696. - (instancetype)init {
  3697. return [self initWithFloats:NULL forKeys:NULL count:0];
  3698. }
  3699. - (instancetype)initWithFloats:(const float [])values
  3700. forKeys:(const int32_t [])keys
  3701. count:(NSUInteger)count {
  3702. self = [super init];
  3703. if (self) {
  3704. _dictionary = [[NSMutableDictionary alloc] init];
  3705. if (count && values && keys) {
  3706. for (NSUInteger i = 0; i < count; ++i) {
  3707. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3708. }
  3709. }
  3710. }
  3711. return self;
  3712. }
  3713. - (instancetype)initWithDictionary:(GPBInt32FloatDictionary *)dictionary {
  3714. self = [self initWithFloats:NULL forKeys:NULL count:0];
  3715. if (self) {
  3716. if (dictionary) {
  3717. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3718. }
  3719. }
  3720. return self;
  3721. }
  3722. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3723. #pragma unused(numItems)
  3724. return [self initWithFloats:NULL forKeys:NULL count:0];
  3725. }
  3726. - (void)dealloc {
  3727. NSAssert(!_autocreator,
  3728. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3729. [self class], _autocreator);
  3730. [_dictionary release];
  3731. [super dealloc];
  3732. }
  3733. - (instancetype)copyWithZone:(NSZone *)zone {
  3734. return [[GPBInt32FloatDictionary allocWithZone:zone] initWithDictionary:self];
  3735. }
  3736. - (BOOL)isEqual:(id)other {
  3737. if (self == other) {
  3738. return YES;
  3739. }
  3740. if (![other isKindOfClass:[GPBInt32FloatDictionary class]]) {
  3741. return NO;
  3742. }
  3743. GPBInt32FloatDictionary *otherDictionary = other;
  3744. return [_dictionary isEqual:otherDictionary->_dictionary];
  3745. }
  3746. - (NSUInteger)hash {
  3747. return _dictionary.count;
  3748. }
  3749. - (NSString *)description {
  3750. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3751. }
  3752. - (NSUInteger)count {
  3753. return _dictionary.count;
  3754. }
  3755. - (void)enumerateKeysAndFloatsUsingBlock:
  3756. (void (NS_NOESCAPE ^)(int32_t key, float value, BOOL *stop))block {
  3757. BOOL stop = NO;
  3758. NSDictionary *internal = _dictionary;
  3759. NSEnumerator *keys = [internal keyEnumerator];
  3760. NSNumber *aKey;
  3761. while ((aKey = [keys nextObject])) {
  3762. NSNumber *aValue = internal[aKey];
  3763. block([aKey intValue], [aValue floatValue], &stop);
  3764. if (stop) {
  3765. break;
  3766. }
  3767. }
  3768. }
  3769. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3770. NSDictionary *internal = _dictionary;
  3771. NSUInteger count = internal.count;
  3772. if (count == 0) {
  3773. return 0;
  3774. }
  3775. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3776. GPBDataType keyDataType = field.mapKeyDataType;
  3777. size_t result = 0;
  3778. NSEnumerator *keys = [internal keyEnumerator];
  3779. NSNumber *aKey;
  3780. while ((aKey = [keys nextObject])) {
  3781. NSNumber *aValue = internal[aKey];
  3782. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3783. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  3784. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3785. }
  3786. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3787. result += tagSize * count;
  3788. return result;
  3789. }
  3790. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3791. asField:(GPBFieldDescriptor *)field {
  3792. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3793. GPBDataType keyDataType = field.mapKeyDataType;
  3794. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3795. NSDictionary *internal = _dictionary;
  3796. NSEnumerator *keys = [internal keyEnumerator];
  3797. NSNumber *aKey;
  3798. while ((aKey = [keys nextObject])) {
  3799. NSNumber *aValue = internal[aKey];
  3800. [outputStream writeInt32NoTag:tag];
  3801. // Write the size of the message.
  3802. int32_t unwrappedKey = [aKey intValue];
  3803. float unwrappedValue = [aValue floatValue];
  3804. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3805. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3806. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3807. // Write the fields.
  3808. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3809. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3810. }
  3811. }
  3812. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3813. forGPBGenericValueKey:(GPBGenericValue *)key {
  3814. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt32)];
  3815. }
  3816. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  3817. [self enumerateKeysAndFloatsUsingBlock:^(int32_t key, float value, BOOL *stop) {
  3818. #pragma unused(stop)
  3819. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  3820. }];
  3821. }
  3822. - (BOOL)getFloat:(nullable float *)value forKey:(int32_t)key {
  3823. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3824. if (wrapped && value) {
  3825. *value = [wrapped floatValue];
  3826. }
  3827. return (wrapped != NULL);
  3828. }
  3829. - (void)addEntriesFromDictionary:(GPBInt32FloatDictionary *)otherDictionary {
  3830. if (otherDictionary) {
  3831. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3832. if (_autocreator) {
  3833. GPBAutocreatedDictionaryModified(_autocreator, self);
  3834. }
  3835. }
  3836. }
  3837. - (void)setFloat:(float)value forKey:(int32_t)key {
  3838. [_dictionary setObject:@(value) forKey:@(key)];
  3839. if (_autocreator) {
  3840. GPBAutocreatedDictionaryModified(_autocreator, self);
  3841. }
  3842. }
  3843. - (void)removeFloatForKey:(int32_t)aKey {
  3844. [_dictionary removeObjectForKey:@(aKey)];
  3845. }
  3846. - (void)removeAll {
  3847. [_dictionary removeAllObjects];
  3848. }
  3849. @end
  3850. #pragma mark - Int32 -> Double
  3851. @implementation GPBInt32DoubleDictionary {
  3852. @package
  3853. NSMutableDictionary *_dictionary;
  3854. }
  3855. - (instancetype)init {
  3856. return [self initWithDoubles:NULL forKeys:NULL count:0];
  3857. }
  3858. - (instancetype)initWithDoubles:(const double [])values
  3859. forKeys:(const int32_t [])keys
  3860. count:(NSUInteger)count {
  3861. self = [super init];
  3862. if (self) {
  3863. _dictionary = [[NSMutableDictionary alloc] init];
  3864. if (count && values && keys) {
  3865. for (NSUInteger i = 0; i < count; ++i) {
  3866. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  3867. }
  3868. }
  3869. }
  3870. return self;
  3871. }
  3872. - (instancetype)initWithDictionary:(GPBInt32DoubleDictionary *)dictionary {
  3873. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  3874. if (self) {
  3875. if (dictionary) {
  3876. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  3877. }
  3878. }
  3879. return self;
  3880. }
  3881. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  3882. #pragma unused(numItems)
  3883. return [self initWithDoubles:NULL forKeys:NULL count:0];
  3884. }
  3885. - (void)dealloc {
  3886. NSAssert(!_autocreator,
  3887. @"%@: Autocreator must be cleared before release, autocreator: %@",
  3888. [self class], _autocreator);
  3889. [_dictionary release];
  3890. [super dealloc];
  3891. }
  3892. - (instancetype)copyWithZone:(NSZone *)zone {
  3893. return [[GPBInt32DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  3894. }
  3895. - (BOOL)isEqual:(id)other {
  3896. if (self == other) {
  3897. return YES;
  3898. }
  3899. if (![other isKindOfClass:[GPBInt32DoubleDictionary class]]) {
  3900. return NO;
  3901. }
  3902. GPBInt32DoubleDictionary *otherDictionary = other;
  3903. return [_dictionary isEqual:otherDictionary->_dictionary];
  3904. }
  3905. - (NSUInteger)hash {
  3906. return _dictionary.count;
  3907. }
  3908. - (NSString *)description {
  3909. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  3910. }
  3911. - (NSUInteger)count {
  3912. return _dictionary.count;
  3913. }
  3914. - (void)enumerateKeysAndDoublesUsingBlock:
  3915. (void (NS_NOESCAPE ^)(int32_t key, double value, BOOL *stop))block {
  3916. BOOL stop = NO;
  3917. NSDictionary *internal = _dictionary;
  3918. NSEnumerator *keys = [internal keyEnumerator];
  3919. NSNumber *aKey;
  3920. while ((aKey = [keys nextObject])) {
  3921. NSNumber *aValue = internal[aKey];
  3922. block([aKey intValue], [aValue doubleValue], &stop);
  3923. if (stop) {
  3924. break;
  3925. }
  3926. }
  3927. }
  3928. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  3929. NSDictionary *internal = _dictionary;
  3930. NSUInteger count = internal.count;
  3931. if (count == 0) {
  3932. return 0;
  3933. }
  3934. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3935. GPBDataType keyDataType = field.mapKeyDataType;
  3936. size_t result = 0;
  3937. NSEnumerator *keys = [internal keyEnumerator];
  3938. NSNumber *aKey;
  3939. while ((aKey = [keys nextObject])) {
  3940. NSNumber *aValue = internal[aKey];
  3941. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  3942. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  3943. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  3944. }
  3945. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  3946. result += tagSize * count;
  3947. return result;
  3948. }
  3949. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  3950. asField:(GPBFieldDescriptor *)field {
  3951. GPBDataType valueDataType = GPBGetFieldDataType(field);
  3952. GPBDataType keyDataType = field.mapKeyDataType;
  3953. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  3954. NSDictionary *internal = _dictionary;
  3955. NSEnumerator *keys = [internal keyEnumerator];
  3956. NSNumber *aKey;
  3957. while ((aKey = [keys nextObject])) {
  3958. NSNumber *aValue = internal[aKey];
  3959. [outputStream writeInt32NoTag:tag];
  3960. // Write the size of the message.
  3961. int32_t unwrappedKey = [aKey intValue];
  3962. double unwrappedValue = [aValue doubleValue];
  3963. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3964. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  3965. [outputStream writeInt32NoTag:(int32_t)msgSize];
  3966. // Write the fields.
  3967. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  3968. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  3969. }
  3970. }
  3971. - (void)setGPBGenericValue:(GPBGenericValue *)value
  3972. forGPBGenericValueKey:(GPBGenericValue *)key {
  3973. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt32)];
  3974. }
  3975. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  3976. [self enumerateKeysAndDoublesUsingBlock:^(int32_t key, double value, BOOL *stop) {
  3977. #pragma unused(stop)
  3978. block([NSString stringWithFormat:@"%d", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  3979. }];
  3980. }
  3981. - (BOOL)getDouble:(nullable double *)value forKey:(int32_t)key {
  3982. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  3983. if (wrapped && value) {
  3984. *value = [wrapped doubleValue];
  3985. }
  3986. return (wrapped != NULL);
  3987. }
  3988. - (void)addEntriesFromDictionary:(GPBInt32DoubleDictionary *)otherDictionary {
  3989. if (otherDictionary) {
  3990. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  3991. if (_autocreator) {
  3992. GPBAutocreatedDictionaryModified(_autocreator, self);
  3993. }
  3994. }
  3995. }
  3996. - (void)setDouble:(double)value forKey:(int32_t)key {
  3997. [_dictionary setObject:@(value) forKey:@(key)];
  3998. if (_autocreator) {
  3999. GPBAutocreatedDictionaryModified(_autocreator, self);
  4000. }
  4001. }
  4002. - (void)removeDoubleForKey:(int32_t)aKey {
  4003. [_dictionary removeObjectForKey:@(aKey)];
  4004. }
  4005. - (void)removeAll {
  4006. [_dictionary removeAllObjects];
  4007. }
  4008. @end
  4009. #pragma mark - Int32 -> Enum
  4010. @implementation GPBInt32EnumDictionary {
  4011. @package
  4012. NSMutableDictionary *_dictionary;
  4013. GPBEnumValidationFunc _validationFunc;
  4014. }
  4015. @synthesize validationFunc = _validationFunc;
  4016. - (instancetype)init {
  4017. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  4018. }
  4019. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  4020. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4021. }
  4022. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4023. rawValues:(const int32_t [])rawValues
  4024. forKeys:(const int32_t [])keys
  4025. count:(NSUInteger)count {
  4026. self = [super init];
  4027. if (self) {
  4028. _dictionary = [[NSMutableDictionary alloc] init];
  4029. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  4030. if (count && rawValues && keys) {
  4031. for (NSUInteger i = 0; i < count; ++i) {
  4032. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  4033. }
  4034. }
  4035. }
  4036. return self;
  4037. }
  4038. - (instancetype)initWithDictionary:(GPBInt32EnumDictionary *)dictionary {
  4039. self = [self initWithValidationFunction:dictionary.validationFunc
  4040. rawValues:NULL
  4041. forKeys:NULL
  4042. count:0];
  4043. if (self) {
  4044. if (dictionary) {
  4045. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4046. }
  4047. }
  4048. return self;
  4049. }
  4050. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  4051. capacity:(NSUInteger)numItems {
  4052. #pragma unused(numItems)
  4053. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  4054. }
  4055. - (void)dealloc {
  4056. NSAssert(!_autocreator,
  4057. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4058. [self class], _autocreator);
  4059. [_dictionary release];
  4060. [super dealloc];
  4061. }
  4062. - (instancetype)copyWithZone:(NSZone *)zone {
  4063. return [[GPBInt32EnumDictionary allocWithZone:zone] initWithDictionary:self];
  4064. }
  4065. - (BOOL)isEqual:(id)other {
  4066. if (self == other) {
  4067. return YES;
  4068. }
  4069. if (![other isKindOfClass:[GPBInt32EnumDictionary class]]) {
  4070. return NO;
  4071. }
  4072. GPBInt32EnumDictionary *otherDictionary = other;
  4073. return [_dictionary isEqual:otherDictionary->_dictionary];
  4074. }
  4075. - (NSUInteger)hash {
  4076. return _dictionary.count;
  4077. }
  4078. - (NSString *)description {
  4079. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4080. }
  4081. - (NSUInteger)count {
  4082. return _dictionary.count;
  4083. }
  4084. - (void)enumerateKeysAndRawValuesUsingBlock:
  4085. (void (NS_NOESCAPE ^)(int32_t key, int32_t value, BOOL *stop))block {
  4086. BOOL stop = NO;
  4087. NSDictionary *internal = _dictionary;
  4088. NSEnumerator *keys = [internal keyEnumerator];
  4089. NSNumber *aKey;
  4090. while ((aKey = [keys nextObject])) {
  4091. NSNumber *aValue = internal[aKey];
  4092. block([aKey intValue], [aValue intValue], &stop);
  4093. if (stop) {
  4094. break;
  4095. }
  4096. }
  4097. }
  4098. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4099. NSDictionary *internal = _dictionary;
  4100. NSUInteger count = internal.count;
  4101. if (count == 0) {
  4102. return 0;
  4103. }
  4104. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4105. GPBDataType keyDataType = field.mapKeyDataType;
  4106. size_t result = 0;
  4107. NSEnumerator *keys = [internal keyEnumerator];
  4108. NSNumber *aKey;
  4109. while ((aKey = [keys nextObject])) {
  4110. NSNumber *aValue = internal[aKey];
  4111. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4112. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4113. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4114. }
  4115. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4116. result += tagSize * count;
  4117. return result;
  4118. }
  4119. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4120. asField:(GPBFieldDescriptor *)field {
  4121. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4122. GPBDataType keyDataType = field.mapKeyDataType;
  4123. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4124. NSDictionary *internal = _dictionary;
  4125. NSEnumerator *keys = [internal keyEnumerator];
  4126. NSNumber *aKey;
  4127. while ((aKey = [keys nextObject])) {
  4128. NSNumber *aValue = internal[aKey];
  4129. [outputStream writeInt32NoTag:tag];
  4130. // Write the size of the message.
  4131. int32_t unwrappedKey = [aKey intValue];
  4132. int32_t unwrappedValue = [aValue intValue];
  4133. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4134. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4135. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4136. // Write the fields.
  4137. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4138. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4139. }
  4140. }
  4141. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  4142. forKey:(GPBGenericValue *)key
  4143. keyDataType:(GPBDataType)keyDataType {
  4144. size_t msgSize = ComputeDictInt32FieldSize(key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4145. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  4146. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  4147. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  4148. WriteDictInt32Field(outputStream, key->valueInt32, kMapKeyFieldNumber, keyDataType);
  4149. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  4150. [outputStream release];
  4151. return data;
  4152. }
  4153. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4154. forGPBGenericValueKey:(GPBGenericValue *)key {
  4155. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt32)];
  4156. }
  4157. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  4158. [self enumerateKeysAndRawValuesUsingBlock:^(int32_t key, int32_t value, BOOL *stop) {
  4159. #pragma unused(stop)
  4160. block([NSString stringWithFormat:@"%d", key], @(value));
  4161. }];
  4162. }
  4163. - (BOOL)getEnum:(int32_t *)value forKey:(int32_t)key {
  4164. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4165. if (wrapped && value) {
  4166. int32_t result = [wrapped intValue];
  4167. if (!_validationFunc(result)) {
  4168. result = kGPBUnrecognizedEnumeratorValue;
  4169. }
  4170. *value = result;
  4171. }
  4172. return (wrapped != NULL);
  4173. }
  4174. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(int32_t)key {
  4175. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4176. if (wrapped && rawValue) {
  4177. *rawValue = [wrapped intValue];
  4178. }
  4179. return (wrapped != NULL);
  4180. }
  4181. - (void)enumerateKeysAndEnumsUsingBlock:
  4182. (void (NS_NOESCAPE ^)(int32_t key, int32_t value, BOOL *stop))block {
  4183. GPBEnumValidationFunc func = _validationFunc;
  4184. BOOL stop = NO;
  4185. NSEnumerator *keys = [_dictionary keyEnumerator];
  4186. NSNumber *aKey;
  4187. while ((aKey = [keys nextObject])) {
  4188. NSNumber *aValue = _dictionary[aKey];
  4189. int32_t unwrapped = [aValue intValue];
  4190. if (!func(unwrapped)) {
  4191. unwrapped = kGPBUnrecognizedEnumeratorValue;
  4192. }
  4193. block([aKey intValue], unwrapped, &stop);
  4194. if (stop) {
  4195. break;
  4196. }
  4197. }
  4198. }
  4199. - (void)addRawEntriesFromDictionary:(GPBInt32EnumDictionary *)otherDictionary {
  4200. if (otherDictionary) {
  4201. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4202. if (_autocreator) {
  4203. GPBAutocreatedDictionaryModified(_autocreator, self);
  4204. }
  4205. }
  4206. }
  4207. - (void)setRawValue:(int32_t)value forKey:(int32_t)key {
  4208. [_dictionary setObject:@(value) forKey:@(key)];
  4209. if (_autocreator) {
  4210. GPBAutocreatedDictionaryModified(_autocreator, self);
  4211. }
  4212. }
  4213. - (void)removeEnumForKey:(int32_t)aKey {
  4214. [_dictionary removeObjectForKey:@(aKey)];
  4215. }
  4216. - (void)removeAll {
  4217. [_dictionary removeAllObjects];
  4218. }
  4219. - (void)setEnum:(int32_t)value forKey:(int32_t)key {
  4220. if (!_validationFunc(value)) {
  4221. [NSException raise:NSInvalidArgumentException
  4222. format:@"GPBInt32EnumDictionary: Attempt to set an unknown enum value (%d)",
  4223. value];
  4224. }
  4225. [_dictionary setObject:@(value) forKey:@(key)];
  4226. if (_autocreator) {
  4227. GPBAutocreatedDictionaryModified(_autocreator, self);
  4228. }
  4229. }
  4230. @end
  4231. #pragma mark - Int32 -> Object
  4232. @implementation GPBInt32ObjectDictionary {
  4233. @package
  4234. NSMutableDictionary *_dictionary;
  4235. }
  4236. - (instancetype)init {
  4237. return [self initWithObjects:NULL forKeys:NULL count:0];
  4238. }
  4239. - (instancetype)initWithObjects:(const id [])objects
  4240. forKeys:(const int32_t [])keys
  4241. count:(NSUInteger)count {
  4242. self = [super init];
  4243. if (self) {
  4244. _dictionary = [[NSMutableDictionary alloc] init];
  4245. if (count && objects && keys) {
  4246. for (NSUInteger i = 0; i < count; ++i) {
  4247. if (!objects[i]) {
  4248. [NSException raise:NSInvalidArgumentException
  4249. format:@"Attempting to add nil object to a Dictionary"];
  4250. }
  4251. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  4252. }
  4253. }
  4254. }
  4255. return self;
  4256. }
  4257. - (instancetype)initWithDictionary:(GPBInt32ObjectDictionary *)dictionary {
  4258. self = [self initWithObjects:NULL forKeys:NULL count:0];
  4259. if (self) {
  4260. if (dictionary) {
  4261. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4262. }
  4263. }
  4264. return self;
  4265. }
  4266. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4267. #pragma unused(numItems)
  4268. return [self initWithObjects:NULL forKeys:NULL count:0];
  4269. }
  4270. - (void)dealloc {
  4271. NSAssert(!_autocreator,
  4272. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4273. [self class], _autocreator);
  4274. [_dictionary release];
  4275. [super dealloc];
  4276. }
  4277. - (instancetype)copyWithZone:(NSZone *)zone {
  4278. return [[GPBInt32ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  4279. }
  4280. - (BOOL)isEqual:(id)other {
  4281. if (self == other) {
  4282. return YES;
  4283. }
  4284. if (![other isKindOfClass:[GPBInt32ObjectDictionary class]]) {
  4285. return NO;
  4286. }
  4287. GPBInt32ObjectDictionary *otherDictionary = other;
  4288. return [_dictionary isEqual:otherDictionary->_dictionary];
  4289. }
  4290. - (NSUInteger)hash {
  4291. return _dictionary.count;
  4292. }
  4293. - (NSString *)description {
  4294. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4295. }
  4296. - (NSUInteger)count {
  4297. return _dictionary.count;
  4298. }
  4299. - (void)enumerateKeysAndObjectsUsingBlock:
  4300. (void (NS_NOESCAPE ^)(int32_t key, id object, BOOL *stop))block {
  4301. BOOL stop = NO;
  4302. NSDictionary *internal = _dictionary;
  4303. NSEnumerator *keys = [internal keyEnumerator];
  4304. NSNumber *aKey;
  4305. while ((aKey = [keys nextObject])) {
  4306. id aObject = internal[aKey];
  4307. block([aKey intValue], aObject, &stop);
  4308. if (stop) {
  4309. break;
  4310. }
  4311. }
  4312. }
  4313. - (BOOL)isInitialized {
  4314. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  4315. if (!msg.initialized) {
  4316. return NO;
  4317. }
  4318. }
  4319. return YES;
  4320. }
  4321. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  4322. GPBInt32ObjectDictionary *newDict =
  4323. [[GPBInt32ObjectDictionary alloc] init];
  4324. NSEnumerator *keys = [_dictionary keyEnumerator];
  4325. id aKey;
  4326. NSMutableDictionary *internalDict = newDict->_dictionary;
  4327. while ((aKey = [keys nextObject])) {
  4328. GPBMessage *msg = _dictionary[aKey];
  4329. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  4330. [internalDict setObject:copiedMsg forKey:aKey];
  4331. [copiedMsg release];
  4332. }
  4333. return newDict;
  4334. }
  4335. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4336. NSDictionary *internal = _dictionary;
  4337. NSUInteger count = internal.count;
  4338. if (count == 0) {
  4339. return 0;
  4340. }
  4341. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4342. GPBDataType keyDataType = field.mapKeyDataType;
  4343. size_t result = 0;
  4344. NSEnumerator *keys = [internal keyEnumerator];
  4345. NSNumber *aKey;
  4346. while ((aKey = [keys nextObject])) {
  4347. id aObject = internal[aKey];
  4348. size_t msgSize = ComputeDictInt32FieldSize([aKey intValue], kMapKeyFieldNumber, keyDataType);
  4349. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  4350. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4351. }
  4352. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4353. result += tagSize * count;
  4354. return result;
  4355. }
  4356. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4357. asField:(GPBFieldDescriptor *)field {
  4358. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4359. GPBDataType keyDataType = field.mapKeyDataType;
  4360. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4361. NSDictionary *internal = _dictionary;
  4362. NSEnumerator *keys = [internal keyEnumerator];
  4363. NSNumber *aKey;
  4364. while ((aKey = [keys nextObject])) {
  4365. id aObject = internal[aKey];
  4366. [outputStream writeInt32NoTag:tag];
  4367. // Write the size of the message.
  4368. int32_t unwrappedKey = [aKey intValue];
  4369. id unwrappedValue = aObject;
  4370. size_t msgSize = ComputeDictInt32FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4371. msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4372. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4373. // Write the fields.
  4374. WriteDictInt32Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4375. WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4376. }
  4377. }
  4378. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4379. forGPBGenericValueKey:(GPBGenericValue *)key {
  4380. [_dictionary setObject:value->valueString forKey:@(key->valueInt32)];
  4381. }
  4382. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  4383. [self enumerateKeysAndObjectsUsingBlock:^(int32_t key, id object, BOOL *stop) {
  4384. #pragma unused(stop)
  4385. block([NSString stringWithFormat:@"%d", key], object);
  4386. }];
  4387. }
  4388. - (id)objectForKey:(int32_t)key {
  4389. id result = [_dictionary objectForKey:@(key)];
  4390. return result;
  4391. }
  4392. - (void)addEntriesFromDictionary:(GPBInt32ObjectDictionary *)otherDictionary {
  4393. if (otherDictionary) {
  4394. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4395. if (_autocreator) {
  4396. GPBAutocreatedDictionaryModified(_autocreator, self);
  4397. }
  4398. }
  4399. }
  4400. - (void)setObject:(id)object forKey:(int32_t)key {
  4401. if (!object) {
  4402. [NSException raise:NSInvalidArgumentException
  4403. format:@"Attempting to add nil object to a Dictionary"];
  4404. }
  4405. [_dictionary setObject:object forKey:@(key)];
  4406. if (_autocreator) {
  4407. GPBAutocreatedDictionaryModified(_autocreator, self);
  4408. }
  4409. }
  4410. - (void)removeObjectForKey:(int32_t)aKey {
  4411. [_dictionary removeObjectForKey:@(aKey)];
  4412. }
  4413. - (void)removeAll {
  4414. [_dictionary removeAllObjects];
  4415. }
  4416. @end
  4417. // clang-format on
  4418. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(UInt64, uint64_t)
  4419. // This block of code is generated, do not edit it directly.
  4420. // clang-format off
  4421. #pragma mark - UInt64 -> UInt32
  4422. @implementation GPBUInt64UInt32Dictionary {
  4423. @package
  4424. NSMutableDictionary *_dictionary;
  4425. }
  4426. - (instancetype)init {
  4427. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  4428. }
  4429. - (instancetype)initWithUInt32s:(const uint32_t [])values
  4430. forKeys:(const uint64_t [])keys
  4431. count:(NSUInteger)count {
  4432. self = [super init];
  4433. if (self) {
  4434. _dictionary = [[NSMutableDictionary alloc] init];
  4435. if (count && values && keys) {
  4436. for (NSUInteger i = 0; i < count; ++i) {
  4437. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4438. }
  4439. }
  4440. }
  4441. return self;
  4442. }
  4443. - (instancetype)initWithDictionary:(GPBUInt64UInt32Dictionary *)dictionary {
  4444. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  4445. if (self) {
  4446. if (dictionary) {
  4447. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4448. }
  4449. }
  4450. return self;
  4451. }
  4452. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4453. #pragma unused(numItems)
  4454. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  4455. }
  4456. - (void)dealloc {
  4457. NSAssert(!_autocreator,
  4458. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4459. [self class], _autocreator);
  4460. [_dictionary release];
  4461. [super dealloc];
  4462. }
  4463. - (instancetype)copyWithZone:(NSZone *)zone {
  4464. return [[GPBUInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  4465. }
  4466. - (BOOL)isEqual:(id)other {
  4467. if (self == other) {
  4468. return YES;
  4469. }
  4470. if (![other isKindOfClass:[GPBUInt64UInt32Dictionary class]]) {
  4471. return NO;
  4472. }
  4473. GPBUInt64UInt32Dictionary *otherDictionary = other;
  4474. return [_dictionary isEqual:otherDictionary->_dictionary];
  4475. }
  4476. - (NSUInteger)hash {
  4477. return _dictionary.count;
  4478. }
  4479. - (NSString *)description {
  4480. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4481. }
  4482. - (NSUInteger)count {
  4483. return _dictionary.count;
  4484. }
  4485. - (void)enumerateKeysAndUInt32sUsingBlock:
  4486. (void (NS_NOESCAPE ^)(uint64_t key, uint32_t value, BOOL *stop))block {
  4487. BOOL stop = NO;
  4488. NSDictionary *internal = _dictionary;
  4489. NSEnumerator *keys = [internal keyEnumerator];
  4490. NSNumber *aKey;
  4491. while ((aKey = [keys nextObject])) {
  4492. NSNumber *aValue = internal[aKey];
  4493. block([aKey unsignedLongLongValue], [aValue unsignedIntValue], &stop);
  4494. if (stop) {
  4495. break;
  4496. }
  4497. }
  4498. }
  4499. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4500. NSDictionary *internal = _dictionary;
  4501. NSUInteger count = internal.count;
  4502. if (count == 0) {
  4503. return 0;
  4504. }
  4505. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4506. GPBDataType keyDataType = field.mapKeyDataType;
  4507. size_t result = 0;
  4508. NSEnumerator *keys = [internal keyEnumerator];
  4509. NSNumber *aKey;
  4510. while ((aKey = [keys nextObject])) {
  4511. NSNumber *aValue = internal[aKey];
  4512. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4513. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  4514. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4515. }
  4516. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4517. result += tagSize * count;
  4518. return result;
  4519. }
  4520. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4521. asField:(GPBFieldDescriptor *)field {
  4522. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4523. GPBDataType keyDataType = field.mapKeyDataType;
  4524. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4525. NSDictionary *internal = _dictionary;
  4526. NSEnumerator *keys = [internal keyEnumerator];
  4527. NSNumber *aKey;
  4528. while ((aKey = [keys nextObject])) {
  4529. NSNumber *aValue = internal[aKey];
  4530. [outputStream writeInt32NoTag:tag];
  4531. // Write the size of the message.
  4532. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  4533. uint32_t unwrappedValue = [aValue unsignedIntValue];
  4534. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4535. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4536. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4537. // Write the fields.
  4538. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4539. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4540. }
  4541. }
  4542. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4543. forGPBGenericValueKey:(GPBGenericValue *)key {
  4544. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueUInt64)];
  4545. }
  4546. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  4547. [self enumerateKeysAndUInt32sUsingBlock:^(uint64_t key, uint32_t value, BOOL *stop) {
  4548. #pragma unused(stop)
  4549. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%u", value]);
  4550. }];
  4551. }
  4552. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(uint64_t)key {
  4553. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4554. if (wrapped && value) {
  4555. *value = [wrapped unsignedIntValue];
  4556. }
  4557. return (wrapped != NULL);
  4558. }
  4559. - (void)addEntriesFromDictionary:(GPBUInt64UInt32Dictionary *)otherDictionary {
  4560. if (otherDictionary) {
  4561. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4562. if (_autocreator) {
  4563. GPBAutocreatedDictionaryModified(_autocreator, self);
  4564. }
  4565. }
  4566. }
  4567. - (void)setUInt32:(uint32_t)value forKey:(uint64_t)key {
  4568. [_dictionary setObject:@(value) forKey:@(key)];
  4569. if (_autocreator) {
  4570. GPBAutocreatedDictionaryModified(_autocreator, self);
  4571. }
  4572. }
  4573. - (void)removeUInt32ForKey:(uint64_t)aKey {
  4574. [_dictionary removeObjectForKey:@(aKey)];
  4575. }
  4576. - (void)removeAll {
  4577. [_dictionary removeAllObjects];
  4578. }
  4579. @end
  4580. #pragma mark - UInt64 -> Int32
  4581. @implementation GPBUInt64Int32Dictionary {
  4582. @package
  4583. NSMutableDictionary *_dictionary;
  4584. }
  4585. - (instancetype)init {
  4586. return [self initWithInt32s:NULL forKeys:NULL count:0];
  4587. }
  4588. - (instancetype)initWithInt32s:(const int32_t [])values
  4589. forKeys:(const uint64_t [])keys
  4590. count:(NSUInteger)count {
  4591. self = [super init];
  4592. if (self) {
  4593. _dictionary = [[NSMutableDictionary alloc] init];
  4594. if (count && values && keys) {
  4595. for (NSUInteger i = 0; i < count; ++i) {
  4596. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4597. }
  4598. }
  4599. }
  4600. return self;
  4601. }
  4602. - (instancetype)initWithDictionary:(GPBUInt64Int32Dictionary *)dictionary {
  4603. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  4604. if (self) {
  4605. if (dictionary) {
  4606. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4607. }
  4608. }
  4609. return self;
  4610. }
  4611. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4612. #pragma unused(numItems)
  4613. return [self initWithInt32s:NULL forKeys:NULL count:0];
  4614. }
  4615. - (void)dealloc {
  4616. NSAssert(!_autocreator,
  4617. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4618. [self class], _autocreator);
  4619. [_dictionary release];
  4620. [super dealloc];
  4621. }
  4622. - (instancetype)copyWithZone:(NSZone *)zone {
  4623. return [[GPBUInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  4624. }
  4625. - (BOOL)isEqual:(id)other {
  4626. if (self == other) {
  4627. return YES;
  4628. }
  4629. if (![other isKindOfClass:[GPBUInt64Int32Dictionary class]]) {
  4630. return NO;
  4631. }
  4632. GPBUInt64Int32Dictionary *otherDictionary = other;
  4633. return [_dictionary isEqual:otherDictionary->_dictionary];
  4634. }
  4635. - (NSUInteger)hash {
  4636. return _dictionary.count;
  4637. }
  4638. - (NSString *)description {
  4639. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4640. }
  4641. - (NSUInteger)count {
  4642. return _dictionary.count;
  4643. }
  4644. - (void)enumerateKeysAndInt32sUsingBlock:
  4645. (void (NS_NOESCAPE ^)(uint64_t key, int32_t value, BOOL *stop))block {
  4646. BOOL stop = NO;
  4647. NSDictionary *internal = _dictionary;
  4648. NSEnumerator *keys = [internal keyEnumerator];
  4649. NSNumber *aKey;
  4650. while ((aKey = [keys nextObject])) {
  4651. NSNumber *aValue = internal[aKey];
  4652. block([aKey unsignedLongLongValue], [aValue intValue], &stop);
  4653. if (stop) {
  4654. break;
  4655. }
  4656. }
  4657. }
  4658. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4659. NSDictionary *internal = _dictionary;
  4660. NSUInteger count = internal.count;
  4661. if (count == 0) {
  4662. return 0;
  4663. }
  4664. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4665. GPBDataType keyDataType = field.mapKeyDataType;
  4666. size_t result = 0;
  4667. NSEnumerator *keys = [internal keyEnumerator];
  4668. NSNumber *aKey;
  4669. while ((aKey = [keys nextObject])) {
  4670. NSNumber *aValue = internal[aKey];
  4671. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4672. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  4673. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4674. }
  4675. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4676. result += tagSize * count;
  4677. return result;
  4678. }
  4679. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4680. asField:(GPBFieldDescriptor *)field {
  4681. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4682. GPBDataType keyDataType = field.mapKeyDataType;
  4683. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4684. NSDictionary *internal = _dictionary;
  4685. NSEnumerator *keys = [internal keyEnumerator];
  4686. NSNumber *aKey;
  4687. while ((aKey = [keys nextObject])) {
  4688. NSNumber *aValue = internal[aKey];
  4689. [outputStream writeInt32NoTag:tag];
  4690. // Write the size of the message.
  4691. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  4692. int32_t unwrappedValue = [aValue intValue];
  4693. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4694. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4695. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4696. // Write the fields.
  4697. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4698. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4699. }
  4700. }
  4701. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4702. forGPBGenericValueKey:(GPBGenericValue *)key {
  4703. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueUInt64)];
  4704. }
  4705. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  4706. [self enumerateKeysAndInt32sUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  4707. #pragma unused(stop)
  4708. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%d", value]);
  4709. }];
  4710. }
  4711. - (BOOL)getInt32:(nullable int32_t *)value forKey:(uint64_t)key {
  4712. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4713. if (wrapped && value) {
  4714. *value = [wrapped intValue];
  4715. }
  4716. return (wrapped != NULL);
  4717. }
  4718. - (void)addEntriesFromDictionary:(GPBUInt64Int32Dictionary *)otherDictionary {
  4719. if (otherDictionary) {
  4720. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4721. if (_autocreator) {
  4722. GPBAutocreatedDictionaryModified(_autocreator, self);
  4723. }
  4724. }
  4725. }
  4726. - (void)setInt32:(int32_t)value forKey:(uint64_t)key {
  4727. [_dictionary setObject:@(value) forKey:@(key)];
  4728. if (_autocreator) {
  4729. GPBAutocreatedDictionaryModified(_autocreator, self);
  4730. }
  4731. }
  4732. - (void)removeInt32ForKey:(uint64_t)aKey {
  4733. [_dictionary removeObjectForKey:@(aKey)];
  4734. }
  4735. - (void)removeAll {
  4736. [_dictionary removeAllObjects];
  4737. }
  4738. @end
  4739. #pragma mark - UInt64 -> UInt64
  4740. @implementation GPBUInt64UInt64Dictionary {
  4741. @package
  4742. NSMutableDictionary *_dictionary;
  4743. }
  4744. - (instancetype)init {
  4745. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  4746. }
  4747. - (instancetype)initWithUInt64s:(const uint64_t [])values
  4748. forKeys:(const uint64_t [])keys
  4749. count:(NSUInteger)count {
  4750. self = [super init];
  4751. if (self) {
  4752. _dictionary = [[NSMutableDictionary alloc] init];
  4753. if (count && values && keys) {
  4754. for (NSUInteger i = 0; i < count; ++i) {
  4755. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4756. }
  4757. }
  4758. }
  4759. return self;
  4760. }
  4761. - (instancetype)initWithDictionary:(GPBUInt64UInt64Dictionary *)dictionary {
  4762. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  4763. if (self) {
  4764. if (dictionary) {
  4765. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4766. }
  4767. }
  4768. return self;
  4769. }
  4770. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4771. #pragma unused(numItems)
  4772. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  4773. }
  4774. - (void)dealloc {
  4775. NSAssert(!_autocreator,
  4776. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4777. [self class], _autocreator);
  4778. [_dictionary release];
  4779. [super dealloc];
  4780. }
  4781. - (instancetype)copyWithZone:(NSZone *)zone {
  4782. return [[GPBUInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  4783. }
  4784. - (BOOL)isEqual:(id)other {
  4785. if (self == other) {
  4786. return YES;
  4787. }
  4788. if (![other isKindOfClass:[GPBUInt64UInt64Dictionary class]]) {
  4789. return NO;
  4790. }
  4791. GPBUInt64UInt64Dictionary *otherDictionary = other;
  4792. return [_dictionary isEqual:otherDictionary->_dictionary];
  4793. }
  4794. - (NSUInteger)hash {
  4795. return _dictionary.count;
  4796. }
  4797. - (NSString *)description {
  4798. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4799. }
  4800. - (NSUInteger)count {
  4801. return _dictionary.count;
  4802. }
  4803. - (void)enumerateKeysAndUInt64sUsingBlock:
  4804. (void (NS_NOESCAPE ^)(uint64_t key, uint64_t value, BOOL *stop))block {
  4805. BOOL stop = NO;
  4806. NSDictionary *internal = _dictionary;
  4807. NSEnumerator *keys = [internal keyEnumerator];
  4808. NSNumber *aKey;
  4809. while ((aKey = [keys nextObject])) {
  4810. NSNumber *aValue = internal[aKey];
  4811. block([aKey unsignedLongLongValue], [aValue unsignedLongLongValue], &stop);
  4812. if (stop) {
  4813. break;
  4814. }
  4815. }
  4816. }
  4817. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4818. NSDictionary *internal = _dictionary;
  4819. NSUInteger count = internal.count;
  4820. if (count == 0) {
  4821. return 0;
  4822. }
  4823. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4824. GPBDataType keyDataType = field.mapKeyDataType;
  4825. size_t result = 0;
  4826. NSEnumerator *keys = [internal keyEnumerator];
  4827. NSNumber *aKey;
  4828. while ((aKey = [keys nextObject])) {
  4829. NSNumber *aValue = internal[aKey];
  4830. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4831. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  4832. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4833. }
  4834. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4835. result += tagSize * count;
  4836. return result;
  4837. }
  4838. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4839. asField:(GPBFieldDescriptor *)field {
  4840. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4841. GPBDataType keyDataType = field.mapKeyDataType;
  4842. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  4843. NSDictionary *internal = _dictionary;
  4844. NSEnumerator *keys = [internal keyEnumerator];
  4845. NSNumber *aKey;
  4846. while ((aKey = [keys nextObject])) {
  4847. NSNumber *aValue = internal[aKey];
  4848. [outputStream writeInt32NoTag:tag];
  4849. // Write the size of the message.
  4850. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  4851. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  4852. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4853. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  4854. [outputStream writeInt32NoTag:(int32_t)msgSize];
  4855. // Write the fields.
  4856. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  4857. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  4858. }
  4859. }
  4860. - (void)setGPBGenericValue:(GPBGenericValue *)value
  4861. forGPBGenericValueKey:(GPBGenericValue *)key {
  4862. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueUInt64)];
  4863. }
  4864. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  4865. [self enumerateKeysAndUInt64sUsingBlock:^(uint64_t key, uint64_t value, BOOL *stop) {
  4866. #pragma unused(stop)
  4867. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%llu", value]);
  4868. }];
  4869. }
  4870. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(uint64_t)key {
  4871. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  4872. if (wrapped && value) {
  4873. *value = [wrapped unsignedLongLongValue];
  4874. }
  4875. return (wrapped != NULL);
  4876. }
  4877. - (void)addEntriesFromDictionary:(GPBUInt64UInt64Dictionary *)otherDictionary {
  4878. if (otherDictionary) {
  4879. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  4880. if (_autocreator) {
  4881. GPBAutocreatedDictionaryModified(_autocreator, self);
  4882. }
  4883. }
  4884. }
  4885. - (void)setUInt64:(uint64_t)value forKey:(uint64_t)key {
  4886. [_dictionary setObject:@(value) forKey:@(key)];
  4887. if (_autocreator) {
  4888. GPBAutocreatedDictionaryModified(_autocreator, self);
  4889. }
  4890. }
  4891. - (void)removeUInt64ForKey:(uint64_t)aKey {
  4892. [_dictionary removeObjectForKey:@(aKey)];
  4893. }
  4894. - (void)removeAll {
  4895. [_dictionary removeAllObjects];
  4896. }
  4897. @end
  4898. #pragma mark - UInt64 -> Int64
  4899. @implementation GPBUInt64Int64Dictionary {
  4900. @package
  4901. NSMutableDictionary *_dictionary;
  4902. }
  4903. - (instancetype)init {
  4904. return [self initWithInt64s:NULL forKeys:NULL count:0];
  4905. }
  4906. - (instancetype)initWithInt64s:(const int64_t [])values
  4907. forKeys:(const uint64_t [])keys
  4908. count:(NSUInteger)count {
  4909. self = [super init];
  4910. if (self) {
  4911. _dictionary = [[NSMutableDictionary alloc] init];
  4912. if (count && values && keys) {
  4913. for (NSUInteger i = 0; i < count; ++i) {
  4914. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  4915. }
  4916. }
  4917. }
  4918. return self;
  4919. }
  4920. - (instancetype)initWithDictionary:(GPBUInt64Int64Dictionary *)dictionary {
  4921. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  4922. if (self) {
  4923. if (dictionary) {
  4924. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  4925. }
  4926. }
  4927. return self;
  4928. }
  4929. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  4930. #pragma unused(numItems)
  4931. return [self initWithInt64s:NULL forKeys:NULL count:0];
  4932. }
  4933. - (void)dealloc {
  4934. NSAssert(!_autocreator,
  4935. @"%@: Autocreator must be cleared before release, autocreator: %@",
  4936. [self class], _autocreator);
  4937. [_dictionary release];
  4938. [super dealloc];
  4939. }
  4940. - (instancetype)copyWithZone:(NSZone *)zone {
  4941. return [[GPBUInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  4942. }
  4943. - (BOOL)isEqual:(id)other {
  4944. if (self == other) {
  4945. return YES;
  4946. }
  4947. if (![other isKindOfClass:[GPBUInt64Int64Dictionary class]]) {
  4948. return NO;
  4949. }
  4950. GPBUInt64Int64Dictionary *otherDictionary = other;
  4951. return [_dictionary isEqual:otherDictionary->_dictionary];
  4952. }
  4953. - (NSUInteger)hash {
  4954. return _dictionary.count;
  4955. }
  4956. - (NSString *)description {
  4957. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  4958. }
  4959. - (NSUInteger)count {
  4960. return _dictionary.count;
  4961. }
  4962. - (void)enumerateKeysAndInt64sUsingBlock:
  4963. (void (NS_NOESCAPE ^)(uint64_t key, int64_t value, BOOL *stop))block {
  4964. BOOL stop = NO;
  4965. NSDictionary *internal = _dictionary;
  4966. NSEnumerator *keys = [internal keyEnumerator];
  4967. NSNumber *aKey;
  4968. while ((aKey = [keys nextObject])) {
  4969. NSNumber *aValue = internal[aKey];
  4970. block([aKey unsignedLongLongValue], [aValue longLongValue], &stop);
  4971. if (stop) {
  4972. break;
  4973. }
  4974. }
  4975. }
  4976. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  4977. NSDictionary *internal = _dictionary;
  4978. NSUInteger count = internal.count;
  4979. if (count == 0) {
  4980. return 0;
  4981. }
  4982. GPBDataType valueDataType = GPBGetFieldDataType(field);
  4983. GPBDataType keyDataType = field.mapKeyDataType;
  4984. size_t result = 0;
  4985. NSEnumerator *keys = [internal keyEnumerator];
  4986. NSNumber *aKey;
  4987. while ((aKey = [keys nextObject])) {
  4988. NSNumber *aValue = internal[aKey];
  4989. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  4990. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  4991. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  4992. }
  4993. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  4994. result += tagSize * count;
  4995. return result;
  4996. }
  4997. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  4998. asField:(GPBFieldDescriptor *)field {
  4999. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5000. GPBDataType keyDataType = field.mapKeyDataType;
  5001. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5002. NSDictionary *internal = _dictionary;
  5003. NSEnumerator *keys = [internal keyEnumerator];
  5004. NSNumber *aKey;
  5005. while ((aKey = [keys nextObject])) {
  5006. NSNumber *aValue = internal[aKey];
  5007. [outputStream writeInt32NoTag:tag];
  5008. // Write the size of the message.
  5009. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5010. int64_t unwrappedValue = [aValue longLongValue];
  5011. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5012. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5013. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5014. // Write the fields.
  5015. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5016. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5017. }
  5018. }
  5019. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5020. forGPBGenericValueKey:(GPBGenericValue *)key {
  5021. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueUInt64)];
  5022. }
  5023. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  5024. [self enumerateKeysAndInt64sUsingBlock:^(uint64_t key, int64_t value, BOOL *stop) {
  5025. #pragma unused(stop)
  5026. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%lld", value]);
  5027. }];
  5028. }
  5029. - (BOOL)getInt64:(nullable int64_t *)value forKey:(uint64_t)key {
  5030. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5031. if (wrapped && value) {
  5032. *value = [wrapped longLongValue];
  5033. }
  5034. return (wrapped != NULL);
  5035. }
  5036. - (void)addEntriesFromDictionary:(GPBUInt64Int64Dictionary *)otherDictionary {
  5037. if (otherDictionary) {
  5038. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5039. if (_autocreator) {
  5040. GPBAutocreatedDictionaryModified(_autocreator, self);
  5041. }
  5042. }
  5043. }
  5044. - (void)setInt64:(int64_t)value forKey:(uint64_t)key {
  5045. [_dictionary setObject:@(value) forKey:@(key)];
  5046. if (_autocreator) {
  5047. GPBAutocreatedDictionaryModified(_autocreator, self);
  5048. }
  5049. }
  5050. - (void)removeInt64ForKey:(uint64_t)aKey {
  5051. [_dictionary removeObjectForKey:@(aKey)];
  5052. }
  5053. - (void)removeAll {
  5054. [_dictionary removeAllObjects];
  5055. }
  5056. @end
  5057. #pragma mark - UInt64 -> Bool
  5058. @implementation GPBUInt64BoolDictionary {
  5059. @package
  5060. NSMutableDictionary *_dictionary;
  5061. }
  5062. - (instancetype)init {
  5063. return [self initWithBools:NULL forKeys:NULL count:0];
  5064. }
  5065. - (instancetype)initWithBools:(const BOOL [])values
  5066. forKeys:(const uint64_t [])keys
  5067. count:(NSUInteger)count {
  5068. self = [super init];
  5069. if (self) {
  5070. _dictionary = [[NSMutableDictionary alloc] init];
  5071. if (count && values && keys) {
  5072. for (NSUInteger i = 0; i < count; ++i) {
  5073. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5074. }
  5075. }
  5076. }
  5077. return self;
  5078. }
  5079. - (instancetype)initWithDictionary:(GPBUInt64BoolDictionary *)dictionary {
  5080. self = [self initWithBools:NULL forKeys:NULL count:0];
  5081. if (self) {
  5082. if (dictionary) {
  5083. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5084. }
  5085. }
  5086. return self;
  5087. }
  5088. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5089. #pragma unused(numItems)
  5090. return [self initWithBools:NULL forKeys:NULL count:0];
  5091. }
  5092. - (void)dealloc {
  5093. NSAssert(!_autocreator,
  5094. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5095. [self class], _autocreator);
  5096. [_dictionary release];
  5097. [super dealloc];
  5098. }
  5099. - (instancetype)copyWithZone:(NSZone *)zone {
  5100. return [[GPBUInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  5101. }
  5102. - (BOOL)isEqual:(id)other {
  5103. if (self == other) {
  5104. return YES;
  5105. }
  5106. if (![other isKindOfClass:[GPBUInt64BoolDictionary class]]) {
  5107. return NO;
  5108. }
  5109. GPBUInt64BoolDictionary *otherDictionary = other;
  5110. return [_dictionary isEqual:otherDictionary->_dictionary];
  5111. }
  5112. - (NSUInteger)hash {
  5113. return _dictionary.count;
  5114. }
  5115. - (NSString *)description {
  5116. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5117. }
  5118. - (NSUInteger)count {
  5119. return _dictionary.count;
  5120. }
  5121. - (void)enumerateKeysAndBoolsUsingBlock:
  5122. (void (NS_NOESCAPE ^)(uint64_t key, BOOL value, BOOL *stop))block {
  5123. BOOL stop = NO;
  5124. NSDictionary *internal = _dictionary;
  5125. NSEnumerator *keys = [internal keyEnumerator];
  5126. NSNumber *aKey;
  5127. while ((aKey = [keys nextObject])) {
  5128. NSNumber *aValue = internal[aKey];
  5129. block([aKey unsignedLongLongValue], [aValue boolValue], &stop);
  5130. if (stop) {
  5131. break;
  5132. }
  5133. }
  5134. }
  5135. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5136. NSDictionary *internal = _dictionary;
  5137. NSUInteger count = internal.count;
  5138. if (count == 0) {
  5139. return 0;
  5140. }
  5141. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5142. GPBDataType keyDataType = field.mapKeyDataType;
  5143. size_t result = 0;
  5144. NSEnumerator *keys = [internal keyEnumerator];
  5145. NSNumber *aKey;
  5146. while ((aKey = [keys nextObject])) {
  5147. NSNumber *aValue = internal[aKey];
  5148. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5149. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  5150. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5151. }
  5152. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5153. result += tagSize * count;
  5154. return result;
  5155. }
  5156. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5157. asField:(GPBFieldDescriptor *)field {
  5158. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5159. GPBDataType keyDataType = field.mapKeyDataType;
  5160. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5161. NSDictionary *internal = _dictionary;
  5162. NSEnumerator *keys = [internal keyEnumerator];
  5163. NSNumber *aKey;
  5164. while ((aKey = [keys nextObject])) {
  5165. NSNumber *aValue = internal[aKey];
  5166. [outputStream writeInt32NoTag:tag];
  5167. // Write the size of the message.
  5168. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5169. BOOL unwrappedValue = [aValue boolValue];
  5170. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5171. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5172. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5173. // Write the fields.
  5174. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5175. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5176. }
  5177. }
  5178. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5179. forGPBGenericValueKey:(GPBGenericValue *)key {
  5180. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueUInt64)];
  5181. }
  5182. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  5183. [self enumerateKeysAndBoolsUsingBlock:^(uint64_t key, BOOL value, BOOL *stop) {
  5184. #pragma unused(stop)
  5185. block([NSString stringWithFormat:@"%llu", key], (value ? @"true" : @"false"));
  5186. }];
  5187. }
  5188. - (BOOL)getBool:(nullable BOOL *)value forKey:(uint64_t)key {
  5189. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5190. if (wrapped && value) {
  5191. *value = [wrapped boolValue];
  5192. }
  5193. return (wrapped != NULL);
  5194. }
  5195. - (void)addEntriesFromDictionary:(GPBUInt64BoolDictionary *)otherDictionary {
  5196. if (otherDictionary) {
  5197. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5198. if (_autocreator) {
  5199. GPBAutocreatedDictionaryModified(_autocreator, self);
  5200. }
  5201. }
  5202. }
  5203. - (void)setBool:(BOOL)value forKey:(uint64_t)key {
  5204. [_dictionary setObject:@(value) forKey:@(key)];
  5205. if (_autocreator) {
  5206. GPBAutocreatedDictionaryModified(_autocreator, self);
  5207. }
  5208. }
  5209. - (void)removeBoolForKey:(uint64_t)aKey {
  5210. [_dictionary removeObjectForKey:@(aKey)];
  5211. }
  5212. - (void)removeAll {
  5213. [_dictionary removeAllObjects];
  5214. }
  5215. @end
  5216. #pragma mark - UInt64 -> Float
  5217. @implementation GPBUInt64FloatDictionary {
  5218. @package
  5219. NSMutableDictionary *_dictionary;
  5220. }
  5221. - (instancetype)init {
  5222. return [self initWithFloats:NULL forKeys:NULL count:0];
  5223. }
  5224. - (instancetype)initWithFloats:(const float [])values
  5225. forKeys:(const uint64_t [])keys
  5226. count:(NSUInteger)count {
  5227. self = [super init];
  5228. if (self) {
  5229. _dictionary = [[NSMutableDictionary alloc] init];
  5230. if (count && values && keys) {
  5231. for (NSUInteger i = 0; i < count; ++i) {
  5232. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5233. }
  5234. }
  5235. }
  5236. return self;
  5237. }
  5238. - (instancetype)initWithDictionary:(GPBUInt64FloatDictionary *)dictionary {
  5239. self = [self initWithFloats:NULL forKeys:NULL count:0];
  5240. if (self) {
  5241. if (dictionary) {
  5242. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5243. }
  5244. }
  5245. return self;
  5246. }
  5247. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5248. #pragma unused(numItems)
  5249. return [self initWithFloats:NULL forKeys:NULL count:0];
  5250. }
  5251. - (void)dealloc {
  5252. NSAssert(!_autocreator,
  5253. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5254. [self class], _autocreator);
  5255. [_dictionary release];
  5256. [super dealloc];
  5257. }
  5258. - (instancetype)copyWithZone:(NSZone *)zone {
  5259. return [[GPBUInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  5260. }
  5261. - (BOOL)isEqual:(id)other {
  5262. if (self == other) {
  5263. return YES;
  5264. }
  5265. if (![other isKindOfClass:[GPBUInt64FloatDictionary class]]) {
  5266. return NO;
  5267. }
  5268. GPBUInt64FloatDictionary *otherDictionary = other;
  5269. return [_dictionary isEqual:otherDictionary->_dictionary];
  5270. }
  5271. - (NSUInteger)hash {
  5272. return _dictionary.count;
  5273. }
  5274. - (NSString *)description {
  5275. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5276. }
  5277. - (NSUInteger)count {
  5278. return _dictionary.count;
  5279. }
  5280. - (void)enumerateKeysAndFloatsUsingBlock:
  5281. (void (NS_NOESCAPE ^)(uint64_t key, float value, BOOL *stop))block {
  5282. BOOL stop = NO;
  5283. NSDictionary *internal = _dictionary;
  5284. NSEnumerator *keys = [internal keyEnumerator];
  5285. NSNumber *aKey;
  5286. while ((aKey = [keys nextObject])) {
  5287. NSNumber *aValue = internal[aKey];
  5288. block([aKey unsignedLongLongValue], [aValue floatValue], &stop);
  5289. if (stop) {
  5290. break;
  5291. }
  5292. }
  5293. }
  5294. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5295. NSDictionary *internal = _dictionary;
  5296. NSUInteger count = internal.count;
  5297. if (count == 0) {
  5298. return 0;
  5299. }
  5300. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5301. GPBDataType keyDataType = field.mapKeyDataType;
  5302. size_t result = 0;
  5303. NSEnumerator *keys = [internal keyEnumerator];
  5304. NSNumber *aKey;
  5305. while ((aKey = [keys nextObject])) {
  5306. NSNumber *aValue = internal[aKey];
  5307. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5308. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  5309. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5310. }
  5311. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5312. result += tagSize * count;
  5313. return result;
  5314. }
  5315. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5316. asField:(GPBFieldDescriptor *)field {
  5317. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5318. GPBDataType keyDataType = field.mapKeyDataType;
  5319. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5320. NSDictionary *internal = _dictionary;
  5321. NSEnumerator *keys = [internal keyEnumerator];
  5322. NSNumber *aKey;
  5323. while ((aKey = [keys nextObject])) {
  5324. NSNumber *aValue = internal[aKey];
  5325. [outputStream writeInt32NoTag:tag];
  5326. // Write the size of the message.
  5327. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5328. float unwrappedValue = [aValue floatValue];
  5329. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5330. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5331. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5332. // Write the fields.
  5333. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5334. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5335. }
  5336. }
  5337. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5338. forGPBGenericValueKey:(GPBGenericValue *)key {
  5339. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueUInt64)];
  5340. }
  5341. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  5342. [self enumerateKeysAndFloatsUsingBlock:^(uint64_t key, float value, BOOL *stop) {
  5343. #pragma unused(stop)
  5344. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  5345. }];
  5346. }
  5347. - (BOOL)getFloat:(nullable float *)value forKey:(uint64_t)key {
  5348. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5349. if (wrapped && value) {
  5350. *value = [wrapped floatValue];
  5351. }
  5352. return (wrapped != NULL);
  5353. }
  5354. - (void)addEntriesFromDictionary:(GPBUInt64FloatDictionary *)otherDictionary {
  5355. if (otherDictionary) {
  5356. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5357. if (_autocreator) {
  5358. GPBAutocreatedDictionaryModified(_autocreator, self);
  5359. }
  5360. }
  5361. }
  5362. - (void)setFloat:(float)value forKey:(uint64_t)key {
  5363. [_dictionary setObject:@(value) forKey:@(key)];
  5364. if (_autocreator) {
  5365. GPBAutocreatedDictionaryModified(_autocreator, self);
  5366. }
  5367. }
  5368. - (void)removeFloatForKey:(uint64_t)aKey {
  5369. [_dictionary removeObjectForKey:@(aKey)];
  5370. }
  5371. - (void)removeAll {
  5372. [_dictionary removeAllObjects];
  5373. }
  5374. @end
  5375. #pragma mark - UInt64 -> Double
  5376. @implementation GPBUInt64DoubleDictionary {
  5377. @package
  5378. NSMutableDictionary *_dictionary;
  5379. }
  5380. - (instancetype)init {
  5381. return [self initWithDoubles:NULL forKeys:NULL count:0];
  5382. }
  5383. - (instancetype)initWithDoubles:(const double [])values
  5384. forKeys:(const uint64_t [])keys
  5385. count:(NSUInteger)count {
  5386. self = [super init];
  5387. if (self) {
  5388. _dictionary = [[NSMutableDictionary alloc] init];
  5389. if (count && values && keys) {
  5390. for (NSUInteger i = 0; i < count; ++i) {
  5391. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5392. }
  5393. }
  5394. }
  5395. return self;
  5396. }
  5397. - (instancetype)initWithDictionary:(GPBUInt64DoubleDictionary *)dictionary {
  5398. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  5399. if (self) {
  5400. if (dictionary) {
  5401. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5402. }
  5403. }
  5404. return self;
  5405. }
  5406. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5407. #pragma unused(numItems)
  5408. return [self initWithDoubles:NULL forKeys:NULL count:0];
  5409. }
  5410. - (void)dealloc {
  5411. NSAssert(!_autocreator,
  5412. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5413. [self class], _autocreator);
  5414. [_dictionary release];
  5415. [super dealloc];
  5416. }
  5417. - (instancetype)copyWithZone:(NSZone *)zone {
  5418. return [[GPBUInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  5419. }
  5420. - (BOOL)isEqual:(id)other {
  5421. if (self == other) {
  5422. return YES;
  5423. }
  5424. if (![other isKindOfClass:[GPBUInt64DoubleDictionary class]]) {
  5425. return NO;
  5426. }
  5427. GPBUInt64DoubleDictionary *otherDictionary = other;
  5428. return [_dictionary isEqual:otherDictionary->_dictionary];
  5429. }
  5430. - (NSUInteger)hash {
  5431. return _dictionary.count;
  5432. }
  5433. - (NSString *)description {
  5434. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5435. }
  5436. - (NSUInteger)count {
  5437. return _dictionary.count;
  5438. }
  5439. - (void)enumerateKeysAndDoublesUsingBlock:
  5440. (void (NS_NOESCAPE ^)(uint64_t key, double value, BOOL *stop))block {
  5441. BOOL stop = NO;
  5442. NSDictionary *internal = _dictionary;
  5443. NSEnumerator *keys = [internal keyEnumerator];
  5444. NSNumber *aKey;
  5445. while ((aKey = [keys nextObject])) {
  5446. NSNumber *aValue = internal[aKey];
  5447. block([aKey unsignedLongLongValue], [aValue doubleValue], &stop);
  5448. if (stop) {
  5449. break;
  5450. }
  5451. }
  5452. }
  5453. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5454. NSDictionary *internal = _dictionary;
  5455. NSUInteger count = internal.count;
  5456. if (count == 0) {
  5457. return 0;
  5458. }
  5459. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5460. GPBDataType keyDataType = field.mapKeyDataType;
  5461. size_t result = 0;
  5462. NSEnumerator *keys = [internal keyEnumerator];
  5463. NSNumber *aKey;
  5464. while ((aKey = [keys nextObject])) {
  5465. NSNumber *aValue = internal[aKey];
  5466. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5467. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  5468. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5469. }
  5470. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5471. result += tagSize * count;
  5472. return result;
  5473. }
  5474. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5475. asField:(GPBFieldDescriptor *)field {
  5476. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5477. GPBDataType keyDataType = field.mapKeyDataType;
  5478. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5479. NSDictionary *internal = _dictionary;
  5480. NSEnumerator *keys = [internal keyEnumerator];
  5481. NSNumber *aKey;
  5482. while ((aKey = [keys nextObject])) {
  5483. NSNumber *aValue = internal[aKey];
  5484. [outputStream writeInt32NoTag:tag];
  5485. // Write the size of the message.
  5486. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5487. double unwrappedValue = [aValue doubleValue];
  5488. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5489. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5490. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5491. // Write the fields.
  5492. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5493. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5494. }
  5495. }
  5496. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5497. forGPBGenericValueKey:(GPBGenericValue *)key {
  5498. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueUInt64)];
  5499. }
  5500. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  5501. [self enumerateKeysAndDoublesUsingBlock:^(uint64_t key, double value, BOOL *stop) {
  5502. #pragma unused(stop)
  5503. block([NSString stringWithFormat:@"%llu", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  5504. }];
  5505. }
  5506. - (BOOL)getDouble:(nullable double *)value forKey:(uint64_t)key {
  5507. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5508. if (wrapped && value) {
  5509. *value = [wrapped doubleValue];
  5510. }
  5511. return (wrapped != NULL);
  5512. }
  5513. - (void)addEntriesFromDictionary:(GPBUInt64DoubleDictionary *)otherDictionary {
  5514. if (otherDictionary) {
  5515. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5516. if (_autocreator) {
  5517. GPBAutocreatedDictionaryModified(_autocreator, self);
  5518. }
  5519. }
  5520. }
  5521. - (void)setDouble:(double)value forKey:(uint64_t)key {
  5522. [_dictionary setObject:@(value) forKey:@(key)];
  5523. if (_autocreator) {
  5524. GPBAutocreatedDictionaryModified(_autocreator, self);
  5525. }
  5526. }
  5527. - (void)removeDoubleForKey:(uint64_t)aKey {
  5528. [_dictionary removeObjectForKey:@(aKey)];
  5529. }
  5530. - (void)removeAll {
  5531. [_dictionary removeAllObjects];
  5532. }
  5533. @end
  5534. #pragma mark - UInt64 -> Enum
  5535. @implementation GPBUInt64EnumDictionary {
  5536. @package
  5537. NSMutableDictionary *_dictionary;
  5538. GPBEnumValidationFunc _validationFunc;
  5539. }
  5540. @synthesize validationFunc = _validationFunc;
  5541. - (instancetype)init {
  5542. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  5543. }
  5544. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  5545. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  5546. }
  5547. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  5548. rawValues:(const int32_t [])rawValues
  5549. forKeys:(const uint64_t [])keys
  5550. count:(NSUInteger)count {
  5551. self = [super init];
  5552. if (self) {
  5553. _dictionary = [[NSMutableDictionary alloc] init];
  5554. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  5555. if (count && rawValues && keys) {
  5556. for (NSUInteger i = 0; i < count; ++i) {
  5557. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  5558. }
  5559. }
  5560. }
  5561. return self;
  5562. }
  5563. - (instancetype)initWithDictionary:(GPBUInt64EnumDictionary *)dictionary {
  5564. self = [self initWithValidationFunction:dictionary.validationFunc
  5565. rawValues:NULL
  5566. forKeys:NULL
  5567. count:0];
  5568. if (self) {
  5569. if (dictionary) {
  5570. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5571. }
  5572. }
  5573. return self;
  5574. }
  5575. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  5576. capacity:(NSUInteger)numItems {
  5577. #pragma unused(numItems)
  5578. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  5579. }
  5580. - (void)dealloc {
  5581. NSAssert(!_autocreator,
  5582. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5583. [self class], _autocreator);
  5584. [_dictionary release];
  5585. [super dealloc];
  5586. }
  5587. - (instancetype)copyWithZone:(NSZone *)zone {
  5588. return [[GPBUInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  5589. }
  5590. - (BOOL)isEqual:(id)other {
  5591. if (self == other) {
  5592. return YES;
  5593. }
  5594. if (![other isKindOfClass:[GPBUInt64EnumDictionary class]]) {
  5595. return NO;
  5596. }
  5597. GPBUInt64EnumDictionary *otherDictionary = other;
  5598. return [_dictionary isEqual:otherDictionary->_dictionary];
  5599. }
  5600. - (NSUInteger)hash {
  5601. return _dictionary.count;
  5602. }
  5603. - (NSString *)description {
  5604. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5605. }
  5606. - (NSUInteger)count {
  5607. return _dictionary.count;
  5608. }
  5609. - (void)enumerateKeysAndRawValuesUsingBlock:
  5610. (void (NS_NOESCAPE ^)(uint64_t key, int32_t value, BOOL *stop))block {
  5611. BOOL stop = NO;
  5612. NSDictionary *internal = _dictionary;
  5613. NSEnumerator *keys = [internal keyEnumerator];
  5614. NSNumber *aKey;
  5615. while ((aKey = [keys nextObject])) {
  5616. NSNumber *aValue = internal[aKey];
  5617. block([aKey unsignedLongLongValue], [aValue intValue], &stop);
  5618. if (stop) {
  5619. break;
  5620. }
  5621. }
  5622. }
  5623. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5624. NSDictionary *internal = _dictionary;
  5625. NSUInteger count = internal.count;
  5626. if (count == 0) {
  5627. return 0;
  5628. }
  5629. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5630. GPBDataType keyDataType = field.mapKeyDataType;
  5631. size_t result = 0;
  5632. NSEnumerator *keys = [internal keyEnumerator];
  5633. NSNumber *aKey;
  5634. while ((aKey = [keys nextObject])) {
  5635. NSNumber *aValue = internal[aKey];
  5636. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5637. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  5638. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5639. }
  5640. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5641. result += tagSize * count;
  5642. return result;
  5643. }
  5644. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5645. asField:(GPBFieldDescriptor *)field {
  5646. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5647. GPBDataType keyDataType = field.mapKeyDataType;
  5648. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5649. NSDictionary *internal = _dictionary;
  5650. NSEnumerator *keys = [internal keyEnumerator];
  5651. NSNumber *aKey;
  5652. while ((aKey = [keys nextObject])) {
  5653. NSNumber *aValue = internal[aKey];
  5654. [outputStream writeInt32NoTag:tag];
  5655. // Write the size of the message.
  5656. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5657. int32_t unwrappedValue = [aValue intValue];
  5658. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5659. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5660. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5661. // Write the fields.
  5662. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5663. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5664. }
  5665. }
  5666. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  5667. forKey:(GPBGenericValue *)key
  5668. keyDataType:(GPBDataType)keyDataType {
  5669. size_t msgSize = ComputeDictUInt64FieldSize(key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  5670. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  5671. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  5672. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  5673. WriteDictUInt64Field(outputStream, key->valueUInt64, kMapKeyFieldNumber, keyDataType);
  5674. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  5675. [outputStream release];
  5676. return data;
  5677. }
  5678. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5679. forGPBGenericValueKey:(GPBGenericValue *)key {
  5680. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueUInt64)];
  5681. }
  5682. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  5683. [self enumerateKeysAndRawValuesUsingBlock:^(uint64_t key, int32_t value, BOOL *stop) {
  5684. #pragma unused(stop)
  5685. block([NSString stringWithFormat:@"%llu", key], @(value));
  5686. }];
  5687. }
  5688. - (BOOL)getEnum:(int32_t *)value forKey:(uint64_t)key {
  5689. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5690. if (wrapped && value) {
  5691. int32_t result = [wrapped intValue];
  5692. if (!_validationFunc(result)) {
  5693. result = kGPBUnrecognizedEnumeratorValue;
  5694. }
  5695. *value = result;
  5696. }
  5697. return (wrapped != NULL);
  5698. }
  5699. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(uint64_t)key {
  5700. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  5701. if (wrapped && rawValue) {
  5702. *rawValue = [wrapped intValue];
  5703. }
  5704. return (wrapped != NULL);
  5705. }
  5706. - (void)enumerateKeysAndEnumsUsingBlock:
  5707. (void (NS_NOESCAPE ^)(uint64_t key, int32_t value, BOOL *stop))block {
  5708. GPBEnumValidationFunc func = _validationFunc;
  5709. BOOL stop = NO;
  5710. NSEnumerator *keys = [_dictionary keyEnumerator];
  5711. NSNumber *aKey;
  5712. while ((aKey = [keys nextObject])) {
  5713. NSNumber *aValue = _dictionary[aKey];
  5714. int32_t unwrapped = [aValue intValue];
  5715. if (!func(unwrapped)) {
  5716. unwrapped = kGPBUnrecognizedEnumeratorValue;
  5717. }
  5718. block([aKey unsignedLongLongValue], unwrapped, &stop);
  5719. if (stop) {
  5720. break;
  5721. }
  5722. }
  5723. }
  5724. - (void)addRawEntriesFromDictionary:(GPBUInt64EnumDictionary *)otherDictionary {
  5725. if (otherDictionary) {
  5726. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5727. if (_autocreator) {
  5728. GPBAutocreatedDictionaryModified(_autocreator, self);
  5729. }
  5730. }
  5731. }
  5732. - (void)setRawValue:(int32_t)value forKey:(uint64_t)key {
  5733. [_dictionary setObject:@(value) forKey:@(key)];
  5734. if (_autocreator) {
  5735. GPBAutocreatedDictionaryModified(_autocreator, self);
  5736. }
  5737. }
  5738. - (void)removeEnumForKey:(uint64_t)aKey {
  5739. [_dictionary removeObjectForKey:@(aKey)];
  5740. }
  5741. - (void)removeAll {
  5742. [_dictionary removeAllObjects];
  5743. }
  5744. - (void)setEnum:(int32_t)value forKey:(uint64_t)key {
  5745. if (!_validationFunc(value)) {
  5746. [NSException raise:NSInvalidArgumentException
  5747. format:@"GPBUInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  5748. value];
  5749. }
  5750. [_dictionary setObject:@(value) forKey:@(key)];
  5751. if (_autocreator) {
  5752. GPBAutocreatedDictionaryModified(_autocreator, self);
  5753. }
  5754. }
  5755. @end
  5756. #pragma mark - UInt64 -> Object
  5757. @implementation GPBUInt64ObjectDictionary {
  5758. @package
  5759. NSMutableDictionary *_dictionary;
  5760. }
  5761. - (instancetype)init {
  5762. return [self initWithObjects:NULL forKeys:NULL count:0];
  5763. }
  5764. - (instancetype)initWithObjects:(const id [])objects
  5765. forKeys:(const uint64_t [])keys
  5766. count:(NSUInteger)count {
  5767. self = [super init];
  5768. if (self) {
  5769. _dictionary = [[NSMutableDictionary alloc] init];
  5770. if (count && objects && keys) {
  5771. for (NSUInteger i = 0; i < count; ++i) {
  5772. if (!objects[i]) {
  5773. [NSException raise:NSInvalidArgumentException
  5774. format:@"Attempting to add nil object to a Dictionary"];
  5775. }
  5776. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  5777. }
  5778. }
  5779. }
  5780. return self;
  5781. }
  5782. - (instancetype)initWithDictionary:(GPBUInt64ObjectDictionary *)dictionary {
  5783. self = [self initWithObjects:NULL forKeys:NULL count:0];
  5784. if (self) {
  5785. if (dictionary) {
  5786. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5787. }
  5788. }
  5789. return self;
  5790. }
  5791. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5792. #pragma unused(numItems)
  5793. return [self initWithObjects:NULL forKeys:NULL count:0];
  5794. }
  5795. - (void)dealloc {
  5796. NSAssert(!_autocreator,
  5797. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5798. [self class], _autocreator);
  5799. [_dictionary release];
  5800. [super dealloc];
  5801. }
  5802. - (instancetype)copyWithZone:(NSZone *)zone {
  5803. return [[GPBUInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  5804. }
  5805. - (BOOL)isEqual:(id)other {
  5806. if (self == other) {
  5807. return YES;
  5808. }
  5809. if (![other isKindOfClass:[GPBUInt64ObjectDictionary class]]) {
  5810. return NO;
  5811. }
  5812. GPBUInt64ObjectDictionary *otherDictionary = other;
  5813. return [_dictionary isEqual:otherDictionary->_dictionary];
  5814. }
  5815. - (NSUInteger)hash {
  5816. return _dictionary.count;
  5817. }
  5818. - (NSString *)description {
  5819. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  5820. }
  5821. - (NSUInteger)count {
  5822. return _dictionary.count;
  5823. }
  5824. - (void)enumerateKeysAndObjectsUsingBlock:
  5825. (void (NS_NOESCAPE ^)(uint64_t key, id object, BOOL *stop))block {
  5826. BOOL stop = NO;
  5827. NSDictionary *internal = _dictionary;
  5828. NSEnumerator *keys = [internal keyEnumerator];
  5829. NSNumber *aKey;
  5830. while ((aKey = [keys nextObject])) {
  5831. id aObject = internal[aKey];
  5832. block([aKey unsignedLongLongValue], aObject, &stop);
  5833. if (stop) {
  5834. break;
  5835. }
  5836. }
  5837. }
  5838. - (BOOL)isInitialized {
  5839. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  5840. if (!msg.initialized) {
  5841. return NO;
  5842. }
  5843. }
  5844. return YES;
  5845. }
  5846. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  5847. GPBUInt64ObjectDictionary *newDict =
  5848. [[GPBUInt64ObjectDictionary alloc] init];
  5849. NSEnumerator *keys = [_dictionary keyEnumerator];
  5850. id aKey;
  5851. NSMutableDictionary *internalDict = newDict->_dictionary;
  5852. while ((aKey = [keys nextObject])) {
  5853. GPBMessage *msg = _dictionary[aKey];
  5854. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  5855. [internalDict setObject:copiedMsg forKey:aKey];
  5856. [copiedMsg release];
  5857. }
  5858. return newDict;
  5859. }
  5860. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  5861. NSDictionary *internal = _dictionary;
  5862. NSUInteger count = internal.count;
  5863. if (count == 0) {
  5864. return 0;
  5865. }
  5866. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5867. GPBDataType keyDataType = field.mapKeyDataType;
  5868. size_t result = 0;
  5869. NSEnumerator *keys = [internal keyEnumerator];
  5870. NSNumber *aKey;
  5871. while ((aKey = [keys nextObject])) {
  5872. id aObject = internal[aKey];
  5873. size_t msgSize = ComputeDictUInt64FieldSize([aKey unsignedLongLongValue], kMapKeyFieldNumber, keyDataType);
  5874. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  5875. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  5876. }
  5877. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  5878. result += tagSize * count;
  5879. return result;
  5880. }
  5881. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  5882. asField:(GPBFieldDescriptor *)field {
  5883. GPBDataType valueDataType = GPBGetFieldDataType(field);
  5884. GPBDataType keyDataType = field.mapKeyDataType;
  5885. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  5886. NSDictionary *internal = _dictionary;
  5887. NSEnumerator *keys = [internal keyEnumerator];
  5888. NSNumber *aKey;
  5889. while ((aKey = [keys nextObject])) {
  5890. id aObject = internal[aKey];
  5891. [outputStream writeInt32NoTag:tag];
  5892. // Write the size of the message.
  5893. uint64_t unwrappedKey = [aKey unsignedLongLongValue];
  5894. id unwrappedValue = aObject;
  5895. size_t msgSize = ComputeDictUInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5896. msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  5897. [outputStream writeInt32NoTag:(int32_t)msgSize];
  5898. // Write the fields.
  5899. WriteDictUInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  5900. WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  5901. }
  5902. }
  5903. - (void)setGPBGenericValue:(GPBGenericValue *)value
  5904. forGPBGenericValueKey:(GPBGenericValue *)key {
  5905. [_dictionary setObject:value->valueString forKey:@(key->valueUInt64)];
  5906. }
  5907. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  5908. [self enumerateKeysAndObjectsUsingBlock:^(uint64_t key, id object, BOOL *stop) {
  5909. #pragma unused(stop)
  5910. block([NSString stringWithFormat:@"%llu", key], object);
  5911. }];
  5912. }
  5913. - (id)objectForKey:(uint64_t)key {
  5914. id result = [_dictionary objectForKey:@(key)];
  5915. return result;
  5916. }
  5917. - (void)addEntriesFromDictionary:(GPBUInt64ObjectDictionary *)otherDictionary {
  5918. if (otherDictionary) {
  5919. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  5920. if (_autocreator) {
  5921. GPBAutocreatedDictionaryModified(_autocreator, self);
  5922. }
  5923. }
  5924. }
  5925. - (void)setObject:(id)object forKey:(uint64_t)key {
  5926. if (!object) {
  5927. [NSException raise:NSInvalidArgumentException
  5928. format:@"Attempting to add nil object to a Dictionary"];
  5929. }
  5930. [_dictionary setObject:object forKey:@(key)];
  5931. if (_autocreator) {
  5932. GPBAutocreatedDictionaryModified(_autocreator, self);
  5933. }
  5934. }
  5935. - (void)removeObjectForKey:(uint64_t)aKey {
  5936. [_dictionary removeObjectForKey:@(aKey)];
  5937. }
  5938. - (void)removeAll {
  5939. [_dictionary removeAllObjects];
  5940. }
  5941. @end
  5942. // clang-format on
  5943. //%PDDM-EXPAND DICTIONARY_IMPL_FOR_POD_KEY(Int64, int64_t)
  5944. // This block of code is generated, do not edit it directly.
  5945. // clang-format off
  5946. #pragma mark - Int64 -> UInt32
  5947. @implementation GPBInt64UInt32Dictionary {
  5948. @package
  5949. NSMutableDictionary *_dictionary;
  5950. }
  5951. - (instancetype)init {
  5952. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  5953. }
  5954. - (instancetype)initWithUInt32s:(const uint32_t [])values
  5955. forKeys:(const int64_t [])keys
  5956. count:(NSUInteger)count {
  5957. self = [super init];
  5958. if (self) {
  5959. _dictionary = [[NSMutableDictionary alloc] init];
  5960. if (count && values && keys) {
  5961. for (NSUInteger i = 0; i < count; ++i) {
  5962. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  5963. }
  5964. }
  5965. }
  5966. return self;
  5967. }
  5968. - (instancetype)initWithDictionary:(GPBInt64UInt32Dictionary *)dictionary {
  5969. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  5970. if (self) {
  5971. if (dictionary) {
  5972. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  5973. }
  5974. }
  5975. return self;
  5976. }
  5977. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  5978. #pragma unused(numItems)
  5979. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  5980. }
  5981. - (void)dealloc {
  5982. NSAssert(!_autocreator,
  5983. @"%@: Autocreator must be cleared before release, autocreator: %@",
  5984. [self class], _autocreator);
  5985. [_dictionary release];
  5986. [super dealloc];
  5987. }
  5988. - (instancetype)copyWithZone:(NSZone *)zone {
  5989. return [[GPBInt64UInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  5990. }
  5991. - (BOOL)isEqual:(id)other {
  5992. if (self == other) {
  5993. return YES;
  5994. }
  5995. if (![other isKindOfClass:[GPBInt64UInt32Dictionary class]]) {
  5996. return NO;
  5997. }
  5998. GPBInt64UInt32Dictionary *otherDictionary = other;
  5999. return [_dictionary isEqual:otherDictionary->_dictionary];
  6000. }
  6001. - (NSUInteger)hash {
  6002. return _dictionary.count;
  6003. }
  6004. - (NSString *)description {
  6005. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6006. }
  6007. - (NSUInteger)count {
  6008. return _dictionary.count;
  6009. }
  6010. - (void)enumerateKeysAndUInt32sUsingBlock:
  6011. (void (NS_NOESCAPE ^)(int64_t key, uint32_t value, BOOL *stop))block {
  6012. BOOL stop = NO;
  6013. NSDictionary *internal = _dictionary;
  6014. NSEnumerator *keys = [internal keyEnumerator];
  6015. NSNumber *aKey;
  6016. while ((aKey = [keys nextObject])) {
  6017. NSNumber *aValue = internal[aKey];
  6018. block([aKey longLongValue], [aValue unsignedIntValue], &stop);
  6019. if (stop) {
  6020. break;
  6021. }
  6022. }
  6023. }
  6024. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6025. NSDictionary *internal = _dictionary;
  6026. NSUInteger count = internal.count;
  6027. if (count == 0) {
  6028. return 0;
  6029. }
  6030. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6031. GPBDataType keyDataType = field.mapKeyDataType;
  6032. size_t result = 0;
  6033. NSEnumerator *keys = [internal keyEnumerator];
  6034. NSNumber *aKey;
  6035. while ((aKey = [keys nextObject])) {
  6036. NSNumber *aValue = internal[aKey];
  6037. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6038. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  6039. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6040. }
  6041. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6042. result += tagSize * count;
  6043. return result;
  6044. }
  6045. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6046. asField:(GPBFieldDescriptor *)field {
  6047. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6048. GPBDataType keyDataType = field.mapKeyDataType;
  6049. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6050. NSDictionary *internal = _dictionary;
  6051. NSEnumerator *keys = [internal keyEnumerator];
  6052. NSNumber *aKey;
  6053. while ((aKey = [keys nextObject])) {
  6054. NSNumber *aValue = internal[aKey];
  6055. [outputStream writeInt32NoTag:tag];
  6056. // Write the size of the message.
  6057. int64_t unwrappedKey = [aKey longLongValue];
  6058. uint32_t unwrappedValue = [aValue unsignedIntValue];
  6059. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6060. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6061. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6062. // Write the fields.
  6063. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6064. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6065. }
  6066. }
  6067. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6068. forGPBGenericValueKey:(GPBGenericValue *)key {
  6069. [_dictionary setObject:@(value->valueUInt32) forKey:@(key->valueInt64)];
  6070. }
  6071. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  6072. [self enumerateKeysAndUInt32sUsingBlock:^(int64_t key, uint32_t value, BOOL *stop) {
  6073. #pragma unused(stop)
  6074. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%u", value]);
  6075. }];
  6076. }
  6077. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(int64_t)key {
  6078. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6079. if (wrapped && value) {
  6080. *value = [wrapped unsignedIntValue];
  6081. }
  6082. return (wrapped != NULL);
  6083. }
  6084. - (void)addEntriesFromDictionary:(GPBInt64UInt32Dictionary *)otherDictionary {
  6085. if (otherDictionary) {
  6086. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6087. if (_autocreator) {
  6088. GPBAutocreatedDictionaryModified(_autocreator, self);
  6089. }
  6090. }
  6091. }
  6092. - (void)setUInt32:(uint32_t)value forKey:(int64_t)key {
  6093. [_dictionary setObject:@(value) forKey:@(key)];
  6094. if (_autocreator) {
  6095. GPBAutocreatedDictionaryModified(_autocreator, self);
  6096. }
  6097. }
  6098. - (void)removeUInt32ForKey:(int64_t)aKey {
  6099. [_dictionary removeObjectForKey:@(aKey)];
  6100. }
  6101. - (void)removeAll {
  6102. [_dictionary removeAllObjects];
  6103. }
  6104. @end
  6105. #pragma mark - Int64 -> Int32
  6106. @implementation GPBInt64Int32Dictionary {
  6107. @package
  6108. NSMutableDictionary *_dictionary;
  6109. }
  6110. - (instancetype)init {
  6111. return [self initWithInt32s:NULL forKeys:NULL count:0];
  6112. }
  6113. - (instancetype)initWithInt32s:(const int32_t [])values
  6114. forKeys:(const int64_t [])keys
  6115. count:(NSUInteger)count {
  6116. self = [super init];
  6117. if (self) {
  6118. _dictionary = [[NSMutableDictionary alloc] init];
  6119. if (count && values && keys) {
  6120. for (NSUInteger i = 0; i < count; ++i) {
  6121. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6122. }
  6123. }
  6124. }
  6125. return self;
  6126. }
  6127. - (instancetype)initWithDictionary:(GPBInt64Int32Dictionary *)dictionary {
  6128. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  6129. if (self) {
  6130. if (dictionary) {
  6131. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6132. }
  6133. }
  6134. return self;
  6135. }
  6136. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6137. #pragma unused(numItems)
  6138. return [self initWithInt32s:NULL forKeys:NULL count:0];
  6139. }
  6140. - (void)dealloc {
  6141. NSAssert(!_autocreator,
  6142. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6143. [self class], _autocreator);
  6144. [_dictionary release];
  6145. [super dealloc];
  6146. }
  6147. - (instancetype)copyWithZone:(NSZone *)zone {
  6148. return [[GPBInt64Int32Dictionary allocWithZone:zone] initWithDictionary:self];
  6149. }
  6150. - (BOOL)isEqual:(id)other {
  6151. if (self == other) {
  6152. return YES;
  6153. }
  6154. if (![other isKindOfClass:[GPBInt64Int32Dictionary class]]) {
  6155. return NO;
  6156. }
  6157. GPBInt64Int32Dictionary *otherDictionary = other;
  6158. return [_dictionary isEqual:otherDictionary->_dictionary];
  6159. }
  6160. - (NSUInteger)hash {
  6161. return _dictionary.count;
  6162. }
  6163. - (NSString *)description {
  6164. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6165. }
  6166. - (NSUInteger)count {
  6167. return _dictionary.count;
  6168. }
  6169. - (void)enumerateKeysAndInt32sUsingBlock:
  6170. (void (NS_NOESCAPE ^)(int64_t key, int32_t value, BOOL *stop))block {
  6171. BOOL stop = NO;
  6172. NSDictionary *internal = _dictionary;
  6173. NSEnumerator *keys = [internal keyEnumerator];
  6174. NSNumber *aKey;
  6175. while ((aKey = [keys nextObject])) {
  6176. NSNumber *aValue = internal[aKey];
  6177. block([aKey longLongValue], [aValue intValue], &stop);
  6178. if (stop) {
  6179. break;
  6180. }
  6181. }
  6182. }
  6183. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6184. NSDictionary *internal = _dictionary;
  6185. NSUInteger count = internal.count;
  6186. if (count == 0) {
  6187. return 0;
  6188. }
  6189. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6190. GPBDataType keyDataType = field.mapKeyDataType;
  6191. size_t result = 0;
  6192. NSEnumerator *keys = [internal keyEnumerator];
  6193. NSNumber *aKey;
  6194. while ((aKey = [keys nextObject])) {
  6195. NSNumber *aValue = internal[aKey];
  6196. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6197. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  6198. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6199. }
  6200. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6201. result += tagSize * count;
  6202. return result;
  6203. }
  6204. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6205. asField:(GPBFieldDescriptor *)field {
  6206. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6207. GPBDataType keyDataType = field.mapKeyDataType;
  6208. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6209. NSDictionary *internal = _dictionary;
  6210. NSEnumerator *keys = [internal keyEnumerator];
  6211. NSNumber *aKey;
  6212. while ((aKey = [keys nextObject])) {
  6213. NSNumber *aValue = internal[aKey];
  6214. [outputStream writeInt32NoTag:tag];
  6215. // Write the size of the message.
  6216. int64_t unwrappedKey = [aKey longLongValue];
  6217. int32_t unwrappedValue = [aValue intValue];
  6218. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6219. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6220. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6221. // Write the fields.
  6222. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6223. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6224. }
  6225. }
  6226. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6227. forGPBGenericValueKey:(GPBGenericValue *)key {
  6228. [_dictionary setObject:@(value->valueInt32) forKey:@(key->valueInt64)];
  6229. }
  6230. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  6231. [self enumerateKeysAndInt32sUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  6232. #pragma unused(stop)
  6233. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%d", value]);
  6234. }];
  6235. }
  6236. - (BOOL)getInt32:(nullable int32_t *)value forKey:(int64_t)key {
  6237. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6238. if (wrapped && value) {
  6239. *value = [wrapped intValue];
  6240. }
  6241. return (wrapped != NULL);
  6242. }
  6243. - (void)addEntriesFromDictionary:(GPBInt64Int32Dictionary *)otherDictionary {
  6244. if (otherDictionary) {
  6245. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6246. if (_autocreator) {
  6247. GPBAutocreatedDictionaryModified(_autocreator, self);
  6248. }
  6249. }
  6250. }
  6251. - (void)setInt32:(int32_t)value forKey:(int64_t)key {
  6252. [_dictionary setObject:@(value) forKey:@(key)];
  6253. if (_autocreator) {
  6254. GPBAutocreatedDictionaryModified(_autocreator, self);
  6255. }
  6256. }
  6257. - (void)removeInt32ForKey:(int64_t)aKey {
  6258. [_dictionary removeObjectForKey:@(aKey)];
  6259. }
  6260. - (void)removeAll {
  6261. [_dictionary removeAllObjects];
  6262. }
  6263. @end
  6264. #pragma mark - Int64 -> UInt64
  6265. @implementation GPBInt64UInt64Dictionary {
  6266. @package
  6267. NSMutableDictionary *_dictionary;
  6268. }
  6269. - (instancetype)init {
  6270. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  6271. }
  6272. - (instancetype)initWithUInt64s:(const uint64_t [])values
  6273. forKeys:(const int64_t [])keys
  6274. count:(NSUInteger)count {
  6275. self = [super init];
  6276. if (self) {
  6277. _dictionary = [[NSMutableDictionary alloc] init];
  6278. if (count && values && keys) {
  6279. for (NSUInteger i = 0; i < count; ++i) {
  6280. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6281. }
  6282. }
  6283. }
  6284. return self;
  6285. }
  6286. - (instancetype)initWithDictionary:(GPBInt64UInt64Dictionary *)dictionary {
  6287. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  6288. if (self) {
  6289. if (dictionary) {
  6290. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6291. }
  6292. }
  6293. return self;
  6294. }
  6295. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6296. #pragma unused(numItems)
  6297. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  6298. }
  6299. - (void)dealloc {
  6300. NSAssert(!_autocreator,
  6301. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6302. [self class], _autocreator);
  6303. [_dictionary release];
  6304. [super dealloc];
  6305. }
  6306. - (instancetype)copyWithZone:(NSZone *)zone {
  6307. return [[GPBInt64UInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  6308. }
  6309. - (BOOL)isEqual:(id)other {
  6310. if (self == other) {
  6311. return YES;
  6312. }
  6313. if (![other isKindOfClass:[GPBInt64UInt64Dictionary class]]) {
  6314. return NO;
  6315. }
  6316. GPBInt64UInt64Dictionary *otherDictionary = other;
  6317. return [_dictionary isEqual:otherDictionary->_dictionary];
  6318. }
  6319. - (NSUInteger)hash {
  6320. return _dictionary.count;
  6321. }
  6322. - (NSString *)description {
  6323. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6324. }
  6325. - (NSUInteger)count {
  6326. return _dictionary.count;
  6327. }
  6328. - (void)enumerateKeysAndUInt64sUsingBlock:
  6329. (void (NS_NOESCAPE ^)(int64_t key, uint64_t value, BOOL *stop))block {
  6330. BOOL stop = NO;
  6331. NSDictionary *internal = _dictionary;
  6332. NSEnumerator *keys = [internal keyEnumerator];
  6333. NSNumber *aKey;
  6334. while ((aKey = [keys nextObject])) {
  6335. NSNumber *aValue = internal[aKey];
  6336. block([aKey longLongValue], [aValue unsignedLongLongValue], &stop);
  6337. if (stop) {
  6338. break;
  6339. }
  6340. }
  6341. }
  6342. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6343. NSDictionary *internal = _dictionary;
  6344. NSUInteger count = internal.count;
  6345. if (count == 0) {
  6346. return 0;
  6347. }
  6348. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6349. GPBDataType keyDataType = field.mapKeyDataType;
  6350. size_t result = 0;
  6351. NSEnumerator *keys = [internal keyEnumerator];
  6352. NSNumber *aKey;
  6353. while ((aKey = [keys nextObject])) {
  6354. NSNumber *aValue = internal[aKey];
  6355. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6356. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  6357. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6358. }
  6359. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6360. result += tagSize * count;
  6361. return result;
  6362. }
  6363. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6364. asField:(GPBFieldDescriptor *)field {
  6365. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6366. GPBDataType keyDataType = field.mapKeyDataType;
  6367. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6368. NSDictionary *internal = _dictionary;
  6369. NSEnumerator *keys = [internal keyEnumerator];
  6370. NSNumber *aKey;
  6371. while ((aKey = [keys nextObject])) {
  6372. NSNumber *aValue = internal[aKey];
  6373. [outputStream writeInt32NoTag:tag];
  6374. // Write the size of the message.
  6375. int64_t unwrappedKey = [aKey longLongValue];
  6376. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  6377. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6378. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6379. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6380. // Write the fields.
  6381. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6382. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6383. }
  6384. }
  6385. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6386. forGPBGenericValueKey:(GPBGenericValue *)key {
  6387. [_dictionary setObject:@(value->valueUInt64) forKey:@(key->valueInt64)];
  6388. }
  6389. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  6390. [self enumerateKeysAndUInt64sUsingBlock:^(int64_t key, uint64_t value, BOOL *stop) {
  6391. #pragma unused(stop)
  6392. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%llu", value]);
  6393. }];
  6394. }
  6395. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(int64_t)key {
  6396. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6397. if (wrapped && value) {
  6398. *value = [wrapped unsignedLongLongValue];
  6399. }
  6400. return (wrapped != NULL);
  6401. }
  6402. - (void)addEntriesFromDictionary:(GPBInt64UInt64Dictionary *)otherDictionary {
  6403. if (otherDictionary) {
  6404. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6405. if (_autocreator) {
  6406. GPBAutocreatedDictionaryModified(_autocreator, self);
  6407. }
  6408. }
  6409. }
  6410. - (void)setUInt64:(uint64_t)value forKey:(int64_t)key {
  6411. [_dictionary setObject:@(value) forKey:@(key)];
  6412. if (_autocreator) {
  6413. GPBAutocreatedDictionaryModified(_autocreator, self);
  6414. }
  6415. }
  6416. - (void)removeUInt64ForKey:(int64_t)aKey {
  6417. [_dictionary removeObjectForKey:@(aKey)];
  6418. }
  6419. - (void)removeAll {
  6420. [_dictionary removeAllObjects];
  6421. }
  6422. @end
  6423. #pragma mark - Int64 -> Int64
  6424. @implementation GPBInt64Int64Dictionary {
  6425. @package
  6426. NSMutableDictionary *_dictionary;
  6427. }
  6428. - (instancetype)init {
  6429. return [self initWithInt64s:NULL forKeys:NULL count:0];
  6430. }
  6431. - (instancetype)initWithInt64s:(const int64_t [])values
  6432. forKeys:(const int64_t [])keys
  6433. count:(NSUInteger)count {
  6434. self = [super init];
  6435. if (self) {
  6436. _dictionary = [[NSMutableDictionary alloc] init];
  6437. if (count && values && keys) {
  6438. for (NSUInteger i = 0; i < count; ++i) {
  6439. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6440. }
  6441. }
  6442. }
  6443. return self;
  6444. }
  6445. - (instancetype)initWithDictionary:(GPBInt64Int64Dictionary *)dictionary {
  6446. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  6447. if (self) {
  6448. if (dictionary) {
  6449. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6450. }
  6451. }
  6452. return self;
  6453. }
  6454. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6455. #pragma unused(numItems)
  6456. return [self initWithInt64s:NULL forKeys:NULL count:0];
  6457. }
  6458. - (void)dealloc {
  6459. NSAssert(!_autocreator,
  6460. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6461. [self class], _autocreator);
  6462. [_dictionary release];
  6463. [super dealloc];
  6464. }
  6465. - (instancetype)copyWithZone:(NSZone *)zone {
  6466. return [[GPBInt64Int64Dictionary allocWithZone:zone] initWithDictionary:self];
  6467. }
  6468. - (BOOL)isEqual:(id)other {
  6469. if (self == other) {
  6470. return YES;
  6471. }
  6472. if (![other isKindOfClass:[GPBInt64Int64Dictionary class]]) {
  6473. return NO;
  6474. }
  6475. GPBInt64Int64Dictionary *otherDictionary = other;
  6476. return [_dictionary isEqual:otherDictionary->_dictionary];
  6477. }
  6478. - (NSUInteger)hash {
  6479. return _dictionary.count;
  6480. }
  6481. - (NSString *)description {
  6482. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6483. }
  6484. - (NSUInteger)count {
  6485. return _dictionary.count;
  6486. }
  6487. - (void)enumerateKeysAndInt64sUsingBlock:
  6488. (void (NS_NOESCAPE ^)(int64_t key, int64_t value, BOOL *stop))block {
  6489. BOOL stop = NO;
  6490. NSDictionary *internal = _dictionary;
  6491. NSEnumerator *keys = [internal keyEnumerator];
  6492. NSNumber *aKey;
  6493. while ((aKey = [keys nextObject])) {
  6494. NSNumber *aValue = internal[aKey];
  6495. block([aKey longLongValue], [aValue longLongValue], &stop);
  6496. if (stop) {
  6497. break;
  6498. }
  6499. }
  6500. }
  6501. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6502. NSDictionary *internal = _dictionary;
  6503. NSUInteger count = internal.count;
  6504. if (count == 0) {
  6505. return 0;
  6506. }
  6507. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6508. GPBDataType keyDataType = field.mapKeyDataType;
  6509. size_t result = 0;
  6510. NSEnumerator *keys = [internal keyEnumerator];
  6511. NSNumber *aKey;
  6512. while ((aKey = [keys nextObject])) {
  6513. NSNumber *aValue = internal[aKey];
  6514. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6515. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  6516. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6517. }
  6518. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6519. result += tagSize * count;
  6520. return result;
  6521. }
  6522. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6523. asField:(GPBFieldDescriptor *)field {
  6524. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6525. GPBDataType keyDataType = field.mapKeyDataType;
  6526. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6527. NSDictionary *internal = _dictionary;
  6528. NSEnumerator *keys = [internal keyEnumerator];
  6529. NSNumber *aKey;
  6530. while ((aKey = [keys nextObject])) {
  6531. NSNumber *aValue = internal[aKey];
  6532. [outputStream writeInt32NoTag:tag];
  6533. // Write the size of the message.
  6534. int64_t unwrappedKey = [aKey longLongValue];
  6535. int64_t unwrappedValue = [aValue longLongValue];
  6536. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6537. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6538. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6539. // Write the fields.
  6540. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6541. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6542. }
  6543. }
  6544. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6545. forGPBGenericValueKey:(GPBGenericValue *)key {
  6546. [_dictionary setObject:@(value->valueInt64) forKey:@(key->valueInt64)];
  6547. }
  6548. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  6549. [self enumerateKeysAndInt64sUsingBlock:^(int64_t key, int64_t value, BOOL *stop) {
  6550. #pragma unused(stop)
  6551. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%lld", value]);
  6552. }];
  6553. }
  6554. - (BOOL)getInt64:(nullable int64_t *)value forKey:(int64_t)key {
  6555. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6556. if (wrapped && value) {
  6557. *value = [wrapped longLongValue];
  6558. }
  6559. return (wrapped != NULL);
  6560. }
  6561. - (void)addEntriesFromDictionary:(GPBInt64Int64Dictionary *)otherDictionary {
  6562. if (otherDictionary) {
  6563. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6564. if (_autocreator) {
  6565. GPBAutocreatedDictionaryModified(_autocreator, self);
  6566. }
  6567. }
  6568. }
  6569. - (void)setInt64:(int64_t)value forKey:(int64_t)key {
  6570. [_dictionary setObject:@(value) forKey:@(key)];
  6571. if (_autocreator) {
  6572. GPBAutocreatedDictionaryModified(_autocreator, self);
  6573. }
  6574. }
  6575. - (void)removeInt64ForKey:(int64_t)aKey {
  6576. [_dictionary removeObjectForKey:@(aKey)];
  6577. }
  6578. - (void)removeAll {
  6579. [_dictionary removeAllObjects];
  6580. }
  6581. @end
  6582. #pragma mark - Int64 -> Bool
  6583. @implementation GPBInt64BoolDictionary {
  6584. @package
  6585. NSMutableDictionary *_dictionary;
  6586. }
  6587. - (instancetype)init {
  6588. return [self initWithBools:NULL forKeys:NULL count:0];
  6589. }
  6590. - (instancetype)initWithBools:(const BOOL [])values
  6591. forKeys:(const int64_t [])keys
  6592. count:(NSUInteger)count {
  6593. self = [super init];
  6594. if (self) {
  6595. _dictionary = [[NSMutableDictionary alloc] init];
  6596. if (count && values && keys) {
  6597. for (NSUInteger i = 0; i < count; ++i) {
  6598. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6599. }
  6600. }
  6601. }
  6602. return self;
  6603. }
  6604. - (instancetype)initWithDictionary:(GPBInt64BoolDictionary *)dictionary {
  6605. self = [self initWithBools:NULL forKeys:NULL count:0];
  6606. if (self) {
  6607. if (dictionary) {
  6608. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6609. }
  6610. }
  6611. return self;
  6612. }
  6613. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6614. #pragma unused(numItems)
  6615. return [self initWithBools:NULL forKeys:NULL count:0];
  6616. }
  6617. - (void)dealloc {
  6618. NSAssert(!_autocreator,
  6619. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6620. [self class], _autocreator);
  6621. [_dictionary release];
  6622. [super dealloc];
  6623. }
  6624. - (instancetype)copyWithZone:(NSZone *)zone {
  6625. return [[GPBInt64BoolDictionary allocWithZone:zone] initWithDictionary:self];
  6626. }
  6627. - (BOOL)isEqual:(id)other {
  6628. if (self == other) {
  6629. return YES;
  6630. }
  6631. if (![other isKindOfClass:[GPBInt64BoolDictionary class]]) {
  6632. return NO;
  6633. }
  6634. GPBInt64BoolDictionary *otherDictionary = other;
  6635. return [_dictionary isEqual:otherDictionary->_dictionary];
  6636. }
  6637. - (NSUInteger)hash {
  6638. return _dictionary.count;
  6639. }
  6640. - (NSString *)description {
  6641. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6642. }
  6643. - (NSUInteger)count {
  6644. return _dictionary.count;
  6645. }
  6646. - (void)enumerateKeysAndBoolsUsingBlock:
  6647. (void (NS_NOESCAPE ^)(int64_t key, BOOL value, BOOL *stop))block {
  6648. BOOL stop = NO;
  6649. NSDictionary *internal = _dictionary;
  6650. NSEnumerator *keys = [internal keyEnumerator];
  6651. NSNumber *aKey;
  6652. while ((aKey = [keys nextObject])) {
  6653. NSNumber *aValue = internal[aKey];
  6654. block([aKey longLongValue], [aValue boolValue], &stop);
  6655. if (stop) {
  6656. break;
  6657. }
  6658. }
  6659. }
  6660. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6661. NSDictionary *internal = _dictionary;
  6662. NSUInteger count = internal.count;
  6663. if (count == 0) {
  6664. return 0;
  6665. }
  6666. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6667. GPBDataType keyDataType = field.mapKeyDataType;
  6668. size_t result = 0;
  6669. NSEnumerator *keys = [internal keyEnumerator];
  6670. NSNumber *aKey;
  6671. while ((aKey = [keys nextObject])) {
  6672. NSNumber *aValue = internal[aKey];
  6673. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6674. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  6675. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6676. }
  6677. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6678. result += tagSize * count;
  6679. return result;
  6680. }
  6681. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6682. asField:(GPBFieldDescriptor *)field {
  6683. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6684. GPBDataType keyDataType = field.mapKeyDataType;
  6685. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6686. NSDictionary *internal = _dictionary;
  6687. NSEnumerator *keys = [internal keyEnumerator];
  6688. NSNumber *aKey;
  6689. while ((aKey = [keys nextObject])) {
  6690. NSNumber *aValue = internal[aKey];
  6691. [outputStream writeInt32NoTag:tag];
  6692. // Write the size of the message.
  6693. int64_t unwrappedKey = [aKey longLongValue];
  6694. BOOL unwrappedValue = [aValue boolValue];
  6695. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6696. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6697. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6698. // Write the fields.
  6699. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6700. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6701. }
  6702. }
  6703. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6704. forGPBGenericValueKey:(GPBGenericValue *)key {
  6705. [_dictionary setObject:@(value->valueBool) forKey:@(key->valueInt64)];
  6706. }
  6707. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  6708. [self enumerateKeysAndBoolsUsingBlock:^(int64_t key, BOOL value, BOOL *stop) {
  6709. #pragma unused(stop)
  6710. block([NSString stringWithFormat:@"%lld", key], (value ? @"true" : @"false"));
  6711. }];
  6712. }
  6713. - (BOOL)getBool:(nullable BOOL *)value forKey:(int64_t)key {
  6714. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6715. if (wrapped && value) {
  6716. *value = [wrapped boolValue];
  6717. }
  6718. return (wrapped != NULL);
  6719. }
  6720. - (void)addEntriesFromDictionary:(GPBInt64BoolDictionary *)otherDictionary {
  6721. if (otherDictionary) {
  6722. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6723. if (_autocreator) {
  6724. GPBAutocreatedDictionaryModified(_autocreator, self);
  6725. }
  6726. }
  6727. }
  6728. - (void)setBool:(BOOL)value forKey:(int64_t)key {
  6729. [_dictionary setObject:@(value) forKey:@(key)];
  6730. if (_autocreator) {
  6731. GPBAutocreatedDictionaryModified(_autocreator, self);
  6732. }
  6733. }
  6734. - (void)removeBoolForKey:(int64_t)aKey {
  6735. [_dictionary removeObjectForKey:@(aKey)];
  6736. }
  6737. - (void)removeAll {
  6738. [_dictionary removeAllObjects];
  6739. }
  6740. @end
  6741. #pragma mark - Int64 -> Float
  6742. @implementation GPBInt64FloatDictionary {
  6743. @package
  6744. NSMutableDictionary *_dictionary;
  6745. }
  6746. - (instancetype)init {
  6747. return [self initWithFloats:NULL forKeys:NULL count:0];
  6748. }
  6749. - (instancetype)initWithFloats:(const float [])values
  6750. forKeys:(const int64_t [])keys
  6751. count:(NSUInteger)count {
  6752. self = [super init];
  6753. if (self) {
  6754. _dictionary = [[NSMutableDictionary alloc] init];
  6755. if (count && values && keys) {
  6756. for (NSUInteger i = 0; i < count; ++i) {
  6757. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6758. }
  6759. }
  6760. }
  6761. return self;
  6762. }
  6763. - (instancetype)initWithDictionary:(GPBInt64FloatDictionary *)dictionary {
  6764. self = [self initWithFloats:NULL forKeys:NULL count:0];
  6765. if (self) {
  6766. if (dictionary) {
  6767. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6768. }
  6769. }
  6770. return self;
  6771. }
  6772. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6773. #pragma unused(numItems)
  6774. return [self initWithFloats:NULL forKeys:NULL count:0];
  6775. }
  6776. - (void)dealloc {
  6777. NSAssert(!_autocreator,
  6778. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6779. [self class], _autocreator);
  6780. [_dictionary release];
  6781. [super dealloc];
  6782. }
  6783. - (instancetype)copyWithZone:(NSZone *)zone {
  6784. return [[GPBInt64FloatDictionary allocWithZone:zone] initWithDictionary:self];
  6785. }
  6786. - (BOOL)isEqual:(id)other {
  6787. if (self == other) {
  6788. return YES;
  6789. }
  6790. if (![other isKindOfClass:[GPBInt64FloatDictionary class]]) {
  6791. return NO;
  6792. }
  6793. GPBInt64FloatDictionary *otherDictionary = other;
  6794. return [_dictionary isEqual:otherDictionary->_dictionary];
  6795. }
  6796. - (NSUInteger)hash {
  6797. return _dictionary.count;
  6798. }
  6799. - (NSString *)description {
  6800. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6801. }
  6802. - (NSUInteger)count {
  6803. return _dictionary.count;
  6804. }
  6805. - (void)enumerateKeysAndFloatsUsingBlock:
  6806. (void (NS_NOESCAPE ^)(int64_t key, float value, BOOL *stop))block {
  6807. BOOL stop = NO;
  6808. NSDictionary *internal = _dictionary;
  6809. NSEnumerator *keys = [internal keyEnumerator];
  6810. NSNumber *aKey;
  6811. while ((aKey = [keys nextObject])) {
  6812. NSNumber *aValue = internal[aKey];
  6813. block([aKey longLongValue], [aValue floatValue], &stop);
  6814. if (stop) {
  6815. break;
  6816. }
  6817. }
  6818. }
  6819. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6820. NSDictionary *internal = _dictionary;
  6821. NSUInteger count = internal.count;
  6822. if (count == 0) {
  6823. return 0;
  6824. }
  6825. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6826. GPBDataType keyDataType = field.mapKeyDataType;
  6827. size_t result = 0;
  6828. NSEnumerator *keys = [internal keyEnumerator];
  6829. NSNumber *aKey;
  6830. while ((aKey = [keys nextObject])) {
  6831. NSNumber *aValue = internal[aKey];
  6832. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6833. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  6834. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6835. }
  6836. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6837. result += tagSize * count;
  6838. return result;
  6839. }
  6840. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  6841. asField:(GPBFieldDescriptor *)field {
  6842. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6843. GPBDataType keyDataType = field.mapKeyDataType;
  6844. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  6845. NSDictionary *internal = _dictionary;
  6846. NSEnumerator *keys = [internal keyEnumerator];
  6847. NSNumber *aKey;
  6848. while ((aKey = [keys nextObject])) {
  6849. NSNumber *aValue = internal[aKey];
  6850. [outputStream writeInt32NoTag:tag];
  6851. // Write the size of the message.
  6852. int64_t unwrappedKey = [aKey longLongValue];
  6853. float unwrappedValue = [aValue floatValue];
  6854. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6855. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  6856. [outputStream writeInt32NoTag:(int32_t)msgSize];
  6857. // Write the fields.
  6858. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  6859. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  6860. }
  6861. }
  6862. - (void)setGPBGenericValue:(GPBGenericValue *)value
  6863. forGPBGenericValueKey:(GPBGenericValue *)key {
  6864. [_dictionary setObject:@(value->valueFloat) forKey:@(key->valueInt64)];
  6865. }
  6866. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  6867. [self enumerateKeysAndFloatsUsingBlock:^(int64_t key, float value, BOOL *stop) {
  6868. #pragma unused(stop)
  6869. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  6870. }];
  6871. }
  6872. - (BOOL)getFloat:(nullable float *)value forKey:(int64_t)key {
  6873. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  6874. if (wrapped && value) {
  6875. *value = [wrapped floatValue];
  6876. }
  6877. return (wrapped != NULL);
  6878. }
  6879. - (void)addEntriesFromDictionary:(GPBInt64FloatDictionary *)otherDictionary {
  6880. if (otherDictionary) {
  6881. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  6882. if (_autocreator) {
  6883. GPBAutocreatedDictionaryModified(_autocreator, self);
  6884. }
  6885. }
  6886. }
  6887. - (void)setFloat:(float)value forKey:(int64_t)key {
  6888. [_dictionary setObject:@(value) forKey:@(key)];
  6889. if (_autocreator) {
  6890. GPBAutocreatedDictionaryModified(_autocreator, self);
  6891. }
  6892. }
  6893. - (void)removeFloatForKey:(int64_t)aKey {
  6894. [_dictionary removeObjectForKey:@(aKey)];
  6895. }
  6896. - (void)removeAll {
  6897. [_dictionary removeAllObjects];
  6898. }
  6899. @end
  6900. #pragma mark - Int64 -> Double
  6901. @implementation GPBInt64DoubleDictionary {
  6902. @package
  6903. NSMutableDictionary *_dictionary;
  6904. }
  6905. - (instancetype)init {
  6906. return [self initWithDoubles:NULL forKeys:NULL count:0];
  6907. }
  6908. - (instancetype)initWithDoubles:(const double [])values
  6909. forKeys:(const int64_t [])keys
  6910. count:(NSUInteger)count {
  6911. self = [super init];
  6912. if (self) {
  6913. _dictionary = [[NSMutableDictionary alloc] init];
  6914. if (count && values && keys) {
  6915. for (NSUInteger i = 0; i < count; ++i) {
  6916. [_dictionary setObject:@(values[i]) forKey:@(keys[i])];
  6917. }
  6918. }
  6919. }
  6920. return self;
  6921. }
  6922. - (instancetype)initWithDictionary:(GPBInt64DoubleDictionary *)dictionary {
  6923. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  6924. if (self) {
  6925. if (dictionary) {
  6926. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  6927. }
  6928. }
  6929. return self;
  6930. }
  6931. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  6932. #pragma unused(numItems)
  6933. return [self initWithDoubles:NULL forKeys:NULL count:0];
  6934. }
  6935. - (void)dealloc {
  6936. NSAssert(!_autocreator,
  6937. @"%@: Autocreator must be cleared before release, autocreator: %@",
  6938. [self class], _autocreator);
  6939. [_dictionary release];
  6940. [super dealloc];
  6941. }
  6942. - (instancetype)copyWithZone:(NSZone *)zone {
  6943. return [[GPBInt64DoubleDictionary allocWithZone:zone] initWithDictionary:self];
  6944. }
  6945. - (BOOL)isEqual:(id)other {
  6946. if (self == other) {
  6947. return YES;
  6948. }
  6949. if (![other isKindOfClass:[GPBInt64DoubleDictionary class]]) {
  6950. return NO;
  6951. }
  6952. GPBInt64DoubleDictionary *otherDictionary = other;
  6953. return [_dictionary isEqual:otherDictionary->_dictionary];
  6954. }
  6955. - (NSUInteger)hash {
  6956. return _dictionary.count;
  6957. }
  6958. - (NSString *)description {
  6959. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  6960. }
  6961. - (NSUInteger)count {
  6962. return _dictionary.count;
  6963. }
  6964. - (void)enumerateKeysAndDoublesUsingBlock:
  6965. (void (NS_NOESCAPE ^)(int64_t key, double value, BOOL *stop))block {
  6966. BOOL stop = NO;
  6967. NSDictionary *internal = _dictionary;
  6968. NSEnumerator *keys = [internal keyEnumerator];
  6969. NSNumber *aKey;
  6970. while ((aKey = [keys nextObject])) {
  6971. NSNumber *aValue = internal[aKey];
  6972. block([aKey longLongValue], [aValue doubleValue], &stop);
  6973. if (stop) {
  6974. break;
  6975. }
  6976. }
  6977. }
  6978. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  6979. NSDictionary *internal = _dictionary;
  6980. NSUInteger count = internal.count;
  6981. if (count == 0) {
  6982. return 0;
  6983. }
  6984. GPBDataType valueDataType = GPBGetFieldDataType(field);
  6985. GPBDataType keyDataType = field.mapKeyDataType;
  6986. size_t result = 0;
  6987. NSEnumerator *keys = [internal keyEnumerator];
  6988. NSNumber *aKey;
  6989. while ((aKey = [keys nextObject])) {
  6990. NSNumber *aValue = internal[aKey];
  6991. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  6992. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  6993. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  6994. }
  6995. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  6996. result += tagSize * count;
  6997. return result;
  6998. }
  6999. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7000. asField:(GPBFieldDescriptor *)field {
  7001. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7002. GPBDataType keyDataType = field.mapKeyDataType;
  7003. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7004. NSDictionary *internal = _dictionary;
  7005. NSEnumerator *keys = [internal keyEnumerator];
  7006. NSNumber *aKey;
  7007. while ((aKey = [keys nextObject])) {
  7008. NSNumber *aValue = internal[aKey];
  7009. [outputStream writeInt32NoTag:tag];
  7010. // Write the size of the message.
  7011. int64_t unwrappedKey = [aKey longLongValue];
  7012. double unwrappedValue = [aValue doubleValue];
  7013. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7014. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7015. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7016. // Write the fields.
  7017. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7018. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7019. }
  7020. }
  7021. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7022. forGPBGenericValueKey:(GPBGenericValue *)key {
  7023. [_dictionary setObject:@(value->valueDouble) forKey:@(key->valueInt64)];
  7024. }
  7025. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  7026. [self enumerateKeysAndDoublesUsingBlock:^(int64_t key, double value, BOOL *stop) {
  7027. #pragma unused(stop)
  7028. block([NSString stringWithFormat:@"%lld", key], [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  7029. }];
  7030. }
  7031. - (BOOL)getDouble:(nullable double *)value forKey:(int64_t)key {
  7032. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7033. if (wrapped && value) {
  7034. *value = [wrapped doubleValue];
  7035. }
  7036. return (wrapped != NULL);
  7037. }
  7038. - (void)addEntriesFromDictionary:(GPBInt64DoubleDictionary *)otherDictionary {
  7039. if (otherDictionary) {
  7040. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7041. if (_autocreator) {
  7042. GPBAutocreatedDictionaryModified(_autocreator, self);
  7043. }
  7044. }
  7045. }
  7046. - (void)setDouble:(double)value forKey:(int64_t)key {
  7047. [_dictionary setObject:@(value) forKey:@(key)];
  7048. if (_autocreator) {
  7049. GPBAutocreatedDictionaryModified(_autocreator, self);
  7050. }
  7051. }
  7052. - (void)removeDoubleForKey:(int64_t)aKey {
  7053. [_dictionary removeObjectForKey:@(aKey)];
  7054. }
  7055. - (void)removeAll {
  7056. [_dictionary removeAllObjects];
  7057. }
  7058. @end
  7059. #pragma mark - Int64 -> Enum
  7060. @implementation GPBInt64EnumDictionary {
  7061. @package
  7062. NSMutableDictionary *_dictionary;
  7063. GPBEnumValidationFunc _validationFunc;
  7064. }
  7065. @synthesize validationFunc = _validationFunc;
  7066. - (instancetype)init {
  7067. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  7068. }
  7069. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  7070. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7071. }
  7072. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7073. rawValues:(const int32_t [])rawValues
  7074. forKeys:(const int64_t [])keys
  7075. count:(NSUInteger)count {
  7076. self = [super init];
  7077. if (self) {
  7078. _dictionary = [[NSMutableDictionary alloc] init];
  7079. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  7080. if (count && rawValues && keys) {
  7081. for (NSUInteger i = 0; i < count; ++i) {
  7082. [_dictionary setObject:@(rawValues[i]) forKey:@(keys[i])];
  7083. }
  7084. }
  7085. }
  7086. return self;
  7087. }
  7088. - (instancetype)initWithDictionary:(GPBInt64EnumDictionary *)dictionary {
  7089. self = [self initWithValidationFunction:dictionary.validationFunc
  7090. rawValues:NULL
  7091. forKeys:NULL
  7092. count:0];
  7093. if (self) {
  7094. if (dictionary) {
  7095. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7096. }
  7097. }
  7098. return self;
  7099. }
  7100. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  7101. capacity:(NSUInteger)numItems {
  7102. #pragma unused(numItems)
  7103. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  7104. }
  7105. - (void)dealloc {
  7106. NSAssert(!_autocreator,
  7107. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7108. [self class], _autocreator);
  7109. [_dictionary release];
  7110. [super dealloc];
  7111. }
  7112. - (instancetype)copyWithZone:(NSZone *)zone {
  7113. return [[GPBInt64EnumDictionary allocWithZone:zone] initWithDictionary:self];
  7114. }
  7115. - (BOOL)isEqual:(id)other {
  7116. if (self == other) {
  7117. return YES;
  7118. }
  7119. if (![other isKindOfClass:[GPBInt64EnumDictionary class]]) {
  7120. return NO;
  7121. }
  7122. GPBInt64EnumDictionary *otherDictionary = other;
  7123. return [_dictionary isEqual:otherDictionary->_dictionary];
  7124. }
  7125. - (NSUInteger)hash {
  7126. return _dictionary.count;
  7127. }
  7128. - (NSString *)description {
  7129. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7130. }
  7131. - (NSUInteger)count {
  7132. return _dictionary.count;
  7133. }
  7134. - (void)enumerateKeysAndRawValuesUsingBlock:
  7135. (void (NS_NOESCAPE ^)(int64_t key, int32_t value, BOOL *stop))block {
  7136. BOOL stop = NO;
  7137. NSDictionary *internal = _dictionary;
  7138. NSEnumerator *keys = [internal keyEnumerator];
  7139. NSNumber *aKey;
  7140. while ((aKey = [keys nextObject])) {
  7141. NSNumber *aValue = internal[aKey];
  7142. block([aKey longLongValue], [aValue intValue], &stop);
  7143. if (stop) {
  7144. break;
  7145. }
  7146. }
  7147. }
  7148. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7149. NSDictionary *internal = _dictionary;
  7150. NSUInteger count = internal.count;
  7151. if (count == 0) {
  7152. return 0;
  7153. }
  7154. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7155. GPBDataType keyDataType = field.mapKeyDataType;
  7156. size_t result = 0;
  7157. NSEnumerator *keys = [internal keyEnumerator];
  7158. NSNumber *aKey;
  7159. while ((aKey = [keys nextObject])) {
  7160. NSNumber *aValue = internal[aKey];
  7161. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7162. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7163. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7164. }
  7165. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7166. result += tagSize * count;
  7167. return result;
  7168. }
  7169. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7170. asField:(GPBFieldDescriptor *)field {
  7171. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7172. GPBDataType keyDataType = field.mapKeyDataType;
  7173. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7174. NSDictionary *internal = _dictionary;
  7175. NSEnumerator *keys = [internal keyEnumerator];
  7176. NSNumber *aKey;
  7177. while ((aKey = [keys nextObject])) {
  7178. NSNumber *aValue = internal[aKey];
  7179. [outputStream writeInt32NoTag:tag];
  7180. // Write the size of the message.
  7181. int64_t unwrappedKey = [aKey longLongValue];
  7182. int32_t unwrappedValue = [aValue intValue];
  7183. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7184. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7185. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7186. // Write the fields.
  7187. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7188. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7189. }
  7190. }
  7191. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  7192. forKey:(GPBGenericValue *)key
  7193. keyDataType:(GPBDataType)keyDataType {
  7194. size_t msgSize = ComputeDictInt64FieldSize(key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7195. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  7196. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  7197. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  7198. WriteDictInt64Field(outputStream, key->valueInt64, kMapKeyFieldNumber, keyDataType);
  7199. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  7200. [outputStream release];
  7201. return data;
  7202. }
  7203. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7204. forGPBGenericValueKey:(GPBGenericValue *)key {
  7205. [_dictionary setObject:@(value->valueEnum) forKey:@(key->valueInt64)];
  7206. }
  7207. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  7208. [self enumerateKeysAndRawValuesUsingBlock:^(int64_t key, int32_t value, BOOL *stop) {
  7209. #pragma unused(stop)
  7210. block([NSString stringWithFormat:@"%lld", key], @(value));
  7211. }];
  7212. }
  7213. - (BOOL)getEnum:(int32_t *)value forKey:(int64_t)key {
  7214. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7215. if (wrapped && value) {
  7216. int32_t result = [wrapped intValue];
  7217. if (!_validationFunc(result)) {
  7218. result = kGPBUnrecognizedEnumeratorValue;
  7219. }
  7220. *value = result;
  7221. }
  7222. return (wrapped != NULL);
  7223. }
  7224. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(int64_t)key {
  7225. NSNumber *wrapped = [_dictionary objectForKey:@(key)];
  7226. if (wrapped && rawValue) {
  7227. *rawValue = [wrapped intValue];
  7228. }
  7229. return (wrapped != NULL);
  7230. }
  7231. - (void)enumerateKeysAndEnumsUsingBlock:
  7232. (void (NS_NOESCAPE ^)(int64_t key, int32_t value, BOOL *stop))block {
  7233. GPBEnumValidationFunc func = _validationFunc;
  7234. BOOL stop = NO;
  7235. NSEnumerator *keys = [_dictionary keyEnumerator];
  7236. NSNumber *aKey;
  7237. while ((aKey = [keys nextObject])) {
  7238. NSNumber *aValue = _dictionary[aKey];
  7239. int32_t unwrapped = [aValue intValue];
  7240. if (!func(unwrapped)) {
  7241. unwrapped = kGPBUnrecognizedEnumeratorValue;
  7242. }
  7243. block([aKey longLongValue], unwrapped, &stop);
  7244. if (stop) {
  7245. break;
  7246. }
  7247. }
  7248. }
  7249. - (void)addRawEntriesFromDictionary:(GPBInt64EnumDictionary *)otherDictionary {
  7250. if (otherDictionary) {
  7251. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7252. if (_autocreator) {
  7253. GPBAutocreatedDictionaryModified(_autocreator, self);
  7254. }
  7255. }
  7256. }
  7257. - (void)setRawValue:(int32_t)value forKey:(int64_t)key {
  7258. [_dictionary setObject:@(value) forKey:@(key)];
  7259. if (_autocreator) {
  7260. GPBAutocreatedDictionaryModified(_autocreator, self);
  7261. }
  7262. }
  7263. - (void)removeEnumForKey:(int64_t)aKey {
  7264. [_dictionary removeObjectForKey:@(aKey)];
  7265. }
  7266. - (void)removeAll {
  7267. [_dictionary removeAllObjects];
  7268. }
  7269. - (void)setEnum:(int32_t)value forKey:(int64_t)key {
  7270. if (!_validationFunc(value)) {
  7271. [NSException raise:NSInvalidArgumentException
  7272. format:@"GPBInt64EnumDictionary: Attempt to set an unknown enum value (%d)",
  7273. value];
  7274. }
  7275. [_dictionary setObject:@(value) forKey:@(key)];
  7276. if (_autocreator) {
  7277. GPBAutocreatedDictionaryModified(_autocreator, self);
  7278. }
  7279. }
  7280. @end
  7281. #pragma mark - Int64 -> Object
  7282. @implementation GPBInt64ObjectDictionary {
  7283. @package
  7284. NSMutableDictionary *_dictionary;
  7285. }
  7286. - (instancetype)init {
  7287. return [self initWithObjects:NULL forKeys:NULL count:0];
  7288. }
  7289. - (instancetype)initWithObjects:(const id [])objects
  7290. forKeys:(const int64_t [])keys
  7291. count:(NSUInteger)count {
  7292. self = [super init];
  7293. if (self) {
  7294. _dictionary = [[NSMutableDictionary alloc] init];
  7295. if (count && objects && keys) {
  7296. for (NSUInteger i = 0; i < count; ++i) {
  7297. if (!objects[i]) {
  7298. [NSException raise:NSInvalidArgumentException
  7299. format:@"Attempting to add nil object to a Dictionary"];
  7300. }
  7301. [_dictionary setObject:objects[i] forKey:@(keys[i])];
  7302. }
  7303. }
  7304. }
  7305. return self;
  7306. }
  7307. - (instancetype)initWithDictionary:(GPBInt64ObjectDictionary *)dictionary {
  7308. self = [self initWithObjects:NULL forKeys:NULL count:0];
  7309. if (self) {
  7310. if (dictionary) {
  7311. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7312. }
  7313. }
  7314. return self;
  7315. }
  7316. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7317. #pragma unused(numItems)
  7318. return [self initWithObjects:NULL forKeys:NULL count:0];
  7319. }
  7320. - (void)dealloc {
  7321. NSAssert(!_autocreator,
  7322. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7323. [self class], _autocreator);
  7324. [_dictionary release];
  7325. [super dealloc];
  7326. }
  7327. - (instancetype)copyWithZone:(NSZone *)zone {
  7328. return [[GPBInt64ObjectDictionary allocWithZone:zone] initWithDictionary:self];
  7329. }
  7330. - (BOOL)isEqual:(id)other {
  7331. if (self == other) {
  7332. return YES;
  7333. }
  7334. if (![other isKindOfClass:[GPBInt64ObjectDictionary class]]) {
  7335. return NO;
  7336. }
  7337. GPBInt64ObjectDictionary *otherDictionary = other;
  7338. return [_dictionary isEqual:otherDictionary->_dictionary];
  7339. }
  7340. - (NSUInteger)hash {
  7341. return _dictionary.count;
  7342. }
  7343. - (NSString *)description {
  7344. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7345. }
  7346. - (NSUInteger)count {
  7347. return _dictionary.count;
  7348. }
  7349. - (void)enumerateKeysAndObjectsUsingBlock:
  7350. (void (NS_NOESCAPE ^)(int64_t key, id object, BOOL *stop))block {
  7351. BOOL stop = NO;
  7352. NSDictionary *internal = _dictionary;
  7353. NSEnumerator *keys = [internal keyEnumerator];
  7354. NSNumber *aKey;
  7355. while ((aKey = [keys nextObject])) {
  7356. id aObject = internal[aKey];
  7357. block([aKey longLongValue], aObject, &stop);
  7358. if (stop) {
  7359. break;
  7360. }
  7361. }
  7362. }
  7363. - (BOOL)isInitialized {
  7364. for (GPBMessage *msg in [_dictionary objectEnumerator]) {
  7365. if (!msg.initialized) {
  7366. return NO;
  7367. }
  7368. }
  7369. return YES;
  7370. }
  7371. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  7372. GPBInt64ObjectDictionary *newDict =
  7373. [[GPBInt64ObjectDictionary alloc] init];
  7374. NSEnumerator *keys = [_dictionary keyEnumerator];
  7375. id aKey;
  7376. NSMutableDictionary *internalDict = newDict->_dictionary;
  7377. while ((aKey = [keys nextObject])) {
  7378. GPBMessage *msg = _dictionary[aKey];
  7379. GPBMessage *copiedMsg = [msg copyWithZone:zone];
  7380. [internalDict setObject:copiedMsg forKey:aKey];
  7381. [copiedMsg release];
  7382. }
  7383. return newDict;
  7384. }
  7385. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7386. NSDictionary *internal = _dictionary;
  7387. NSUInteger count = internal.count;
  7388. if (count == 0) {
  7389. return 0;
  7390. }
  7391. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7392. GPBDataType keyDataType = field.mapKeyDataType;
  7393. size_t result = 0;
  7394. NSEnumerator *keys = [internal keyEnumerator];
  7395. NSNumber *aKey;
  7396. while ((aKey = [keys nextObject])) {
  7397. id aObject = internal[aKey];
  7398. size_t msgSize = ComputeDictInt64FieldSize([aKey longLongValue], kMapKeyFieldNumber, keyDataType);
  7399. msgSize += ComputeDictObjectFieldSize(aObject, kMapValueFieldNumber, valueDataType);
  7400. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7401. }
  7402. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7403. result += tagSize * count;
  7404. return result;
  7405. }
  7406. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7407. asField:(GPBFieldDescriptor *)field {
  7408. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7409. GPBDataType keyDataType = field.mapKeyDataType;
  7410. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7411. NSDictionary *internal = _dictionary;
  7412. NSEnumerator *keys = [internal keyEnumerator];
  7413. NSNumber *aKey;
  7414. while ((aKey = [keys nextObject])) {
  7415. id aObject = internal[aKey];
  7416. [outputStream writeInt32NoTag:tag];
  7417. // Write the size of the message.
  7418. int64_t unwrappedKey = [aKey longLongValue];
  7419. id unwrappedValue = aObject;
  7420. size_t msgSize = ComputeDictInt64FieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7421. msgSize += ComputeDictObjectFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7422. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7423. // Write the fields.
  7424. WriteDictInt64Field(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7425. WriteDictObjectField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7426. }
  7427. }
  7428. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7429. forGPBGenericValueKey:(GPBGenericValue *)key {
  7430. [_dictionary setObject:value->valueString forKey:@(key->valueInt64)];
  7431. }
  7432. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  7433. [self enumerateKeysAndObjectsUsingBlock:^(int64_t key, id object, BOOL *stop) {
  7434. #pragma unused(stop)
  7435. block([NSString stringWithFormat:@"%lld", key], object);
  7436. }];
  7437. }
  7438. - (id)objectForKey:(int64_t)key {
  7439. id result = [_dictionary objectForKey:@(key)];
  7440. return result;
  7441. }
  7442. - (void)addEntriesFromDictionary:(GPBInt64ObjectDictionary *)otherDictionary {
  7443. if (otherDictionary) {
  7444. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7445. if (_autocreator) {
  7446. GPBAutocreatedDictionaryModified(_autocreator, self);
  7447. }
  7448. }
  7449. }
  7450. - (void)setObject:(id)object forKey:(int64_t)key {
  7451. if (!object) {
  7452. [NSException raise:NSInvalidArgumentException
  7453. format:@"Attempting to add nil object to a Dictionary"];
  7454. }
  7455. [_dictionary setObject:object forKey:@(key)];
  7456. if (_autocreator) {
  7457. GPBAutocreatedDictionaryModified(_autocreator, self);
  7458. }
  7459. }
  7460. - (void)removeObjectForKey:(int64_t)aKey {
  7461. [_dictionary removeObjectForKey:@(aKey)];
  7462. }
  7463. - (void)removeAll {
  7464. [_dictionary removeAllObjects];
  7465. }
  7466. @end
  7467. // clang-format on
  7468. //%PDDM-EXPAND DICTIONARY_POD_IMPL_FOR_KEY(String, NSString, *, OBJECT)
  7469. // This block of code is generated, do not edit it directly.
  7470. // clang-format off
  7471. #pragma mark - String -> UInt32
  7472. @implementation GPBStringUInt32Dictionary {
  7473. @package
  7474. NSMutableDictionary *_dictionary;
  7475. }
  7476. - (instancetype)init {
  7477. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  7478. }
  7479. - (instancetype)initWithUInt32s:(const uint32_t [])values
  7480. forKeys:(const NSString * [])keys
  7481. count:(NSUInteger)count {
  7482. self = [super init];
  7483. if (self) {
  7484. _dictionary = [[NSMutableDictionary alloc] init];
  7485. if (count && values && keys) {
  7486. for (NSUInteger i = 0; i < count; ++i) {
  7487. if (!keys[i]) {
  7488. [NSException raise:NSInvalidArgumentException
  7489. format:@"Attempting to add nil key to a Dictionary"];
  7490. }
  7491. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  7492. }
  7493. }
  7494. }
  7495. return self;
  7496. }
  7497. - (instancetype)initWithDictionary:(GPBStringUInt32Dictionary *)dictionary {
  7498. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  7499. if (self) {
  7500. if (dictionary) {
  7501. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7502. }
  7503. }
  7504. return self;
  7505. }
  7506. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7507. #pragma unused(numItems)
  7508. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  7509. }
  7510. - (void)dealloc {
  7511. NSAssert(!_autocreator,
  7512. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7513. [self class], _autocreator);
  7514. [_dictionary release];
  7515. [super dealloc];
  7516. }
  7517. - (instancetype)copyWithZone:(NSZone *)zone {
  7518. return [[GPBStringUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  7519. }
  7520. - (BOOL)isEqual:(id)other {
  7521. if (self == other) {
  7522. return YES;
  7523. }
  7524. if (![other isKindOfClass:[GPBStringUInt32Dictionary class]]) {
  7525. return NO;
  7526. }
  7527. GPBStringUInt32Dictionary *otherDictionary = other;
  7528. return [_dictionary isEqual:otherDictionary->_dictionary];
  7529. }
  7530. - (NSUInteger)hash {
  7531. return _dictionary.count;
  7532. }
  7533. - (NSString *)description {
  7534. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7535. }
  7536. - (NSUInteger)count {
  7537. return _dictionary.count;
  7538. }
  7539. - (void)enumerateKeysAndUInt32sUsingBlock:
  7540. (void (NS_NOESCAPE ^)(NSString *key, uint32_t value, BOOL *stop))block {
  7541. BOOL stop = NO;
  7542. NSDictionary *internal = _dictionary;
  7543. NSEnumerator *keys = [internal keyEnumerator];
  7544. NSString *aKey;
  7545. while ((aKey = [keys nextObject])) {
  7546. NSNumber *aValue = internal[aKey];
  7547. block(aKey, [aValue unsignedIntValue], &stop);
  7548. if (stop) {
  7549. break;
  7550. }
  7551. }
  7552. }
  7553. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7554. NSDictionary *internal = _dictionary;
  7555. NSUInteger count = internal.count;
  7556. if (count == 0) {
  7557. return 0;
  7558. }
  7559. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7560. GPBDataType keyDataType = field.mapKeyDataType;
  7561. size_t result = 0;
  7562. NSEnumerator *keys = [internal keyEnumerator];
  7563. NSString *aKey;
  7564. while ((aKey = [keys nextObject])) {
  7565. NSNumber *aValue = internal[aKey];
  7566. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  7567. msgSize += ComputeDictUInt32FieldSize([aValue unsignedIntValue], kMapValueFieldNumber, valueDataType);
  7568. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7569. }
  7570. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7571. result += tagSize * count;
  7572. return result;
  7573. }
  7574. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7575. asField:(GPBFieldDescriptor *)field {
  7576. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7577. GPBDataType keyDataType = field.mapKeyDataType;
  7578. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7579. NSDictionary *internal = _dictionary;
  7580. NSEnumerator *keys = [internal keyEnumerator];
  7581. NSString *aKey;
  7582. while ((aKey = [keys nextObject])) {
  7583. NSNumber *aValue = internal[aKey];
  7584. [outputStream writeInt32NoTag:tag];
  7585. // Write the size of the message.
  7586. NSString *unwrappedKey = aKey;
  7587. uint32_t unwrappedValue = [aValue unsignedIntValue];
  7588. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7589. msgSize += ComputeDictUInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7590. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7591. // Write the fields.
  7592. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7593. WriteDictUInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7594. }
  7595. }
  7596. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7597. forGPBGenericValueKey:(GPBGenericValue *)key {
  7598. [_dictionary setObject:@(value->valueUInt32) forKey:key->valueString];
  7599. }
  7600. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  7601. [self enumerateKeysAndUInt32sUsingBlock:^(NSString *key, uint32_t value, BOOL *stop) {
  7602. #pragma unused(stop)
  7603. block(key, [NSString stringWithFormat:@"%u", value]);
  7604. }];
  7605. }
  7606. - (BOOL)getUInt32:(nullable uint32_t *)value forKey:(NSString *)key {
  7607. NSNumber *wrapped = [_dictionary objectForKey:key];
  7608. if (wrapped && value) {
  7609. *value = [wrapped unsignedIntValue];
  7610. }
  7611. return (wrapped != NULL);
  7612. }
  7613. - (void)addEntriesFromDictionary:(GPBStringUInt32Dictionary *)otherDictionary {
  7614. if (otherDictionary) {
  7615. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7616. if (_autocreator) {
  7617. GPBAutocreatedDictionaryModified(_autocreator, self);
  7618. }
  7619. }
  7620. }
  7621. - (void)setUInt32:(uint32_t)value forKey:(NSString *)key {
  7622. if (!key) {
  7623. [NSException raise:NSInvalidArgumentException
  7624. format:@"Attempting to add nil key to a Dictionary"];
  7625. }
  7626. [_dictionary setObject:@(value) forKey:key];
  7627. if (_autocreator) {
  7628. GPBAutocreatedDictionaryModified(_autocreator, self);
  7629. }
  7630. }
  7631. - (void)removeUInt32ForKey:(NSString *)aKey {
  7632. [_dictionary removeObjectForKey:aKey];
  7633. }
  7634. - (void)removeAll {
  7635. [_dictionary removeAllObjects];
  7636. }
  7637. @end
  7638. #pragma mark - String -> Int32
  7639. @implementation GPBStringInt32Dictionary {
  7640. @package
  7641. NSMutableDictionary *_dictionary;
  7642. }
  7643. - (instancetype)init {
  7644. return [self initWithInt32s:NULL forKeys:NULL count:0];
  7645. }
  7646. - (instancetype)initWithInt32s:(const int32_t [])values
  7647. forKeys:(const NSString * [])keys
  7648. count:(NSUInteger)count {
  7649. self = [super init];
  7650. if (self) {
  7651. _dictionary = [[NSMutableDictionary alloc] init];
  7652. if (count && values && keys) {
  7653. for (NSUInteger i = 0; i < count; ++i) {
  7654. if (!keys[i]) {
  7655. [NSException raise:NSInvalidArgumentException
  7656. format:@"Attempting to add nil key to a Dictionary"];
  7657. }
  7658. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  7659. }
  7660. }
  7661. }
  7662. return self;
  7663. }
  7664. - (instancetype)initWithDictionary:(GPBStringInt32Dictionary *)dictionary {
  7665. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  7666. if (self) {
  7667. if (dictionary) {
  7668. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7669. }
  7670. }
  7671. return self;
  7672. }
  7673. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7674. #pragma unused(numItems)
  7675. return [self initWithInt32s:NULL forKeys:NULL count:0];
  7676. }
  7677. - (void)dealloc {
  7678. NSAssert(!_autocreator,
  7679. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7680. [self class], _autocreator);
  7681. [_dictionary release];
  7682. [super dealloc];
  7683. }
  7684. - (instancetype)copyWithZone:(NSZone *)zone {
  7685. return [[GPBStringInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  7686. }
  7687. - (BOOL)isEqual:(id)other {
  7688. if (self == other) {
  7689. return YES;
  7690. }
  7691. if (![other isKindOfClass:[GPBStringInt32Dictionary class]]) {
  7692. return NO;
  7693. }
  7694. GPBStringInt32Dictionary *otherDictionary = other;
  7695. return [_dictionary isEqual:otherDictionary->_dictionary];
  7696. }
  7697. - (NSUInteger)hash {
  7698. return _dictionary.count;
  7699. }
  7700. - (NSString *)description {
  7701. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7702. }
  7703. - (NSUInteger)count {
  7704. return _dictionary.count;
  7705. }
  7706. - (void)enumerateKeysAndInt32sUsingBlock:
  7707. (void (NS_NOESCAPE ^)(NSString *key, int32_t value, BOOL *stop))block {
  7708. BOOL stop = NO;
  7709. NSDictionary *internal = _dictionary;
  7710. NSEnumerator *keys = [internal keyEnumerator];
  7711. NSString *aKey;
  7712. while ((aKey = [keys nextObject])) {
  7713. NSNumber *aValue = internal[aKey];
  7714. block(aKey, [aValue intValue], &stop);
  7715. if (stop) {
  7716. break;
  7717. }
  7718. }
  7719. }
  7720. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7721. NSDictionary *internal = _dictionary;
  7722. NSUInteger count = internal.count;
  7723. if (count == 0) {
  7724. return 0;
  7725. }
  7726. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7727. GPBDataType keyDataType = field.mapKeyDataType;
  7728. size_t result = 0;
  7729. NSEnumerator *keys = [internal keyEnumerator];
  7730. NSString *aKey;
  7731. while ((aKey = [keys nextObject])) {
  7732. NSNumber *aValue = internal[aKey];
  7733. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  7734. msgSize += ComputeDictInt32FieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  7735. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7736. }
  7737. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7738. result += tagSize * count;
  7739. return result;
  7740. }
  7741. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7742. asField:(GPBFieldDescriptor *)field {
  7743. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7744. GPBDataType keyDataType = field.mapKeyDataType;
  7745. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7746. NSDictionary *internal = _dictionary;
  7747. NSEnumerator *keys = [internal keyEnumerator];
  7748. NSString *aKey;
  7749. while ((aKey = [keys nextObject])) {
  7750. NSNumber *aValue = internal[aKey];
  7751. [outputStream writeInt32NoTag:tag];
  7752. // Write the size of the message.
  7753. NSString *unwrappedKey = aKey;
  7754. int32_t unwrappedValue = [aValue intValue];
  7755. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7756. msgSize += ComputeDictInt32FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7757. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7758. // Write the fields.
  7759. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7760. WriteDictInt32Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7761. }
  7762. }
  7763. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7764. forGPBGenericValueKey:(GPBGenericValue *)key {
  7765. [_dictionary setObject:@(value->valueInt32) forKey:key->valueString];
  7766. }
  7767. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  7768. [self enumerateKeysAndInt32sUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  7769. #pragma unused(stop)
  7770. block(key, [NSString stringWithFormat:@"%d", value]);
  7771. }];
  7772. }
  7773. - (BOOL)getInt32:(nullable int32_t *)value forKey:(NSString *)key {
  7774. NSNumber *wrapped = [_dictionary objectForKey:key];
  7775. if (wrapped && value) {
  7776. *value = [wrapped intValue];
  7777. }
  7778. return (wrapped != NULL);
  7779. }
  7780. - (void)addEntriesFromDictionary:(GPBStringInt32Dictionary *)otherDictionary {
  7781. if (otherDictionary) {
  7782. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7783. if (_autocreator) {
  7784. GPBAutocreatedDictionaryModified(_autocreator, self);
  7785. }
  7786. }
  7787. }
  7788. - (void)setInt32:(int32_t)value forKey:(NSString *)key {
  7789. if (!key) {
  7790. [NSException raise:NSInvalidArgumentException
  7791. format:@"Attempting to add nil key to a Dictionary"];
  7792. }
  7793. [_dictionary setObject:@(value) forKey:key];
  7794. if (_autocreator) {
  7795. GPBAutocreatedDictionaryModified(_autocreator, self);
  7796. }
  7797. }
  7798. - (void)removeInt32ForKey:(NSString *)aKey {
  7799. [_dictionary removeObjectForKey:aKey];
  7800. }
  7801. - (void)removeAll {
  7802. [_dictionary removeAllObjects];
  7803. }
  7804. @end
  7805. #pragma mark - String -> UInt64
  7806. @implementation GPBStringUInt64Dictionary {
  7807. @package
  7808. NSMutableDictionary *_dictionary;
  7809. }
  7810. - (instancetype)init {
  7811. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  7812. }
  7813. - (instancetype)initWithUInt64s:(const uint64_t [])values
  7814. forKeys:(const NSString * [])keys
  7815. count:(NSUInteger)count {
  7816. self = [super init];
  7817. if (self) {
  7818. _dictionary = [[NSMutableDictionary alloc] init];
  7819. if (count && values && keys) {
  7820. for (NSUInteger i = 0; i < count; ++i) {
  7821. if (!keys[i]) {
  7822. [NSException raise:NSInvalidArgumentException
  7823. format:@"Attempting to add nil key to a Dictionary"];
  7824. }
  7825. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  7826. }
  7827. }
  7828. }
  7829. return self;
  7830. }
  7831. - (instancetype)initWithDictionary:(GPBStringUInt64Dictionary *)dictionary {
  7832. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  7833. if (self) {
  7834. if (dictionary) {
  7835. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  7836. }
  7837. }
  7838. return self;
  7839. }
  7840. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  7841. #pragma unused(numItems)
  7842. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  7843. }
  7844. - (void)dealloc {
  7845. NSAssert(!_autocreator,
  7846. @"%@: Autocreator must be cleared before release, autocreator: %@",
  7847. [self class], _autocreator);
  7848. [_dictionary release];
  7849. [super dealloc];
  7850. }
  7851. - (instancetype)copyWithZone:(NSZone *)zone {
  7852. return [[GPBStringUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  7853. }
  7854. - (BOOL)isEqual:(id)other {
  7855. if (self == other) {
  7856. return YES;
  7857. }
  7858. if (![other isKindOfClass:[GPBStringUInt64Dictionary class]]) {
  7859. return NO;
  7860. }
  7861. GPBStringUInt64Dictionary *otherDictionary = other;
  7862. return [_dictionary isEqual:otherDictionary->_dictionary];
  7863. }
  7864. - (NSUInteger)hash {
  7865. return _dictionary.count;
  7866. }
  7867. - (NSString *)description {
  7868. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  7869. }
  7870. - (NSUInteger)count {
  7871. return _dictionary.count;
  7872. }
  7873. - (void)enumerateKeysAndUInt64sUsingBlock:
  7874. (void (NS_NOESCAPE ^)(NSString *key, uint64_t value, BOOL *stop))block {
  7875. BOOL stop = NO;
  7876. NSDictionary *internal = _dictionary;
  7877. NSEnumerator *keys = [internal keyEnumerator];
  7878. NSString *aKey;
  7879. while ((aKey = [keys nextObject])) {
  7880. NSNumber *aValue = internal[aKey];
  7881. block(aKey, [aValue unsignedLongLongValue], &stop);
  7882. if (stop) {
  7883. break;
  7884. }
  7885. }
  7886. }
  7887. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  7888. NSDictionary *internal = _dictionary;
  7889. NSUInteger count = internal.count;
  7890. if (count == 0) {
  7891. return 0;
  7892. }
  7893. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7894. GPBDataType keyDataType = field.mapKeyDataType;
  7895. size_t result = 0;
  7896. NSEnumerator *keys = [internal keyEnumerator];
  7897. NSString *aKey;
  7898. while ((aKey = [keys nextObject])) {
  7899. NSNumber *aValue = internal[aKey];
  7900. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  7901. msgSize += ComputeDictUInt64FieldSize([aValue unsignedLongLongValue], kMapValueFieldNumber, valueDataType);
  7902. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  7903. }
  7904. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  7905. result += tagSize * count;
  7906. return result;
  7907. }
  7908. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  7909. asField:(GPBFieldDescriptor *)field {
  7910. GPBDataType valueDataType = GPBGetFieldDataType(field);
  7911. GPBDataType keyDataType = field.mapKeyDataType;
  7912. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  7913. NSDictionary *internal = _dictionary;
  7914. NSEnumerator *keys = [internal keyEnumerator];
  7915. NSString *aKey;
  7916. while ((aKey = [keys nextObject])) {
  7917. NSNumber *aValue = internal[aKey];
  7918. [outputStream writeInt32NoTag:tag];
  7919. // Write the size of the message.
  7920. NSString *unwrappedKey = aKey;
  7921. uint64_t unwrappedValue = [aValue unsignedLongLongValue];
  7922. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7923. msgSize += ComputeDictUInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  7924. [outputStream writeInt32NoTag:(int32_t)msgSize];
  7925. // Write the fields.
  7926. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  7927. WriteDictUInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  7928. }
  7929. }
  7930. - (void)setGPBGenericValue:(GPBGenericValue *)value
  7931. forGPBGenericValueKey:(GPBGenericValue *)key {
  7932. [_dictionary setObject:@(value->valueUInt64) forKey:key->valueString];
  7933. }
  7934. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  7935. [self enumerateKeysAndUInt64sUsingBlock:^(NSString *key, uint64_t value, BOOL *stop) {
  7936. #pragma unused(stop)
  7937. block(key, [NSString stringWithFormat:@"%llu", value]);
  7938. }];
  7939. }
  7940. - (BOOL)getUInt64:(nullable uint64_t *)value forKey:(NSString *)key {
  7941. NSNumber *wrapped = [_dictionary objectForKey:key];
  7942. if (wrapped && value) {
  7943. *value = [wrapped unsignedLongLongValue];
  7944. }
  7945. return (wrapped != NULL);
  7946. }
  7947. - (void)addEntriesFromDictionary:(GPBStringUInt64Dictionary *)otherDictionary {
  7948. if (otherDictionary) {
  7949. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  7950. if (_autocreator) {
  7951. GPBAutocreatedDictionaryModified(_autocreator, self);
  7952. }
  7953. }
  7954. }
  7955. - (void)setUInt64:(uint64_t)value forKey:(NSString *)key {
  7956. if (!key) {
  7957. [NSException raise:NSInvalidArgumentException
  7958. format:@"Attempting to add nil key to a Dictionary"];
  7959. }
  7960. [_dictionary setObject:@(value) forKey:key];
  7961. if (_autocreator) {
  7962. GPBAutocreatedDictionaryModified(_autocreator, self);
  7963. }
  7964. }
  7965. - (void)removeUInt64ForKey:(NSString *)aKey {
  7966. [_dictionary removeObjectForKey:aKey];
  7967. }
  7968. - (void)removeAll {
  7969. [_dictionary removeAllObjects];
  7970. }
  7971. @end
  7972. #pragma mark - String -> Int64
  7973. @implementation GPBStringInt64Dictionary {
  7974. @package
  7975. NSMutableDictionary *_dictionary;
  7976. }
  7977. - (instancetype)init {
  7978. return [self initWithInt64s:NULL forKeys:NULL count:0];
  7979. }
  7980. - (instancetype)initWithInt64s:(const int64_t [])values
  7981. forKeys:(const NSString * [])keys
  7982. count:(NSUInteger)count {
  7983. self = [super init];
  7984. if (self) {
  7985. _dictionary = [[NSMutableDictionary alloc] init];
  7986. if (count && values && keys) {
  7987. for (NSUInteger i = 0; i < count; ++i) {
  7988. if (!keys[i]) {
  7989. [NSException raise:NSInvalidArgumentException
  7990. format:@"Attempting to add nil key to a Dictionary"];
  7991. }
  7992. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  7993. }
  7994. }
  7995. }
  7996. return self;
  7997. }
  7998. - (instancetype)initWithDictionary:(GPBStringInt64Dictionary *)dictionary {
  7999. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  8000. if (self) {
  8001. if (dictionary) {
  8002. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8003. }
  8004. }
  8005. return self;
  8006. }
  8007. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8008. #pragma unused(numItems)
  8009. return [self initWithInt64s:NULL forKeys:NULL count:0];
  8010. }
  8011. - (void)dealloc {
  8012. NSAssert(!_autocreator,
  8013. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8014. [self class], _autocreator);
  8015. [_dictionary release];
  8016. [super dealloc];
  8017. }
  8018. - (instancetype)copyWithZone:(NSZone *)zone {
  8019. return [[GPBStringInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  8020. }
  8021. - (BOOL)isEqual:(id)other {
  8022. if (self == other) {
  8023. return YES;
  8024. }
  8025. if (![other isKindOfClass:[GPBStringInt64Dictionary class]]) {
  8026. return NO;
  8027. }
  8028. GPBStringInt64Dictionary *otherDictionary = other;
  8029. return [_dictionary isEqual:otherDictionary->_dictionary];
  8030. }
  8031. - (NSUInteger)hash {
  8032. return _dictionary.count;
  8033. }
  8034. - (NSString *)description {
  8035. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8036. }
  8037. - (NSUInteger)count {
  8038. return _dictionary.count;
  8039. }
  8040. - (void)enumerateKeysAndInt64sUsingBlock:
  8041. (void (NS_NOESCAPE ^)(NSString *key, int64_t value, BOOL *stop))block {
  8042. BOOL stop = NO;
  8043. NSDictionary *internal = _dictionary;
  8044. NSEnumerator *keys = [internal keyEnumerator];
  8045. NSString *aKey;
  8046. while ((aKey = [keys nextObject])) {
  8047. NSNumber *aValue = internal[aKey];
  8048. block(aKey, [aValue longLongValue], &stop);
  8049. if (stop) {
  8050. break;
  8051. }
  8052. }
  8053. }
  8054. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8055. NSDictionary *internal = _dictionary;
  8056. NSUInteger count = internal.count;
  8057. if (count == 0) {
  8058. return 0;
  8059. }
  8060. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8061. GPBDataType keyDataType = field.mapKeyDataType;
  8062. size_t result = 0;
  8063. NSEnumerator *keys = [internal keyEnumerator];
  8064. NSString *aKey;
  8065. while ((aKey = [keys nextObject])) {
  8066. NSNumber *aValue = internal[aKey];
  8067. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8068. msgSize += ComputeDictInt64FieldSize([aValue longLongValue], kMapValueFieldNumber, valueDataType);
  8069. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8070. }
  8071. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8072. result += tagSize * count;
  8073. return result;
  8074. }
  8075. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8076. asField:(GPBFieldDescriptor *)field {
  8077. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8078. GPBDataType keyDataType = field.mapKeyDataType;
  8079. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8080. NSDictionary *internal = _dictionary;
  8081. NSEnumerator *keys = [internal keyEnumerator];
  8082. NSString *aKey;
  8083. while ((aKey = [keys nextObject])) {
  8084. NSNumber *aValue = internal[aKey];
  8085. [outputStream writeInt32NoTag:tag];
  8086. // Write the size of the message.
  8087. NSString *unwrappedKey = aKey;
  8088. int64_t unwrappedValue = [aValue longLongValue];
  8089. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8090. msgSize += ComputeDictInt64FieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  8091. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8092. // Write the fields.
  8093. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8094. WriteDictInt64Field(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  8095. }
  8096. }
  8097. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8098. forGPBGenericValueKey:(GPBGenericValue *)key {
  8099. [_dictionary setObject:@(value->valueInt64) forKey:key->valueString];
  8100. }
  8101. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  8102. [self enumerateKeysAndInt64sUsingBlock:^(NSString *key, int64_t value, BOOL *stop) {
  8103. #pragma unused(stop)
  8104. block(key, [NSString stringWithFormat:@"%lld", value]);
  8105. }];
  8106. }
  8107. - (BOOL)getInt64:(nullable int64_t *)value forKey:(NSString *)key {
  8108. NSNumber *wrapped = [_dictionary objectForKey:key];
  8109. if (wrapped && value) {
  8110. *value = [wrapped longLongValue];
  8111. }
  8112. return (wrapped != NULL);
  8113. }
  8114. - (void)addEntriesFromDictionary:(GPBStringInt64Dictionary *)otherDictionary {
  8115. if (otherDictionary) {
  8116. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8117. if (_autocreator) {
  8118. GPBAutocreatedDictionaryModified(_autocreator, self);
  8119. }
  8120. }
  8121. }
  8122. - (void)setInt64:(int64_t)value forKey:(NSString *)key {
  8123. if (!key) {
  8124. [NSException raise:NSInvalidArgumentException
  8125. format:@"Attempting to add nil key to a Dictionary"];
  8126. }
  8127. [_dictionary setObject:@(value) forKey:key];
  8128. if (_autocreator) {
  8129. GPBAutocreatedDictionaryModified(_autocreator, self);
  8130. }
  8131. }
  8132. - (void)removeInt64ForKey:(NSString *)aKey {
  8133. [_dictionary removeObjectForKey:aKey];
  8134. }
  8135. - (void)removeAll {
  8136. [_dictionary removeAllObjects];
  8137. }
  8138. @end
  8139. #pragma mark - String -> Bool
  8140. @implementation GPBStringBoolDictionary {
  8141. @package
  8142. NSMutableDictionary *_dictionary;
  8143. }
  8144. - (instancetype)init {
  8145. return [self initWithBools:NULL forKeys:NULL count:0];
  8146. }
  8147. - (instancetype)initWithBools:(const BOOL [])values
  8148. forKeys:(const NSString * [])keys
  8149. count:(NSUInteger)count {
  8150. self = [super init];
  8151. if (self) {
  8152. _dictionary = [[NSMutableDictionary alloc] init];
  8153. if (count && values && keys) {
  8154. for (NSUInteger i = 0; i < count; ++i) {
  8155. if (!keys[i]) {
  8156. [NSException raise:NSInvalidArgumentException
  8157. format:@"Attempting to add nil key to a Dictionary"];
  8158. }
  8159. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8160. }
  8161. }
  8162. }
  8163. return self;
  8164. }
  8165. - (instancetype)initWithDictionary:(GPBStringBoolDictionary *)dictionary {
  8166. self = [self initWithBools:NULL forKeys:NULL count:0];
  8167. if (self) {
  8168. if (dictionary) {
  8169. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8170. }
  8171. }
  8172. return self;
  8173. }
  8174. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8175. #pragma unused(numItems)
  8176. return [self initWithBools:NULL forKeys:NULL count:0];
  8177. }
  8178. - (void)dealloc {
  8179. NSAssert(!_autocreator,
  8180. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8181. [self class], _autocreator);
  8182. [_dictionary release];
  8183. [super dealloc];
  8184. }
  8185. - (instancetype)copyWithZone:(NSZone *)zone {
  8186. return [[GPBStringBoolDictionary allocWithZone:zone] initWithDictionary:self];
  8187. }
  8188. - (BOOL)isEqual:(id)other {
  8189. if (self == other) {
  8190. return YES;
  8191. }
  8192. if (![other isKindOfClass:[GPBStringBoolDictionary class]]) {
  8193. return NO;
  8194. }
  8195. GPBStringBoolDictionary *otherDictionary = other;
  8196. return [_dictionary isEqual:otherDictionary->_dictionary];
  8197. }
  8198. - (NSUInteger)hash {
  8199. return _dictionary.count;
  8200. }
  8201. - (NSString *)description {
  8202. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8203. }
  8204. - (NSUInteger)count {
  8205. return _dictionary.count;
  8206. }
  8207. - (void)enumerateKeysAndBoolsUsingBlock:
  8208. (void (NS_NOESCAPE ^)(NSString *key, BOOL value, BOOL *stop))block {
  8209. BOOL stop = NO;
  8210. NSDictionary *internal = _dictionary;
  8211. NSEnumerator *keys = [internal keyEnumerator];
  8212. NSString *aKey;
  8213. while ((aKey = [keys nextObject])) {
  8214. NSNumber *aValue = internal[aKey];
  8215. block(aKey, [aValue boolValue], &stop);
  8216. if (stop) {
  8217. break;
  8218. }
  8219. }
  8220. }
  8221. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8222. NSDictionary *internal = _dictionary;
  8223. NSUInteger count = internal.count;
  8224. if (count == 0) {
  8225. return 0;
  8226. }
  8227. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8228. GPBDataType keyDataType = field.mapKeyDataType;
  8229. size_t result = 0;
  8230. NSEnumerator *keys = [internal keyEnumerator];
  8231. NSString *aKey;
  8232. while ((aKey = [keys nextObject])) {
  8233. NSNumber *aValue = internal[aKey];
  8234. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8235. msgSize += ComputeDictBoolFieldSize([aValue boolValue], kMapValueFieldNumber, valueDataType);
  8236. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8237. }
  8238. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8239. result += tagSize * count;
  8240. return result;
  8241. }
  8242. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8243. asField:(GPBFieldDescriptor *)field {
  8244. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8245. GPBDataType keyDataType = field.mapKeyDataType;
  8246. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8247. NSDictionary *internal = _dictionary;
  8248. NSEnumerator *keys = [internal keyEnumerator];
  8249. NSString *aKey;
  8250. while ((aKey = [keys nextObject])) {
  8251. NSNumber *aValue = internal[aKey];
  8252. [outputStream writeInt32NoTag:tag];
  8253. // Write the size of the message.
  8254. NSString *unwrappedKey = aKey;
  8255. BOOL unwrappedValue = [aValue boolValue];
  8256. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8257. msgSize += ComputeDictBoolFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  8258. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8259. // Write the fields.
  8260. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8261. WriteDictBoolField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  8262. }
  8263. }
  8264. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8265. forGPBGenericValueKey:(GPBGenericValue *)key {
  8266. [_dictionary setObject:@(value->valueBool) forKey:key->valueString];
  8267. }
  8268. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  8269. [self enumerateKeysAndBoolsUsingBlock:^(NSString *key, BOOL value, BOOL *stop) {
  8270. #pragma unused(stop)
  8271. block(key, (value ? @"true" : @"false"));
  8272. }];
  8273. }
  8274. - (BOOL)getBool:(nullable BOOL *)value forKey:(NSString *)key {
  8275. NSNumber *wrapped = [_dictionary objectForKey:key];
  8276. if (wrapped && value) {
  8277. *value = [wrapped boolValue];
  8278. }
  8279. return (wrapped != NULL);
  8280. }
  8281. - (void)addEntriesFromDictionary:(GPBStringBoolDictionary *)otherDictionary {
  8282. if (otherDictionary) {
  8283. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8284. if (_autocreator) {
  8285. GPBAutocreatedDictionaryModified(_autocreator, self);
  8286. }
  8287. }
  8288. }
  8289. - (void)setBool:(BOOL)value forKey:(NSString *)key {
  8290. if (!key) {
  8291. [NSException raise:NSInvalidArgumentException
  8292. format:@"Attempting to add nil key to a Dictionary"];
  8293. }
  8294. [_dictionary setObject:@(value) forKey:key];
  8295. if (_autocreator) {
  8296. GPBAutocreatedDictionaryModified(_autocreator, self);
  8297. }
  8298. }
  8299. - (void)removeBoolForKey:(NSString *)aKey {
  8300. [_dictionary removeObjectForKey:aKey];
  8301. }
  8302. - (void)removeAll {
  8303. [_dictionary removeAllObjects];
  8304. }
  8305. @end
  8306. #pragma mark - String -> Float
  8307. @implementation GPBStringFloatDictionary {
  8308. @package
  8309. NSMutableDictionary *_dictionary;
  8310. }
  8311. - (instancetype)init {
  8312. return [self initWithFloats:NULL forKeys:NULL count:0];
  8313. }
  8314. - (instancetype)initWithFloats:(const float [])values
  8315. forKeys:(const NSString * [])keys
  8316. count:(NSUInteger)count {
  8317. self = [super init];
  8318. if (self) {
  8319. _dictionary = [[NSMutableDictionary alloc] init];
  8320. if (count && values && keys) {
  8321. for (NSUInteger i = 0; i < count; ++i) {
  8322. if (!keys[i]) {
  8323. [NSException raise:NSInvalidArgumentException
  8324. format:@"Attempting to add nil key to a Dictionary"];
  8325. }
  8326. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8327. }
  8328. }
  8329. }
  8330. return self;
  8331. }
  8332. - (instancetype)initWithDictionary:(GPBStringFloatDictionary *)dictionary {
  8333. self = [self initWithFloats:NULL forKeys:NULL count:0];
  8334. if (self) {
  8335. if (dictionary) {
  8336. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8337. }
  8338. }
  8339. return self;
  8340. }
  8341. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8342. #pragma unused(numItems)
  8343. return [self initWithFloats:NULL forKeys:NULL count:0];
  8344. }
  8345. - (void)dealloc {
  8346. NSAssert(!_autocreator,
  8347. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8348. [self class], _autocreator);
  8349. [_dictionary release];
  8350. [super dealloc];
  8351. }
  8352. - (instancetype)copyWithZone:(NSZone *)zone {
  8353. return [[GPBStringFloatDictionary allocWithZone:zone] initWithDictionary:self];
  8354. }
  8355. - (BOOL)isEqual:(id)other {
  8356. if (self == other) {
  8357. return YES;
  8358. }
  8359. if (![other isKindOfClass:[GPBStringFloatDictionary class]]) {
  8360. return NO;
  8361. }
  8362. GPBStringFloatDictionary *otherDictionary = other;
  8363. return [_dictionary isEqual:otherDictionary->_dictionary];
  8364. }
  8365. - (NSUInteger)hash {
  8366. return _dictionary.count;
  8367. }
  8368. - (NSString *)description {
  8369. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8370. }
  8371. - (NSUInteger)count {
  8372. return _dictionary.count;
  8373. }
  8374. - (void)enumerateKeysAndFloatsUsingBlock:
  8375. (void (NS_NOESCAPE ^)(NSString *key, float value, BOOL *stop))block {
  8376. BOOL stop = NO;
  8377. NSDictionary *internal = _dictionary;
  8378. NSEnumerator *keys = [internal keyEnumerator];
  8379. NSString *aKey;
  8380. while ((aKey = [keys nextObject])) {
  8381. NSNumber *aValue = internal[aKey];
  8382. block(aKey, [aValue floatValue], &stop);
  8383. if (stop) {
  8384. break;
  8385. }
  8386. }
  8387. }
  8388. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8389. NSDictionary *internal = _dictionary;
  8390. NSUInteger count = internal.count;
  8391. if (count == 0) {
  8392. return 0;
  8393. }
  8394. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8395. GPBDataType keyDataType = field.mapKeyDataType;
  8396. size_t result = 0;
  8397. NSEnumerator *keys = [internal keyEnumerator];
  8398. NSString *aKey;
  8399. while ((aKey = [keys nextObject])) {
  8400. NSNumber *aValue = internal[aKey];
  8401. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8402. msgSize += ComputeDictFloatFieldSize([aValue floatValue], kMapValueFieldNumber, valueDataType);
  8403. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8404. }
  8405. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8406. result += tagSize * count;
  8407. return result;
  8408. }
  8409. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8410. asField:(GPBFieldDescriptor *)field {
  8411. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8412. GPBDataType keyDataType = field.mapKeyDataType;
  8413. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8414. NSDictionary *internal = _dictionary;
  8415. NSEnumerator *keys = [internal keyEnumerator];
  8416. NSString *aKey;
  8417. while ((aKey = [keys nextObject])) {
  8418. NSNumber *aValue = internal[aKey];
  8419. [outputStream writeInt32NoTag:tag];
  8420. // Write the size of the message.
  8421. NSString *unwrappedKey = aKey;
  8422. float unwrappedValue = [aValue floatValue];
  8423. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8424. msgSize += ComputeDictFloatFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  8425. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8426. // Write the fields.
  8427. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8428. WriteDictFloatField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  8429. }
  8430. }
  8431. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8432. forGPBGenericValueKey:(GPBGenericValue *)key {
  8433. [_dictionary setObject:@(value->valueFloat) forKey:key->valueString];
  8434. }
  8435. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  8436. [self enumerateKeysAndFloatsUsingBlock:^(NSString *key, float value, BOOL *stop) {
  8437. #pragma unused(stop)
  8438. block(key, [NSString stringWithFormat:@"%.*g", FLT_DIG, value]);
  8439. }];
  8440. }
  8441. - (BOOL)getFloat:(nullable float *)value forKey:(NSString *)key {
  8442. NSNumber *wrapped = [_dictionary objectForKey:key];
  8443. if (wrapped && value) {
  8444. *value = [wrapped floatValue];
  8445. }
  8446. return (wrapped != NULL);
  8447. }
  8448. - (void)addEntriesFromDictionary:(GPBStringFloatDictionary *)otherDictionary {
  8449. if (otherDictionary) {
  8450. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8451. if (_autocreator) {
  8452. GPBAutocreatedDictionaryModified(_autocreator, self);
  8453. }
  8454. }
  8455. }
  8456. - (void)setFloat:(float)value forKey:(NSString *)key {
  8457. if (!key) {
  8458. [NSException raise:NSInvalidArgumentException
  8459. format:@"Attempting to add nil key to a Dictionary"];
  8460. }
  8461. [_dictionary setObject:@(value) forKey:key];
  8462. if (_autocreator) {
  8463. GPBAutocreatedDictionaryModified(_autocreator, self);
  8464. }
  8465. }
  8466. - (void)removeFloatForKey:(NSString *)aKey {
  8467. [_dictionary removeObjectForKey:aKey];
  8468. }
  8469. - (void)removeAll {
  8470. [_dictionary removeAllObjects];
  8471. }
  8472. @end
  8473. #pragma mark - String -> Double
  8474. @implementation GPBStringDoubleDictionary {
  8475. @package
  8476. NSMutableDictionary *_dictionary;
  8477. }
  8478. - (instancetype)init {
  8479. return [self initWithDoubles:NULL forKeys:NULL count:0];
  8480. }
  8481. - (instancetype)initWithDoubles:(const double [])values
  8482. forKeys:(const NSString * [])keys
  8483. count:(NSUInteger)count {
  8484. self = [super init];
  8485. if (self) {
  8486. _dictionary = [[NSMutableDictionary alloc] init];
  8487. if (count && values && keys) {
  8488. for (NSUInteger i = 0; i < count; ++i) {
  8489. if (!keys[i]) {
  8490. [NSException raise:NSInvalidArgumentException
  8491. format:@"Attempting to add nil key to a Dictionary"];
  8492. }
  8493. [_dictionary setObject:@(values[i]) forKey:keys[i]];
  8494. }
  8495. }
  8496. }
  8497. return self;
  8498. }
  8499. - (instancetype)initWithDictionary:(GPBStringDoubleDictionary *)dictionary {
  8500. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  8501. if (self) {
  8502. if (dictionary) {
  8503. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8504. }
  8505. }
  8506. return self;
  8507. }
  8508. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8509. #pragma unused(numItems)
  8510. return [self initWithDoubles:NULL forKeys:NULL count:0];
  8511. }
  8512. - (void)dealloc {
  8513. NSAssert(!_autocreator,
  8514. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8515. [self class], _autocreator);
  8516. [_dictionary release];
  8517. [super dealloc];
  8518. }
  8519. - (instancetype)copyWithZone:(NSZone *)zone {
  8520. return [[GPBStringDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  8521. }
  8522. - (BOOL)isEqual:(id)other {
  8523. if (self == other) {
  8524. return YES;
  8525. }
  8526. if (![other isKindOfClass:[GPBStringDoubleDictionary class]]) {
  8527. return NO;
  8528. }
  8529. GPBStringDoubleDictionary *otherDictionary = other;
  8530. return [_dictionary isEqual:otherDictionary->_dictionary];
  8531. }
  8532. - (NSUInteger)hash {
  8533. return _dictionary.count;
  8534. }
  8535. - (NSString *)description {
  8536. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8537. }
  8538. - (NSUInteger)count {
  8539. return _dictionary.count;
  8540. }
  8541. - (void)enumerateKeysAndDoublesUsingBlock:
  8542. (void (NS_NOESCAPE ^)(NSString *key, double value, BOOL *stop))block {
  8543. BOOL stop = NO;
  8544. NSDictionary *internal = _dictionary;
  8545. NSEnumerator *keys = [internal keyEnumerator];
  8546. NSString *aKey;
  8547. while ((aKey = [keys nextObject])) {
  8548. NSNumber *aValue = internal[aKey];
  8549. block(aKey, [aValue doubleValue], &stop);
  8550. if (stop) {
  8551. break;
  8552. }
  8553. }
  8554. }
  8555. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8556. NSDictionary *internal = _dictionary;
  8557. NSUInteger count = internal.count;
  8558. if (count == 0) {
  8559. return 0;
  8560. }
  8561. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8562. GPBDataType keyDataType = field.mapKeyDataType;
  8563. size_t result = 0;
  8564. NSEnumerator *keys = [internal keyEnumerator];
  8565. NSString *aKey;
  8566. while ((aKey = [keys nextObject])) {
  8567. NSNumber *aValue = internal[aKey];
  8568. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8569. msgSize += ComputeDictDoubleFieldSize([aValue doubleValue], kMapValueFieldNumber, valueDataType);
  8570. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8571. }
  8572. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8573. result += tagSize * count;
  8574. return result;
  8575. }
  8576. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8577. asField:(GPBFieldDescriptor *)field {
  8578. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8579. GPBDataType keyDataType = field.mapKeyDataType;
  8580. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8581. NSDictionary *internal = _dictionary;
  8582. NSEnumerator *keys = [internal keyEnumerator];
  8583. NSString *aKey;
  8584. while ((aKey = [keys nextObject])) {
  8585. NSNumber *aValue = internal[aKey];
  8586. [outputStream writeInt32NoTag:tag];
  8587. // Write the size of the message.
  8588. NSString *unwrappedKey = aKey;
  8589. double unwrappedValue = [aValue doubleValue];
  8590. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8591. msgSize += ComputeDictDoubleFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  8592. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8593. // Write the fields.
  8594. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8595. WriteDictDoubleField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  8596. }
  8597. }
  8598. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8599. forGPBGenericValueKey:(GPBGenericValue *)key {
  8600. [_dictionary setObject:@(value->valueDouble) forKey:key->valueString];
  8601. }
  8602. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  8603. [self enumerateKeysAndDoublesUsingBlock:^(NSString *key, double value, BOOL *stop) {
  8604. #pragma unused(stop)
  8605. block(key, [NSString stringWithFormat:@"%.*lg", DBL_DIG, value]);
  8606. }];
  8607. }
  8608. - (BOOL)getDouble:(nullable double *)value forKey:(NSString *)key {
  8609. NSNumber *wrapped = [_dictionary objectForKey:key];
  8610. if (wrapped && value) {
  8611. *value = [wrapped doubleValue];
  8612. }
  8613. return (wrapped != NULL);
  8614. }
  8615. - (void)addEntriesFromDictionary:(GPBStringDoubleDictionary *)otherDictionary {
  8616. if (otherDictionary) {
  8617. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8618. if (_autocreator) {
  8619. GPBAutocreatedDictionaryModified(_autocreator, self);
  8620. }
  8621. }
  8622. }
  8623. - (void)setDouble:(double)value forKey:(NSString *)key {
  8624. if (!key) {
  8625. [NSException raise:NSInvalidArgumentException
  8626. format:@"Attempting to add nil key to a Dictionary"];
  8627. }
  8628. [_dictionary setObject:@(value) forKey:key];
  8629. if (_autocreator) {
  8630. GPBAutocreatedDictionaryModified(_autocreator, self);
  8631. }
  8632. }
  8633. - (void)removeDoubleForKey:(NSString *)aKey {
  8634. [_dictionary removeObjectForKey:aKey];
  8635. }
  8636. - (void)removeAll {
  8637. [_dictionary removeAllObjects];
  8638. }
  8639. @end
  8640. #pragma mark - String -> Enum
  8641. @implementation GPBStringEnumDictionary {
  8642. @package
  8643. NSMutableDictionary *_dictionary;
  8644. GPBEnumValidationFunc _validationFunc;
  8645. }
  8646. @synthesize validationFunc = _validationFunc;
  8647. - (instancetype)init {
  8648. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  8649. }
  8650. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  8651. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  8652. }
  8653. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  8654. rawValues:(const int32_t [])rawValues
  8655. forKeys:(const NSString * [])keys
  8656. count:(NSUInteger)count {
  8657. self = [super init];
  8658. if (self) {
  8659. _dictionary = [[NSMutableDictionary alloc] init];
  8660. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  8661. if (count && rawValues && keys) {
  8662. for (NSUInteger i = 0; i < count; ++i) {
  8663. if (!keys[i]) {
  8664. [NSException raise:NSInvalidArgumentException
  8665. format:@"Attempting to add nil key to a Dictionary"];
  8666. }
  8667. [_dictionary setObject:@(rawValues[i]) forKey:keys[i]];
  8668. }
  8669. }
  8670. }
  8671. return self;
  8672. }
  8673. - (instancetype)initWithDictionary:(GPBStringEnumDictionary *)dictionary {
  8674. self = [self initWithValidationFunction:dictionary.validationFunc
  8675. rawValues:NULL
  8676. forKeys:NULL
  8677. count:0];
  8678. if (self) {
  8679. if (dictionary) {
  8680. [_dictionary addEntriesFromDictionary:dictionary->_dictionary];
  8681. }
  8682. }
  8683. return self;
  8684. }
  8685. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  8686. capacity:(NSUInteger)numItems {
  8687. #pragma unused(numItems)
  8688. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  8689. }
  8690. - (void)dealloc {
  8691. NSAssert(!_autocreator,
  8692. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8693. [self class], _autocreator);
  8694. [_dictionary release];
  8695. [super dealloc];
  8696. }
  8697. - (instancetype)copyWithZone:(NSZone *)zone {
  8698. return [[GPBStringEnumDictionary allocWithZone:zone] initWithDictionary:self];
  8699. }
  8700. - (BOOL)isEqual:(id)other {
  8701. if (self == other) {
  8702. return YES;
  8703. }
  8704. if (![other isKindOfClass:[GPBStringEnumDictionary class]]) {
  8705. return NO;
  8706. }
  8707. GPBStringEnumDictionary *otherDictionary = other;
  8708. return [_dictionary isEqual:otherDictionary->_dictionary];
  8709. }
  8710. - (NSUInteger)hash {
  8711. return _dictionary.count;
  8712. }
  8713. - (NSString *)description {
  8714. return [NSString stringWithFormat:@"<%@ %p> { %@ }", [self class], self, _dictionary];
  8715. }
  8716. - (NSUInteger)count {
  8717. return _dictionary.count;
  8718. }
  8719. - (void)enumerateKeysAndRawValuesUsingBlock:
  8720. (void (NS_NOESCAPE ^)(NSString *key, int32_t value, BOOL *stop))block {
  8721. BOOL stop = NO;
  8722. NSDictionary *internal = _dictionary;
  8723. NSEnumerator *keys = [internal keyEnumerator];
  8724. NSString *aKey;
  8725. while ((aKey = [keys nextObject])) {
  8726. NSNumber *aValue = internal[aKey];
  8727. block(aKey, [aValue intValue], &stop);
  8728. if (stop) {
  8729. break;
  8730. }
  8731. }
  8732. }
  8733. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  8734. NSDictionary *internal = _dictionary;
  8735. NSUInteger count = internal.count;
  8736. if (count == 0) {
  8737. return 0;
  8738. }
  8739. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8740. GPBDataType keyDataType = field.mapKeyDataType;
  8741. size_t result = 0;
  8742. NSEnumerator *keys = [internal keyEnumerator];
  8743. NSString *aKey;
  8744. while ((aKey = [keys nextObject])) {
  8745. NSNumber *aValue = internal[aKey];
  8746. size_t msgSize = ComputeDictStringFieldSize(aKey, kMapKeyFieldNumber, keyDataType);
  8747. msgSize += ComputeDictEnumFieldSize([aValue intValue], kMapValueFieldNumber, valueDataType);
  8748. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  8749. }
  8750. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  8751. result += tagSize * count;
  8752. return result;
  8753. }
  8754. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  8755. asField:(GPBFieldDescriptor *)field {
  8756. GPBDataType valueDataType = GPBGetFieldDataType(field);
  8757. GPBDataType keyDataType = field.mapKeyDataType;
  8758. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  8759. NSDictionary *internal = _dictionary;
  8760. NSEnumerator *keys = [internal keyEnumerator];
  8761. NSString *aKey;
  8762. while ((aKey = [keys nextObject])) {
  8763. NSNumber *aValue = internal[aKey];
  8764. [outputStream writeInt32NoTag:tag];
  8765. // Write the size of the message.
  8766. NSString *unwrappedKey = aKey;
  8767. int32_t unwrappedValue = [aValue intValue];
  8768. size_t msgSize = ComputeDictStringFieldSize(unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8769. msgSize += ComputeDictEnumFieldSize(unwrappedValue, kMapValueFieldNumber, valueDataType);
  8770. [outputStream writeInt32NoTag:(int32_t)msgSize];
  8771. // Write the fields.
  8772. WriteDictStringField(outputStream, unwrappedKey, kMapKeyFieldNumber, keyDataType);
  8773. WriteDictEnumField(outputStream, unwrappedValue, kMapValueFieldNumber, valueDataType);
  8774. }
  8775. }
  8776. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  8777. forKey:(GPBGenericValue *)key
  8778. keyDataType:(GPBDataType)keyDataType {
  8779. size_t msgSize = ComputeDictStringFieldSize(key->valueString, kMapKeyFieldNumber, keyDataType);
  8780. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  8781. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  8782. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  8783. WriteDictStringField(outputStream, key->valueString, kMapKeyFieldNumber, keyDataType);
  8784. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  8785. [outputStream release];
  8786. return data;
  8787. }
  8788. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8789. forGPBGenericValueKey:(GPBGenericValue *)key {
  8790. [_dictionary setObject:@(value->valueEnum) forKey:key->valueString];
  8791. }
  8792. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  8793. [self enumerateKeysAndRawValuesUsingBlock:^(NSString *key, int32_t value, BOOL *stop) {
  8794. #pragma unused(stop)
  8795. block(key, @(value));
  8796. }];
  8797. }
  8798. - (BOOL)getEnum:(int32_t *)value forKey:(NSString *)key {
  8799. NSNumber *wrapped = [_dictionary objectForKey:key];
  8800. if (wrapped && value) {
  8801. int32_t result = [wrapped intValue];
  8802. if (!_validationFunc(result)) {
  8803. result = kGPBUnrecognizedEnumeratorValue;
  8804. }
  8805. *value = result;
  8806. }
  8807. return (wrapped != NULL);
  8808. }
  8809. - (BOOL)getRawValue:(int32_t *)rawValue forKey:(NSString *)key {
  8810. NSNumber *wrapped = [_dictionary objectForKey:key];
  8811. if (wrapped && rawValue) {
  8812. *rawValue = [wrapped intValue];
  8813. }
  8814. return (wrapped != NULL);
  8815. }
  8816. - (void)enumerateKeysAndEnumsUsingBlock:
  8817. (void (NS_NOESCAPE ^)(NSString *key, int32_t value, BOOL *stop))block {
  8818. GPBEnumValidationFunc func = _validationFunc;
  8819. BOOL stop = NO;
  8820. NSEnumerator *keys = [_dictionary keyEnumerator];
  8821. NSString *aKey;
  8822. while ((aKey = [keys nextObject])) {
  8823. NSNumber *aValue = _dictionary[aKey];
  8824. int32_t unwrapped = [aValue intValue];
  8825. if (!func(unwrapped)) {
  8826. unwrapped = kGPBUnrecognizedEnumeratorValue;
  8827. }
  8828. block(aKey, unwrapped, &stop);
  8829. if (stop) {
  8830. break;
  8831. }
  8832. }
  8833. }
  8834. - (void)addRawEntriesFromDictionary:(GPBStringEnumDictionary *)otherDictionary {
  8835. if (otherDictionary) {
  8836. [_dictionary addEntriesFromDictionary:otherDictionary->_dictionary];
  8837. if (_autocreator) {
  8838. GPBAutocreatedDictionaryModified(_autocreator, self);
  8839. }
  8840. }
  8841. }
  8842. - (void)setRawValue:(int32_t)value forKey:(NSString *)key {
  8843. if (!key) {
  8844. [NSException raise:NSInvalidArgumentException
  8845. format:@"Attempting to add nil key to a Dictionary"];
  8846. }
  8847. [_dictionary setObject:@(value) forKey:key];
  8848. if (_autocreator) {
  8849. GPBAutocreatedDictionaryModified(_autocreator, self);
  8850. }
  8851. }
  8852. - (void)removeEnumForKey:(NSString *)aKey {
  8853. [_dictionary removeObjectForKey:aKey];
  8854. }
  8855. - (void)removeAll {
  8856. [_dictionary removeAllObjects];
  8857. }
  8858. - (void)setEnum:(int32_t)value forKey:(NSString *)key {
  8859. if (!key) {
  8860. [NSException raise:NSInvalidArgumentException
  8861. format:@"Attempting to add nil key to a Dictionary"];
  8862. }
  8863. if (!_validationFunc(value)) {
  8864. [NSException raise:NSInvalidArgumentException
  8865. format:@"GPBStringEnumDictionary: Attempt to set an unknown enum value (%d)",
  8866. value];
  8867. }
  8868. [_dictionary setObject:@(value) forKey:key];
  8869. if (_autocreator) {
  8870. GPBAutocreatedDictionaryModified(_autocreator, self);
  8871. }
  8872. }
  8873. @end
  8874. // clang-format on
  8875. //%PDDM-EXPAND-END (5 expansions)
  8876. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt32, uint32_t)
  8877. // This block of code is generated, do not edit it directly.
  8878. // clang-format off
  8879. #pragma mark - Bool -> UInt32
  8880. @implementation GPBBoolUInt32Dictionary {
  8881. @package
  8882. uint32_t _values[2];
  8883. BOOL _valueSet[2];
  8884. }
  8885. - (instancetype)init {
  8886. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  8887. }
  8888. - (instancetype)initWithUInt32s:(const uint32_t [])values
  8889. forKeys:(const BOOL [])keys
  8890. count:(NSUInteger)count {
  8891. self = [super init];
  8892. if (self) {
  8893. for (NSUInteger i = 0; i < count; ++i) {
  8894. int idx = keys[i] ? 1 : 0;
  8895. _values[idx] = values[i];
  8896. _valueSet[idx] = YES;
  8897. }
  8898. }
  8899. return self;
  8900. }
  8901. - (instancetype)initWithDictionary:(GPBBoolUInt32Dictionary *)dictionary {
  8902. self = [self initWithUInt32s:NULL forKeys:NULL count:0];
  8903. if (self) {
  8904. if (dictionary) {
  8905. for (int i = 0; i < 2; ++i) {
  8906. if (dictionary->_valueSet[i]) {
  8907. _values[i] = dictionary->_values[i];
  8908. _valueSet[i] = YES;
  8909. }
  8910. }
  8911. }
  8912. }
  8913. return self;
  8914. }
  8915. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  8916. #pragma unused(numItems)
  8917. return [self initWithUInt32s:NULL forKeys:NULL count:0];
  8918. }
  8919. #if !defined(NS_BLOCK_ASSERTIONS)
  8920. - (void)dealloc {
  8921. NSAssert(!_autocreator,
  8922. @"%@: Autocreator must be cleared before release, autocreator: %@",
  8923. [self class], _autocreator);
  8924. [super dealloc];
  8925. }
  8926. #endif // !defined(NS_BLOCK_ASSERTIONS)
  8927. - (instancetype)copyWithZone:(NSZone *)zone {
  8928. return [[GPBBoolUInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  8929. }
  8930. - (BOOL)isEqual:(id)other {
  8931. if (self == other) {
  8932. return YES;
  8933. }
  8934. if (![other isKindOfClass:[GPBBoolUInt32Dictionary class]]) {
  8935. return NO;
  8936. }
  8937. GPBBoolUInt32Dictionary *otherDictionary = other;
  8938. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  8939. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  8940. return NO;
  8941. }
  8942. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  8943. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  8944. return NO;
  8945. }
  8946. return YES;
  8947. }
  8948. - (NSUInteger)hash {
  8949. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  8950. }
  8951. - (NSString *)description {
  8952. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  8953. if (_valueSet[0]) {
  8954. [result appendFormat:@"NO: %u", _values[0]];
  8955. }
  8956. if (_valueSet[1]) {
  8957. [result appendFormat:@"YES: %u", _values[1]];
  8958. }
  8959. [result appendString:@" }"];
  8960. return result;
  8961. }
  8962. - (NSUInteger)count {
  8963. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  8964. }
  8965. - (BOOL)getUInt32:(uint32_t *)value forKey:(BOOL)key {
  8966. int idx = (key ? 1 : 0);
  8967. if (_valueSet[idx]) {
  8968. if (value) {
  8969. *value = _values[idx];
  8970. }
  8971. return YES;
  8972. }
  8973. return NO;
  8974. }
  8975. - (void)setGPBGenericValue:(GPBGenericValue *)value
  8976. forGPBGenericValueKey:(GPBGenericValue *)key {
  8977. int idx = (key->valueBool ? 1 : 0);
  8978. _values[idx] = value->valueUInt32;
  8979. _valueSet[idx] = YES;
  8980. }
  8981. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  8982. if (_valueSet[0]) {
  8983. block(@"false", [NSString stringWithFormat:@"%u", _values[0]]);
  8984. }
  8985. if (_valueSet[1]) {
  8986. block(@"true", [NSString stringWithFormat:@"%u", _values[1]]);
  8987. }
  8988. }
  8989. - (void)enumerateKeysAndUInt32sUsingBlock:
  8990. (void (NS_NOESCAPE ^)(BOOL key, uint32_t value, BOOL *stop))block {
  8991. BOOL stop = NO;
  8992. if (_valueSet[0]) {
  8993. block(NO, _values[0], &stop);
  8994. }
  8995. if (!stop && _valueSet[1]) {
  8996. block(YES, _values[1], &stop);
  8997. }
  8998. }
  8999. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9000. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9001. NSUInteger count = 0;
  9002. size_t result = 0;
  9003. for (int i = 0; i < 2; ++i) {
  9004. if (_valueSet[i]) {
  9005. ++count;
  9006. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9007. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9008. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9009. }
  9010. }
  9011. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9012. result += tagSize * count;
  9013. return result;
  9014. }
  9015. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9016. asField:(GPBFieldDescriptor *)field {
  9017. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9018. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9019. for (int i = 0; i < 2; ++i) {
  9020. if (_valueSet[i]) {
  9021. // Write the tag.
  9022. [outputStream writeInt32NoTag:tag];
  9023. // Write the size of the message.
  9024. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9025. msgSize += ComputeDictUInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9026. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9027. // Write the fields.
  9028. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9029. WriteDictUInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9030. }
  9031. }
  9032. }
  9033. - (void)addEntriesFromDictionary:(GPBBoolUInt32Dictionary *)otherDictionary {
  9034. if (otherDictionary) {
  9035. for (int i = 0; i < 2; ++i) {
  9036. if (otherDictionary->_valueSet[i]) {
  9037. _valueSet[i] = YES;
  9038. _values[i] = otherDictionary->_values[i];
  9039. }
  9040. }
  9041. if (_autocreator) {
  9042. GPBAutocreatedDictionaryModified(_autocreator, self);
  9043. }
  9044. }
  9045. }
  9046. - (void)setUInt32:(uint32_t)value forKey:(BOOL)key {
  9047. int idx = (key ? 1 : 0);
  9048. _values[idx] = value;
  9049. _valueSet[idx] = YES;
  9050. if (_autocreator) {
  9051. GPBAutocreatedDictionaryModified(_autocreator, self);
  9052. }
  9053. }
  9054. - (void)removeUInt32ForKey:(BOOL)aKey {
  9055. _valueSet[aKey ? 1 : 0] = NO;
  9056. }
  9057. - (void)removeAll {
  9058. _valueSet[0] = NO;
  9059. _valueSet[1] = NO;
  9060. }
  9061. @end
  9062. // clang-format on
  9063. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int32, int32_t)
  9064. // This block of code is generated, do not edit it directly.
  9065. // clang-format off
  9066. #pragma mark - Bool -> Int32
  9067. @implementation GPBBoolInt32Dictionary {
  9068. @package
  9069. int32_t _values[2];
  9070. BOOL _valueSet[2];
  9071. }
  9072. - (instancetype)init {
  9073. return [self initWithInt32s:NULL forKeys:NULL count:0];
  9074. }
  9075. - (instancetype)initWithInt32s:(const int32_t [])values
  9076. forKeys:(const BOOL [])keys
  9077. count:(NSUInteger)count {
  9078. self = [super init];
  9079. if (self) {
  9080. for (NSUInteger i = 0; i < count; ++i) {
  9081. int idx = keys[i] ? 1 : 0;
  9082. _values[idx] = values[i];
  9083. _valueSet[idx] = YES;
  9084. }
  9085. }
  9086. return self;
  9087. }
  9088. - (instancetype)initWithDictionary:(GPBBoolInt32Dictionary *)dictionary {
  9089. self = [self initWithInt32s:NULL forKeys:NULL count:0];
  9090. if (self) {
  9091. if (dictionary) {
  9092. for (int i = 0; i < 2; ++i) {
  9093. if (dictionary->_valueSet[i]) {
  9094. _values[i] = dictionary->_values[i];
  9095. _valueSet[i] = YES;
  9096. }
  9097. }
  9098. }
  9099. }
  9100. return self;
  9101. }
  9102. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9103. #pragma unused(numItems)
  9104. return [self initWithInt32s:NULL forKeys:NULL count:0];
  9105. }
  9106. #if !defined(NS_BLOCK_ASSERTIONS)
  9107. - (void)dealloc {
  9108. NSAssert(!_autocreator,
  9109. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9110. [self class], _autocreator);
  9111. [super dealloc];
  9112. }
  9113. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9114. - (instancetype)copyWithZone:(NSZone *)zone {
  9115. return [[GPBBoolInt32Dictionary allocWithZone:zone] initWithDictionary:self];
  9116. }
  9117. - (BOOL)isEqual:(id)other {
  9118. if (self == other) {
  9119. return YES;
  9120. }
  9121. if (![other isKindOfClass:[GPBBoolInt32Dictionary class]]) {
  9122. return NO;
  9123. }
  9124. GPBBoolInt32Dictionary *otherDictionary = other;
  9125. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  9126. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  9127. return NO;
  9128. }
  9129. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  9130. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  9131. return NO;
  9132. }
  9133. return YES;
  9134. }
  9135. - (NSUInteger)hash {
  9136. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9137. }
  9138. - (NSString *)description {
  9139. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9140. if (_valueSet[0]) {
  9141. [result appendFormat:@"NO: %d", _values[0]];
  9142. }
  9143. if (_valueSet[1]) {
  9144. [result appendFormat:@"YES: %d", _values[1]];
  9145. }
  9146. [result appendString:@" }"];
  9147. return result;
  9148. }
  9149. - (NSUInteger)count {
  9150. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9151. }
  9152. - (BOOL)getInt32:(int32_t *)value forKey:(BOOL)key {
  9153. int idx = (key ? 1 : 0);
  9154. if (_valueSet[idx]) {
  9155. if (value) {
  9156. *value = _values[idx];
  9157. }
  9158. return YES;
  9159. }
  9160. return NO;
  9161. }
  9162. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9163. forGPBGenericValueKey:(GPBGenericValue *)key {
  9164. int idx = (key->valueBool ? 1 : 0);
  9165. _values[idx] = value->valueInt32;
  9166. _valueSet[idx] = YES;
  9167. }
  9168. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  9169. if (_valueSet[0]) {
  9170. block(@"false", [NSString stringWithFormat:@"%d", _values[0]]);
  9171. }
  9172. if (_valueSet[1]) {
  9173. block(@"true", [NSString stringWithFormat:@"%d", _values[1]]);
  9174. }
  9175. }
  9176. - (void)enumerateKeysAndInt32sUsingBlock:
  9177. (void (NS_NOESCAPE ^)(BOOL key, int32_t value, BOOL *stop))block {
  9178. BOOL stop = NO;
  9179. if (_valueSet[0]) {
  9180. block(NO, _values[0], &stop);
  9181. }
  9182. if (!stop && _valueSet[1]) {
  9183. block(YES, _values[1], &stop);
  9184. }
  9185. }
  9186. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9187. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9188. NSUInteger count = 0;
  9189. size_t result = 0;
  9190. for (int i = 0; i < 2; ++i) {
  9191. if (_valueSet[i]) {
  9192. ++count;
  9193. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9194. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9195. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9196. }
  9197. }
  9198. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9199. result += tagSize * count;
  9200. return result;
  9201. }
  9202. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9203. asField:(GPBFieldDescriptor *)field {
  9204. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9205. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9206. for (int i = 0; i < 2; ++i) {
  9207. if (_valueSet[i]) {
  9208. // Write the tag.
  9209. [outputStream writeInt32NoTag:tag];
  9210. // Write the size of the message.
  9211. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9212. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9213. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9214. // Write the fields.
  9215. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9216. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9217. }
  9218. }
  9219. }
  9220. - (void)addEntriesFromDictionary:(GPBBoolInt32Dictionary *)otherDictionary {
  9221. if (otherDictionary) {
  9222. for (int i = 0; i < 2; ++i) {
  9223. if (otherDictionary->_valueSet[i]) {
  9224. _valueSet[i] = YES;
  9225. _values[i] = otherDictionary->_values[i];
  9226. }
  9227. }
  9228. if (_autocreator) {
  9229. GPBAutocreatedDictionaryModified(_autocreator, self);
  9230. }
  9231. }
  9232. }
  9233. - (void)setInt32:(int32_t)value forKey:(BOOL)key {
  9234. int idx = (key ? 1 : 0);
  9235. _values[idx] = value;
  9236. _valueSet[idx] = YES;
  9237. if (_autocreator) {
  9238. GPBAutocreatedDictionaryModified(_autocreator, self);
  9239. }
  9240. }
  9241. - (void)removeInt32ForKey:(BOOL)aKey {
  9242. _valueSet[aKey ? 1 : 0] = NO;
  9243. }
  9244. - (void)removeAll {
  9245. _valueSet[0] = NO;
  9246. _valueSet[1] = NO;
  9247. }
  9248. @end
  9249. // clang-format on
  9250. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(UInt64, uint64_t)
  9251. // This block of code is generated, do not edit it directly.
  9252. // clang-format off
  9253. #pragma mark - Bool -> UInt64
  9254. @implementation GPBBoolUInt64Dictionary {
  9255. @package
  9256. uint64_t _values[2];
  9257. BOOL _valueSet[2];
  9258. }
  9259. - (instancetype)init {
  9260. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  9261. }
  9262. - (instancetype)initWithUInt64s:(const uint64_t [])values
  9263. forKeys:(const BOOL [])keys
  9264. count:(NSUInteger)count {
  9265. self = [super init];
  9266. if (self) {
  9267. for (NSUInteger i = 0; i < count; ++i) {
  9268. int idx = keys[i] ? 1 : 0;
  9269. _values[idx] = values[i];
  9270. _valueSet[idx] = YES;
  9271. }
  9272. }
  9273. return self;
  9274. }
  9275. - (instancetype)initWithDictionary:(GPBBoolUInt64Dictionary *)dictionary {
  9276. self = [self initWithUInt64s:NULL forKeys:NULL count:0];
  9277. if (self) {
  9278. if (dictionary) {
  9279. for (int i = 0; i < 2; ++i) {
  9280. if (dictionary->_valueSet[i]) {
  9281. _values[i] = dictionary->_values[i];
  9282. _valueSet[i] = YES;
  9283. }
  9284. }
  9285. }
  9286. }
  9287. return self;
  9288. }
  9289. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9290. #pragma unused(numItems)
  9291. return [self initWithUInt64s:NULL forKeys:NULL count:0];
  9292. }
  9293. #if !defined(NS_BLOCK_ASSERTIONS)
  9294. - (void)dealloc {
  9295. NSAssert(!_autocreator,
  9296. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9297. [self class], _autocreator);
  9298. [super dealloc];
  9299. }
  9300. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9301. - (instancetype)copyWithZone:(NSZone *)zone {
  9302. return [[GPBBoolUInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  9303. }
  9304. - (BOOL)isEqual:(id)other {
  9305. if (self == other) {
  9306. return YES;
  9307. }
  9308. if (![other isKindOfClass:[GPBBoolUInt64Dictionary class]]) {
  9309. return NO;
  9310. }
  9311. GPBBoolUInt64Dictionary *otherDictionary = other;
  9312. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  9313. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  9314. return NO;
  9315. }
  9316. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  9317. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  9318. return NO;
  9319. }
  9320. return YES;
  9321. }
  9322. - (NSUInteger)hash {
  9323. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9324. }
  9325. - (NSString *)description {
  9326. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9327. if (_valueSet[0]) {
  9328. [result appendFormat:@"NO: %llu", _values[0]];
  9329. }
  9330. if (_valueSet[1]) {
  9331. [result appendFormat:@"YES: %llu", _values[1]];
  9332. }
  9333. [result appendString:@" }"];
  9334. return result;
  9335. }
  9336. - (NSUInteger)count {
  9337. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9338. }
  9339. - (BOOL)getUInt64:(uint64_t *)value forKey:(BOOL)key {
  9340. int idx = (key ? 1 : 0);
  9341. if (_valueSet[idx]) {
  9342. if (value) {
  9343. *value = _values[idx];
  9344. }
  9345. return YES;
  9346. }
  9347. return NO;
  9348. }
  9349. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9350. forGPBGenericValueKey:(GPBGenericValue *)key {
  9351. int idx = (key->valueBool ? 1 : 0);
  9352. _values[idx] = value->valueUInt64;
  9353. _valueSet[idx] = YES;
  9354. }
  9355. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  9356. if (_valueSet[0]) {
  9357. block(@"false", [NSString stringWithFormat:@"%llu", _values[0]]);
  9358. }
  9359. if (_valueSet[1]) {
  9360. block(@"true", [NSString stringWithFormat:@"%llu", _values[1]]);
  9361. }
  9362. }
  9363. - (void)enumerateKeysAndUInt64sUsingBlock:
  9364. (void (NS_NOESCAPE ^)(BOOL key, uint64_t value, BOOL *stop))block {
  9365. BOOL stop = NO;
  9366. if (_valueSet[0]) {
  9367. block(NO, _values[0], &stop);
  9368. }
  9369. if (!stop && _valueSet[1]) {
  9370. block(YES, _values[1], &stop);
  9371. }
  9372. }
  9373. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9374. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9375. NSUInteger count = 0;
  9376. size_t result = 0;
  9377. for (int i = 0; i < 2; ++i) {
  9378. if (_valueSet[i]) {
  9379. ++count;
  9380. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9381. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9382. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9383. }
  9384. }
  9385. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9386. result += tagSize * count;
  9387. return result;
  9388. }
  9389. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9390. asField:(GPBFieldDescriptor *)field {
  9391. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9392. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9393. for (int i = 0; i < 2; ++i) {
  9394. if (_valueSet[i]) {
  9395. // Write the tag.
  9396. [outputStream writeInt32NoTag:tag];
  9397. // Write the size of the message.
  9398. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9399. msgSize += ComputeDictUInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9400. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9401. // Write the fields.
  9402. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9403. WriteDictUInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9404. }
  9405. }
  9406. }
  9407. - (void)addEntriesFromDictionary:(GPBBoolUInt64Dictionary *)otherDictionary {
  9408. if (otherDictionary) {
  9409. for (int i = 0; i < 2; ++i) {
  9410. if (otherDictionary->_valueSet[i]) {
  9411. _valueSet[i] = YES;
  9412. _values[i] = otherDictionary->_values[i];
  9413. }
  9414. }
  9415. if (_autocreator) {
  9416. GPBAutocreatedDictionaryModified(_autocreator, self);
  9417. }
  9418. }
  9419. }
  9420. - (void)setUInt64:(uint64_t)value forKey:(BOOL)key {
  9421. int idx = (key ? 1 : 0);
  9422. _values[idx] = value;
  9423. _valueSet[idx] = YES;
  9424. if (_autocreator) {
  9425. GPBAutocreatedDictionaryModified(_autocreator, self);
  9426. }
  9427. }
  9428. - (void)removeUInt64ForKey:(BOOL)aKey {
  9429. _valueSet[aKey ? 1 : 0] = NO;
  9430. }
  9431. - (void)removeAll {
  9432. _valueSet[0] = NO;
  9433. _valueSet[1] = NO;
  9434. }
  9435. @end
  9436. // clang-format on
  9437. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Int64, int64_t)
  9438. // This block of code is generated, do not edit it directly.
  9439. // clang-format off
  9440. #pragma mark - Bool -> Int64
  9441. @implementation GPBBoolInt64Dictionary {
  9442. @package
  9443. int64_t _values[2];
  9444. BOOL _valueSet[2];
  9445. }
  9446. - (instancetype)init {
  9447. return [self initWithInt64s:NULL forKeys:NULL count:0];
  9448. }
  9449. - (instancetype)initWithInt64s:(const int64_t [])values
  9450. forKeys:(const BOOL [])keys
  9451. count:(NSUInteger)count {
  9452. self = [super init];
  9453. if (self) {
  9454. for (NSUInteger i = 0; i < count; ++i) {
  9455. int idx = keys[i] ? 1 : 0;
  9456. _values[idx] = values[i];
  9457. _valueSet[idx] = YES;
  9458. }
  9459. }
  9460. return self;
  9461. }
  9462. - (instancetype)initWithDictionary:(GPBBoolInt64Dictionary *)dictionary {
  9463. self = [self initWithInt64s:NULL forKeys:NULL count:0];
  9464. if (self) {
  9465. if (dictionary) {
  9466. for (int i = 0; i < 2; ++i) {
  9467. if (dictionary->_valueSet[i]) {
  9468. _values[i] = dictionary->_values[i];
  9469. _valueSet[i] = YES;
  9470. }
  9471. }
  9472. }
  9473. }
  9474. return self;
  9475. }
  9476. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9477. #pragma unused(numItems)
  9478. return [self initWithInt64s:NULL forKeys:NULL count:0];
  9479. }
  9480. #if !defined(NS_BLOCK_ASSERTIONS)
  9481. - (void)dealloc {
  9482. NSAssert(!_autocreator,
  9483. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9484. [self class], _autocreator);
  9485. [super dealloc];
  9486. }
  9487. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9488. - (instancetype)copyWithZone:(NSZone *)zone {
  9489. return [[GPBBoolInt64Dictionary allocWithZone:zone] initWithDictionary:self];
  9490. }
  9491. - (BOOL)isEqual:(id)other {
  9492. if (self == other) {
  9493. return YES;
  9494. }
  9495. if (![other isKindOfClass:[GPBBoolInt64Dictionary class]]) {
  9496. return NO;
  9497. }
  9498. GPBBoolInt64Dictionary *otherDictionary = other;
  9499. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  9500. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  9501. return NO;
  9502. }
  9503. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  9504. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  9505. return NO;
  9506. }
  9507. return YES;
  9508. }
  9509. - (NSUInteger)hash {
  9510. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9511. }
  9512. - (NSString *)description {
  9513. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9514. if (_valueSet[0]) {
  9515. [result appendFormat:@"NO: %lld", _values[0]];
  9516. }
  9517. if (_valueSet[1]) {
  9518. [result appendFormat:@"YES: %lld", _values[1]];
  9519. }
  9520. [result appendString:@" }"];
  9521. return result;
  9522. }
  9523. - (NSUInteger)count {
  9524. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9525. }
  9526. - (BOOL)getInt64:(int64_t *)value forKey:(BOOL)key {
  9527. int idx = (key ? 1 : 0);
  9528. if (_valueSet[idx]) {
  9529. if (value) {
  9530. *value = _values[idx];
  9531. }
  9532. return YES;
  9533. }
  9534. return NO;
  9535. }
  9536. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9537. forGPBGenericValueKey:(GPBGenericValue *)key {
  9538. int idx = (key->valueBool ? 1 : 0);
  9539. _values[idx] = value->valueInt64;
  9540. _valueSet[idx] = YES;
  9541. }
  9542. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  9543. if (_valueSet[0]) {
  9544. block(@"false", [NSString stringWithFormat:@"%lld", _values[0]]);
  9545. }
  9546. if (_valueSet[1]) {
  9547. block(@"true", [NSString stringWithFormat:@"%lld", _values[1]]);
  9548. }
  9549. }
  9550. - (void)enumerateKeysAndInt64sUsingBlock:
  9551. (void (NS_NOESCAPE ^)(BOOL key, int64_t value, BOOL *stop))block {
  9552. BOOL stop = NO;
  9553. if (_valueSet[0]) {
  9554. block(NO, _values[0], &stop);
  9555. }
  9556. if (!stop && _valueSet[1]) {
  9557. block(YES, _values[1], &stop);
  9558. }
  9559. }
  9560. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9561. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9562. NSUInteger count = 0;
  9563. size_t result = 0;
  9564. for (int i = 0; i < 2; ++i) {
  9565. if (_valueSet[i]) {
  9566. ++count;
  9567. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9568. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9569. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9570. }
  9571. }
  9572. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9573. result += tagSize * count;
  9574. return result;
  9575. }
  9576. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9577. asField:(GPBFieldDescriptor *)field {
  9578. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9579. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9580. for (int i = 0; i < 2; ++i) {
  9581. if (_valueSet[i]) {
  9582. // Write the tag.
  9583. [outputStream writeInt32NoTag:tag];
  9584. // Write the size of the message.
  9585. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9586. msgSize += ComputeDictInt64FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9587. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9588. // Write the fields.
  9589. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9590. WriteDictInt64Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9591. }
  9592. }
  9593. }
  9594. - (void)addEntriesFromDictionary:(GPBBoolInt64Dictionary *)otherDictionary {
  9595. if (otherDictionary) {
  9596. for (int i = 0; i < 2; ++i) {
  9597. if (otherDictionary->_valueSet[i]) {
  9598. _valueSet[i] = YES;
  9599. _values[i] = otherDictionary->_values[i];
  9600. }
  9601. }
  9602. if (_autocreator) {
  9603. GPBAutocreatedDictionaryModified(_autocreator, self);
  9604. }
  9605. }
  9606. }
  9607. - (void)setInt64:(int64_t)value forKey:(BOOL)key {
  9608. int idx = (key ? 1 : 0);
  9609. _values[idx] = value;
  9610. _valueSet[idx] = YES;
  9611. if (_autocreator) {
  9612. GPBAutocreatedDictionaryModified(_autocreator, self);
  9613. }
  9614. }
  9615. - (void)removeInt64ForKey:(BOOL)aKey {
  9616. _valueSet[aKey ? 1 : 0] = NO;
  9617. }
  9618. - (void)removeAll {
  9619. _valueSet[0] = NO;
  9620. _valueSet[1] = NO;
  9621. }
  9622. @end
  9623. // clang-format on
  9624. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Bool, BOOL)
  9625. // This block of code is generated, do not edit it directly.
  9626. // clang-format off
  9627. #pragma mark - Bool -> Bool
  9628. @implementation GPBBoolBoolDictionary {
  9629. @package
  9630. BOOL _values[2];
  9631. BOOL _valueSet[2];
  9632. }
  9633. - (instancetype)init {
  9634. return [self initWithBools:NULL forKeys:NULL count:0];
  9635. }
  9636. - (instancetype)initWithBools:(const BOOL [])values
  9637. forKeys:(const BOOL [])keys
  9638. count:(NSUInteger)count {
  9639. self = [super init];
  9640. if (self) {
  9641. for (NSUInteger i = 0; i < count; ++i) {
  9642. int idx = keys[i] ? 1 : 0;
  9643. _values[idx] = values[i];
  9644. _valueSet[idx] = YES;
  9645. }
  9646. }
  9647. return self;
  9648. }
  9649. - (instancetype)initWithDictionary:(GPBBoolBoolDictionary *)dictionary {
  9650. self = [self initWithBools:NULL forKeys:NULL count:0];
  9651. if (self) {
  9652. if (dictionary) {
  9653. for (int i = 0; i < 2; ++i) {
  9654. if (dictionary->_valueSet[i]) {
  9655. _values[i] = dictionary->_values[i];
  9656. _valueSet[i] = YES;
  9657. }
  9658. }
  9659. }
  9660. }
  9661. return self;
  9662. }
  9663. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9664. #pragma unused(numItems)
  9665. return [self initWithBools:NULL forKeys:NULL count:0];
  9666. }
  9667. #if !defined(NS_BLOCK_ASSERTIONS)
  9668. - (void)dealloc {
  9669. NSAssert(!_autocreator,
  9670. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9671. [self class], _autocreator);
  9672. [super dealloc];
  9673. }
  9674. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9675. - (instancetype)copyWithZone:(NSZone *)zone {
  9676. return [[GPBBoolBoolDictionary allocWithZone:zone] initWithDictionary:self];
  9677. }
  9678. - (BOOL)isEqual:(id)other {
  9679. if (self == other) {
  9680. return YES;
  9681. }
  9682. if (![other isKindOfClass:[GPBBoolBoolDictionary class]]) {
  9683. return NO;
  9684. }
  9685. GPBBoolBoolDictionary *otherDictionary = other;
  9686. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  9687. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  9688. return NO;
  9689. }
  9690. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  9691. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  9692. return NO;
  9693. }
  9694. return YES;
  9695. }
  9696. - (NSUInteger)hash {
  9697. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9698. }
  9699. - (NSString *)description {
  9700. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9701. if (_valueSet[0]) {
  9702. [result appendFormat:@"NO: %d", _values[0]];
  9703. }
  9704. if (_valueSet[1]) {
  9705. [result appendFormat:@"YES: %d", _values[1]];
  9706. }
  9707. [result appendString:@" }"];
  9708. return result;
  9709. }
  9710. - (NSUInteger)count {
  9711. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9712. }
  9713. - (BOOL)getBool:(BOOL *)value forKey:(BOOL)key {
  9714. int idx = (key ? 1 : 0);
  9715. if (_valueSet[idx]) {
  9716. if (value) {
  9717. *value = _values[idx];
  9718. }
  9719. return YES;
  9720. }
  9721. return NO;
  9722. }
  9723. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9724. forGPBGenericValueKey:(GPBGenericValue *)key {
  9725. int idx = (key->valueBool ? 1 : 0);
  9726. _values[idx] = value->valueBool;
  9727. _valueSet[idx] = YES;
  9728. }
  9729. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  9730. if (_valueSet[0]) {
  9731. block(@"false", (_values[0] ? @"true" : @"false"));
  9732. }
  9733. if (_valueSet[1]) {
  9734. block(@"true", (_values[1] ? @"true" : @"false"));
  9735. }
  9736. }
  9737. - (void)enumerateKeysAndBoolsUsingBlock:
  9738. (void (NS_NOESCAPE ^)(BOOL key, BOOL value, BOOL *stop))block {
  9739. BOOL stop = NO;
  9740. if (_valueSet[0]) {
  9741. block(NO, _values[0], &stop);
  9742. }
  9743. if (!stop && _valueSet[1]) {
  9744. block(YES, _values[1], &stop);
  9745. }
  9746. }
  9747. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9748. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9749. NSUInteger count = 0;
  9750. size_t result = 0;
  9751. for (int i = 0; i < 2; ++i) {
  9752. if (_valueSet[i]) {
  9753. ++count;
  9754. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9755. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9756. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9757. }
  9758. }
  9759. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9760. result += tagSize * count;
  9761. return result;
  9762. }
  9763. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9764. asField:(GPBFieldDescriptor *)field {
  9765. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9766. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9767. for (int i = 0; i < 2; ++i) {
  9768. if (_valueSet[i]) {
  9769. // Write the tag.
  9770. [outputStream writeInt32NoTag:tag];
  9771. // Write the size of the message.
  9772. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9773. msgSize += ComputeDictBoolFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9774. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9775. // Write the fields.
  9776. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9777. WriteDictBoolField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9778. }
  9779. }
  9780. }
  9781. - (void)addEntriesFromDictionary:(GPBBoolBoolDictionary *)otherDictionary {
  9782. if (otherDictionary) {
  9783. for (int i = 0; i < 2; ++i) {
  9784. if (otherDictionary->_valueSet[i]) {
  9785. _valueSet[i] = YES;
  9786. _values[i] = otherDictionary->_values[i];
  9787. }
  9788. }
  9789. if (_autocreator) {
  9790. GPBAutocreatedDictionaryModified(_autocreator, self);
  9791. }
  9792. }
  9793. }
  9794. - (void)setBool:(BOOL)value forKey:(BOOL)key {
  9795. int idx = (key ? 1 : 0);
  9796. _values[idx] = value;
  9797. _valueSet[idx] = YES;
  9798. if (_autocreator) {
  9799. GPBAutocreatedDictionaryModified(_autocreator, self);
  9800. }
  9801. }
  9802. - (void)removeBoolForKey:(BOOL)aKey {
  9803. _valueSet[aKey ? 1 : 0] = NO;
  9804. }
  9805. - (void)removeAll {
  9806. _valueSet[0] = NO;
  9807. _valueSet[1] = NO;
  9808. }
  9809. @end
  9810. // clang-format on
  9811. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Float, float)
  9812. // This block of code is generated, do not edit it directly.
  9813. // clang-format off
  9814. #pragma mark - Bool -> Float
  9815. @implementation GPBBoolFloatDictionary {
  9816. @package
  9817. float _values[2];
  9818. BOOL _valueSet[2];
  9819. }
  9820. - (instancetype)init {
  9821. return [self initWithFloats:NULL forKeys:NULL count:0];
  9822. }
  9823. - (instancetype)initWithFloats:(const float [])values
  9824. forKeys:(const BOOL [])keys
  9825. count:(NSUInteger)count {
  9826. self = [super init];
  9827. if (self) {
  9828. for (NSUInteger i = 0; i < count; ++i) {
  9829. int idx = keys[i] ? 1 : 0;
  9830. _values[idx] = values[i];
  9831. _valueSet[idx] = YES;
  9832. }
  9833. }
  9834. return self;
  9835. }
  9836. - (instancetype)initWithDictionary:(GPBBoolFloatDictionary *)dictionary {
  9837. self = [self initWithFloats:NULL forKeys:NULL count:0];
  9838. if (self) {
  9839. if (dictionary) {
  9840. for (int i = 0; i < 2; ++i) {
  9841. if (dictionary->_valueSet[i]) {
  9842. _values[i] = dictionary->_values[i];
  9843. _valueSet[i] = YES;
  9844. }
  9845. }
  9846. }
  9847. }
  9848. return self;
  9849. }
  9850. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  9851. #pragma unused(numItems)
  9852. return [self initWithFloats:NULL forKeys:NULL count:0];
  9853. }
  9854. #if !defined(NS_BLOCK_ASSERTIONS)
  9855. - (void)dealloc {
  9856. NSAssert(!_autocreator,
  9857. @"%@: Autocreator must be cleared before release, autocreator: %@",
  9858. [self class], _autocreator);
  9859. [super dealloc];
  9860. }
  9861. #endif // !defined(NS_BLOCK_ASSERTIONS)
  9862. - (instancetype)copyWithZone:(NSZone *)zone {
  9863. return [[GPBBoolFloatDictionary allocWithZone:zone] initWithDictionary:self];
  9864. }
  9865. - (BOOL)isEqual:(id)other {
  9866. if (self == other) {
  9867. return YES;
  9868. }
  9869. if (![other isKindOfClass:[GPBBoolFloatDictionary class]]) {
  9870. return NO;
  9871. }
  9872. GPBBoolFloatDictionary *otherDictionary = other;
  9873. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  9874. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  9875. return NO;
  9876. }
  9877. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  9878. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  9879. return NO;
  9880. }
  9881. return YES;
  9882. }
  9883. - (NSUInteger)hash {
  9884. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9885. }
  9886. - (NSString *)description {
  9887. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  9888. if (_valueSet[0]) {
  9889. [result appendFormat:@"NO: %f", _values[0]];
  9890. }
  9891. if (_valueSet[1]) {
  9892. [result appendFormat:@"YES: %f", _values[1]];
  9893. }
  9894. [result appendString:@" }"];
  9895. return result;
  9896. }
  9897. - (NSUInteger)count {
  9898. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  9899. }
  9900. - (BOOL)getFloat:(float *)value forKey:(BOOL)key {
  9901. int idx = (key ? 1 : 0);
  9902. if (_valueSet[idx]) {
  9903. if (value) {
  9904. *value = _values[idx];
  9905. }
  9906. return YES;
  9907. }
  9908. return NO;
  9909. }
  9910. - (void)setGPBGenericValue:(GPBGenericValue *)value
  9911. forGPBGenericValueKey:(GPBGenericValue *)key {
  9912. int idx = (key->valueBool ? 1 : 0);
  9913. _values[idx] = value->valueFloat;
  9914. _valueSet[idx] = YES;
  9915. }
  9916. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  9917. if (_valueSet[0]) {
  9918. block(@"false", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[0]]);
  9919. }
  9920. if (_valueSet[1]) {
  9921. block(@"true", [NSString stringWithFormat:@"%.*g", FLT_DIG, _values[1]]);
  9922. }
  9923. }
  9924. - (void)enumerateKeysAndFloatsUsingBlock:
  9925. (void (NS_NOESCAPE ^)(BOOL key, float value, BOOL *stop))block {
  9926. BOOL stop = NO;
  9927. if (_valueSet[0]) {
  9928. block(NO, _values[0], &stop);
  9929. }
  9930. if (!stop && _valueSet[1]) {
  9931. block(YES, _values[1], &stop);
  9932. }
  9933. }
  9934. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  9935. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9936. NSUInteger count = 0;
  9937. size_t result = 0;
  9938. for (int i = 0; i < 2; ++i) {
  9939. if (_valueSet[i]) {
  9940. ++count;
  9941. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9942. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9943. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  9944. }
  9945. }
  9946. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  9947. result += tagSize * count;
  9948. return result;
  9949. }
  9950. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  9951. asField:(GPBFieldDescriptor *)field {
  9952. GPBDataType valueDataType = GPBGetFieldDataType(field);
  9953. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  9954. for (int i = 0; i < 2; ++i) {
  9955. if (_valueSet[i]) {
  9956. // Write the tag.
  9957. [outputStream writeInt32NoTag:tag];
  9958. // Write the size of the message.
  9959. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9960. msgSize += ComputeDictFloatFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  9961. [outputStream writeInt32NoTag:(int32_t)msgSize];
  9962. // Write the fields.
  9963. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  9964. WriteDictFloatField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  9965. }
  9966. }
  9967. }
  9968. - (void)addEntriesFromDictionary:(GPBBoolFloatDictionary *)otherDictionary {
  9969. if (otherDictionary) {
  9970. for (int i = 0; i < 2; ++i) {
  9971. if (otherDictionary->_valueSet[i]) {
  9972. _valueSet[i] = YES;
  9973. _values[i] = otherDictionary->_values[i];
  9974. }
  9975. }
  9976. if (_autocreator) {
  9977. GPBAutocreatedDictionaryModified(_autocreator, self);
  9978. }
  9979. }
  9980. }
  9981. - (void)setFloat:(float)value forKey:(BOOL)key {
  9982. int idx = (key ? 1 : 0);
  9983. _values[idx] = value;
  9984. _valueSet[idx] = YES;
  9985. if (_autocreator) {
  9986. GPBAutocreatedDictionaryModified(_autocreator, self);
  9987. }
  9988. }
  9989. - (void)removeFloatForKey:(BOOL)aKey {
  9990. _valueSet[aKey ? 1 : 0] = NO;
  9991. }
  9992. - (void)removeAll {
  9993. _valueSet[0] = NO;
  9994. _valueSet[1] = NO;
  9995. }
  9996. @end
  9997. // clang-format on
  9998. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_POD_IMPL(Double, double)
  9999. // This block of code is generated, do not edit it directly.
  10000. // clang-format off
  10001. #pragma mark - Bool -> Double
  10002. @implementation GPBBoolDoubleDictionary {
  10003. @package
  10004. double _values[2];
  10005. BOOL _valueSet[2];
  10006. }
  10007. - (instancetype)init {
  10008. return [self initWithDoubles:NULL forKeys:NULL count:0];
  10009. }
  10010. - (instancetype)initWithDoubles:(const double [])values
  10011. forKeys:(const BOOL [])keys
  10012. count:(NSUInteger)count {
  10013. self = [super init];
  10014. if (self) {
  10015. for (NSUInteger i = 0; i < count; ++i) {
  10016. int idx = keys[i] ? 1 : 0;
  10017. _values[idx] = values[i];
  10018. _valueSet[idx] = YES;
  10019. }
  10020. }
  10021. return self;
  10022. }
  10023. - (instancetype)initWithDictionary:(GPBBoolDoubleDictionary *)dictionary {
  10024. self = [self initWithDoubles:NULL forKeys:NULL count:0];
  10025. if (self) {
  10026. if (dictionary) {
  10027. for (int i = 0; i < 2; ++i) {
  10028. if (dictionary->_valueSet[i]) {
  10029. _values[i] = dictionary->_values[i];
  10030. _valueSet[i] = YES;
  10031. }
  10032. }
  10033. }
  10034. }
  10035. return self;
  10036. }
  10037. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10038. #pragma unused(numItems)
  10039. return [self initWithDoubles:NULL forKeys:NULL count:0];
  10040. }
  10041. #if !defined(NS_BLOCK_ASSERTIONS)
  10042. - (void)dealloc {
  10043. NSAssert(!_autocreator,
  10044. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10045. [self class], _autocreator);
  10046. [super dealloc];
  10047. }
  10048. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10049. - (instancetype)copyWithZone:(NSZone *)zone {
  10050. return [[GPBBoolDoubleDictionary allocWithZone:zone] initWithDictionary:self];
  10051. }
  10052. - (BOOL)isEqual:(id)other {
  10053. if (self == other) {
  10054. return YES;
  10055. }
  10056. if (![other isKindOfClass:[GPBBoolDoubleDictionary class]]) {
  10057. return NO;
  10058. }
  10059. GPBBoolDoubleDictionary *otherDictionary = other;
  10060. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10061. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10062. return NO;
  10063. }
  10064. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10065. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10066. return NO;
  10067. }
  10068. return YES;
  10069. }
  10070. - (NSUInteger)hash {
  10071. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10072. }
  10073. - (NSString *)description {
  10074. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10075. if (_valueSet[0]) {
  10076. [result appendFormat:@"NO: %lf", _values[0]];
  10077. }
  10078. if (_valueSet[1]) {
  10079. [result appendFormat:@"YES: %lf", _values[1]];
  10080. }
  10081. [result appendString:@" }"];
  10082. return result;
  10083. }
  10084. - (NSUInteger)count {
  10085. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10086. }
  10087. - (BOOL)getDouble:(double *)value forKey:(BOOL)key {
  10088. int idx = (key ? 1 : 0);
  10089. if (_valueSet[idx]) {
  10090. if (value) {
  10091. *value = _values[idx];
  10092. }
  10093. return YES;
  10094. }
  10095. return NO;
  10096. }
  10097. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10098. forGPBGenericValueKey:(GPBGenericValue *)key {
  10099. int idx = (key->valueBool ? 1 : 0);
  10100. _values[idx] = value->valueDouble;
  10101. _valueSet[idx] = YES;
  10102. }
  10103. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  10104. if (_valueSet[0]) {
  10105. block(@"false", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[0]]);
  10106. }
  10107. if (_valueSet[1]) {
  10108. block(@"true", [NSString stringWithFormat:@"%.*lg", DBL_DIG, _values[1]]);
  10109. }
  10110. }
  10111. - (void)enumerateKeysAndDoublesUsingBlock:
  10112. (void (NS_NOESCAPE ^)(BOOL key, double value, BOOL *stop))block {
  10113. BOOL stop = NO;
  10114. if (_valueSet[0]) {
  10115. block(NO, _values[0], &stop);
  10116. }
  10117. if (!stop && _valueSet[1]) {
  10118. block(YES, _values[1], &stop);
  10119. }
  10120. }
  10121. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10122. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10123. NSUInteger count = 0;
  10124. size_t result = 0;
  10125. for (int i = 0; i < 2; ++i) {
  10126. if (_valueSet[i]) {
  10127. ++count;
  10128. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10129. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10130. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10131. }
  10132. }
  10133. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10134. result += tagSize * count;
  10135. return result;
  10136. }
  10137. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10138. asField:(GPBFieldDescriptor *)field {
  10139. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10140. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10141. for (int i = 0; i < 2; ++i) {
  10142. if (_valueSet[i]) {
  10143. // Write the tag.
  10144. [outputStream writeInt32NoTag:tag];
  10145. // Write the size of the message.
  10146. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10147. msgSize += ComputeDictDoubleFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10148. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10149. // Write the fields.
  10150. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10151. WriteDictDoubleField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10152. }
  10153. }
  10154. }
  10155. - (void)addEntriesFromDictionary:(GPBBoolDoubleDictionary *)otherDictionary {
  10156. if (otherDictionary) {
  10157. for (int i = 0; i < 2; ++i) {
  10158. if (otherDictionary->_valueSet[i]) {
  10159. _valueSet[i] = YES;
  10160. _values[i] = otherDictionary->_values[i];
  10161. }
  10162. }
  10163. if (_autocreator) {
  10164. GPBAutocreatedDictionaryModified(_autocreator, self);
  10165. }
  10166. }
  10167. }
  10168. - (void)setDouble:(double)value forKey:(BOOL)key {
  10169. int idx = (key ? 1 : 0);
  10170. _values[idx] = value;
  10171. _valueSet[idx] = YES;
  10172. if (_autocreator) {
  10173. GPBAutocreatedDictionaryModified(_autocreator, self);
  10174. }
  10175. }
  10176. - (void)removeDoubleForKey:(BOOL)aKey {
  10177. _valueSet[aKey ? 1 : 0] = NO;
  10178. }
  10179. - (void)removeAll {
  10180. _valueSet[0] = NO;
  10181. _valueSet[1] = NO;
  10182. }
  10183. @end
  10184. // clang-format on
  10185. //%PDDM-EXPAND DICTIONARY_BOOL_KEY_TO_OBJECT_IMPL(Object, id)
  10186. // This block of code is generated, do not edit it directly.
  10187. // clang-format off
  10188. #pragma mark - Bool -> Object
  10189. @implementation GPBBoolObjectDictionary {
  10190. @package
  10191. id _values[2];
  10192. }
  10193. - (instancetype)init {
  10194. return [self initWithObjects:NULL forKeys:NULL count:0];
  10195. }
  10196. - (instancetype)initWithObjects:(const id [])objects
  10197. forKeys:(const BOOL [])keys
  10198. count:(NSUInteger)count {
  10199. self = [super init];
  10200. if (self) {
  10201. for (NSUInteger i = 0; i < count; ++i) {
  10202. if (!objects[i]) {
  10203. [NSException raise:NSInvalidArgumentException
  10204. format:@"Attempting to add nil object to a Dictionary"];
  10205. }
  10206. int idx = keys[i] ? 1 : 0;
  10207. [_values[idx] release];
  10208. _values[idx] = (id)[objects[i] retain];
  10209. }
  10210. }
  10211. return self;
  10212. }
  10213. - (instancetype)initWithDictionary:(GPBBoolObjectDictionary *)dictionary {
  10214. self = [self initWithObjects:NULL forKeys:NULL count:0];
  10215. if (self) {
  10216. if (dictionary) {
  10217. _values[0] = [dictionary->_values[0] retain];
  10218. _values[1] = [dictionary->_values[1] retain];
  10219. }
  10220. }
  10221. return self;
  10222. }
  10223. - (instancetype)initWithCapacity:(NSUInteger)numItems {
  10224. #pragma unused(numItems)
  10225. return [self initWithObjects:NULL forKeys:NULL count:0];
  10226. }
  10227. - (void)dealloc {
  10228. NSAssert(!_autocreator,
  10229. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10230. [self class], _autocreator);
  10231. [_values[0] release];
  10232. [_values[1] release];
  10233. [super dealloc];
  10234. }
  10235. - (instancetype)copyWithZone:(NSZone *)zone {
  10236. return [[GPBBoolObjectDictionary allocWithZone:zone] initWithDictionary:self];
  10237. }
  10238. - (BOOL)isEqual:(id)other {
  10239. if (self == other) {
  10240. return YES;
  10241. }
  10242. if (![other isKindOfClass:[GPBBoolObjectDictionary class]]) {
  10243. return NO;
  10244. }
  10245. GPBBoolObjectDictionary *otherDictionary = other;
  10246. if (((_values[0] != nil) != (otherDictionary->_values[0] != nil)) ||
  10247. ((_values[1] != nil) != (otherDictionary->_values[1] != nil))) {
  10248. return NO;
  10249. }
  10250. if (((_values[0] != nil) && (![_values[0] isEqual:otherDictionary->_values[0]])) ||
  10251. ((_values[1] != nil) && (![_values[1] isEqual:otherDictionary->_values[1]]))) {
  10252. return NO;
  10253. }
  10254. return YES;
  10255. }
  10256. - (NSUInteger)hash {
  10257. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  10258. }
  10259. - (NSString *)description {
  10260. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10261. if ((_values[0] != nil)) {
  10262. [result appendFormat:@"NO: %@", _values[0]];
  10263. }
  10264. if ((_values[1] != nil)) {
  10265. [result appendFormat:@"YES: %@", _values[1]];
  10266. }
  10267. [result appendString:@" }"];
  10268. return result;
  10269. }
  10270. - (NSUInteger)count {
  10271. return ((_values[0] != nil) ? 1 : 0) + ((_values[1] != nil) ? 1 : 0);
  10272. }
  10273. - (id)objectForKey:(BOOL)key {
  10274. return _values[key ? 1 : 0];
  10275. }
  10276. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10277. forGPBGenericValueKey:(GPBGenericValue *)key {
  10278. int idx = (key->valueBool ? 1 : 0);
  10279. [_values[idx] release];
  10280. _values[idx] = [value->valueString retain];
  10281. }
  10282. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  10283. if (_values[0] != nil) {
  10284. block(@"false", _values[0]);
  10285. }
  10286. if ((_values[1] != nil)) {
  10287. block(@"true", _values[1]);
  10288. }
  10289. }
  10290. - (void)enumerateKeysAndObjectsUsingBlock:
  10291. (void (NS_NOESCAPE ^)(BOOL key, id object, BOOL *stop))block {
  10292. BOOL stop = NO;
  10293. if (_values[0] != nil) {
  10294. block(NO, _values[0], &stop);
  10295. }
  10296. if (!stop && (_values[1] != nil)) {
  10297. block(YES, _values[1], &stop);
  10298. }
  10299. }
  10300. - (BOOL)isInitialized {
  10301. if (_values[0] && ![_values[0] isInitialized]) {
  10302. return NO;
  10303. }
  10304. if (_values[1] && ![_values[1] isInitialized]) {
  10305. return NO;
  10306. }
  10307. return YES;
  10308. }
  10309. - (instancetype)deepCopyWithZone:(NSZone *)zone {
  10310. GPBBoolObjectDictionary *newDict =
  10311. [[GPBBoolObjectDictionary alloc] init];
  10312. for (int i = 0; i < 2; ++i) {
  10313. if (_values[i] != nil) {
  10314. newDict->_values[i] = [_values[i] copyWithZone:zone];
  10315. }
  10316. }
  10317. return newDict;
  10318. }
  10319. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10320. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10321. NSUInteger count = 0;
  10322. size_t result = 0;
  10323. for (int i = 0; i < 2; ++i) {
  10324. if (_values[i] != nil) {
  10325. ++count;
  10326. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10327. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10328. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10329. }
  10330. }
  10331. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10332. result += tagSize * count;
  10333. return result;
  10334. }
  10335. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10336. asField:(GPBFieldDescriptor *)field {
  10337. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10338. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10339. for (int i = 0; i < 2; ++i) {
  10340. if (_values[i] != nil) {
  10341. // Write the tag.
  10342. [outputStream writeInt32NoTag:tag];
  10343. // Write the size of the message.
  10344. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10345. msgSize += ComputeDictObjectFieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10346. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10347. // Write the fields.
  10348. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10349. WriteDictObjectField(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10350. }
  10351. }
  10352. }
  10353. - (void)addEntriesFromDictionary:(GPBBoolObjectDictionary *)otherDictionary {
  10354. if (otherDictionary) {
  10355. for (int i = 0; i < 2; ++i) {
  10356. if (otherDictionary->_values[i] != nil) {
  10357. [_values[i] release];
  10358. _values[i] = [otherDictionary->_values[i] retain];
  10359. }
  10360. }
  10361. if (_autocreator) {
  10362. GPBAutocreatedDictionaryModified(_autocreator, self);
  10363. }
  10364. }
  10365. }
  10366. - (void)setObject:(id)object forKey:(BOOL)key {
  10367. if (!object) {
  10368. [NSException raise:NSInvalidArgumentException
  10369. format:@"Attempting to add nil object to a Dictionary"];
  10370. }
  10371. int idx = (key ? 1 : 0);
  10372. [_values[idx] release];
  10373. _values[idx] = [object retain];
  10374. if (_autocreator) {
  10375. GPBAutocreatedDictionaryModified(_autocreator, self);
  10376. }
  10377. }
  10378. - (void)removeObjectForKey:(BOOL)aKey {
  10379. int idx = (aKey ? 1 : 0);
  10380. [_values[idx] release];
  10381. _values[idx] = nil;
  10382. }
  10383. - (void)removeAll {
  10384. for (int i = 0; i < 2; ++i) {
  10385. [_values[i] release];
  10386. _values[i] = nil;
  10387. }
  10388. }
  10389. @end
  10390. // clang-format on
  10391. //%PDDM-EXPAND-END (8 expansions)
  10392. #pragma mark - Bool -> Enum
  10393. @implementation GPBBoolEnumDictionary {
  10394. @package
  10395. GPBEnumValidationFunc _validationFunc;
  10396. int32_t _values[2];
  10397. BOOL _valueSet[2];
  10398. }
  10399. @synthesize validationFunc = _validationFunc;
  10400. - (instancetype)init {
  10401. return [self initWithValidationFunction:NULL rawValues:NULL forKeys:NULL count:0];
  10402. }
  10403. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func {
  10404. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  10405. }
  10406. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  10407. rawValues:(const int32_t [])rawValues
  10408. forKeys:(const BOOL [])keys
  10409. count:(NSUInteger)count {
  10410. self = [super init];
  10411. if (self) {
  10412. _validationFunc = (func != NULL ? func : DictDefault_IsValidValue);
  10413. for (NSUInteger i = 0; i < count; ++i) {
  10414. int idx = keys[i] ? 1 : 0;
  10415. _values[idx] = rawValues[i];
  10416. _valueSet[idx] = YES;
  10417. }
  10418. }
  10419. return self;
  10420. }
  10421. - (instancetype)initWithDictionary:(GPBBoolEnumDictionary *)dictionary {
  10422. self = [self initWithValidationFunction:dictionary.validationFunc
  10423. rawValues:NULL
  10424. forKeys:NULL
  10425. count:0];
  10426. if (self) {
  10427. if (dictionary) {
  10428. for (int i = 0; i < 2; ++i) {
  10429. if (dictionary->_valueSet[i]) {
  10430. _values[i] = dictionary->_values[i];
  10431. _valueSet[i] = YES;
  10432. }
  10433. }
  10434. }
  10435. }
  10436. return self;
  10437. }
  10438. - (instancetype)initWithValidationFunction:(GPBEnumValidationFunc)func
  10439. capacity:(NSUInteger)numItems {
  10440. #pragma unused(numItems)
  10441. return [self initWithValidationFunction:func rawValues:NULL forKeys:NULL count:0];
  10442. }
  10443. #if !defined(NS_BLOCK_ASSERTIONS)
  10444. - (void)dealloc {
  10445. NSAssert(!_autocreator,
  10446. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10447. [self class], _autocreator);
  10448. [super dealloc];
  10449. }
  10450. #endif // !defined(NS_BLOCK_ASSERTIONS)
  10451. - (instancetype)copyWithZone:(NSZone *)zone {
  10452. return [[GPBBoolEnumDictionary allocWithZone:zone] initWithDictionary:self];
  10453. }
  10454. - (BOOL)isEqual:(id)other {
  10455. if (self == other) {
  10456. return YES;
  10457. }
  10458. if (![other isKindOfClass:[GPBBoolEnumDictionary class]]) {
  10459. return NO;
  10460. }
  10461. GPBBoolEnumDictionary *otherDictionary = other;
  10462. if ((_valueSet[0] != otherDictionary->_valueSet[0]) ||
  10463. (_valueSet[1] != otherDictionary->_valueSet[1])) {
  10464. return NO;
  10465. }
  10466. if ((_valueSet[0] && (_values[0] != otherDictionary->_values[0])) ||
  10467. (_valueSet[1] && (_values[1] != otherDictionary->_values[1]))) {
  10468. return NO;
  10469. }
  10470. return YES;
  10471. }
  10472. - (NSUInteger)hash {
  10473. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10474. }
  10475. - (NSString *)description {
  10476. NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p> {", [self class], self];
  10477. if (_valueSet[0]) {
  10478. [result appendFormat:@"NO: %d", _values[0]];
  10479. }
  10480. if (_valueSet[1]) {
  10481. [result appendFormat:@"YES: %d", _values[1]];
  10482. }
  10483. [result appendString:@" }"];
  10484. return result;
  10485. }
  10486. - (NSUInteger)count {
  10487. return (_valueSet[0] ? 1 : 0) + (_valueSet[1] ? 1 : 0);
  10488. }
  10489. - (BOOL)getEnum:(int32_t*)value forKey:(BOOL)key {
  10490. int idx = (key ? 1 : 0);
  10491. if (_valueSet[idx]) {
  10492. if (value) {
  10493. int32_t result = _values[idx];
  10494. if (!_validationFunc(result)) {
  10495. result = kGPBUnrecognizedEnumeratorValue;
  10496. }
  10497. *value = result;
  10498. }
  10499. return YES;
  10500. }
  10501. return NO;
  10502. }
  10503. - (BOOL)getRawValue:(int32_t*)rawValue forKey:(BOOL)key {
  10504. int idx = (key ? 1 : 0);
  10505. if (_valueSet[idx]) {
  10506. if (rawValue) {
  10507. *rawValue = _values[idx];
  10508. }
  10509. return YES;
  10510. }
  10511. return NO;
  10512. }
  10513. - (void)enumerateKeysAndRawValuesUsingBlock:
  10514. (void (NS_NOESCAPE ^)(BOOL key, int32_t value, BOOL *stop))block {
  10515. BOOL stop = NO;
  10516. if (_valueSet[0]) {
  10517. block(NO, _values[0], &stop);
  10518. }
  10519. if (!stop && _valueSet[1]) {
  10520. block(YES, _values[1], &stop);
  10521. }
  10522. }
  10523. - (void)enumerateKeysAndEnumsUsingBlock:
  10524. (void (NS_NOESCAPE ^)(BOOL key, int32_t rawValue, BOOL *stop))block {
  10525. BOOL stop = NO;
  10526. GPBEnumValidationFunc func = _validationFunc;
  10527. int32_t validatedValue;
  10528. if (_valueSet[0]) {
  10529. validatedValue = _values[0];
  10530. if (!func(validatedValue)) {
  10531. validatedValue = kGPBUnrecognizedEnumeratorValue;
  10532. }
  10533. block(NO, validatedValue, &stop);
  10534. }
  10535. if (!stop && _valueSet[1]) {
  10536. validatedValue = _values[1];
  10537. if (!func(validatedValue)) {
  10538. validatedValue = kGPBUnrecognizedEnumeratorValue;
  10539. }
  10540. block(YES, validatedValue, &stop);
  10541. }
  10542. }
  10543. //%PDDM-EXPAND SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  10544. // This block of code is generated, do not edit it directly.
  10545. // clang-format off
  10546. - (NSData *)serializedDataForUnknownValue:(int32_t)value
  10547. forKey:(GPBGenericValue *)key
  10548. keyDataType:(GPBDataType)keyDataType {
  10549. size_t msgSize = ComputeDictBoolFieldSize(key->valueBool, kMapKeyFieldNumber, keyDataType);
  10550. msgSize += ComputeDictEnumFieldSize(value, kMapValueFieldNumber, GPBDataTypeEnum);
  10551. NSMutableData *data = [NSMutableData dataWithLength:msgSize];
  10552. GPBCodedOutputStream *outputStream = [[GPBCodedOutputStream alloc] initWithData:data];
  10553. WriteDictBoolField(outputStream, key->valueBool, kMapKeyFieldNumber, keyDataType);
  10554. WriteDictEnumField(outputStream, value, kMapValueFieldNumber, GPBDataTypeEnum);
  10555. [outputStream release];
  10556. return data;
  10557. }
  10558. // clang-format on
  10559. //%PDDM-EXPAND-END SERIAL_DATA_FOR_ENTRY_POD_Enum(Bool)
  10560. - (size_t)computeSerializedSizeAsField:(GPBFieldDescriptor *)field {
  10561. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10562. NSUInteger count = 0;
  10563. size_t result = 0;
  10564. for (int i = 0; i < 2; ++i) {
  10565. if (_valueSet[i]) {
  10566. ++count;
  10567. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10568. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10569. result += GPBComputeRawVarint32SizeForInteger(msgSize) + msgSize;
  10570. }
  10571. }
  10572. size_t tagSize = GPBComputeWireFormatTagSize(GPBFieldNumber(field), GPBDataTypeMessage);
  10573. result += tagSize * count;
  10574. return result;
  10575. }
  10576. - (void)writeToCodedOutputStream:(GPBCodedOutputStream *)outputStream
  10577. asField:(GPBFieldDescriptor *)field {
  10578. GPBDataType valueDataType = GPBGetFieldDataType(field);
  10579. uint32_t tag = GPBWireFormatMakeTag(GPBFieldNumber(field), GPBWireFormatLengthDelimited);
  10580. for (int i = 0; i < 2; ++i) {
  10581. if (_valueSet[i]) {
  10582. // Write the tag.
  10583. [outputStream writeInt32NoTag:tag];
  10584. // Write the size of the message.
  10585. size_t msgSize = ComputeDictBoolFieldSize((i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10586. msgSize += ComputeDictInt32FieldSize(_values[i], kMapValueFieldNumber, valueDataType);
  10587. [outputStream writeInt32NoTag:(int32_t)msgSize];
  10588. // Write the fields.
  10589. WriteDictBoolField(outputStream, (i == 1), kMapKeyFieldNumber, GPBDataTypeBool);
  10590. WriteDictInt32Field(outputStream, _values[i], kMapValueFieldNumber, valueDataType);
  10591. }
  10592. }
  10593. }
  10594. - (void)enumerateForTextFormat:(void (NS_NOESCAPE ^)(id keyObj, id valueObj))block {
  10595. if (_valueSet[0]) {
  10596. block(@"false", @(_values[0]));
  10597. }
  10598. if (_valueSet[1]) {
  10599. block(@"true", @(_values[1]));
  10600. }
  10601. }
  10602. - (void)setGPBGenericValue:(GPBGenericValue *)value
  10603. forGPBGenericValueKey:(GPBGenericValue *)key {
  10604. int idx = (key->valueBool ? 1 : 0);
  10605. _values[idx] = value->valueInt32;
  10606. _valueSet[idx] = YES;
  10607. }
  10608. - (void)addRawEntriesFromDictionary:(GPBBoolEnumDictionary *)otherDictionary {
  10609. if (otherDictionary) {
  10610. for (int i = 0; i < 2; ++i) {
  10611. if (otherDictionary->_valueSet[i]) {
  10612. _valueSet[i] = YES;
  10613. _values[i] = otherDictionary->_values[i];
  10614. }
  10615. }
  10616. if (_autocreator) {
  10617. GPBAutocreatedDictionaryModified(_autocreator, self);
  10618. }
  10619. }
  10620. }
  10621. - (void)setEnum:(int32_t)value forKey:(BOOL)key {
  10622. if (!_validationFunc(value)) {
  10623. [NSException raise:NSInvalidArgumentException
  10624. format:@"GPBBoolEnumDictionary: Attempt to set an unknown enum value (%d)",
  10625. value];
  10626. }
  10627. int idx = (key ? 1 : 0);
  10628. _values[idx] = value;
  10629. _valueSet[idx] = YES;
  10630. if (_autocreator) {
  10631. GPBAutocreatedDictionaryModified(_autocreator, self);
  10632. }
  10633. }
  10634. - (void)setRawValue:(int32_t)rawValue forKey:(BOOL)key {
  10635. int idx = (key ? 1 : 0);
  10636. _values[idx] = rawValue;
  10637. _valueSet[idx] = YES;
  10638. if (_autocreator) {
  10639. GPBAutocreatedDictionaryModified(_autocreator, self);
  10640. }
  10641. }
  10642. - (void)removeEnumForKey:(BOOL)aKey {
  10643. _valueSet[aKey ? 1 : 0] = NO;
  10644. }
  10645. - (void)removeAll {
  10646. _valueSet[0] = NO;
  10647. _valueSet[1] = NO;
  10648. }
  10649. @end
  10650. #pragma mark - NSDictionary Subclass
  10651. @implementation GPBAutocreatedDictionary {
  10652. NSMutableDictionary *_dictionary;
  10653. }
  10654. - (void)dealloc {
  10655. NSAssert(!_autocreator,
  10656. @"%@: Autocreator must be cleared before release, autocreator: %@",
  10657. [self class], _autocreator);
  10658. [_dictionary release];
  10659. [super dealloc];
  10660. }
  10661. #pragma mark Required NSDictionary overrides
  10662. - (instancetype)initWithObjects:(const id [])objects
  10663. forKeys:(const id<NSCopying> [])keys
  10664. count:(NSUInteger)count {
  10665. self = [super init];
  10666. if (self) {
  10667. _dictionary = [[NSMutableDictionary alloc] initWithObjects:objects
  10668. forKeys:keys
  10669. count:count];
  10670. }
  10671. return self;
  10672. }
  10673. - (NSUInteger)count {
  10674. return [_dictionary count];
  10675. }
  10676. - (id)objectForKey:(id)aKey {
  10677. return [_dictionary objectForKey:aKey];
  10678. }
  10679. - (NSEnumerator *)keyEnumerator {
  10680. if (_dictionary == nil) {
  10681. _dictionary = [[NSMutableDictionary alloc] init];
  10682. }
  10683. return [_dictionary keyEnumerator];
  10684. }
  10685. #pragma mark Required NSMutableDictionary overrides
  10686. // Only need to call GPBAutocreatedDictionaryModified() when adding things
  10687. // since we only autocreate empty dictionaries.
  10688. - (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey {
  10689. if (_dictionary == nil) {
  10690. _dictionary = [[NSMutableDictionary alloc] init];
  10691. }
  10692. [_dictionary setObject:anObject forKey:aKey];
  10693. if (_autocreator) {
  10694. GPBAutocreatedDictionaryModified(_autocreator, self);
  10695. }
  10696. }
  10697. - (void)removeObjectForKey:(id)aKey {
  10698. [_dictionary removeObjectForKey:aKey];
  10699. }
  10700. #pragma mark Extra things hooked
  10701. - (id)copyWithZone:(NSZone *)zone {
  10702. if (_dictionary == nil) {
  10703. return [[NSMutableDictionary allocWithZone:zone] init];
  10704. }
  10705. return [_dictionary copyWithZone:zone];
  10706. }
  10707. - (id)mutableCopyWithZone:(NSZone *)zone {
  10708. if (_dictionary == nil) {
  10709. return [[NSMutableDictionary allocWithZone:zone] init];
  10710. }
  10711. return [_dictionary mutableCopyWithZone:zone];
  10712. }
  10713. // Not really needed, but subscripting is likely common enough it doesn't hurt
  10714. // to ensure it goes directly to the real NSMutableDictionary.
  10715. - (id)objectForKeyedSubscript:(id)key {
  10716. return [_dictionary objectForKeyedSubscript:key];
  10717. }
  10718. // Not really needed, but subscripting is likely common enough it doesn't hurt
  10719. // to ensure it goes directly to the real NSMutableDictionary.
  10720. - (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key {
  10721. if (_dictionary == nil) {
  10722. _dictionary = [[NSMutableDictionary alloc] init];
  10723. }
  10724. [_dictionary setObject:obj forKeyedSubscript:key];
  10725. if (_autocreator) {
  10726. GPBAutocreatedDictionaryModified(_autocreator, self);
  10727. }
  10728. }
  10729. - (void)enumerateKeysAndObjectsUsingBlock:(void (NS_NOESCAPE ^)(id key,
  10730. id obj,
  10731. BOOL *stop))block {
  10732. [_dictionary enumerateKeysAndObjectsUsingBlock:block];
  10733. }
  10734. - (void)enumerateKeysAndObjectsWithOptions:(NSEnumerationOptions)opts
  10735. usingBlock:(void (NS_NOESCAPE ^)(id key,
  10736. id obj,
  10737. BOOL *stop))block {
  10738. [_dictionary enumerateKeysAndObjectsWithOptions:opts usingBlock:block];
  10739. }
  10740. @end
  10741. #pragma clang diagnostic pop